file.c 56 KB

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