PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/cifs/file.c

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