file.c 88 KB

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