file.c 22 KB

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