file.c 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * vfs operations that deal with files
  5. *
  6. * Copyright (C) International Business Machines Corp., 2002,2010
  7. * Author(s): Steve French ([email protected])
  8. * Jeremy Allison ([email protected])
  9. *
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/backing-dev.h>
  13. #include <linux/stat.h>
  14. #include <linux/fcntl.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/pagevec.h>
  17. #include <linux/writeback.h>
  18. #include <linux/task_io_accounting_ops.h>
  19. #include <linux/delay.h>
  20. #include <linux/mount.h>
  21. #include <linux/slab.h>
  22. #include <linux/swap.h>
  23. #include <linux/mm.h>
  24. #include <asm/div64.h>
  25. #include "cifsfs.h"
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "smb2proto.h"
  30. #include "cifs_unicode.h"
  31. #include "cifs_debug.h"
  32. #include "cifs_fs_sb.h"
  33. #include "fscache.h"
  34. #include "smbdirect.h"
  35. #include "fs_context.h"
  36. #include "cifs_ioctl.h"
  37. #include "cached_dir.h"
  38. /*
  39. * Mark as invalid, all open files on tree connections since they
  40. * were closed when session to server was lost.
  41. */
  42. void
  43. cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
  44. {
  45. struct cifsFileInfo *open_file = NULL;
  46. struct list_head *tmp;
  47. struct list_head *tmp1;
  48. /* only send once per connect */
  49. spin_lock(&tcon->ses->ses_lock);
  50. if ((tcon->ses->ses_status != SES_GOOD) || (tcon->status != TID_NEED_RECON)) {
  51. spin_unlock(&tcon->ses->ses_lock);
  52. return;
  53. }
  54. tcon->status = TID_IN_FILES_INVALIDATE;
  55. spin_unlock(&tcon->ses->ses_lock);
  56. /* list all files open on tree connection and mark them invalid */
  57. spin_lock(&tcon->open_file_lock);
  58. list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
  59. open_file = list_entry(tmp, struct cifsFileInfo, tlist);
  60. open_file->invalidHandle = true;
  61. open_file->oplock_break_cancelled = true;
  62. }
  63. spin_unlock(&tcon->open_file_lock);
  64. invalidate_all_cached_dirs(tcon);
  65. spin_lock(&tcon->tc_lock);
  66. if (tcon->status == TID_IN_FILES_INVALIDATE)
  67. tcon->status = TID_NEED_TCON;
  68. spin_unlock(&tcon->tc_lock);
  69. /*
  70. * BB Add call to invalidate_inodes(sb) for all superblocks mounted
  71. * to this tcon.
  72. */
  73. }
  74. static inline int cifs_convert_flags(unsigned int flags)
  75. {
  76. if ((flags & O_ACCMODE) == O_RDONLY)
  77. return GENERIC_READ;
  78. else if ((flags & O_ACCMODE) == O_WRONLY)
  79. return GENERIC_WRITE;
  80. else if ((flags & O_ACCMODE) == O_RDWR) {
  81. /* GENERIC_ALL is too much permission to request
  82. can cause unnecessary access denied on create */
  83. /* return GENERIC_ALL; */
  84. return (GENERIC_READ | GENERIC_WRITE);
  85. }
  86. return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
  87. FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
  88. FILE_READ_DATA);
  89. }
  90. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  91. static u32 cifs_posix_convert_flags(unsigned int flags)
  92. {
  93. u32 posix_flags = 0;
  94. if ((flags & O_ACCMODE) == O_RDONLY)
  95. posix_flags = SMB_O_RDONLY;
  96. else if ((flags & O_ACCMODE) == O_WRONLY)
  97. posix_flags = SMB_O_WRONLY;
  98. else if ((flags & O_ACCMODE) == O_RDWR)
  99. posix_flags = SMB_O_RDWR;
  100. if (flags & O_CREAT) {
  101. posix_flags |= SMB_O_CREAT;
  102. if (flags & O_EXCL)
  103. posix_flags |= SMB_O_EXCL;
  104. } else if (flags & O_EXCL)
  105. cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
  106. current->comm, current->tgid);
  107. if (flags & O_TRUNC)
  108. posix_flags |= SMB_O_TRUNC;
  109. /* be safe and imply O_SYNC for O_DSYNC */
  110. if (flags & O_DSYNC)
  111. posix_flags |= SMB_O_SYNC;
  112. if (flags & O_DIRECTORY)
  113. posix_flags |= SMB_O_DIRECTORY;
  114. if (flags & O_NOFOLLOW)
  115. posix_flags |= SMB_O_NOFOLLOW;
  116. if (flags & O_DIRECT)
  117. posix_flags |= SMB_O_DIRECT;
  118. return posix_flags;
  119. }
  120. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  121. static inline int cifs_get_disposition(unsigned int flags)
  122. {
  123. if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  124. return FILE_CREATE;
  125. else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  126. return FILE_OVERWRITE_IF;
  127. else if ((flags & O_CREAT) == O_CREAT)
  128. return FILE_OPEN_IF;
  129. else if ((flags & O_TRUNC) == O_TRUNC)
  130. return FILE_OVERWRITE;
  131. else
  132. return FILE_OPEN;
  133. }
  134. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  135. int cifs_posix_open(const char *full_path, struct inode **pinode,
  136. struct super_block *sb, int mode, unsigned int f_flags,
  137. __u32 *poplock, __u16 *pnetfid, unsigned int xid)
  138. {
  139. int rc;
  140. FILE_UNIX_BASIC_INFO *presp_data;
  141. __u32 posix_flags = 0;
  142. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  143. struct cifs_fattr fattr;
  144. struct tcon_link *tlink;
  145. struct cifs_tcon *tcon;
  146. cifs_dbg(FYI, "posix open %s\n", full_path);
  147. presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  148. if (presp_data == NULL)
  149. return -ENOMEM;
  150. tlink = cifs_sb_tlink(cifs_sb);
  151. if (IS_ERR(tlink)) {
  152. rc = PTR_ERR(tlink);
  153. goto posix_open_ret;
  154. }
  155. tcon = tlink_tcon(tlink);
  156. mode &= ~current_umask();
  157. posix_flags = cifs_posix_convert_flags(f_flags);
  158. rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
  159. poplock, full_path, cifs_sb->local_nls,
  160. cifs_remap(cifs_sb));
  161. cifs_put_tlink(tlink);
  162. if (rc)
  163. goto posix_open_ret;
  164. if (presp_data->Type == cpu_to_le32(-1))
  165. goto posix_open_ret; /* open ok, caller does qpathinfo */
  166. if (!pinode)
  167. goto posix_open_ret; /* caller does not need info */
  168. cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
  169. /* get new inode and set it up */
  170. if (*pinode == NULL) {
  171. cifs_fill_uniqueid(sb, &fattr);
  172. *pinode = cifs_iget(sb, &fattr);
  173. if (!*pinode) {
  174. rc = -ENOMEM;
  175. goto posix_open_ret;
  176. }
  177. } else {
  178. cifs_revalidate_mapping(*pinode);
  179. rc = cifs_fattr_to_inode(*pinode, &fattr);
  180. }
  181. posix_open_ret:
  182. kfree(presp_data);
  183. return rc;
  184. }
  185. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  186. static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
  187. struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
  188. struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf)
  189. {
  190. int rc;
  191. int desired_access;
  192. int disposition;
  193. int create_options = CREATE_NOT_DIR;
  194. struct TCP_Server_Info *server = tcon->ses->server;
  195. struct cifs_open_parms oparms;
  196. if (!server->ops->open)
  197. return -ENOSYS;
  198. desired_access = cifs_convert_flags(f_flags);
  199. /*********************************************************************
  200. * open flag mapping table:
  201. *
  202. * POSIX Flag CIFS Disposition
  203. * ---------- ----------------
  204. * O_CREAT FILE_OPEN_IF
  205. * O_CREAT | O_EXCL FILE_CREATE
  206. * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
  207. * O_TRUNC FILE_OVERWRITE
  208. * none of the above FILE_OPEN
  209. *
  210. * Note that there is not a direct match between disposition
  211. * FILE_SUPERSEDE (ie create whether or not file exists although
  212. * O_CREAT | O_TRUNC is similar but truncates the existing
  213. * file rather than creating a new file as FILE_SUPERSEDE does
  214. * (which uses the attributes / metadata passed in on open call)
  215. *?
  216. *? O_SYNC is a reasonable match to CIFS writethrough flag
  217. *? and the read write flags match reasonably. O_LARGEFILE
  218. *? is irrelevant because largefile support is always used
  219. *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
  220. * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
  221. *********************************************************************/
  222. disposition = cifs_get_disposition(f_flags);
  223. /* BB pass O_SYNC flag through on file attributes .. BB */
  224. /* O_SYNC also has bit for O_DSYNC so following check picks up either */
  225. if (f_flags & O_SYNC)
  226. create_options |= CREATE_WRITE_THROUGH;
  227. if (f_flags & O_DIRECT)
  228. create_options |= CREATE_NO_BUFFER;
  229. oparms = (struct cifs_open_parms) {
  230. .tcon = tcon,
  231. .cifs_sb = cifs_sb,
  232. .desired_access = desired_access,
  233. .create_options = cifs_create_options(cifs_sb, create_options),
  234. .disposition = disposition,
  235. .path = full_path,
  236. .fid = fid,
  237. };
  238. rc = server->ops->open(xid, &oparms, oplock, buf);
  239. if (rc)
  240. return rc;
  241. /* TODO: Add support for calling posix query info but with passing in fid */
  242. if (tcon->unix_ext)
  243. rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
  244. xid);
  245. else
  246. rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
  247. xid, fid);
  248. if (rc) {
  249. server->ops->close(xid, tcon, fid);
  250. if (rc == -ESTALE)
  251. rc = -EOPENSTALE;
  252. }
  253. return rc;
  254. }
  255. static bool
  256. cifs_has_mand_locks(struct cifsInodeInfo *cinode)
  257. {
  258. struct cifs_fid_locks *cur;
  259. bool has_locks = false;
  260. down_read(&cinode->lock_sem);
  261. list_for_each_entry(cur, &cinode->llist, llist) {
  262. if (!list_empty(&cur->locks)) {
  263. has_locks = true;
  264. break;
  265. }
  266. }
  267. up_read(&cinode->lock_sem);
  268. return has_locks;
  269. }
  270. void
  271. cifs_down_write(struct rw_semaphore *sem)
  272. {
  273. while (!down_write_trylock(sem))
  274. msleep(10);
  275. }
  276. static void cifsFileInfo_put_work(struct work_struct *work);
  277. struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
  278. struct tcon_link *tlink, __u32 oplock,
  279. const char *symlink_target)
  280. {
  281. struct dentry *dentry = file_dentry(file);
  282. struct inode *inode = d_inode(dentry);
  283. struct cifsInodeInfo *cinode = CIFS_I(inode);
  284. struct cifsFileInfo *cfile;
  285. struct cifs_fid_locks *fdlocks;
  286. struct cifs_tcon *tcon = tlink_tcon(tlink);
  287. struct TCP_Server_Info *server = tcon->ses->server;
  288. cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  289. if (cfile == NULL)
  290. return cfile;
  291. fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
  292. if (!fdlocks) {
  293. kfree(cfile);
  294. return NULL;
  295. }
  296. if (symlink_target) {
  297. cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL);
  298. if (!cfile->symlink_target) {
  299. kfree(fdlocks);
  300. kfree(cfile);
  301. return NULL;
  302. }
  303. }
  304. INIT_LIST_HEAD(&fdlocks->locks);
  305. fdlocks->cfile = cfile;
  306. cfile->llist = fdlocks;
  307. cfile->count = 1;
  308. cfile->pid = current->tgid;
  309. cfile->uid = current_fsuid();
  310. cfile->dentry = dget(dentry);
  311. cfile->f_flags = file->f_flags;
  312. cfile->invalidHandle = false;
  313. cfile->deferred_close_scheduled = false;
  314. cfile->tlink = cifs_get_tlink(tlink);
  315. INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
  316. INIT_WORK(&cfile->put, cifsFileInfo_put_work);
  317. INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
  318. mutex_init(&cfile->fh_mutex);
  319. spin_lock_init(&cfile->file_info_lock);
  320. cifs_sb_active(inode->i_sb);
  321. /*
  322. * If the server returned a read oplock and we have mandatory brlocks,
  323. * set oplock level to None.
  324. */
  325. if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
  326. cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
  327. oplock = 0;
  328. }
  329. cifs_down_write(&cinode->lock_sem);
  330. list_add(&fdlocks->llist, &cinode->llist);
  331. up_write(&cinode->lock_sem);
  332. spin_lock(&tcon->open_file_lock);
  333. if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
  334. oplock = fid->pending_open->oplock;
  335. list_del(&fid->pending_open->olist);
  336. fid->purge_cache = false;
  337. server->ops->set_fid(cfile, fid, oplock);
  338. list_add(&cfile->tlist, &tcon->openFileList);
  339. atomic_inc(&tcon->num_local_opens);
  340. /* if readable file instance put first in list*/
  341. spin_lock(&cinode->open_file_lock);
  342. if (file->f_mode & FMODE_READ)
  343. list_add(&cfile->flist, &cinode->openFileList);
  344. else
  345. list_add_tail(&cfile->flist, &cinode->openFileList);
  346. spin_unlock(&cinode->open_file_lock);
  347. spin_unlock(&tcon->open_file_lock);
  348. if (fid->purge_cache)
  349. cifs_zap_mapping(inode);
  350. file->private_data = cfile;
  351. return cfile;
  352. }
  353. struct cifsFileInfo *
  354. cifsFileInfo_get(struct cifsFileInfo *cifs_file)
  355. {
  356. spin_lock(&cifs_file->file_info_lock);
  357. cifsFileInfo_get_locked(cifs_file);
  358. spin_unlock(&cifs_file->file_info_lock);
  359. return cifs_file;
  360. }
  361. static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
  362. {
  363. struct inode *inode = d_inode(cifs_file->dentry);
  364. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  365. struct cifsLockInfo *li, *tmp;
  366. struct super_block *sb = inode->i_sb;
  367. /*
  368. * Delete any outstanding lock records. We'll lose them when the file
  369. * is closed anyway.
  370. */
  371. cifs_down_write(&cifsi->lock_sem);
  372. list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
  373. list_del(&li->llist);
  374. cifs_del_lock_waiters(li);
  375. kfree(li);
  376. }
  377. list_del(&cifs_file->llist->llist);
  378. kfree(cifs_file->llist);
  379. up_write(&cifsi->lock_sem);
  380. cifs_put_tlink(cifs_file->tlink);
  381. dput(cifs_file->dentry);
  382. cifs_sb_deactive(sb);
  383. kfree(cifs_file->symlink_target);
  384. kfree(cifs_file);
  385. }
  386. static void cifsFileInfo_put_work(struct work_struct *work)
  387. {
  388. struct cifsFileInfo *cifs_file = container_of(work,
  389. struct cifsFileInfo, put);
  390. cifsFileInfo_put_final(cifs_file);
  391. }
  392. /**
  393. * cifsFileInfo_put - release a reference of file priv data
  394. *
  395. * Always potentially wait for oplock handler. See _cifsFileInfo_put().
  396. *
  397. * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
  398. */
  399. void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
  400. {
  401. _cifsFileInfo_put(cifs_file, true, true);
  402. }
  403. /**
  404. * _cifsFileInfo_put - release a reference of file priv data
  405. *
  406. * This may involve closing the filehandle @cifs_file out on the
  407. * server. Must be called without holding tcon->open_file_lock,
  408. * cinode->open_file_lock and cifs_file->file_info_lock.
  409. *
  410. * If @wait_for_oplock_handler is true and we are releasing the last
  411. * reference, wait for any running oplock break handler of the file
  412. * and cancel any pending one.
  413. *
  414. * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
  415. * @wait_oplock_handler: must be false if called from oplock_break_handler
  416. * @offload: not offloaded on close and oplock breaks
  417. *
  418. */
  419. void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
  420. bool wait_oplock_handler, bool offload)
  421. {
  422. struct inode *inode = d_inode(cifs_file->dentry);
  423. struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
  424. struct TCP_Server_Info *server = tcon->ses->server;
  425. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  426. struct super_block *sb = inode->i_sb;
  427. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  428. struct cifs_fid fid = {};
  429. struct cifs_pending_open open;
  430. bool oplock_break_cancelled;
  431. spin_lock(&tcon->open_file_lock);
  432. spin_lock(&cifsi->open_file_lock);
  433. spin_lock(&cifs_file->file_info_lock);
  434. if (--cifs_file->count > 0) {
  435. spin_unlock(&cifs_file->file_info_lock);
  436. spin_unlock(&cifsi->open_file_lock);
  437. spin_unlock(&tcon->open_file_lock);
  438. return;
  439. }
  440. spin_unlock(&cifs_file->file_info_lock);
  441. if (server->ops->get_lease_key)
  442. server->ops->get_lease_key(inode, &fid);
  443. /* store open in pending opens to make sure we don't miss lease break */
  444. cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
  445. /* remove it from the lists */
  446. list_del(&cifs_file->flist);
  447. list_del(&cifs_file->tlist);
  448. atomic_dec(&tcon->num_local_opens);
  449. if (list_empty(&cifsi->openFileList)) {
  450. cifs_dbg(FYI, "closing last open instance for inode %p\n",
  451. d_inode(cifs_file->dentry));
  452. /*
  453. * In strict cache mode we need invalidate mapping on the last
  454. * close because it may cause a error when we open this file
  455. * again and get at least level II oplock.
  456. */
  457. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
  458. set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
  459. cifs_set_oplock_level(cifsi, 0);
  460. }
  461. spin_unlock(&cifsi->open_file_lock);
  462. spin_unlock(&tcon->open_file_lock);
  463. oplock_break_cancelled = wait_oplock_handler ?
  464. cancel_work_sync(&cifs_file->oplock_break) : false;
  465. if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
  466. struct TCP_Server_Info *server = tcon->ses->server;
  467. unsigned int xid;
  468. xid = get_xid();
  469. if (server->ops->close_getattr)
  470. server->ops->close_getattr(xid, tcon, cifs_file);
  471. else if (server->ops->close)
  472. server->ops->close(xid, tcon, &cifs_file->fid);
  473. _free_xid(xid);
  474. }
  475. if (oplock_break_cancelled)
  476. cifs_done_oplock_break(cifsi);
  477. cifs_del_pending_open(&open);
  478. if (offload)
  479. queue_work(fileinfo_put_wq, &cifs_file->put);
  480. else
  481. cifsFileInfo_put_final(cifs_file);
  482. }
  483. int cifs_open(struct inode *inode, struct file *file)
  484. {
  485. int rc = -EACCES;
  486. unsigned int xid;
  487. __u32 oplock;
  488. struct cifs_sb_info *cifs_sb;
  489. struct TCP_Server_Info *server;
  490. struct cifs_tcon *tcon;
  491. struct tcon_link *tlink;
  492. struct cifsFileInfo *cfile = NULL;
  493. void *page;
  494. const char *full_path;
  495. bool posix_open_ok = false;
  496. struct cifs_fid fid = {};
  497. struct cifs_pending_open open;
  498. struct cifs_open_info_data data = {};
  499. xid = get_xid();
  500. cifs_sb = CIFS_SB(inode->i_sb);
  501. if (unlikely(cifs_forced_shutdown(cifs_sb))) {
  502. free_xid(xid);
  503. return -EIO;
  504. }
  505. tlink = cifs_sb_tlink(cifs_sb);
  506. if (IS_ERR(tlink)) {
  507. free_xid(xid);
  508. return PTR_ERR(tlink);
  509. }
  510. tcon = tlink_tcon(tlink);
  511. server = tcon->ses->server;
  512. page = alloc_dentry_path();
  513. full_path = build_path_from_dentry(file_dentry(file), page);
  514. if (IS_ERR(full_path)) {
  515. rc = PTR_ERR(full_path);
  516. goto out;
  517. }
  518. cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
  519. inode, file->f_flags, full_path);
  520. if (file->f_flags & O_DIRECT &&
  521. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  522. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  523. file->f_op = &cifs_file_direct_nobrl_ops;
  524. else
  525. file->f_op = &cifs_file_direct_ops;
  526. }
  527. /* Get the cached handle as SMB2 close is deferred */
  528. rc = cifs_get_readable_path(tcon, full_path, &cfile);
  529. if (rc == 0) {
  530. if (file->f_flags == cfile->f_flags) {
  531. file->private_data = cfile;
  532. spin_lock(&CIFS_I(inode)->deferred_lock);
  533. cifs_del_deferred_close(cfile);
  534. spin_unlock(&CIFS_I(inode)->deferred_lock);
  535. goto use_cache;
  536. } else {
  537. _cifsFileInfo_put(cfile, true, false);
  538. }
  539. }
  540. if (server->oplocks)
  541. oplock = REQ_OPLOCK;
  542. else
  543. oplock = 0;
  544. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  545. if (!tcon->broken_posix_open && tcon->unix_ext &&
  546. cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  547. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  548. /* can not refresh inode info since size could be stale */
  549. rc = cifs_posix_open(full_path, &inode, inode->i_sb,
  550. cifs_sb->ctx->file_mode /* ignored */,
  551. file->f_flags, &oplock, &fid.netfid, xid);
  552. if (rc == 0) {
  553. cifs_dbg(FYI, "posix open succeeded\n");
  554. posix_open_ok = true;
  555. } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  556. if (tcon->ses->serverNOS)
  557. cifs_dbg(VFS, "server %s of type %s returned unexpected error on SMB posix open, disabling posix open support. Check if server update available.\n",
  558. tcon->ses->ip_addr,
  559. tcon->ses->serverNOS);
  560. tcon->broken_posix_open = true;
  561. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  562. (rc != -EOPNOTSUPP)) /* path not found or net err */
  563. goto out;
  564. /*
  565. * Else fallthrough to retry open the old way on network i/o
  566. * or DFS errors.
  567. */
  568. }
  569. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  570. if (server->ops->get_lease_key)
  571. server->ops->get_lease_key(inode, &fid);
  572. cifs_add_pending_open(&fid, tlink, &open);
  573. if (!posix_open_ok) {
  574. if (server->ops->get_lease_key)
  575. server->ops->get_lease_key(inode, &fid);
  576. rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid,
  577. xid, &data);
  578. if (rc) {
  579. cifs_del_pending_open(&open);
  580. goto out;
  581. }
  582. }
  583. cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target);
  584. if (cfile == NULL) {
  585. if (server->ops->close)
  586. server->ops->close(xid, tcon, &fid);
  587. cifs_del_pending_open(&open);
  588. rc = -ENOMEM;
  589. goto out;
  590. }
  591. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  592. if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
  593. /*
  594. * Time to set mode which we can not set earlier due to
  595. * problems creating new read-only files.
  596. */
  597. struct cifs_unix_set_info_args args = {
  598. .mode = inode->i_mode,
  599. .uid = INVALID_UID, /* no change */
  600. .gid = INVALID_GID, /* no change */
  601. .ctime = NO_CHANGE_64,
  602. .atime = NO_CHANGE_64,
  603. .mtime = NO_CHANGE_64,
  604. .device = 0,
  605. };
  606. CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
  607. cfile->pid);
  608. }
  609. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  610. use_cache:
  611. fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
  612. file->f_mode & FMODE_WRITE);
  613. if (file->f_flags & O_DIRECT &&
  614. (!((file->f_flags & O_ACCMODE) != O_RDONLY) ||
  615. file->f_flags & O_APPEND))
  616. cifs_invalidate_cache(file_inode(file),
  617. FSCACHE_INVAL_DIO_WRITE);
  618. out:
  619. free_dentry_path(page);
  620. free_xid(xid);
  621. cifs_put_tlink(tlink);
  622. cifs_free_open_info(&data);
  623. return rc;
  624. }
  625. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  626. static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
  627. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  628. /*
  629. * Try to reacquire byte range locks that were released when session
  630. * to server was lost.
  631. */
  632. static int
  633. cifs_relock_file(struct cifsFileInfo *cfile)
  634. {
  635. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  636. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  637. int rc = 0;
  638. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  639. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  640. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  641. down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
  642. if (cinode->can_cache_brlcks) {
  643. /* can cache locks - no need to relock */
  644. up_read(&cinode->lock_sem);
  645. return rc;
  646. }
  647. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  648. if (cap_unix(tcon->ses) &&
  649. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  650. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  651. rc = cifs_push_posix_locks(cfile);
  652. else
  653. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  654. rc = tcon->ses->server->ops->push_mand_locks(cfile);
  655. up_read(&cinode->lock_sem);
  656. return rc;
  657. }
  658. static int
  659. cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
  660. {
  661. int rc = -EACCES;
  662. unsigned int xid;
  663. __u32 oplock;
  664. struct cifs_sb_info *cifs_sb;
  665. struct cifs_tcon *tcon;
  666. struct TCP_Server_Info *server;
  667. struct cifsInodeInfo *cinode;
  668. struct inode *inode;
  669. void *page;
  670. const char *full_path;
  671. int desired_access;
  672. int disposition = FILE_OPEN;
  673. int create_options = CREATE_NOT_DIR;
  674. struct cifs_open_parms oparms;
  675. xid = get_xid();
  676. mutex_lock(&cfile->fh_mutex);
  677. if (!cfile->invalidHandle) {
  678. mutex_unlock(&cfile->fh_mutex);
  679. free_xid(xid);
  680. return 0;
  681. }
  682. inode = d_inode(cfile->dentry);
  683. cifs_sb = CIFS_SB(inode->i_sb);
  684. tcon = tlink_tcon(cfile->tlink);
  685. server = tcon->ses->server;
  686. /*
  687. * Can not grab rename sem here because various ops, including those
  688. * that already have the rename sem can end up causing writepage to get
  689. * called and if the server was down that means we end up here, and we
  690. * can never tell if the caller already has the rename_sem.
  691. */
  692. page = alloc_dentry_path();
  693. full_path = build_path_from_dentry(cfile->dentry, page);
  694. if (IS_ERR(full_path)) {
  695. mutex_unlock(&cfile->fh_mutex);
  696. free_dentry_path(page);
  697. free_xid(xid);
  698. return PTR_ERR(full_path);
  699. }
  700. cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
  701. inode, cfile->f_flags, full_path);
  702. if (tcon->ses->server->oplocks)
  703. oplock = REQ_OPLOCK;
  704. else
  705. oplock = 0;
  706. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  707. if (tcon->unix_ext && cap_unix(tcon->ses) &&
  708. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  709. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  710. /*
  711. * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
  712. * original open. Must mask them off for a reopen.
  713. */
  714. unsigned int oflags = cfile->f_flags &
  715. ~(O_CREAT | O_EXCL | O_TRUNC);
  716. rc = cifs_posix_open(full_path, NULL, inode->i_sb,
  717. cifs_sb->ctx->file_mode /* ignored */,
  718. oflags, &oplock, &cfile->fid.netfid, xid);
  719. if (rc == 0) {
  720. cifs_dbg(FYI, "posix reopen succeeded\n");
  721. oparms.reconnect = true;
  722. goto reopen_success;
  723. }
  724. /*
  725. * fallthrough to retry open the old way on errors, especially
  726. * in the reconnect path it is important to retry hard
  727. */
  728. }
  729. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  730. desired_access = cifs_convert_flags(cfile->f_flags);
  731. /* O_SYNC also has bit for O_DSYNC so following check picks up either */
  732. if (cfile->f_flags & O_SYNC)
  733. create_options |= CREATE_WRITE_THROUGH;
  734. if (cfile->f_flags & O_DIRECT)
  735. create_options |= CREATE_NO_BUFFER;
  736. if (server->ops->get_lease_key)
  737. server->ops->get_lease_key(inode, &cfile->fid);
  738. oparms = (struct cifs_open_parms) {
  739. .tcon = tcon,
  740. .cifs_sb = cifs_sb,
  741. .desired_access = desired_access,
  742. .create_options = cifs_create_options(cifs_sb, create_options),
  743. .disposition = disposition,
  744. .path = full_path,
  745. .fid = &cfile->fid,
  746. .reconnect = true,
  747. };
  748. /*
  749. * Can not refresh inode by passing in file_info buf to be returned by
  750. * ops->open and then calling get_inode_info with returned buf since
  751. * file might have write behind data that needs to be flushed and server
  752. * version of file size can be stale. If we knew for sure that inode was
  753. * not dirty locally we could do this.
  754. */
  755. rc = server->ops->open(xid, &oparms, &oplock, NULL);
  756. if (rc == -ENOENT && oparms.reconnect == false) {
  757. /* durable handle timeout is expired - open the file again */
  758. rc = server->ops->open(xid, &oparms, &oplock, NULL);
  759. /* indicate that we need to relock the file */
  760. oparms.reconnect = true;
  761. }
  762. if (rc) {
  763. mutex_unlock(&cfile->fh_mutex);
  764. cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
  765. cifs_dbg(FYI, "oplock: %d\n", oplock);
  766. goto reopen_error_exit;
  767. }
  768. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  769. reopen_success:
  770. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  771. cfile->invalidHandle = false;
  772. mutex_unlock(&cfile->fh_mutex);
  773. cinode = CIFS_I(inode);
  774. if (can_flush) {
  775. rc = filemap_write_and_wait(inode->i_mapping);
  776. if (!is_interrupt_error(rc))
  777. mapping_set_error(inode->i_mapping, rc);
  778. if (tcon->posix_extensions)
  779. rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid);
  780. else if (tcon->unix_ext)
  781. rc = cifs_get_inode_info_unix(&inode, full_path,
  782. inode->i_sb, xid);
  783. else
  784. rc = cifs_get_inode_info(&inode, full_path, NULL,
  785. inode->i_sb, xid, NULL);
  786. }
  787. /*
  788. * Else we are writing out data to server already and could deadlock if
  789. * we tried to flush data, and since we do not know if we have data that
  790. * would invalidate the current end of file on the server we can not go
  791. * to the server to get the new inode info.
  792. */
  793. /*
  794. * If the server returned a read oplock and we have mandatory brlocks,
  795. * set oplock level to None.
  796. */
  797. if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
  798. cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
  799. oplock = 0;
  800. }
  801. server->ops->set_fid(cfile, &cfile->fid, oplock);
  802. if (oparms.reconnect)
  803. cifs_relock_file(cfile);
  804. reopen_error_exit:
  805. free_dentry_path(page);
  806. free_xid(xid);
  807. return rc;
  808. }
  809. void smb2_deferred_work_close(struct work_struct *work)
  810. {
  811. struct cifsFileInfo *cfile = container_of(work,
  812. struct cifsFileInfo, deferred.work);
  813. spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
  814. cifs_del_deferred_close(cfile);
  815. cfile->deferred_close_scheduled = false;
  816. spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
  817. _cifsFileInfo_put(cfile, true, false);
  818. }
  819. int cifs_close(struct inode *inode, struct file *file)
  820. {
  821. struct cifsFileInfo *cfile;
  822. struct cifsInodeInfo *cinode = CIFS_I(inode);
  823. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  824. struct cifs_deferred_close *dclose;
  825. cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE);
  826. if (file->private_data != NULL) {
  827. cfile = file->private_data;
  828. file->private_data = NULL;
  829. dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
  830. if ((cifs_sb->ctx->closetimeo && cinode->oplock == CIFS_CACHE_RHW_FLG)
  831. && cinode->lease_granted &&
  832. !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags) &&
  833. dclose) {
  834. if (test_and_clear_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags)) {
  835. inode->i_ctime = inode->i_mtime = current_time(inode);
  836. }
  837. spin_lock(&cinode->deferred_lock);
  838. cifs_add_deferred_close(cfile, dclose);
  839. if (cfile->deferred_close_scheduled &&
  840. delayed_work_pending(&cfile->deferred)) {
  841. /*
  842. * If there is no pending work, mod_delayed_work queues new work.
  843. * So, Increase the ref count to avoid use-after-free.
  844. */
  845. if (!mod_delayed_work(deferredclose_wq,
  846. &cfile->deferred, cifs_sb->ctx->closetimeo))
  847. cifsFileInfo_get(cfile);
  848. } else {
  849. /* Deferred close for files */
  850. queue_delayed_work(deferredclose_wq,
  851. &cfile->deferred, cifs_sb->ctx->closetimeo);
  852. cfile->deferred_close_scheduled = true;
  853. spin_unlock(&cinode->deferred_lock);
  854. return 0;
  855. }
  856. spin_unlock(&cinode->deferred_lock);
  857. _cifsFileInfo_put(cfile, true, false);
  858. } else {
  859. _cifsFileInfo_put(cfile, true, false);
  860. kfree(dclose);
  861. }
  862. }
  863. /* return code from the ->release op is always ignored */
  864. return 0;
  865. }
  866. void
  867. cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
  868. {
  869. struct cifsFileInfo *open_file, *tmp;
  870. struct list_head tmp_list;
  871. if (!tcon->use_persistent || !tcon->need_reopen_files)
  872. return;
  873. tcon->need_reopen_files = false;
  874. cifs_dbg(FYI, "Reopen persistent handles\n");
  875. INIT_LIST_HEAD(&tmp_list);
  876. /* list all files open on tree connection, reopen resilient handles */
  877. spin_lock(&tcon->open_file_lock);
  878. list_for_each_entry(open_file, &tcon->openFileList, tlist) {
  879. if (!open_file->invalidHandle)
  880. continue;
  881. cifsFileInfo_get(open_file);
  882. list_add_tail(&open_file->rlist, &tmp_list);
  883. }
  884. spin_unlock(&tcon->open_file_lock);
  885. list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
  886. if (cifs_reopen_file(open_file, false /* do not flush */))
  887. tcon->need_reopen_files = true;
  888. list_del_init(&open_file->rlist);
  889. cifsFileInfo_put(open_file);
  890. }
  891. }
  892. int cifs_closedir(struct inode *inode, struct file *file)
  893. {
  894. int rc = 0;
  895. unsigned int xid;
  896. struct cifsFileInfo *cfile = file->private_data;
  897. struct cifs_tcon *tcon;
  898. struct TCP_Server_Info *server;
  899. char *buf;
  900. cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
  901. if (cfile == NULL)
  902. return rc;
  903. xid = get_xid();
  904. tcon = tlink_tcon(cfile->tlink);
  905. server = tcon->ses->server;
  906. cifs_dbg(FYI, "Freeing private data in close dir\n");
  907. spin_lock(&cfile->file_info_lock);
  908. if (server->ops->dir_needs_close(cfile)) {
  909. cfile->invalidHandle = true;
  910. spin_unlock(&cfile->file_info_lock);
  911. if (server->ops->close_dir)
  912. rc = server->ops->close_dir(xid, tcon, &cfile->fid);
  913. else
  914. rc = -ENOSYS;
  915. cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
  916. /* not much we can do if it fails anyway, ignore rc */
  917. rc = 0;
  918. } else
  919. spin_unlock(&cfile->file_info_lock);
  920. buf = cfile->srch_inf.ntwrk_buf_start;
  921. if (buf) {
  922. cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
  923. cfile->srch_inf.ntwrk_buf_start = NULL;
  924. if (cfile->srch_inf.smallBuf)
  925. cifs_small_buf_release(buf);
  926. else
  927. cifs_buf_release(buf);
  928. }
  929. cifs_put_tlink(cfile->tlink);
  930. kfree(file->private_data);
  931. file->private_data = NULL;
  932. /* BB can we lock the filestruct while this is going on? */
  933. free_xid(xid);
  934. return rc;
  935. }
  936. static struct cifsLockInfo *
  937. cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
  938. {
  939. struct cifsLockInfo *lock =
  940. kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
  941. if (!lock)
  942. return lock;
  943. lock->offset = offset;
  944. lock->length = length;
  945. lock->type = type;
  946. lock->pid = current->tgid;
  947. lock->flags = flags;
  948. INIT_LIST_HEAD(&lock->blist);
  949. init_waitqueue_head(&lock->block_q);
  950. return lock;
  951. }
  952. void
  953. cifs_del_lock_waiters(struct cifsLockInfo *lock)
  954. {
  955. struct cifsLockInfo *li, *tmp;
  956. list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
  957. list_del_init(&li->blist);
  958. wake_up(&li->block_q);
  959. }
  960. }
  961. #define CIFS_LOCK_OP 0
  962. #define CIFS_READ_OP 1
  963. #define CIFS_WRITE_OP 2
  964. /* @rw_check : 0 - no op, 1 - read, 2 - write */
  965. static bool
  966. cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
  967. __u64 length, __u8 type, __u16 flags,
  968. struct cifsFileInfo *cfile,
  969. struct cifsLockInfo **conf_lock, int rw_check)
  970. {
  971. struct cifsLockInfo *li;
  972. struct cifsFileInfo *cur_cfile = fdlocks->cfile;
  973. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  974. list_for_each_entry(li, &fdlocks->locks, llist) {
  975. if (offset + length <= li->offset ||
  976. offset >= li->offset + li->length)
  977. continue;
  978. if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
  979. server->ops->compare_fids(cfile, cur_cfile)) {
  980. /* shared lock prevents write op through the same fid */
  981. if (!(li->type & server->vals->shared_lock_type) ||
  982. rw_check != CIFS_WRITE_OP)
  983. continue;
  984. }
  985. if ((type & server->vals->shared_lock_type) &&
  986. ((server->ops->compare_fids(cfile, cur_cfile) &&
  987. current->tgid == li->pid) || type == li->type))
  988. continue;
  989. if (rw_check == CIFS_LOCK_OP &&
  990. (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
  991. server->ops->compare_fids(cfile, cur_cfile))
  992. continue;
  993. if (conf_lock)
  994. *conf_lock = li;
  995. return true;
  996. }
  997. return false;
  998. }
  999. bool
  1000. cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
  1001. __u8 type, __u16 flags,
  1002. struct cifsLockInfo **conf_lock, int rw_check)
  1003. {
  1004. bool rc = false;
  1005. struct cifs_fid_locks *cur;
  1006. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1007. list_for_each_entry(cur, &cinode->llist, llist) {
  1008. rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
  1009. flags, cfile, conf_lock,
  1010. rw_check);
  1011. if (rc)
  1012. break;
  1013. }
  1014. return rc;
  1015. }
  1016. /*
  1017. * Check if there is another lock that prevents us to set the lock (mandatory
  1018. * style). If such a lock exists, update the flock structure with its
  1019. * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
  1020. * or leave it the same if we can't. Returns 0 if we don't need to request to
  1021. * the server or 1 otherwise.
  1022. */
  1023. static int
  1024. cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
  1025. __u8 type, struct file_lock *flock)
  1026. {
  1027. int rc = 0;
  1028. struct cifsLockInfo *conf_lock;
  1029. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1030. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  1031. bool exist;
  1032. down_read(&cinode->lock_sem);
  1033. exist = cifs_find_lock_conflict(cfile, offset, length, type,
  1034. flock->fl_flags, &conf_lock,
  1035. CIFS_LOCK_OP);
  1036. if (exist) {
  1037. flock->fl_start = conf_lock->offset;
  1038. flock->fl_end = conf_lock->offset + conf_lock->length - 1;
  1039. flock->fl_pid = conf_lock->pid;
  1040. if (conf_lock->type & server->vals->shared_lock_type)
  1041. flock->fl_type = F_RDLCK;
  1042. else
  1043. flock->fl_type = F_WRLCK;
  1044. } else if (!cinode->can_cache_brlcks)
  1045. rc = 1;
  1046. else
  1047. flock->fl_type = F_UNLCK;
  1048. up_read(&cinode->lock_sem);
  1049. return rc;
  1050. }
  1051. static void
  1052. cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
  1053. {
  1054. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1055. cifs_down_write(&cinode->lock_sem);
  1056. list_add_tail(&lock->llist, &cfile->llist->locks);
  1057. up_write(&cinode->lock_sem);
  1058. }
  1059. /*
  1060. * Set the byte-range lock (mandatory style). Returns:
  1061. * 1) 0, if we set the lock and don't need to request to the server;
  1062. * 2) 1, if no locks prevent us but we need to request to the server;
  1063. * 3) -EACCES, if there is a lock that prevents us and wait is false.
  1064. */
  1065. static int
  1066. cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
  1067. bool wait)
  1068. {
  1069. struct cifsLockInfo *conf_lock;
  1070. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1071. bool exist;
  1072. int rc = 0;
  1073. try_again:
  1074. exist = false;
  1075. cifs_down_write(&cinode->lock_sem);
  1076. exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
  1077. lock->type, lock->flags, &conf_lock,
  1078. CIFS_LOCK_OP);
  1079. if (!exist && cinode->can_cache_brlcks) {
  1080. list_add_tail(&lock->llist, &cfile->llist->locks);
  1081. up_write(&cinode->lock_sem);
  1082. return rc;
  1083. }
  1084. if (!exist)
  1085. rc = 1;
  1086. else if (!wait)
  1087. rc = -EACCES;
  1088. else {
  1089. list_add_tail(&lock->blist, &conf_lock->blist);
  1090. up_write(&cinode->lock_sem);
  1091. rc = wait_event_interruptible(lock->block_q,
  1092. (lock->blist.prev == &lock->blist) &&
  1093. (lock->blist.next == &lock->blist));
  1094. if (!rc)
  1095. goto try_again;
  1096. cifs_down_write(&cinode->lock_sem);
  1097. list_del_init(&lock->blist);
  1098. }
  1099. up_write(&cinode->lock_sem);
  1100. return rc;
  1101. }
  1102. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1103. /*
  1104. * Check if there is another lock that prevents us to set the lock (posix
  1105. * style). If such a lock exists, update the flock structure with its
  1106. * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
  1107. * or leave it the same if we can't. Returns 0 if we don't need to request to
  1108. * the server or 1 otherwise.
  1109. */
  1110. static int
  1111. cifs_posix_lock_test(struct file *file, struct file_lock *flock)
  1112. {
  1113. int rc = 0;
  1114. struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
  1115. unsigned char saved_type = flock->fl_type;
  1116. if ((flock->fl_flags & FL_POSIX) == 0)
  1117. return 1;
  1118. down_read(&cinode->lock_sem);
  1119. posix_test_lock(file, flock);
  1120. if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
  1121. flock->fl_type = saved_type;
  1122. rc = 1;
  1123. }
  1124. up_read(&cinode->lock_sem);
  1125. return rc;
  1126. }
  1127. /*
  1128. * Set the byte-range lock (posix style). Returns:
  1129. * 1) <0, if the error occurs while setting the lock;
  1130. * 2) 0, if we set the lock and don't need to request to the server;
  1131. * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
  1132. * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
  1133. */
  1134. static int
  1135. cifs_posix_lock_set(struct file *file, struct file_lock *flock)
  1136. {
  1137. struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
  1138. int rc = FILE_LOCK_DEFERRED + 1;
  1139. if ((flock->fl_flags & FL_POSIX) == 0)
  1140. return rc;
  1141. cifs_down_write(&cinode->lock_sem);
  1142. if (!cinode->can_cache_brlcks) {
  1143. up_write(&cinode->lock_sem);
  1144. return rc;
  1145. }
  1146. rc = posix_lock_file(file, flock, NULL);
  1147. up_write(&cinode->lock_sem);
  1148. return rc;
  1149. }
  1150. int
  1151. cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
  1152. {
  1153. unsigned int xid;
  1154. int rc = 0, stored_rc;
  1155. struct cifsLockInfo *li, *tmp;
  1156. struct cifs_tcon *tcon;
  1157. unsigned int num, max_num, max_buf;
  1158. LOCKING_ANDX_RANGE *buf, *cur;
  1159. static const int types[] = {
  1160. LOCKING_ANDX_LARGE_FILES,
  1161. LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
  1162. };
  1163. int i;
  1164. xid = get_xid();
  1165. tcon = tlink_tcon(cfile->tlink);
  1166. /*
  1167. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  1168. * and check it before using.
  1169. */
  1170. max_buf = tcon->ses->server->maxBuf;
  1171. if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
  1172. free_xid(xid);
  1173. return -EINVAL;
  1174. }
  1175. BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
  1176. PAGE_SIZE);
  1177. max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
  1178. PAGE_SIZE);
  1179. max_num = (max_buf - sizeof(struct smb_hdr)) /
  1180. sizeof(LOCKING_ANDX_RANGE);
  1181. buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
  1182. if (!buf) {
  1183. free_xid(xid);
  1184. return -ENOMEM;
  1185. }
  1186. for (i = 0; i < 2; i++) {
  1187. cur = buf;
  1188. num = 0;
  1189. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  1190. if (li->type != types[i])
  1191. continue;
  1192. cur->Pid = cpu_to_le16(li->pid);
  1193. cur->LengthLow = cpu_to_le32((u32)li->length);
  1194. cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
  1195. cur->OffsetLow = cpu_to_le32((u32)li->offset);
  1196. cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
  1197. if (++num == max_num) {
  1198. stored_rc = cifs_lockv(xid, tcon,
  1199. cfile->fid.netfid,
  1200. (__u8)li->type, 0, num,
  1201. buf);
  1202. if (stored_rc)
  1203. rc = stored_rc;
  1204. cur = buf;
  1205. num = 0;
  1206. } else
  1207. cur++;
  1208. }
  1209. if (num) {
  1210. stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
  1211. (__u8)types[i], 0, num, buf);
  1212. if (stored_rc)
  1213. rc = stored_rc;
  1214. }
  1215. }
  1216. kfree(buf);
  1217. free_xid(xid);
  1218. return rc;
  1219. }
  1220. static __u32
  1221. hash_lockowner(fl_owner_t owner)
  1222. {
  1223. return cifs_lock_secret ^ hash32_ptr((const void *)owner);
  1224. }
  1225. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1226. struct lock_to_push {
  1227. struct list_head llist;
  1228. __u64 offset;
  1229. __u64 length;
  1230. __u32 pid;
  1231. __u16 netfid;
  1232. __u8 type;
  1233. };
  1234. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1235. static int
  1236. cifs_push_posix_locks(struct cifsFileInfo *cfile)
  1237. {
  1238. struct inode *inode = d_inode(cfile->dentry);
  1239. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1240. struct file_lock *flock;
  1241. struct file_lock_context *flctx = inode->i_flctx;
  1242. unsigned int count = 0, i;
  1243. int rc = 0, xid, type;
  1244. struct list_head locks_to_send, *el;
  1245. struct lock_to_push *lck, *tmp;
  1246. __u64 length;
  1247. xid = get_xid();
  1248. if (!flctx)
  1249. goto out;
  1250. spin_lock(&flctx->flc_lock);
  1251. list_for_each(el, &flctx->flc_posix) {
  1252. count++;
  1253. }
  1254. spin_unlock(&flctx->flc_lock);
  1255. INIT_LIST_HEAD(&locks_to_send);
  1256. /*
  1257. * Allocating count locks is enough because no FL_POSIX locks can be
  1258. * added to the list while we are holding cinode->lock_sem that
  1259. * protects locking operations of this inode.
  1260. */
  1261. for (i = 0; i < count; i++) {
  1262. lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
  1263. if (!lck) {
  1264. rc = -ENOMEM;
  1265. goto err_out;
  1266. }
  1267. list_add_tail(&lck->llist, &locks_to_send);
  1268. }
  1269. el = locks_to_send.next;
  1270. spin_lock(&flctx->flc_lock);
  1271. list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
  1272. if (el == &locks_to_send) {
  1273. /*
  1274. * The list ended. We don't have enough allocated
  1275. * structures - something is really wrong.
  1276. */
  1277. cifs_dbg(VFS, "Can't push all brlocks!\n");
  1278. break;
  1279. }
  1280. length = cifs_flock_len(flock);
  1281. if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
  1282. type = CIFS_RDLCK;
  1283. else
  1284. type = CIFS_WRLCK;
  1285. lck = list_entry(el, struct lock_to_push, llist);
  1286. lck->pid = hash_lockowner(flock->fl_owner);
  1287. lck->netfid = cfile->fid.netfid;
  1288. lck->length = length;
  1289. lck->type = type;
  1290. lck->offset = flock->fl_start;
  1291. }
  1292. spin_unlock(&flctx->flc_lock);
  1293. list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
  1294. int stored_rc;
  1295. stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
  1296. lck->offset, lck->length, NULL,
  1297. lck->type, 0);
  1298. if (stored_rc)
  1299. rc = stored_rc;
  1300. list_del(&lck->llist);
  1301. kfree(lck);
  1302. }
  1303. out:
  1304. free_xid(xid);
  1305. return rc;
  1306. err_out:
  1307. list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
  1308. list_del(&lck->llist);
  1309. kfree(lck);
  1310. }
  1311. goto out;
  1312. }
  1313. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1314. static int
  1315. cifs_push_locks(struct cifsFileInfo *cfile)
  1316. {
  1317. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1318. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1319. int rc = 0;
  1320. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1321. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  1322. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1323. /* we are going to update can_cache_brlcks here - need a write access */
  1324. cifs_down_write(&cinode->lock_sem);
  1325. if (!cinode->can_cache_brlcks) {
  1326. up_write(&cinode->lock_sem);
  1327. return rc;
  1328. }
  1329. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1330. if (cap_unix(tcon->ses) &&
  1331. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  1332. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  1333. rc = cifs_push_posix_locks(cfile);
  1334. else
  1335. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1336. rc = tcon->ses->server->ops->push_mand_locks(cfile);
  1337. cinode->can_cache_brlcks = false;
  1338. up_write(&cinode->lock_sem);
  1339. return rc;
  1340. }
  1341. static void
  1342. cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
  1343. bool *wait_flag, struct TCP_Server_Info *server)
  1344. {
  1345. if (flock->fl_flags & FL_POSIX)
  1346. cifs_dbg(FYI, "Posix\n");
  1347. if (flock->fl_flags & FL_FLOCK)
  1348. cifs_dbg(FYI, "Flock\n");
  1349. if (flock->fl_flags & FL_SLEEP) {
  1350. cifs_dbg(FYI, "Blocking lock\n");
  1351. *wait_flag = true;
  1352. }
  1353. if (flock->fl_flags & FL_ACCESS)
  1354. cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
  1355. if (flock->fl_flags & FL_LEASE)
  1356. cifs_dbg(FYI, "Lease on file - not implemented yet\n");
  1357. if (flock->fl_flags &
  1358. (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
  1359. FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
  1360. cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
  1361. *type = server->vals->large_lock_type;
  1362. if (flock->fl_type == F_WRLCK) {
  1363. cifs_dbg(FYI, "F_WRLCK\n");
  1364. *type |= server->vals->exclusive_lock_type;
  1365. *lock = 1;
  1366. } else if (flock->fl_type == F_UNLCK) {
  1367. cifs_dbg(FYI, "F_UNLCK\n");
  1368. *type |= server->vals->unlock_lock_type;
  1369. *unlock = 1;
  1370. /* Check if unlock includes more than one lock range */
  1371. } else if (flock->fl_type == F_RDLCK) {
  1372. cifs_dbg(FYI, "F_RDLCK\n");
  1373. *type |= server->vals->shared_lock_type;
  1374. *lock = 1;
  1375. } else if (flock->fl_type == F_EXLCK) {
  1376. cifs_dbg(FYI, "F_EXLCK\n");
  1377. *type |= server->vals->exclusive_lock_type;
  1378. *lock = 1;
  1379. } else if (flock->fl_type == F_SHLCK) {
  1380. cifs_dbg(FYI, "F_SHLCK\n");
  1381. *type |= server->vals->shared_lock_type;
  1382. *lock = 1;
  1383. } else
  1384. cifs_dbg(FYI, "Unknown type of lock\n");
  1385. }
  1386. static int
  1387. cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
  1388. bool wait_flag, bool posix_lck, unsigned int xid)
  1389. {
  1390. int rc = 0;
  1391. __u64 length = cifs_flock_len(flock);
  1392. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  1393. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1394. struct TCP_Server_Info *server = tcon->ses->server;
  1395. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1396. __u16 netfid = cfile->fid.netfid;
  1397. if (posix_lck) {
  1398. int posix_lock_type;
  1399. rc = cifs_posix_lock_test(file, flock);
  1400. if (!rc)
  1401. return rc;
  1402. if (type & server->vals->shared_lock_type)
  1403. posix_lock_type = CIFS_RDLCK;
  1404. else
  1405. posix_lock_type = CIFS_WRLCK;
  1406. rc = CIFSSMBPosixLock(xid, tcon, netfid,
  1407. hash_lockowner(flock->fl_owner),
  1408. flock->fl_start, length, flock,
  1409. posix_lock_type, wait_flag);
  1410. return rc;
  1411. }
  1412. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1413. rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
  1414. if (!rc)
  1415. return rc;
  1416. /* BB we could chain these into one lock request BB */
  1417. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
  1418. 1, 0, false);
  1419. if (rc == 0) {
  1420. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1421. type, 0, 1, false);
  1422. flock->fl_type = F_UNLCK;
  1423. if (rc != 0)
  1424. cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
  1425. rc);
  1426. return 0;
  1427. }
  1428. if (type & server->vals->shared_lock_type) {
  1429. flock->fl_type = F_WRLCK;
  1430. return 0;
  1431. }
  1432. type &= ~server->vals->exclusive_lock_type;
  1433. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1434. type | server->vals->shared_lock_type,
  1435. 1, 0, false);
  1436. if (rc == 0) {
  1437. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1438. type | server->vals->shared_lock_type, 0, 1, false);
  1439. flock->fl_type = F_RDLCK;
  1440. if (rc != 0)
  1441. cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
  1442. rc);
  1443. } else
  1444. flock->fl_type = F_WRLCK;
  1445. return 0;
  1446. }
  1447. void
  1448. cifs_move_llist(struct list_head *source, struct list_head *dest)
  1449. {
  1450. struct list_head *li, *tmp;
  1451. list_for_each_safe(li, tmp, source)
  1452. list_move(li, dest);
  1453. }
  1454. void
  1455. cifs_free_llist(struct list_head *llist)
  1456. {
  1457. struct cifsLockInfo *li, *tmp;
  1458. list_for_each_entry_safe(li, tmp, llist, llist) {
  1459. cifs_del_lock_waiters(li);
  1460. list_del(&li->llist);
  1461. kfree(li);
  1462. }
  1463. }
  1464. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1465. int
  1466. cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
  1467. unsigned int xid)
  1468. {
  1469. int rc = 0, stored_rc;
  1470. static const int types[] = {
  1471. LOCKING_ANDX_LARGE_FILES,
  1472. LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
  1473. };
  1474. unsigned int i;
  1475. unsigned int max_num, num, max_buf;
  1476. LOCKING_ANDX_RANGE *buf, *cur;
  1477. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1478. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1479. struct cifsLockInfo *li, *tmp;
  1480. __u64 length = cifs_flock_len(flock);
  1481. struct list_head tmp_llist;
  1482. INIT_LIST_HEAD(&tmp_llist);
  1483. /*
  1484. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  1485. * and check it before using.
  1486. */
  1487. max_buf = tcon->ses->server->maxBuf;
  1488. if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
  1489. return -EINVAL;
  1490. BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
  1491. PAGE_SIZE);
  1492. max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
  1493. PAGE_SIZE);
  1494. max_num = (max_buf - sizeof(struct smb_hdr)) /
  1495. sizeof(LOCKING_ANDX_RANGE);
  1496. buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
  1497. if (!buf)
  1498. return -ENOMEM;
  1499. cifs_down_write(&cinode->lock_sem);
  1500. for (i = 0; i < 2; i++) {
  1501. cur = buf;
  1502. num = 0;
  1503. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  1504. if (flock->fl_start > li->offset ||
  1505. (flock->fl_start + length) <
  1506. (li->offset + li->length))
  1507. continue;
  1508. if (current->tgid != li->pid)
  1509. continue;
  1510. if (types[i] != li->type)
  1511. continue;
  1512. if (cinode->can_cache_brlcks) {
  1513. /*
  1514. * We can cache brlock requests - simply remove
  1515. * a lock from the file's list.
  1516. */
  1517. list_del(&li->llist);
  1518. cifs_del_lock_waiters(li);
  1519. kfree(li);
  1520. continue;
  1521. }
  1522. cur->Pid = cpu_to_le16(li->pid);
  1523. cur->LengthLow = cpu_to_le32((u32)li->length);
  1524. cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
  1525. cur->OffsetLow = cpu_to_le32((u32)li->offset);
  1526. cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
  1527. /*
  1528. * We need to save a lock here to let us add it again to
  1529. * the file's list if the unlock range request fails on
  1530. * the server.
  1531. */
  1532. list_move(&li->llist, &tmp_llist);
  1533. if (++num == max_num) {
  1534. stored_rc = cifs_lockv(xid, tcon,
  1535. cfile->fid.netfid,
  1536. li->type, num, 0, buf);
  1537. if (stored_rc) {
  1538. /*
  1539. * We failed on the unlock range
  1540. * request - add all locks from the tmp
  1541. * list to the head of the file's list.
  1542. */
  1543. cifs_move_llist(&tmp_llist,
  1544. &cfile->llist->locks);
  1545. rc = stored_rc;
  1546. } else
  1547. /*
  1548. * The unlock range request succeed -
  1549. * free the tmp list.
  1550. */
  1551. cifs_free_llist(&tmp_llist);
  1552. cur = buf;
  1553. num = 0;
  1554. } else
  1555. cur++;
  1556. }
  1557. if (num) {
  1558. stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
  1559. types[i], num, 0, buf);
  1560. if (stored_rc) {
  1561. cifs_move_llist(&tmp_llist,
  1562. &cfile->llist->locks);
  1563. rc = stored_rc;
  1564. } else
  1565. cifs_free_llist(&tmp_llist);
  1566. }
  1567. }
  1568. up_write(&cinode->lock_sem);
  1569. kfree(buf);
  1570. return rc;
  1571. }
  1572. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1573. static int
  1574. cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
  1575. bool wait_flag, bool posix_lck, int lock, int unlock,
  1576. unsigned int xid)
  1577. {
  1578. int rc = 0;
  1579. __u64 length = cifs_flock_len(flock);
  1580. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  1581. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1582. struct TCP_Server_Info *server = tcon->ses->server;
  1583. struct inode *inode = d_inode(cfile->dentry);
  1584. #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
  1585. if (posix_lck) {
  1586. int posix_lock_type;
  1587. rc = cifs_posix_lock_set(file, flock);
  1588. if (rc <= FILE_LOCK_DEFERRED)
  1589. return rc;
  1590. if (type & server->vals->shared_lock_type)
  1591. posix_lock_type = CIFS_RDLCK;
  1592. else
  1593. posix_lock_type = CIFS_WRLCK;
  1594. if (unlock == 1)
  1595. posix_lock_type = CIFS_UNLCK;
  1596. rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
  1597. hash_lockowner(flock->fl_owner),
  1598. flock->fl_start, length,
  1599. NULL, posix_lock_type, wait_flag);
  1600. goto out;
  1601. }
  1602. #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
  1603. if (lock) {
  1604. struct cifsLockInfo *lock;
  1605. lock = cifs_lock_init(flock->fl_start, length, type,
  1606. flock->fl_flags);
  1607. if (!lock)
  1608. return -ENOMEM;
  1609. rc = cifs_lock_add_if(cfile, lock, wait_flag);
  1610. if (rc < 0) {
  1611. kfree(lock);
  1612. return rc;
  1613. }
  1614. if (!rc)
  1615. goto out;
  1616. /*
  1617. * Windows 7 server can delay breaking lease from read to None
  1618. * if we set a byte-range lock on a file - break it explicitly
  1619. * before sending the lock to the server to be sure the next
  1620. * read won't conflict with non-overlapted locks due to
  1621. * pagereading.
  1622. */
  1623. if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
  1624. CIFS_CACHE_READ(CIFS_I(inode))) {
  1625. cifs_zap_mapping(inode);
  1626. cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
  1627. inode);
  1628. CIFS_I(inode)->oplock = 0;
  1629. }
  1630. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1631. type, 1, 0, wait_flag);
  1632. if (rc) {
  1633. kfree(lock);
  1634. return rc;
  1635. }
  1636. cifs_lock_add(cfile, lock);
  1637. } else if (unlock)
  1638. rc = server->ops->mand_unlock_range(cfile, flock, xid);
  1639. out:
  1640. if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
  1641. /*
  1642. * If this is a request to remove all locks because we
  1643. * are closing the file, it doesn't matter if the
  1644. * unlocking failed as both cifs.ko and the SMB server
  1645. * remove the lock on file close
  1646. */
  1647. if (rc) {
  1648. cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
  1649. if (!(flock->fl_flags & FL_CLOSE))
  1650. return rc;
  1651. }
  1652. rc = locks_lock_file_wait(file, flock);
  1653. }
  1654. return rc;
  1655. }
  1656. int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
  1657. {
  1658. int rc, xid;
  1659. int lock = 0, unlock = 0;
  1660. bool wait_flag = false;
  1661. bool posix_lck = false;
  1662. struct cifs_sb_info *cifs_sb;
  1663. struct cifs_tcon *tcon;
  1664. struct cifsFileInfo *cfile;
  1665. __u32 type;
  1666. xid = get_xid();
  1667. if (!(fl->fl_flags & FL_FLOCK)) {
  1668. rc = -ENOLCK;
  1669. free_xid(xid);
  1670. return rc;
  1671. }
  1672. cfile = (struct cifsFileInfo *)file->private_data;
  1673. tcon = tlink_tcon(cfile->tlink);
  1674. cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
  1675. tcon->ses->server);
  1676. cifs_sb = CIFS_FILE_SB(file);
  1677. if (cap_unix(tcon->ses) &&
  1678. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  1679. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  1680. posix_lck = true;
  1681. if (!lock && !unlock) {
  1682. /*
  1683. * if no lock or unlock then nothing to do since we do not
  1684. * know what it is
  1685. */
  1686. rc = -EOPNOTSUPP;
  1687. free_xid(xid);
  1688. return rc;
  1689. }
  1690. rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
  1691. xid);
  1692. free_xid(xid);
  1693. return rc;
  1694. }
  1695. int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
  1696. {
  1697. int rc, xid;
  1698. int lock = 0, unlock = 0;
  1699. bool wait_flag = false;
  1700. bool posix_lck = false;
  1701. struct cifs_sb_info *cifs_sb;
  1702. struct cifs_tcon *tcon;
  1703. struct cifsFileInfo *cfile;
  1704. __u32 type;
  1705. rc = -EACCES;
  1706. xid = get_xid();
  1707. cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd,
  1708. flock->fl_flags, flock->fl_type, (long long)flock->fl_start,
  1709. (long long)flock->fl_end);
  1710. cfile = (struct cifsFileInfo *)file->private_data;
  1711. tcon = tlink_tcon(cfile->tlink);
  1712. cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
  1713. tcon->ses->server);
  1714. cifs_sb = CIFS_FILE_SB(file);
  1715. set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
  1716. if (cap_unix(tcon->ses) &&
  1717. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  1718. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  1719. posix_lck = true;
  1720. /*
  1721. * BB add code here to normalize offset and length to account for
  1722. * negative length which we can not accept over the wire.
  1723. */
  1724. if (IS_GETLK(cmd)) {
  1725. rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
  1726. free_xid(xid);
  1727. return rc;
  1728. }
  1729. if (!lock && !unlock) {
  1730. /*
  1731. * if no lock or unlock then nothing to do since we do not
  1732. * know what it is
  1733. */
  1734. free_xid(xid);
  1735. return -EOPNOTSUPP;
  1736. }
  1737. rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
  1738. xid);
  1739. free_xid(xid);
  1740. return rc;
  1741. }
  1742. /*
  1743. * update the file size (if needed) after a write. Should be called with
  1744. * the inode->i_lock held
  1745. */
  1746. void
  1747. cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
  1748. unsigned int bytes_written)
  1749. {
  1750. loff_t end_of_write = offset + bytes_written;
  1751. if (end_of_write > cifsi->server_eof)
  1752. cifsi->server_eof = end_of_write;
  1753. }
  1754. static ssize_t
  1755. cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
  1756. size_t write_size, loff_t *offset)
  1757. {
  1758. int rc = 0;
  1759. unsigned int bytes_written = 0;
  1760. unsigned int total_written;
  1761. struct cifs_tcon *tcon;
  1762. struct TCP_Server_Info *server;
  1763. unsigned int xid;
  1764. struct dentry *dentry = open_file->dentry;
  1765. struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
  1766. struct cifs_io_parms io_parms = {0};
  1767. cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
  1768. write_size, *offset, dentry);
  1769. tcon = tlink_tcon(open_file->tlink);
  1770. server = tcon->ses->server;
  1771. if (!server->ops->sync_write)
  1772. return -ENOSYS;
  1773. xid = get_xid();
  1774. for (total_written = 0; write_size > total_written;
  1775. total_written += bytes_written) {
  1776. rc = -EAGAIN;
  1777. while (rc == -EAGAIN) {
  1778. struct kvec iov[2];
  1779. unsigned int len;
  1780. if (open_file->invalidHandle) {
  1781. /* we could deadlock if we called
  1782. filemap_fdatawait from here so tell
  1783. reopen_file not to flush data to
  1784. server now */
  1785. rc = cifs_reopen_file(open_file, false);
  1786. if (rc != 0)
  1787. break;
  1788. }
  1789. len = min(server->ops->wp_retry_size(d_inode(dentry)),
  1790. (unsigned int)write_size - total_written);
  1791. /* iov[0] is reserved for smb header */
  1792. iov[1].iov_base = (char *)write_data + total_written;
  1793. iov[1].iov_len = len;
  1794. io_parms.pid = pid;
  1795. io_parms.tcon = tcon;
  1796. io_parms.offset = *offset;
  1797. io_parms.length = len;
  1798. rc = server->ops->sync_write(xid, &open_file->fid,
  1799. &io_parms, &bytes_written, iov, 1);
  1800. }
  1801. if (rc || (bytes_written == 0)) {
  1802. if (total_written)
  1803. break;
  1804. else {
  1805. free_xid(xid);
  1806. return rc;
  1807. }
  1808. } else {
  1809. spin_lock(&d_inode(dentry)->i_lock);
  1810. cifs_update_eof(cifsi, *offset, bytes_written);
  1811. spin_unlock(&d_inode(dentry)->i_lock);
  1812. *offset += bytes_written;
  1813. }
  1814. }
  1815. cifs_stats_bytes_written(tcon, total_written);
  1816. if (total_written > 0) {
  1817. spin_lock(&d_inode(dentry)->i_lock);
  1818. if (*offset > d_inode(dentry)->i_size) {
  1819. i_size_write(d_inode(dentry), *offset);
  1820. d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9;
  1821. }
  1822. spin_unlock(&d_inode(dentry)->i_lock);
  1823. }
  1824. mark_inode_dirty_sync(d_inode(dentry));
  1825. free_xid(xid);
  1826. return total_written;
  1827. }
  1828. struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
  1829. bool fsuid_only)
  1830. {
  1831. struct cifsFileInfo *open_file = NULL;
  1832. struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
  1833. /* only filter by fsuid on multiuser mounts */
  1834. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  1835. fsuid_only = false;
  1836. spin_lock(&cifs_inode->open_file_lock);
  1837. /* we could simply get the first_list_entry since write-only entries
  1838. are always at the end of the list but since the first entry might
  1839. have a close pending, we go through the whole list */
  1840. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1841. if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
  1842. continue;
  1843. if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
  1844. if ((!open_file->invalidHandle)) {
  1845. /* found a good file */
  1846. /* lock it so it will not be closed on us */
  1847. cifsFileInfo_get(open_file);
  1848. spin_unlock(&cifs_inode->open_file_lock);
  1849. return open_file;
  1850. } /* else might as well continue, and look for
  1851. another, or simply have the caller reopen it
  1852. again rather than trying to fix this handle */
  1853. } else /* write only file */
  1854. break; /* write only files are last so must be done */
  1855. }
  1856. spin_unlock(&cifs_inode->open_file_lock);
  1857. return NULL;
  1858. }
  1859. /* Return -EBADF if no handle is found and general rc otherwise */
  1860. int
  1861. cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
  1862. struct cifsFileInfo **ret_file)
  1863. {
  1864. struct cifsFileInfo *open_file, *inv_file = NULL;
  1865. struct cifs_sb_info *cifs_sb;
  1866. bool any_available = false;
  1867. int rc = -EBADF;
  1868. unsigned int refind = 0;
  1869. bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
  1870. bool with_delete = flags & FIND_WR_WITH_DELETE;
  1871. *ret_file = NULL;
  1872. /*
  1873. * Having a null inode here (because mapping->host was set to zero by
  1874. * the VFS or MM) should not happen but we had reports of on oops (due
  1875. * to it being zero) during stress testcases so we need to check for it
  1876. */
  1877. if (cifs_inode == NULL) {
  1878. cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
  1879. dump_stack();
  1880. return rc;
  1881. }
  1882. cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
  1883. /* only filter by fsuid on multiuser mounts */
  1884. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  1885. fsuid_only = false;
  1886. spin_lock(&cifs_inode->open_file_lock);
  1887. refind_writable:
  1888. if (refind > MAX_REOPEN_ATT) {
  1889. spin_unlock(&cifs_inode->open_file_lock);
  1890. return rc;
  1891. }
  1892. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1893. if (!any_available && open_file->pid != current->tgid)
  1894. continue;
  1895. if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
  1896. continue;
  1897. if (with_delete && !(open_file->fid.access & DELETE))
  1898. continue;
  1899. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  1900. if (!open_file->invalidHandle) {
  1901. /* found a good writable file */
  1902. cifsFileInfo_get(open_file);
  1903. spin_unlock(&cifs_inode->open_file_lock);
  1904. *ret_file = open_file;
  1905. return 0;
  1906. } else {
  1907. if (!inv_file)
  1908. inv_file = open_file;
  1909. }
  1910. }
  1911. }
  1912. /* couldn't find useable FH with same pid, try any available */
  1913. if (!any_available) {
  1914. any_available = true;
  1915. goto refind_writable;
  1916. }
  1917. if (inv_file) {
  1918. any_available = false;
  1919. cifsFileInfo_get(inv_file);
  1920. }
  1921. spin_unlock(&cifs_inode->open_file_lock);
  1922. if (inv_file) {
  1923. rc = cifs_reopen_file(inv_file, false);
  1924. if (!rc) {
  1925. *ret_file = inv_file;
  1926. return 0;
  1927. }
  1928. spin_lock(&cifs_inode->open_file_lock);
  1929. list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
  1930. spin_unlock(&cifs_inode->open_file_lock);
  1931. cifsFileInfo_put(inv_file);
  1932. ++refind;
  1933. inv_file = NULL;
  1934. spin_lock(&cifs_inode->open_file_lock);
  1935. goto refind_writable;
  1936. }
  1937. return rc;
  1938. }
  1939. struct cifsFileInfo *
  1940. find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
  1941. {
  1942. struct cifsFileInfo *cfile;
  1943. int rc;
  1944. rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
  1945. if (rc)
  1946. cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
  1947. return cfile;
  1948. }
  1949. int
  1950. cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
  1951. int flags,
  1952. struct cifsFileInfo **ret_file)
  1953. {
  1954. struct cifsFileInfo *cfile;
  1955. void *page = alloc_dentry_path();
  1956. *ret_file = NULL;
  1957. spin_lock(&tcon->open_file_lock);
  1958. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  1959. struct cifsInodeInfo *cinode;
  1960. const char *full_path = build_path_from_dentry(cfile->dentry, page);
  1961. if (IS_ERR(full_path)) {
  1962. spin_unlock(&tcon->open_file_lock);
  1963. free_dentry_path(page);
  1964. return PTR_ERR(full_path);
  1965. }
  1966. if (strcmp(full_path, name))
  1967. continue;
  1968. cinode = CIFS_I(d_inode(cfile->dentry));
  1969. spin_unlock(&tcon->open_file_lock);
  1970. free_dentry_path(page);
  1971. return cifs_get_writable_file(cinode, flags, ret_file);
  1972. }
  1973. spin_unlock(&tcon->open_file_lock);
  1974. free_dentry_path(page);
  1975. return -ENOENT;
  1976. }
  1977. int
  1978. cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
  1979. struct cifsFileInfo **ret_file)
  1980. {
  1981. struct cifsFileInfo *cfile;
  1982. void *page = alloc_dentry_path();
  1983. *ret_file = NULL;
  1984. spin_lock(&tcon->open_file_lock);
  1985. list_for_each_entry(cfile, &tcon->openFileList, tlist) {
  1986. struct cifsInodeInfo *cinode;
  1987. const char *full_path = build_path_from_dentry(cfile->dentry, page);
  1988. if (IS_ERR(full_path)) {
  1989. spin_unlock(&tcon->open_file_lock);
  1990. free_dentry_path(page);
  1991. return PTR_ERR(full_path);
  1992. }
  1993. if (strcmp(full_path, name))
  1994. continue;
  1995. cinode = CIFS_I(d_inode(cfile->dentry));
  1996. spin_unlock(&tcon->open_file_lock);
  1997. free_dentry_path(page);
  1998. *ret_file = find_readable_file(cinode, 0);
  1999. return *ret_file ? 0 : -ENOENT;
  2000. }
  2001. spin_unlock(&tcon->open_file_lock);
  2002. free_dentry_path(page);
  2003. return -ENOENT;
  2004. }
  2005. void
  2006. cifs_writedata_release(struct kref *refcount)
  2007. {
  2008. struct cifs_writedata *wdata = container_of(refcount,
  2009. struct cifs_writedata, refcount);
  2010. #ifdef CONFIG_CIFS_SMB_DIRECT
  2011. if (wdata->mr) {
  2012. smbd_deregister_mr(wdata->mr);
  2013. wdata->mr = NULL;
  2014. }
  2015. #endif
  2016. if (wdata->cfile)
  2017. cifsFileInfo_put(wdata->cfile);
  2018. kvfree(wdata->pages);
  2019. kfree(wdata);
  2020. }
  2021. /*
  2022. * Write failed with a retryable error. Resend the write request. It's also
  2023. * possible that the page was redirtied so re-clean the page.
  2024. */
  2025. static void
  2026. cifs_writev_requeue(struct cifs_writedata *wdata)
  2027. {
  2028. int i, rc = 0;
  2029. struct inode *inode = d_inode(wdata->cfile->dentry);
  2030. struct TCP_Server_Info *server;
  2031. unsigned int rest_len;
  2032. server = tlink_tcon(wdata->cfile->tlink)->ses->server;
  2033. i = 0;
  2034. rest_len = wdata->bytes;
  2035. do {
  2036. struct cifs_writedata *wdata2;
  2037. unsigned int j, nr_pages, wsize, tailsz, cur_len;
  2038. wsize = server->ops->wp_retry_size(inode);
  2039. if (wsize < rest_len) {
  2040. nr_pages = wsize / PAGE_SIZE;
  2041. if (!nr_pages) {
  2042. rc = -EOPNOTSUPP;
  2043. break;
  2044. }
  2045. cur_len = nr_pages * PAGE_SIZE;
  2046. tailsz = PAGE_SIZE;
  2047. } else {
  2048. nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
  2049. cur_len = rest_len;
  2050. tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
  2051. }
  2052. wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
  2053. if (!wdata2) {
  2054. rc = -ENOMEM;
  2055. break;
  2056. }
  2057. for (j = 0; j < nr_pages; j++) {
  2058. wdata2->pages[j] = wdata->pages[i + j];
  2059. lock_page(wdata2->pages[j]);
  2060. clear_page_dirty_for_io(wdata2->pages[j]);
  2061. }
  2062. wdata2->sync_mode = wdata->sync_mode;
  2063. wdata2->nr_pages = nr_pages;
  2064. wdata2->offset = page_offset(wdata2->pages[0]);
  2065. wdata2->pagesz = PAGE_SIZE;
  2066. wdata2->tailsz = tailsz;
  2067. wdata2->bytes = cur_len;
  2068. rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY,
  2069. &wdata2->cfile);
  2070. if (!wdata2->cfile) {
  2071. cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
  2072. rc);
  2073. if (!is_retryable_error(rc))
  2074. rc = -EBADF;
  2075. } else {
  2076. wdata2->pid = wdata2->cfile->pid;
  2077. rc = server->ops->async_writev(wdata2,
  2078. cifs_writedata_release);
  2079. }
  2080. for (j = 0; j < nr_pages; j++) {
  2081. unlock_page(wdata2->pages[j]);
  2082. if (rc != 0 && !is_retryable_error(rc)) {
  2083. SetPageError(wdata2->pages[j]);
  2084. end_page_writeback(wdata2->pages[j]);
  2085. put_page(wdata2->pages[j]);
  2086. }
  2087. }
  2088. kref_put(&wdata2->refcount, cifs_writedata_release);
  2089. if (rc) {
  2090. if (is_retryable_error(rc))
  2091. continue;
  2092. i += nr_pages;
  2093. break;
  2094. }
  2095. rest_len -= cur_len;
  2096. i += nr_pages;
  2097. } while (i < wdata->nr_pages);
  2098. /* cleanup remaining pages from the original wdata */
  2099. for (; i < wdata->nr_pages; i++) {
  2100. SetPageError(wdata->pages[i]);
  2101. end_page_writeback(wdata->pages[i]);
  2102. put_page(wdata->pages[i]);
  2103. }
  2104. if (rc != 0 && !is_retryable_error(rc))
  2105. mapping_set_error(inode->i_mapping, rc);
  2106. kref_put(&wdata->refcount, cifs_writedata_release);
  2107. }
  2108. void
  2109. cifs_writev_complete(struct work_struct *work)
  2110. {
  2111. struct cifs_writedata *wdata = container_of(work,
  2112. struct cifs_writedata, work);
  2113. struct inode *inode = d_inode(wdata->cfile->dentry);
  2114. int i = 0;
  2115. if (wdata->result == 0) {
  2116. spin_lock(&inode->i_lock);
  2117. cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
  2118. spin_unlock(&inode->i_lock);
  2119. cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
  2120. wdata->bytes);
  2121. } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
  2122. return cifs_writev_requeue(wdata);
  2123. for (i = 0; i < wdata->nr_pages; i++) {
  2124. struct page *page = wdata->pages[i];
  2125. if (wdata->result == -EAGAIN)
  2126. __set_page_dirty_nobuffers(page);
  2127. else if (wdata->result < 0)
  2128. SetPageError(page);
  2129. end_page_writeback(page);
  2130. cifs_readpage_to_fscache(inode, page);
  2131. put_page(page);
  2132. }
  2133. if (wdata->result != -EAGAIN)
  2134. mapping_set_error(inode->i_mapping, wdata->result);
  2135. kref_put(&wdata->refcount, cifs_writedata_release);
  2136. }
  2137. struct cifs_writedata *
  2138. cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
  2139. {
  2140. struct cifs_writedata *writedata = NULL;
  2141. struct page **pages =
  2142. kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
  2143. if (pages) {
  2144. writedata = cifs_writedata_direct_alloc(pages, complete);
  2145. if (!writedata)
  2146. kvfree(pages);
  2147. }
  2148. return writedata;
  2149. }
  2150. struct cifs_writedata *
  2151. cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
  2152. {
  2153. struct cifs_writedata *wdata;
  2154. wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
  2155. if (wdata != NULL) {
  2156. wdata->pages = pages;
  2157. kref_init(&wdata->refcount);
  2158. INIT_LIST_HEAD(&wdata->list);
  2159. init_completion(&wdata->done);
  2160. INIT_WORK(&wdata->work, complete);
  2161. }
  2162. return wdata;
  2163. }
  2164. static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
  2165. {
  2166. struct address_space *mapping = page->mapping;
  2167. loff_t offset = (loff_t)page->index << PAGE_SHIFT;
  2168. char *write_data;
  2169. int rc = -EFAULT;
  2170. int bytes_written = 0;
  2171. struct inode *inode;
  2172. struct cifsFileInfo *open_file;
  2173. if (!mapping || !mapping->host)
  2174. return -EFAULT;
  2175. inode = page->mapping->host;
  2176. offset += (loff_t)from;
  2177. write_data = kmap(page);
  2178. write_data += from;
  2179. if ((to > PAGE_SIZE) || (from > to)) {
  2180. kunmap(page);
  2181. return -EIO;
  2182. }
  2183. /* racing with truncate? */
  2184. if (offset > mapping->host->i_size) {
  2185. kunmap(page);
  2186. return 0; /* don't care */
  2187. }
  2188. /* check to make sure that we are not extending the file */
  2189. if (mapping->host->i_size - offset < (loff_t)to)
  2190. to = (unsigned)(mapping->host->i_size - offset);
  2191. rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
  2192. &open_file);
  2193. if (!rc) {
  2194. bytes_written = cifs_write(open_file, open_file->pid,
  2195. write_data, to - from, &offset);
  2196. cifsFileInfo_put(open_file);
  2197. /* Does mm or vfs already set times? */
  2198. inode->i_atime = inode->i_mtime = current_time(inode);
  2199. if ((bytes_written > 0) && (offset))
  2200. rc = 0;
  2201. else if (bytes_written < 0)
  2202. rc = bytes_written;
  2203. else
  2204. rc = -EFAULT;
  2205. } else {
  2206. cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
  2207. if (!is_retryable_error(rc))
  2208. rc = -EIO;
  2209. }
  2210. kunmap(page);
  2211. return rc;
  2212. }
  2213. static struct cifs_writedata *
  2214. wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
  2215. pgoff_t end, pgoff_t *index,
  2216. unsigned int *found_pages)
  2217. {
  2218. struct cifs_writedata *wdata;
  2219. wdata = cifs_writedata_alloc((unsigned int)tofind,
  2220. cifs_writev_complete);
  2221. if (!wdata)
  2222. return NULL;
  2223. *found_pages = find_get_pages_range_tag(mapping, index, end,
  2224. PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
  2225. return wdata;
  2226. }
  2227. static unsigned int
  2228. wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
  2229. struct address_space *mapping,
  2230. struct writeback_control *wbc,
  2231. pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
  2232. {
  2233. unsigned int nr_pages = 0, i;
  2234. struct page *page;
  2235. for (i = 0; i < found_pages; i++) {
  2236. page = wdata->pages[i];
  2237. /*
  2238. * At this point we hold neither the i_pages lock nor the
  2239. * page lock: the page may be truncated or invalidated
  2240. * (changing page->mapping to NULL), or even swizzled
  2241. * back from swapper_space to tmpfs file mapping
  2242. */
  2243. if (nr_pages == 0)
  2244. lock_page(page);
  2245. else if (!trylock_page(page))
  2246. break;
  2247. if (unlikely(page->mapping != mapping)) {
  2248. unlock_page(page);
  2249. break;
  2250. }
  2251. if (!wbc->range_cyclic && page->index > end) {
  2252. *done = true;
  2253. unlock_page(page);
  2254. break;
  2255. }
  2256. if (*next && (page->index != *next)) {
  2257. /* Not next consecutive page */
  2258. unlock_page(page);
  2259. break;
  2260. }
  2261. if (wbc->sync_mode != WB_SYNC_NONE)
  2262. wait_on_page_writeback(page);
  2263. if (PageWriteback(page) ||
  2264. !clear_page_dirty_for_io(page)) {
  2265. unlock_page(page);
  2266. break;
  2267. }
  2268. /*
  2269. * This actually clears the dirty bit in the radix tree.
  2270. * See cifs_writepage() for more commentary.
  2271. */
  2272. set_page_writeback(page);
  2273. if (page_offset(page) >= i_size_read(mapping->host)) {
  2274. *done = true;
  2275. unlock_page(page);
  2276. end_page_writeback(page);
  2277. break;
  2278. }
  2279. wdata->pages[i] = page;
  2280. *next = page->index + 1;
  2281. ++nr_pages;
  2282. }
  2283. /* reset index to refind any pages skipped */
  2284. if (nr_pages == 0)
  2285. *index = wdata->pages[0]->index + 1;
  2286. /* put any pages we aren't going to use */
  2287. for (i = nr_pages; i < found_pages; i++) {
  2288. put_page(wdata->pages[i]);
  2289. wdata->pages[i] = NULL;
  2290. }
  2291. return nr_pages;
  2292. }
  2293. static int
  2294. wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
  2295. struct address_space *mapping, struct writeback_control *wbc)
  2296. {
  2297. int rc;
  2298. wdata->sync_mode = wbc->sync_mode;
  2299. wdata->nr_pages = nr_pages;
  2300. wdata->offset = page_offset(wdata->pages[0]);
  2301. wdata->pagesz = PAGE_SIZE;
  2302. wdata->tailsz = min(i_size_read(mapping->host) -
  2303. page_offset(wdata->pages[nr_pages - 1]),
  2304. (loff_t)PAGE_SIZE);
  2305. wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
  2306. wdata->pid = wdata->cfile->pid;
  2307. rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
  2308. if (rc)
  2309. return rc;
  2310. if (wdata->cfile->invalidHandle)
  2311. rc = -EAGAIN;
  2312. else
  2313. rc = wdata->server->ops->async_writev(wdata,
  2314. cifs_writedata_release);
  2315. return rc;
  2316. }
  2317. static int cifs_writepages(struct address_space *mapping,
  2318. struct writeback_control *wbc)
  2319. {
  2320. struct inode *inode = mapping->host;
  2321. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2322. struct TCP_Server_Info *server;
  2323. bool done = false, scanned = false, range_whole = false;
  2324. pgoff_t end, index;
  2325. struct cifs_writedata *wdata;
  2326. struct cifsFileInfo *cfile = NULL;
  2327. int rc = 0;
  2328. int saved_rc = 0;
  2329. unsigned int xid;
  2330. /*
  2331. * If wsize is smaller than the page cache size, default to writing
  2332. * one page at a time via cifs_writepage
  2333. */
  2334. if (cifs_sb->ctx->wsize < PAGE_SIZE)
  2335. return generic_writepages(mapping, wbc);
  2336. xid = get_xid();
  2337. if (wbc->range_cyclic) {
  2338. index = mapping->writeback_index; /* Start from prev offset */
  2339. end = -1;
  2340. } else {
  2341. index = wbc->range_start >> PAGE_SHIFT;
  2342. end = wbc->range_end >> PAGE_SHIFT;
  2343. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  2344. range_whole = true;
  2345. scanned = true;
  2346. }
  2347. server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
  2348. retry:
  2349. while (!done && index <= end) {
  2350. unsigned int i, nr_pages, found_pages, wsize;
  2351. pgoff_t next = 0, tofind, saved_index = index;
  2352. struct cifs_credits credits_on_stack;
  2353. struct cifs_credits *credits = &credits_on_stack;
  2354. int get_file_rc = 0;
  2355. if (cfile)
  2356. cifsFileInfo_put(cfile);
  2357. rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
  2358. /* in case of an error store it to return later */
  2359. if (rc)
  2360. get_file_rc = rc;
  2361. rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
  2362. &wsize, credits);
  2363. if (rc != 0) {
  2364. done = true;
  2365. break;
  2366. }
  2367. tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
  2368. wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
  2369. &found_pages);
  2370. if (!wdata) {
  2371. rc = -ENOMEM;
  2372. done = true;
  2373. add_credits_and_wake_if(server, credits, 0);
  2374. break;
  2375. }
  2376. if (found_pages == 0) {
  2377. kref_put(&wdata->refcount, cifs_writedata_release);
  2378. add_credits_and_wake_if(server, credits, 0);
  2379. break;
  2380. }
  2381. nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
  2382. end, &index, &next, &done);
  2383. /* nothing to write? */
  2384. if (nr_pages == 0) {
  2385. kref_put(&wdata->refcount, cifs_writedata_release);
  2386. add_credits_and_wake_if(server, credits, 0);
  2387. continue;
  2388. }
  2389. wdata->credits = credits_on_stack;
  2390. wdata->cfile = cfile;
  2391. wdata->server = server;
  2392. cfile = NULL;
  2393. if (!wdata->cfile) {
  2394. cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
  2395. get_file_rc);
  2396. if (is_retryable_error(get_file_rc))
  2397. rc = get_file_rc;
  2398. else
  2399. rc = -EBADF;
  2400. } else
  2401. rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
  2402. for (i = 0; i < nr_pages; ++i)
  2403. unlock_page(wdata->pages[i]);
  2404. /* send failure -- clean up the mess */
  2405. if (rc != 0) {
  2406. add_credits_and_wake_if(server, &wdata->credits, 0);
  2407. for (i = 0; i < nr_pages; ++i) {
  2408. if (is_retryable_error(rc))
  2409. redirty_page_for_writepage(wbc,
  2410. wdata->pages[i]);
  2411. else
  2412. SetPageError(wdata->pages[i]);
  2413. end_page_writeback(wdata->pages[i]);
  2414. put_page(wdata->pages[i]);
  2415. }
  2416. if (!is_retryable_error(rc))
  2417. mapping_set_error(mapping, rc);
  2418. }
  2419. kref_put(&wdata->refcount, cifs_writedata_release);
  2420. if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
  2421. index = saved_index;
  2422. continue;
  2423. }
  2424. /* Return immediately if we received a signal during writing */
  2425. if (is_interrupt_error(rc)) {
  2426. done = true;
  2427. break;
  2428. }
  2429. if (rc != 0 && saved_rc == 0)
  2430. saved_rc = rc;
  2431. wbc->nr_to_write -= nr_pages;
  2432. if (wbc->nr_to_write <= 0)
  2433. done = true;
  2434. index = next;
  2435. }
  2436. if (!scanned && !done) {
  2437. /*
  2438. * We hit the last page and there is more work to be done: wrap
  2439. * back to the start of the file
  2440. */
  2441. scanned = true;
  2442. index = 0;
  2443. goto retry;
  2444. }
  2445. if (saved_rc != 0)
  2446. rc = saved_rc;
  2447. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  2448. mapping->writeback_index = index;
  2449. if (cfile)
  2450. cifsFileInfo_put(cfile);
  2451. free_xid(xid);
  2452. /* Indication to update ctime and mtime as close is deferred */
  2453. set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
  2454. return rc;
  2455. }
  2456. static int
  2457. cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
  2458. {
  2459. int rc;
  2460. unsigned int xid;
  2461. xid = get_xid();
  2462. /* BB add check for wbc flags */
  2463. get_page(page);
  2464. if (!PageUptodate(page))
  2465. cifs_dbg(FYI, "ppw - page not up to date\n");
  2466. /*
  2467. * Set the "writeback" flag, and clear "dirty" in the radix tree.
  2468. *
  2469. * A writepage() implementation always needs to do either this,
  2470. * or re-dirty the page with "redirty_page_for_writepage()" in
  2471. * the case of a failure.
  2472. *
  2473. * Just unlocking the page will cause the radix tree tag-bits
  2474. * to fail to update with the state of the page correctly.
  2475. */
  2476. set_page_writeback(page);
  2477. retry_write:
  2478. rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
  2479. if (is_retryable_error(rc)) {
  2480. if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
  2481. goto retry_write;
  2482. redirty_page_for_writepage(wbc, page);
  2483. } else if (rc != 0) {
  2484. SetPageError(page);
  2485. mapping_set_error(page->mapping, rc);
  2486. } else {
  2487. SetPageUptodate(page);
  2488. }
  2489. end_page_writeback(page);
  2490. put_page(page);
  2491. free_xid(xid);
  2492. return rc;
  2493. }
  2494. static int cifs_writepage(struct page *page, struct writeback_control *wbc)
  2495. {
  2496. int rc = cifs_writepage_locked(page, wbc);
  2497. unlock_page(page);
  2498. return rc;
  2499. }
  2500. static int cifs_write_end(struct file *file, struct address_space *mapping,
  2501. loff_t pos, unsigned len, unsigned copied,
  2502. struct page *page, void *fsdata)
  2503. {
  2504. int rc;
  2505. struct inode *inode = mapping->host;
  2506. struct cifsFileInfo *cfile = file->private_data;
  2507. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  2508. __u32 pid;
  2509. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2510. pid = cfile->pid;
  2511. else
  2512. pid = current->tgid;
  2513. cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
  2514. page, pos, copied);
  2515. if (PageChecked(page)) {
  2516. if (copied == len)
  2517. SetPageUptodate(page);
  2518. ClearPageChecked(page);
  2519. } else if (!PageUptodate(page) && copied == PAGE_SIZE)
  2520. SetPageUptodate(page);
  2521. if (!PageUptodate(page)) {
  2522. char *page_data;
  2523. unsigned offset = pos & (PAGE_SIZE - 1);
  2524. unsigned int xid;
  2525. xid = get_xid();
  2526. /* this is probably better than directly calling
  2527. partialpage_write since in this function the file handle is
  2528. known which we might as well leverage */
  2529. /* BB check if anything else missing out of ppw
  2530. such as updating last write time */
  2531. page_data = kmap(page);
  2532. rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
  2533. /* if (rc < 0) should we set writebehind rc? */
  2534. kunmap(page);
  2535. free_xid(xid);
  2536. } else {
  2537. rc = copied;
  2538. pos += copied;
  2539. set_page_dirty(page);
  2540. }
  2541. if (rc > 0) {
  2542. spin_lock(&inode->i_lock);
  2543. if (pos > inode->i_size) {
  2544. i_size_write(inode, pos);
  2545. inode->i_blocks = (512 - 1 + pos) >> 9;
  2546. }
  2547. spin_unlock(&inode->i_lock);
  2548. }
  2549. unlock_page(page);
  2550. put_page(page);
  2551. /* Indication to update ctime and mtime as close is deferred */
  2552. set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
  2553. return rc;
  2554. }
  2555. int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
  2556. int datasync)
  2557. {
  2558. unsigned int xid;
  2559. int rc = 0;
  2560. struct cifs_tcon *tcon;
  2561. struct TCP_Server_Info *server;
  2562. struct cifsFileInfo *smbfile = file->private_data;
  2563. struct inode *inode = file_inode(file);
  2564. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2565. rc = file_write_and_wait_range(file, start, end);
  2566. if (rc) {
  2567. trace_cifs_fsync_err(inode->i_ino, rc);
  2568. return rc;
  2569. }
  2570. xid = get_xid();
  2571. cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
  2572. file, datasync);
  2573. if (!CIFS_CACHE_READ(CIFS_I(inode))) {
  2574. rc = cifs_zap_mapping(inode);
  2575. if (rc) {
  2576. cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
  2577. rc = 0; /* don't care about it in fsync */
  2578. }
  2579. }
  2580. tcon = tlink_tcon(smbfile->tlink);
  2581. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  2582. server = tcon->ses->server;
  2583. if (server->ops->flush == NULL) {
  2584. rc = -ENOSYS;
  2585. goto strict_fsync_exit;
  2586. }
  2587. if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
  2588. smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
  2589. if (smbfile) {
  2590. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  2591. cifsFileInfo_put(smbfile);
  2592. } else
  2593. cifs_dbg(FYI, "ignore fsync for file not open for write\n");
  2594. } else
  2595. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  2596. }
  2597. strict_fsync_exit:
  2598. free_xid(xid);
  2599. return rc;
  2600. }
  2601. int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  2602. {
  2603. unsigned int xid;
  2604. int rc = 0;
  2605. struct cifs_tcon *tcon;
  2606. struct TCP_Server_Info *server;
  2607. struct cifsFileInfo *smbfile = file->private_data;
  2608. struct inode *inode = file_inode(file);
  2609. struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
  2610. rc = file_write_and_wait_range(file, start, end);
  2611. if (rc) {
  2612. trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
  2613. return rc;
  2614. }
  2615. xid = get_xid();
  2616. cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
  2617. file, datasync);
  2618. tcon = tlink_tcon(smbfile->tlink);
  2619. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  2620. server = tcon->ses->server;
  2621. if (server->ops->flush == NULL) {
  2622. rc = -ENOSYS;
  2623. goto fsync_exit;
  2624. }
  2625. if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
  2626. smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
  2627. if (smbfile) {
  2628. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  2629. cifsFileInfo_put(smbfile);
  2630. } else
  2631. cifs_dbg(FYI, "ignore fsync for file not open for write\n");
  2632. } else
  2633. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  2634. }
  2635. fsync_exit:
  2636. free_xid(xid);
  2637. return rc;
  2638. }
  2639. /*
  2640. * As file closes, flush all cached write data for this inode checking
  2641. * for write behind errors.
  2642. */
  2643. int cifs_flush(struct file *file, fl_owner_t id)
  2644. {
  2645. struct inode *inode = file_inode(file);
  2646. int rc = 0;
  2647. if (file->f_mode & FMODE_WRITE)
  2648. rc = filemap_write_and_wait(inode->i_mapping);
  2649. cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
  2650. if (rc) {
  2651. /* get more nuanced writeback errors */
  2652. rc = filemap_check_wb_err(file->f_mapping, 0);
  2653. trace_cifs_flush_err(inode->i_ino, rc);
  2654. }
  2655. return rc;
  2656. }
  2657. static int
  2658. cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
  2659. {
  2660. int rc = 0;
  2661. unsigned long i;
  2662. for (i = 0; i < num_pages; i++) {
  2663. pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
  2664. if (!pages[i]) {
  2665. /*
  2666. * save number of pages we have already allocated and
  2667. * return with ENOMEM error
  2668. */
  2669. num_pages = i;
  2670. rc = -ENOMEM;
  2671. break;
  2672. }
  2673. }
  2674. if (rc) {
  2675. for (i = 0; i < num_pages; i++)
  2676. put_page(pages[i]);
  2677. }
  2678. return rc;
  2679. }
  2680. static inline
  2681. size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
  2682. {
  2683. size_t num_pages;
  2684. size_t clen;
  2685. clen = min_t(const size_t, len, wsize);
  2686. num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
  2687. if (cur_len)
  2688. *cur_len = clen;
  2689. return num_pages;
  2690. }
  2691. static void
  2692. cifs_uncached_writedata_release(struct kref *refcount)
  2693. {
  2694. int i;
  2695. struct cifs_writedata *wdata = container_of(refcount,
  2696. struct cifs_writedata, refcount);
  2697. kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
  2698. for (i = 0; i < wdata->nr_pages; i++)
  2699. put_page(wdata->pages[i]);
  2700. cifs_writedata_release(refcount);
  2701. }
  2702. static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
  2703. static void
  2704. cifs_uncached_writev_complete(struct work_struct *work)
  2705. {
  2706. struct cifs_writedata *wdata = container_of(work,
  2707. struct cifs_writedata, work);
  2708. struct inode *inode = d_inode(wdata->cfile->dentry);
  2709. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  2710. spin_lock(&inode->i_lock);
  2711. cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
  2712. if (cifsi->server_eof > inode->i_size)
  2713. i_size_write(inode, cifsi->server_eof);
  2714. spin_unlock(&inode->i_lock);
  2715. complete(&wdata->done);
  2716. collect_uncached_write_data(wdata->ctx);
  2717. /* the below call can possibly free the last ref to aio ctx */
  2718. kref_put(&wdata->refcount, cifs_uncached_writedata_release);
  2719. }
  2720. static int
  2721. wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
  2722. size_t *len, unsigned long *num_pages)
  2723. {
  2724. size_t save_len, copied, bytes, cur_len = *len;
  2725. unsigned long i, nr_pages = *num_pages;
  2726. save_len = cur_len;
  2727. for (i = 0; i < nr_pages; i++) {
  2728. bytes = min_t(const size_t, cur_len, PAGE_SIZE);
  2729. copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
  2730. cur_len -= copied;
  2731. /*
  2732. * If we didn't copy as much as we expected, then that
  2733. * may mean we trod into an unmapped area. Stop copying
  2734. * at that point. On the next pass through the big
  2735. * loop, we'll likely end up getting a zero-length
  2736. * write and bailing out of it.
  2737. */
  2738. if (copied < bytes)
  2739. break;
  2740. }
  2741. cur_len = save_len - cur_len;
  2742. *len = cur_len;
  2743. /*
  2744. * If we have no data to send, then that probably means that
  2745. * the copy above failed altogether. That's most likely because
  2746. * the address in the iovec was bogus. Return -EFAULT and let
  2747. * the caller free anything we allocated and bail out.
  2748. */
  2749. if (!cur_len)
  2750. return -EFAULT;
  2751. /*
  2752. * i + 1 now represents the number of pages we actually used in
  2753. * the copy phase above.
  2754. */
  2755. *num_pages = i + 1;
  2756. return 0;
  2757. }
  2758. static int
  2759. cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
  2760. struct cifs_aio_ctx *ctx)
  2761. {
  2762. unsigned int wsize;
  2763. struct cifs_credits credits;
  2764. int rc;
  2765. struct TCP_Server_Info *server = wdata->server;
  2766. do {
  2767. if (wdata->cfile->invalidHandle) {
  2768. rc = cifs_reopen_file(wdata->cfile, false);
  2769. if (rc == -EAGAIN)
  2770. continue;
  2771. else if (rc)
  2772. break;
  2773. }
  2774. /*
  2775. * Wait for credits to resend this wdata.
  2776. * Note: we are attempting to resend the whole wdata not in
  2777. * segments
  2778. */
  2779. do {
  2780. rc = server->ops->wait_mtu_credits(server, wdata->bytes,
  2781. &wsize, &credits);
  2782. if (rc)
  2783. goto fail;
  2784. if (wsize < wdata->bytes) {
  2785. add_credits_and_wake_if(server, &credits, 0);
  2786. msleep(1000);
  2787. }
  2788. } while (wsize < wdata->bytes);
  2789. wdata->credits = credits;
  2790. rc = adjust_credits(server, &wdata->credits, wdata->bytes);
  2791. if (!rc) {
  2792. if (wdata->cfile->invalidHandle)
  2793. rc = -EAGAIN;
  2794. else {
  2795. #ifdef CONFIG_CIFS_SMB_DIRECT
  2796. if (wdata->mr) {
  2797. wdata->mr->need_invalidate = true;
  2798. smbd_deregister_mr(wdata->mr);
  2799. wdata->mr = NULL;
  2800. }
  2801. #endif
  2802. rc = server->ops->async_writev(wdata,
  2803. cifs_uncached_writedata_release);
  2804. }
  2805. }
  2806. /* If the write was successfully sent, we are done */
  2807. if (!rc) {
  2808. list_add_tail(&wdata->list, wdata_list);
  2809. return 0;
  2810. }
  2811. /* Roll back credits and retry if needed */
  2812. add_credits_and_wake_if(server, &wdata->credits, 0);
  2813. } while (rc == -EAGAIN);
  2814. fail:
  2815. kref_put(&wdata->refcount, cifs_uncached_writedata_release);
  2816. return rc;
  2817. }
  2818. static int
  2819. cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
  2820. struct cifsFileInfo *open_file,
  2821. struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
  2822. struct cifs_aio_ctx *ctx)
  2823. {
  2824. int rc = 0;
  2825. size_t cur_len;
  2826. unsigned long nr_pages, num_pages, i;
  2827. struct cifs_writedata *wdata;
  2828. struct iov_iter saved_from = *from;
  2829. loff_t saved_offset = offset;
  2830. pid_t pid;
  2831. struct TCP_Server_Info *server;
  2832. struct page **pagevec;
  2833. size_t start;
  2834. unsigned int xid;
  2835. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2836. pid = open_file->pid;
  2837. else
  2838. pid = current->tgid;
  2839. server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
  2840. xid = get_xid();
  2841. do {
  2842. unsigned int wsize;
  2843. struct cifs_credits credits_on_stack;
  2844. struct cifs_credits *credits = &credits_on_stack;
  2845. if (open_file->invalidHandle) {
  2846. rc = cifs_reopen_file(open_file, false);
  2847. if (rc == -EAGAIN)
  2848. continue;
  2849. else if (rc)
  2850. break;
  2851. }
  2852. rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
  2853. &wsize, credits);
  2854. if (rc)
  2855. break;
  2856. cur_len = min_t(const size_t, len, wsize);
  2857. if (ctx->direct_io) {
  2858. ssize_t result;
  2859. result = iov_iter_get_pages_alloc2(
  2860. from, &pagevec, cur_len, &start);
  2861. if (result < 0) {
  2862. cifs_dbg(VFS,
  2863. "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
  2864. result, iov_iter_type(from),
  2865. from->iov_offset, from->count);
  2866. dump_stack();
  2867. rc = result;
  2868. add_credits_and_wake_if(server, credits, 0);
  2869. break;
  2870. }
  2871. cur_len = (size_t)result;
  2872. nr_pages =
  2873. (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
  2874. wdata = cifs_writedata_direct_alloc(pagevec,
  2875. cifs_uncached_writev_complete);
  2876. if (!wdata) {
  2877. rc = -ENOMEM;
  2878. for (i = 0; i < nr_pages; i++)
  2879. put_page(pagevec[i]);
  2880. kvfree(pagevec);
  2881. add_credits_and_wake_if(server, credits, 0);
  2882. break;
  2883. }
  2884. wdata->page_offset = start;
  2885. wdata->tailsz =
  2886. nr_pages > 1 ?
  2887. cur_len - (PAGE_SIZE - start) -
  2888. (nr_pages - 2) * PAGE_SIZE :
  2889. cur_len;
  2890. } else {
  2891. nr_pages = get_numpages(wsize, len, &cur_len);
  2892. wdata = cifs_writedata_alloc(nr_pages,
  2893. cifs_uncached_writev_complete);
  2894. if (!wdata) {
  2895. rc = -ENOMEM;
  2896. add_credits_and_wake_if(server, credits, 0);
  2897. break;
  2898. }
  2899. rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
  2900. if (rc) {
  2901. kvfree(wdata->pages);
  2902. kfree(wdata);
  2903. add_credits_and_wake_if(server, credits, 0);
  2904. break;
  2905. }
  2906. num_pages = nr_pages;
  2907. rc = wdata_fill_from_iovec(
  2908. wdata, from, &cur_len, &num_pages);
  2909. if (rc) {
  2910. for (i = 0; i < nr_pages; i++)
  2911. put_page(wdata->pages[i]);
  2912. kvfree(wdata->pages);
  2913. kfree(wdata);
  2914. add_credits_and_wake_if(server, credits, 0);
  2915. break;
  2916. }
  2917. /*
  2918. * Bring nr_pages down to the number of pages we
  2919. * actually used, and free any pages that we didn't use.
  2920. */
  2921. for ( ; nr_pages > num_pages; nr_pages--)
  2922. put_page(wdata->pages[nr_pages - 1]);
  2923. wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
  2924. }
  2925. wdata->sync_mode = WB_SYNC_ALL;
  2926. wdata->nr_pages = nr_pages;
  2927. wdata->offset = (__u64)offset;
  2928. wdata->cfile = cifsFileInfo_get(open_file);
  2929. wdata->server = server;
  2930. wdata->pid = pid;
  2931. wdata->bytes = cur_len;
  2932. wdata->pagesz = PAGE_SIZE;
  2933. wdata->credits = credits_on_stack;
  2934. wdata->ctx = ctx;
  2935. kref_get(&ctx->refcount);
  2936. rc = adjust_credits(server, &wdata->credits, wdata->bytes);
  2937. if (!rc) {
  2938. if (wdata->cfile->invalidHandle)
  2939. rc = -EAGAIN;
  2940. else
  2941. rc = server->ops->async_writev(wdata,
  2942. cifs_uncached_writedata_release);
  2943. }
  2944. if (rc) {
  2945. add_credits_and_wake_if(server, &wdata->credits, 0);
  2946. kref_put(&wdata->refcount,
  2947. cifs_uncached_writedata_release);
  2948. if (rc == -EAGAIN) {
  2949. *from = saved_from;
  2950. iov_iter_advance(from, offset - saved_offset);
  2951. continue;
  2952. }
  2953. break;
  2954. }
  2955. list_add_tail(&wdata->list, wdata_list);
  2956. offset += cur_len;
  2957. len -= cur_len;
  2958. } while (len > 0);
  2959. free_xid(xid);
  2960. return rc;
  2961. }
  2962. static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
  2963. {
  2964. struct cifs_writedata *wdata, *tmp;
  2965. struct cifs_tcon *tcon;
  2966. struct cifs_sb_info *cifs_sb;
  2967. struct dentry *dentry = ctx->cfile->dentry;
  2968. ssize_t rc;
  2969. tcon = tlink_tcon(ctx->cfile->tlink);
  2970. cifs_sb = CIFS_SB(dentry->d_sb);
  2971. mutex_lock(&ctx->aio_mutex);
  2972. if (list_empty(&ctx->list)) {
  2973. mutex_unlock(&ctx->aio_mutex);
  2974. return;
  2975. }
  2976. rc = ctx->rc;
  2977. /*
  2978. * Wait for and collect replies for any successful sends in order of
  2979. * increasing offset. Once an error is hit, then return without waiting
  2980. * for any more replies.
  2981. */
  2982. restart_loop:
  2983. list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
  2984. if (!rc) {
  2985. if (!try_wait_for_completion(&wdata->done)) {
  2986. mutex_unlock(&ctx->aio_mutex);
  2987. return;
  2988. }
  2989. if (wdata->result)
  2990. rc = wdata->result;
  2991. else
  2992. ctx->total_len += wdata->bytes;
  2993. /* resend call if it's a retryable error */
  2994. if (rc == -EAGAIN) {
  2995. struct list_head tmp_list;
  2996. struct iov_iter tmp_from = ctx->iter;
  2997. INIT_LIST_HEAD(&tmp_list);
  2998. list_del_init(&wdata->list);
  2999. if (ctx->direct_io)
  3000. rc = cifs_resend_wdata(
  3001. wdata, &tmp_list, ctx);
  3002. else {
  3003. iov_iter_advance(&tmp_from,
  3004. wdata->offset - ctx->pos);
  3005. rc = cifs_write_from_iter(wdata->offset,
  3006. wdata->bytes, &tmp_from,
  3007. ctx->cfile, cifs_sb, &tmp_list,
  3008. ctx);
  3009. kref_put(&wdata->refcount,
  3010. cifs_uncached_writedata_release);
  3011. }
  3012. list_splice(&tmp_list, &ctx->list);
  3013. goto restart_loop;
  3014. }
  3015. }
  3016. list_del_init(&wdata->list);
  3017. kref_put(&wdata->refcount, cifs_uncached_writedata_release);
  3018. }
  3019. cifs_stats_bytes_written(tcon, ctx->total_len);
  3020. set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
  3021. ctx->rc = (rc == 0) ? ctx->total_len : rc;
  3022. mutex_unlock(&ctx->aio_mutex);
  3023. if (ctx->iocb && ctx->iocb->ki_complete)
  3024. ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
  3025. else
  3026. complete(&ctx->done);
  3027. }
  3028. static ssize_t __cifs_writev(
  3029. struct kiocb *iocb, struct iov_iter *from, bool direct)
  3030. {
  3031. struct file *file = iocb->ki_filp;
  3032. ssize_t total_written = 0;
  3033. struct cifsFileInfo *cfile;
  3034. struct cifs_tcon *tcon;
  3035. struct cifs_sb_info *cifs_sb;
  3036. struct cifs_aio_ctx *ctx;
  3037. struct iov_iter saved_from = *from;
  3038. size_t len = iov_iter_count(from);
  3039. int rc;
  3040. /*
  3041. * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
  3042. * In this case, fall back to non-direct write function.
  3043. * this could be improved by getting pages directly in ITER_KVEC
  3044. */
  3045. if (direct && iov_iter_is_kvec(from)) {
  3046. cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
  3047. direct = false;
  3048. }
  3049. rc = generic_write_checks(iocb, from);
  3050. if (rc <= 0)
  3051. return rc;
  3052. cifs_sb = CIFS_FILE_SB(file);
  3053. cfile = file->private_data;
  3054. tcon = tlink_tcon(cfile->tlink);
  3055. if (!tcon->ses->server->ops->async_writev)
  3056. return -ENOSYS;
  3057. ctx = cifs_aio_ctx_alloc();
  3058. if (!ctx)
  3059. return -ENOMEM;
  3060. ctx->cfile = cifsFileInfo_get(cfile);
  3061. if (!is_sync_kiocb(iocb))
  3062. ctx->iocb = iocb;
  3063. ctx->pos = iocb->ki_pos;
  3064. if (direct) {
  3065. ctx->direct_io = true;
  3066. ctx->iter = *from;
  3067. ctx->len = len;
  3068. } else {
  3069. rc = setup_aio_ctx_iter(ctx, from, ITER_SOURCE);
  3070. if (rc) {
  3071. kref_put(&ctx->refcount, cifs_aio_ctx_release);
  3072. return rc;
  3073. }
  3074. }
  3075. /* grab a lock here due to read response handlers can access ctx */
  3076. mutex_lock(&ctx->aio_mutex);
  3077. rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
  3078. cfile, cifs_sb, &ctx->list, ctx);
  3079. /*
  3080. * If at least one write was successfully sent, then discard any rc
  3081. * value from the later writes. If the other write succeeds, then
  3082. * we'll end up returning whatever was written. If it fails, then
  3083. * we'll get a new rc value from that.
  3084. */
  3085. if (!list_empty(&ctx->list))
  3086. rc = 0;
  3087. mutex_unlock(&ctx->aio_mutex);
  3088. if (rc) {
  3089. kref_put(&ctx->refcount, cifs_aio_ctx_release);
  3090. return rc;
  3091. }
  3092. if (!is_sync_kiocb(iocb)) {
  3093. kref_put(&ctx->refcount, cifs_aio_ctx_release);
  3094. return -EIOCBQUEUED;
  3095. }
  3096. rc = wait_for_completion_killable(&ctx->done);
  3097. if (rc) {
  3098. mutex_lock(&ctx->aio_mutex);
  3099. ctx->rc = rc = -EINTR;
  3100. total_written = ctx->total_len;
  3101. mutex_unlock(&ctx->aio_mutex);
  3102. } else {
  3103. rc = ctx->rc;
  3104. total_written = ctx->total_len;
  3105. }
  3106. kref_put(&ctx->refcount, cifs_aio_ctx_release);
  3107. if (unlikely(!total_written))
  3108. return rc;
  3109. iocb->ki_pos += total_written;
  3110. return total_written;
  3111. }
  3112. ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
  3113. {
  3114. struct file *file = iocb->ki_filp;
  3115. cifs_revalidate_mapping(file->f_inode);
  3116. return __cifs_writev(iocb, from, true);
  3117. }
  3118. ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
  3119. {
  3120. return __cifs_writev(iocb, from, false);
  3121. }
  3122. static ssize_t
  3123. cifs_writev(struct kiocb *iocb, struct iov_iter *from)
  3124. {
  3125. struct file *file = iocb->ki_filp;
  3126. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  3127. struct inode *inode = file->f_mapping->host;
  3128. struct cifsInodeInfo *cinode = CIFS_I(inode);
  3129. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  3130. ssize_t rc;
  3131. inode_lock(inode);
  3132. /*
  3133. * We need to hold the sem to be sure nobody modifies lock list
  3134. * with a brlock that prevents writing.
  3135. */
  3136. down_read(&cinode->lock_sem);
  3137. rc = generic_write_checks(iocb, from);
  3138. if (rc <= 0)
  3139. goto out;
  3140. if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
  3141. server->vals->exclusive_lock_type, 0,
  3142. NULL, CIFS_WRITE_OP))
  3143. rc = __generic_file_write_iter(iocb, from);
  3144. else
  3145. rc = -EACCES;
  3146. out:
  3147. up_read(&cinode->lock_sem);
  3148. inode_unlock(inode);
  3149. if (rc > 0)
  3150. rc = generic_write_sync(iocb, rc);
  3151. return rc;
  3152. }
  3153. ssize_t
  3154. cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
  3155. {
  3156. struct inode *inode = file_inode(iocb->ki_filp);
  3157. struct cifsInodeInfo *cinode = CIFS_I(inode);
  3158. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  3159. struct cifsFileInfo *cfile = (struct cifsFileInfo *)
  3160. iocb->ki_filp->private_data;
  3161. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  3162. ssize_t written;
  3163. written = cifs_get_writer(cinode);
  3164. if (written)
  3165. return written;
  3166. if (CIFS_CACHE_WRITE(cinode)) {
  3167. if (cap_unix(tcon->ses) &&
  3168. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
  3169. && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
  3170. written = generic_file_write_iter(iocb, from);
  3171. goto out;
  3172. }
  3173. written = cifs_writev(iocb, from);
  3174. goto out;
  3175. }
  3176. /*
  3177. * For non-oplocked files in strict cache mode we need to write the data
  3178. * to the server exactly from the pos to pos+len-1 rather than flush all
  3179. * affected pages because it may cause a error with mandatory locks on
  3180. * these pages but not on the region from pos to ppos+len-1.
  3181. */
  3182. written = cifs_user_writev(iocb, from);
  3183. if (CIFS_CACHE_READ(cinode)) {
  3184. /*
  3185. * We have read level caching and we have just sent a write
  3186. * request to the server thus making data in the cache stale.
  3187. * Zap the cache and set oplock/lease level to NONE to avoid
  3188. * reading stale data from the cache. All subsequent read
  3189. * operations will read new data from the server.
  3190. */
  3191. cifs_zap_mapping(inode);
  3192. cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
  3193. inode);
  3194. cinode->oplock = 0;
  3195. }
  3196. out:
  3197. cifs_put_writer(cinode);
  3198. return written;
  3199. }
  3200. static struct cifs_readdata *
  3201. cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
  3202. {
  3203. struct cifs_readdata *rdata;
  3204. rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
  3205. if (rdata != NULL) {
  3206. rdata->pages = pages;
  3207. kref_init(&rdata->refcount);
  3208. INIT_LIST_HEAD(&rdata->list);
  3209. init_completion(&rdata->done);
  3210. INIT_WORK(&rdata->work, complete);
  3211. }
  3212. return rdata;
  3213. }
  3214. static struct cifs_readdata *
  3215. cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
  3216. {
  3217. struct page **pages =
  3218. kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  3219. struct cifs_readdata *ret = NULL;
  3220. if (pages) {
  3221. ret = cifs_readdata_direct_alloc(pages, complete);
  3222. if (!ret)
  3223. kfree(pages);
  3224. }
  3225. return ret;
  3226. }
  3227. void
  3228. cifs_readdata_release(struct kref *refcount)
  3229. {
  3230. struct cifs_readdata *rdata = container_of(refcount,
  3231. struct cifs_readdata, refcount);
  3232. #ifdef CONFIG_CIFS_SMB_DIRECT
  3233. if (rdata->mr) {
  3234. smbd_deregister_mr(rdata->mr);
  3235. rdata->mr = NULL;
  3236. }
  3237. #endif
  3238. if (rdata->cfile)
  3239. cifsFileInfo_put(rdata->cfile);
  3240. kvfree(rdata->pages);
  3241. kfree(rdata);
  3242. }
  3243. static int
  3244. cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
  3245. {
  3246. int rc = 0;
  3247. struct page *page;
  3248. unsigned int i;
  3249. for (i = 0; i < nr_pages; i++) {
  3250. page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
  3251. if (!page) {
  3252. rc = -ENOMEM;
  3253. break;
  3254. }
  3255. rdata->pages[i] = page;
  3256. }
  3257. if (rc) {
  3258. unsigned int nr_page_failed = i;
  3259. for (i = 0; i < nr_page_failed; i++) {
  3260. put_page(rdata->pages[i]);
  3261. rdata->pages[i] = NULL;
  3262. }
  3263. }
  3264. return rc;
  3265. }
  3266. static void
  3267. cifs_uncached_readdata_release(struct kref *refcount)
  3268. {
  3269. struct cifs_readdata *rdata = container_of(refcount,
  3270. struct cifs_readdata, refcount);
  3271. unsigned int i;
  3272. kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
  3273. for (i = 0; i < rdata->nr_pages; i++) {
  3274. put_page(rdata->pages[i]);
  3275. }
  3276. cifs_readdata_release(refcount);
  3277. }
  3278. /**
  3279. * cifs_readdata_to_iov - copy data from pages in response to an iovec
  3280. * @rdata: the readdata response with list of pages holding data
  3281. * @iter: destination for our data
  3282. *
  3283. * This function copies data from a list of pages in a readdata response into
  3284. * an array of iovecs. It will first calculate where the data should go
  3285. * based on the info in the readdata and then copy the data into that spot.
  3286. */
  3287. static int
  3288. cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
  3289. {
  3290. size_t remaining = rdata->got_bytes;
  3291. unsigned int i;
  3292. for (i = 0; i < rdata->nr_pages; i++) {
  3293. struct page *page = rdata->pages[i];
  3294. size_t copy = min_t(size_t, remaining, PAGE_SIZE);
  3295. size_t written;
  3296. if (unlikely(iov_iter_is_pipe(iter))) {
  3297. void *addr = kmap_atomic(page);
  3298. written = copy_to_iter(addr, copy, iter);
  3299. kunmap_atomic(addr);
  3300. } else
  3301. written = copy_page_to_iter(page, 0, copy, iter);
  3302. remaining -= written;
  3303. if (written < copy && iov_iter_count(iter) > 0)
  3304. break;
  3305. }
  3306. return remaining ? -EFAULT : 0;
  3307. }
  3308. static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
  3309. static void
  3310. cifs_uncached_readv_complete(struct work_struct *work)
  3311. {
  3312. struct cifs_readdata *rdata = container_of(work,
  3313. struct cifs_readdata, work);
  3314. complete(&rdata->done);
  3315. collect_uncached_read_data(rdata->ctx);
  3316. /* the below call can possibly free the last ref to aio ctx */
  3317. kref_put(&rdata->refcount, cifs_uncached_readdata_release);
  3318. }
  3319. static int
  3320. uncached_fill_pages(struct TCP_Server_Info *server,
  3321. struct cifs_readdata *rdata, struct iov_iter *iter,
  3322. unsigned int len)
  3323. {
  3324. int result = 0;
  3325. unsigned int i;
  3326. unsigned int nr_pages = rdata->nr_pages;
  3327. unsigned int page_offset = rdata->page_offset;
  3328. rdata->got_bytes = 0;
  3329. rdata->tailsz = PAGE_SIZE;
  3330. for (i = 0; i < nr_pages; i++) {
  3331. struct page *page = rdata->pages[i];
  3332. size_t n;
  3333. unsigned int segment_size = rdata->pagesz;
  3334. if (i == 0)
  3335. segment_size -= page_offset;
  3336. else
  3337. page_offset = 0;
  3338. if (len <= 0) {
  3339. /* no need to hold page hostage */
  3340. rdata->pages[i] = NULL;
  3341. rdata->nr_pages--;
  3342. put_page(page);
  3343. continue;
  3344. }
  3345. n = len;
  3346. if (len >= segment_size)
  3347. /* enough data to fill the page */
  3348. n = segment_size;
  3349. else
  3350. rdata->tailsz = len;
  3351. len -= n;
  3352. if (iter)
  3353. result = copy_page_from_iter(
  3354. page, page_offset, n, iter);
  3355. #ifdef CONFIG_CIFS_SMB_DIRECT
  3356. else if (rdata->mr)
  3357. result = n;
  3358. #endif
  3359. else
  3360. result = cifs_read_page_from_socket(
  3361. server, page, page_offset, n);
  3362. if (result < 0)
  3363. break;
  3364. rdata->got_bytes += result;
  3365. }
  3366. return result != -ECONNABORTED && rdata->got_bytes > 0 ?
  3367. rdata->got_bytes : result;
  3368. }
  3369. static int
  3370. cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
  3371. struct cifs_readdata *rdata, unsigned int len)
  3372. {
  3373. return uncached_fill_pages(server, rdata, NULL, len);
  3374. }
  3375. static int
  3376. cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
  3377. struct cifs_readdata *rdata,
  3378. struct iov_iter *iter)
  3379. {
  3380. return uncached_fill_pages(server, rdata, iter, iter->count);
  3381. }
  3382. static int cifs_resend_rdata(struct cifs_readdata *rdata,
  3383. struct list_head *rdata_list,
  3384. struct cifs_aio_ctx *ctx)
  3385. {
  3386. unsigned int rsize;
  3387. struct cifs_credits credits;
  3388. int rc;
  3389. struct TCP_Server_Info *server;
  3390. /* XXX: should we pick a new channel here? */
  3391. server = rdata->server;
  3392. do {
  3393. if (rdata->cfile->invalidHandle) {
  3394. rc = cifs_reopen_file(rdata->cfile, true);
  3395. if (rc == -EAGAIN)
  3396. continue;
  3397. else if (rc)
  3398. break;
  3399. }
  3400. /*
  3401. * Wait for credits to resend this rdata.
  3402. * Note: we are attempting to resend the whole rdata not in
  3403. * segments
  3404. */
  3405. do {
  3406. rc = server->ops->wait_mtu_credits(server, rdata->bytes,
  3407. &rsize, &credits);
  3408. if (rc)
  3409. goto fail;
  3410. if (rsize < rdata->bytes) {
  3411. add_credits_and_wake_if(server, &credits, 0);
  3412. msleep(1000);
  3413. }
  3414. } while (rsize < rdata->bytes);
  3415. rdata->credits = credits;
  3416. rc = adjust_credits(server, &rdata->credits, rdata->bytes);
  3417. if (!rc) {
  3418. if (rdata->cfile->invalidHandle)
  3419. rc = -EAGAIN;
  3420. else {
  3421. #ifdef CONFIG_CIFS_SMB_DIRECT
  3422. if (rdata->mr) {
  3423. rdata->mr->need_invalidate = true;
  3424. smbd_deregister_mr(rdata->mr);
  3425. rdata->mr = NULL;
  3426. }
  3427. #endif
  3428. rc = server->ops->async_readv(rdata);
  3429. }
  3430. }
  3431. /* If the read was successfully sent, we are done */
  3432. if (!rc) {
  3433. /* Add to aio pending list */
  3434. list_add_tail(&rdata->list, rdata_list);
  3435. return 0;
  3436. }
  3437. /* Roll back credits and retry if needed */
  3438. add_credits_and_wake_if(server, &rdata->credits, 0);
  3439. } while (rc == -EAGAIN);
  3440. fail:
  3441. kref_put(&rdata->refcount, cifs_uncached_readdata_release);
  3442. return rc;
  3443. }
  3444. static int
  3445. cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
  3446. struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
  3447. struct cifs_aio_ctx *ctx)
  3448. {
  3449. struct cifs_readdata *rdata;
  3450. unsigned int npages, rsize;
  3451. struct cifs_credits credits_on_stack;
  3452. struct cifs_credits *credits = &credits_on_stack;
  3453. size_t cur_len;
  3454. int rc;
  3455. pid_t pid;
  3456. struct TCP_Server_Info *server;
  3457. struct page **pagevec;
  3458. size_t start;
  3459. struct iov_iter direct_iov = ctx->iter;
  3460. server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
  3461. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  3462. pid = open_file->pid;
  3463. else
  3464. pid = current->tgid;
  3465. if (ctx->direct_io)
  3466. iov_iter_advance(&direct_iov, offset - ctx->pos);
  3467. do {
  3468. if (open_file->invalidHandle) {
  3469. rc = cifs_reopen_file(open_file, true);
  3470. if (rc == -EAGAIN)
  3471. continue;
  3472. else if (rc)
  3473. break;
  3474. }
  3475. if (cifs_sb->ctx->rsize == 0)
  3476. cifs_sb->ctx->rsize =
  3477. server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
  3478. cifs_sb->ctx);
  3479. rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
  3480. &rsize, credits);
  3481. if (rc)
  3482. break;
  3483. cur_len = min_t(const size_t, len, rsize);
  3484. if (ctx->direct_io) {
  3485. ssize_t result;
  3486. result = iov_iter_get_pages_alloc2(
  3487. &direct_iov, &pagevec,
  3488. cur_len, &start);
  3489. if (result < 0) {
  3490. cifs_dbg(VFS,
  3491. "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
  3492. result, iov_iter_type(&direct_iov),
  3493. direct_iov.iov_offset,
  3494. direct_iov.count);
  3495. dump_stack();
  3496. rc = result;
  3497. add_credits_and_wake_if(server, credits, 0);
  3498. break;
  3499. }
  3500. cur_len = (size_t)result;
  3501. rdata = cifs_readdata_direct_alloc(
  3502. pagevec, cifs_uncached_readv_complete);
  3503. if (!rdata) {
  3504. add_credits_and_wake_if(server, credits, 0);
  3505. rc = -ENOMEM;
  3506. break;
  3507. }
  3508. npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
  3509. rdata->page_offset = start;
  3510. rdata->tailsz = npages > 1 ?
  3511. cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
  3512. cur_len;
  3513. } else {
  3514. npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
  3515. /* allocate a readdata struct */
  3516. rdata = cifs_readdata_alloc(npages,
  3517. cifs_uncached_readv_complete);
  3518. if (!rdata) {
  3519. add_credits_and_wake_if(server, credits, 0);
  3520. rc = -ENOMEM;
  3521. break;
  3522. }
  3523. rc = cifs_read_allocate_pages(rdata, npages);
  3524. if (rc) {
  3525. kvfree(rdata->pages);
  3526. kfree(rdata);
  3527. add_credits_and_wake_if(server, credits, 0);
  3528. break;
  3529. }
  3530. rdata->tailsz = PAGE_SIZE;
  3531. }
  3532. rdata->server = server;
  3533. rdata->cfile = cifsFileInfo_get(open_file);
  3534. rdata->nr_pages = npages;
  3535. rdata->offset = offset;
  3536. rdata->bytes = cur_len;
  3537. rdata->pid = pid;
  3538. rdata->pagesz = PAGE_SIZE;
  3539. rdata->read_into_pages = cifs_uncached_read_into_pages;
  3540. rdata->copy_into_pages = cifs_uncached_copy_into_pages;
  3541. rdata->credits = credits_on_stack;
  3542. rdata->ctx = ctx;
  3543. kref_get(&ctx->refcount);
  3544. rc = adjust_credits(server, &rdata->credits, rdata->bytes);
  3545. if (!rc) {
  3546. if (rdata->cfile->invalidHandle)
  3547. rc = -EAGAIN;
  3548. else
  3549. rc = server->ops->async_readv(rdata);
  3550. }
  3551. if (rc) {
  3552. add_credits_and_wake_if(server, &rdata->credits, 0);
  3553. kref_put(&rdata->refcount,
  3554. cifs_uncached_readdata_release);
  3555. if (rc == -EAGAIN) {
  3556. iov_iter_revert(&direct_iov, cur_len);
  3557. continue;
  3558. }
  3559. break;
  3560. }
  3561. list_add_tail(&rdata->list, rdata_list);
  3562. offset += cur_len;
  3563. len -= cur_len;
  3564. } while (len > 0);
  3565. return rc;
  3566. }
  3567. static void
  3568. collect_uncached_read_data(struct cifs_aio_ctx *ctx)
  3569. {
  3570. struct cifs_readdata *rdata, *tmp;
  3571. struct iov_iter *to = &ctx->iter;
  3572. struct cifs_sb_info *cifs_sb;
  3573. int rc;
  3574. cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
  3575. mutex_lock(&ctx->aio_mutex);
  3576. if (list_empty(&ctx->list)) {
  3577. mutex_unlock(&ctx->aio_mutex);
  3578. return;
  3579. }
  3580. rc = ctx->rc;
  3581. /* the loop below should proceed in the order of increasing offsets */
  3582. again:
  3583. list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
  3584. if (!rc) {
  3585. if (!try_wait_for_completion(&rdata->done)) {
  3586. mutex_unlock(&ctx->aio_mutex);
  3587. return;
  3588. }
  3589. if (rdata->result == -EAGAIN) {
  3590. /* resend call if it's a retryable error */
  3591. struct list_head tmp_list;
  3592. unsigned int got_bytes = rdata->got_bytes;
  3593. list_del_init(&rdata->list);
  3594. INIT_LIST_HEAD(&tmp_list);
  3595. /*
  3596. * Got a part of data and then reconnect has
  3597. * happened -- fill the buffer and continue
  3598. * reading.
  3599. */
  3600. if (got_bytes && got_bytes < rdata->bytes) {
  3601. rc = 0;
  3602. if (!ctx->direct_io)
  3603. rc = cifs_readdata_to_iov(rdata, to);
  3604. if (rc) {
  3605. kref_put(&rdata->refcount,
  3606. cifs_uncached_readdata_release);
  3607. continue;
  3608. }
  3609. }
  3610. if (ctx->direct_io) {
  3611. /*
  3612. * Re-use rdata as this is a
  3613. * direct I/O
  3614. */
  3615. rc = cifs_resend_rdata(
  3616. rdata,
  3617. &tmp_list, ctx);
  3618. } else {
  3619. rc = cifs_send_async_read(
  3620. rdata->offset + got_bytes,
  3621. rdata->bytes - got_bytes,
  3622. rdata->cfile, cifs_sb,
  3623. &tmp_list, ctx);
  3624. kref_put(&rdata->refcount,
  3625. cifs_uncached_readdata_release);
  3626. }
  3627. list_splice(&tmp_list, &ctx->list);
  3628. goto again;
  3629. } else if (rdata->result)
  3630. rc = rdata->result;
  3631. else if (!ctx->direct_io)
  3632. rc = cifs_readdata_to_iov(rdata, to);
  3633. /* if there was a short read -- discard anything left */
  3634. if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
  3635. rc = -ENODATA;
  3636. ctx->total_len += rdata->got_bytes;
  3637. }
  3638. list_del_init(&rdata->list);
  3639. kref_put(&rdata->refcount, cifs_uncached_readdata_release);
  3640. }
  3641. if (!ctx->direct_io)
  3642. ctx->total_len = ctx->len - iov_iter_count(to);
  3643. /* mask nodata case */
  3644. if (rc == -ENODATA)
  3645. rc = 0;
  3646. ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
  3647. mutex_unlock(&ctx->aio_mutex);
  3648. if (ctx->iocb && ctx->iocb->ki_complete)
  3649. ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
  3650. else
  3651. complete(&ctx->done);
  3652. }
  3653. static ssize_t __cifs_readv(
  3654. struct kiocb *iocb, struct iov_iter *to, bool direct)
  3655. {
  3656. size_t len;
  3657. struct file *file = iocb->ki_filp;
  3658. struct cifs_sb_info *cifs_sb;
  3659. struct cifsFileInfo *cfile;
  3660. struct cifs_tcon *tcon;
  3661. ssize_t rc, total_read = 0;
  3662. loff_t offset = iocb->ki_pos;
  3663. struct cifs_aio_ctx *ctx;
  3664. /*
  3665. * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
  3666. * fall back to data copy read path
  3667. * this could be improved by getting pages directly in ITER_KVEC
  3668. */
  3669. if (direct && iov_iter_is_kvec(to)) {
  3670. cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
  3671. direct = false;
  3672. }
  3673. len = iov_iter_count(to);
  3674. if (!len)
  3675. return 0;
  3676. cifs_sb = CIFS_FILE_SB(file);
  3677. cfile = file->private_data;
  3678. tcon = tlink_tcon(cfile->tlink);
  3679. if (!tcon->ses->server->ops->async_readv)
  3680. return -ENOSYS;
  3681. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  3682. cifs_dbg(FYI, "attempting read on write only file instance\n");
  3683. ctx = cifs_aio_ctx_alloc();
  3684. if (!ctx)
  3685. return -ENOMEM;
  3686. ctx->cfile = cifsFileInfo_get(cfile);
  3687. if (!is_sync_kiocb(iocb))
  3688. ctx->iocb = iocb;
  3689. if (user_backed_iter(to))
  3690. ctx->should_dirty = true;
  3691. if (direct) {
  3692. ctx->pos = offset;
  3693. ctx->direct_io = true;
  3694. ctx->iter = *to;
  3695. ctx->len = len;
  3696. } else {
  3697. rc = setup_aio_ctx_iter(ctx, to, ITER_DEST);
  3698. if (rc) {
  3699. kref_put(&ctx->refcount, cifs_aio_ctx_release);
  3700. return rc;
  3701. }
  3702. len = ctx->len;
  3703. }
  3704. if (direct) {
  3705. rc = filemap_write_and_wait_range(file->f_inode->i_mapping,
  3706. offset, offset + len - 1);
  3707. if (rc) {
  3708. kref_put(&ctx->refcount, cifs_aio_ctx_release);
  3709. return -EAGAIN;
  3710. }
  3711. }
  3712. /* grab a lock here due to read response handlers can access ctx */
  3713. mutex_lock(&ctx->aio_mutex);
  3714. rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
  3715. /* if at least one read request send succeeded, then reset rc */
  3716. if (!list_empty(&ctx->list))
  3717. rc = 0;
  3718. mutex_unlock(&ctx->aio_mutex);
  3719. if (rc) {
  3720. kref_put(&ctx->refcount, cifs_aio_ctx_release);
  3721. return rc;
  3722. }
  3723. if (!is_sync_kiocb(iocb)) {
  3724. kref_put(&ctx->refcount, cifs_aio_ctx_release);
  3725. return -EIOCBQUEUED;
  3726. }
  3727. rc = wait_for_completion_killable(&ctx->done);
  3728. if (rc) {
  3729. mutex_lock(&ctx->aio_mutex);
  3730. ctx->rc = rc = -EINTR;
  3731. total_read = ctx->total_len;
  3732. mutex_unlock(&ctx->aio_mutex);
  3733. } else {
  3734. rc = ctx->rc;
  3735. total_read = ctx->total_len;
  3736. }
  3737. kref_put(&ctx->refcount, cifs_aio_ctx_release);
  3738. if (total_read) {
  3739. iocb->ki_pos += total_read;
  3740. return total_read;
  3741. }
  3742. return rc;
  3743. }
  3744. ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
  3745. {
  3746. return __cifs_readv(iocb, to, true);
  3747. }
  3748. ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
  3749. {
  3750. return __cifs_readv(iocb, to, false);
  3751. }
  3752. ssize_t
  3753. cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
  3754. {
  3755. struct inode *inode = file_inode(iocb->ki_filp);
  3756. struct cifsInodeInfo *cinode = CIFS_I(inode);
  3757. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  3758. struct cifsFileInfo *cfile = (struct cifsFileInfo *)
  3759. iocb->ki_filp->private_data;
  3760. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  3761. int rc = -EACCES;
  3762. /*
  3763. * In strict cache mode we need to read from the server all the time
  3764. * if we don't have level II oplock because the server can delay mtime
  3765. * change - so we can't make a decision about inode invalidating.
  3766. * And we can also fail with pagereading if there are mandatory locks
  3767. * on pages affected by this read but not on the region from pos to
  3768. * pos+len-1.
  3769. */
  3770. if (!CIFS_CACHE_READ(cinode))
  3771. return cifs_user_readv(iocb, to);
  3772. if (cap_unix(tcon->ses) &&
  3773. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  3774. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  3775. return generic_file_read_iter(iocb, to);
  3776. /*
  3777. * We need to hold the sem to be sure nobody modifies lock list
  3778. * with a brlock that prevents reading.
  3779. */
  3780. down_read(&cinode->lock_sem);
  3781. if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
  3782. tcon->ses->server->vals->shared_lock_type,
  3783. 0, NULL, CIFS_READ_OP))
  3784. rc = generic_file_read_iter(iocb, to);
  3785. up_read(&cinode->lock_sem);
  3786. return rc;
  3787. }
  3788. static ssize_t
  3789. cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
  3790. {
  3791. int rc = -EACCES;
  3792. unsigned int bytes_read = 0;
  3793. unsigned int total_read;
  3794. unsigned int current_read_size;
  3795. unsigned int rsize;
  3796. struct cifs_sb_info *cifs_sb;
  3797. struct cifs_tcon *tcon;
  3798. struct TCP_Server_Info *server;
  3799. unsigned int xid;
  3800. char *cur_offset;
  3801. struct cifsFileInfo *open_file;
  3802. struct cifs_io_parms io_parms = {0};
  3803. int buf_type = CIFS_NO_BUFFER;
  3804. __u32 pid;
  3805. xid = get_xid();
  3806. cifs_sb = CIFS_FILE_SB(file);
  3807. /* FIXME: set up handlers for larger reads and/or convert to async */
  3808. rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize);
  3809. if (file->private_data == NULL) {
  3810. rc = -EBADF;
  3811. free_xid(xid);
  3812. return rc;
  3813. }
  3814. open_file = file->private_data;
  3815. tcon = tlink_tcon(open_file->tlink);
  3816. server = cifs_pick_channel(tcon->ses);
  3817. if (!server->ops->sync_read) {
  3818. free_xid(xid);
  3819. return -ENOSYS;
  3820. }
  3821. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  3822. pid = open_file->pid;
  3823. else
  3824. pid = current->tgid;
  3825. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  3826. cifs_dbg(FYI, "attempting read on write only file instance\n");
  3827. for (total_read = 0, cur_offset = read_data; read_size > total_read;
  3828. total_read += bytes_read, cur_offset += bytes_read) {
  3829. do {
  3830. current_read_size = min_t(uint, read_size - total_read,
  3831. rsize);
  3832. /*
  3833. * For windows me and 9x we do not want to request more
  3834. * than it negotiated since it will refuse the read
  3835. * then.
  3836. */
  3837. if (!(tcon->ses->capabilities &
  3838. tcon->ses->server->vals->cap_large_files)) {
  3839. current_read_size = min_t(uint,
  3840. current_read_size, CIFSMaxBufSize);
  3841. }
  3842. if (open_file->invalidHandle) {
  3843. rc = cifs_reopen_file(open_file, true);
  3844. if (rc != 0)
  3845. break;
  3846. }
  3847. io_parms.pid = pid;
  3848. io_parms.tcon = tcon;
  3849. io_parms.offset = *offset;
  3850. io_parms.length = current_read_size;
  3851. io_parms.server = server;
  3852. rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
  3853. &bytes_read, &cur_offset,
  3854. &buf_type);
  3855. } while (rc == -EAGAIN);
  3856. if (rc || (bytes_read == 0)) {
  3857. if (total_read) {
  3858. break;
  3859. } else {
  3860. free_xid(xid);
  3861. return rc;
  3862. }
  3863. } else {
  3864. cifs_stats_bytes_read(tcon, total_read);
  3865. *offset += bytes_read;
  3866. }
  3867. }
  3868. free_xid(xid);
  3869. return total_read;
  3870. }
  3871. /*
  3872. * If the page is mmap'ed into a process' page tables, then we need to make
  3873. * sure that it doesn't change while being written back.
  3874. */
  3875. static vm_fault_t
  3876. cifs_page_mkwrite(struct vm_fault *vmf)
  3877. {
  3878. struct page *page = vmf->page;
  3879. /* Wait for the page to be written to the cache before we allow it to
  3880. * be modified. We then assume the entire page will need writing back.
  3881. */
  3882. #ifdef CONFIG_CIFS_FSCACHE
  3883. if (PageFsCache(page) &&
  3884. wait_on_page_fscache_killable(page) < 0)
  3885. return VM_FAULT_RETRY;
  3886. #endif
  3887. wait_on_page_writeback(page);
  3888. if (lock_page_killable(page) < 0)
  3889. return VM_FAULT_RETRY;
  3890. return VM_FAULT_LOCKED;
  3891. }
  3892. static const struct vm_operations_struct cifs_file_vm_ops = {
  3893. .fault = filemap_fault,
  3894. .map_pages = filemap_map_pages,
  3895. .page_mkwrite = cifs_page_mkwrite,
  3896. };
  3897. int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
  3898. {
  3899. int xid, rc = 0;
  3900. struct inode *inode = file_inode(file);
  3901. xid = get_xid();
  3902. if (!CIFS_CACHE_READ(CIFS_I(inode)))
  3903. rc = cifs_zap_mapping(inode);
  3904. if (!rc)
  3905. rc = generic_file_mmap(file, vma);
  3906. if (!rc)
  3907. vma->vm_ops = &cifs_file_vm_ops;
  3908. free_xid(xid);
  3909. return rc;
  3910. }
  3911. int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
  3912. {
  3913. int rc, xid;
  3914. xid = get_xid();
  3915. rc = cifs_revalidate_file(file);
  3916. if (rc)
  3917. cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
  3918. rc);
  3919. if (!rc)
  3920. rc = generic_file_mmap(file, vma);
  3921. if (!rc)
  3922. vma->vm_ops = &cifs_file_vm_ops;
  3923. free_xid(xid);
  3924. return rc;
  3925. }
  3926. static void
  3927. cifs_readv_complete(struct work_struct *work)
  3928. {
  3929. unsigned int i, got_bytes;
  3930. struct cifs_readdata *rdata = container_of(work,
  3931. struct cifs_readdata, work);
  3932. got_bytes = rdata->got_bytes;
  3933. for (i = 0; i < rdata->nr_pages; i++) {
  3934. struct page *page = rdata->pages[i];
  3935. if (rdata->result == 0 ||
  3936. (rdata->result == -EAGAIN && got_bytes)) {
  3937. flush_dcache_page(page);
  3938. SetPageUptodate(page);
  3939. } else
  3940. SetPageError(page);
  3941. if (rdata->result == 0 ||
  3942. (rdata->result == -EAGAIN && got_bytes))
  3943. cifs_readpage_to_fscache(rdata->mapping->host, page);
  3944. unlock_page(page);
  3945. got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
  3946. put_page(page);
  3947. rdata->pages[i] = NULL;
  3948. }
  3949. kref_put(&rdata->refcount, cifs_readdata_release);
  3950. }
  3951. static int
  3952. readpages_fill_pages(struct TCP_Server_Info *server,
  3953. struct cifs_readdata *rdata, struct iov_iter *iter,
  3954. unsigned int len)
  3955. {
  3956. int result = 0;
  3957. unsigned int i;
  3958. u64 eof;
  3959. pgoff_t eof_index;
  3960. unsigned int nr_pages = rdata->nr_pages;
  3961. unsigned int page_offset = rdata->page_offset;
  3962. /* determine the eof that the server (probably) has */
  3963. eof = CIFS_I(rdata->mapping->host)->server_eof;
  3964. eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
  3965. cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
  3966. rdata->got_bytes = 0;
  3967. rdata->tailsz = PAGE_SIZE;
  3968. for (i = 0; i < nr_pages; i++) {
  3969. struct page *page = rdata->pages[i];
  3970. unsigned int to_read = rdata->pagesz;
  3971. size_t n;
  3972. if (i == 0)
  3973. to_read -= page_offset;
  3974. else
  3975. page_offset = 0;
  3976. n = to_read;
  3977. if (len >= to_read) {
  3978. len -= to_read;
  3979. } else if (len > 0) {
  3980. /* enough for partial page, fill and zero the rest */
  3981. zero_user(page, len + page_offset, to_read - len);
  3982. n = rdata->tailsz = len;
  3983. len = 0;
  3984. } else if (page->index > eof_index) {
  3985. /*
  3986. * The VFS will not try to do readahead past the
  3987. * i_size, but it's possible that we have outstanding
  3988. * writes with gaps in the middle and the i_size hasn't
  3989. * caught up yet. Populate those with zeroed out pages
  3990. * to prevent the VFS from repeatedly attempting to
  3991. * fill them until the writes are flushed.
  3992. */
  3993. zero_user(page, 0, PAGE_SIZE);
  3994. flush_dcache_page(page);
  3995. SetPageUptodate(page);
  3996. unlock_page(page);
  3997. put_page(page);
  3998. rdata->pages[i] = NULL;
  3999. rdata->nr_pages--;
  4000. continue;
  4001. } else {
  4002. /* no need to hold page hostage */
  4003. unlock_page(page);
  4004. put_page(page);
  4005. rdata->pages[i] = NULL;
  4006. rdata->nr_pages--;
  4007. continue;
  4008. }
  4009. if (iter)
  4010. result = copy_page_from_iter(
  4011. page, page_offset, n, iter);
  4012. #ifdef CONFIG_CIFS_SMB_DIRECT
  4013. else if (rdata->mr)
  4014. result = n;
  4015. #endif
  4016. else
  4017. result = cifs_read_page_from_socket(
  4018. server, page, page_offset, n);
  4019. if (result < 0)
  4020. break;
  4021. rdata->got_bytes += result;
  4022. }
  4023. return result != -ECONNABORTED && rdata->got_bytes > 0 ?
  4024. rdata->got_bytes : result;
  4025. }
  4026. static int
  4027. cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
  4028. struct cifs_readdata *rdata, unsigned int len)
  4029. {
  4030. return readpages_fill_pages(server, rdata, NULL, len);
  4031. }
  4032. static int
  4033. cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
  4034. struct cifs_readdata *rdata,
  4035. struct iov_iter *iter)
  4036. {
  4037. return readpages_fill_pages(server, rdata, iter, iter->count);
  4038. }
  4039. static void cifs_readahead(struct readahead_control *ractl)
  4040. {
  4041. int rc;
  4042. struct cifsFileInfo *open_file = ractl->file->private_data;
  4043. struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(ractl->file);
  4044. struct TCP_Server_Info *server;
  4045. pid_t pid;
  4046. unsigned int xid, nr_pages, last_batch_size = 0, cache_nr_pages = 0;
  4047. pgoff_t next_cached = ULONG_MAX;
  4048. bool caching = fscache_cookie_enabled(cifs_inode_cookie(ractl->mapping->host)) &&
  4049. cifs_inode_cookie(ractl->mapping->host)->cache_priv;
  4050. bool check_cache = caching;
  4051. xid = get_xid();
  4052. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  4053. pid = open_file->pid;
  4054. else
  4055. pid = current->tgid;
  4056. rc = 0;
  4057. server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
  4058. cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
  4059. __func__, ractl->file, ractl->mapping, readahead_count(ractl));
  4060. /*
  4061. * Chop the readahead request up into rsize-sized read requests.
  4062. */
  4063. while ((nr_pages = readahead_count(ractl) - last_batch_size)) {
  4064. unsigned int i, got, rsize;
  4065. struct page *page;
  4066. struct cifs_readdata *rdata;
  4067. struct cifs_credits credits_on_stack;
  4068. struct cifs_credits *credits = &credits_on_stack;
  4069. pgoff_t index = readahead_index(ractl) + last_batch_size;
  4070. /*
  4071. * Find out if we have anything cached in the range of
  4072. * interest, and if so, where the next chunk of cached data is.
  4073. */
  4074. if (caching) {
  4075. if (check_cache) {
  4076. rc = cifs_fscache_query_occupancy(
  4077. ractl->mapping->host, index, nr_pages,
  4078. &next_cached, &cache_nr_pages);
  4079. if (rc < 0)
  4080. caching = false;
  4081. check_cache = false;
  4082. }
  4083. if (index == next_cached) {
  4084. /*
  4085. * TODO: Send a whole batch of pages to be read
  4086. * by the cache.
  4087. */
  4088. struct folio *folio = readahead_folio(ractl);
  4089. last_batch_size = folio_nr_pages(folio);
  4090. if (cifs_readpage_from_fscache(ractl->mapping->host,
  4091. &folio->page) < 0) {
  4092. /*
  4093. * TODO: Deal with cache read failure
  4094. * here, but for the moment, delegate
  4095. * that to readpage.
  4096. */
  4097. caching = false;
  4098. }
  4099. folio_unlock(folio);
  4100. next_cached++;
  4101. cache_nr_pages--;
  4102. if (cache_nr_pages == 0)
  4103. check_cache = true;
  4104. continue;
  4105. }
  4106. }
  4107. if (open_file->invalidHandle) {
  4108. rc = cifs_reopen_file(open_file, true);
  4109. if (rc) {
  4110. if (rc == -EAGAIN)
  4111. continue;
  4112. break;
  4113. }
  4114. }
  4115. if (cifs_sb->ctx->rsize == 0)
  4116. cifs_sb->ctx->rsize =
  4117. server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
  4118. cifs_sb->ctx);
  4119. rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
  4120. &rsize, credits);
  4121. if (rc)
  4122. break;
  4123. nr_pages = min_t(size_t, rsize / PAGE_SIZE, readahead_count(ractl));
  4124. nr_pages = min_t(size_t, nr_pages, next_cached - index);
  4125. /*
  4126. * Give up immediately if rsize is too small to read an entire
  4127. * page. The VFS will fall back to readpage. We should never
  4128. * reach this point however since we set ra_pages to 0 when the
  4129. * rsize is smaller than a cache page.
  4130. */
  4131. if (unlikely(!nr_pages)) {
  4132. add_credits_and_wake_if(server, credits, 0);
  4133. break;
  4134. }
  4135. rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
  4136. if (!rdata) {
  4137. /* best to give up if we're out of mem */
  4138. add_credits_and_wake_if(server, credits, 0);
  4139. break;
  4140. }
  4141. got = __readahead_batch(ractl, rdata->pages, nr_pages);
  4142. if (got != nr_pages) {
  4143. pr_warn("__readahead_batch() returned %u/%u\n",
  4144. got, nr_pages);
  4145. nr_pages = got;
  4146. }
  4147. rdata->nr_pages = nr_pages;
  4148. rdata->bytes = readahead_batch_length(ractl);
  4149. rdata->cfile = cifsFileInfo_get(open_file);
  4150. rdata->server = server;
  4151. rdata->mapping = ractl->mapping;
  4152. rdata->offset = readahead_pos(ractl);
  4153. rdata->pid = pid;
  4154. rdata->pagesz = PAGE_SIZE;
  4155. rdata->tailsz = PAGE_SIZE;
  4156. rdata->read_into_pages = cifs_readpages_read_into_pages;
  4157. rdata->copy_into_pages = cifs_readpages_copy_into_pages;
  4158. rdata->credits = credits_on_stack;
  4159. rc = adjust_credits(server, &rdata->credits, rdata->bytes);
  4160. if (!rc) {
  4161. if (rdata->cfile->invalidHandle)
  4162. rc = -EAGAIN;
  4163. else
  4164. rc = server->ops->async_readv(rdata);
  4165. }
  4166. if (rc) {
  4167. add_credits_and_wake_if(server, &rdata->credits, 0);
  4168. for (i = 0; i < rdata->nr_pages; i++) {
  4169. page = rdata->pages[i];
  4170. unlock_page(page);
  4171. put_page(page);
  4172. }
  4173. /* Fallback to the readpage in error/reconnect cases */
  4174. kref_put(&rdata->refcount, cifs_readdata_release);
  4175. break;
  4176. }
  4177. kref_put(&rdata->refcount, cifs_readdata_release);
  4178. last_batch_size = nr_pages;
  4179. }
  4180. free_xid(xid);
  4181. }
  4182. /*
  4183. * cifs_readpage_worker must be called with the page pinned
  4184. */
  4185. static int cifs_readpage_worker(struct file *file, struct page *page,
  4186. loff_t *poffset)
  4187. {
  4188. char *read_data;
  4189. int rc;
  4190. /* Is the page cached? */
  4191. rc = cifs_readpage_from_fscache(file_inode(file), page);
  4192. if (rc == 0)
  4193. goto read_complete;
  4194. read_data = kmap(page);
  4195. /* for reads over a certain size could initiate async read ahead */
  4196. rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
  4197. if (rc < 0)
  4198. goto io_error;
  4199. else
  4200. cifs_dbg(FYI, "Bytes read %d\n", rc);
  4201. /* we do not want atime to be less than mtime, it broke some apps */
  4202. file_inode(file)->i_atime = current_time(file_inode(file));
  4203. if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
  4204. file_inode(file)->i_atime = file_inode(file)->i_mtime;
  4205. else
  4206. file_inode(file)->i_atime = current_time(file_inode(file));
  4207. if (PAGE_SIZE > rc)
  4208. memset(read_data + rc, 0, PAGE_SIZE - rc);
  4209. flush_dcache_page(page);
  4210. SetPageUptodate(page);
  4211. /* send this page to the cache */
  4212. cifs_readpage_to_fscache(file_inode(file), page);
  4213. rc = 0;
  4214. io_error:
  4215. kunmap(page);
  4216. read_complete:
  4217. unlock_page(page);
  4218. return rc;
  4219. }
  4220. static int cifs_read_folio(struct file *file, struct folio *folio)
  4221. {
  4222. struct page *page = &folio->page;
  4223. loff_t offset = page_file_offset(page);
  4224. int rc = -EACCES;
  4225. unsigned int xid;
  4226. xid = get_xid();
  4227. if (file->private_data == NULL) {
  4228. rc = -EBADF;
  4229. free_xid(xid);
  4230. return rc;
  4231. }
  4232. cifs_dbg(FYI, "read_folio %p at offset %d 0x%x\n",
  4233. page, (int)offset, (int)offset);
  4234. rc = cifs_readpage_worker(file, page, &offset);
  4235. free_xid(xid);
  4236. return rc;
  4237. }
  4238. static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
  4239. {
  4240. struct cifsFileInfo *open_file;
  4241. spin_lock(&cifs_inode->open_file_lock);
  4242. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  4243. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  4244. spin_unlock(&cifs_inode->open_file_lock);
  4245. return 1;
  4246. }
  4247. }
  4248. spin_unlock(&cifs_inode->open_file_lock);
  4249. return 0;
  4250. }
  4251. /* We do not want to update the file size from server for inodes
  4252. open for write - to avoid races with writepage extending
  4253. the file - in the future we could consider allowing
  4254. refreshing the inode only on increases in the file size
  4255. but this is tricky to do without racing with writebehind
  4256. page caching in the current Linux kernel design */
  4257. bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
  4258. {
  4259. if (!cifsInode)
  4260. return true;
  4261. if (is_inode_writable(cifsInode)) {
  4262. /* This inode is open for write at least once */
  4263. struct cifs_sb_info *cifs_sb;
  4264. cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb);
  4265. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  4266. /* since no page cache to corrupt on directio
  4267. we can change size safely */
  4268. return true;
  4269. }
  4270. if (i_size_read(&cifsInode->netfs.inode) < end_of_file)
  4271. return true;
  4272. return false;
  4273. } else
  4274. return true;
  4275. }
  4276. static int cifs_write_begin(struct file *file, struct address_space *mapping,
  4277. loff_t pos, unsigned len,
  4278. struct page **pagep, void **fsdata)
  4279. {
  4280. int oncethru = 0;
  4281. pgoff_t index = pos >> PAGE_SHIFT;
  4282. loff_t offset = pos & (PAGE_SIZE - 1);
  4283. loff_t page_start = pos & PAGE_MASK;
  4284. loff_t i_size;
  4285. struct page *page;
  4286. int rc = 0;
  4287. cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
  4288. start:
  4289. page = grab_cache_page_write_begin(mapping, index);
  4290. if (!page) {
  4291. rc = -ENOMEM;
  4292. goto out;
  4293. }
  4294. if (PageUptodate(page))
  4295. goto out;
  4296. /*
  4297. * If we write a full page it will be up to date, no need to read from
  4298. * the server. If the write is short, we'll end up doing a sync write
  4299. * instead.
  4300. */
  4301. if (len == PAGE_SIZE)
  4302. goto out;
  4303. /*
  4304. * optimize away the read when we have an oplock, and we're not
  4305. * expecting to use any of the data we'd be reading in. That
  4306. * is, when the page lies beyond the EOF, or straddles the EOF
  4307. * and the write will cover all of the existing data.
  4308. */
  4309. if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
  4310. i_size = i_size_read(mapping->host);
  4311. if (page_start >= i_size ||
  4312. (offset == 0 && (pos + len) >= i_size)) {
  4313. zero_user_segments(page, 0, offset,
  4314. offset + len,
  4315. PAGE_SIZE);
  4316. /*
  4317. * PageChecked means that the parts of the page
  4318. * to which we're not writing are considered up
  4319. * to date. Once the data is copied to the
  4320. * page, it can be set uptodate.
  4321. */
  4322. SetPageChecked(page);
  4323. goto out;
  4324. }
  4325. }
  4326. if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
  4327. /*
  4328. * might as well read a page, it is fast enough. If we get
  4329. * an error, we don't need to return it. cifs_write_end will
  4330. * do a sync write instead since PG_uptodate isn't set.
  4331. */
  4332. cifs_readpage_worker(file, page, &page_start);
  4333. put_page(page);
  4334. oncethru = 1;
  4335. goto start;
  4336. } else {
  4337. /* we could try using another file handle if there is one -
  4338. but how would we lock it to prevent close of that handle
  4339. racing with this read? In any case
  4340. this will be written out by write_end so is fine */
  4341. }
  4342. out:
  4343. *pagep = page;
  4344. return rc;
  4345. }
  4346. static bool cifs_release_folio(struct folio *folio, gfp_t gfp)
  4347. {
  4348. if (folio_test_private(folio))
  4349. return 0;
  4350. if (folio_test_fscache(folio)) {
  4351. if (current_is_kswapd() || !(gfp & __GFP_FS))
  4352. return false;
  4353. folio_wait_fscache(folio);
  4354. }
  4355. fscache_note_page_release(cifs_inode_cookie(folio->mapping->host));
  4356. return true;
  4357. }
  4358. static void cifs_invalidate_folio(struct folio *folio, size_t offset,
  4359. size_t length)
  4360. {
  4361. folio_wait_fscache(folio);
  4362. }
  4363. static int cifs_launder_folio(struct folio *folio)
  4364. {
  4365. int rc = 0;
  4366. loff_t range_start = folio_pos(folio);
  4367. loff_t range_end = range_start + folio_size(folio);
  4368. struct writeback_control wbc = {
  4369. .sync_mode = WB_SYNC_ALL,
  4370. .nr_to_write = 0,
  4371. .range_start = range_start,
  4372. .range_end = range_end,
  4373. };
  4374. cifs_dbg(FYI, "Launder page: %lu\n", folio->index);
  4375. if (folio_clear_dirty_for_io(folio))
  4376. rc = cifs_writepage_locked(&folio->page, &wbc);
  4377. folio_wait_fscache(folio);
  4378. return rc;
  4379. }
  4380. void cifs_oplock_break(struct work_struct *work)
  4381. {
  4382. struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
  4383. oplock_break);
  4384. struct inode *inode = d_inode(cfile->dentry);
  4385. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  4386. struct cifsInodeInfo *cinode = CIFS_I(inode);
  4387. struct cifs_tcon *tcon;
  4388. struct TCP_Server_Info *server;
  4389. struct tcon_link *tlink;
  4390. int rc = 0;
  4391. bool purge_cache = false, oplock_break_cancelled;
  4392. __u64 persistent_fid, volatile_fid;
  4393. __u16 net_fid;
  4394. wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
  4395. TASK_UNINTERRUPTIBLE);
  4396. tlink = cifs_sb_tlink(cifs_sb);
  4397. if (IS_ERR(tlink))
  4398. goto out;
  4399. tcon = tlink_tcon(tlink);
  4400. server = tcon->ses->server;
  4401. server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
  4402. cfile->oplock_epoch, &purge_cache);
  4403. if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
  4404. cifs_has_mand_locks(cinode)) {
  4405. cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
  4406. inode);
  4407. cinode->oplock = 0;
  4408. }
  4409. if (inode && S_ISREG(inode->i_mode)) {
  4410. if (CIFS_CACHE_READ(cinode))
  4411. break_lease(inode, O_RDONLY);
  4412. else
  4413. break_lease(inode, O_WRONLY);
  4414. rc = filemap_fdatawrite(inode->i_mapping);
  4415. if (!CIFS_CACHE_READ(cinode) || purge_cache) {
  4416. rc = filemap_fdatawait(inode->i_mapping);
  4417. mapping_set_error(inode->i_mapping, rc);
  4418. cifs_zap_mapping(inode);
  4419. }
  4420. cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
  4421. if (CIFS_CACHE_WRITE(cinode))
  4422. goto oplock_break_ack;
  4423. }
  4424. rc = cifs_push_locks(cfile);
  4425. if (rc)
  4426. cifs_dbg(VFS, "Push locks rc = %d\n", rc);
  4427. oplock_break_ack:
  4428. /*
  4429. * When oplock break is received and there are no active
  4430. * file handles but cached, then schedule deferred close immediately.
  4431. * So, new open will not use cached handle.
  4432. */
  4433. if (!CIFS_CACHE_HANDLE(cinode) && !list_empty(&cinode->deferred_closes))
  4434. cifs_close_deferred_file(cinode);
  4435. persistent_fid = cfile->fid.persistent_fid;
  4436. volatile_fid = cfile->fid.volatile_fid;
  4437. net_fid = cfile->fid.netfid;
  4438. oplock_break_cancelled = cfile->oplock_break_cancelled;
  4439. _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
  4440. /*
  4441. * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require
  4442. * an acknowledgment to be sent when the file has already been closed.
  4443. */
  4444. spin_lock(&cinode->open_file_lock);
  4445. /* check list empty since can race with kill_sb calling tree disconnect */
  4446. if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) {
  4447. spin_unlock(&cinode->open_file_lock);
  4448. rc = server->ops->oplock_response(tcon, persistent_fid,
  4449. volatile_fid, net_fid, cinode);
  4450. cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
  4451. } else
  4452. spin_unlock(&cinode->open_file_lock);
  4453. cifs_put_tlink(tlink);
  4454. out:
  4455. cifs_done_oplock_break(cinode);
  4456. }
  4457. /*
  4458. * The presence of cifs_direct_io() in the address space ops vector
  4459. * allowes open() O_DIRECT flags which would have failed otherwise.
  4460. *
  4461. * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
  4462. * so this method should never be called.
  4463. *
  4464. * Direct IO is not yet supported in the cached mode.
  4465. */
  4466. static ssize_t
  4467. cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
  4468. {
  4469. /*
  4470. * FIXME
  4471. * Eventually need to support direct IO for non forcedirectio mounts
  4472. */
  4473. return -EINVAL;
  4474. }
  4475. static int cifs_swap_activate(struct swap_info_struct *sis,
  4476. struct file *swap_file, sector_t *span)
  4477. {
  4478. struct cifsFileInfo *cfile = swap_file->private_data;
  4479. struct inode *inode = swap_file->f_mapping->host;
  4480. unsigned long blocks;
  4481. long long isize;
  4482. cifs_dbg(FYI, "swap activate\n");
  4483. if (!swap_file->f_mapping->a_ops->swap_rw)
  4484. /* Cannot support swap */
  4485. return -EINVAL;
  4486. spin_lock(&inode->i_lock);
  4487. blocks = inode->i_blocks;
  4488. isize = inode->i_size;
  4489. spin_unlock(&inode->i_lock);
  4490. if (blocks*512 < isize) {
  4491. pr_warn("swap activate: swapfile has holes\n");
  4492. return -EINVAL;
  4493. }
  4494. *span = sis->pages;
  4495. pr_warn_once("Swap support over SMB3 is experimental\n");
  4496. /*
  4497. * TODO: consider adding ACL (or documenting how) to prevent other
  4498. * users (on this or other systems) from reading it
  4499. */
  4500. /* TODO: add sk_set_memalloc(inet) or similar */
  4501. if (cfile)
  4502. cfile->swapfile = true;
  4503. /*
  4504. * TODO: Since file already open, we can't open with DENY_ALL here
  4505. * but we could add call to grab a byte range lock to prevent others
  4506. * from reading or writing the file
  4507. */
  4508. sis->flags |= SWP_FS_OPS;
  4509. return add_swap_extent(sis, 0, sis->max, 0);
  4510. }
  4511. static void cifs_swap_deactivate(struct file *file)
  4512. {
  4513. struct cifsFileInfo *cfile = file->private_data;
  4514. cifs_dbg(FYI, "swap deactivate\n");
  4515. /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
  4516. if (cfile)
  4517. cfile->swapfile = false;
  4518. /* do we need to unpin (or unlock) the file */
  4519. }
  4520. /*
  4521. * Mark a page as having been made dirty and thus needing writeback. We also
  4522. * need to pin the cache object to write back to.
  4523. */
  4524. #ifdef CONFIG_CIFS_FSCACHE
  4525. static bool cifs_dirty_folio(struct address_space *mapping, struct folio *folio)
  4526. {
  4527. return fscache_dirty_folio(mapping, folio,
  4528. cifs_inode_cookie(mapping->host));
  4529. }
  4530. #else
  4531. #define cifs_dirty_folio filemap_dirty_folio
  4532. #endif
  4533. const struct address_space_operations cifs_addr_ops = {
  4534. .read_folio = cifs_read_folio,
  4535. .readahead = cifs_readahead,
  4536. .writepage = cifs_writepage,
  4537. .writepages = cifs_writepages,
  4538. .write_begin = cifs_write_begin,
  4539. .write_end = cifs_write_end,
  4540. .dirty_folio = cifs_dirty_folio,
  4541. .release_folio = cifs_release_folio,
  4542. .direct_IO = cifs_direct_io,
  4543. .invalidate_folio = cifs_invalidate_folio,
  4544. .launder_folio = cifs_launder_folio,
  4545. /*
  4546. * TODO: investigate and if useful we could add an cifs_migratePage
  4547. * helper (under an CONFIG_MIGRATION) in the future, and also
  4548. * investigate and add an is_dirty_writeback helper if needed
  4549. */
  4550. .swap_activate = cifs_swap_activate,
  4551. .swap_deactivate = cifs_swap_deactivate,
  4552. };
  4553. /*
  4554. * cifs_readahead requires the server to support a buffer large enough to
  4555. * contain the header plus one complete page of data. Otherwise, we need
  4556. * to leave cifs_readahead out of the address space operations.
  4557. */
  4558. const struct address_space_operations cifs_addr_ops_smallbuf = {
  4559. .read_folio = cifs_read_folio,
  4560. .writepage = cifs_writepage,
  4561. .writepages = cifs_writepages,
  4562. .write_begin = cifs_write_begin,
  4563. .write_end = cifs_write_end,
  4564. .dirty_folio = cifs_dirty_folio,
  4565. .release_folio = cifs_release_folio,
  4566. .invalidate_folio = cifs_invalidate_folio,
  4567. .launder_folio = cifs_launder_folio,
  4568. };