file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * linux/fs/nfs/file.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * Changes Copyright (C) 1994 by Florian La Roche
  7. * - Do not copy data too often around in the kernel.
  8. * - In nfs_file_read the return value of kmalloc wasn't checked.
  9. * - Put in a better version of read look-ahead buffering. Original idea
  10. * and implementation by Wai S Kok elekokws@ee.nus.sg.
  11. *
  12. * Expire cache on write to a file by Wai S Kok (Oct 1994).
  13. *
  14. * Total rewrite of read side for new NFS buffer cache.. Linus.
  15. *
  16. * nfs regular file handling functions
  17. */
  18. #include <linux/time.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/stat.h>
  23. #include <linux/nfs_fs.h>
  24. #include <linux/nfs_mount.h>
  25. #include <linux/mm.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/aio.h>
  28. #include <linux/gfp.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/system.h>
  31. #include "delegation.h"
  32. #include "internal.h"
  33. #include "iostat.h"
  34. #include "fscache.h"
  35. #define NFSDBG_FACILITY NFSDBG_FILE
  36. static int nfs_file_open(struct inode *, struct file *);
  37. static int nfs_file_release(struct inode *, struct file *);
  38. static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
  39. static int nfs_file_mmap(struct file *, struct vm_area_struct *);
  40. static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos,
  41. struct pipe_inode_info *pipe,
  42. size_t count, unsigned int flags);
  43. static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov,
  44. unsigned long nr_segs, loff_t pos);
  45. static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
  46. struct file *filp, loff_t *ppos,
  47. size_t count, unsigned int flags);
  48. static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
  49. unsigned long nr_segs, loff_t pos);
  50. static int nfs_file_flush(struct file *, fl_owner_t id);
  51. static int nfs_file_fsync(struct file *, struct dentry *dentry, int datasync);
  52. static int nfs_check_flags(int flags);
  53. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
  54. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
  55. static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);
  56. static const struct vm_operations_struct nfs_file_vm_ops;
  57. const struct file_operations nfs_file_operations = {
  58. .llseek = nfs_file_llseek,
  59. .read = do_sync_read,
  60. .write = do_sync_write,
  61. .aio_read = nfs_file_read,
  62. .aio_write = nfs_file_write,
  63. .mmap = nfs_file_mmap,
  64. .open = nfs_file_open,
  65. .flush = nfs_file_flush,
  66. .release = nfs_file_release,
  67. .fsync = nfs_file_fsync,
  68. .lock = nfs_lock,
  69. .flock = nfs_flock,
  70. .splice_read = nfs_file_splice_read,
  71. .splice_write = nfs_file_splice_write,
  72. .check_flags = nfs_check_flags,
  73. .setlease = nfs_setlease,
  74. };
  75. const struct inode_operations nfs_file_inode_operations = {
  76. .permission = nfs_permission,
  77. .getattr = nfs_getattr,
  78. .setattr = nfs_setattr,
  79. };
  80. #ifdef CONFIG_NFS_V3
  81. const struct inode_operations nfs3_file_inode_operations = {
  82. .permission = nfs_permission,
  83. .getattr = nfs_getattr,
  84. .setattr = nfs_setattr,
  85. .listxattr = nfs3_listxattr,
  86. .getxattr = nfs3_getxattr,
  87. .setxattr = nfs3_setxattr,
  88. .removexattr = nfs3_removexattr,
  89. };
  90. #endif /* CONFIG_NFS_v3 */
  91. /* Hack for future NFS swap support */
  92. #ifndef IS_SWAPFILE
  93. # define IS_SWAPFILE(inode) (0)
  94. #endif
  95. static int nfs_check_flags(int flags)
  96. {
  97. if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
  98. return -EINVAL;
  99. return 0;
  100. }
  101. /*
  102. * Open file
  103. */
  104. static int
  105. nfs_file_open(struct inode *inode, struct file *filp)
  106. {
  107. int res;
  108. dprintk("NFS: open file(%s/%s)\n",
  109. filp->f_path.dentry->d_parent->d_name.name,
  110. filp->f_path.dentry->d_name.name);
  111. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  112. res = nfs_check_flags(filp->f_flags);
  113. if (res)
  114. return res;
  115. res = nfs_open(inode, filp);
  116. return res;
  117. }
  118. static int
  119. nfs_file_release(struct inode *inode, struct file *filp)
  120. {
  121. struct dentry *dentry = filp->f_path.dentry;
  122. dprintk("NFS: release(%s/%s)\n",
  123. dentry->d_parent->d_name.name,
  124. dentry->d_name.name);
  125. nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
  126. return nfs_release(inode, filp);
  127. }
  128. /**
  129. * nfs_revalidate_size - Revalidate the file size
  130. * @inode - pointer to inode struct
  131. * @file - pointer to struct file
  132. *
  133. * Revalidates the file length. This is basically a wrapper around
  134. * nfs_revalidate_inode() that takes into account the fact that we may
  135. * have cached writes (in which case we don't care about the server's
  136. * idea of what the file length is), or O_DIRECT (in which case we
  137. * shouldn't trust the cache).
  138. */
  139. static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
  140. {
  141. struct nfs_server *server = NFS_SERVER(inode);
  142. struct nfs_inode *nfsi = NFS_I(inode);
  143. if (server->flags & NFS_MOUNT_NOAC)
  144. goto force_reval;
  145. if (filp->f_flags & O_DIRECT)
  146. goto force_reval;
  147. if (nfsi->npages != 0)
  148. return 0;
  149. if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode))
  150. return 0;
  151. force_reval:
  152. return __nfs_revalidate_inode(server, inode);
  153. }
  154. static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
  155. {
  156. loff_t loff;
  157. dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
  158. filp->f_path.dentry->d_parent->d_name.name,
  159. filp->f_path.dentry->d_name.name,
  160. offset, origin);
  161. /* origin == SEEK_END => we must revalidate the cached file length */
  162. if (origin == SEEK_END) {
  163. struct inode *inode = filp->f_mapping->host;
  164. int retval = nfs_revalidate_file_size(inode, filp);
  165. if (retval < 0)
  166. return (loff_t)retval;
  167. spin_lock(&inode->i_lock);
  168. loff = generic_file_llseek_unlocked(filp, offset, origin);
  169. spin_unlock(&inode->i_lock);
  170. } else
  171. loff = generic_file_llseek_unlocked(filp, offset, origin);
  172. return loff;
  173. }
  174. /*
  175. * Helper for nfs_file_flush() and nfs_file_fsync()
  176. *
  177. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  178. * disk, but it retrieves and clears ctx->error after synching, despite
  179. * the two being set at the same time in nfs_context_set_write_error().
  180. * This is because the former is used to notify the _next_ call to
  181. * nfs_file_write() that a write error occured, and hence cause it to
  182. * fall back to doing a synchronous write.
  183. */
  184. static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
  185. {
  186. int have_error, status;
  187. int ret = 0;
  188. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  189. status = nfs_wb_all(inode);
  190. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  191. if (have_error)
  192. ret = xchg(&ctx->error, 0);
  193. if (!ret)
  194. ret = status;
  195. return ret;
  196. }
  197. /*
  198. * Flush all dirty pages, and check for write errors.
  199. */
  200. static int
  201. nfs_file_flush(struct file *file, fl_owner_t id)
  202. {
  203. struct nfs_open_context *ctx = nfs_file_open_context(file);
  204. struct dentry *dentry = file->f_path.dentry;
  205. struct inode *inode = dentry->d_inode;
  206. dprintk("NFS: flush(%s/%s)\n",
  207. dentry->d_parent->d_name.name,
  208. dentry->d_name.name);
  209. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  210. if ((file->f_mode & FMODE_WRITE) == 0)
  211. return 0;
  212. /* Flush writes to the server and return any errors */
  213. return nfs_do_fsync(ctx, inode);
  214. }
  215. static ssize_t
  216. nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
  217. unsigned long nr_segs, loff_t pos)
  218. {
  219. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  220. struct inode * inode = dentry->d_inode;
  221. ssize_t result;
  222. size_t count = iov_length(iov, nr_segs);
  223. if (iocb->ki_filp->f_flags & O_DIRECT)
  224. return nfs_file_direct_read(iocb, iov, nr_segs, pos);
  225. dprintk("NFS: read(%s/%s, %lu@%lu)\n",
  226. dentry->d_parent->d_name.name, dentry->d_name.name,
  227. (unsigned long) count, (unsigned long) pos);
  228. result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  229. if (!result) {
  230. result = generic_file_aio_read(iocb, iov, nr_segs, pos);
  231. if (result > 0)
  232. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
  233. }
  234. return result;
  235. }
  236. static ssize_t
  237. nfs_file_splice_read(struct file *filp, loff_t *ppos,
  238. struct pipe_inode_info *pipe, size_t count,
  239. unsigned int flags)
  240. {
  241. struct dentry *dentry = filp->f_path.dentry;
  242. struct inode *inode = dentry->d_inode;
  243. ssize_t res;
  244. dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
  245. dentry->d_parent->d_name.name, dentry->d_name.name,
  246. (unsigned long) count, (unsigned long long) *ppos);
  247. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  248. if (!res) {
  249. res = generic_file_splice_read(filp, ppos, pipe, count, flags);
  250. if (res > 0)
  251. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
  252. }
  253. return res;
  254. }
  255. static int
  256. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  257. {
  258. struct dentry *dentry = file->f_path.dentry;
  259. struct inode *inode = dentry->d_inode;
  260. int status;
  261. dprintk("NFS: mmap(%s/%s)\n",
  262. dentry->d_parent->d_name.name, dentry->d_name.name);
  263. /* Note: generic_file_mmap() returns ENOSYS on nommu systems
  264. * so we call that before revalidating the mapping
  265. */
  266. status = generic_file_mmap(file, vma);
  267. if (!status) {
  268. vma->vm_ops = &nfs_file_vm_ops;
  269. status = nfs_revalidate_mapping(inode, file->f_mapping);
  270. }
  271. return status;
  272. }
  273. /*
  274. * Flush any dirty pages for this process, and check for write errors.
  275. * The return status from this call provides a reliable indication of
  276. * whether any write errors occurred for this process.
  277. */
  278. static int
  279. nfs_file_fsync(struct file *file, struct dentry *dentry, int datasync)
  280. {
  281. struct nfs_open_context *ctx = nfs_file_open_context(file);
  282. struct inode *inode = dentry->d_inode;
  283. dprintk("NFS: fsync file(%s/%s) datasync %d\n",
  284. dentry->d_parent->d_name.name, dentry->d_name.name,
  285. datasync);
  286. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  287. return nfs_do_fsync(ctx, inode);
  288. }
  289. /*
  290. * Decide whether a read/modify/write cycle may be more efficient
  291. * then a modify/write/read cycle when writing to a page in the
  292. * page cache.
  293. *
  294. * The modify/write/read cycle may occur if a page is read before
  295. * being completely filled by the writer. In this situation, the
  296. * page must be completely written to stable storage on the server
  297. * before it can be refilled by reading in the page from the server.
  298. * This can lead to expensive, small, FILE_SYNC mode writes being
  299. * done.
  300. *
  301. * It may be more efficient to read the page first if the file is
  302. * open for reading in addition to writing, the page is not marked
  303. * as Uptodate, it is not dirty or waiting to be committed,
  304. * indicating that it was previously allocated and then modified,
  305. * that there were valid bytes of data in that range of the file,
  306. * and that the new data won't completely replace the old data in
  307. * that range of the file.
  308. */
  309. static int nfs_want_read_modify_write(struct file *file, struct page *page,
  310. loff_t pos, unsigned len)
  311. {
  312. unsigned int pglen = nfs_page_length(page);
  313. unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
  314. unsigned int end = offset + len;
  315. if ((file->f_mode & FMODE_READ) && /* open for read? */
  316. !PageUptodate(page) && /* Uptodate? */
  317. !PagePrivate(page) && /* i/o request already? */
  318. pglen && /* valid bytes of file? */
  319. (end < pglen || offset)) /* replace all valid bytes? */
  320. return 1;
  321. return 0;
  322. }
  323. /*
  324. * This does the "real" work of the write. We must allocate and lock the
  325. * page to be sent back to the generic routine, which then copies the
  326. * data from user space.
  327. *
  328. * If the writer ends up delaying the write, the writer needs to
  329. * increment the page use counts until he is done with the page.
  330. */
  331. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  332. loff_t pos, unsigned len, unsigned flags,
  333. struct page **pagep, void **fsdata)
  334. {
  335. int ret;
  336. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  337. struct page *page;
  338. int once_thru = 0;
  339. dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
  340. file->f_path.dentry->d_parent->d_name.name,
  341. file->f_path.dentry->d_name.name,
  342. mapping->host->i_ino, len, (long long) pos);
  343. start:
  344. /*
  345. * Prevent starvation issues if someone is doing a consistency
  346. * sync-to-disk
  347. */
  348. ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
  349. nfs_wait_bit_killable, TASK_KILLABLE);
  350. if (ret)
  351. return ret;
  352. page = grab_cache_page_write_begin(mapping, index, flags);
  353. if (!page)
  354. return -ENOMEM;
  355. *pagep = page;
  356. ret = nfs_flush_incompatible(file, page);
  357. if (ret) {
  358. unlock_page(page);
  359. page_cache_release(page);
  360. } else if (!once_thru &&
  361. nfs_want_read_modify_write(file, page, pos, len)) {
  362. once_thru = 1;
  363. ret = nfs_readpage(file, page);
  364. page_cache_release(page);
  365. if (!ret)
  366. goto start;
  367. }
  368. return ret;
  369. }
  370. static int nfs_write_end(struct file *file, struct address_space *mapping,
  371. loff_t pos, unsigned len, unsigned copied,
  372. struct page *page, void *fsdata)
  373. {
  374. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  375. int status;
  376. dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
  377. file->f_path.dentry->d_parent->d_name.name,
  378. file->f_path.dentry->d_name.name,
  379. mapping->host->i_ino, len, (long long) pos);
  380. /*
  381. * Zero any uninitialised parts of the page, and then mark the page
  382. * as up to date if it turns out that we're extending the file.
  383. */
  384. if (!PageUptodate(page)) {
  385. unsigned pglen = nfs_page_length(page);
  386. unsigned end = offset + len;
  387. if (pglen == 0) {
  388. zero_user_segments(page, 0, offset,
  389. end, PAGE_CACHE_SIZE);
  390. SetPageUptodate(page);
  391. } else if (end >= pglen) {
  392. zero_user_segment(page, end, PAGE_CACHE_SIZE);
  393. if (offset == 0)
  394. SetPageUptodate(page);
  395. } else
  396. zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
  397. }
  398. status = nfs_updatepage(file, page, offset, copied);
  399. unlock_page(page);
  400. page_cache_release(page);
  401. if (status < 0)
  402. return status;
  403. return copied;
  404. }
  405. /*
  406. * Partially or wholly invalidate a page
  407. * - Release the private state associated with a page if undergoing complete
  408. * page invalidation
  409. * - Called if either PG_private or PG_fscache is set on the page
  410. * - Caller holds page lock
  411. */
  412. static void nfs_invalidate_page(struct page *page, unsigned long offset)
  413. {
  414. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
  415. if (offset != 0)
  416. return;
  417. /* Cancel any unstarted writes on this page */
  418. nfs_wb_page_cancel(page->mapping->host, page);
  419. nfs_fscache_invalidate_page(page, page->mapping->host);
  420. }
  421. /*
  422. * Attempt to release the private state associated with a page
  423. * - Called if either PG_private or PG_fscache is set on the page
  424. * - Caller holds page lock
  425. * - Return true (may release page) or false (may not)
  426. */
  427. static int nfs_release_page(struct page *page, gfp_t gfp)
  428. {
  429. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  430. /* Only do I/O if gfp is a superset of GFP_KERNEL */
  431. if ((gfp & GFP_KERNEL) == GFP_KERNEL)
  432. nfs_wb_page(page->mapping->host, page);
  433. /* If PagePrivate() is set, then the page is not freeable */
  434. if (PagePrivate(page))
  435. return 0;
  436. return nfs_fscache_release_page(page, gfp);
  437. }
  438. /*
  439. * Attempt to clear the private state associated with a page when an error
  440. * occurs that requires the cached contents of an inode to be written back or
  441. * destroyed
  442. * - Called if either PG_private or fscache is set on the page
  443. * - Caller holds page lock
  444. * - Return 0 if successful, -error otherwise
  445. */
  446. static int nfs_launder_page(struct page *page)
  447. {
  448. struct inode *inode = page->mapping->host;
  449. struct nfs_inode *nfsi = NFS_I(inode);
  450. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  451. inode->i_ino, (long long)page_offset(page));
  452. nfs_fscache_wait_on_page_write(nfsi, page);
  453. return nfs_wb_page(inode, page);
  454. }
  455. const struct address_space_operations nfs_file_aops = {
  456. .readpage = nfs_readpage,
  457. .readpages = nfs_readpages,
  458. .set_page_dirty = __set_page_dirty_nobuffers,
  459. .writepage = nfs_writepage,
  460. .writepages = nfs_writepages,
  461. .write_begin = nfs_write_begin,
  462. .write_end = nfs_write_end,
  463. .invalidatepage = nfs_invalidate_page,
  464. .releasepage = nfs_release_page,
  465. .direct_IO = nfs_direct_IO,
  466. .migratepage = nfs_migrate_page,
  467. .launder_page = nfs_launder_page,
  468. .error_remove_page = generic_error_remove_page,
  469. };
  470. /*
  471. * Notification that a PTE pointing to an NFS page is about to be made
  472. * writable, implying that someone is about to modify the page through a
  473. * shared-writable mapping
  474. */
  475. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  476. {
  477. struct page *page = vmf->page;
  478. struct file *filp = vma->vm_file;
  479. struct dentry *dentry = filp->f_path.dentry;
  480. unsigned pagelen;
  481. int ret = -EINVAL;
  482. struct address_space *mapping;
  483. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
  484. dentry->d_parent->d_name.name, dentry->d_name.name,
  485. filp->f_mapping->host->i_ino,
  486. (long long)page_offset(page));
  487. /* make sure the cache has finished storing the page */
  488. nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
  489. lock_page(page);
  490. mapping = page->mapping;
  491. if (mapping != dentry->d_inode->i_mapping)
  492. goto out_unlock;
  493. ret = 0;
  494. pagelen = nfs_page_length(page);
  495. if (pagelen == 0)
  496. goto out_unlock;
  497. ret = nfs_flush_incompatible(filp, page);
  498. if (ret != 0)
  499. goto out_unlock;
  500. ret = nfs_updatepage(filp, page, 0, pagelen);
  501. out_unlock:
  502. if (!ret)
  503. return VM_FAULT_LOCKED;
  504. unlock_page(page);
  505. return VM_FAULT_SIGBUS;
  506. }
  507. static const struct vm_operations_struct nfs_file_vm_ops = {
  508. .fault = filemap_fault,
  509. .page_mkwrite = nfs_vm_page_mkwrite,
  510. };
  511. static int nfs_need_sync_write(struct file *filp, struct inode *inode)
  512. {
  513. struct nfs_open_context *ctx;
  514. if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
  515. return 1;
  516. ctx = nfs_file_open_context(filp);
  517. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
  518. return 1;
  519. return 0;
  520. }
  521. static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
  522. unsigned long nr_segs, loff_t pos)
  523. {
  524. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  525. struct inode * inode = dentry->d_inode;
  526. unsigned long written = 0;
  527. ssize_t result;
  528. size_t count = iov_length(iov, nr_segs);
  529. if (iocb->ki_filp->f_flags & O_DIRECT)
  530. return nfs_file_direct_write(iocb, iov, nr_segs, pos);
  531. dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
  532. dentry->d_parent->d_name.name, dentry->d_name.name,
  533. (unsigned long) count, (long long) pos);
  534. result = -EBUSY;
  535. if (IS_SWAPFILE(inode))
  536. goto out_swapfile;
  537. /*
  538. * O_APPEND implies that we must revalidate the file length.
  539. */
  540. if (iocb->ki_filp->f_flags & O_APPEND) {
  541. result = nfs_revalidate_file_size(inode, iocb->ki_filp);
  542. if (result)
  543. goto out;
  544. }
  545. result = count;
  546. if (!count)
  547. goto out;
  548. result = generic_file_aio_write(iocb, iov, nr_segs, pos);
  549. if (result > 0)
  550. written = result;
  551. /* Return error values for O_DSYNC and IS_SYNC() */
  552. if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
  553. int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
  554. if (err < 0)
  555. result = err;
  556. }
  557. if (result > 0)
  558. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  559. out:
  560. return result;
  561. out_swapfile:
  562. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  563. goto out;
  564. }
  565. static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
  566. struct file *filp, loff_t *ppos,
  567. size_t count, unsigned int flags)
  568. {
  569. struct dentry *dentry = filp->f_path.dentry;
  570. struct inode *inode = dentry->d_inode;
  571. unsigned long written = 0;
  572. ssize_t ret;
  573. dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
  574. dentry->d_parent->d_name.name, dentry->d_name.name,
  575. (unsigned long) count, (unsigned long long) *ppos);
  576. /*
  577. * The combination of splice and an O_APPEND destination is disallowed.
  578. */
  579. ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
  580. if (ret > 0)
  581. written = ret;
  582. if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
  583. int err = nfs_do_fsync(nfs_file_open_context(filp), inode);
  584. if (err < 0)
  585. ret = err;
  586. }
  587. if (ret > 0)
  588. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  589. return ret;
  590. }
  591. static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
  592. {
  593. struct inode *inode = filp->f_mapping->host;
  594. int status = 0;
  595. /* Try local locking first */
  596. posix_test_lock(filp, fl);
  597. if (fl->fl_type != F_UNLCK) {
  598. /* found a conflict */
  599. goto out;
  600. }
  601. if (nfs_have_delegation(inode, FMODE_READ))
  602. goto out_noconflict;
  603. if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
  604. goto out_noconflict;
  605. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  606. out:
  607. return status;
  608. out_noconflict:
  609. fl->fl_type = F_UNLCK;
  610. goto out;
  611. }
  612. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  613. {
  614. int res = 0;
  615. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  616. case FL_POSIX:
  617. res = posix_lock_file_wait(file, fl);
  618. break;
  619. case FL_FLOCK:
  620. res = flock_lock_file_wait(file, fl);
  621. break;
  622. default:
  623. BUG();
  624. }
  625. if (res < 0)
  626. dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
  627. " - error %d!\n",
  628. __func__, res);
  629. return res;
  630. }
  631. static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
  632. {
  633. struct inode *inode = filp->f_mapping->host;
  634. int status;
  635. /*
  636. * Flush all pending writes before doing anything
  637. * with locks..
  638. */
  639. nfs_sync_mapping(filp->f_mapping);
  640. /* NOTE: special case
  641. * If we're signalled while cleaning up locks on process exit, we
  642. * still need to complete the unlock.
  643. */
  644. /* Use local locking if mounted with "-onolock" */
  645. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  646. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  647. else
  648. status = do_vfs_lock(filp, fl);
  649. return status;
  650. }
  651. static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
  652. {
  653. struct inode *inode = filp->f_mapping->host;
  654. int status;
  655. /*
  656. * Flush all pending writes before doing anything
  657. * with locks..
  658. */
  659. status = nfs_sync_mapping(filp->f_mapping);
  660. if (status != 0)
  661. goto out;
  662. /* Use local locking if mounted with "-onolock" */
  663. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  664. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  665. else
  666. status = do_vfs_lock(filp, fl);
  667. if (status < 0)
  668. goto out;
  669. /*
  670. * Make sure we clear the cache whenever we try to get the lock.
  671. * This makes locking act as a cache coherency point.
  672. */
  673. nfs_sync_mapping(filp->f_mapping);
  674. if (!nfs_have_delegation(inode, FMODE_READ))
  675. nfs_zap_caches(inode);
  676. out:
  677. return status;
  678. }
  679. /*
  680. * Lock a (portion of) a file
  681. */
  682. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  683. {
  684. struct inode *inode = filp->f_mapping->host;
  685. int ret = -ENOLCK;
  686. dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
  687. filp->f_path.dentry->d_parent->d_name.name,
  688. filp->f_path.dentry->d_name.name,
  689. fl->fl_type, fl->fl_flags,
  690. (long long)fl->fl_start, (long long)fl->fl_end);
  691. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  692. /* No mandatory locks over NFS */
  693. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  694. goto out_err;
  695. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  696. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  697. if (ret < 0)
  698. goto out_err;
  699. }
  700. if (IS_GETLK(cmd))
  701. ret = do_getlk(filp, cmd, fl);
  702. else if (fl->fl_type == F_UNLCK)
  703. ret = do_unlk(filp, cmd, fl);
  704. else
  705. ret = do_setlk(filp, cmd, fl);
  706. out_err:
  707. return ret;
  708. }
  709. /*
  710. * Lock a (portion of) a file
  711. */
  712. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  713. {
  714. dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
  715. filp->f_path.dentry->d_parent->d_name.name,
  716. filp->f_path.dentry->d_name.name,
  717. fl->fl_type, fl->fl_flags);
  718. if (!(fl->fl_flags & FL_FLOCK))
  719. return -ENOLCK;
  720. /* We're simulating flock() locks using posix locks on the server */
  721. fl->fl_owner = (fl_owner_t)filp;
  722. fl->fl_start = 0;
  723. fl->fl_end = OFFSET_MAX;
  724. if (fl->fl_type == F_UNLCK)
  725. return do_unlk(filp, cmd, fl);
  726. return do_setlk(filp, cmd, fl);
  727. }
  728. /*
  729. * There is no protocol support for leases, so we have no way to implement
  730. * them correctly in the face of opens by other clients.
  731. */
  732. static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
  733. {
  734. dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
  735. file->f_path.dentry->d_parent->d_name.name,
  736. file->f_path.dentry->d_name.name, arg);
  737. return -EINVAL;
  738. }