file.c 56 KB

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