file.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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, int datasync)
  179. {
  180. struct nfs_open_context *ctx = nfs_file_open_context(file);
  181. struct inode *inode = file_inode(file);
  182. int 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. status = nfs_commit_inode(inode, FLUSH_SYNC);
  188. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) {
  189. ret = xchg(&ctx->error, 0);
  190. if (ret)
  191. goto out;
  192. }
  193. if (status < 0) {
  194. ret = status;
  195. goto out;
  196. }
  197. do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  198. if (do_resend)
  199. ret = -EAGAIN;
  200. out:
  201. return ret;
  202. }
  203. int
  204. nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  205. {
  206. int ret;
  207. struct inode *inode = file_inode(file);
  208. trace_nfs_fsync_enter(inode);
  209. do {
  210. struct nfs_open_context *ctx = nfs_file_open_context(file);
  211. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  212. if (test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags)) {
  213. int ret2 = xchg(&ctx->error, 0);
  214. if (ret2)
  215. ret = ret2;
  216. }
  217. if (ret != 0)
  218. break;
  219. ret = nfs_file_fsync_commit(file, datasync);
  220. if (!ret)
  221. ret = pnfs_sync_inode(inode, !!datasync);
  222. /*
  223. * If nfs_file_fsync_commit detected a server reboot, then
  224. * resend all dirty pages that might have been covered by
  225. * the NFS_CONTEXT_RESEND_WRITES flag
  226. */
  227. start = 0;
  228. end = LLONG_MAX;
  229. } while (ret == -EAGAIN);
  230. trace_nfs_fsync_exit(inode, ret);
  231. return ret;
  232. }
  233. EXPORT_SYMBOL_GPL(nfs_file_fsync);
  234. /*
  235. * Decide whether a read/modify/write cycle may be more efficient
  236. * then a modify/write/read cycle when writing to a page in the
  237. * page cache.
  238. *
  239. * The modify/write/read cycle may occur if a page is read before
  240. * being completely filled by the writer. In this situation, the
  241. * page must be completely written to stable storage on the server
  242. * before it can be refilled by reading in the page from the server.
  243. * This can lead to expensive, small, FILE_SYNC mode writes being
  244. * done.
  245. *
  246. * It may be more efficient to read the page first if the file is
  247. * open for reading in addition to writing, the page is not marked
  248. * as Uptodate, it is not dirty or waiting to be committed,
  249. * indicating that it was previously allocated and then modified,
  250. * that there were valid bytes of data in that range of the file,
  251. * and that the new data won't completely replace the old data in
  252. * that range of the file.
  253. */
  254. static int nfs_want_read_modify_write(struct file *file, struct page *page,
  255. loff_t pos, unsigned len)
  256. {
  257. unsigned int pglen = nfs_page_length(page);
  258. unsigned int offset = pos & (PAGE_SIZE - 1);
  259. unsigned int end = offset + len;
  260. if (pnfs_ld_read_whole_page(file->f_mapping->host)) {
  261. if (!PageUptodate(page))
  262. return 1;
  263. return 0;
  264. }
  265. if ((file->f_mode & FMODE_READ) && /* open for read? */
  266. !PageUptodate(page) && /* Uptodate? */
  267. !PagePrivate(page) && /* i/o request already? */
  268. pglen && /* valid bytes of file? */
  269. (end < pglen || offset)) /* replace all valid bytes? */
  270. return 1;
  271. return 0;
  272. }
  273. /*
  274. * This does the "real" work of the write. We must allocate and lock the
  275. * page to be sent back to the generic routine, which then copies the
  276. * data from user space.
  277. *
  278. * If the writer ends up delaying the write, the writer needs to
  279. * increment the page use counts until he is done with the page.
  280. */
  281. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  282. loff_t pos, unsigned len, unsigned flags,
  283. struct page **pagep, void **fsdata)
  284. {
  285. int ret;
  286. pgoff_t index = pos >> PAGE_SHIFT;
  287. struct page *page;
  288. int once_thru = 0;
  289. dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
  290. file, mapping->host->i_ino, len, (long long) pos);
  291. start:
  292. page = grab_cache_page_write_begin(mapping, index, flags);
  293. if (!page)
  294. return -ENOMEM;
  295. *pagep = page;
  296. ret = nfs_flush_incompatible(file, page);
  297. if (ret) {
  298. unlock_page(page);
  299. put_page(page);
  300. } else if (!once_thru &&
  301. nfs_want_read_modify_write(file, page, pos, len)) {
  302. once_thru = 1;
  303. ret = nfs_readpage(file, page);
  304. put_page(page);
  305. if (!ret)
  306. goto start;
  307. }
  308. return ret;
  309. }
  310. static int nfs_write_end(struct file *file, struct address_space *mapping,
  311. loff_t pos, unsigned len, unsigned copied,
  312. struct page *page, void *fsdata)
  313. {
  314. unsigned offset = pos & (PAGE_SIZE - 1);
  315. struct nfs_open_context *ctx = nfs_file_open_context(file);
  316. int status;
  317. dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
  318. file, mapping->host->i_ino, len, (long long) pos);
  319. /*
  320. * Zero any uninitialised parts of the page, and then mark the page
  321. * as up to date if it turns out that we're extending the file.
  322. */
  323. if (!PageUptodate(page)) {
  324. unsigned pglen = nfs_page_length(page);
  325. unsigned end = offset + copied;
  326. if (pglen == 0) {
  327. zero_user_segments(page, 0, offset,
  328. end, PAGE_SIZE);
  329. SetPageUptodate(page);
  330. } else if (end >= pglen) {
  331. zero_user_segment(page, end, PAGE_SIZE);
  332. if (offset == 0)
  333. SetPageUptodate(page);
  334. } else
  335. zero_user_segment(page, pglen, PAGE_SIZE);
  336. }
  337. status = nfs_updatepage(file, page, offset, copied);
  338. unlock_page(page);
  339. put_page(page);
  340. if (status < 0)
  341. return status;
  342. NFS_I(mapping->host)->write_io += copied;
  343. if (nfs_ctx_key_to_expire(ctx, mapping->host)) {
  344. status = nfs_wb_all(mapping->host);
  345. if (status < 0)
  346. return status;
  347. }
  348. return copied;
  349. }
  350. /*
  351. * Partially or wholly invalidate a page
  352. * - Release the private state associated with a page if undergoing complete
  353. * page invalidation
  354. * - Called if either PG_private or PG_fscache is set on the page
  355. * - Caller holds page lock
  356. */
  357. static void nfs_invalidate_page(struct page *page, unsigned int offset,
  358. unsigned int length)
  359. {
  360. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
  361. page, offset, length);
  362. if (offset != 0 || length < PAGE_SIZE)
  363. return;
  364. /* Cancel any unstarted writes on this page */
  365. nfs_wb_page_cancel(page_file_mapping(page)->host, page);
  366. nfs_fscache_invalidate_page(page, page->mapping->host);
  367. }
  368. /*
  369. * Attempt to release the private state associated with a page
  370. * - Called if either PG_private or PG_fscache is set on the page
  371. * - Caller holds page lock
  372. * - Return true (may release page) or false (may not)
  373. */
  374. static int nfs_release_page(struct page *page, gfp_t gfp)
  375. {
  376. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  377. /* If PagePrivate() is set, then the page is not freeable */
  378. if (PagePrivate(page))
  379. return 0;
  380. return nfs_fscache_release_page(page, gfp);
  381. }
  382. static void nfs_check_dirty_writeback(struct page *page,
  383. bool *dirty, bool *writeback)
  384. {
  385. struct nfs_inode *nfsi;
  386. struct address_space *mapping = page_file_mapping(page);
  387. if (!mapping || PageSwapCache(page))
  388. return;
  389. /*
  390. * Check if an unstable page is currently being committed and
  391. * if so, have the VM treat it as if the page is under writeback
  392. * so it will not block due to pages that will shortly be freeable.
  393. */
  394. nfsi = NFS_I(mapping->host);
  395. if (atomic_read(&nfsi->commit_info.rpcs_out)) {
  396. *writeback = true;
  397. return;
  398. }
  399. /*
  400. * If PagePrivate() is set, then the page is not freeable and as the
  401. * inode is not being committed, it's not going to be cleaned in the
  402. * near future so treat it as dirty
  403. */
  404. if (PagePrivate(page))
  405. *dirty = true;
  406. }
  407. /*
  408. * Attempt to clear the private state associated with a page when an error
  409. * occurs that requires the cached contents of an inode to be written back or
  410. * destroyed
  411. * - Called if either PG_private or fscache is set on the page
  412. * - Caller holds page lock
  413. * - Return 0 if successful, -error otherwise
  414. */
  415. static int nfs_launder_page(struct page *page)
  416. {
  417. struct inode *inode = page_file_mapping(page)->host;
  418. struct nfs_inode *nfsi = NFS_I(inode);
  419. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  420. inode->i_ino, (long long)page_offset(page));
  421. nfs_fscache_wait_on_page_write(nfsi, page);
  422. return nfs_wb_page(inode, page);
  423. }
  424. static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
  425. sector_t *span)
  426. {
  427. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  428. *span = sis->pages;
  429. return rpc_clnt_swap_activate(clnt);
  430. }
  431. static void nfs_swap_deactivate(struct file *file)
  432. {
  433. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  434. rpc_clnt_swap_deactivate(clnt);
  435. }
  436. const struct address_space_operations nfs_file_aops = {
  437. .readpage = nfs_readpage,
  438. .readpages = nfs_readpages,
  439. .set_page_dirty = __set_page_dirty_nobuffers,
  440. .writepage = nfs_writepage,
  441. .writepages = nfs_writepages,
  442. .write_begin = nfs_write_begin,
  443. .write_end = nfs_write_end,
  444. .invalidatepage = nfs_invalidate_page,
  445. .releasepage = nfs_release_page,
  446. .direct_IO = nfs_direct_IO,
  447. #ifdef CONFIG_MIGRATION
  448. .migratepage = nfs_migrate_page,
  449. #endif
  450. .launder_page = nfs_launder_page,
  451. .is_dirty_writeback = nfs_check_dirty_writeback,
  452. .error_remove_page = generic_error_remove_page,
  453. .swap_activate = nfs_swap_activate,
  454. .swap_deactivate = nfs_swap_deactivate,
  455. };
  456. /*
  457. * Notification that a PTE pointing to an NFS page is about to be made
  458. * writable, implying that someone is about to modify the page through a
  459. * shared-writable mapping
  460. */
  461. static vm_fault_t nfs_vm_page_mkwrite(struct vm_fault *vmf)
  462. {
  463. struct page *page = vmf->page;
  464. struct file *filp = vmf->vma->vm_file;
  465. struct inode *inode = file_inode(filp);
  466. unsigned pagelen;
  467. vm_fault_t ret = VM_FAULT_NOPAGE;
  468. struct address_space *mapping;
  469. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
  470. filp, filp->f_mapping->host->i_ino,
  471. (long long)page_offset(page));
  472. sb_start_pagefault(inode->i_sb);
  473. /* make sure the cache has finished storing the page */
  474. nfs_fscache_wait_on_page_write(NFS_I(inode), page);
  475. wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
  476. nfs_wait_bit_killable, TASK_KILLABLE);
  477. lock_page(page);
  478. mapping = page_file_mapping(page);
  479. if (mapping != inode->i_mapping)
  480. goto out_unlock;
  481. wait_on_page_writeback(page);
  482. pagelen = nfs_page_length(page);
  483. if (pagelen == 0)
  484. goto out_unlock;
  485. ret = VM_FAULT_LOCKED;
  486. if (nfs_flush_incompatible(filp, page) == 0 &&
  487. nfs_updatepage(filp, page, 0, pagelen) == 0)
  488. goto out;
  489. ret = VM_FAULT_SIGBUS;
  490. out_unlock:
  491. unlock_page(page);
  492. out:
  493. sb_end_pagefault(inode->i_sb);
  494. return ret;
  495. }
  496. static const struct vm_operations_struct nfs_file_vm_ops = {
  497. .fault = filemap_fault,
  498. .map_pages = filemap_map_pages,
  499. .page_mkwrite = nfs_vm_page_mkwrite,
  500. };
  501. static int nfs_need_check_write(struct file *filp, struct inode *inode)
  502. {
  503. struct nfs_open_context *ctx;
  504. ctx = nfs_file_open_context(filp);
  505. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
  506. nfs_ctx_key_to_expire(ctx, inode))
  507. return 1;
  508. return 0;
  509. }
  510. ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
  511. {
  512. struct file *file = iocb->ki_filp;
  513. struct inode *inode = file_inode(file);
  514. unsigned long written = 0;
  515. ssize_t result;
  516. result = nfs_key_timeout_notify(file, inode);
  517. if (result)
  518. return result;
  519. if (iocb->ki_flags & IOCB_DIRECT)
  520. return nfs_file_direct_write(iocb, from);
  521. dprintk("NFS: write(%pD2, %zu@%Ld)\n",
  522. file, iov_iter_count(from), (long long) iocb->ki_pos);
  523. if (IS_SWAPFILE(inode))
  524. goto out_swapfile;
  525. /*
  526. * O_APPEND implies that we must revalidate the file length.
  527. */
  528. if (iocb->ki_flags & IOCB_APPEND) {
  529. result = nfs_revalidate_file_size(inode, file);
  530. if (result)
  531. goto out;
  532. }
  533. if (iocb->ki_pos > i_size_read(inode))
  534. nfs_revalidate_mapping(inode, file->f_mapping);
  535. nfs_start_io_write(inode);
  536. result = generic_write_checks(iocb, from);
  537. if (result > 0) {
  538. current->backing_dev_info = inode_to_bdi(inode);
  539. result = generic_perform_write(file, from, iocb->ki_pos);
  540. current->backing_dev_info = NULL;
  541. }
  542. nfs_end_io_write(inode);
  543. if (result <= 0)
  544. goto out;
  545. written = result;
  546. iocb->ki_pos += written;
  547. result = generic_write_sync(iocb, written);
  548. if (result < 0)
  549. goto out;
  550. /* Return error values */
  551. if (nfs_need_check_write(file, inode)) {
  552. int err = vfs_fsync(file, 0);
  553. if (err < 0)
  554. result = err;
  555. }
  556. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  557. out:
  558. return result;
  559. out_swapfile:
  560. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  561. return -EBUSY;
  562. }
  563. EXPORT_SYMBOL_GPL(nfs_file_write);
  564. static int
  565. do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  566. {
  567. struct inode *inode = filp->f_mapping->host;
  568. int status = 0;
  569. unsigned int saved_type = fl->fl_type;
  570. /* Try local locking first */
  571. posix_test_lock(filp, fl);
  572. if (fl->fl_type != F_UNLCK) {
  573. /* found a conflict */
  574. goto out;
  575. }
  576. fl->fl_type = saved_type;
  577. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  578. goto out_noconflict;
  579. if (is_local)
  580. goto out_noconflict;
  581. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  582. out:
  583. return status;
  584. out_noconflict:
  585. fl->fl_type = F_UNLCK;
  586. goto out;
  587. }
  588. static int
  589. do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  590. {
  591. struct inode *inode = filp->f_mapping->host;
  592. struct nfs_lock_context *l_ctx;
  593. int status;
  594. /*
  595. * Flush all pending writes before doing anything
  596. * with locks..
  597. */
  598. vfs_fsync(filp, 0);
  599. l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
  600. if (!IS_ERR(l_ctx)) {
  601. status = nfs_iocounter_wait(l_ctx);
  602. nfs_put_lock_context(l_ctx);
  603. /* NOTE: special case
  604. * If we're signalled while cleaning up locks on process exit, we
  605. * still need to complete the unlock.
  606. */
  607. if (status < 0 && !(fl->fl_flags & FL_CLOSE))
  608. return status;
  609. }
  610. /*
  611. * Use local locking if mounted with "-onolock" or with appropriate
  612. * "-olocal_lock="
  613. */
  614. if (!is_local)
  615. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  616. else
  617. status = locks_lock_file_wait(filp, fl);
  618. return status;
  619. }
  620. static int
  621. do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  622. {
  623. struct inode *inode = filp->f_mapping->host;
  624. int status;
  625. /*
  626. * Flush all pending writes before doing anything
  627. * with locks..
  628. */
  629. status = nfs_sync_mapping(filp->f_mapping);
  630. if (status != 0)
  631. goto out;
  632. /*
  633. * Use local locking if mounted with "-onolock" or with appropriate
  634. * "-olocal_lock="
  635. */
  636. if (!is_local)
  637. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  638. else
  639. status = locks_lock_file_wait(filp, fl);
  640. if (status < 0)
  641. goto out;
  642. /*
  643. * Invalidate cache to prevent missing any changes. If
  644. * the file is mapped, clear the page cache as well so
  645. * those mappings will be loaded.
  646. *
  647. * This makes locking act as a cache coherency point.
  648. */
  649. nfs_sync_mapping(filp->f_mapping);
  650. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
  651. nfs_zap_caches(inode);
  652. if (mapping_mapped(filp->f_mapping))
  653. nfs_revalidate_mapping(inode, filp->f_mapping);
  654. }
  655. out:
  656. return status;
  657. }
  658. /*
  659. * Lock a (portion of) a file
  660. */
  661. int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  662. {
  663. struct inode *inode = filp->f_mapping->host;
  664. int ret = -ENOLCK;
  665. int is_local = 0;
  666. dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
  667. filp, fl->fl_type, fl->fl_flags,
  668. (long long)fl->fl_start, (long long)fl->fl_end);
  669. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  670. /* No mandatory locks over NFS */
  671. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  672. goto out_err;
  673. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
  674. is_local = 1;
  675. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  676. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  677. if (ret < 0)
  678. goto out_err;
  679. }
  680. if (IS_GETLK(cmd))
  681. ret = do_getlk(filp, cmd, fl, is_local);
  682. else if (fl->fl_type == F_UNLCK)
  683. ret = do_unlk(filp, cmd, fl, is_local);
  684. else
  685. ret = do_setlk(filp, cmd, fl, is_local);
  686. out_err:
  687. return ret;
  688. }
  689. EXPORT_SYMBOL_GPL(nfs_lock);
  690. /*
  691. * Lock a (portion of) a file
  692. */
  693. int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  694. {
  695. struct inode *inode = filp->f_mapping->host;
  696. int is_local = 0;
  697. dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
  698. filp, fl->fl_type, fl->fl_flags);
  699. if (!(fl->fl_flags & FL_FLOCK))
  700. return -ENOLCK;
  701. /*
  702. * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
  703. * any standard. In principle we might be able to support LOCK_MAND
  704. * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
  705. * NFS code is not set up for it.
  706. */
  707. if (fl->fl_type & LOCK_MAND)
  708. return -EINVAL;
  709. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
  710. is_local = 1;
  711. /* We're simulating flock() locks using posix locks on the server */
  712. if (fl->fl_type == F_UNLCK)
  713. return do_unlk(filp, cmd, fl, is_local);
  714. return do_setlk(filp, cmd, fl, is_local);
  715. }
  716. EXPORT_SYMBOL_GPL(nfs_flock);
  717. const struct file_operations nfs_file_operations = {
  718. .llseek = nfs_file_llseek,
  719. .read_iter = nfs_file_read,
  720. .write_iter = nfs_file_write,
  721. .mmap = nfs_file_mmap,
  722. .open = nfs_file_open,
  723. .flush = nfs_file_flush,
  724. .release = nfs_file_release,
  725. .fsync = nfs_file_fsync,
  726. .lock = nfs_lock,
  727. .flock = nfs_flock,
  728. .splice_read = generic_file_splice_read,
  729. .splice_write = iter_file_splice_write,
  730. .check_flags = nfs_check_flags,
  731. .setlease = simple_nosetlease,
  732. };
  733. EXPORT_SYMBOL_GPL(nfs_file_operations);