file.c 23 KB

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