file.c 63 KB

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