file.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897
  1. /*
  2. * fs/cifs/file.c
  3. *
  4. * vfs operations that deal with files
  5. *
  6. * Copyright (C) International Business Machines Corp., 2002,2010
  7. * Author(s): Steve French (sfrench@us.ibm.com)
  8. * Jeremy Allison (jra@samba.org)
  9. *
  10. * This library is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published
  12. * by the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18. * the GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/stat.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/pagevec.h>
  30. #include <linux/writeback.h>
  31. #include <linux/task_io_accounting_ops.h>
  32. #include <linux/delay.h>
  33. #include <linux/mount.h>
  34. #include <linux/slab.h>
  35. #include <linux/swap.h>
  36. #include <asm/div64.h>
  37. #include "cifsfs.h"
  38. #include "cifspdu.h"
  39. #include "cifsglob.h"
  40. #include "cifsproto.h"
  41. #include "cifs_unicode.h"
  42. #include "cifs_debug.h"
  43. #include "cifs_fs_sb.h"
  44. #include "fscache.h"
  45. static inline int cifs_convert_flags(unsigned int flags)
  46. {
  47. if ((flags & O_ACCMODE) == O_RDONLY)
  48. return GENERIC_READ;
  49. else if ((flags & O_ACCMODE) == O_WRONLY)
  50. return GENERIC_WRITE;
  51. else if ((flags & O_ACCMODE) == O_RDWR) {
  52. /* GENERIC_ALL is too much permission to request
  53. can cause unnecessary access denied on create */
  54. /* return GENERIC_ALL; */
  55. return (GENERIC_READ | GENERIC_WRITE);
  56. }
  57. return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
  58. FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
  59. FILE_READ_DATA);
  60. }
  61. static u32 cifs_posix_convert_flags(unsigned int flags)
  62. {
  63. u32 posix_flags = 0;
  64. if ((flags & O_ACCMODE) == O_RDONLY)
  65. posix_flags = SMB_O_RDONLY;
  66. else if ((flags & O_ACCMODE) == O_WRONLY)
  67. posix_flags = SMB_O_WRONLY;
  68. else if ((flags & O_ACCMODE) == O_RDWR)
  69. posix_flags = SMB_O_RDWR;
  70. if (flags & O_CREAT) {
  71. posix_flags |= SMB_O_CREAT;
  72. if (flags & O_EXCL)
  73. posix_flags |= SMB_O_EXCL;
  74. } else if (flags & O_EXCL)
  75. cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
  76. current->comm, current->tgid);
  77. if (flags & O_TRUNC)
  78. posix_flags |= SMB_O_TRUNC;
  79. /* be safe and imply O_SYNC for O_DSYNC */
  80. if (flags & O_DSYNC)
  81. posix_flags |= SMB_O_SYNC;
  82. if (flags & O_DIRECTORY)
  83. posix_flags |= SMB_O_DIRECTORY;
  84. if (flags & O_NOFOLLOW)
  85. posix_flags |= SMB_O_NOFOLLOW;
  86. if (flags & O_DIRECT)
  87. posix_flags |= SMB_O_DIRECT;
  88. return posix_flags;
  89. }
  90. static inline int cifs_get_disposition(unsigned int flags)
  91. {
  92. if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  93. return FILE_CREATE;
  94. else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  95. return FILE_OVERWRITE_IF;
  96. else if ((flags & O_CREAT) == O_CREAT)
  97. return FILE_OPEN_IF;
  98. else if ((flags & O_TRUNC) == O_TRUNC)
  99. return FILE_OVERWRITE;
  100. else
  101. return FILE_OPEN;
  102. }
  103. int cifs_posix_open(char *full_path, struct inode **pinode,
  104. struct super_block *sb, int mode, unsigned int f_flags,
  105. __u32 *poplock, __u16 *pnetfid, unsigned int xid)
  106. {
  107. int rc;
  108. FILE_UNIX_BASIC_INFO *presp_data;
  109. __u32 posix_flags = 0;
  110. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  111. struct cifs_fattr fattr;
  112. struct tcon_link *tlink;
  113. struct cifs_tcon *tcon;
  114. cifs_dbg(FYI, "posix open %s\n", full_path);
  115. presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  116. if (presp_data == NULL)
  117. return -ENOMEM;
  118. tlink = cifs_sb_tlink(cifs_sb);
  119. if (IS_ERR(tlink)) {
  120. rc = PTR_ERR(tlink);
  121. goto posix_open_ret;
  122. }
  123. tcon = tlink_tcon(tlink);
  124. mode &= ~current_umask();
  125. posix_flags = cifs_posix_convert_flags(f_flags);
  126. rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
  127. poplock, full_path, cifs_sb->local_nls,
  128. cifs_remap(cifs_sb));
  129. cifs_put_tlink(tlink);
  130. if (rc)
  131. goto posix_open_ret;
  132. if (presp_data->Type == cpu_to_le32(-1))
  133. goto posix_open_ret; /* open ok, caller does qpathinfo */
  134. if (!pinode)
  135. goto posix_open_ret; /* caller does not need info */
  136. cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
  137. /* get new inode and set it up */
  138. if (*pinode == NULL) {
  139. cifs_fill_uniqueid(sb, &fattr);
  140. *pinode = cifs_iget(sb, &fattr);
  141. if (!*pinode) {
  142. rc = -ENOMEM;
  143. goto posix_open_ret;
  144. }
  145. } else {
  146. cifs_fattr_to_inode(*pinode, &fattr);
  147. }
  148. posix_open_ret:
  149. kfree(presp_data);
  150. return rc;
  151. }
  152. static int
  153. cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
  154. struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
  155. struct cifs_fid *fid, unsigned int xid)
  156. {
  157. int rc;
  158. int desired_access;
  159. int disposition;
  160. int create_options = CREATE_NOT_DIR;
  161. FILE_ALL_INFO *buf;
  162. struct TCP_Server_Info *server = tcon->ses->server;
  163. struct cifs_open_parms oparms;
  164. if (!server->ops->open)
  165. return -ENOSYS;
  166. desired_access = cifs_convert_flags(f_flags);
  167. /*********************************************************************
  168. * open flag mapping table:
  169. *
  170. * POSIX Flag CIFS Disposition
  171. * ---------- ----------------
  172. * O_CREAT FILE_OPEN_IF
  173. * O_CREAT | O_EXCL FILE_CREATE
  174. * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
  175. * O_TRUNC FILE_OVERWRITE
  176. * none of the above FILE_OPEN
  177. *
  178. * Note that there is not a direct match between disposition
  179. * FILE_SUPERSEDE (ie create whether or not file exists although
  180. * O_CREAT | O_TRUNC is similar but truncates the existing
  181. * file rather than creating a new file as FILE_SUPERSEDE does
  182. * (which uses the attributes / metadata passed in on open call)
  183. *?
  184. *? O_SYNC is a reasonable match to CIFS writethrough flag
  185. *? and the read write flags match reasonably. O_LARGEFILE
  186. *? is irrelevant because largefile support is always used
  187. *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
  188. * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
  189. *********************************************************************/
  190. disposition = cifs_get_disposition(f_flags);
  191. /* BB pass O_SYNC flag through on file attributes .. BB */
  192. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  193. if (!buf)
  194. return -ENOMEM;
  195. if (backup_cred(cifs_sb))
  196. create_options |= CREATE_OPEN_BACKUP_INTENT;
  197. oparms.tcon = tcon;
  198. oparms.cifs_sb = cifs_sb;
  199. oparms.desired_access = desired_access;
  200. oparms.create_options = create_options;
  201. oparms.disposition = disposition;
  202. oparms.path = full_path;
  203. oparms.fid = fid;
  204. oparms.reconnect = false;
  205. rc = server->ops->open(xid, &oparms, oplock, buf);
  206. if (rc)
  207. goto out;
  208. if (tcon->unix_ext)
  209. rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
  210. xid);
  211. else
  212. rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
  213. xid, fid);
  214. out:
  215. kfree(buf);
  216. return rc;
  217. }
  218. static bool
  219. cifs_has_mand_locks(struct cifsInodeInfo *cinode)
  220. {
  221. struct cifs_fid_locks *cur;
  222. bool has_locks = false;
  223. down_read(&cinode->lock_sem);
  224. list_for_each_entry(cur, &cinode->llist, llist) {
  225. if (!list_empty(&cur->locks)) {
  226. has_locks = true;
  227. break;
  228. }
  229. }
  230. up_read(&cinode->lock_sem);
  231. return has_locks;
  232. }
  233. struct cifsFileInfo *
  234. cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
  235. struct tcon_link *tlink, __u32 oplock)
  236. {
  237. struct dentry *dentry = file->f_path.dentry;
  238. struct inode *inode = d_inode(dentry);
  239. struct cifsInodeInfo *cinode = CIFS_I(inode);
  240. struct cifsFileInfo *cfile;
  241. struct cifs_fid_locks *fdlocks;
  242. struct cifs_tcon *tcon = tlink_tcon(tlink);
  243. struct TCP_Server_Info *server = tcon->ses->server;
  244. cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  245. if (cfile == NULL)
  246. return cfile;
  247. fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
  248. if (!fdlocks) {
  249. kfree(cfile);
  250. return NULL;
  251. }
  252. INIT_LIST_HEAD(&fdlocks->locks);
  253. fdlocks->cfile = cfile;
  254. cfile->llist = fdlocks;
  255. down_write(&cinode->lock_sem);
  256. list_add(&fdlocks->llist, &cinode->llist);
  257. up_write(&cinode->lock_sem);
  258. cfile->count = 1;
  259. cfile->pid = current->tgid;
  260. cfile->uid = current_fsuid();
  261. cfile->dentry = dget(dentry);
  262. cfile->f_flags = file->f_flags;
  263. cfile->invalidHandle = false;
  264. cfile->tlink = cifs_get_tlink(tlink);
  265. INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
  266. mutex_init(&cfile->fh_mutex);
  267. cifs_sb_active(inode->i_sb);
  268. /*
  269. * If the server returned a read oplock and we have mandatory brlocks,
  270. * set oplock level to None.
  271. */
  272. if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
  273. cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
  274. oplock = 0;
  275. }
  276. spin_lock(&cifs_file_list_lock);
  277. if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
  278. oplock = fid->pending_open->oplock;
  279. list_del(&fid->pending_open->olist);
  280. fid->purge_cache = false;
  281. server->ops->set_fid(cfile, fid, oplock);
  282. list_add(&cfile->tlist, &tcon->openFileList);
  283. /* if readable file instance put first in list*/
  284. if (file->f_mode & FMODE_READ)
  285. list_add(&cfile->flist, &cinode->openFileList);
  286. else
  287. list_add_tail(&cfile->flist, &cinode->openFileList);
  288. spin_unlock(&cifs_file_list_lock);
  289. if (fid->purge_cache)
  290. cifs_zap_mapping(inode);
  291. file->private_data = cfile;
  292. return cfile;
  293. }
  294. struct cifsFileInfo *
  295. cifsFileInfo_get(struct cifsFileInfo *cifs_file)
  296. {
  297. spin_lock(&cifs_file_list_lock);
  298. cifsFileInfo_get_locked(cifs_file);
  299. spin_unlock(&cifs_file_list_lock);
  300. return cifs_file;
  301. }
  302. /*
  303. * Release a reference on the file private data. This may involve closing
  304. * the filehandle out on the server. Must be called without holding
  305. * cifs_file_list_lock.
  306. */
  307. void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
  308. {
  309. struct inode *inode = d_inode(cifs_file->dentry);
  310. struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
  311. struct TCP_Server_Info *server = tcon->ses->server;
  312. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  313. struct super_block *sb = inode->i_sb;
  314. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  315. struct cifsLockInfo *li, *tmp;
  316. struct cifs_fid fid;
  317. struct cifs_pending_open open;
  318. bool oplock_break_cancelled;
  319. spin_lock(&cifs_file_list_lock);
  320. if (--cifs_file->count > 0) {
  321. spin_unlock(&cifs_file_list_lock);
  322. return;
  323. }
  324. if (server->ops->get_lease_key)
  325. server->ops->get_lease_key(inode, &fid);
  326. /* store open in pending opens to make sure we don't miss lease break */
  327. cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
  328. /* remove it from the lists */
  329. list_del(&cifs_file->flist);
  330. list_del(&cifs_file->tlist);
  331. if (list_empty(&cifsi->openFileList)) {
  332. cifs_dbg(FYI, "closing last open instance for inode %p\n",
  333. d_inode(cifs_file->dentry));
  334. /*
  335. * In strict cache mode we need invalidate mapping on the last
  336. * close because it may cause a error when we open this file
  337. * again and get at least level II oplock.
  338. */
  339. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
  340. set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
  341. cifs_set_oplock_level(cifsi, 0);
  342. }
  343. spin_unlock(&cifs_file_list_lock);
  344. oplock_break_cancelled = cancel_work_sync(&cifs_file->oplock_break);
  345. if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
  346. struct TCP_Server_Info *server = tcon->ses->server;
  347. unsigned int xid;
  348. xid = get_xid();
  349. if (server->ops->close)
  350. server->ops->close(xid, tcon, &cifs_file->fid);
  351. _free_xid(xid);
  352. }
  353. if (oplock_break_cancelled)
  354. cifs_done_oplock_break(cifsi);
  355. cifs_del_pending_open(&open);
  356. /*
  357. * Delete any outstanding lock records. We'll lose them when the file
  358. * is closed anyway.
  359. */
  360. down_write(&cifsi->lock_sem);
  361. list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
  362. list_del(&li->llist);
  363. cifs_del_lock_waiters(li);
  364. kfree(li);
  365. }
  366. list_del(&cifs_file->llist->llist);
  367. kfree(cifs_file->llist);
  368. up_write(&cifsi->lock_sem);
  369. cifs_put_tlink(cifs_file->tlink);
  370. dput(cifs_file->dentry);
  371. cifs_sb_deactive(sb);
  372. kfree(cifs_file);
  373. }
  374. int cifs_open(struct inode *inode, struct file *file)
  375. {
  376. int rc = -EACCES;
  377. unsigned int xid;
  378. __u32 oplock;
  379. struct cifs_sb_info *cifs_sb;
  380. struct TCP_Server_Info *server;
  381. struct cifs_tcon *tcon;
  382. struct tcon_link *tlink;
  383. struct cifsFileInfo *cfile = NULL;
  384. char *full_path = NULL;
  385. bool posix_open_ok = false;
  386. struct cifs_fid fid;
  387. struct cifs_pending_open open;
  388. xid = get_xid();
  389. cifs_sb = CIFS_SB(inode->i_sb);
  390. tlink = cifs_sb_tlink(cifs_sb);
  391. if (IS_ERR(tlink)) {
  392. free_xid(xid);
  393. return PTR_ERR(tlink);
  394. }
  395. tcon = tlink_tcon(tlink);
  396. server = tcon->ses->server;
  397. full_path = build_path_from_dentry(file->f_path.dentry);
  398. if (full_path == NULL) {
  399. rc = -ENOMEM;
  400. goto out;
  401. }
  402. cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
  403. inode, file->f_flags, full_path);
  404. if (file->f_flags & O_DIRECT &&
  405. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  406. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  407. file->f_op = &cifs_file_direct_nobrl_ops;
  408. else
  409. file->f_op = &cifs_file_direct_ops;
  410. }
  411. if (server->oplocks)
  412. oplock = REQ_OPLOCK;
  413. else
  414. oplock = 0;
  415. if (!tcon->broken_posix_open && tcon->unix_ext &&
  416. cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  417. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  418. /* can not refresh inode info since size could be stale */
  419. rc = cifs_posix_open(full_path, &inode, inode->i_sb,
  420. cifs_sb->mnt_file_mode /* ignored */,
  421. file->f_flags, &oplock, &fid.netfid, xid);
  422. if (rc == 0) {
  423. cifs_dbg(FYI, "posix open succeeded\n");
  424. posix_open_ok = true;
  425. } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  426. if (tcon->ses->serverNOS)
  427. 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",
  428. tcon->ses->serverName,
  429. tcon->ses->serverNOS);
  430. tcon->broken_posix_open = true;
  431. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  432. (rc != -EOPNOTSUPP)) /* path not found or net err */
  433. goto out;
  434. /*
  435. * Else fallthrough to retry open the old way on network i/o
  436. * or DFS errors.
  437. */
  438. }
  439. if (server->ops->get_lease_key)
  440. server->ops->get_lease_key(inode, &fid);
  441. cifs_add_pending_open(&fid, tlink, &open);
  442. if (!posix_open_ok) {
  443. if (server->ops->get_lease_key)
  444. server->ops->get_lease_key(inode, &fid);
  445. rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
  446. file->f_flags, &oplock, &fid, xid);
  447. if (rc) {
  448. cifs_del_pending_open(&open);
  449. goto out;
  450. }
  451. }
  452. cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
  453. if (cfile == NULL) {
  454. if (server->ops->close)
  455. server->ops->close(xid, tcon, &fid);
  456. cifs_del_pending_open(&open);
  457. rc = -ENOMEM;
  458. goto out;
  459. }
  460. cifs_fscache_set_inode_cookie(inode, file);
  461. if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
  462. /*
  463. * Time to set mode which we can not set earlier due to
  464. * problems creating new read-only files.
  465. */
  466. struct cifs_unix_set_info_args args = {
  467. .mode = inode->i_mode,
  468. .uid = INVALID_UID, /* no change */
  469. .gid = INVALID_GID, /* no change */
  470. .ctime = NO_CHANGE_64,
  471. .atime = NO_CHANGE_64,
  472. .mtime = NO_CHANGE_64,
  473. .device = 0,
  474. };
  475. CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
  476. cfile->pid);
  477. }
  478. out:
  479. kfree(full_path);
  480. free_xid(xid);
  481. cifs_put_tlink(tlink);
  482. return rc;
  483. }
  484. static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
  485. /*
  486. * Try to reacquire byte range locks that were released when session
  487. * to server was lost.
  488. */
  489. static int
  490. cifs_relock_file(struct cifsFileInfo *cfile)
  491. {
  492. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  493. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  494. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  495. int rc = 0;
  496. down_read(&cinode->lock_sem);
  497. if (cinode->can_cache_brlcks) {
  498. /* can cache locks - no need to relock */
  499. up_read(&cinode->lock_sem);
  500. return rc;
  501. }
  502. if (cap_unix(tcon->ses) &&
  503. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  504. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  505. rc = cifs_push_posix_locks(cfile);
  506. else
  507. rc = tcon->ses->server->ops->push_mand_locks(cfile);
  508. up_read(&cinode->lock_sem);
  509. return rc;
  510. }
  511. static int
  512. cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
  513. {
  514. int rc = -EACCES;
  515. unsigned int xid;
  516. __u32 oplock;
  517. struct cifs_sb_info *cifs_sb;
  518. struct cifs_tcon *tcon;
  519. struct TCP_Server_Info *server;
  520. struct cifsInodeInfo *cinode;
  521. struct inode *inode;
  522. char *full_path = NULL;
  523. int desired_access;
  524. int disposition = FILE_OPEN;
  525. int create_options = CREATE_NOT_DIR;
  526. struct cifs_open_parms oparms;
  527. xid = get_xid();
  528. mutex_lock(&cfile->fh_mutex);
  529. if (!cfile->invalidHandle) {
  530. mutex_unlock(&cfile->fh_mutex);
  531. rc = 0;
  532. free_xid(xid);
  533. return rc;
  534. }
  535. inode = d_inode(cfile->dentry);
  536. cifs_sb = CIFS_SB(inode->i_sb);
  537. tcon = tlink_tcon(cfile->tlink);
  538. server = tcon->ses->server;
  539. /*
  540. * Can not grab rename sem here because various ops, including those
  541. * that already have the rename sem can end up causing writepage to get
  542. * called and if the server was down that means we end up here, and we
  543. * can never tell if the caller already has the rename_sem.
  544. */
  545. full_path = build_path_from_dentry(cfile->dentry);
  546. if (full_path == NULL) {
  547. rc = -ENOMEM;
  548. mutex_unlock(&cfile->fh_mutex);
  549. free_xid(xid);
  550. return rc;
  551. }
  552. cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
  553. inode, cfile->f_flags, full_path);
  554. if (tcon->ses->server->oplocks)
  555. oplock = REQ_OPLOCK;
  556. else
  557. oplock = 0;
  558. if (tcon->unix_ext && cap_unix(tcon->ses) &&
  559. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  560. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  561. /*
  562. * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
  563. * original open. Must mask them off for a reopen.
  564. */
  565. unsigned int oflags = cfile->f_flags &
  566. ~(O_CREAT | O_EXCL | O_TRUNC);
  567. rc = cifs_posix_open(full_path, NULL, inode->i_sb,
  568. cifs_sb->mnt_file_mode /* ignored */,
  569. oflags, &oplock, &cfile->fid.netfid, xid);
  570. if (rc == 0) {
  571. cifs_dbg(FYI, "posix reopen succeeded\n");
  572. oparms.reconnect = true;
  573. goto reopen_success;
  574. }
  575. /*
  576. * fallthrough to retry open the old way on errors, especially
  577. * in the reconnect path it is important to retry hard
  578. */
  579. }
  580. desired_access = cifs_convert_flags(cfile->f_flags);
  581. if (backup_cred(cifs_sb))
  582. create_options |= CREATE_OPEN_BACKUP_INTENT;
  583. if (server->ops->get_lease_key)
  584. server->ops->get_lease_key(inode, &cfile->fid);
  585. oparms.tcon = tcon;
  586. oparms.cifs_sb = cifs_sb;
  587. oparms.desired_access = desired_access;
  588. oparms.create_options = create_options;
  589. oparms.disposition = disposition;
  590. oparms.path = full_path;
  591. oparms.fid = &cfile->fid;
  592. oparms.reconnect = true;
  593. /*
  594. * Can not refresh inode by passing in file_info buf to be returned by
  595. * ops->open and then calling get_inode_info with returned buf since
  596. * file might have write behind data that needs to be flushed and server
  597. * version of file size can be stale. If we knew for sure that inode was
  598. * not dirty locally we could do this.
  599. */
  600. rc = server->ops->open(xid, &oparms, &oplock, NULL);
  601. if (rc == -ENOENT && oparms.reconnect == false) {
  602. /* durable handle timeout is expired - open the file again */
  603. rc = server->ops->open(xid, &oparms, &oplock, NULL);
  604. /* indicate that we need to relock the file */
  605. oparms.reconnect = true;
  606. }
  607. if (rc) {
  608. mutex_unlock(&cfile->fh_mutex);
  609. cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
  610. cifs_dbg(FYI, "oplock: %d\n", oplock);
  611. goto reopen_error_exit;
  612. }
  613. reopen_success:
  614. cfile->invalidHandle = false;
  615. mutex_unlock(&cfile->fh_mutex);
  616. cinode = CIFS_I(inode);
  617. if (can_flush) {
  618. rc = filemap_write_and_wait(inode->i_mapping);
  619. mapping_set_error(inode->i_mapping, rc);
  620. if (tcon->unix_ext)
  621. rc = cifs_get_inode_info_unix(&inode, full_path,
  622. inode->i_sb, xid);
  623. else
  624. rc = cifs_get_inode_info(&inode, full_path, NULL,
  625. inode->i_sb, xid, NULL);
  626. }
  627. /*
  628. * Else we are writing out data to server already and could deadlock if
  629. * we tried to flush data, and since we do not know if we have data that
  630. * would invalidate the current end of file on the server we can not go
  631. * to the server to get the new inode info.
  632. */
  633. server->ops->set_fid(cfile, &cfile->fid, oplock);
  634. if (oparms.reconnect)
  635. cifs_relock_file(cfile);
  636. reopen_error_exit:
  637. kfree(full_path);
  638. free_xid(xid);
  639. return rc;
  640. }
  641. int cifs_close(struct inode *inode, struct file *file)
  642. {
  643. if (file->private_data != NULL) {
  644. cifsFileInfo_put(file->private_data);
  645. file->private_data = NULL;
  646. }
  647. /* return code from the ->release op is always ignored */
  648. return 0;
  649. }
  650. int cifs_closedir(struct inode *inode, struct file *file)
  651. {
  652. int rc = 0;
  653. unsigned int xid;
  654. struct cifsFileInfo *cfile = file->private_data;
  655. struct cifs_tcon *tcon;
  656. struct TCP_Server_Info *server;
  657. char *buf;
  658. cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
  659. if (cfile == NULL)
  660. return rc;
  661. xid = get_xid();
  662. tcon = tlink_tcon(cfile->tlink);
  663. server = tcon->ses->server;
  664. cifs_dbg(FYI, "Freeing private data in close dir\n");
  665. spin_lock(&cifs_file_list_lock);
  666. if (server->ops->dir_needs_close(cfile)) {
  667. cfile->invalidHandle = true;
  668. spin_unlock(&cifs_file_list_lock);
  669. if (server->ops->close_dir)
  670. rc = server->ops->close_dir(xid, tcon, &cfile->fid);
  671. else
  672. rc = -ENOSYS;
  673. cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
  674. /* not much we can do if it fails anyway, ignore rc */
  675. rc = 0;
  676. } else
  677. spin_unlock(&cifs_file_list_lock);
  678. buf = cfile->srch_inf.ntwrk_buf_start;
  679. if (buf) {
  680. cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
  681. cfile->srch_inf.ntwrk_buf_start = NULL;
  682. if (cfile->srch_inf.smallBuf)
  683. cifs_small_buf_release(buf);
  684. else
  685. cifs_buf_release(buf);
  686. }
  687. cifs_put_tlink(cfile->tlink);
  688. kfree(file->private_data);
  689. file->private_data = NULL;
  690. /* BB can we lock the filestruct while this is going on? */
  691. free_xid(xid);
  692. return rc;
  693. }
  694. static struct cifsLockInfo *
  695. cifs_lock_init(__u64 offset, __u64 length, __u8 type)
  696. {
  697. struct cifsLockInfo *lock =
  698. kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
  699. if (!lock)
  700. return lock;
  701. lock->offset = offset;
  702. lock->length = length;
  703. lock->type = type;
  704. lock->pid = current->tgid;
  705. INIT_LIST_HEAD(&lock->blist);
  706. init_waitqueue_head(&lock->block_q);
  707. return lock;
  708. }
  709. void
  710. cifs_del_lock_waiters(struct cifsLockInfo *lock)
  711. {
  712. struct cifsLockInfo *li, *tmp;
  713. list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
  714. list_del_init(&li->blist);
  715. wake_up(&li->block_q);
  716. }
  717. }
  718. #define CIFS_LOCK_OP 0
  719. #define CIFS_READ_OP 1
  720. #define CIFS_WRITE_OP 2
  721. /* @rw_check : 0 - no op, 1 - read, 2 - write */
  722. static bool
  723. cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
  724. __u64 length, __u8 type, struct cifsFileInfo *cfile,
  725. struct cifsLockInfo **conf_lock, int rw_check)
  726. {
  727. struct cifsLockInfo *li;
  728. struct cifsFileInfo *cur_cfile = fdlocks->cfile;
  729. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  730. list_for_each_entry(li, &fdlocks->locks, llist) {
  731. if (offset + length <= li->offset ||
  732. offset >= li->offset + li->length)
  733. continue;
  734. if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
  735. server->ops->compare_fids(cfile, cur_cfile)) {
  736. /* shared lock prevents write op through the same fid */
  737. if (!(li->type & server->vals->shared_lock_type) ||
  738. rw_check != CIFS_WRITE_OP)
  739. continue;
  740. }
  741. if ((type & server->vals->shared_lock_type) &&
  742. ((server->ops->compare_fids(cfile, cur_cfile) &&
  743. current->tgid == li->pid) || type == li->type))
  744. continue;
  745. if (conf_lock)
  746. *conf_lock = li;
  747. return true;
  748. }
  749. return false;
  750. }
  751. bool
  752. cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
  753. __u8 type, struct cifsLockInfo **conf_lock,
  754. int rw_check)
  755. {
  756. bool rc = false;
  757. struct cifs_fid_locks *cur;
  758. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  759. list_for_each_entry(cur, &cinode->llist, llist) {
  760. rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
  761. cfile, conf_lock, rw_check);
  762. if (rc)
  763. break;
  764. }
  765. return rc;
  766. }
  767. /*
  768. * Check if there is another lock that prevents us to set the lock (mandatory
  769. * style). If such a lock exists, update the flock structure with its
  770. * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
  771. * or leave it the same if we can't. Returns 0 if we don't need to request to
  772. * the server or 1 otherwise.
  773. */
  774. static int
  775. cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
  776. __u8 type, struct file_lock *flock)
  777. {
  778. int rc = 0;
  779. struct cifsLockInfo *conf_lock;
  780. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  781. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  782. bool exist;
  783. down_read(&cinode->lock_sem);
  784. exist = cifs_find_lock_conflict(cfile, offset, length, type,
  785. &conf_lock, CIFS_LOCK_OP);
  786. if (exist) {
  787. flock->fl_start = conf_lock->offset;
  788. flock->fl_end = conf_lock->offset + conf_lock->length - 1;
  789. flock->fl_pid = conf_lock->pid;
  790. if (conf_lock->type & server->vals->shared_lock_type)
  791. flock->fl_type = F_RDLCK;
  792. else
  793. flock->fl_type = F_WRLCK;
  794. } else if (!cinode->can_cache_brlcks)
  795. rc = 1;
  796. else
  797. flock->fl_type = F_UNLCK;
  798. up_read(&cinode->lock_sem);
  799. return rc;
  800. }
  801. static void
  802. cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
  803. {
  804. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  805. down_write(&cinode->lock_sem);
  806. list_add_tail(&lock->llist, &cfile->llist->locks);
  807. up_write(&cinode->lock_sem);
  808. }
  809. /*
  810. * Set the byte-range lock (mandatory style). Returns:
  811. * 1) 0, if we set the lock and don't need to request to the server;
  812. * 2) 1, if no locks prevent us but we need to request to the server;
  813. * 3) -EACCESS, if there is a lock that prevents us and wait is false.
  814. */
  815. static int
  816. cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
  817. bool wait)
  818. {
  819. struct cifsLockInfo *conf_lock;
  820. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  821. bool exist;
  822. int rc = 0;
  823. try_again:
  824. exist = false;
  825. down_write(&cinode->lock_sem);
  826. exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
  827. lock->type, &conf_lock, CIFS_LOCK_OP);
  828. if (!exist && cinode->can_cache_brlcks) {
  829. list_add_tail(&lock->llist, &cfile->llist->locks);
  830. up_write(&cinode->lock_sem);
  831. return rc;
  832. }
  833. if (!exist)
  834. rc = 1;
  835. else if (!wait)
  836. rc = -EACCES;
  837. else {
  838. list_add_tail(&lock->blist, &conf_lock->blist);
  839. up_write(&cinode->lock_sem);
  840. rc = wait_event_interruptible(lock->block_q,
  841. (lock->blist.prev == &lock->blist) &&
  842. (lock->blist.next == &lock->blist));
  843. if (!rc)
  844. goto try_again;
  845. down_write(&cinode->lock_sem);
  846. list_del_init(&lock->blist);
  847. }
  848. up_write(&cinode->lock_sem);
  849. return rc;
  850. }
  851. /*
  852. * Check if there is another lock that prevents us to set the lock (posix
  853. * style). If such a lock exists, update the flock structure with its
  854. * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
  855. * or leave it the same if we can't. Returns 0 if we don't need to request to
  856. * the server or 1 otherwise.
  857. */
  858. static int
  859. cifs_posix_lock_test(struct file *file, struct file_lock *flock)
  860. {
  861. int rc = 0;
  862. struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
  863. unsigned char saved_type = flock->fl_type;
  864. if ((flock->fl_flags & FL_POSIX) == 0)
  865. return 1;
  866. down_read(&cinode->lock_sem);
  867. posix_test_lock(file, flock);
  868. if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
  869. flock->fl_type = saved_type;
  870. rc = 1;
  871. }
  872. up_read(&cinode->lock_sem);
  873. return rc;
  874. }
  875. /*
  876. * Set the byte-range lock (posix style). Returns:
  877. * 1) 0, if we set the lock and don't need to request to the server;
  878. * 2) 1, if we need to request to the server;
  879. * 3) <0, if the error occurs while setting the lock.
  880. */
  881. static int
  882. cifs_posix_lock_set(struct file *file, struct file_lock *flock)
  883. {
  884. struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
  885. int rc = 1;
  886. if ((flock->fl_flags & FL_POSIX) == 0)
  887. return rc;
  888. try_again:
  889. down_write(&cinode->lock_sem);
  890. if (!cinode->can_cache_brlcks) {
  891. up_write(&cinode->lock_sem);
  892. return rc;
  893. }
  894. rc = posix_lock_file(file, flock, NULL);
  895. up_write(&cinode->lock_sem);
  896. if (rc == FILE_LOCK_DEFERRED) {
  897. rc = wait_event_interruptible(flock->fl_wait, !flock->fl_next);
  898. if (!rc)
  899. goto try_again;
  900. posix_unblock_lock(flock);
  901. }
  902. return rc;
  903. }
  904. int
  905. cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
  906. {
  907. unsigned int xid;
  908. int rc = 0, stored_rc;
  909. struct cifsLockInfo *li, *tmp;
  910. struct cifs_tcon *tcon;
  911. unsigned int num, max_num, max_buf;
  912. LOCKING_ANDX_RANGE *buf, *cur;
  913. int types[] = {LOCKING_ANDX_LARGE_FILES,
  914. LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES};
  915. int i;
  916. xid = get_xid();
  917. tcon = tlink_tcon(cfile->tlink);
  918. /*
  919. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  920. * and check it for zero before using.
  921. */
  922. max_buf = tcon->ses->server->maxBuf;
  923. if (!max_buf) {
  924. free_xid(xid);
  925. return -EINVAL;
  926. }
  927. max_num = (max_buf - sizeof(struct smb_hdr)) /
  928. sizeof(LOCKING_ANDX_RANGE);
  929. buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
  930. if (!buf) {
  931. free_xid(xid);
  932. return -ENOMEM;
  933. }
  934. for (i = 0; i < 2; i++) {
  935. cur = buf;
  936. num = 0;
  937. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  938. if (li->type != types[i])
  939. continue;
  940. cur->Pid = cpu_to_le16(li->pid);
  941. cur->LengthLow = cpu_to_le32((u32)li->length);
  942. cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
  943. cur->OffsetLow = cpu_to_le32((u32)li->offset);
  944. cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
  945. if (++num == max_num) {
  946. stored_rc = cifs_lockv(xid, tcon,
  947. cfile->fid.netfid,
  948. (__u8)li->type, 0, num,
  949. buf);
  950. if (stored_rc)
  951. rc = stored_rc;
  952. cur = buf;
  953. num = 0;
  954. } else
  955. cur++;
  956. }
  957. if (num) {
  958. stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
  959. (__u8)types[i], 0, num, buf);
  960. if (stored_rc)
  961. rc = stored_rc;
  962. }
  963. }
  964. kfree(buf);
  965. free_xid(xid);
  966. return rc;
  967. }
  968. struct lock_to_push {
  969. struct list_head llist;
  970. __u64 offset;
  971. __u64 length;
  972. __u32 pid;
  973. __u16 netfid;
  974. __u8 type;
  975. };
  976. static int
  977. cifs_push_posix_locks(struct cifsFileInfo *cfile)
  978. {
  979. struct inode *inode = d_inode(cfile->dentry);
  980. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  981. struct file_lock *flock;
  982. struct file_lock_context *flctx = inode->i_flctx;
  983. unsigned int count = 0, i;
  984. int rc = 0, xid, type;
  985. struct list_head locks_to_send, *el;
  986. struct lock_to_push *lck, *tmp;
  987. __u64 length;
  988. xid = get_xid();
  989. if (!flctx)
  990. goto out;
  991. spin_lock(&flctx->flc_lock);
  992. list_for_each(el, &flctx->flc_posix) {
  993. count++;
  994. }
  995. spin_unlock(&flctx->flc_lock);
  996. INIT_LIST_HEAD(&locks_to_send);
  997. /*
  998. * Allocating count locks is enough because no FL_POSIX locks can be
  999. * added to the list while we are holding cinode->lock_sem that
  1000. * protects locking operations of this inode.
  1001. */
  1002. for (i = 0; i < count; i++) {
  1003. lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
  1004. if (!lck) {
  1005. rc = -ENOMEM;
  1006. goto err_out;
  1007. }
  1008. list_add_tail(&lck->llist, &locks_to_send);
  1009. }
  1010. el = locks_to_send.next;
  1011. spin_lock(&flctx->flc_lock);
  1012. list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
  1013. if (el == &locks_to_send) {
  1014. /*
  1015. * The list ended. We don't have enough allocated
  1016. * structures - something is really wrong.
  1017. */
  1018. cifs_dbg(VFS, "Can't push all brlocks!\n");
  1019. break;
  1020. }
  1021. length = 1 + flock->fl_end - flock->fl_start;
  1022. if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
  1023. type = CIFS_RDLCK;
  1024. else
  1025. type = CIFS_WRLCK;
  1026. lck = list_entry(el, struct lock_to_push, llist);
  1027. lck->pid = flock->fl_pid;
  1028. lck->netfid = cfile->fid.netfid;
  1029. lck->length = length;
  1030. lck->type = type;
  1031. lck->offset = flock->fl_start;
  1032. }
  1033. spin_unlock(&flctx->flc_lock);
  1034. list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
  1035. int stored_rc;
  1036. stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
  1037. lck->offset, lck->length, NULL,
  1038. lck->type, 0);
  1039. if (stored_rc)
  1040. rc = stored_rc;
  1041. list_del(&lck->llist);
  1042. kfree(lck);
  1043. }
  1044. out:
  1045. free_xid(xid);
  1046. return rc;
  1047. err_out:
  1048. list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
  1049. list_del(&lck->llist);
  1050. kfree(lck);
  1051. }
  1052. goto out;
  1053. }
  1054. static int
  1055. cifs_push_locks(struct cifsFileInfo *cfile)
  1056. {
  1057. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  1058. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1059. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1060. int rc = 0;
  1061. /* we are going to update can_cache_brlcks here - need a write access */
  1062. down_write(&cinode->lock_sem);
  1063. if (!cinode->can_cache_brlcks) {
  1064. up_write(&cinode->lock_sem);
  1065. return rc;
  1066. }
  1067. if (cap_unix(tcon->ses) &&
  1068. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  1069. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  1070. rc = cifs_push_posix_locks(cfile);
  1071. else
  1072. rc = tcon->ses->server->ops->push_mand_locks(cfile);
  1073. cinode->can_cache_brlcks = false;
  1074. up_write(&cinode->lock_sem);
  1075. return rc;
  1076. }
  1077. static void
  1078. cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
  1079. bool *wait_flag, struct TCP_Server_Info *server)
  1080. {
  1081. if (flock->fl_flags & FL_POSIX)
  1082. cifs_dbg(FYI, "Posix\n");
  1083. if (flock->fl_flags & FL_FLOCK)
  1084. cifs_dbg(FYI, "Flock\n");
  1085. if (flock->fl_flags & FL_SLEEP) {
  1086. cifs_dbg(FYI, "Blocking lock\n");
  1087. *wait_flag = true;
  1088. }
  1089. if (flock->fl_flags & FL_ACCESS)
  1090. cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
  1091. if (flock->fl_flags & FL_LEASE)
  1092. cifs_dbg(FYI, "Lease on file - not implemented yet\n");
  1093. if (flock->fl_flags &
  1094. (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
  1095. FL_ACCESS | FL_LEASE | FL_CLOSE)))
  1096. cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
  1097. *type = server->vals->large_lock_type;
  1098. if (flock->fl_type == F_WRLCK) {
  1099. cifs_dbg(FYI, "F_WRLCK\n");
  1100. *type |= server->vals->exclusive_lock_type;
  1101. *lock = 1;
  1102. } else if (flock->fl_type == F_UNLCK) {
  1103. cifs_dbg(FYI, "F_UNLCK\n");
  1104. *type |= server->vals->unlock_lock_type;
  1105. *unlock = 1;
  1106. /* Check if unlock includes more than one lock range */
  1107. } else if (flock->fl_type == F_RDLCK) {
  1108. cifs_dbg(FYI, "F_RDLCK\n");
  1109. *type |= server->vals->shared_lock_type;
  1110. *lock = 1;
  1111. } else if (flock->fl_type == F_EXLCK) {
  1112. cifs_dbg(FYI, "F_EXLCK\n");
  1113. *type |= server->vals->exclusive_lock_type;
  1114. *lock = 1;
  1115. } else if (flock->fl_type == F_SHLCK) {
  1116. cifs_dbg(FYI, "F_SHLCK\n");
  1117. *type |= server->vals->shared_lock_type;
  1118. *lock = 1;
  1119. } else
  1120. cifs_dbg(FYI, "Unknown type of lock\n");
  1121. }
  1122. static int
  1123. cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
  1124. bool wait_flag, bool posix_lck, unsigned int xid)
  1125. {
  1126. int rc = 0;
  1127. __u64 length = 1 + flock->fl_end - flock->fl_start;
  1128. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  1129. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1130. struct TCP_Server_Info *server = tcon->ses->server;
  1131. __u16 netfid = cfile->fid.netfid;
  1132. if (posix_lck) {
  1133. int posix_lock_type;
  1134. rc = cifs_posix_lock_test(file, flock);
  1135. if (!rc)
  1136. return rc;
  1137. if (type & server->vals->shared_lock_type)
  1138. posix_lock_type = CIFS_RDLCK;
  1139. else
  1140. posix_lock_type = CIFS_WRLCK;
  1141. rc = CIFSSMBPosixLock(xid, tcon, netfid, current->tgid,
  1142. flock->fl_start, length, flock,
  1143. posix_lock_type, wait_flag);
  1144. return rc;
  1145. }
  1146. rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
  1147. if (!rc)
  1148. return rc;
  1149. /* BB we could chain these into one lock request BB */
  1150. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
  1151. 1, 0, false);
  1152. if (rc == 0) {
  1153. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1154. type, 0, 1, false);
  1155. flock->fl_type = F_UNLCK;
  1156. if (rc != 0)
  1157. cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
  1158. rc);
  1159. return 0;
  1160. }
  1161. if (type & server->vals->shared_lock_type) {
  1162. flock->fl_type = F_WRLCK;
  1163. return 0;
  1164. }
  1165. type &= ~server->vals->exclusive_lock_type;
  1166. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1167. type | server->vals->shared_lock_type,
  1168. 1, 0, false);
  1169. if (rc == 0) {
  1170. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1171. type | server->vals->shared_lock_type, 0, 1, false);
  1172. flock->fl_type = F_RDLCK;
  1173. if (rc != 0)
  1174. cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
  1175. rc);
  1176. } else
  1177. flock->fl_type = F_WRLCK;
  1178. return 0;
  1179. }
  1180. void
  1181. cifs_move_llist(struct list_head *source, struct list_head *dest)
  1182. {
  1183. struct list_head *li, *tmp;
  1184. list_for_each_safe(li, tmp, source)
  1185. list_move(li, dest);
  1186. }
  1187. void
  1188. cifs_free_llist(struct list_head *llist)
  1189. {
  1190. struct cifsLockInfo *li, *tmp;
  1191. list_for_each_entry_safe(li, tmp, llist, llist) {
  1192. cifs_del_lock_waiters(li);
  1193. list_del(&li->llist);
  1194. kfree(li);
  1195. }
  1196. }
  1197. int
  1198. cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
  1199. unsigned int xid)
  1200. {
  1201. int rc = 0, stored_rc;
  1202. int types[] = {LOCKING_ANDX_LARGE_FILES,
  1203. LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES};
  1204. unsigned int i;
  1205. unsigned int max_num, num, max_buf;
  1206. LOCKING_ANDX_RANGE *buf, *cur;
  1207. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1208. struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
  1209. struct cifsLockInfo *li, *tmp;
  1210. __u64 length = 1 + flock->fl_end - flock->fl_start;
  1211. struct list_head tmp_llist;
  1212. INIT_LIST_HEAD(&tmp_llist);
  1213. /*
  1214. * Accessing maxBuf is racy with cifs_reconnect - need to store value
  1215. * and check it for zero before using.
  1216. */
  1217. max_buf = tcon->ses->server->maxBuf;
  1218. if (!max_buf)
  1219. return -EINVAL;
  1220. max_num = (max_buf - sizeof(struct smb_hdr)) /
  1221. sizeof(LOCKING_ANDX_RANGE);
  1222. buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
  1223. if (!buf)
  1224. return -ENOMEM;
  1225. down_write(&cinode->lock_sem);
  1226. for (i = 0; i < 2; i++) {
  1227. cur = buf;
  1228. num = 0;
  1229. list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
  1230. if (flock->fl_start > li->offset ||
  1231. (flock->fl_start + length) <
  1232. (li->offset + li->length))
  1233. continue;
  1234. if (current->tgid != li->pid)
  1235. continue;
  1236. if (types[i] != li->type)
  1237. continue;
  1238. if (cinode->can_cache_brlcks) {
  1239. /*
  1240. * We can cache brlock requests - simply remove
  1241. * a lock from the file's list.
  1242. */
  1243. list_del(&li->llist);
  1244. cifs_del_lock_waiters(li);
  1245. kfree(li);
  1246. continue;
  1247. }
  1248. cur->Pid = cpu_to_le16(li->pid);
  1249. cur->LengthLow = cpu_to_le32((u32)li->length);
  1250. cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
  1251. cur->OffsetLow = cpu_to_le32((u32)li->offset);
  1252. cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
  1253. /*
  1254. * We need to save a lock here to let us add it again to
  1255. * the file's list if the unlock range request fails on
  1256. * the server.
  1257. */
  1258. list_move(&li->llist, &tmp_llist);
  1259. if (++num == max_num) {
  1260. stored_rc = cifs_lockv(xid, tcon,
  1261. cfile->fid.netfid,
  1262. li->type, num, 0, buf);
  1263. if (stored_rc) {
  1264. /*
  1265. * We failed on the unlock range
  1266. * request - add all locks from the tmp
  1267. * list to the head of the file's list.
  1268. */
  1269. cifs_move_llist(&tmp_llist,
  1270. &cfile->llist->locks);
  1271. rc = stored_rc;
  1272. } else
  1273. /*
  1274. * The unlock range request succeed -
  1275. * free the tmp list.
  1276. */
  1277. cifs_free_llist(&tmp_llist);
  1278. cur = buf;
  1279. num = 0;
  1280. } else
  1281. cur++;
  1282. }
  1283. if (num) {
  1284. stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
  1285. types[i], num, 0, buf);
  1286. if (stored_rc) {
  1287. cifs_move_llist(&tmp_llist,
  1288. &cfile->llist->locks);
  1289. rc = stored_rc;
  1290. } else
  1291. cifs_free_llist(&tmp_llist);
  1292. }
  1293. }
  1294. up_write(&cinode->lock_sem);
  1295. kfree(buf);
  1296. return rc;
  1297. }
  1298. static int
  1299. cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
  1300. bool wait_flag, bool posix_lck, int lock, int unlock,
  1301. unsigned int xid)
  1302. {
  1303. int rc = 0;
  1304. __u64 length = 1 + flock->fl_end - flock->fl_start;
  1305. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  1306. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  1307. struct TCP_Server_Info *server = tcon->ses->server;
  1308. struct inode *inode = d_inode(cfile->dentry);
  1309. if (posix_lck) {
  1310. int posix_lock_type;
  1311. rc = cifs_posix_lock_set(file, flock);
  1312. if (!rc || rc < 0)
  1313. return rc;
  1314. if (type & server->vals->shared_lock_type)
  1315. posix_lock_type = CIFS_RDLCK;
  1316. else
  1317. posix_lock_type = CIFS_WRLCK;
  1318. if (unlock == 1)
  1319. posix_lock_type = CIFS_UNLCK;
  1320. rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
  1321. current->tgid, flock->fl_start, length,
  1322. NULL, posix_lock_type, wait_flag);
  1323. goto out;
  1324. }
  1325. if (lock) {
  1326. struct cifsLockInfo *lock;
  1327. lock = cifs_lock_init(flock->fl_start, length, type);
  1328. if (!lock)
  1329. return -ENOMEM;
  1330. rc = cifs_lock_add_if(cfile, lock, wait_flag);
  1331. if (rc < 0) {
  1332. kfree(lock);
  1333. return rc;
  1334. }
  1335. if (!rc)
  1336. goto out;
  1337. /*
  1338. * Windows 7 server can delay breaking lease from read to None
  1339. * if we set a byte-range lock on a file - break it explicitly
  1340. * before sending the lock to the server to be sure the next
  1341. * read won't conflict with non-overlapted locks due to
  1342. * pagereading.
  1343. */
  1344. if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
  1345. CIFS_CACHE_READ(CIFS_I(inode))) {
  1346. cifs_zap_mapping(inode);
  1347. cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
  1348. inode);
  1349. CIFS_I(inode)->oplock = 0;
  1350. }
  1351. rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
  1352. type, 1, 0, wait_flag);
  1353. if (rc) {
  1354. kfree(lock);
  1355. return rc;
  1356. }
  1357. cifs_lock_add(cfile, lock);
  1358. } else if (unlock)
  1359. rc = server->ops->mand_unlock_range(cfile, flock, xid);
  1360. out:
  1361. if (flock->fl_flags & FL_POSIX && !rc)
  1362. rc = posix_lock_file_wait(file, flock);
  1363. return rc;
  1364. }
  1365. int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
  1366. {
  1367. int rc, xid;
  1368. int lock = 0, unlock = 0;
  1369. bool wait_flag = false;
  1370. bool posix_lck = false;
  1371. struct cifs_sb_info *cifs_sb;
  1372. struct cifs_tcon *tcon;
  1373. struct cifsInodeInfo *cinode;
  1374. struct cifsFileInfo *cfile;
  1375. __u16 netfid;
  1376. __u32 type;
  1377. rc = -EACCES;
  1378. xid = get_xid();
  1379. cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
  1380. cmd, flock->fl_flags, flock->fl_type,
  1381. flock->fl_start, flock->fl_end);
  1382. cfile = (struct cifsFileInfo *)file->private_data;
  1383. tcon = tlink_tcon(cfile->tlink);
  1384. cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
  1385. tcon->ses->server);
  1386. cifs_sb = CIFS_FILE_SB(file);
  1387. netfid = cfile->fid.netfid;
  1388. cinode = CIFS_I(file_inode(file));
  1389. if (cap_unix(tcon->ses) &&
  1390. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  1391. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  1392. posix_lck = true;
  1393. /*
  1394. * BB add code here to normalize offset and length to account for
  1395. * negative length which we can not accept over the wire.
  1396. */
  1397. if (IS_GETLK(cmd)) {
  1398. rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
  1399. free_xid(xid);
  1400. return rc;
  1401. }
  1402. if (!lock && !unlock) {
  1403. /*
  1404. * if no lock or unlock then nothing to do since we do not
  1405. * know what it is
  1406. */
  1407. free_xid(xid);
  1408. return -EOPNOTSUPP;
  1409. }
  1410. rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
  1411. xid);
  1412. free_xid(xid);
  1413. return rc;
  1414. }
  1415. /*
  1416. * update the file size (if needed) after a write. Should be called with
  1417. * the inode->i_lock held
  1418. */
  1419. void
  1420. cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
  1421. unsigned int bytes_written)
  1422. {
  1423. loff_t end_of_write = offset + bytes_written;
  1424. if (end_of_write > cifsi->server_eof)
  1425. cifsi->server_eof = end_of_write;
  1426. }
  1427. static ssize_t
  1428. cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
  1429. size_t write_size, loff_t *offset)
  1430. {
  1431. int rc = 0;
  1432. unsigned int bytes_written = 0;
  1433. unsigned int total_written;
  1434. struct cifs_sb_info *cifs_sb;
  1435. struct cifs_tcon *tcon;
  1436. struct TCP_Server_Info *server;
  1437. unsigned int xid;
  1438. struct dentry *dentry = open_file->dentry;
  1439. struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
  1440. struct cifs_io_parms io_parms;
  1441. cifs_sb = CIFS_SB(dentry->d_sb);
  1442. cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
  1443. write_size, *offset, dentry);
  1444. tcon = tlink_tcon(open_file->tlink);
  1445. server = tcon->ses->server;
  1446. if (!server->ops->sync_write)
  1447. return -ENOSYS;
  1448. xid = get_xid();
  1449. for (total_written = 0; write_size > total_written;
  1450. total_written += bytes_written) {
  1451. rc = -EAGAIN;
  1452. while (rc == -EAGAIN) {
  1453. struct kvec iov[2];
  1454. unsigned int len;
  1455. if (open_file->invalidHandle) {
  1456. /* we could deadlock if we called
  1457. filemap_fdatawait from here so tell
  1458. reopen_file not to flush data to
  1459. server now */
  1460. rc = cifs_reopen_file(open_file, false);
  1461. if (rc != 0)
  1462. break;
  1463. }
  1464. len = min(server->ops->wp_retry_size(d_inode(dentry)),
  1465. (unsigned int)write_size - total_written);
  1466. /* iov[0] is reserved for smb header */
  1467. iov[1].iov_base = (char *)write_data + total_written;
  1468. iov[1].iov_len = len;
  1469. io_parms.pid = pid;
  1470. io_parms.tcon = tcon;
  1471. io_parms.offset = *offset;
  1472. io_parms.length = len;
  1473. rc = server->ops->sync_write(xid, &open_file->fid,
  1474. &io_parms, &bytes_written, iov, 1);
  1475. }
  1476. if (rc || (bytes_written == 0)) {
  1477. if (total_written)
  1478. break;
  1479. else {
  1480. free_xid(xid);
  1481. return rc;
  1482. }
  1483. } else {
  1484. spin_lock(&d_inode(dentry)->i_lock);
  1485. cifs_update_eof(cifsi, *offset, bytes_written);
  1486. spin_unlock(&d_inode(dentry)->i_lock);
  1487. *offset += bytes_written;
  1488. }
  1489. }
  1490. cifs_stats_bytes_written(tcon, total_written);
  1491. if (total_written > 0) {
  1492. spin_lock(&d_inode(dentry)->i_lock);
  1493. if (*offset > d_inode(dentry)->i_size)
  1494. i_size_write(d_inode(dentry), *offset);
  1495. spin_unlock(&d_inode(dentry)->i_lock);
  1496. }
  1497. mark_inode_dirty_sync(d_inode(dentry));
  1498. free_xid(xid);
  1499. return total_written;
  1500. }
  1501. struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
  1502. bool fsuid_only)
  1503. {
  1504. struct cifsFileInfo *open_file = NULL;
  1505. struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
  1506. /* only filter by fsuid on multiuser mounts */
  1507. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  1508. fsuid_only = false;
  1509. spin_lock(&cifs_file_list_lock);
  1510. /* we could simply get the first_list_entry since write-only entries
  1511. are always at the end of the list but since the first entry might
  1512. have a close pending, we go through the whole list */
  1513. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1514. if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
  1515. continue;
  1516. if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
  1517. if (!open_file->invalidHandle) {
  1518. /* found a good file */
  1519. /* lock it so it will not be closed on us */
  1520. cifsFileInfo_get_locked(open_file);
  1521. spin_unlock(&cifs_file_list_lock);
  1522. return open_file;
  1523. } /* else might as well continue, and look for
  1524. another, or simply have the caller reopen it
  1525. again rather than trying to fix this handle */
  1526. } else /* write only file */
  1527. break; /* write only files are last so must be done */
  1528. }
  1529. spin_unlock(&cifs_file_list_lock);
  1530. return NULL;
  1531. }
  1532. struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode,
  1533. bool fsuid_only)
  1534. {
  1535. struct cifsFileInfo *open_file, *inv_file = NULL;
  1536. struct cifs_sb_info *cifs_sb;
  1537. bool any_available = false;
  1538. int rc;
  1539. unsigned int refind = 0;
  1540. /* Having a null inode here (because mapping->host was set to zero by
  1541. the VFS or MM) should not happen but we had reports of on oops (due to
  1542. it being zero) during stress testcases so we need to check for it */
  1543. if (cifs_inode == NULL) {
  1544. cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
  1545. dump_stack();
  1546. return NULL;
  1547. }
  1548. cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
  1549. /* only filter by fsuid on multiuser mounts */
  1550. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  1551. fsuid_only = false;
  1552. spin_lock(&cifs_file_list_lock);
  1553. refind_writable:
  1554. if (refind > MAX_REOPEN_ATT) {
  1555. spin_unlock(&cifs_file_list_lock);
  1556. return NULL;
  1557. }
  1558. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1559. if (!any_available && open_file->pid != current->tgid)
  1560. continue;
  1561. if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
  1562. continue;
  1563. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  1564. if (!open_file->invalidHandle) {
  1565. /* found a good writable file */
  1566. cifsFileInfo_get_locked(open_file);
  1567. spin_unlock(&cifs_file_list_lock);
  1568. return open_file;
  1569. } else {
  1570. if (!inv_file)
  1571. inv_file = open_file;
  1572. }
  1573. }
  1574. }
  1575. /* couldn't find useable FH with same pid, try any available */
  1576. if (!any_available) {
  1577. any_available = true;
  1578. goto refind_writable;
  1579. }
  1580. if (inv_file) {
  1581. any_available = false;
  1582. cifsFileInfo_get_locked(inv_file);
  1583. }
  1584. spin_unlock(&cifs_file_list_lock);
  1585. if (inv_file) {
  1586. rc = cifs_reopen_file(inv_file, false);
  1587. if (!rc)
  1588. return inv_file;
  1589. else {
  1590. spin_lock(&cifs_file_list_lock);
  1591. list_move_tail(&inv_file->flist,
  1592. &cifs_inode->openFileList);
  1593. spin_unlock(&cifs_file_list_lock);
  1594. cifsFileInfo_put(inv_file);
  1595. spin_lock(&cifs_file_list_lock);
  1596. ++refind;
  1597. inv_file = NULL;
  1598. goto refind_writable;
  1599. }
  1600. }
  1601. return NULL;
  1602. }
  1603. static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
  1604. {
  1605. struct address_space *mapping = page->mapping;
  1606. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1607. char *write_data;
  1608. int rc = -EFAULT;
  1609. int bytes_written = 0;
  1610. struct inode *inode;
  1611. struct cifsFileInfo *open_file;
  1612. if (!mapping || !mapping->host)
  1613. return -EFAULT;
  1614. inode = page->mapping->host;
  1615. offset += (loff_t)from;
  1616. write_data = kmap(page);
  1617. write_data += from;
  1618. if ((to > PAGE_CACHE_SIZE) || (from > to)) {
  1619. kunmap(page);
  1620. return -EIO;
  1621. }
  1622. /* racing with truncate? */
  1623. if (offset > mapping->host->i_size) {
  1624. kunmap(page);
  1625. return 0; /* don't care */
  1626. }
  1627. /* check to make sure that we are not extending the file */
  1628. if (mapping->host->i_size - offset < (loff_t)to)
  1629. to = (unsigned)(mapping->host->i_size - offset);
  1630. open_file = find_writable_file(CIFS_I(mapping->host), false);
  1631. if (open_file) {
  1632. bytes_written = cifs_write(open_file, open_file->pid,
  1633. write_data, to - from, &offset);
  1634. cifsFileInfo_put(open_file);
  1635. /* Does mm or vfs already set times? */
  1636. inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
  1637. if ((bytes_written > 0) && (offset))
  1638. rc = 0;
  1639. else if (bytes_written < 0)
  1640. rc = bytes_written;
  1641. } else {
  1642. cifs_dbg(FYI, "No writeable filehandles for inode\n");
  1643. rc = -EIO;
  1644. }
  1645. kunmap(page);
  1646. return rc;
  1647. }
  1648. static struct cifs_writedata *
  1649. wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
  1650. pgoff_t end, pgoff_t *index,
  1651. unsigned int *found_pages)
  1652. {
  1653. unsigned int nr_pages;
  1654. struct page **pages;
  1655. struct cifs_writedata *wdata;
  1656. wdata = cifs_writedata_alloc((unsigned int)tofind,
  1657. cifs_writev_complete);
  1658. if (!wdata)
  1659. return NULL;
  1660. /*
  1661. * find_get_pages_tag seems to return a max of 256 on each
  1662. * iteration, so we must call it several times in order to
  1663. * fill the array or the wsize is effectively limited to
  1664. * 256 * PAGE_CACHE_SIZE.
  1665. */
  1666. *found_pages = 0;
  1667. pages = wdata->pages;
  1668. do {
  1669. nr_pages = find_get_pages_tag(mapping, index,
  1670. PAGECACHE_TAG_DIRTY, tofind,
  1671. pages);
  1672. *found_pages += nr_pages;
  1673. tofind -= nr_pages;
  1674. pages += nr_pages;
  1675. } while (nr_pages && tofind && *index <= end);
  1676. return wdata;
  1677. }
  1678. static unsigned int
  1679. wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
  1680. struct address_space *mapping,
  1681. struct writeback_control *wbc,
  1682. pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
  1683. {
  1684. unsigned int nr_pages = 0, i;
  1685. struct page *page;
  1686. for (i = 0; i < found_pages; i++) {
  1687. page = wdata->pages[i];
  1688. /*
  1689. * At this point we hold neither mapping->tree_lock nor
  1690. * lock on the page itself: the page may be truncated or
  1691. * invalidated (changing page->mapping to NULL), or even
  1692. * swizzled back from swapper_space to tmpfs file
  1693. * mapping
  1694. */
  1695. if (nr_pages == 0)
  1696. lock_page(page);
  1697. else if (!trylock_page(page))
  1698. break;
  1699. if (unlikely(page->mapping != mapping)) {
  1700. unlock_page(page);
  1701. break;
  1702. }
  1703. if (!wbc->range_cyclic && page->index > end) {
  1704. *done = true;
  1705. unlock_page(page);
  1706. break;
  1707. }
  1708. if (*next && (page->index != *next)) {
  1709. /* Not next consecutive page */
  1710. unlock_page(page);
  1711. break;
  1712. }
  1713. if (wbc->sync_mode != WB_SYNC_NONE)
  1714. wait_on_page_writeback(page);
  1715. if (PageWriteback(page) ||
  1716. !clear_page_dirty_for_io(page)) {
  1717. unlock_page(page);
  1718. break;
  1719. }
  1720. /*
  1721. * This actually clears the dirty bit in the radix tree.
  1722. * See cifs_writepage() for more commentary.
  1723. */
  1724. set_page_writeback(page);
  1725. if (page_offset(page) >= i_size_read(mapping->host)) {
  1726. *done = true;
  1727. unlock_page(page);
  1728. end_page_writeback(page);
  1729. break;
  1730. }
  1731. wdata->pages[i] = page;
  1732. *next = page->index + 1;
  1733. ++nr_pages;
  1734. }
  1735. /* reset index to refind any pages skipped */
  1736. if (nr_pages == 0)
  1737. *index = wdata->pages[0]->index + 1;
  1738. /* put any pages we aren't going to use */
  1739. for (i = nr_pages; i < found_pages; i++) {
  1740. page_cache_release(wdata->pages[i]);
  1741. wdata->pages[i] = NULL;
  1742. }
  1743. return nr_pages;
  1744. }
  1745. static int
  1746. wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
  1747. struct address_space *mapping, struct writeback_control *wbc)
  1748. {
  1749. int rc = 0;
  1750. struct TCP_Server_Info *server;
  1751. unsigned int i;
  1752. wdata->sync_mode = wbc->sync_mode;
  1753. wdata->nr_pages = nr_pages;
  1754. wdata->offset = page_offset(wdata->pages[0]);
  1755. wdata->pagesz = PAGE_CACHE_SIZE;
  1756. wdata->tailsz = min(i_size_read(mapping->host) -
  1757. page_offset(wdata->pages[nr_pages - 1]),
  1758. (loff_t)PAGE_CACHE_SIZE);
  1759. wdata->bytes = ((nr_pages - 1) * PAGE_CACHE_SIZE) + wdata->tailsz;
  1760. if (wdata->cfile != NULL)
  1761. cifsFileInfo_put(wdata->cfile);
  1762. wdata->cfile = find_writable_file(CIFS_I(mapping->host), false);
  1763. if (!wdata->cfile) {
  1764. cifs_dbg(VFS, "No writable handles for inode\n");
  1765. rc = -EBADF;
  1766. } else {
  1767. wdata->pid = wdata->cfile->pid;
  1768. server = tlink_tcon(wdata->cfile->tlink)->ses->server;
  1769. rc = server->ops->async_writev(wdata, cifs_writedata_release);
  1770. }
  1771. for (i = 0; i < nr_pages; ++i)
  1772. unlock_page(wdata->pages[i]);
  1773. return rc;
  1774. }
  1775. static int cifs_writepages(struct address_space *mapping,
  1776. struct writeback_control *wbc)
  1777. {
  1778. struct cifs_sb_info *cifs_sb = CIFS_SB(mapping->host->i_sb);
  1779. struct TCP_Server_Info *server;
  1780. bool done = false, scanned = false, range_whole = false;
  1781. pgoff_t end, index;
  1782. struct cifs_writedata *wdata;
  1783. int rc = 0;
  1784. /*
  1785. * If wsize is smaller than the page cache size, default to writing
  1786. * one page at a time via cifs_writepage
  1787. */
  1788. if (cifs_sb->wsize < PAGE_CACHE_SIZE)
  1789. return generic_writepages(mapping, wbc);
  1790. if (wbc->range_cyclic) {
  1791. index = mapping->writeback_index; /* Start from prev offset */
  1792. end = -1;
  1793. } else {
  1794. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  1795. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1796. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  1797. range_whole = true;
  1798. scanned = true;
  1799. }
  1800. server = cifs_sb_master_tcon(cifs_sb)->ses->server;
  1801. retry:
  1802. while (!done && index <= end) {
  1803. unsigned int i, nr_pages, found_pages, wsize, credits;
  1804. pgoff_t next = 0, tofind, saved_index = index;
  1805. rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
  1806. &wsize, &credits);
  1807. if (rc)
  1808. break;
  1809. tofind = min((wsize / PAGE_CACHE_SIZE) - 1, end - index) + 1;
  1810. wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
  1811. &found_pages);
  1812. if (!wdata) {
  1813. rc = -ENOMEM;
  1814. add_credits_and_wake_if(server, credits, 0);
  1815. break;
  1816. }
  1817. if (found_pages == 0) {
  1818. kref_put(&wdata->refcount, cifs_writedata_release);
  1819. add_credits_and_wake_if(server, credits, 0);
  1820. break;
  1821. }
  1822. nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
  1823. end, &index, &next, &done);
  1824. /* nothing to write? */
  1825. if (nr_pages == 0) {
  1826. kref_put(&wdata->refcount, cifs_writedata_release);
  1827. add_credits_and_wake_if(server, credits, 0);
  1828. continue;
  1829. }
  1830. wdata->credits = credits;
  1831. rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
  1832. /* send failure -- clean up the mess */
  1833. if (rc != 0) {
  1834. add_credits_and_wake_if(server, wdata->credits, 0);
  1835. for (i = 0; i < nr_pages; ++i) {
  1836. if (rc == -EAGAIN)
  1837. redirty_page_for_writepage(wbc,
  1838. wdata->pages[i]);
  1839. else
  1840. SetPageError(wdata->pages[i]);
  1841. end_page_writeback(wdata->pages[i]);
  1842. page_cache_release(wdata->pages[i]);
  1843. }
  1844. if (rc != -EAGAIN)
  1845. mapping_set_error(mapping, rc);
  1846. }
  1847. kref_put(&wdata->refcount, cifs_writedata_release);
  1848. if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
  1849. index = saved_index;
  1850. continue;
  1851. }
  1852. wbc->nr_to_write -= nr_pages;
  1853. if (wbc->nr_to_write <= 0)
  1854. done = true;
  1855. index = next;
  1856. }
  1857. if (!scanned && !done) {
  1858. /*
  1859. * We hit the last page and there is more work to be done: wrap
  1860. * back to the start of the file
  1861. */
  1862. scanned = true;
  1863. index = 0;
  1864. goto retry;
  1865. }
  1866. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  1867. mapping->writeback_index = index;
  1868. return rc;
  1869. }
  1870. static int
  1871. cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
  1872. {
  1873. int rc;
  1874. unsigned int xid;
  1875. xid = get_xid();
  1876. /* BB add check for wbc flags */
  1877. page_cache_get(page);
  1878. if (!PageUptodate(page))
  1879. cifs_dbg(FYI, "ppw - page not up to date\n");
  1880. /*
  1881. * Set the "writeback" flag, and clear "dirty" in the radix tree.
  1882. *
  1883. * A writepage() implementation always needs to do either this,
  1884. * or re-dirty the page with "redirty_page_for_writepage()" in
  1885. * the case of a failure.
  1886. *
  1887. * Just unlocking the page will cause the radix tree tag-bits
  1888. * to fail to update with the state of the page correctly.
  1889. */
  1890. set_page_writeback(page);
  1891. retry_write:
  1892. rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
  1893. if (rc == -EAGAIN && wbc->sync_mode == WB_SYNC_ALL)
  1894. goto retry_write;
  1895. else if (rc == -EAGAIN)
  1896. redirty_page_for_writepage(wbc, page);
  1897. else if (rc != 0)
  1898. SetPageError(page);
  1899. else
  1900. SetPageUptodate(page);
  1901. end_page_writeback(page);
  1902. page_cache_release(page);
  1903. free_xid(xid);
  1904. return rc;
  1905. }
  1906. static int cifs_writepage(struct page *page, struct writeback_control *wbc)
  1907. {
  1908. int rc = cifs_writepage_locked(page, wbc);
  1909. unlock_page(page);
  1910. return rc;
  1911. }
  1912. static int cifs_write_end(struct file *file, struct address_space *mapping,
  1913. loff_t pos, unsigned len, unsigned copied,
  1914. struct page *page, void *fsdata)
  1915. {
  1916. int rc;
  1917. struct inode *inode = mapping->host;
  1918. struct cifsFileInfo *cfile = file->private_data;
  1919. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
  1920. __u32 pid;
  1921. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  1922. pid = cfile->pid;
  1923. else
  1924. pid = current->tgid;
  1925. cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
  1926. page, pos, copied);
  1927. if (PageChecked(page)) {
  1928. if (copied == len)
  1929. SetPageUptodate(page);
  1930. ClearPageChecked(page);
  1931. } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
  1932. SetPageUptodate(page);
  1933. if (!PageUptodate(page)) {
  1934. char *page_data;
  1935. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  1936. unsigned int xid;
  1937. xid = get_xid();
  1938. /* this is probably better than directly calling
  1939. partialpage_write since in this function the file handle is
  1940. known which we might as well leverage */
  1941. /* BB check if anything else missing out of ppw
  1942. such as updating last write time */
  1943. page_data = kmap(page);
  1944. rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
  1945. /* if (rc < 0) should we set writebehind rc? */
  1946. kunmap(page);
  1947. free_xid(xid);
  1948. } else {
  1949. rc = copied;
  1950. pos += copied;
  1951. set_page_dirty(page);
  1952. }
  1953. if (rc > 0) {
  1954. spin_lock(&inode->i_lock);
  1955. if (pos > inode->i_size)
  1956. i_size_write(inode, pos);
  1957. spin_unlock(&inode->i_lock);
  1958. }
  1959. unlock_page(page);
  1960. page_cache_release(page);
  1961. return rc;
  1962. }
  1963. int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
  1964. int datasync)
  1965. {
  1966. unsigned int xid;
  1967. int rc = 0;
  1968. struct cifs_tcon *tcon;
  1969. struct TCP_Server_Info *server;
  1970. struct cifsFileInfo *smbfile = file->private_data;
  1971. struct inode *inode = file_inode(file);
  1972. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1973. rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
  1974. if (rc)
  1975. return rc;
  1976. mutex_lock(&inode->i_mutex);
  1977. xid = get_xid();
  1978. cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
  1979. file, datasync);
  1980. if (!CIFS_CACHE_READ(CIFS_I(inode))) {
  1981. rc = cifs_zap_mapping(inode);
  1982. if (rc) {
  1983. cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
  1984. rc = 0; /* don't care about it in fsync */
  1985. }
  1986. }
  1987. tcon = tlink_tcon(smbfile->tlink);
  1988. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  1989. server = tcon->ses->server;
  1990. if (server->ops->flush)
  1991. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  1992. else
  1993. rc = -ENOSYS;
  1994. }
  1995. free_xid(xid);
  1996. mutex_unlock(&inode->i_mutex);
  1997. return rc;
  1998. }
  1999. int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  2000. {
  2001. unsigned int xid;
  2002. int rc = 0;
  2003. struct cifs_tcon *tcon;
  2004. struct TCP_Server_Info *server;
  2005. struct cifsFileInfo *smbfile = file->private_data;
  2006. struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
  2007. struct inode *inode = file->f_mapping->host;
  2008. rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
  2009. if (rc)
  2010. return rc;
  2011. mutex_lock(&inode->i_mutex);
  2012. xid = get_xid();
  2013. cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
  2014. file, datasync);
  2015. tcon = tlink_tcon(smbfile->tlink);
  2016. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  2017. server = tcon->ses->server;
  2018. if (server->ops->flush)
  2019. rc = server->ops->flush(xid, tcon, &smbfile->fid);
  2020. else
  2021. rc = -ENOSYS;
  2022. }
  2023. free_xid(xid);
  2024. mutex_unlock(&inode->i_mutex);
  2025. return rc;
  2026. }
  2027. /*
  2028. * As file closes, flush all cached write data for this inode checking
  2029. * for write behind errors.
  2030. */
  2031. int cifs_flush(struct file *file, fl_owner_t id)
  2032. {
  2033. struct inode *inode = file_inode(file);
  2034. int rc = 0;
  2035. if (file->f_mode & FMODE_WRITE)
  2036. rc = filemap_write_and_wait(inode->i_mapping);
  2037. cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
  2038. return rc;
  2039. }
  2040. static int
  2041. cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
  2042. {
  2043. int rc = 0;
  2044. unsigned long i;
  2045. for (i = 0; i < num_pages; i++) {
  2046. pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
  2047. if (!pages[i]) {
  2048. /*
  2049. * save number of pages we have already allocated and
  2050. * return with ENOMEM error
  2051. */
  2052. num_pages = i;
  2053. rc = -ENOMEM;
  2054. break;
  2055. }
  2056. }
  2057. if (rc) {
  2058. for (i = 0; i < num_pages; i++)
  2059. put_page(pages[i]);
  2060. }
  2061. return rc;
  2062. }
  2063. static inline
  2064. size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
  2065. {
  2066. size_t num_pages;
  2067. size_t clen;
  2068. clen = min_t(const size_t, len, wsize);
  2069. num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
  2070. if (cur_len)
  2071. *cur_len = clen;
  2072. return num_pages;
  2073. }
  2074. static void
  2075. cifs_uncached_writedata_release(struct kref *refcount)
  2076. {
  2077. int i;
  2078. struct cifs_writedata *wdata = container_of(refcount,
  2079. struct cifs_writedata, refcount);
  2080. for (i = 0; i < wdata->nr_pages; i++)
  2081. put_page(wdata->pages[i]);
  2082. cifs_writedata_release(refcount);
  2083. }
  2084. static void
  2085. cifs_uncached_writev_complete(struct work_struct *work)
  2086. {
  2087. struct cifs_writedata *wdata = container_of(work,
  2088. struct cifs_writedata, work);
  2089. struct inode *inode = d_inode(wdata->cfile->dentry);
  2090. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  2091. spin_lock(&inode->i_lock);
  2092. cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
  2093. if (cifsi->server_eof > inode->i_size)
  2094. i_size_write(inode, cifsi->server_eof);
  2095. spin_unlock(&inode->i_lock);
  2096. complete(&wdata->done);
  2097. kref_put(&wdata->refcount, cifs_uncached_writedata_release);
  2098. }
  2099. static int
  2100. wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
  2101. size_t *len, unsigned long *num_pages)
  2102. {
  2103. size_t save_len, copied, bytes, cur_len = *len;
  2104. unsigned long i, nr_pages = *num_pages;
  2105. save_len = cur_len;
  2106. for (i = 0; i < nr_pages; i++) {
  2107. bytes = min_t(const size_t, cur_len, PAGE_SIZE);
  2108. copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
  2109. cur_len -= copied;
  2110. /*
  2111. * If we didn't copy as much as we expected, then that
  2112. * may mean we trod into an unmapped area. Stop copying
  2113. * at that point. On the next pass through the big
  2114. * loop, we'll likely end up getting a zero-length
  2115. * write and bailing out of it.
  2116. */
  2117. if (copied < bytes)
  2118. break;
  2119. }
  2120. cur_len = save_len - cur_len;
  2121. *len = cur_len;
  2122. /*
  2123. * If we have no data to send, then that probably means that
  2124. * the copy above failed altogether. That's most likely because
  2125. * the address in the iovec was bogus. Return -EFAULT and let
  2126. * the caller free anything we allocated and bail out.
  2127. */
  2128. if (!cur_len)
  2129. return -EFAULT;
  2130. /*
  2131. * i + 1 now represents the number of pages we actually used in
  2132. * the copy phase above.
  2133. */
  2134. *num_pages = i + 1;
  2135. return 0;
  2136. }
  2137. static int
  2138. cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
  2139. struct cifsFileInfo *open_file,
  2140. struct cifs_sb_info *cifs_sb, struct list_head *wdata_list)
  2141. {
  2142. int rc = 0;
  2143. size_t cur_len;
  2144. unsigned long nr_pages, num_pages, i;
  2145. struct cifs_writedata *wdata;
  2146. struct iov_iter saved_from;
  2147. loff_t saved_offset = offset;
  2148. pid_t pid;
  2149. struct TCP_Server_Info *server;
  2150. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2151. pid = open_file->pid;
  2152. else
  2153. pid = current->tgid;
  2154. server = tlink_tcon(open_file->tlink)->ses->server;
  2155. memcpy(&saved_from, from, sizeof(struct iov_iter));
  2156. do {
  2157. unsigned int wsize, credits;
  2158. rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
  2159. &wsize, &credits);
  2160. if (rc)
  2161. break;
  2162. nr_pages = get_numpages(wsize, len, &cur_len);
  2163. wdata = cifs_writedata_alloc(nr_pages,
  2164. cifs_uncached_writev_complete);
  2165. if (!wdata) {
  2166. rc = -ENOMEM;
  2167. add_credits_and_wake_if(server, credits, 0);
  2168. break;
  2169. }
  2170. rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
  2171. if (rc) {
  2172. kfree(wdata);
  2173. add_credits_and_wake_if(server, credits, 0);
  2174. break;
  2175. }
  2176. num_pages = nr_pages;
  2177. rc = wdata_fill_from_iovec(wdata, from, &cur_len, &num_pages);
  2178. if (rc) {
  2179. for (i = 0; i < nr_pages; i++)
  2180. put_page(wdata->pages[i]);
  2181. kfree(wdata);
  2182. add_credits_and_wake_if(server, credits, 0);
  2183. break;
  2184. }
  2185. /*
  2186. * Bring nr_pages down to the number of pages we actually used,
  2187. * and free any pages that we didn't use.
  2188. */
  2189. for ( ; nr_pages > num_pages; nr_pages--)
  2190. put_page(wdata->pages[nr_pages - 1]);
  2191. wdata->sync_mode = WB_SYNC_ALL;
  2192. wdata->nr_pages = nr_pages;
  2193. wdata->offset = (__u64)offset;
  2194. wdata->cfile = cifsFileInfo_get(open_file);
  2195. wdata->pid = pid;
  2196. wdata->bytes = cur_len;
  2197. wdata->pagesz = PAGE_SIZE;
  2198. wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
  2199. wdata->credits = credits;
  2200. if (!wdata->cfile->invalidHandle ||
  2201. !cifs_reopen_file(wdata->cfile, false))
  2202. rc = server->ops->async_writev(wdata,
  2203. cifs_uncached_writedata_release);
  2204. if (rc) {
  2205. add_credits_and_wake_if(server, wdata->credits, 0);
  2206. kref_put(&wdata->refcount,
  2207. cifs_uncached_writedata_release);
  2208. if (rc == -EAGAIN) {
  2209. memcpy(from, &saved_from,
  2210. sizeof(struct iov_iter));
  2211. iov_iter_advance(from, offset - saved_offset);
  2212. continue;
  2213. }
  2214. break;
  2215. }
  2216. list_add_tail(&wdata->list, wdata_list);
  2217. offset += cur_len;
  2218. len -= cur_len;
  2219. } while (len > 0);
  2220. return rc;
  2221. }
  2222. ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
  2223. {
  2224. struct file *file = iocb->ki_filp;
  2225. ssize_t total_written = 0;
  2226. struct cifsFileInfo *open_file;
  2227. struct cifs_tcon *tcon;
  2228. struct cifs_sb_info *cifs_sb;
  2229. struct cifs_writedata *wdata, *tmp;
  2230. struct list_head wdata_list;
  2231. struct iov_iter saved_from;
  2232. int rc;
  2233. /*
  2234. * BB - optimize the way when signing is disabled. We can drop this
  2235. * extra memory-to-memory copying and use iovec buffers for constructing
  2236. * write request.
  2237. */
  2238. rc = generic_write_checks(iocb, from);
  2239. if (rc <= 0)
  2240. return rc;
  2241. INIT_LIST_HEAD(&wdata_list);
  2242. cifs_sb = CIFS_FILE_SB(file);
  2243. open_file = file->private_data;
  2244. tcon = tlink_tcon(open_file->tlink);
  2245. if (!tcon->ses->server->ops->async_writev)
  2246. return -ENOSYS;
  2247. memcpy(&saved_from, from, sizeof(struct iov_iter));
  2248. rc = cifs_write_from_iter(iocb->ki_pos, iov_iter_count(from), from,
  2249. open_file, cifs_sb, &wdata_list);
  2250. /*
  2251. * If at least one write was successfully sent, then discard any rc
  2252. * value from the later writes. If the other write succeeds, then
  2253. * we'll end up returning whatever was written. If it fails, then
  2254. * we'll get a new rc value from that.
  2255. */
  2256. if (!list_empty(&wdata_list))
  2257. rc = 0;
  2258. /*
  2259. * Wait for and collect replies for any successful sends in order of
  2260. * increasing offset. Once an error is hit or we get a fatal signal
  2261. * while waiting, then return without waiting for any more replies.
  2262. */
  2263. restart_loop:
  2264. list_for_each_entry_safe(wdata, tmp, &wdata_list, list) {
  2265. if (!rc) {
  2266. /* FIXME: freezable too? */
  2267. rc = wait_for_completion_killable(&wdata->done);
  2268. if (rc)
  2269. rc = -EINTR;
  2270. else if (wdata->result)
  2271. rc = wdata->result;
  2272. else
  2273. total_written += wdata->bytes;
  2274. /* resend call if it's a retryable error */
  2275. if (rc == -EAGAIN) {
  2276. struct list_head tmp_list;
  2277. struct iov_iter tmp_from;
  2278. INIT_LIST_HEAD(&tmp_list);
  2279. list_del_init(&wdata->list);
  2280. memcpy(&tmp_from, &saved_from,
  2281. sizeof(struct iov_iter));
  2282. iov_iter_advance(&tmp_from,
  2283. wdata->offset - iocb->ki_pos);
  2284. rc = cifs_write_from_iter(wdata->offset,
  2285. wdata->bytes, &tmp_from,
  2286. open_file, cifs_sb, &tmp_list);
  2287. list_splice(&tmp_list, &wdata_list);
  2288. kref_put(&wdata->refcount,
  2289. cifs_uncached_writedata_release);
  2290. goto restart_loop;
  2291. }
  2292. }
  2293. list_del_init(&wdata->list);
  2294. kref_put(&wdata->refcount, cifs_uncached_writedata_release);
  2295. }
  2296. if (unlikely(!total_written))
  2297. return rc;
  2298. iocb->ki_pos += total_written;
  2299. set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(file_inode(file))->flags);
  2300. cifs_stats_bytes_written(tcon, total_written);
  2301. return total_written;
  2302. }
  2303. static ssize_t
  2304. cifs_writev(struct kiocb *iocb, struct iov_iter *from)
  2305. {
  2306. struct file *file = iocb->ki_filp;
  2307. struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
  2308. struct inode *inode = file->f_mapping->host;
  2309. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2310. struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
  2311. ssize_t rc;
  2312. /*
  2313. * We need to hold the sem to be sure nobody modifies lock list
  2314. * with a brlock that prevents writing.
  2315. */
  2316. down_read(&cinode->lock_sem);
  2317. mutex_lock(&inode->i_mutex);
  2318. rc = generic_write_checks(iocb, from);
  2319. if (rc <= 0)
  2320. goto out;
  2321. if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
  2322. server->vals->exclusive_lock_type, NULL,
  2323. CIFS_WRITE_OP))
  2324. rc = __generic_file_write_iter(iocb, from);
  2325. else
  2326. rc = -EACCES;
  2327. out:
  2328. mutex_unlock(&inode->i_mutex);
  2329. if (rc > 0) {
  2330. ssize_t err = generic_write_sync(file, iocb->ki_pos - rc, rc);
  2331. if (err < 0)
  2332. rc = err;
  2333. }
  2334. up_read(&cinode->lock_sem);
  2335. return rc;
  2336. }
  2337. ssize_t
  2338. cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
  2339. {
  2340. struct inode *inode = file_inode(iocb->ki_filp);
  2341. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2342. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2343. struct cifsFileInfo *cfile = (struct cifsFileInfo *)
  2344. iocb->ki_filp->private_data;
  2345. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  2346. ssize_t written;
  2347. written = cifs_get_writer(cinode);
  2348. if (written)
  2349. return written;
  2350. if (CIFS_CACHE_WRITE(cinode)) {
  2351. if (cap_unix(tcon->ses) &&
  2352. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
  2353. && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
  2354. written = generic_file_write_iter(iocb, from);
  2355. goto out;
  2356. }
  2357. written = cifs_writev(iocb, from);
  2358. goto out;
  2359. }
  2360. /*
  2361. * For non-oplocked files in strict cache mode we need to write the data
  2362. * to the server exactly from the pos to pos+len-1 rather than flush all
  2363. * affected pages because it may cause a error with mandatory locks on
  2364. * these pages but not on the region from pos to ppos+len-1.
  2365. */
  2366. written = cifs_user_writev(iocb, from);
  2367. if (written > 0 && CIFS_CACHE_READ(cinode)) {
  2368. /*
  2369. * Windows 7 server can delay breaking level2 oplock if a write
  2370. * request comes - break it on the client to prevent reading
  2371. * an old data.
  2372. */
  2373. cifs_zap_mapping(inode);
  2374. cifs_dbg(FYI, "Set no oplock for inode=%p after a write operation\n",
  2375. inode);
  2376. cinode->oplock = 0;
  2377. }
  2378. out:
  2379. cifs_put_writer(cinode);
  2380. return written;
  2381. }
  2382. static struct cifs_readdata *
  2383. cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
  2384. {
  2385. struct cifs_readdata *rdata;
  2386. rdata = kzalloc(sizeof(*rdata) + (sizeof(struct page *) * nr_pages),
  2387. GFP_KERNEL);
  2388. if (rdata != NULL) {
  2389. kref_init(&rdata->refcount);
  2390. INIT_LIST_HEAD(&rdata->list);
  2391. init_completion(&rdata->done);
  2392. INIT_WORK(&rdata->work, complete);
  2393. }
  2394. return rdata;
  2395. }
  2396. void
  2397. cifs_readdata_release(struct kref *refcount)
  2398. {
  2399. struct cifs_readdata *rdata = container_of(refcount,
  2400. struct cifs_readdata, refcount);
  2401. if (rdata->cfile)
  2402. cifsFileInfo_put(rdata->cfile);
  2403. kfree(rdata);
  2404. }
  2405. static int
  2406. cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
  2407. {
  2408. int rc = 0;
  2409. struct page *page;
  2410. unsigned int i;
  2411. for (i = 0; i < nr_pages; i++) {
  2412. page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
  2413. if (!page) {
  2414. rc = -ENOMEM;
  2415. break;
  2416. }
  2417. rdata->pages[i] = page;
  2418. }
  2419. if (rc) {
  2420. for (i = 0; i < nr_pages; i++) {
  2421. put_page(rdata->pages[i]);
  2422. rdata->pages[i] = NULL;
  2423. }
  2424. }
  2425. return rc;
  2426. }
  2427. static void
  2428. cifs_uncached_readdata_release(struct kref *refcount)
  2429. {
  2430. struct cifs_readdata *rdata = container_of(refcount,
  2431. struct cifs_readdata, refcount);
  2432. unsigned int i;
  2433. for (i = 0; i < rdata->nr_pages; i++) {
  2434. put_page(rdata->pages[i]);
  2435. rdata->pages[i] = NULL;
  2436. }
  2437. cifs_readdata_release(refcount);
  2438. }
  2439. /**
  2440. * cifs_readdata_to_iov - copy data from pages in response to an iovec
  2441. * @rdata: the readdata response with list of pages holding data
  2442. * @iter: destination for our data
  2443. *
  2444. * This function copies data from a list of pages in a readdata response into
  2445. * an array of iovecs. It will first calculate where the data should go
  2446. * based on the info in the readdata and then copy the data into that spot.
  2447. */
  2448. static int
  2449. cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
  2450. {
  2451. size_t remaining = rdata->got_bytes;
  2452. unsigned int i;
  2453. for (i = 0; i < rdata->nr_pages; i++) {
  2454. struct page *page = rdata->pages[i];
  2455. size_t copy = min_t(size_t, remaining, PAGE_SIZE);
  2456. size_t written = copy_page_to_iter(page, 0, copy, iter);
  2457. remaining -= written;
  2458. if (written < copy && iov_iter_count(iter) > 0)
  2459. break;
  2460. }
  2461. return remaining ? -EFAULT : 0;
  2462. }
  2463. static void
  2464. cifs_uncached_readv_complete(struct work_struct *work)
  2465. {
  2466. struct cifs_readdata *rdata = container_of(work,
  2467. struct cifs_readdata, work);
  2468. complete(&rdata->done);
  2469. kref_put(&rdata->refcount, cifs_uncached_readdata_release);
  2470. }
  2471. static int
  2472. cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
  2473. struct cifs_readdata *rdata, unsigned int len)
  2474. {
  2475. int result = 0;
  2476. unsigned int i;
  2477. unsigned int nr_pages = rdata->nr_pages;
  2478. struct kvec iov;
  2479. rdata->got_bytes = 0;
  2480. rdata->tailsz = PAGE_SIZE;
  2481. for (i = 0; i < nr_pages; i++) {
  2482. struct page *page = rdata->pages[i];
  2483. if (len >= PAGE_SIZE) {
  2484. /* enough data to fill the page */
  2485. iov.iov_base = kmap(page);
  2486. iov.iov_len = PAGE_SIZE;
  2487. cifs_dbg(FYI, "%u: iov_base=%p iov_len=%zu\n",
  2488. i, iov.iov_base, iov.iov_len);
  2489. len -= PAGE_SIZE;
  2490. } else if (len > 0) {
  2491. /* enough for partial page, fill and zero the rest */
  2492. iov.iov_base = kmap(page);
  2493. iov.iov_len = len;
  2494. cifs_dbg(FYI, "%u: iov_base=%p iov_len=%zu\n",
  2495. i, iov.iov_base, iov.iov_len);
  2496. memset(iov.iov_base + len, '\0', PAGE_SIZE - len);
  2497. rdata->tailsz = len;
  2498. len = 0;
  2499. } else {
  2500. /* no need to hold page hostage */
  2501. rdata->pages[i] = NULL;
  2502. rdata->nr_pages--;
  2503. put_page(page);
  2504. continue;
  2505. }
  2506. result = cifs_readv_from_socket(server, &iov, 1, iov.iov_len);
  2507. kunmap(page);
  2508. if (result < 0)
  2509. break;
  2510. rdata->got_bytes += result;
  2511. }
  2512. return rdata->got_bytes > 0 && result != -ECONNABORTED ?
  2513. rdata->got_bytes : result;
  2514. }
  2515. static int
  2516. cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
  2517. struct cifs_sb_info *cifs_sb, struct list_head *rdata_list)
  2518. {
  2519. struct cifs_readdata *rdata;
  2520. unsigned int npages, rsize, credits;
  2521. size_t cur_len;
  2522. int rc;
  2523. pid_t pid;
  2524. struct TCP_Server_Info *server;
  2525. server = tlink_tcon(open_file->tlink)->ses->server;
  2526. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2527. pid = open_file->pid;
  2528. else
  2529. pid = current->tgid;
  2530. do {
  2531. rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
  2532. &rsize, &credits);
  2533. if (rc)
  2534. break;
  2535. cur_len = min_t(const size_t, len, rsize);
  2536. npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
  2537. /* allocate a readdata struct */
  2538. rdata = cifs_readdata_alloc(npages,
  2539. cifs_uncached_readv_complete);
  2540. if (!rdata) {
  2541. add_credits_and_wake_if(server, credits, 0);
  2542. rc = -ENOMEM;
  2543. break;
  2544. }
  2545. rc = cifs_read_allocate_pages(rdata, npages);
  2546. if (rc)
  2547. goto error;
  2548. rdata->cfile = cifsFileInfo_get(open_file);
  2549. rdata->nr_pages = npages;
  2550. rdata->offset = offset;
  2551. rdata->bytes = cur_len;
  2552. rdata->pid = pid;
  2553. rdata->pagesz = PAGE_SIZE;
  2554. rdata->read_into_pages = cifs_uncached_read_into_pages;
  2555. rdata->credits = credits;
  2556. if (!rdata->cfile->invalidHandle ||
  2557. !cifs_reopen_file(rdata->cfile, true))
  2558. rc = server->ops->async_readv(rdata);
  2559. error:
  2560. if (rc) {
  2561. add_credits_and_wake_if(server, rdata->credits, 0);
  2562. kref_put(&rdata->refcount,
  2563. cifs_uncached_readdata_release);
  2564. if (rc == -EAGAIN)
  2565. continue;
  2566. break;
  2567. }
  2568. list_add_tail(&rdata->list, rdata_list);
  2569. offset += cur_len;
  2570. len -= cur_len;
  2571. } while (len > 0);
  2572. return rc;
  2573. }
  2574. ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
  2575. {
  2576. struct file *file = iocb->ki_filp;
  2577. ssize_t rc;
  2578. size_t len;
  2579. ssize_t total_read = 0;
  2580. loff_t offset = iocb->ki_pos;
  2581. struct cifs_sb_info *cifs_sb;
  2582. struct cifs_tcon *tcon;
  2583. struct cifsFileInfo *open_file;
  2584. struct cifs_readdata *rdata, *tmp;
  2585. struct list_head rdata_list;
  2586. len = iov_iter_count(to);
  2587. if (!len)
  2588. return 0;
  2589. INIT_LIST_HEAD(&rdata_list);
  2590. cifs_sb = CIFS_FILE_SB(file);
  2591. open_file = file->private_data;
  2592. tcon = tlink_tcon(open_file->tlink);
  2593. if (!tcon->ses->server->ops->async_readv)
  2594. return -ENOSYS;
  2595. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  2596. cifs_dbg(FYI, "attempting read on write only file instance\n");
  2597. rc = cifs_send_async_read(offset, len, open_file, cifs_sb, &rdata_list);
  2598. /* if at least one read request send succeeded, then reset rc */
  2599. if (!list_empty(&rdata_list))
  2600. rc = 0;
  2601. len = iov_iter_count(to);
  2602. /* the loop below should proceed in the order of increasing offsets */
  2603. again:
  2604. list_for_each_entry_safe(rdata, tmp, &rdata_list, list) {
  2605. if (!rc) {
  2606. /* FIXME: freezable sleep too? */
  2607. rc = wait_for_completion_killable(&rdata->done);
  2608. if (rc)
  2609. rc = -EINTR;
  2610. else if (rdata->result == -EAGAIN) {
  2611. /* resend call if it's a retryable error */
  2612. struct list_head tmp_list;
  2613. unsigned int got_bytes = rdata->got_bytes;
  2614. list_del_init(&rdata->list);
  2615. INIT_LIST_HEAD(&tmp_list);
  2616. /*
  2617. * Got a part of data and then reconnect has
  2618. * happened -- fill the buffer and continue
  2619. * reading.
  2620. */
  2621. if (got_bytes && got_bytes < rdata->bytes) {
  2622. rc = cifs_readdata_to_iov(rdata, to);
  2623. if (rc) {
  2624. kref_put(&rdata->refcount,
  2625. cifs_uncached_readdata_release);
  2626. continue;
  2627. }
  2628. }
  2629. rc = cifs_send_async_read(
  2630. rdata->offset + got_bytes,
  2631. rdata->bytes - got_bytes,
  2632. rdata->cfile, cifs_sb,
  2633. &tmp_list);
  2634. list_splice(&tmp_list, &rdata_list);
  2635. kref_put(&rdata->refcount,
  2636. cifs_uncached_readdata_release);
  2637. goto again;
  2638. } else if (rdata->result)
  2639. rc = rdata->result;
  2640. else
  2641. rc = cifs_readdata_to_iov(rdata, to);
  2642. /* if there was a short read -- discard anything left */
  2643. if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
  2644. rc = -ENODATA;
  2645. }
  2646. list_del_init(&rdata->list);
  2647. kref_put(&rdata->refcount, cifs_uncached_readdata_release);
  2648. }
  2649. total_read = len - iov_iter_count(to);
  2650. cifs_stats_bytes_read(tcon, total_read);
  2651. /* mask nodata case */
  2652. if (rc == -ENODATA)
  2653. rc = 0;
  2654. if (total_read) {
  2655. iocb->ki_pos += total_read;
  2656. return total_read;
  2657. }
  2658. return rc;
  2659. }
  2660. ssize_t
  2661. cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
  2662. {
  2663. struct inode *inode = file_inode(iocb->ki_filp);
  2664. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2665. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2666. struct cifsFileInfo *cfile = (struct cifsFileInfo *)
  2667. iocb->ki_filp->private_data;
  2668. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  2669. int rc = -EACCES;
  2670. /*
  2671. * In strict cache mode we need to read from the server all the time
  2672. * if we don't have level II oplock because the server can delay mtime
  2673. * change - so we can't make a decision about inode invalidating.
  2674. * And we can also fail with pagereading if there are mandatory locks
  2675. * on pages affected by this read but not on the region from pos to
  2676. * pos+len-1.
  2677. */
  2678. if (!CIFS_CACHE_READ(cinode))
  2679. return cifs_user_readv(iocb, to);
  2680. if (cap_unix(tcon->ses) &&
  2681. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  2682. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  2683. return generic_file_read_iter(iocb, to);
  2684. /*
  2685. * We need to hold the sem to be sure nobody modifies lock list
  2686. * with a brlock that prevents reading.
  2687. */
  2688. down_read(&cinode->lock_sem);
  2689. if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
  2690. tcon->ses->server->vals->shared_lock_type,
  2691. NULL, CIFS_READ_OP))
  2692. rc = generic_file_read_iter(iocb, to);
  2693. up_read(&cinode->lock_sem);
  2694. return rc;
  2695. }
  2696. static ssize_t
  2697. cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
  2698. {
  2699. int rc = -EACCES;
  2700. unsigned int bytes_read = 0;
  2701. unsigned int total_read;
  2702. unsigned int current_read_size;
  2703. unsigned int rsize;
  2704. struct cifs_sb_info *cifs_sb;
  2705. struct cifs_tcon *tcon;
  2706. struct TCP_Server_Info *server;
  2707. unsigned int xid;
  2708. char *cur_offset;
  2709. struct cifsFileInfo *open_file;
  2710. struct cifs_io_parms io_parms;
  2711. int buf_type = CIFS_NO_BUFFER;
  2712. __u32 pid;
  2713. xid = get_xid();
  2714. cifs_sb = CIFS_FILE_SB(file);
  2715. /* FIXME: set up handlers for larger reads and/or convert to async */
  2716. rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
  2717. if (file->private_data == NULL) {
  2718. rc = -EBADF;
  2719. free_xid(xid);
  2720. return rc;
  2721. }
  2722. open_file = file->private_data;
  2723. tcon = tlink_tcon(open_file->tlink);
  2724. server = tcon->ses->server;
  2725. if (!server->ops->sync_read) {
  2726. free_xid(xid);
  2727. return -ENOSYS;
  2728. }
  2729. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2730. pid = open_file->pid;
  2731. else
  2732. pid = current->tgid;
  2733. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  2734. cifs_dbg(FYI, "attempting read on write only file instance\n");
  2735. for (total_read = 0, cur_offset = read_data; read_size > total_read;
  2736. total_read += bytes_read, cur_offset += bytes_read) {
  2737. do {
  2738. current_read_size = min_t(uint, read_size - total_read,
  2739. rsize);
  2740. /*
  2741. * For windows me and 9x we do not want to request more
  2742. * than it negotiated since it will refuse the read
  2743. * then.
  2744. */
  2745. if ((tcon->ses) && !(tcon->ses->capabilities &
  2746. tcon->ses->server->vals->cap_large_files)) {
  2747. current_read_size = min_t(uint,
  2748. current_read_size, CIFSMaxBufSize);
  2749. }
  2750. if (open_file->invalidHandle) {
  2751. rc = cifs_reopen_file(open_file, true);
  2752. if (rc != 0)
  2753. break;
  2754. }
  2755. io_parms.pid = pid;
  2756. io_parms.tcon = tcon;
  2757. io_parms.offset = *offset;
  2758. io_parms.length = current_read_size;
  2759. rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
  2760. &bytes_read, &cur_offset,
  2761. &buf_type);
  2762. } while (rc == -EAGAIN);
  2763. if (rc || (bytes_read == 0)) {
  2764. if (total_read) {
  2765. break;
  2766. } else {
  2767. free_xid(xid);
  2768. return rc;
  2769. }
  2770. } else {
  2771. cifs_stats_bytes_read(tcon, total_read);
  2772. *offset += bytes_read;
  2773. }
  2774. }
  2775. free_xid(xid);
  2776. return total_read;
  2777. }
  2778. /*
  2779. * If the page is mmap'ed into a process' page tables, then we need to make
  2780. * sure that it doesn't change while being written back.
  2781. */
  2782. static int
  2783. cifs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  2784. {
  2785. struct page *page = vmf->page;
  2786. lock_page(page);
  2787. return VM_FAULT_LOCKED;
  2788. }
  2789. static struct vm_operations_struct cifs_file_vm_ops = {
  2790. .fault = filemap_fault,
  2791. .map_pages = filemap_map_pages,
  2792. .page_mkwrite = cifs_page_mkwrite,
  2793. };
  2794. int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
  2795. {
  2796. int rc, xid;
  2797. struct inode *inode = file_inode(file);
  2798. xid = get_xid();
  2799. if (!CIFS_CACHE_READ(CIFS_I(inode))) {
  2800. rc = cifs_zap_mapping(inode);
  2801. if (rc)
  2802. return rc;
  2803. }
  2804. rc = generic_file_mmap(file, vma);
  2805. if (rc == 0)
  2806. vma->vm_ops = &cifs_file_vm_ops;
  2807. free_xid(xid);
  2808. return rc;
  2809. }
  2810. int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
  2811. {
  2812. int rc, xid;
  2813. xid = get_xid();
  2814. rc = cifs_revalidate_file(file);
  2815. if (rc) {
  2816. cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
  2817. rc);
  2818. free_xid(xid);
  2819. return rc;
  2820. }
  2821. rc = generic_file_mmap(file, vma);
  2822. if (rc == 0)
  2823. vma->vm_ops = &cifs_file_vm_ops;
  2824. free_xid(xid);
  2825. return rc;
  2826. }
  2827. static void
  2828. cifs_readv_complete(struct work_struct *work)
  2829. {
  2830. unsigned int i, got_bytes;
  2831. struct cifs_readdata *rdata = container_of(work,
  2832. struct cifs_readdata, work);
  2833. got_bytes = rdata->got_bytes;
  2834. for (i = 0; i < rdata->nr_pages; i++) {
  2835. struct page *page = rdata->pages[i];
  2836. lru_cache_add_file(page);
  2837. if (rdata->result == 0 ||
  2838. (rdata->result == -EAGAIN && got_bytes)) {
  2839. flush_dcache_page(page);
  2840. SetPageUptodate(page);
  2841. }
  2842. unlock_page(page);
  2843. if (rdata->result == 0 ||
  2844. (rdata->result == -EAGAIN && got_bytes))
  2845. cifs_readpage_to_fscache(rdata->mapping->host, page);
  2846. got_bytes -= min_t(unsigned int, PAGE_CACHE_SIZE, got_bytes);
  2847. page_cache_release(page);
  2848. rdata->pages[i] = NULL;
  2849. }
  2850. kref_put(&rdata->refcount, cifs_readdata_release);
  2851. }
  2852. static int
  2853. cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
  2854. struct cifs_readdata *rdata, unsigned int len)
  2855. {
  2856. int result = 0;
  2857. unsigned int i;
  2858. u64 eof;
  2859. pgoff_t eof_index;
  2860. unsigned int nr_pages = rdata->nr_pages;
  2861. struct kvec iov;
  2862. /* determine the eof that the server (probably) has */
  2863. eof = CIFS_I(rdata->mapping->host)->server_eof;
  2864. eof_index = eof ? (eof - 1) >> PAGE_CACHE_SHIFT : 0;
  2865. cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
  2866. rdata->got_bytes = 0;
  2867. rdata->tailsz = PAGE_CACHE_SIZE;
  2868. for (i = 0; i < nr_pages; i++) {
  2869. struct page *page = rdata->pages[i];
  2870. if (len >= PAGE_CACHE_SIZE) {
  2871. /* enough data to fill the page */
  2872. iov.iov_base = kmap(page);
  2873. iov.iov_len = PAGE_CACHE_SIZE;
  2874. cifs_dbg(FYI, "%u: idx=%lu iov_base=%p iov_len=%zu\n",
  2875. i, page->index, iov.iov_base, iov.iov_len);
  2876. len -= PAGE_CACHE_SIZE;
  2877. } else if (len > 0) {
  2878. /* enough for partial page, fill and zero the rest */
  2879. iov.iov_base = kmap(page);
  2880. iov.iov_len = len;
  2881. cifs_dbg(FYI, "%u: idx=%lu iov_base=%p iov_len=%zu\n",
  2882. i, page->index, iov.iov_base, iov.iov_len);
  2883. memset(iov.iov_base + len,
  2884. '\0', PAGE_CACHE_SIZE - len);
  2885. rdata->tailsz = len;
  2886. len = 0;
  2887. } else if (page->index > eof_index) {
  2888. /*
  2889. * The VFS will not try to do readahead past the
  2890. * i_size, but it's possible that we have outstanding
  2891. * writes with gaps in the middle and the i_size hasn't
  2892. * caught up yet. Populate those with zeroed out pages
  2893. * to prevent the VFS from repeatedly attempting to
  2894. * fill them until the writes are flushed.
  2895. */
  2896. zero_user(page, 0, PAGE_CACHE_SIZE);
  2897. lru_cache_add_file(page);
  2898. flush_dcache_page(page);
  2899. SetPageUptodate(page);
  2900. unlock_page(page);
  2901. page_cache_release(page);
  2902. rdata->pages[i] = NULL;
  2903. rdata->nr_pages--;
  2904. continue;
  2905. } else {
  2906. /* no need to hold page hostage */
  2907. lru_cache_add_file(page);
  2908. unlock_page(page);
  2909. page_cache_release(page);
  2910. rdata->pages[i] = NULL;
  2911. rdata->nr_pages--;
  2912. continue;
  2913. }
  2914. result = cifs_readv_from_socket(server, &iov, 1, iov.iov_len);
  2915. kunmap(page);
  2916. if (result < 0)
  2917. break;
  2918. rdata->got_bytes += result;
  2919. }
  2920. return rdata->got_bytes > 0 && result != -ECONNABORTED ?
  2921. rdata->got_bytes : result;
  2922. }
  2923. static int
  2924. readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
  2925. unsigned int rsize, struct list_head *tmplist,
  2926. unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
  2927. {
  2928. struct page *page, *tpage;
  2929. unsigned int expected_index;
  2930. int rc;
  2931. INIT_LIST_HEAD(tmplist);
  2932. page = list_entry(page_list->prev, struct page, lru);
  2933. /*
  2934. * Lock the page and put it in the cache. Since no one else
  2935. * should have access to this page, we're safe to simply set
  2936. * PG_locked without checking it first.
  2937. */
  2938. __set_page_locked(page);
  2939. rc = add_to_page_cache_locked(page, mapping,
  2940. page->index, GFP_KERNEL);
  2941. /* give up if we can't stick it in the cache */
  2942. if (rc) {
  2943. __clear_page_locked(page);
  2944. return rc;
  2945. }
  2946. /* move first page to the tmplist */
  2947. *offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  2948. *bytes = PAGE_CACHE_SIZE;
  2949. *nr_pages = 1;
  2950. list_move_tail(&page->lru, tmplist);
  2951. /* now try and add more pages onto the request */
  2952. expected_index = page->index + 1;
  2953. list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
  2954. /* discontinuity ? */
  2955. if (page->index != expected_index)
  2956. break;
  2957. /* would this page push the read over the rsize? */
  2958. if (*bytes + PAGE_CACHE_SIZE > rsize)
  2959. break;
  2960. __set_page_locked(page);
  2961. if (add_to_page_cache_locked(page, mapping, page->index,
  2962. GFP_KERNEL)) {
  2963. __clear_page_locked(page);
  2964. break;
  2965. }
  2966. list_move_tail(&page->lru, tmplist);
  2967. (*bytes) += PAGE_CACHE_SIZE;
  2968. expected_index++;
  2969. (*nr_pages)++;
  2970. }
  2971. return rc;
  2972. }
  2973. static int cifs_readpages(struct file *file, struct address_space *mapping,
  2974. struct list_head *page_list, unsigned num_pages)
  2975. {
  2976. int rc;
  2977. struct list_head tmplist;
  2978. struct cifsFileInfo *open_file = file->private_data;
  2979. struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
  2980. struct TCP_Server_Info *server;
  2981. pid_t pid;
  2982. /*
  2983. * Reads as many pages as possible from fscache. Returns -ENOBUFS
  2984. * immediately if the cookie is negative
  2985. *
  2986. * After this point, every page in the list might have PG_fscache set,
  2987. * so we will need to clean that up off of every page we don't use.
  2988. */
  2989. rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
  2990. &num_pages);
  2991. if (rc == 0)
  2992. return rc;
  2993. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
  2994. pid = open_file->pid;
  2995. else
  2996. pid = current->tgid;
  2997. rc = 0;
  2998. server = tlink_tcon(open_file->tlink)->ses->server;
  2999. cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
  3000. __func__, file, mapping, num_pages);
  3001. /*
  3002. * Start with the page at end of list and move it to private
  3003. * list. Do the same with any following pages until we hit
  3004. * the rsize limit, hit an index discontinuity, or run out of
  3005. * pages. Issue the async read and then start the loop again
  3006. * until the list is empty.
  3007. *
  3008. * Note that list order is important. The page_list is in
  3009. * the order of declining indexes. When we put the pages in
  3010. * the rdata->pages, then we want them in increasing order.
  3011. */
  3012. while (!list_empty(page_list)) {
  3013. unsigned int i, nr_pages, bytes, rsize;
  3014. loff_t offset;
  3015. struct page *page, *tpage;
  3016. struct cifs_readdata *rdata;
  3017. unsigned credits;
  3018. rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
  3019. &rsize, &credits);
  3020. if (rc)
  3021. break;
  3022. /*
  3023. * Give up immediately if rsize is too small to read an entire
  3024. * page. The VFS will fall back to readpage. We should never
  3025. * reach this point however since we set ra_pages to 0 when the
  3026. * rsize is smaller than a cache page.
  3027. */
  3028. if (unlikely(rsize < PAGE_CACHE_SIZE)) {
  3029. add_credits_and_wake_if(server, credits, 0);
  3030. return 0;
  3031. }
  3032. rc = readpages_get_pages(mapping, page_list, rsize, &tmplist,
  3033. &nr_pages, &offset, &bytes);
  3034. if (rc) {
  3035. add_credits_and_wake_if(server, credits, 0);
  3036. break;
  3037. }
  3038. rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
  3039. if (!rdata) {
  3040. /* best to give up if we're out of mem */
  3041. list_for_each_entry_safe(page, tpage, &tmplist, lru) {
  3042. list_del(&page->lru);
  3043. lru_cache_add_file(page);
  3044. unlock_page(page);
  3045. page_cache_release(page);
  3046. }
  3047. rc = -ENOMEM;
  3048. add_credits_and_wake_if(server, credits, 0);
  3049. break;
  3050. }
  3051. rdata->cfile = cifsFileInfo_get(open_file);
  3052. rdata->mapping = mapping;
  3053. rdata->offset = offset;
  3054. rdata->bytes = bytes;
  3055. rdata->pid = pid;
  3056. rdata->pagesz = PAGE_CACHE_SIZE;
  3057. rdata->read_into_pages = cifs_readpages_read_into_pages;
  3058. rdata->credits = credits;
  3059. list_for_each_entry_safe(page, tpage, &tmplist, lru) {
  3060. list_del(&page->lru);
  3061. rdata->pages[rdata->nr_pages++] = page;
  3062. }
  3063. if (!rdata->cfile->invalidHandle ||
  3064. !cifs_reopen_file(rdata->cfile, true))
  3065. rc = server->ops->async_readv(rdata);
  3066. if (rc) {
  3067. add_credits_and_wake_if(server, rdata->credits, 0);
  3068. for (i = 0; i < rdata->nr_pages; i++) {
  3069. page = rdata->pages[i];
  3070. lru_cache_add_file(page);
  3071. unlock_page(page);
  3072. page_cache_release(page);
  3073. }
  3074. /* Fallback to the readpage in error/reconnect cases */
  3075. kref_put(&rdata->refcount, cifs_readdata_release);
  3076. break;
  3077. }
  3078. kref_put(&rdata->refcount, cifs_readdata_release);
  3079. }
  3080. /* Any pages that have been shown to fscache but didn't get added to
  3081. * the pagecache must be uncached before they get returned to the
  3082. * allocator.
  3083. */
  3084. cifs_fscache_readpages_cancel(mapping->host, page_list);
  3085. return rc;
  3086. }
  3087. /*
  3088. * cifs_readpage_worker must be called with the page pinned
  3089. */
  3090. static int cifs_readpage_worker(struct file *file, struct page *page,
  3091. loff_t *poffset)
  3092. {
  3093. char *read_data;
  3094. int rc;
  3095. /* Is the page cached? */
  3096. rc = cifs_readpage_from_fscache(file_inode(file), page);
  3097. if (rc == 0)
  3098. goto read_complete;
  3099. read_data = kmap(page);
  3100. /* for reads over a certain size could initiate async read ahead */
  3101. rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
  3102. if (rc < 0)
  3103. goto io_error;
  3104. else
  3105. cifs_dbg(FYI, "Bytes read %d\n", rc);
  3106. file_inode(file)->i_atime =
  3107. current_fs_time(file_inode(file)->i_sb);
  3108. if (PAGE_CACHE_SIZE > rc)
  3109. memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
  3110. flush_dcache_page(page);
  3111. SetPageUptodate(page);
  3112. /* send this page to the cache */
  3113. cifs_readpage_to_fscache(file_inode(file), page);
  3114. rc = 0;
  3115. io_error:
  3116. kunmap(page);
  3117. unlock_page(page);
  3118. read_complete:
  3119. return rc;
  3120. }
  3121. static int cifs_readpage(struct file *file, struct page *page)
  3122. {
  3123. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  3124. int rc = -EACCES;
  3125. unsigned int xid;
  3126. xid = get_xid();
  3127. if (file->private_data == NULL) {
  3128. rc = -EBADF;
  3129. free_xid(xid);
  3130. return rc;
  3131. }
  3132. cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
  3133. page, (int)offset, (int)offset);
  3134. rc = cifs_readpage_worker(file, page, &offset);
  3135. free_xid(xid);
  3136. return rc;
  3137. }
  3138. static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
  3139. {
  3140. struct cifsFileInfo *open_file;
  3141. spin_lock(&cifs_file_list_lock);
  3142. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  3143. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  3144. spin_unlock(&cifs_file_list_lock);
  3145. return 1;
  3146. }
  3147. }
  3148. spin_unlock(&cifs_file_list_lock);
  3149. return 0;
  3150. }
  3151. /* We do not want to update the file size from server for inodes
  3152. open for write - to avoid races with writepage extending
  3153. the file - in the future we could consider allowing
  3154. refreshing the inode only on increases in the file size
  3155. but this is tricky to do without racing with writebehind
  3156. page caching in the current Linux kernel design */
  3157. bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
  3158. {
  3159. if (!cifsInode)
  3160. return true;
  3161. if (is_inode_writable(cifsInode)) {
  3162. /* This inode is open for write at least once */
  3163. struct cifs_sb_info *cifs_sb;
  3164. cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
  3165. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  3166. /* since no page cache to corrupt on directio
  3167. we can change size safely */
  3168. return true;
  3169. }
  3170. if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
  3171. return true;
  3172. return false;
  3173. } else
  3174. return true;
  3175. }
  3176. static int cifs_write_begin(struct file *file, struct address_space *mapping,
  3177. loff_t pos, unsigned len, unsigned flags,
  3178. struct page **pagep, void **fsdata)
  3179. {
  3180. int oncethru = 0;
  3181. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  3182. loff_t offset = pos & (PAGE_CACHE_SIZE - 1);
  3183. loff_t page_start = pos & PAGE_MASK;
  3184. loff_t i_size;
  3185. struct page *page;
  3186. int rc = 0;
  3187. cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
  3188. start:
  3189. page = grab_cache_page_write_begin(mapping, index, flags);
  3190. if (!page) {
  3191. rc = -ENOMEM;
  3192. goto out;
  3193. }
  3194. if (PageUptodate(page))
  3195. goto out;
  3196. /*
  3197. * If we write a full page it will be up to date, no need to read from
  3198. * the server. If the write is short, we'll end up doing a sync write
  3199. * instead.
  3200. */
  3201. if (len == PAGE_CACHE_SIZE)
  3202. goto out;
  3203. /*
  3204. * optimize away the read when we have an oplock, and we're not
  3205. * expecting to use any of the data we'd be reading in. That
  3206. * is, when the page lies beyond the EOF, or straddles the EOF
  3207. * and the write will cover all of the existing data.
  3208. */
  3209. if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
  3210. i_size = i_size_read(mapping->host);
  3211. if (page_start >= i_size ||
  3212. (offset == 0 && (pos + len) >= i_size)) {
  3213. zero_user_segments(page, 0, offset,
  3214. offset + len,
  3215. PAGE_CACHE_SIZE);
  3216. /*
  3217. * PageChecked means that the parts of the page
  3218. * to which we're not writing are considered up
  3219. * to date. Once the data is copied to the
  3220. * page, it can be set uptodate.
  3221. */
  3222. SetPageChecked(page);
  3223. goto out;
  3224. }
  3225. }
  3226. if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
  3227. /*
  3228. * might as well read a page, it is fast enough. If we get
  3229. * an error, we don't need to return it. cifs_write_end will
  3230. * do a sync write instead since PG_uptodate isn't set.
  3231. */
  3232. cifs_readpage_worker(file, page, &page_start);
  3233. page_cache_release(page);
  3234. oncethru = 1;
  3235. goto start;
  3236. } else {
  3237. /* we could try using another file handle if there is one -
  3238. but how would we lock it to prevent close of that handle
  3239. racing with this read? In any case
  3240. this will be written out by write_end so is fine */
  3241. }
  3242. out:
  3243. *pagep = page;
  3244. return rc;
  3245. }
  3246. static int cifs_release_page(struct page *page, gfp_t gfp)
  3247. {
  3248. if (PagePrivate(page))
  3249. return 0;
  3250. return cifs_fscache_release_page(page, gfp);
  3251. }
  3252. static void cifs_invalidate_page(struct page *page, unsigned int offset,
  3253. unsigned int length)
  3254. {
  3255. struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
  3256. if (offset == 0 && length == PAGE_CACHE_SIZE)
  3257. cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
  3258. }
  3259. static int cifs_launder_page(struct page *page)
  3260. {
  3261. int rc = 0;
  3262. loff_t range_start = page_offset(page);
  3263. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  3264. struct writeback_control wbc = {
  3265. .sync_mode = WB_SYNC_ALL,
  3266. .nr_to_write = 0,
  3267. .range_start = range_start,
  3268. .range_end = range_end,
  3269. };
  3270. cifs_dbg(FYI, "Launder page: %p\n", page);
  3271. if (clear_page_dirty_for_io(page))
  3272. rc = cifs_writepage_locked(page, &wbc);
  3273. cifs_fscache_invalidate_page(page, page->mapping->host);
  3274. return rc;
  3275. }
  3276. void cifs_oplock_break(struct work_struct *work)
  3277. {
  3278. struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
  3279. oplock_break);
  3280. struct inode *inode = d_inode(cfile->dentry);
  3281. struct cifsInodeInfo *cinode = CIFS_I(inode);
  3282. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  3283. struct TCP_Server_Info *server = tcon->ses->server;
  3284. int rc = 0;
  3285. wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
  3286. TASK_UNINTERRUPTIBLE);
  3287. server->ops->downgrade_oplock(server, cinode,
  3288. test_bit(CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2, &cinode->flags));
  3289. if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
  3290. cifs_has_mand_locks(cinode)) {
  3291. cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
  3292. inode);
  3293. cinode->oplock = 0;
  3294. }
  3295. if (inode && S_ISREG(inode->i_mode)) {
  3296. if (CIFS_CACHE_READ(cinode))
  3297. break_lease(inode, O_RDONLY);
  3298. else
  3299. break_lease(inode, O_WRONLY);
  3300. rc = filemap_fdatawrite(inode->i_mapping);
  3301. if (!CIFS_CACHE_READ(cinode)) {
  3302. rc = filemap_fdatawait(inode->i_mapping);
  3303. mapping_set_error(inode->i_mapping, rc);
  3304. cifs_zap_mapping(inode);
  3305. }
  3306. cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
  3307. }
  3308. rc = cifs_push_locks(cfile);
  3309. if (rc)
  3310. cifs_dbg(VFS, "Push locks rc = %d\n", rc);
  3311. /*
  3312. * releasing stale oplock after recent reconnect of smb session using
  3313. * a now incorrect file handle is not a data integrity issue but do
  3314. * not bother sending an oplock release if session to server still is
  3315. * disconnected since oplock already released by the server
  3316. */
  3317. if (!cfile->oplock_break_cancelled) {
  3318. rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
  3319. cinode);
  3320. cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
  3321. }
  3322. cifs_done_oplock_break(cinode);
  3323. }
  3324. /*
  3325. * The presence of cifs_direct_io() in the address space ops vector
  3326. * allowes open() O_DIRECT flags which would have failed otherwise.
  3327. *
  3328. * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
  3329. * so this method should never be called.
  3330. *
  3331. * Direct IO is not yet supported in the cached mode.
  3332. */
  3333. static ssize_t
  3334. cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter, loff_t pos)
  3335. {
  3336. /*
  3337. * FIXME
  3338. * Eventually need to support direct IO for non forcedirectio mounts
  3339. */
  3340. return -EINVAL;
  3341. }
  3342. const struct address_space_operations cifs_addr_ops = {
  3343. .readpage = cifs_readpage,
  3344. .readpages = cifs_readpages,
  3345. .writepage = cifs_writepage,
  3346. .writepages = cifs_writepages,
  3347. .write_begin = cifs_write_begin,
  3348. .write_end = cifs_write_end,
  3349. .set_page_dirty = __set_page_dirty_nobuffers,
  3350. .releasepage = cifs_release_page,
  3351. .direct_IO = cifs_direct_io,
  3352. .invalidatepage = cifs_invalidate_page,
  3353. .launder_page = cifs_launder_page,
  3354. };
  3355. /*
  3356. * cifs_readpages requires the server to support a buffer large enough to
  3357. * contain the header plus one complete page of data. Otherwise, we need
  3358. * to leave cifs_readpages out of the address space operations.
  3359. */
  3360. const struct address_space_operations cifs_addr_ops_smallbuf = {
  3361. .readpage = cifs_readpage,
  3362. .writepage = cifs_writepage,
  3363. .writepages = cifs_writepages,
  3364. .write_begin = cifs_write_begin,
  3365. .write_end = cifs_write_end,
  3366. .set_page_dirty = __set_page_dirty_nobuffers,
  3367. .releasepage = cifs_release_page,
  3368. .invalidatepage = cifs_invalidate_page,
  3369. .launder_page = cifs_launder_page,
  3370. };