file.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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/module.h>
  19. #include <linux/time.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/stat.h>
  24. #include <linux/nfs_fs.h>
  25. #include <linux/nfs_mount.h>
  26. #include <linux/mm.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/gfp.h>
  29. #include <linux/swap.h>
  30. #include <asm/uaccess.h>
  31. #include "delegation.h"
  32. #include "internal.h"
  33. #include "iostat.h"
  34. #include "fscache.h"
  35. #include "pnfs.h"
  36. #include "nfstrace.h"
  37. #define NFSDBG_FACILITY NFSDBG_FILE
  38. static const struct vm_operations_struct nfs_file_vm_ops;
  39. /* Hack for future NFS swap support */
  40. #ifndef IS_SWAPFILE
  41. # define IS_SWAPFILE(inode) (0)
  42. #endif
  43. int nfs_check_flags(int flags)
  44. {
  45. if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
  46. return -EINVAL;
  47. return 0;
  48. }
  49. EXPORT_SYMBOL_GPL(nfs_check_flags);
  50. /*
  51. * Open file
  52. */
  53. static int
  54. nfs_file_open(struct inode *inode, struct file *filp)
  55. {
  56. int res;
  57. dprintk("NFS: open file(%pD2)\n", filp);
  58. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  59. res = nfs_check_flags(filp->f_flags);
  60. if (res)
  61. return res;
  62. res = nfs_open(inode, filp);
  63. return res;
  64. }
  65. int
  66. nfs_file_release(struct inode *inode, struct file *filp)
  67. {
  68. dprintk("NFS: release(%pD2)\n", filp);
  69. nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
  70. nfs_file_clear_open_context(filp);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL_GPL(nfs_file_release);
  74. /**
  75. * nfs_revalidate_size - Revalidate the file size
  76. * @inode - pointer to inode struct
  77. * @file - pointer to struct file
  78. *
  79. * Revalidates the file length. This is basically a wrapper around
  80. * nfs_revalidate_inode() that takes into account the fact that we may
  81. * have cached writes (in which case we don't care about the server's
  82. * idea of what the file length is), or O_DIRECT (in which case we
  83. * shouldn't trust the cache).
  84. */
  85. static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
  86. {
  87. struct nfs_server *server = NFS_SERVER(inode);
  88. struct nfs_inode *nfsi = NFS_I(inode);
  89. if (nfs_have_delegated_attributes(inode))
  90. goto out_noreval;
  91. if (filp->f_flags & O_DIRECT)
  92. goto force_reval;
  93. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  94. goto force_reval;
  95. if (nfs_attribute_timeout(inode))
  96. goto force_reval;
  97. out_noreval:
  98. return 0;
  99. force_reval:
  100. return __nfs_revalidate_inode(server, inode);
  101. }
  102. loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
  103. {
  104. dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
  105. filp, offset, whence);
  106. /*
  107. * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
  108. * the cached file length
  109. */
  110. if (whence != SEEK_SET && whence != SEEK_CUR) {
  111. struct inode *inode = filp->f_mapping->host;
  112. int retval = nfs_revalidate_file_size(inode, filp);
  113. if (retval < 0)
  114. return (loff_t)retval;
  115. }
  116. return generic_file_llseek(filp, offset, whence);
  117. }
  118. EXPORT_SYMBOL_GPL(nfs_file_llseek);
  119. /*
  120. * Flush all dirty pages, and check for write errors.
  121. */
  122. static int
  123. nfs_file_flush(struct file *file, fl_owner_t id)
  124. {
  125. struct inode *inode = file_inode(file);
  126. dprintk("NFS: flush(%pD2)\n", file);
  127. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  128. if ((file->f_mode & FMODE_WRITE) == 0)
  129. return 0;
  130. /* Flush writes to the server and return any errors */
  131. return vfs_fsync(file, 0);
  132. }
  133. ssize_t
  134. nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
  135. {
  136. struct inode *inode = file_inode(iocb->ki_filp);
  137. ssize_t result;
  138. if (iocb->ki_flags & IOCB_DIRECT)
  139. return nfs_file_direct_read(iocb, to);
  140. dprintk("NFS: read(%pD2, %zu@%lu)\n",
  141. iocb->ki_filp,
  142. iov_iter_count(to), (unsigned long) iocb->ki_pos);
  143. nfs_start_io_read(inode);
  144. result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  145. if (!result) {
  146. result = generic_file_read_iter(iocb, to);
  147. if (result > 0)
  148. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
  149. }
  150. nfs_end_io_read(inode);
  151. return result;
  152. }
  153. EXPORT_SYMBOL_GPL(nfs_file_read);
  154. ssize_t
  155. nfs_file_splice_read(struct file *filp, loff_t *ppos,
  156. struct pipe_inode_info *pipe, size_t count,
  157. unsigned int flags)
  158. {
  159. struct inode *inode = file_inode(filp);
  160. ssize_t res;
  161. dprintk("NFS: splice_read(%pD2, %lu@%Lu)\n",
  162. filp, (unsigned long) count, (unsigned long long) *ppos);
  163. nfs_start_io_read(inode);
  164. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  165. if (!res) {
  166. res = generic_file_splice_read(filp, ppos, pipe, count, flags);
  167. if (res > 0)
  168. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
  169. }
  170. nfs_end_io_read(inode);
  171. return res;
  172. }
  173. EXPORT_SYMBOL_GPL(nfs_file_splice_read);
  174. int
  175. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  176. {
  177. struct inode *inode = file_inode(file);
  178. int status;
  179. dprintk("NFS: mmap(%pD2)\n", file);
  180. /* Note: generic_file_mmap() returns ENOSYS on nommu systems
  181. * so we call that before revalidating the mapping
  182. */
  183. status = generic_file_mmap(file, vma);
  184. if (!status) {
  185. vma->vm_ops = &nfs_file_vm_ops;
  186. status = nfs_revalidate_mapping(inode, file->f_mapping);
  187. }
  188. return status;
  189. }
  190. EXPORT_SYMBOL_GPL(nfs_file_mmap);
  191. /*
  192. * Flush any dirty pages for this process, and check for write errors.
  193. * The return status from this call provides a reliable indication of
  194. * whether any write errors occurred for this process.
  195. *
  196. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  197. * disk, but it retrieves and clears ctx->error after synching, despite
  198. * the two being set at the same time in nfs_context_set_write_error().
  199. * This is because the former is used to notify the _next_ call to
  200. * nfs_file_write() that a write error occurred, and hence cause it to
  201. * fall back to doing a synchronous write.
  202. */
  203. static int
  204. nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
  205. {
  206. struct nfs_open_context *ctx = nfs_file_open_context(file);
  207. struct inode *inode = file_inode(file);
  208. int have_error, do_resend, status;
  209. int ret = 0;
  210. dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
  211. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  212. do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  213. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  214. status = nfs_commit_inode(inode, FLUSH_SYNC);
  215. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  216. if (have_error) {
  217. ret = xchg(&ctx->error, 0);
  218. if (ret)
  219. goto out;
  220. }
  221. if (status < 0) {
  222. ret = status;
  223. goto out;
  224. }
  225. do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  226. if (do_resend)
  227. ret = -EAGAIN;
  228. out:
  229. return ret;
  230. }
  231. int
  232. nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  233. {
  234. int ret;
  235. struct inode *inode = file_inode(file);
  236. trace_nfs_fsync_enter(inode);
  237. do {
  238. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  239. if (ret != 0)
  240. break;
  241. ret = nfs_file_fsync_commit(file, start, end, datasync);
  242. if (!ret)
  243. ret = pnfs_sync_inode(inode, !!datasync);
  244. /*
  245. * If nfs_file_fsync_commit detected a server reboot, then
  246. * resend all dirty pages that might have been covered by
  247. * the NFS_CONTEXT_RESEND_WRITES flag
  248. */
  249. start = 0;
  250. end = LLONG_MAX;
  251. } while (ret == -EAGAIN);
  252. trace_nfs_fsync_exit(inode, ret);
  253. return ret;
  254. }
  255. EXPORT_SYMBOL_GPL(nfs_file_fsync);
  256. /*
  257. * Decide whether a read/modify/write cycle may be more efficient
  258. * then a modify/write/read cycle when writing to a page in the
  259. * page cache.
  260. *
  261. * The modify/write/read cycle may occur if a page is read before
  262. * being completely filled by the writer. In this situation, the
  263. * page must be completely written to stable storage on the server
  264. * before it can be refilled by reading in the page from the server.
  265. * This can lead to expensive, small, FILE_SYNC mode writes being
  266. * done.
  267. *
  268. * It may be more efficient to read the page first if the file is
  269. * open for reading in addition to writing, the page is not marked
  270. * as Uptodate, it is not dirty or waiting to be committed,
  271. * indicating that it was previously allocated and then modified,
  272. * that there were valid bytes of data in that range of the file,
  273. * and that the new data won't completely replace the old data in
  274. * that range of the file.
  275. */
  276. static int nfs_want_read_modify_write(struct file *file, struct page *page,
  277. loff_t pos, unsigned len)
  278. {
  279. unsigned int pglen = nfs_page_length(page);
  280. unsigned int offset = pos & (PAGE_SIZE - 1);
  281. unsigned int end = offset + len;
  282. if (pnfs_ld_read_whole_page(file->f_mapping->host)) {
  283. if (!PageUptodate(page))
  284. return 1;
  285. return 0;
  286. }
  287. if ((file->f_mode & FMODE_READ) && /* open for read? */
  288. !PageUptodate(page) && /* Uptodate? */
  289. !PagePrivate(page) && /* i/o request already? */
  290. pglen && /* valid bytes of file? */
  291. (end < pglen || offset)) /* replace all valid bytes? */
  292. return 1;
  293. return 0;
  294. }
  295. /*
  296. * This does the "real" work of the write. We must allocate and lock the
  297. * page to be sent back to the generic routine, which then copies the
  298. * data from user space.
  299. *
  300. * If the writer ends up delaying the write, the writer needs to
  301. * increment the page use counts until he is done with the page.
  302. */
  303. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  304. loff_t pos, unsigned len, unsigned flags,
  305. struct page **pagep, void **fsdata)
  306. {
  307. int ret;
  308. pgoff_t index = pos >> PAGE_SHIFT;
  309. struct page *page;
  310. int once_thru = 0;
  311. dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
  312. file, mapping->host->i_ino, len, (long long) pos);
  313. start:
  314. page = grab_cache_page_write_begin(mapping, index, flags);
  315. if (!page)
  316. return -ENOMEM;
  317. *pagep = page;
  318. ret = nfs_flush_incompatible(file, page);
  319. if (ret) {
  320. unlock_page(page);
  321. put_page(page);
  322. } else if (!once_thru &&
  323. nfs_want_read_modify_write(file, page, pos, len)) {
  324. once_thru = 1;
  325. ret = nfs_readpage(file, page);
  326. put_page(page);
  327. if (!ret)
  328. goto start;
  329. }
  330. return ret;
  331. }
  332. static int nfs_write_end(struct file *file, struct address_space *mapping,
  333. loff_t pos, unsigned len, unsigned copied,
  334. struct page *page, void *fsdata)
  335. {
  336. unsigned offset = pos & (PAGE_SIZE - 1);
  337. struct nfs_open_context *ctx = nfs_file_open_context(file);
  338. int status;
  339. dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
  340. file, mapping->host->i_ino, len, (long long) pos);
  341. /*
  342. * Zero any uninitialised parts of the page, and then mark the page
  343. * as up to date if it turns out that we're extending the file.
  344. */
  345. if (!PageUptodate(page)) {
  346. unsigned pglen = nfs_page_length(page);
  347. unsigned end = offset + len;
  348. if (pglen == 0) {
  349. zero_user_segments(page, 0, offset,
  350. end, PAGE_SIZE);
  351. SetPageUptodate(page);
  352. } else if (end >= pglen) {
  353. zero_user_segment(page, end, PAGE_SIZE);
  354. if (offset == 0)
  355. SetPageUptodate(page);
  356. } else
  357. zero_user_segment(page, pglen, PAGE_SIZE);
  358. }
  359. status = nfs_updatepage(file, page, offset, copied);
  360. unlock_page(page);
  361. put_page(page);
  362. if (status < 0)
  363. return status;
  364. NFS_I(mapping->host)->write_io += copied;
  365. if (nfs_ctx_key_to_expire(ctx, mapping->host)) {
  366. status = nfs_wb_all(mapping->host);
  367. if (status < 0)
  368. return status;
  369. }
  370. return copied;
  371. }
  372. /*
  373. * Partially or wholly invalidate a page
  374. * - Release the private state associated with a page if undergoing complete
  375. * page invalidation
  376. * - Called if either PG_private or PG_fscache is set on the page
  377. * - Caller holds page lock
  378. */
  379. static void nfs_invalidate_page(struct page *page, unsigned int offset,
  380. unsigned int length)
  381. {
  382. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
  383. page, offset, length);
  384. if (offset != 0 || length < PAGE_SIZE)
  385. return;
  386. /* Cancel any unstarted writes on this page */
  387. nfs_wb_page_cancel(page_file_mapping(page)->host, page);
  388. nfs_fscache_invalidate_page(page, page->mapping->host);
  389. }
  390. /*
  391. * Attempt to release the private state associated with a page
  392. * - Called if either PG_private or PG_fscache is set on the page
  393. * - Caller holds page lock
  394. * - Return true (may release page) or false (may not)
  395. */
  396. static int nfs_release_page(struct page *page, gfp_t gfp)
  397. {
  398. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  399. /* If PagePrivate() is set, then the page is not freeable */
  400. if (PagePrivate(page))
  401. return 0;
  402. return nfs_fscache_release_page(page, gfp);
  403. }
  404. static void nfs_check_dirty_writeback(struct page *page,
  405. bool *dirty, bool *writeback)
  406. {
  407. struct nfs_inode *nfsi;
  408. struct address_space *mapping = page_file_mapping(page);
  409. if (!mapping || PageSwapCache(page))
  410. return;
  411. /*
  412. * Check if an unstable page is currently being committed and
  413. * if so, have the VM treat it as if the page is under writeback
  414. * so it will not block due to pages that will shortly be freeable.
  415. */
  416. nfsi = NFS_I(mapping->host);
  417. if (atomic_read(&nfsi->commit_info.rpcs_out)) {
  418. *writeback = true;
  419. return;
  420. }
  421. /*
  422. * If PagePrivate() is set, then the page is not freeable and as the
  423. * inode is not being committed, it's not going to be cleaned in the
  424. * near future so treat it as dirty
  425. */
  426. if (PagePrivate(page))
  427. *dirty = true;
  428. }
  429. /*
  430. * Attempt to clear the private state associated with a page when an error
  431. * occurs that requires the cached contents of an inode to be written back or
  432. * destroyed
  433. * - Called if either PG_private or fscache is set on the page
  434. * - Caller holds page lock
  435. * - Return 0 if successful, -error otherwise
  436. */
  437. static int nfs_launder_page(struct page *page)
  438. {
  439. struct inode *inode = page_file_mapping(page)->host;
  440. struct nfs_inode *nfsi = NFS_I(inode);
  441. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  442. inode->i_ino, (long long)page_offset(page));
  443. nfs_fscache_wait_on_page_write(nfsi, page);
  444. return nfs_wb_launder_page(inode, page);
  445. }
  446. static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
  447. sector_t *span)
  448. {
  449. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  450. *span = sis->pages;
  451. return rpc_clnt_swap_activate(clnt);
  452. }
  453. static void nfs_swap_deactivate(struct file *file)
  454. {
  455. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  456. rpc_clnt_swap_deactivate(clnt);
  457. }
  458. const struct address_space_operations nfs_file_aops = {
  459. .readpage = nfs_readpage,
  460. .readpages = nfs_readpages,
  461. .set_page_dirty = __set_page_dirty_nobuffers,
  462. .writepage = nfs_writepage,
  463. .writepages = nfs_writepages,
  464. .write_begin = nfs_write_begin,
  465. .write_end = nfs_write_end,
  466. .invalidatepage = nfs_invalidate_page,
  467. .releasepage = nfs_release_page,
  468. .direct_IO = nfs_direct_IO,
  469. .migratepage = nfs_migrate_page,
  470. .launder_page = nfs_launder_page,
  471. .is_dirty_writeback = nfs_check_dirty_writeback,
  472. .error_remove_page = generic_error_remove_page,
  473. .swap_activate = nfs_swap_activate,
  474. .swap_deactivate = nfs_swap_deactivate,
  475. };
  476. /*
  477. * Notification that a PTE pointing to an NFS page is about to be made
  478. * writable, implying that someone is about to modify the page through a
  479. * shared-writable mapping
  480. */
  481. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  482. {
  483. struct page *page = vmf->page;
  484. struct file *filp = vma->vm_file;
  485. struct inode *inode = file_inode(filp);
  486. unsigned pagelen;
  487. int ret = VM_FAULT_NOPAGE;
  488. struct address_space *mapping;
  489. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
  490. filp, filp->f_mapping->host->i_ino,
  491. (long long)page_offset(page));
  492. sb_start_pagefault(inode->i_sb);
  493. /* make sure the cache has finished storing the page */
  494. nfs_fscache_wait_on_page_write(NFS_I(inode), page);
  495. wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
  496. nfs_wait_bit_killable, TASK_KILLABLE);
  497. lock_page(page);
  498. mapping = page_file_mapping(page);
  499. if (mapping != inode->i_mapping)
  500. goto out_unlock;
  501. wait_on_page_writeback(page);
  502. pagelen = nfs_page_length(page);
  503. if (pagelen == 0)
  504. goto out_unlock;
  505. ret = VM_FAULT_LOCKED;
  506. if (nfs_flush_incompatible(filp, page) == 0 &&
  507. nfs_updatepage(filp, page, 0, pagelen) == 0)
  508. goto out;
  509. ret = VM_FAULT_SIGBUS;
  510. out_unlock:
  511. unlock_page(page);
  512. out:
  513. sb_end_pagefault(inode->i_sb);
  514. return ret;
  515. }
  516. static const struct vm_operations_struct nfs_file_vm_ops = {
  517. .fault = filemap_fault,
  518. .map_pages = filemap_map_pages,
  519. .page_mkwrite = nfs_vm_page_mkwrite,
  520. };
  521. static int nfs_need_check_write(struct file *filp, struct inode *inode)
  522. {
  523. struct nfs_open_context *ctx;
  524. ctx = nfs_file_open_context(filp);
  525. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
  526. nfs_ctx_key_to_expire(ctx, inode))
  527. return 1;
  528. return 0;
  529. }
  530. ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
  531. {
  532. struct file *file = iocb->ki_filp;
  533. struct inode *inode = file_inode(file);
  534. unsigned long written = 0;
  535. ssize_t result;
  536. result = nfs_key_timeout_notify(file, inode);
  537. if (result)
  538. return result;
  539. if (iocb->ki_flags & IOCB_DIRECT)
  540. return nfs_file_direct_write(iocb, from);
  541. dprintk("NFS: write(%pD2, %zu@%Ld)\n",
  542. file, iov_iter_count(from), (long long) iocb->ki_pos);
  543. if (IS_SWAPFILE(inode))
  544. goto out_swapfile;
  545. /*
  546. * O_APPEND implies that we must revalidate the file length.
  547. */
  548. if (iocb->ki_flags & IOCB_APPEND) {
  549. result = nfs_revalidate_file_size(inode, file);
  550. if (result)
  551. goto out;
  552. }
  553. nfs_start_io_write(inode);
  554. result = generic_write_checks(iocb, from);
  555. if (result > 0) {
  556. current->backing_dev_info = inode_to_bdi(inode);
  557. result = generic_perform_write(file, from, iocb->ki_pos);
  558. current->backing_dev_info = NULL;
  559. }
  560. nfs_end_io_write(inode);
  561. if (result <= 0)
  562. goto out;
  563. written = generic_write_sync(iocb, result);
  564. iocb->ki_pos += written;
  565. /* Return error values */
  566. if (nfs_need_check_write(file, inode)) {
  567. int err = vfs_fsync(file, 0);
  568. if (err < 0)
  569. result = err;
  570. }
  571. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  572. out:
  573. return result;
  574. out_swapfile:
  575. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  576. return -EBUSY;
  577. }
  578. EXPORT_SYMBOL_GPL(nfs_file_write);
  579. static int
  580. do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  581. {
  582. struct inode *inode = filp->f_mapping->host;
  583. int status = 0;
  584. unsigned int saved_type = fl->fl_type;
  585. /* Try local locking first */
  586. posix_test_lock(filp, fl);
  587. if (fl->fl_type != F_UNLCK) {
  588. /* found a conflict */
  589. goto out;
  590. }
  591. fl->fl_type = saved_type;
  592. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  593. goto out_noconflict;
  594. if (is_local)
  595. goto out_noconflict;
  596. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  597. out:
  598. return status;
  599. out_noconflict:
  600. fl->fl_type = F_UNLCK;
  601. goto out;
  602. }
  603. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  604. {
  605. return locks_lock_file_wait(file, fl);
  606. }
  607. static int
  608. do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  609. {
  610. struct inode *inode = filp->f_mapping->host;
  611. struct nfs_lock_context *l_ctx;
  612. int status;
  613. /*
  614. * Flush all pending writes before doing anything
  615. * with locks..
  616. */
  617. vfs_fsync(filp, 0);
  618. l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
  619. if (!IS_ERR(l_ctx)) {
  620. status = nfs_iocounter_wait(l_ctx);
  621. nfs_put_lock_context(l_ctx);
  622. if (status < 0)
  623. return status;
  624. }
  625. /* NOTE: special case
  626. * If we're signalled while cleaning up locks on process exit, we
  627. * still need to complete the unlock.
  628. */
  629. /*
  630. * Use local locking if mounted with "-onolock" or with appropriate
  631. * "-olocal_lock="
  632. */
  633. if (!is_local)
  634. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  635. else
  636. status = do_vfs_lock(filp, fl);
  637. return status;
  638. }
  639. static int
  640. do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  641. {
  642. struct inode *inode = filp->f_mapping->host;
  643. int status;
  644. /*
  645. * Flush all pending writes before doing anything
  646. * with locks..
  647. */
  648. status = nfs_sync_mapping(filp->f_mapping);
  649. if (status != 0)
  650. goto out;
  651. /*
  652. * Use local locking if mounted with "-onolock" or with appropriate
  653. * "-olocal_lock="
  654. */
  655. if (!is_local)
  656. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  657. else
  658. status = do_vfs_lock(filp, fl);
  659. if (status < 0)
  660. goto out;
  661. /*
  662. * Revalidate the cache if the server has time stamps granular
  663. * enough to detect subsecond changes. Otherwise, clear the
  664. * cache to prevent missing any changes.
  665. *
  666. * This makes locking act as a cache coherency point.
  667. */
  668. nfs_sync_mapping(filp->f_mapping);
  669. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  670. nfs_zap_mapping(inode, filp->f_mapping);
  671. out:
  672. return status;
  673. }
  674. /*
  675. * Lock a (portion of) a file
  676. */
  677. int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  678. {
  679. struct inode *inode = filp->f_mapping->host;
  680. int ret = -ENOLCK;
  681. int is_local = 0;
  682. dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
  683. filp, fl->fl_type, fl->fl_flags,
  684. (long long)fl->fl_start, (long long)fl->fl_end);
  685. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  686. /* No mandatory locks over NFS */
  687. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  688. goto out_err;
  689. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
  690. is_local = 1;
  691. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  692. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  693. if (ret < 0)
  694. goto out_err;
  695. }
  696. if (IS_GETLK(cmd))
  697. ret = do_getlk(filp, cmd, fl, is_local);
  698. else if (fl->fl_type == F_UNLCK)
  699. ret = do_unlk(filp, cmd, fl, is_local);
  700. else
  701. ret = do_setlk(filp, cmd, fl, is_local);
  702. out_err:
  703. return ret;
  704. }
  705. EXPORT_SYMBOL_GPL(nfs_lock);
  706. /*
  707. * Lock a (portion of) a file
  708. */
  709. int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  710. {
  711. struct inode *inode = filp->f_mapping->host;
  712. int is_local = 0;
  713. dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
  714. filp, fl->fl_type, fl->fl_flags);
  715. if (!(fl->fl_flags & FL_FLOCK))
  716. return -ENOLCK;
  717. /*
  718. * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
  719. * any standard. In principle we might be able to support LOCK_MAND
  720. * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
  721. * NFS code is not set up for it.
  722. */
  723. if (fl->fl_type & LOCK_MAND)
  724. return -EINVAL;
  725. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
  726. is_local = 1;
  727. /* We're simulating flock() locks using posix locks on the server */
  728. if (fl->fl_type == F_UNLCK)
  729. return do_unlk(filp, cmd, fl, is_local);
  730. return do_setlk(filp, cmd, fl, is_local);
  731. }
  732. EXPORT_SYMBOL_GPL(nfs_flock);
  733. const struct file_operations nfs_file_operations = {
  734. .llseek = nfs_file_llseek,
  735. .read_iter = nfs_file_read,
  736. .write_iter = nfs_file_write,
  737. .mmap = nfs_file_mmap,
  738. .open = nfs_file_open,
  739. .flush = nfs_file_flush,
  740. .release = nfs_file_release,
  741. .fsync = nfs_file_fsync,
  742. .lock = nfs_lock,
  743. .flock = nfs_flock,
  744. .splice_read = nfs_file_splice_read,
  745. .splice_write = iter_file_splice_write,
  746. .check_flags = nfs_check_flags,
  747. .setlease = simple_nosetlease,
  748. };
  749. EXPORT_SYMBOL_GPL(nfs_file_operations);