file.c 25 KB

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