file.c 93 KB

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