file.c 22 KB

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