file.c 22 KB

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