file.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400
  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 <asm/div64.h>
  36. #include "cifsfs.h"
  37. #include "cifspdu.h"
  38. #include "cifsglob.h"
  39. #include "cifsproto.h"
  40. #include "cifs_unicode.h"
  41. #include "cifs_debug.h"
  42. #include "cifs_fs_sb.h"
  43. #include "fscache.h"
  44. static inline int cifs_convert_flags(unsigned int flags)
  45. {
  46. if ((flags & O_ACCMODE) == O_RDONLY)
  47. return GENERIC_READ;
  48. else if ((flags & O_ACCMODE) == O_WRONLY)
  49. return GENERIC_WRITE;
  50. else if ((flags & O_ACCMODE) == O_RDWR) {
  51. /* GENERIC_ALL is too much permission to request
  52. can cause unnecessary access denied on create */
  53. /* return GENERIC_ALL; */
  54. return (GENERIC_READ | GENERIC_WRITE);
  55. }
  56. return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
  57. FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
  58. FILE_READ_DATA);
  59. }
  60. static inline fmode_t cifs_posix_convert_flags(unsigned int flags)
  61. {
  62. fmode_t posix_flags = 0;
  63. if ((flags & O_ACCMODE) == O_RDONLY)
  64. posix_flags = FMODE_READ;
  65. else if ((flags & O_ACCMODE) == O_WRONLY)
  66. posix_flags = FMODE_WRITE;
  67. else if ((flags & O_ACCMODE) == O_RDWR) {
  68. /* GENERIC_ALL is too much permission to request
  69. can cause unnecessary access denied on create */
  70. /* return GENERIC_ALL; */
  71. posix_flags = FMODE_READ | FMODE_WRITE;
  72. }
  73. /* can not map O_CREAT or O_EXCL or O_TRUNC flags when
  74. reopening a file. They had their effect on the original open */
  75. if (flags & O_APPEND)
  76. posix_flags |= (fmode_t)O_APPEND;
  77. if (flags & O_DSYNC)
  78. posix_flags |= (fmode_t)O_DSYNC;
  79. if (flags & __O_SYNC)
  80. posix_flags |= (fmode_t)__O_SYNC;
  81. if (flags & O_DIRECTORY)
  82. posix_flags |= (fmode_t)O_DIRECTORY;
  83. if (flags & O_NOFOLLOW)
  84. posix_flags |= (fmode_t)O_NOFOLLOW;
  85. if (flags & O_DIRECT)
  86. posix_flags |= (fmode_t)O_DIRECT;
  87. return posix_flags;
  88. }
  89. static inline int cifs_get_disposition(unsigned int flags)
  90. {
  91. if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  92. return FILE_CREATE;
  93. else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  94. return FILE_OVERWRITE_IF;
  95. else if ((flags & O_CREAT) == O_CREAT)
  96. return FILE_OPEN_IF;
  97. else if ((flags & O_TRUNC) == O_TRUNC)
  98. return FILE_OVERWRITE;
  99. else
  100. return FILE_OPEN;
  101. }
  102. /* all arguments to this function must be checked for validity in caller */
  103. static inline int
  104. cifs_posix_open_inode_helper(struct inode *inode, struct file *file,
  105. struct cifsInodeInfo *pCifsInode, __u32 oplock,
  106. u16 netfid)
  107. {
  108. write_lock(&GlobalSMBSeslock);
  109. pCifsInode = CIFS_I(file->f_path.dentry->d_inode);
  110. if (pCifsInode == NULL) {
  111. write_unlock(&GlobalSMBSeslock);
  112. return -EINVAL;
  113. }
  114. if (pCifsInode->clientCanCacheRead) {
  115. /* we have the inode open somewhere else
  116. no need to discard cache data */
  117. goto psx_client_can_cache;
  118. }
  119. /* BB FIXME need to fix this check to move it earlier into posix_open
  120. BB fIX following section BB FIXME */
  121. /* if not oplocked, invalidate inode pages if mtime or file
  122. size changed */
  123. /* temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime));
  124. if (timespec_equal(&file->f_path.dentry->d_inode->i_mtime, &temp) &&
  125. (file->f_path.dentry->d_inode->i_size ==
  126. (loff_t)le64_to_cpu(buf->EndOfFile))) {
  127. cFYI(1, "inode unchanged on server");
  128. } else {
  129. if (file->f_path.dentry->d_inode->i_mapping) {
  130. rc = filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping);
  131. if (rc != 0)
  132. CIFS_I(file->f_path.dentry->d_inode)->write_behind_rc = rc;
  133. }
  134. cFYI(1, "invalidating remote inode since open detected it "
  135. "changed");
  136. invalidate_remote_inode(file->f_path.dentry->d_inode);
  137. } */
  138. psx_client_can_cache:
  139. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  140. pCifsInode->clientCanCacheAll = true;
  141. pCifsInode->clientCanCacheRead = true;
  142. cFYI(1, "Exclusive Oplock granted on inode %p",
  143. file->f_path.dentry->d_inode);
  144. } else if ((oplock & 0xF) == OPLOCK_READ)
  145. pCifsInode->clientCanCacheRead = true;
  146. /* will have to change the unlock if we reenable the
  147. filemap_fdatawrite (which does not seem necessary */
  148. write_unlock(&GlobalSMBSeslock);
  149. return 0;
  150. }
  151. /* all arguments to this function must be checked for validity in caller */
  152. static inline int cifs_open_inode_helper(struct inode *inode,
  153. struct cifsTconInfo *pTcon, int *oplock, FILE_ALL_INFO *buf,
  154. char *full_path, int xid)
  155. {
  156. struct cifsInodeInfo *pCifsInode = CIFS_I(inode);
  157. struct timespec temp;
  158. int rc;
  159. if (pCifsInode->clientCanCacheRead) {
  160. /* we have the inode open somewhere else
  161. no need to discard cache data */
  162. goto client_can_cache;
  163. }
  164. /* BB need same check in cifs_create too? */
  165. /* if not oplocked, invalidate inode pages if mtime or file
  166. size changed */
  167. temp = cifs_NTtimeToUnix(buf->LastWriteTime);
  168. if (timespec_equal(&inode->i_mtime, &temp) &&
  169. (inode->i_size ==
  170. (loff_t)le64_to_cpu(buf->EndOfFile))) {
  171. cFYI(1, "inode unchanged on server");
  172. } else {
  173. if (inode->i_mapping) {
  174. /* BB no need to lock inode until after invalidate
  175. since namei code should already have it locked? */
  176. rc = filemap_write_and_wait(inode->i_mapping);
  177. if (rc != 0)
  178. pCifsInode->write_behind_rc = rc;
  179. }
  180. cFYI(1, "invalidating remote inode since open detected it "
  181. "changed");
  182. invalidate_remote_inode(inode);
  183. }
  184. client_can_cache:
  185. if (pTcon->unix_ext)
  186. rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
  187. xid);
  188. else
  189. rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
  190. xid, NULL);
  191. if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  192. pCifsInode->clientCanCacheAll = true;
  193. pCifsInode->clientCanCacheRead = true;
  194. cFYI(1, "Exclusive Oplock granted on inode %p", inode);
  195. } else if ((*oplock & 0xF) == OPLOCK_READ)
  196. pCifsInode->clientCanCacheRead = true;
  197. return rc;
  198. }
  199. int cifs_open(struct inode *inode, struct file *file)
  200. {
  201. int rc = -EACCES;
  202. int xid;
  203. __u32 oplock;
  204. struct cifs_sb_info *cifs_sb;
  205. struct cifsTconInfo *tcon;
  206. struct cifsFileInfo *pCifsFile = NULL;
  207. struct cifsInodeInfo *pCifsInode;
  208. char *full_path = NULL;
  209. int desiredAccess;
  210. int disposition;
  211. __u16 netfid;
  212. FILE_ALL_INFO *buf = NULL;
  213. xid = GetXid();
  214. cifs_sb = CIFS_SB(inode->i_sb);
  215. tcon = cifs_sb->tcon;
  216. pCifsInode = CIFS_I(file->f_path.dentry->d_inode);
  217. full_path = build_path_from_dentry(file->f_path.dentry);
  218. if (full_path == NULL) {
  219. rc = -ENOMEM;
  220. goto out;
  221. }
  222. cFYI(1, "inode = 0x%p file flags are 0x%x for %s",
  223. inode, file->f_flags, full_path);
  224. if (oplockEnabled)
  225. oplock = REQ_OPLOCK;
  226. else
  227. oplock = 0;
  228. if (!tcon->broken_posix_open && tcon->unix_ext &&
  229. (tcon->ses->capabilities & CAP_UNIX) &&
  230. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  231. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  232. int oflags = (int) cifs_posix_convert_flags(file->f_flags);
  233. oflags |= SMB_O_CREAT;
  234. /* can not refresh inode info since size could be stale */
  235. rc = cifs_posix_open(full_path, &inode, inode->i_sb,
  236. cifs_sb->mnt_file_mode /* ignored */,
  237. oflags, &oplock, &netfid, xid);
  238. if (rc == 0) {
  239. cFYI(1, "posix open succeeded");
  240. /* no need for special case handling of setting mode
  241. on read only files needed here */
  242. rc = cifs_posix_open_inode_helper(inode, file,
  243. pCifsInode, oplock, netfid);
  244. if (rc != 0) {
  245. CIFSSMBClose(xid, tcon, netfid);
  246. goto out;
  247. }
  248. pCifsFile = cifs_new_fileinfo(inode, netfid, file,
  249. file->f_path.mnt,
  250. oflags);
  251. if (pCifsFile == NULL) {
  252. CIFSSMBClose(xid, tcon, netfid);
  253. rc = -ENOMEM;
  254. }
  255. cifs_fscache_set_inode_cookie(inode, file);
  256. goto out;
  257. } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  258. if (tcon->ses->serverNOS)
  259. cERROR(1, "server %s of type %s returned"
  260. " unexpected error on SMB posix open"
  261. ", disabling posix open support."
  262. " Check if server update available.",
  263. tcon->ses->serverName,
  264. tcon->ses->serverNOS);
  265. tcon->broken_posix_open = true;
  266. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  267. (rc != -EOPNOTSUPP)) /* path not found or net err */
  268. goto out;
  269. /* else fallthrough to retry open the old way on network i/o
  270. or DFS errors */
  271. }
  272. desiredAccess = cifs_convert_flags(file->f_flags);
  273. /*********************************************************************
  274. * open flag mapping table:
  275. *
  276. * POSIX Flag CIFS Disposition
  277. * ---------- ----------------
  278. * O_CREAT FILE_OPEN_IF
  279. * O_CREAT | O_EXCL FILE_CREATE
  280. * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
  281. * O_TRUNC FILE_OVERWRITE
  282. * none of the above FILE_OPEN
  283. *
  284. * Note that there is not a direct match between disposition
  285. * FILE_SUPERSEDE (ie create whether or not file exists although
  286. * O_CREAT | O_TRUNC is similar but truncates the existing
  287. * file rather than creating a new file as FILE_SUPERSEDE does
  288. * (which uses the attributes / metadata passed in on open call)
  289. *?
  290. *? O_SYNC is a reasonable match to CIFS writethrough flag
  291. *? and the read write flags match reasonably. O_LARGEFILE
  292. *? is irrelevant because largefile support is always used
  293. *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
  294. * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
  295. *********************************************************************/
  296. disposition = cifs_get_disposition(file->f_flags);
  297. /* BB pass O_SYNC flag through on file attributes .. BB */
  298. /* Also refresh inode by passing in file_info buf returned by SMBOpen
  299. and calling get_inode_info with returned buf (at least helps
  300. non-Unix server case) */
  301. /* BB we can not do this if this is the second open of a file
  302. and the first handle has writebehind data, we might be
  303. able to simply do a filemap_fdatawrite/filemap_fdatawait first */
  304. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  305. if (!buf) {
  306. rc = -ENOMEM;
  307. goto out;
  308. }
  309. if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
  310. rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
  311. desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf,
  312. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
  313. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  314. else
  315. rc = -EIO; /* no NT SMB support fall into legacy open below */
  316. if (rc == -EIO) {
  317. /* Old server, try legacy style OpenX */
  318. rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
  319. desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf,
  320. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
  321. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  322. }
  323. if (rc) {
  324. cFYI(1, "cifs_open returned 0x%x", rc);
  325. goto out;
  326. }
  327. rc = cifs_open_inode_helper(inode, tcon, &oplock, buf, full_path, xid);
  328. if (rc != 0)
  329. goto out;
  330. pCifsFile = cifs_new_fileinfo(inode, netfid, file, file->f_path.mnt,
  331. file->f_flags);
  332. if (pCifsFile == NULL) {
  333. rc = -ENOMEM;
  334. goto out;
  335. }
  336. cifs_fscache_set_inode_cookie(inode, file);
  337. if (oplock & CIFS_CREATE_ACTION) {
  338. /* time to set mode which we can not set earlier due to
  339. problems creating new read-only files */
  340. if (tcon->unix_ext) {
  341. struct cifs_unix_set_info_args args = {
  342. .mode = inode->i_mode,
  343. .uid = NO_CHANGE_64,
  344. .gid = NO_CHANGE_64,
  345. .ctime = NO_CHANGE_64,
  346. .atime = NO_CHANGE_64,
  347. .mtime = NO_CHANGE_64,
  348. .device = 0,
  349. };
  350. CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  351. cifs_sb->local_nls,
  352. cifs_sb->mnt_cifs_flags &
  353. CIFS_MOUNT_MAP_SPECIAL_CHR);
  354. }
  355. }
  356. out:
  357. kfree(buf);
  358. kfree(full_path);
  359. FreeXid(xid);
  360. return rc;
  361. }
  362. /* Try to reacquire byte range locks that were released when session */
  363. /* to server was lost */
  364. static int cifs_relock_file(struct cifsFileInfo *cifsFile)
  365. {
  366. int rc = 0;
  367. /* BB list all locks open on this file and relock */
  368. return rc;
  369. }
  370. static int cifs_reopen_file(struct file *file, bool can_flush)
  371. {
  372. int rc = -EACCES;
  373. int xid;
  374. __u32 oplock;
  375. struct cifs_sb_info *cifs_sb;
  376. struct cifsTconInfo *tcon;
  377. struct cifsFileInfo *pCifsFile;
  378. struct cifsInodeInfo *pCifsInode;
  379. struct inode *inode;
  380. char *full_path = NULL;
  381. int desiredAccess;
  382. int disposition = FILE_OPEN;
  383. __u16 netfid;
  384. if (file->private_data)
  385. pCifsFile = file->private_data;
  386. else
  387. return -EBADF;
  388. xid = GetXid();
  389. mutex_lock(&pCifsFile->fh_mutex);
  390. if (!pCifsFile->invalidHandle) {
  391. mutex_unlock(&pCifsFile->fh_mutex);
  392. rc = 0;
  393. FreeXid(xid);
  394. return rc;
  395. }
  396. if (file->f_path.dentry == NULL) {
  397. cERROR(1, "no valid name if dentry freed");
  398. dump_stack();
  399. rc = -EBADF;
  400. goto reopen_error_exit;
  401. }
  402. inode = file->f_path.dentry->d_inode;
  403. if (inode == NULL) {
  404. cERROR(1, "inode not valid");
  405. dump_stack();
  406. rc = -EBADF;
  407. goto reopen_error_exit;
  408. }
  409. cifs_sb = CIFS_SB(inode->i_sb);
  410. tcon = cifs_sb->tcon;
  411. /* can not grab rename sem here because various ops, including
  412. those that already have the rename sem can end up causing writepage
  413. to get called and if the server was down that means we end up here,
  414. and we can never tell if the caller already has the rename_sem */
  415. full_path = build_path_from_dentry(file->f_path.dentry);
  416. if (full_path == NULL) {
  417. rc = -ENOMEM;
  418. reopen_error_exit:
  419. mutex_unlock(&pCifsFile->fh_mutex);
  420. FreeXid(xid);
  421. return rc;
  422. }
  423. cFYI(1, "inode = 0x%p file flags 0x%x for %s",
  424. inode, file->f_flags, full_path);
  425. if (oplockEnabled)
  426. oplock = REQ_OPLOCK;
  427. else
  428. oplock = 0;
  429. if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
  430. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  431. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  432. int oflags = (int) cifs_posix_convert_flags(file->f_flags);
  433. /* can not refresh inode info since size could be stale */
  434. rc = cifs_posix_open(full_path, NULL, inode->i_sb,
  435. cifs_sb->mnt_file_mode /* ignored */,
  436. oflags, &oplock, &netfid, xid);
  437. if (rc == 0) {
  438. cFYI(1, "posix reopen succeeded");
  439. goto reopen_success;
  440. }
  441. /* fallthrough to retry open the old way on errors, especially
  442. in the reconnect path it is important to retry hard */
  443. }
  444. desiredAccess = cifs_convert_flags(file->f_flags);
  445. /* Can not refresh inode by passing in file_info buf to be returned
  446. by SMBOpen and then calling get_inode_info with returned buf
  447. since file might have write behind data that needs to be flushed
  448. and server version of file size can be stale. If we knew for sure
  449. that inode was not dirty locally we could do this */
  450. rc = CIFSSMBOpen(xid, tcon, full_path, disposition, desiredAccess,
  451. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  452. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  453. CIFS_MOUNT_MAP_SPECIAL_CHR);
  454. if (rc) {
  455. mutex_unlock(&pCifsFile->fh_mutex);
  456. cFYI(1, "cifs_open returned 0x%x", rc);
  457. cFYI(1, "oplock: %d", oplock);
  458. } else {
  459. reopen_success:
  460. pCifsFile->netfid = netfid;
  461. pCifsFile->invalidHandle = false;
  462. mutex_unlock(&pCifsFile->fh_mutex);
  463. pCifsInode = CIFS_I(inode);
  464. if (pCifsInode) {
  465. if (can_flush) {
  466. rc = filemap_write_and_wait(inode->i_mapping);
  467. if (rc != 0)
  468. CIFS_I(inode)->write_behind_rc = rc;
  469. /* temporarily disable caching while we
  470. go to server to get inode info */
  471. pCifsInode->clientCanCacheAll = false;
  472. pCifsInode->clientCanCacheRead = false;
  473. if (tcon->unix_ext)
  474. rc = cifs_get_inode_info_unix(&inode,
  475. full_path, inode->i_sb, xid);
  476. else
  477. rc = cifs_get_inode_info(&inode,
  478. full_path, NULL, inode->i_sb,
  479. xid, NULL);
  480. } /* else we are writing out data to server already
  481. and could deadlock if we tried to flush data, and
  482. since we do not know if we have data that would
  483. invalidate the current end of file on the server
  484. we can not go to the server to get the new inod
  485. info */
  486. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  487. pCifsInode->clientCanCacheAll = true;
  488. pCifsInode->clientCanCacheRead = true;
  489. cFYI(1, "Exclusive Oplock granted on inode %p",
  490. file->f_path.dentry->d_inode);
  491. } else if ((oplock & 0xF) == OPLOCK_READ) {
  492. pCifsInode->clientCanCacheRead = true;
  493. pCifsInode->clientCanCacheAll = false;
  494. } else {
  495. pCifsInode->clientCanCacheRead = false;
  496. pCifsInode->clientCanCacheAll = false;
  497. }
  498. cifs_relock_file(pCifsFile);
  499. }
  500. }
  501. kfree(full_path);
  502. FreeXid(xid);
  503. return rc;
  504. }
  505. int cifs_close(struct inode *inode, struct file *file)
  506. {
  507. int rc = 0;
  508. int xid, timeout;
  509. struct cifs_sb_info *cifs_sb;
  510. struct cifsTconInfo *pTcon;
  511. struct cifsFileInfo *pSMBFile = file->private_data;
  512. xid = GetXid();
  513. cifs_sb = CIFS_SB(inode->i_sb);
  514. pTcon = cifs_sb->tcon;
  515. if (pSMBFile) {
  516. struct cifsLockInfo *li, *tmp;
  517. write_lock(&GlobalSMBSeslock);
  518. pSMBFile->closePend = true;
  519. if (pTcon) {
  520. /* no sense reconnecting to close a file that is
  521. already closed */
  522. if (!pTcon->need_reconnect) {
  523. write_unlock(&GlobalSMBSeslock);
  524. timeout = 2;
  525. while ((atomic_read(&pSMBFile->count) != 1)
  526. && (timeout <= 2048)) {
  527. /* Give write a better chance to get to
  528. server ahead of the close. We do not
  529. want to add a wait_q here as it would
  530. increase the memory utilization as
  531. the struct would be in each open file,
  532. but this should give enough time to
  533. clear the socket */
  534. cFYI(DBG2, "close delay, write pending");
  535. msleep(timeout);
  536. timeout *= 4;
  537. }
  538. if (!pTcon->need_reconnect &&
  539. !pSMBFile->invalidHandle)
  540. rc = CIFSSMBClose(xid, pTcon,
  541. pSMBFile->netfid);
  542. } else
  543. write_unlock(&GlobalSMBSeslock);
  544. } else
  545. write_unlock(&GlobalSMBSeslock);
  546. /* Delete any outstanding lock records.
  547. We'll lose them when the file is closed anyway. */
  548. mutex_lock(&pSMBFile->lock_mutex);
  549. list_for_each_entry_safe(li, tmp, &pSMBFile->llist, llist) {
  550. list_del(&li->llist);
  551. kfree(li);
  552. }
  553. mutex_unlock(&pSMBFile->lock_mutex);
  554. write_lock(&GlobalSMBSeslock);
  555. list_del(&pSMBFile->flist);
  556. list_del(&pSMBFile->tlist);
  557. write_unlock(&GlobalSMBSeslock);
  558. cifsFileInfo_put(file->private_data);
  559. file->private_data = NULL;
  560. } else
  561. rc = -EBADF;
  562. read_lock(&GlobalSMBSeslock);
  563. if (list_empty(&(CIFS_I(inode)->openFileList))) {
  564. cFYI(1, "closing last open instance for inode %p", inode);
  565. /* if the file is not open we do not know if we can cache info
  566. on this inode, much less write behind and read ahead */
  567. CIFS_I(inode)->clientCanCacheRead = false;
  568. CIFS_I(inode)->clientCanCacheAll = false;
  569. }
  570. read_unlock(&GlobalSMBSeslock);
  571. if ((rc == 0) && CIFS_I(inode)->write_behind_rc)
  572. rc = CIFS_I(inode)->write_behind_rc;
  573. FreeXid(xid);
  574. return rc;
  575. }
  576. int cifs_closedir(struct inode *inode, struct file *file)
  577. {
  578. int rc = 0;
  579. int xid;
  580. struct cifsFileInfo *pCFileStruct = file->private_data;
  581. char *ptmp;
  582. cFYI(1, "Closedir inode = 0x%p", inode);
  583. xid = GetXid();
  584. if (pCFileStruct) {
  585. struct cifsTconInfo *pTcon;
  586. struct cifs_sb_info *cifs_sb =
  587. CIFS_SB(file->f_path.dentry->d_sb);
  588. pTcon = cifs_sb->tcon;
  589. cFYI(1, "Freeing private data in close dir");
  590. write_lock(&GlobalSMBSeslock);
  591. if (!pCFileStruct->srch_inf.endOfSearch &&
  592. !pCFileStruct->invalidHandle) {
  593. pCFileStruct->invalidHandle = true;
  594. write_unlock(&GlobalSMBSeslock);
  595. rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid);
  596. cFYI(1, "Closing uncompleted readdir with rc %d",
  597. rc);
  598. /* not much we can do if it fails anyway, ignore rc */
  599. rc = 0;
  600. } else
  601. write_unlock(&GlobalSMBSeslock);
  602. ptmp = pCFileStruct->srch_inf.ntwrk_buf_start;
  603. if (ptmp) {
  604. cFYI(1, "closedir free smb buf in srch struct");
  605. pCFileStruct->srch_inf.ntwrk_buf_start = NULL;
  606. if (pCFileStruct->srch_inf.smallBuf)
  607. cifs_small_buf_release(ptmp);
  608. else
  609. cifs_buf_release(ptmp);
  610. }
  611. kfree(file->private_data);
  612. file->private_data = NULL;
  613. }
  614. /* BB can we lock the filestruct while this is going on? */
  615. FreeXid(xid);
  616. return rc;
  617. }
  618. static int store_file_lock(struct cifsFileInfo *fid, __u64 len,
  619. __u64 offset, __u8 lockType)
  620. {
  621. struct cifsLockInfo *li =
  622. kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
  623. if (li == NULL)
  624. return -ENOMEM;
  625. li->offset = offset;
  626. li->length = len;
  627. li->type = lockType;
  628. mutex_lock(&fid->lock_mutex);
  629. list_add(&li->llist, &fid->llist);
  630. mutex_unlock(&fid->lock_mutex);
  631. return 0;
  632. }
  633. int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock)
  634. {
  635. int rc, xid;
  636. __u32 numLock = 0;
  637. __u32 numUnlock = 0;
  638. __u64 length;
  639. bool wait_flag = false;
  640. struct cifs_sb_info *cifs_sb;
  641. struct cifsTconInfo *tcon;
  642. __u16 netfid;
  643. __u8 lockType = LOCKING_ANDX_LARGE_FILES;
  644. bool posix_locking = 0;
  645. length = 1 + pfLock->fl_end - pfLock->fl_start;
  646. rc = -EACCES;
  647. xid = GetXid();
  648. cFYI(1, "Lock parm: 0x%x flockflags: "
  649. "0x%x flocktype: 0x%x start: %lld end: %lld",
  650. cmd, pfLock->fl_flags, pfLock->fl_type, pfLock->fl_start,
  651. pfLock->fl_end);
  652. if (pfLock->fl_flags & FL_POSIX)
  653. cFYI(1, "Posix");
  654. if (pfLock->fl_flags & FL_FLOCK)
  655. cFYI(1, "Flock");
  656. if (pfLock->fl_flags & FL_SLEEP) {
  657. cFYI(1, "Blocking lock");
  658. wait_flag = true;
  659. }
  660. if (pfLock->fl_flags & FL_ACCESS)
  661. cFYI(1, "Process suspended by mandatory locking - "
  662. "not implemented yet");
  663. if (pfLock->fl_flags & FL_LEASE)
  664. cFYI(1, "Lease on file - not implemented yet");
  665. if (pfLock->fl_flags &
  666. (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE)))
  667. cFYI(1, "Unknown lock flags 0x%x", pfLock->fl_flags);
  668. if (pfLock->fl_type == F_WRLCK) {
  669. cFYI(1, "F_WRLCK ");
  670. numLock = 1;
  671. } else if (pfLock->fl_type == F_UNLCK) {
  672. cFYI(1, "F_UNLCK");
  673. numUnlock = 1;
  674. /* Check if unlock includes more than
  675. one lock range */
  676. } else if (pfLock->fl_type == F_RDLCK) {
  677. cFYI(1, "F_RDLCK");
  678. lockType |= LOCKING_ANDX_SHARED_LOCK;
  679. numLock = 1;
  680. } else if (pfLock->fl_type == F_EXLCK) {
  681. cFYI(1, "F_EXLCK");
  682. numLock = 1;
  683. } else if (pfLock->fl_type == F_SHLCK) {
  684. cFYI(1, "F_SHLCK");
  685. lockType |= LOCKING_ANDX_SHARED_LOCK;
  686. numLock = 1;
  687. } else
  688. cFYI(1, "Unknown type of lock");
  689. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  690. tcon = cifs_sb->tcon;
  691. if (file->private_data == NULL) {
  692. rc = -EBADF;
  693. FreeXid(xid);
  694. return rc;
  695. }
  696. netfid = ((struct cifsFileInfo *)file->private_data)->netfid;
  697. if ((tcon->ses->capabilities & CAP_UNIX) &&
  698. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  699. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  700. posix_locking = 1;
  701. /* BB add code here to normalize offset and length to
  702. account for negative length which we can not accept over the
  703. wire */
  704. if (IS_GETLK(cmd)) {
  705. if (posix_locking) {
  706. int posix_lock_type;
  707. if (lockType & LOCKING_ANDX_SHARED_LOCK)
  708. posix_lock_type = CIFS_RDLCK;
  709. else
  710. posix_lock_type = CIFS_WRLCK;
  711. rc = CIFSSMBPosixLock(xid, tcon, netfid, 1 /* get */,
  712. length, pfLock,
  713. posix_lock_type, wait_flag);
  714. FreeXid(xid);
  715. return rc;
  716. }
  717. /* BB we could chain these into one lock request BB */
  718. rc = CIFSSMBLock(xid, tcon, netfid, length, pfLock->fl_start,
  719. 0, 1, lockType, 0 /* wait flag */ );
  720. if (rc == 0) {
  721. rc = CIFSSMBLock(xid, tcon, netfid, length,
  722. pfLock->fl_start, 1 /* numUnlock */ ,
  723. 0 /* numLock */ , lockType,
  724. 0 /* wait flag */ );
  725. pfLock->fl_type = F_UNLCK;
  726. if (rc != 0)
  727. cERROR(1, "Error unlocking previously locked "
  728. "range %d during test of lock", rc);
  729. rc = 0;
  730. } else {
  731. /* if rc == ERR_SHARING_VIOLATION ? */
  732. rc = 0;
  733. if (lockType & LOCKING_ANDX_SHARED_LOCK) {
  734. pfLock->fl_type = F_WRLCK;
  735. } else {
  736. rc = CIFSSMBLock(xid, tcon, netfid, length,
  737. pfLock->fl_start, 0, 1,
  738. lockType | LOCKING_ANDX_SHARED_LOCK,
  739. 0 /* wait flag */);
  740. if (rc == 0) {
  741. rc = CIFSSMBLock(xid, tcon, netfid,
  742. length, pfLock->fl_start, 1, 0,
  743. lockType |
  744. LOCKING_ANDX_SHARED_LOCK,
  745. 0 /* wait flag */);
  746. pfLock->fl_type = F_RDLCK;
  747. if (rc != 0)
  748. cERROR(1, "Error unlocking "
  749. "previously locked range %d "
  750. "during test of lock", rc);
  751. rc = 0;
  752. } else {
  753. pfLock->fl_type = F_WRLCK;
  754. rc = 0;
  755. }
  756. }
  757. }
  758. FreeXid(xid);
  759. return rc;
  760. }
  761. if (!numLock && !numUnlock) {
  762. /* if no lock or unlock then nothing
  763. to do since we do not know what it is */
  764. FreeXid(xid);
  765. return -EOPNOTSUPP;
  766. }
  767. if (posix_locking) {
  768. int posix_lock_type;
  769. if (lockType & LOCKING_ANDX_SHARED_LOCK)
  770. posix_lock_type = CIFS_RDLCK;
  771. else
  772. posix_lock_type = CIFS_WRLCK;
  773. if (numUnlock == 1)
  774. posix_lock_type = CIFS_UNLCK;
  775. rc = CIFSSMBPosixLock(xid, tcon, netfid, 0 /* set */,
  776. length, pfLock,
  777. posix_lock_type, wait_flag);
  778. } else {
  779. struct cifsFileInfo *fid = file->private_data;
  780. if (numLock) {
  781. rc = CIFSSMBLock(xid, tcon, netfid, length,
  782. pfLock->fl_start,
  783. 0, numLock, lockType, wait_flag);
  784. if (rc == 0) {
  785. /* For Windows locks we must store them. */
  786. rc = store_file_lock(fid, length,
  787. pfLock->fl_start, lockType);
  788. }
  789. } else if (numUnlock) {
  790. /* For each stored lock that this unlock overlaps
  791. completely, unlock it. */
  792. int stored_rc = 0;
  793. struct cifsLockInfo *li, *tmp;
  794. rc = 0;
  795. mutex_lock(&fid->lock_mutex);
  796. list_for_each_entry_safe(li, tmp, &fid->llist, llist) {
  797. if (pfLock->fl_start <= li->offset &&
  798. (pfLock->fl_start + length) >=
  799. (li->offset + li->length)) {
  800. stored_rc = CIFSSMBLock(xid, tcon,
  801. netfid,
  802. li->length, li->offset,
  803. 1, 0, li->type, false);
  804. if (stored_rc)
  805. rc = stored_rc;
  806. else {
  807. list_del(&li->llist);
  808. kfree(li);
  809. }
  810. }
  811. }
  812. mutex_unlock(&fid->lock_mutex);
  813. }
  814. }
  815. if (pfLock->fl_flags & FL_POSIX)
  816. posix_lock_file_wait(file, pfLock);
  817. FreeXid(xid);
  818. return rc;
  819. }
  820. /*
  821. * Set the timeout on write requests past EOF. For some servers (Windows)
  822. * these calls can be very long.
  823. *
  824. * If we're writing >10M past the EOF we give a 180s timeout. Anything less
  825. * than that gets a 45s timeout. Writes not past EOF get 15s timeouts.
  826. * The 10M cutoff is totally arbitrary. A better scheme for this would be
  827. * welcome if someone wants to suggest one.
  828. *
  829. * We may be able to do a better job with this if there were some way to
  830. * declare that a file should be sparse.
  831. */
  832. static int
  833. cifs_write_timeout(struct cifsInodeInfo *cifsi, loff_t offset)
  834. {
  835. if (offset <= cifsi->server_eof)
  836. return CIFS_STD_OP;
  837. else if (offset > (cifsi->server_eof + (10 * 1024 * 1024)))
  838. return CIFS_VLONG_OP;
  839. else
  840. return CIFS_LONG_OP;
  841. }
  842. /* update the file size (if needed) after a write */
  843. static void
  844. cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
  845. unsigned int bytes_written)
  846. {
  847. loff_t end_of_write = offset + bytes_written;
  848. if (end_of_write > cifsi->server_eof)
  849. cifsi->server_eof = end_of_write;
  850. }
  851. ssize_t cifs_user_write(struct file *file, const char __user *write_data,
  852. size_t write_size, loff_t *poffset)
  853. {
  854. int rc = 0;
  855. unsigned int bytes_written = 0;
  856. unsigned int total_written;
  857. struct cifs_sb_info *cifs_sb;
  858. struct cifsTconInfo *pTcon;
  859. int xid, long_op;
  860. struct cifsFileInfo *open_file;
  861. struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode);
  862. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  863. pTcon = cifs_sb->tcon;
  864. /* cFYI(1, " write %d bytes to offset %lld of %s", write_size,
  865. *poffset, file->f_path.dentry->d_name.name); */
  866. if (file->private_data == NULL)
  867. return -EBADF;
  868. open_file = file->private_data;
  869. rc = generic_write_checks(file, poffset, &write_size, 0);
  870. if (rc)
  871. return rc;
  872. xid = GetXid();
  873. long_op = cifs_write_timeout(cifsi, *poffset);
  874. for (total_written = 0; write_size > total_written;
  875. total_written += bytes_written) {
  876. rc = -EAGAIN;
  877. while (rc == -EAGAIN) {
  878. if (file->private_data == NULL) {
  879. /* file has been closed on us */
  880. FreeXid(xid);
  881. /* if we have gotten here we have written some data
  882. and blocked, and the file has been freed on us while
  883. we blocked so return what we managed to write */
  884. return total_written;
  885. }
  886. if (open_file->closePend) {
  887. FreeXid(xid);
  888. if (total_written)
  889. return total_written;
  890. else
  891. return -EBADF;
  892. }
  893. if (open_file->invalidHandle) {
  894. /* we could deadlock if we called
  895. filemap_fdatawait from here so tell
  896. reopen_file not to flush data to server
  897. now */
  898. rc = cifs_reopen_file(file, false);
  899. if (rc != 0)
  900. break;
  901. }
  902. rc = CIFSSMBWrite(xid, pTcon,
  903. open_file->netfid,
  904. min_t(const int, cifs_sb->wsize,
  905. write_size - total_written),
  906. *poffset, &bytes_written,
  907. NULL, write_data + total_written, long_op);
  908. }
  909. if (rc || (bytes_written == 0)) {
  910. if (total_written)
  911. break;
  912. else {
  913. FreeXid(xid);
  914. return rc;
  915. }
  916. } else {
  917. cifs_update_eof(cifsi, *poffset, bytes_written);
  918. *poffset += bytes_written;
  919. }
  920. long_op = CIFS_STD_OP; /* subsequent writes fast -
  921. 15 seconds is plenty */
  922. }
  923. cifs_stats_bytes_written(pTcon, total_written);
  924. /* since the write may have blocked check these pointers again */
  925. if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) {
  926. struct inode *inode = file->f_path.dentry->d_inode;
  927. /* Do not update local mtime - server will set its actual value on write
  928. * inode->i_ctime = inode->i_mtime =
  929. * current_fs_time(inode->i_sb);*/
  930. if (total_written > 0) {
  931. spin_lock(&inode->i_lock);
  932. if (*poffset > file->f_path.dentry->d_inode->i_size)
  933. i_size_write(file->f_path.dentry->d_inode,
  934. *poffset);
  935. spin_unlock(&inode->i_lock);
  936. }
  937. mark_inode_dirty_sync(file->f_path.dentry->d_inode);
  938. }
  939. FreeXid(xid);
  940. return total_written;
  941. }
  942. static ssize_t cifs_write(struct file *file, const char *write_data,
  943. size_t write_size, loff_t *poffset)
  944. {
  945. int rc = 0;
  946. unsigned int bytes_written = 0;
  947. unsigned int total_written;
  948. struct cifs_sb_info *cifs_sb;
  949. struct cifsTconInfo *pTcon;
  950. int xid, long_op;
  951. struct cifsFileInfo *open_file;
  952. struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode);
  953. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  954. pTcon = cifs_sb->tcon;
  955. cFYI(1, "write %zd bytes to offset %lld of %s", write_size,
  956. *poffset, file->f_path.dentry->d_name.name);
  957. if (file->private_data == NULL)
  958. return -EBADF;
  959. open_file = file->private_data;
  960. xid = GetXid();
  961. long_op = cifs_write_timeout(cifsi, *poffset);
  962. for (total_written = 0; write_size > total_written;
  963. total_written += bytes_written) {
  964. rc = -EAGAIN;
  965. while (rc == -EAGAIN) {
  966. if (file->private_data == NULL) {
  967. /* file has been closed on us */
  968. FreeXid(xid);
  969. /* if we have gotten here we have written some data
  970. and blocked, and the file has been freed on us
  971. while we blocked so return what we managed to
  972. write */
  973. return total_written;
  974. }
  975. if (open_file->closePend) {
  976. FreeXid(xid);
  977. if (total_written)
  978. return total_written;
  979. else
  980. return -EBADF;
  981. }
  982. if (open_file->invalidHandle) {
  983. /* we could deadlock if we called
  984. filemap_fdatawait from here so tell
  985. reopen_file not to flush data to
  986. server now */
  987. rc = cifs_reopen_file(file, false);
  988. if (rc != 0)
  989. break;
  990. }
  991. if (experimEnabled || (pTcon->ses->server &&
  992. ((pTcon->ses->server->secMode &
  993. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  994. == 0))) {
  995. struct kvec iov[2];
  996. unsigned int len;
  997. len = min((size_t)cifs_sb->wsize,
  998. write_size - total_written);
  999. /* iov[0] is reserved for smb header */
  1000. iov[1].iov_base = (char *)write_data +
  1001. total_written;
  1002. iov[1].iov_len = len;
  1003. rc = CIFSSMBWrite2(xid, pTcon,
  1004. open_file->netfid, len,
  1005. *poffset, &bytes_written,
  1006. iov, 1, long_op);
  1007. } else
  1008. rc = CIFSSMBWrite(xid, pTcon,
  1009. open_file->netfid,
  1010. min_t(const int, cifs_sb->wsize,
  1011. write_size - total_written),
  1012. *poffset, &bytes_written,
  1013. write_data + total_written,
  1014. NULL, long_op);
  1015. }
  1016. if (rc || (bytes_written == 0)) {
  1017. if (total_written)
  1018. break;
  1019. else {
  1020. FreeXid(xid);
  1021. return rc;
  1022. }
  1023. } else {
  1024. cifs_update_eof(cifsi, *poffset, bytes_written);
  1025. *poffset += bytes_written;
  1026. }
  1027. long_op = CIFS_STD_OP; /* subsequent writes fast -
  1028. 15 seconds is plenty */
  1029. }
  1030. cifs_stats_bytes_written(pTcon, total_written);
  1031. /* since the write may have blocked check these pointers again */
  1032. if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) {
  1033. /*BB We could make this contingent on superblock ATIME flag too */
  1034. /* file->f_path.dentry->d_inode->i_ctime =
  1035. file->f_path.dentry->d_inode->i_mtime = CURRENT_TIME;*/
  1036. if (total_written > 0) {
  1037. spin_lock(&file->f_path.dentry->d_inode->i_lock);
  1038. if (*poffset > file->f_path.dentry->d_inode->i_size)
  1039. i_size_write(file->f_path.dentry->d_inode,
  1040. *poffset);
  1041. spin_unlock(&file->f_path.dentry->d_inode->i_lock);
  1042. }
  1043. mark_inode_dirty_sync(file->f_path.dentry->d_inode);
  1044. }
  1045. FreeXid(xid);
  1046. return total_written;
  1047. }
  1048. #ifdef CONFIG_CIFS_EXPERIMENTAL
  1049. struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode)
  1050. {
  1051. struct cifsFileInfo *open_file = NULL;
  1052. read_lock(&GlobalSMBSeslock);
  1053. /* we could simply get the first_list_entry since write-only entries
  1054. are always at the end of the list but since the first entry might
  1055. have a close pending, we go through the whole list */
  1056. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1057. if (open_file->closePend)
  1058. continue;
  1059. if (open_file->pfile && ((open_file->pfile->f_flags & O_RDWR) ||
  1060. (open_file->pfile->f_flags & O_RDONLY))) {
  1061. if (!open_file->invalidHandle) {
  1062. /* found a good file */
  1063. /* lock it so it will not be closed on us */
  1064. cifsFileInfo_get(open_file);
  1065. read_unlock(&GlobalSMBSeslock);
  1066. return open_file;
  1067. } /* else might as well continue, and look for
  1068. another, or simply have the caller reopen it
  1069. again rather than trying to fix this handle */
  1070. } else /* write only file */
  1071. break; /* write only files are last so must be done */
  1072. }
  1073. read_unlock(&GlobalSMBSeslock);
  1074. return NULL;
  1075. }
  1076. #endif
  1077. struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode)
  1078. {
  1079. struct cifsFileInfo *open_file;
  1080. bool any_available = false;
  1081. int rc;
  1082. /* Having a null inode here (because mapping->host was set to zero by
  1083. the VFS or MM) should not happen but we had reports of on oops (due to
  1084. it being zero) during stress testcases so we need to check for it */
  1085. if (cifs_inode == NULL) {
  1086. cERROR(1, "Null inode passed to cifs_writeable_file");
  1087. dump_stack();
  1088. return NULL;
  1089. }
  1090. read_lock(&GlobalSMBSeslock);
  1091. refind_writable:
  1092. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1093. if (open_file->closePend ||
  1094. (!any_available && open_file->pid != current->tgid))
  1095. continue;
  1096. if (open_file->pfile &&
  1097. ((open_file->pfile->f_flags & O_RDWR) ||
  1098. (open_file->pfile->f_flags & O_WRONLY))) {
  1099. cifsFileInfo_get(open_file);
  1100. if (!open_file->invalidHandle) {
  1101. /* found a good writable file */
  1102. read_unlock(&GlobalSMBSeslock);
  1103. return open_file;
  1104. }
  1105. read_unlock(&GlobalSMBSeslock);
  1106. /* Had to unlock since following call can block */
  1107. rc = cifs_reopen_file(open_file->pfile, false);
  1108. if (!rc) {
  1109. if (!open_file->closePend)
  1110. return open_file;
  1111. else { /* start over in case this was deleted */
  1112. /* since the list could be modified */
  1113. read_lock(&GlobalSMBSeslock);
  1114. cifsFileInfo_put(open_file);
  1115. goto refind_writable;
  1116. }
  1117. }
  1118. /* if it fails, try another handle if possible -
  1119. (we can not do this if closePending since
  1120. loop could be modified - in which case we
  1121. have to start at the beginning of the list
  1122. again. Note that it would be bad
  1123. to hold up writepages here (rather than
  1124. in caller) with continuous retries */
  1125. cFYI(1, "wp failed on reopen file");
  1126. read_lock(&GlobalSMBSeslock);
  1127. /* can not use this handle, no write
  1128. pending on this one after all */
  1129. cifsFileInfo_put(open_file);
  1130. if (open_file->closePend) /* list could have changed */
  1131. goto refind_writable;
  1132. /* else we simply continue to the next entry. Thus
  1133. we do not loop on reopen errors. If we
  1134. can not reopen the file, for example if we
  1135. reconnected to a server with another client
  1136. racing to delete or lock the file we would not
  1137. make progress if we restarted before the beginning
  1138. of the loop here. */
  1139. }
  1140. }
  1141. /* couldn't find useable FH with same pid, try any available */
  1142. if (!any_available) {
  1143. any_available = true;
  1144. goto refind_writable;
  1145. }
  1146. read_unlock(&GlobalSMBSeslock);
  1147. return NULL;
  1148. }
  1149. static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
  1150. {
  1151. struct address_space *mapping = page->mapping;
  1152. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1153. char *write_data;
  1154. int rc = -EFAULT;
  1155. int bytes_written = 0;
  1156. struct cifs_sb_info *cifs_sb;
  1157. struct cifsTconInfo *pTcon;
  1158. struct inode *inode;
  1159. struct cifsFileInfo *open_file;
  1160. if (!mapping || !mapping->host)
  1161. return -EFAULT;
  1162. inode = page->mapping->host;
  1163. cifs_sb = CIFS_SB(inode->i_sb);
  1164. pTcon = cifs_sb->tcon;
  1165. offset += (loff_t)from;
  1166. write_data = kmap(page);
  1167. write_data += from;
  1168. if ((to > PAGE_CACHE_SIZE) || (from > to)) {
  1169. kunmap(page);
  1170. return -EIO;
  1171. }
  1172. /* racing with truncate? */
  1173. if (offset > mapping->host->i_size) {
  1174. kunmap(page);
  1175. return 0; /* don't care */
  1176. }
  1177. /* check to make sure that we are not extending the file */
  1178. if (mapping->host->i_size - offset < (loff_t)to)
  1179. to = (unsigned)(mapping->host->i_size - offset);
  1180. open_file = find_writable_file(CIFS_I(mapping->host));
  1181. if (open_file) {
  1182. bytes_written = cifs_write(open_file->pfile, write_data,
  1183. to-from, &offset);
  1184. cifsFileInfo_put(open_file);
  1185. /* Does mm or vfs already set times? */
  1186. inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
  1187. if ((bytes_written > 0) && (offset))
  1188. rc = 0;
  1189. else if (bytes_written < 0)
  1190. rc = bytes_written;
  1191. } else {
  1192. cFYI(1, "No writeable filehandles for inode");
  1193. rc = -EIO;
  1194. }
  1195. kunmap(page);
  1196. return rc;
  1197. }
  1198. static int cifs_writepages(struct address_space *mapping,
  1199. struct writeback_control *wbc)
  1200. {
  1201. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1202. unsigned int bytes_to_write;
  1203. unsigned int bytes_written;
  1204. struct cifs_sb_info *cifs_sb;
  1205. int done = 0;
  1206. pgoff_t end;
  1207. pgoff_t index;
  1208. int range_whole = 0;
  1209. struct kvec *iov;
  1210. int len;
  1211. int n_iov = 0;
  1212. pgoff_t next;
  1213. int nr_pages;
  1214. __u64 offset = 0;
  1215. struct cifsFileInfo *open_file;
  1216. struct cifsInodeInfo *cifsi = CIFS_I(mapping->host);
  1217. struct page *page;
  1218. struct pagevec pvec;
  1219. int rc = 0;
  1220. int scanned = 0;
  1221. int xid, long_op;
  1222. cifs_sb = CIFS_SB(mapping->host->i_sb);
  1223. /*
  1224. * If wsize is smaller that the page cache size, default to writing
  1225. * one page at a time via cifs_writepage
  1226. */
  1227. if (cifs_sb->wsize < PAGE_CACHE_SIZE)
  1228. return generic_writepages(mapping, wbc);
  1229. if ((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->server))
  1230. if (cifs_sb->tcon->ses->server->secMode &
  1231. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  1232. if (!experimEnabled)
  1233. return generic_writepages(mapping, wbc);
  1234. iov = kmalloc(32 * sizeof(struct kvec), GFP_KERNEL);
  1235. if (iov == NULL)
  1236. return generic_writepages(mapping, wbc);
  1237. /*
  1238. * BB: Is this meaningful for a non-block-device file system?
  1239. * If it is, we should test it again after we do I/O
  1240. */
  1241. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  1242. wbc->encountered_congestion = 1;
  1243. kfree(iov);
  1244. return 0;
  1245. }
  1246. xid = GetXid();
  1247. pagevec_init(&pvec, 0);
  1248. if (wbc->range_cyclic) {
  1249. index = mapping->writeback_index; /* Start from prev offset */
  1250. end = -1;
  1251. } else {
  1252. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  1253. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1254. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  1255. range_whole = 1;
  1256. scanned = 1;
  1257. }
  1258. retry:
  1259. while (!done && (index <= end) &&
  1260. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  1261. PAGECACHE_TAG_DIRTY,
  1262. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1))) {
  1263. int first;
  1264. unsigned int i;
  1265. first = -1;
  1266. next = 0;
  1267. n_iov = 0;
  1268. bytes_to_write = 0;
  1269. for (i = 0; i < nr_pages; i++) {
  1270. page = pvec.pages[i];
  1271. /*
  1272. * At this point we hold neither mapping->tree_lock nor
  1273. * lock on the page itself: the page may be truncated or
  1274. * invalidated (changing page->mapping to NULL), or even
  1275. * swizzled back from swapper_space to tmpfs file
  1276. * mapping
  1277. */
  1278. if (first < 0)
  1279. lock_page(page);
  1280. else if (!trylock_page(page))
  1281. break;
  1282. if (unlikely(page->mapping != mapping)) {
  1283. unlock_page(page);
  1284. break;
  1285. }
  1286. if (!wbc->range_cyclic && page->index > end) {
  1287. done = 1;
  1288. unlock_page(page);
  1289. break;
  1290. }
  1291. if (next && (page->index != next)) {
  1292. /* Not next consecutive page */
  1293. unlock_page(page);
  1294. break;
  1295. }
  1296. if (wbc->sync_mode != WB_SYNC_NONE)
  1297. wait_on_page_writeback(page);
  1298. if (PageWriteback(page) ||
  1299. !clear_page_dirty_for_io(page)) {
  1300. unlock_page(page);
  1301. break;
  1302. }
  1303. /*
  1304. * This actually clears the dirty bit in the radix tree.
  1305. * See cifs_writepage() for more commentary.
  1306. */
  1307. set_page_writeback(page);
  1308. if (page_offset(page) >= mapping->host->i_size) {
  1309. done = 1;
  1310. unlock_page(page);
  1311. end_page_writeback(page);
  1312. break;
  1313. }
  1314. /*
  1315. * BB can we get rid of this? pages are held by pvec
  1316. */
  1317. page_cache_get(page);
  1318. len = min(mapping->host->i_size - page_offset(page),
  1319. (loff_t)PAGE_CACHE_SIZE);
  1320. /* reserve iov[0] for the smb header */
  1321. n_iov++;
  1322. iov[n_iov].iov_base = kmap(page);
  1323. iov[n_iov].iov_len = len;
  1324. bytes_to_write += len;
  1325. if (first < 0) {
  1326. first = i;
  1327. offset = page_offset(page);
  1328. }
  1329. next = page->index + 1;
  1330. if (bytes_to_write + PAGE_CACHE_SIZE > cifs_sb->wsize)
  1331. break;
  1332. }
  1333. if (n_iov) {
  1334. /* Search for a writable handle every time we call
  1335. * CIFSSMBWrite2. We can't rely on the last handle
  1336. * we used to still be valid
  1337. */
  1338. open_file = find_writable_file(CIFS_I(mapping->host));
  1339. if (!open_file) {
  1340. cERROR(1, "No writable handles for inode");
  1341. rc = -EBADF;
  1342. } else {
  1343. long_op = cifs_write_timeout(cifsi, offset);
  1344. rc = CIFSSMBWrite2(xid, cifs_sb->tcon,
  1345. open_file->netfid,
  1346. bytes_to_write, offset,
  1347. &bytes_written, iov, n_iov,
  1348. long_op);
  1349. cifsFileInfo_put(open_file);
  1350. cifs_update_eof(cifsi, offset, bytes_written);
  1351. if (rc || bytes_written < bytes_to_write) {
  1352. cERROR(1, "Write2 ret %d, wrote %d",
  1353. rc, bytes_written);
  1354. /* BB what if continued retry is
  1355. requested via mount flags? */
  1356. if (rc == -ENOSPC)
  1357. set_bit(AS_ENOSPC, &mapping->flags);
  1358. else
  1359. set_bit(AS_EIO, &mapping->flags);
  1360. } else {
  1361. cifs_stats_bytes_written(cifs_sb->tcon,
  1362. bytes_written);
  1363. }
  1364. }
  1365. for (i = 0; i < n_iov; i++) {
  1366. page = pvec.pages[first + i];
  1367. /* Should we also set page error on
  1368. success rc but too little data written? */
  1369. /* BB investigate retry logic on temporary
  1370. server crash cases and how recovery works
  1371. when page marked as error */
  1372. if (rc)
  1373. SetPageError(page);
  1374. kunmap(page);
  1375. unlock_page(page);
  1376. end_page_writeback(page);
  1377. page_cache_release(page);
  1378. }
  1379. if ((wbc->nr_to_write -= n_iov) <= 0)
  1380. done = 1;
  1381. index = next;
  1382. } else
  1383. /* Need to re-find the pages we skipped */
  1384. index = pvec.pages[0]->index + 1;
  1385. pagevec_release(&pvec);
  1386. }
  1387. if (!scanned && !done) {
  1388. /*
  1389. * We hit the last page and there is more work to be done: wrap
  1390. * back to the start of the file
  1391. */
  1392. scanned = 1;
  1393. index = 0;
  1394. goto retry;
  1395. }
  1396. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  1397. mapping->writeback_index = index;
  1398. FreeXid(xid);
  1399. kfree(iov);
  1400. return rc;
  1401. }
  1402. static int cifs_writepage(struct page *page, struct writeback_control *wbc)
  1403. {
  1404. int rc = -EFAULT;
  1405. int xid;
  1406. xid = GetXid();
  1407. /* BB add check for wbc flags */
  1408. page_cache_get(page);
  1409. if (!PageUptodate(page))
  1410. cFYI(1, "ppw - page not up to date");
  1411. /*
  1412. * Set the "writeback" flag, and clear "dirty" in the radix tree.
  1413. *
  1414. * A writepage() implementation always needs to do either this,
  1415. * or re-dirty the page with "redirty_page_for_writepage()" in
  1416. * the case of a failure.
  1417. *
  1418. * Just unlocking the page will cause the radix tree tag-bits
  1419. * to fail to update with the state of the page correctly.
  1420. */
  1421. set_page_writeback(page);
  1422. rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
  1423. SetPageUptodate(page); /* BB add check for error and Clearuptodate? */
  1424. unlock_page(page);
  1425. end_page_writeback(page);
  1426. page_cache_release(page);
  1427. FreeXid(xid);
  1428. return rc;
  1429. }
  1430. static int cifs_write_end(struct file *file, struct address_space *mapping,
  1431. loff_t pos, unsigned len, unsigned copied,
  1432. struct page *page, void *fsdata)
  1433. {
  1434. int rc;
  1435. struct inode *inode = mapping->host;
  1436. cFYI(1, "write_end for page %p from pos %lld with %d bytes",
  1437. page, pos, copied);
  1438. if (PageChecked(page)) {
  1439. if (copied == len)
  1440. SetPageUptodate(page);
  1441. ClearPageChecked(page);
  1442. } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
  1443. SetPageUptodate(page);
  1444. if (!PageUptodate(page)) {
  1445. char *page_data;
  1446. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  1447. int xid;
  1448. xid = GetXid();
  1449. /* this is probably better than directly calling
  1450. partialpage_write since in this function the file handle is
  1451. known which we might as well leverage */
  1452. /* BB check if anything else missing out of ppw
  1453. such as updating last write time */
  1454. page_data = kmap(page);
  1455. rc = cifs_write(file, page_data + offset, copied, &pos);
  1456. /* if (rc < 0) should we set writebehind rc? */
  1457. kunmap(page);
  1458. FreeXid(xid);
  1459. } else {
  1460. rc = copied;
  1461. pos += copied;
  1462. set_page_dirty(page);
  1463. }
  1464. if (rc > 0) {
  1465. spin_lock(&inode->i_lock);
  1466. if (pos > inode->i_size)
  1467. i_size_write(inode, pos);
  1468. spin_unlock(&inode->i_lock);
  1469. }
  1470. unlock_page(page);
  1471. page_cache_release(page);
  1472. return rc;
  1473. }
  1474. int cifs_fsync(struct file *file, int datasync)
  1475. {
  1476. int xid;
  1477. int rc = 0;
  1478. struct cifsTconInfo *tcon;
  1479. struct cifsFileInfo *smbfile = file->private_data;
  1480. struct inode *inode = file->f_path.dentry->d_inode;
  1481. xid = GetXid();
  1482. cFYI(1, "Sync file - name: %s datasync: 0x%x",
  1483. file->f_path.dentry->d_name.name, datasync);
  1484. rc = filemap_write_and_wait(inode->i_mapping);
  1485. if (rc == 0) {
  1486. rc = CIFS_I(inode)->write_behind_rc;
  1487. CIFS_I(inode)->write_behind_rc = 0;
  1488. tcon = CIFS_SB(inode->i_sb)->tcon;
  1489. if (!rc && tcon && smbfile &&
  1490. !(CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
  1491. rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
  1492. }
  1493. FreeXid(xid);
  1494. return rc;
  1495. }
  1496. /* static void cifs_sync_page(struct page *page)
  1497. {
  1498. struct address_space *mapping;
  1499. struct inode *inode;
  1500. unsigned long index = page->index;
  1501. unsigned int rpages = 0;
  1502. int rc = 0;
  1503. cFYI(1, "sync page %p", page);
  1504. mapping = page->mapping;
  1505. if (!mapping)
  1506. return 0;
  1507. inode = mapping->host;
  1508. if (!inode)
  1509. return; */
  1510. /* fill in rpages then
  1511. result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */
  1512. /* cFYI(1, "rpages is %d for sync page of Index %ld", rpages, index);
  1513. #if 0
  1514. if (rc < 0)
  1515. return rc;
  1516. return 0;
  1517. #endif
  1518. } */
  1519. /*
  1520. * As file closes, flush all cached write data for this inode checking
  1521. * for write behind errors.
  1522. */
  1523. int cifs_flush(struct file *file, fl_owner_t id)
  1524. {
  1525. struct inode *inode = file->f_path.dentry->d_inode;
  1526. int rc = 0;
  1527. /* Rather than do the steps manually:
  1528. lock the inode for writing
  1529. loop through pages looking for write behind data (dirty pages)
  1530. coalesce into contiguous 16K (or smaller) chunks to write to server
  1531. send to server (prefer in parallel)
  1532. deal with writebehind errors
  1533. unlock inode for writing
  1534. filemapfdatawrite appears easier for the time being */
  1535. rc = filemap_fdatawrite(inode->i_mapping);
  1536. /* reset wb rc if we were able to write out dirty pages */
  1537. if (!rc) {
  1538. rc = CIFS_I(inode)->write_behind_rc;
  1539. CIFS_I(inode)->write_behind_rc = 0;
  1540. }
  1541. cFYI(1, "Flush inode %p file %p rc %d", inode, file, rc);
  1542. return rc;
  1543. }
  1544. ssize_t cifs_user_read(struct file *file, char __user *read_data,
  1545. size_t read_size, loff_t *poffset)
  1546. {
  1547. int rc = -EACCES;
  1548. unsigned int bytes_read = 0;
  1549. unsigned int total_read = 0;
  1550. unsigned int current_read_size;
  1551. struct cifs_sb_info *cifs_sb;
  1552. struct cifsTconInfo *pTcon;
  1553. int xid;
  1554. struct cifsFileInfo *open_file;
  1555. char *smb_read_data;
  1556. char __user *current_offset;
  1557. struct smb_com_read_rsp *pSMBr;
  1558. xid = GetXid();
  1559. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1560. pTcon = cifs_sb->tcon;
  1561. if (file->private_data == NULL) {
  1562. rc = -EBADF;
  1563. FreeXid(xid);
  1564. return rc;
  1565. }
  1566. open_file = file->private_data;
  1567. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  1568. cFYI(1, "attempting read on write only file instance");
  1569. for (total_read = 0, current_offset = read_data;
  1570. read_size > total_read;
  1571. total_read += bytes_read, current_offset += bytes_read) {
  1572. current_read_size = min_t(const int, read_size - total_read,
  1573. cifs_sb->rsize);
  1574. rc = -EAGAIN;
  1575. smb_read_data = NULL;
  1576. while (rc == -EAGAIN) {
  1577. int buf_type = CIFS_NO_BUFFER;
  1578. if ((open_file->invalidHandle) &&
  1579. (!open_file->closePend)) {
  1580. rc = cifs_reopen_file(file, true);
  1581. if (rc != 0)
  1582. break;
  1583. }
  1584. rc = CIFSSMBRead(xid, pTcon,
  1585. open_file->netfid,
  1586. current_read_size, *poffset,
  1587. &bytes_read, &smb_read_data,
  1588. &buf_type);
  1589. pSMBr = (struct smb_com_read_rsp *)smb_read_data;
  1590. if (smb_read_data) {
  1591. if (copy_to_user(current_offset,
  1592. smb_read_data +
  1593. 4 /* RFC1001 length field */ +
  1594. le16_to_cpu(pSMBr->DataOffset),
  1595. bytes_read))
  1596. rc = -EFAULT;
  1597. if (buf_type == CIFS_SMALL_BUFFER)
  1598. cifs_small_buf_release(smb_read_data);
  1599. else if (buf_type == CIFS_LARGE_BUFFER)
  1600. cifs_buf_release(smb_read_data);
  1601. smb_read_data = NULL;
  1602. }
  1603. }
  1604. if (rc || (bytes_read == 0)) {
  1605. if (total_read) {
  1606. break;
  1607. } else {
  1608. FreeXid(xid);
  1609. return rc;
  1610. }
  1611. } else {
  1612. cifs_stats_bytes_read(pTcon, bytes_read);
  1613. *poffset += bytes_read;
  1614. }
  1615. }
  1616. FreeXid(xid);
  1617. return total_read;
  1618. }
  1619. static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
  1620. loff_t *poffset)
  1621. {
  1622. int rc = -EACCES;
  1623. unsigned int bytes_read = 0;
  1624. unsigned int total_read;
  1625. unsigned int current_read_size;
  1626. struct cifs_sb_info *cifs_sb;
  1627. struct cifsTconInfo *pTcon;
  1628. int xid;
  1629. char *current_offset;
  1630. struct cifsFileInfo *open_file;
  1631. int buf_type = CIFS_NO_BUFFER;
  1632. xid = GetXid();
  1633. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1634. pTcon = cifs_sb->tcon;
  1635. if (file->private_data == NULL) {
  1636. rc = -EBADF;
  1637. FreeXid(xid);
  1638. return rc;
  1639. }
  1640. open_file = file->private_data;
  1641. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  1642. cFYI(1, "attempting read on write only file instance");
  1643. for (total_read = 0, current_offset = read_data;
  1644. read_size > total_read;
  1645. total_read += bytes_read, current_offset += bytes_read) {
  1646. current_read_size = min_t(const int, read_size - total_read,
  1647. cifs_sb->rsize);
  1648. /* For windows me and 9x we do not want to request more
  1649. than it negotiated since it will refuse the read then */
  1650. if ((pTcon->ses) &&
  1651. !(pTcon->ses->capabilities & CAP_LARGE_FILES)) {
  1652. current_read_size = min_t(const int, current_read_size,
  1653. pTcon->ses->server->maxBuf - 128);
  1654. }
  1655. rc = -EAGAIN;
  1656. while (rc == -EAGAIN) {
  1657. if ((open_file->invalidHandle) &&
  1658. (!open_file->closePend)) {
  1659. rc = cifs_reopen_file(file, true);
  1660. if (rc != 0)
  1661. break;
  1662. }
  1663. rc = CIFSSMBRead(xid, pTcon,
  1664. open_file->netfid,
  1665. current_read_size, *poffset,
  1666. &bytes_read, &current_offset,
  1667. &buf_type);
  1668. }
  1669. if (rc || (bytes_read == 0)) {
  1670. if (total_read) {
  1671. break;
  1672. } else {
  1673. FreeXid(xid);
  1674. return rc;
  1675. }
  1676. } else {
  1677. cifs_stats_bytes_read(pTcon, total_read);
  1678. *poffset += bytes_read;
  1679. }
  1680. }
  1681. FreeXid(xid);
  1682. return total_read;
  1683. }
  1684. int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
  1685. {
  1686. int rc, xid;
  1687. xid = GetXid();
  1688. rc = cifs_revalidate_file(file);
  1689. if (rc) {
  1690. cFYI(1, "Validation prior to mmap failed, error=%d", rc);
  1691. FreeXid(xid);
  1692. return rc;
  1693. }
  1694. rc = generic_file_mmap(file, vma);
  1695. FreeXid(xid);
  1696. return rc;
  1697. }
  1698. static void cifs_copy_cache_pages(struct address_space *mapping,
  1699. struct list_head *pages, int bytes_read, char *data)
  1700. {
  1701. struct page *page;
  1702. char *target;
  1703. while (bytes_read > 0) {
  1704. if (list_empty(pages))
  1705. break;
  1706. page = list_entry(pages->prev, struct page, lru);
  1707. list_del(&page->lru);
  1708. if (add_to_page_cache_lru(page, mapping, page->index,
  1709. GFP_KERNEL)) {
  1710. page_cache_release(page);
  1711. cFYI(1, "Add page cache failed");
  1712. data += PAGE_CACHE_SIZE;
  1713. bytes_read -= PAGE_CACHE_SIZE;
  1714. continue;
  1715. }
  1716. page_cache_release(page);
  1717. target = kmap_atomic(page, KM_USER0);
  1718. if (PAGE_CACHE_SIZE > bytes_read) {
  1719. memcpy(target, data, bytes_read);
  1720. /* zero the tail end of this partial page */
  1721. memset(target + bytes_read, 0,
  1722. PAGE_CACHE_SIZE - bytes_read);
  1723. bytes_read = 0;
  1724. } else {
  1725. memcpy(target, data, PAGE_CACHE_SIZE);
  1726. bytes_read -= PAGE_CACHE_SIZE;
  1727. }
  1728. kunmap_atomic(target, KM_USER0);
  1729. flush_dcache_page(page);
  1730. SetPageUptodate(page);
  1731. unlock_page(page);
  1732. data += PAGE_CACHE_SIZE;
  1733. /* add page to FS-Cache */
  1734. cifs_readpage_to_fscache(mapping->host, page);
  1735. }
  1736. return;
  1737. }
  1738. static int cifs_readpages(struct file *file, struct address_space *mapping,
  1739. struct list_head *page_list, unsigned num_pages)
  1740. {
  1741. int rc = -EACCES;
  1742. int xid;
  1743. loff_t offset;
  1744. struct page *page;
  1745. struct cifs_sb_info *cifs_sb;
  1746. struct cifsTconInfo *pTcon;
  1747. unsigned int bytes_read = 0;
  1748. unsigned int read_size, i;
  1749. char *smb_read_data = NULL;
  1750. struct smb_com_read_rsp *pSMBr;
  1751. struct cifsFileInfo *open_file;
  1752. int buf_type = CIFS_NO_BUFFER;
  1753. xid = GetXid();
  1754. if (file->private_data == NULL) {
  1755. rc = -EBADF;
  1756. FreeXid(xid);
  1757. return rc;
  1758. }
  1759. open_file = file->private_data;
  1760. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1761. pTcon = cifs_sb->tcon;
  1762. /*
  1763. * Reads as many pages as possible from fscache. Returns -ENOBUFS
  1764. * immediately if the cookie is negative
  1765. */
  1766. rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
  1767. &num_pages);
  1768. if (rc == 0)
  1769. goto read_complete;
  1770. cFYI(DBG2, "rpages: num pages %d", num_pages);
  1771. for (i = 0; i < num_pages; ) {
  1772. unsigned contig_pages;
  1773. struct page *tmp_page;
  1774. unsigned long expected_index;
  1775. if (list_empty(page_list))
  1776. break;
  1777. page = list_entry(page_list->prev, struct page, lru);
  1778. offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1779. /* count adjacent pages that we will read into */
  1780. contig_pages = 0;
  1781. expected_index =
  1782. list_entry(page_list->prev, struct page, lru)->index;
  1783. list_for_each_entry_reverse(tmp_page, page_list, lru) {
  1784. if (tmp_page->index == expected_index) {
  1785. contig_pages++;
  1786. expected_index++;
  1787. } else
  1788. break;
  1789. }
  1790. if (contig_pages + i > num_pages)
  1791. contig_pages = num_pages - i;
  1792. /* for reads over a certain size could initiate async
  1793. read ahead */
  1794. read_size = contig_pages * PAGE_CACHE_SIZE;
  1795. /* Read size needs to be in multiples of one page */
  1796. read_size = min_t(const unsigned int, read_size,
  1797. cifs_sb->rsize & PAGE_CACHE_MASK);
  1798. cFYI(DBG2, "rpages: read size 0x%x contiguous pages %d",
  1799. read_size, contig_pages);
  1800. rc = -EAGAIN;
  1801. while (rc == -EAGAIN) {
  1802. if ((open_file->invalidHandle) &&
  1803. (!open_file->closePend)) {
  1804. rc = cifs_reopen_file(file, true);
  1805. if (rc != 0)
  1806. break;
  1807. }
  1808. rc = CIFSSMBRead(xid, pTcon,
  1809. open_file->netfid,
  1810. read_size, offset,
  1811. &bytes_read, &smb_read_data,
  1812. &buf_type);
  1813. /* BB more RC checks ? */
  1814. if (rc == -EAGAIN) {
  1815. if (smb_read_data) {
  1816. if (buf_type == CIFS_SMALL_BUFFER)
  1817. cifs_small_buf_release(smb_read_data);
  1818. else if (buf_type == CIFS_LARGE_BUFFER)
  1819. cifs_buf_release(smb_read_data);
  1820. smb_read_data = NULL;
  1821. }
  1822. }
  1823. }
  1824. if ((rc < 0) || (smb_read_data == NULL)) {
  1825. cFYI(1, "Read error in readpages: %d", rc);
  1826. break;
  1827. } else if (bytes_read > 0) {
  1828. task_io_account_read(bytes_read);
  1829. pSMBr = (struct smb_com_read_rsp *)smb_read_data;
  1830. cifs_copy_cache_pages(mapping, page_list, bytes_read,
  1831. smb_read_data + 4 /* RFC1001 hdr */ +
  1832. le16_to_cpu(pSMBr->DataOffset));
  1833. i += bytes_read >> PAGE_CACHE_SHIFT;
  1834. cifs_stats_bytes_read(pTcon, bytes_read);
  1835. if ((bytes_read & PAGE_CACHE_MASK) != bytes_read) {
  1836. i++; /* account for partial page */
  1837. /* server copy of file can have smaller size
  1838. than client */
  1839. /* BB do we need to verify this common case ?
  1840. this case is ok - if we are at server EOF
  1841. we will hit it on next read */
  1842. /* break; */
  1843. }
  1844. } else {
  1845. cFYI(1, "No bytes read (%d) at offset %lld . "
  1846. "Cleaning remaining pages from readahead list",
  1847. bytes_read, offset);
  1848. /* BB turn off caching and do new lookup on
  1849. file size at server? */
  1850. break;
  1851. }
  1852. if (smb_read_data) {
  1853. if (buf_type == CIFS_SMALL_BUFFER)
  1854. cifs_small_buf_release(smb_read_data);
  1855. else if (buf_type == CIFS_LARGE_BUFFER)
  1856. cifs_buf_release(smb_read_data);
  1857. smb_read_data = NULL;
  1858. }
  1859. bytes_read = 0;
  1860. }
  1861. /* need to free smb_read_data buf before exit */
  1862. if (smb_read_data) {
  1863. if (buf_type == CIFS_SMALL_BUFFER)
  1864. cifs_small_buf_release(smb_read_data);
  1865. else if (buf_type == CIFS_LARGE_BUFFER)
  1866. cifs_buf_release(smb_read_data);
  1867. smb_read_data = NULL;
  1868. }
  1869. read_complete:
  1870. FreeXid(xid);
  1871. return rc;
  1872. }
  1873. static int cifs_readpage_worker(struct file *file, struct page *page,
  1874. loff_t *poffset)
  1875. {
  1876. char *read_data;
  1877. int rc;
  1878. /* Is the page cached? */
  1879. rc = cifs_readpage_from_fscache(file->f_path.dentry->d_inode, page);
  1880. if (rc == 0)
  1881. goto read_complete;
  1882. page_cache_get(page);
  1883. read_data = kmap(page);
  1884. /* for reads over a certain size could initiate async read ahead */
  1885. rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
  1886. if (rc < 0)
  1887. goto io_error;
  1888. else
  1889. cFYI(1, "Bytes read %d", rc);
  1890. file->f_path.dentry->d_inode->i_atime =
  1891. current_fs_time(file->f_path.dentry->d_inode->i_sb);
  1892. if (PAGE_CACHE_SIZE > rc)
  1893. memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
  1894. flush_dcache_page(page);
  1895. SetPageUptodate(page);
  1896. /* send this page to the cache */
  1897. cifs_readpage_to_fscache(file->f_path.dentry->d_inode, page);
  1898. rc = 0;
  1899. io_error:
  1900. kunmap(page);
  1901. page_cache_release(page);
  1902. read_complete:
  1903. return rc;
  1904. }
  1905. static int cifs_readpage(struct file *file, struct page *page)
  1906. {
  1907. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1908. int rc = -EACCES;
  1909. int xid;
  1910. xid = GetXid();
  1911. if (file->private_data == NULL) {
  1912. rc = -EBADF;
  1913. FreeXid(xid);
  1914. return rc;
  1915. }
  1916. cFYI(1, "readpage %p at offset %d 0x%x\n",
  1917. page, (int)offset, (int)offset);
  1918. rc = cifs_readpage_worker(file, page, &offset);
  1919. unlock_page(page);
  1920. FreeXid(xid);
  1921. return rc;
  1922. }
  1923. static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
  1924. {
  1925. struct cifsFileInfo *open_file;
  1926. read_lock(&GlobalSMBSeslock);
  1927. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1928. if (open_file->closePend)
  1929. continue;
  1930. if (open_file->pfile &&
  1931. ((open_file->pfile->f_flags & O_RDWR) ||
  1932. (open_file->pfile->f_flags & O_WRONLY))) {
  1933. read_unlock(&GlobalSMBSeslock);
  1934. return 1;
  1935. }
  1936. }
  1937. read_unlock(&GlobalSMBSeslock);
  1938. return 0;
  1939. }
  1940. /* We do not want to update the file size from server for inodes
  1941. open for write - to avoid races with writepage extending
  1942. the file - in the future we could consider allowing
  1943. refreshing the inode only on increases in the file size
  1944. but this is tricky to do without racing with writebehind
  1945. page caching in the current Linux kernel design */
  1946. bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
  1947. {
  1948. if (!cifsInode)
  1949. return true;
  1950. if (is_inode_writable(cifsInode)) {
  1951. /* This inode is open for write at least once */
  1952. struct cifs_sb_info *cifs_sb;
  1953. cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
  1954. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  1955. /* since no page cache to corrupt on directio
  1956. we can change size safely */
  1957. return true;
  1958. }
  1959. if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
  1960. return true;
  1961. return false;
  1962. } else
  1963. return true;
  1964. }
  1965. static int cifs_write_begin(struct file *file, struct address_space *mapping,
  1966. loff_t pos, unsigned len, unsigned flags,
  1967. struct page **pagep, void **fsdata)
  1968. {
  1969. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1970. loff_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1971. loff_t page_start = pos & PAGE_MASK;
  1972. loff_t i_size;
  1973. struct page *page;
  1974. int rc = 0;
  1975. cFYI(1, "write_begin from %lld len %d", (long long)pos, len);
  1976. page = grab_cache_page_write_begin(mapping, index, flags);
  1977. if (!page) {
  1978. rc = -ENOMEM;
  1979. goto out;
  1980. }
  1981. if (PageUptodate(page))
  1982. goto out;
  1983. /*
  1984. * If we write a full page it will be up to date, no need to read from
  1985. * the server. If the write is short, we'll end up doing a sync write
  1986. * instead.
  1987. */
  1988. if (len == PAGE_CACHE_SIZE)
  1989. goto out;
  1990. /*
  1991. * optimize away the read when we have an oplock, and we're not
  1992. * expecting to use any of the data we'd be reading in. That
  1993. * is, when the page lies beyond the EOF, or straddles the EOF
  1994. * and the write will cover all of the existing data.
  1995. */
  1996. if (CIFS_I(mapping->host)->clientCanCacheRead) {
  1997. i_size = i_size_read(mapping->host);
  1998. if (page_start >= i_size ||
  1999. (offset == 0 && (pos + len) >= i_size)) {
  2000. zero_user_segments(page, 0, offset,
  2001. offset + len,
  2002. PAGE_CACHE_SIZE);
  2003. /*
  2004. * PageChecked means that the parts of the page
  2005. * to which we're not writing are considered up
  2006. * to date. Once the data is copied to the
  2007. * page, it can be set uptodate.
  2008. */
  2009. SetPageChecked(page);
  2010. goto out;
  2011. }
  2012. }
  2013. if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
  2014. /*
  2015. * might as well read a page, it is fast enough. If we get
  2016. * an error, we don't need to return it. cifs_write_end will
  2017. * do a sync write instead since PG_uptodate isn't set.
  2018. */
  2019. cifs_readpage_worker(file, page, &page_start);
  2020. } else {
  2021. /* we could try using another file handle if there is one -
  2022. but how would we lock it to prevent close of that handle
  2023. racing with this read? In any case
  2024. this will be written out by write_end so is fine */
  2025. }
  2026. out:
  2027. *pagep = page;
  2028. return rc;
  2029. }
  2030. static int cifs_release_page(struct page *page, gfp_t gfp)
  2031. {
  2032. if (PagePrivate(page))
  2033. return 0;
  2034. return cifs_fscache_release_page(page, gfp);
  2035. }
  2036. static void cifs_invalidate_page(struct page *page, unsigned long offset)
  2037. {
  2038. struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
  2039. if (offset == 0)
  2040. cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
  2041. }
  2042. void cifs_oplock_break(struct work_struct *work)
  2043. {
  2044. struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
  2045. oplock_break);
  2046. struct inode *inode = cfile->pInode;
  2047. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2048. struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->mnt->mnt_sb);
  2049. int rc, waitrc = 0;
  2050. if (inode && S_ISREG(inode->i_mode)) {
  2051. if (cinode->clientCanCacheRead)
  2052. break_lease(inode, O_RDONLY);
  2053. else
  2054. break_lease(inode, O_WRONLY);
  2055. rc = filemap_fdatawrite(inode->i_mapping);
  2056. if (cinode->clientCanCacheRead == 0) {
  2057. waitrc = filemap_fdatawait(inode->i_mapping);
  2058. invalidate_remote_inode(inode);
  2059. }
  2060. if (!rc)
  2061. rc = waitrc;
  2062. if (rc)
  2063. cinode->write_behind_rc = rc;
  2064. cFYI(1, "Oplock flush inode %p rc %d", inode, rc);
  2065. }
  2066. /*
  2067. * releasing stale oplock after recent reconnect of smb session using
  2068. * a now incorrect file handle is not a data integrity issue but do
  2069. * not bother sending an oplock release if session to server still is
  2070. * disconnected since oplock already released by the server
  2071. */
  2072. if (!cfile->closePend && !cfile->oplock_break_cancelled) {
  2073. rc = CIFSSMBLock(0, cifs_sb->tcon, cfile->netfid, 0, 0, 0, 0,
  2074. LOCKING_ANDX_OPLOCK_RELEASE, false);
  2075. cFYI(1, "Oplock release rc = %d", rc);
  2076. }
  2077. /*
  2078. * We might have kicked in before is_valid_oplock_break()
  2079. * finished grabbing reference for us. Make sure it's done by
  2080. * waiting for GlobalSMSSeslock.
  2081. */
  2082. write_lock(&GlobalSMBSeslock);
  2083. write_unlock(&GlobalSMBSeslock);
  2084. cifs_oplock_break_put(cfile);
  2085. }
  2086. void cifs_oplock_break_get(struct cifsFileInfo *cfile)
  2087. {
  2088. mntget(cfile->mnt);
  2089. cifsFileInfo_get(cfile);
  2090. }
  2091. void cifs_oplock_break_put(struct cifsFileInfo *cfile)
  2092. {
  2093. mntput(cfile->mnt);
  2094. cifsFileInfo_put(cfile);
  2095. }
  2096. const struct address_space_operations cifs_addr_ops = {
  2097. .readpage = cifs_readpage,
  2098. .readpages = cifs_readpages,
  2099. .writepage = cifs_writepage,
  2100. .writepages = cifs_writepages,
  2101. .write_begin = cifs_write_begin,
  2102. .write_end = cifs_write_end,
  2103. .set_page_dirty = __set_page_dirty_nobuffers,
  2104. .releasepage = cifs_release_page,
  2105. .invalidatepage = cifs_invalidate_page,
  2106. /* .sync_page = cifs_sync_page, */
  2107. /* .direct_IO = */
  2108. };
  2109. /*
  2110. * cifs_readpages requires the server to support a buffer large enough to
  2111. * contain the header plus one complete page of data. Otherwise, we need
  2112. * to leave cifs_readpages out of the address space operations.
  2113. */
  2114. const struct address_space_operations cifs_addr_ops_smallbuf = {
  2115. .readpage = cifs_readpage,
  2116. .writepage = cifs_writepage,
  2117. .writepages = cifs_writepages,
  2118. .write_begin = cifs_write_begin,
  2119. .write_end = cifs_write_end,
  2120. .set_page_dirty = __set_page_dirty_nobuffers,
  2121. .releasepage = cifs_release_page,
  2122. .invalidatepage = cifs_invalidate_page,
  2123. /* .sync_page = cifs_sync_page, */
  2124. /* .direct_IO = */
  2125. };