file.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 <asm/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. struct nfs_inode *nfsi = NFS_I(inode);
  89. if (nfs_have_delegated_attributes(inode))
  90. goto out_noreval;
  91. if (filp->f_flags & O_DIRECT)
  92. goto force_reval;
  93. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  94. goto force_reval;
  95. if (nfs_attribute_timeout(inode))
  96. goto force_reval;
  97. out_noreval:
  98. return 0;
  99. force_reval:
  100. return __nfs_revalidate_inode(server, inode);
  101. }
  102. loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
  103. {
  104. dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
  105. filp, offset, whence);
  106. /*
  107. * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
  108. * the cached file length
  109. */
  110. if (whence != SEEK_SET && whence != SEEK_CUR) {
  111. struct inode *inode = filp->f_mapping->host;
  112. int retval = nfs_revalidate_file_size(inode, filp);
  113. if (retval < 0)
  114. return (loff_t)retval;
  115. }
  116. return generic_file_llseek(filp, offset, whence);
  117. }
  118. EXPORT_SYMBOL_GPL(nfs_file_llseek);
  119. /*
  120. * Flush all dirty pages, and check for write errors.
  121. */
  122. static int
  123. nfs_file_flush(struct file *file, fl_owner_t id)
  124. {
  125. struct inode *inode = file_inode(file);
  126. dprintk("NFS: flush(%pD2)\n", file);
  127. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  128. if ((file->f_mode & FMODE_WRITE) == 0)
  129. return 0;
  130. /* Flush writes to the server and return any errors */
  131. return vfs_fsync(file, 0);
  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. if (iocb->ki_flags & IOCB_DIRECT)
  139. return nfs_file_direct_read(iocb, to);
  140. dprintk("NFS: read(%pD2, %zu@%lu)\n",
  141. iocb->ki_filp,
  142. iov_iter_count(to), (unsigned long) iocb->ki_pos);
  143. nfs_start_io_read(inode);
  144. result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  145. if (!result) {
  146. result = generic_file_read_iter(iocb, to);
  147. if (result > 0)
  148. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
  149. }
  150. nfs_end_io_read(inode);
  151. return result;
  152. }
  153. EXPORT_SYMBOL_GPL(nfs_file_read);
  154. int
  155. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  156. {
  157. struct inode *inode = file_inode(file);
  158. int status;
  159. dprintk("NFS: mmap(%pD2)\n", file);
  160. /* Note: generic_file_mmap() returns ENOSYS on nommu systems
  161. * so we call that before revalidating the mapping
  162. */
  163. status = generic_file_mmap(file, vma);
  164. if (!status) {
  165. vma->vm_ops = &nfs_file_vm_ops;
  166. status = nfs_revalidate_mapping(inode, file->f_mapping);
  167. }
  168. return status;
  169. }
  170. EXPORT_SYMBOL_GPL(nfs_file_mmap);
  171. /*
  172. * Flush any dirty pages for this process, and check for write errors.
  173. * The return status from this call provides a reliable indication of
  174. * whether any write errors occurred for this process.
  175. *
  176. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  177. * disk, but it retrieves and clears ctx->error after synching, despite
  178. * the two being set at the same time in nfs_context_set_write_error().
  179. * This is because the former is used to notify the _next_ call to
  180. * nfs_file_write() that a write error occurred, and hence cause it to
  181. * fall back to doing a synchronous write.
  182. */
  183. static int
  184. nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
  185. {
  186. struct nfs_open_context *ctx = nfs_file_open_context(file);
  187. struct inode *inode = file_inode(file);
  188. int have_error, do_resend, status;
  189. int ret = 0;
  190. dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
  191. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  192. do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  193. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  194. status = nfs_commit_inode(inode, FLUSH_SYNC);
  195. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  196. if (have_error) {
  197. ret = xchg(&ctx->error, 0);
  198. if (ret)
  199. goto out;
  200. }
  201. if (status < 0) {
  202. ret = status;
  203. goto out;
  204. }
  205. do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  206. if (do_resend)
  207. ret = -EAGAIN;
  208. out:
  209. return ret;
  210. }
  211. int
  212. nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  213. {
  214. int ret;
  215. struct inode *inode = file_inode(file);
  216. trace_nfs_fsync_enter(inode);
  217. do {
  218. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  219. if (ret != 0)
  220. break;
  221. ret = nfs_file_fsync_commit(file, start, end, datasync);
  222. if (!ret)
  223. ret = pnfs_sync_inode(inode, !!datasync);
  224. /*
  225. * If nfs_file_fsync_commit detected a server reboot, then
  226. * resend all dirty pages that might have been covered by
  227. * the NFS_CONTEXT_RESEND_WRITES flag
  228. */
  229. start = 0;
  230. end = LLONG_MAX;
  231. } while (ret == -EAGAIN);
  232. trace_nfs_fsync_exit(inode, ret);
  233. return ret;
  234. }
  235. EXPORT_SYMBOL_GPL(nfs_file_fsync);
  236. /*
  237. * Decide whether a read/modify/write cycle may be more efficient
  238. * then a modify/write/read cycle when writing to a page in the
  239. * page cache.
  240. *
  241. * The modify/write/read cycle may occur if a page is read before
  242. * being completely filled by the writer. In this situation, the
  243. * page must be completely written to stable storage on the server
  244. * before it can be refilled by reading in the page from the server.
  245. * This can lead to expensive, small, FILE_SYNC mode writes being
  246. * done.
  247. *
  248. * It may be more efficient to read the page first if the file is
  249. * open for reading in addition to writing, the page is not marked
  250. * as Uptodate, it is not dirty or waiting to be committed,
  251. * indicating that it was previously allocated and then modified,
  252. * that there were valid bytes of data in that range of the file,
  253. * and that the new data won't completely replace the old data in
  254. * that range of the file.
  255. */
  256. static int nfs_want_read_modify_write(struct file *file, struct page *page,
  257. loff_t pos, unsigned len)
  258. {
  259. unsigned int pglen = nfs_page_length(page);
  260. unsigned int offset = pos & (PAGE_SIZE - 1);
  261. unsigned int end = offset + len;
  262. if (pnfs_ld_read_whole_page(file->f_mapping->host)) {
  263. if (!PageUptodate(page))
  264. return 1;
  265. return 0;
  266. }
  267. if ((file->f_mode & FMODE_READ) && /* open for read? */
  268. !PageUptodate(page) && /* Uptodate? */
  269. !PagePrivate(page) && /* i/o request already? */
  270. pglen && /* valid bytes of file? */
  271. (end < pglen || offset)) /* replace all valid bytes? */
  272. return 1;
  273. return 0;
  274. }
  275. /*
  276. * This does the "real" work of the write. We must allocate and lock the
  277. * page to be sent back to the generic routine, which then copies the
  278. * data from user space.
  279. *
  280. * If the writer ends up delaying the write, the writer needs to
  281. * increment the page use counts until he is done with the page.
  282. */
  283. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  284. loff_t pos, unsigned len, unsigned flags,
  285. struct page **pagep, void **fsdata)
  286. {
  287. int ret;
  288. pgoff_t index = pos >> PAGE_SHIFT;
  289. struct page *page;
  290. int once_thru = 0;
  291. dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
  292. file, mapping->host->i_ino, len, (long long) pos);
  293. start:
  294. page = grab_cache_page_write_begin(mapping, index, flags);
  295. if (!page)
  296. return -ENOMEM;
  297. *pagep = page;
  298. ret = nfs_flush_incompatible(file, page);
  299. if (ret) {
  300. unlock_page(page);
  301. put_page(page);
  302. } else if (!once_thru &&
  303. nfs_want_read_modify_write(file, page, pos, len)) {
  304. once_thru = 1;
  305. ret = nfs_readpage(file, page);
  306. put_page(page);
  307. if (!ret)
  308. goto start;
  309. }
  310. return ret;
  311. }
  312. static int nfs_write_end(struct file *file, struct address_space *mapping,
  313. loff_t pos, unsigned len, unsigned copied,
  314. struct page *page, void *fsdata)
  315. {
  316. unsigned offset = pos & (PAGE_SIZE - 1);
  317. struct nfs_open_context *ctx = nfs_file_open_context(file);
  318. int status;
  319. dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
  320. file, mapping->host->i_ino, len, (long long) pos);
  321. /*
  322. * Zero any uninitialised parts of the page, and then mark the page
  323. * as up to date if it turns out that we're extending the file.
  324. */
  325. if (!PageUptodate(page)) {
  326. unsigned pglen = nfs_page_length(page);
  327. unsigned end = offset + copied;
  328. if (pglen == 0) {
  329. zero_user_segments(page, 0, offset,
  330. end, PAGE_SIZE);
  331. SetPageUptodate(page);
  332. } else if (end >= pglen) {
  333. zero_user_segment(page, end, PAGE_SIZE);
  334. if (offset == 0)
  335. SetPageUptodate(page);
  336. } else
  337. zero_user_segment(page, pglen, PAGE_SIZE);
  338. }
  339. status = nfs_updatepage(file, page, offset, copied);
  340. unlock_page(page);
  341. put_page(page);
  342. if (status < 0)
  343. return status;
  344. NFS_I(mapping->host)->write_io += copied;
  345. if (nfs_ctx_key_to_expire(ctx, mapping->host)) {
  346. status = nfs_wb_all(mapping->host);
  347. if (status < 0)
  348. return status;
  349. }
  350. return copied;
  351. }
  352. /*
  353. * Partially or wholly invalidate a page
  354. * - Release the private state associated with a page if undergoing complete
  355. * page invalidation
  356. * - Called if either PG_private or PG_fscache is set on the page
  357. * - Caller holds page lock
  358. */
  359. static void nfs_invalidate_page(struct page *page, unsigned int offset,
  360. unsigned int length)
  361. {
  362. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
  363. page, offset, length);
  364. if (offset != 0 || length < PAGE_SIZE)
  365. return;
  366. /* Cancel any unstarted writes on this page */
  367. nfs_wb_page_cancel(page_file_mapping(page)->host, page);
  368. nfs_fscache_invalidate_page(page, page->mapping->host);
  369. }
  370. /*
  371. * Attempt to release the private state associated with a page
  372. * - Called if either PG_private or PG_fscache is set on the page
  373. * - Caller holds page lock
  374. * - Return true (may release page) or false (may not)
  375. */
  376. static int nfs_release_page(struct page *page, gfp_t gfp)
  377. {
  378. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  379. /* If PagePrivate() is set, then the page is not freeable */
  380. if (PagePrivate(page))
  381. return 0;
  382. return nfs_fscache_release_page(page, gfp);
  383. }
  384. static void nfs_check_dirty_writeback(struct page *page,
  385. bool *dirty, bool *writeback)
  386. {
  387. struct nfs_inode *nfsi;
  388. struct address_space *mapping = page_file_mapping(page);
  389. if (!mapping || PageSwapCache(page))
  390. return;
  391. /*
  392. * Check if an unstable page is currently being committed and
  393. * if so, have the VM treat it as if the page is under writeback
  394. * so it will not block due to pages that will shortly be freeable.
  395. */
  396. nfsi = NFS_I(mapping->host);
  397. if (atomic_read(&nfsi->commit_info.rpcs_out)) {
  398. *writeback = true;
  399. return;
  400. }
  401. /*
  402. * If PagePrivate() is set, then the page is not freeable and as the
  403. * inode is not being committed, it's not going to be cleaned in the
  404. * near future so treat it as dirty
  405. */
  406. if (PagePrivate(page))
  407. *dirty = true;
  408. }
  409. /*
  410. * Attempt to clear the private state associated with a page when an error
  411. * occurs that requires the cached contents of an inode to be written back or
  412. * destroyed
  413. * - Called if either PG_private or fscache is set on the page
  414. * - Caller holds page lock
  415. * - Return 0 if successful, -error otherwise
  416. */
  417. static int nfs_launder_page(struct page *page)
  418. {
  419. struct inode *inode = page_file_mapping(page)->host;
  420. struct nfs_inode *nfsi = NFS_I(inode);
  421. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  422. inode->i_ino, (long long)page_offset(page));
  423. nfs_fscache_wait_on_page_write(nfsi, page);
  424. return nfs_wb_launder_page(inode, page);
  425. }
  426. static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
  427. sector_t *span)
  428. {
  429. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  430. *span = sis->pages;
  431. return rpc_clnt_swap_activate(clnt);
  432. }
  433. static void nfs_swap_deactivate(struct file *file)
  434. {
  435. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  436. rpc_clnt_swap_deactivate(clnt);
  437. }
  438. const struct address_space_operations nfs_file_aops = {
  439. .readpage = nfs_readpage,
  440. .readpages = nfs_readpages,
  441. .set_page_dirty = __set_page_dirty_nobuffers,
  442. .writepage = nfs_writepage,
  443. .writepages = nfs_writepages,
  444. .write_begin = nfs_write_begin,
  445. .write_end = nfs_write_end,
  446. .invalidatepage = nfs_invalidate_page,
  447. .releasepage = nfs_release_page,
  448. .direct_IO = nfs_direct_IO,
  449. #ifdef CONFIG_MIGRATION
  450. .migratepage = nfs_migrate_page,
  451. #endif
  452. .launder_page = nfs_launder_page,
  453. .is_dirty_writeback = nfs_check_dirty_writeback,
  454. .error_remove_page = generic_error_remove_page,
  455. .swap_activate = nfs_swap_activate,
  456. .swap_deactivate = nfs_swap_deactivate,
  457. };
  458. /*
  459. * Notification that a PTE pointing to an NFS page is about to be made
  460. * writable, implying that someone is about to modify the page through a
  461. * shared-writable mapping
  462. */
  463. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  464. {
  465. struct page *page = vmf->page;
  466. struct file *filp = vma->vm_file;
  467. struct inode *inode = file_inode(filp);
  468. unsigned pagelen;
  469. int ret = VM_FAULT_NOPAGE;
  470. struct address_space *mapping;
  471. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
  472. filp, filp->f_mapping->host->i_ino,
  473. (long long)page_offset(page));
  474. sb_start_pagefault(inode->i_sb);
  475. /* make sure the cache has finished storing the page */
  476. nfs_fscache_wait_on_page_write(NFS_I(inode), page);
  477. wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
  478. nfs_wait_bit_killable, TASK_KILLABLE);
  479. lock_page(page);
  480. mapping = page_file_mapping(page);
  481. if (mapping != inode->i_mapping)
  482. goto out_unlock;
  483. wait_on_page_writeback(page);
  484. pagelen = nfs_page_length(page);
  485. if (pagelen == 0)
  486. goto out_unlock;
  487. ret = VM_FAULT_LOCKED;
  488. if (nfs_flush_incompatible(filp, page) == 0 &&
  489. nfs_updatepage(filp, page, 0, pagelen) == 0)
  490. goto out;
  491. ret = VM_FAULT_SIGBUS;
  492. out_unlock:
  493. unlock_page(page);
  494. out:
  495. sb_end_pagefault(inode->i_sb);
  496. return ret;
  497. }
  498. static const struct vm_operations_struct nfs_file_vm_ops = {
  499. .fault = filemap_fault,
  500. .map_pages = filemap_map_pages,
  501. .page_mkwrite = nfs_vm_page_mkwrite,
  502. };
  503. static int nfs_need_check_write(struct file *filp, struct inode *inode)
  504. {
  505. struct nfs_open_context *ctx;
  506. ctx = nfs_file_open_context(filp);
  507. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
  508. nfs_ctx_key_to_expire(ctx, inode))
  509. return 1;
  510. return 0;
  511. }
  512. ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
  513. {
  514. struct file *file = iocb->ki_filp;
  515. struct inode *inode = file_inode(file);
  516. unsigned long written = 0;
  517. ssize_t result;
  518. result = nfs_key_timeout_notify(file, inode);
  519. if (result)
  520. return result;
  521. if (iocb->ki_flags & IOCB_DIRECT)
  522. return nfs_file_direct_write(iocb, from);
  523. dprintk("NFS: write(%pD2, %zu@%Ld)\n",
  524. file, iov_iter_count(from), (long long) iocb->ki_pos);
  525. if (IS_SWAPFILE(inode))
  526. goto out_swapfile;
  527. /*
  528. * O_APPEND implies that we must revalidate the file length.
  529. */
  530. if (iocb->ki_flags & IOCB_APPEND) {
  531. result = nfs_revalidate_file_size(inode, file);
  532. if (result)
  533. goto out;
  534. }
  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. if (status < 0)
  604. return status;
  605. }
  606. /* NOTE: special case
  607. * If we're signalled while cleaning up locks on process exit, we
  608. * still need to complete the unlock.
  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. * Revalidate the cache if the server has time stamps granular
  644. * enough to detect subsecond changes. Otherwise, clear the
  645. * cache to prevent missing any changes.
  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. out:
  653. return status;
  654. }
  655. /*
  656. * Lock a (portion of) a file
  657. */
  658. int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  659. {
  660. struct inode *inode = filp->f_mapping->host;
  661. int ret = -ENOLCK;
  662. int is_local = 0;
  663. dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
  664. filp, fl->fl_type, fl->fl_flags,
  665. (long long)fl->fl_start, (long long)fl->fl_end);
  666. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  667. /* No mandatory locks over NFS */
  668. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  669. goto out_err;
  670. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
  671. is_local = 1;
  672. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  673. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  674. if (ret < 0)
  675. goto out_err;
  676. }
  677. if (IS_GETLK(cmd))
  678. ret = do_getlk(filp, cmd, fl, is_local);
  679. else if (fl->fl_type == F_UNLCK)
  680. ret = do_unlk(filp, cmd, fl, is_local);
  681. else
  682. ret = do_setlk(filp, cmd, fl, is_local);
  683. out_err:
  684. return ret;
  685. }
  686. EXPORT_SYMBOL_GPL(nfs_lock);
  687. /*
  688. * Lock a (portion of) a file
  689. */
  690. int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  691. {
  692. struct inode *inode = filp->f_mapping->host;
  693. int is_local = 0;
  694. dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
  695. filp, fl->fl_type, fl->fl_flags);
  696. if (!(fl->fl_flags & FL_FLOCK))
  697. return -ENOLCK;
  698. /*
  699. * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
  700. * any standard. In principle we might be able to support LOCK_MAND
  701. * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
  702. * NFS code is not set up for it.
  703. */
  704. if (fl->fl_type & LOCK_MAND)
  705. return -EINVAL;
  706. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
  707. is_local = 1;
  708. /* We're simulating flock() locks using posix locks on the server */
  709. if (fl->fl_type == F_UNLCK)
  710. return do_unlk(filp, cmd, fl, is_local);
  711. return do_setlk(filp, cmd, fl, is_local);
  712. }
  713. EXPORT_SYMBOL_GPL(nfs_flock);
  714. const struct file_operations nfs_file_operations = {
  715. .llseek = nfs_file_llseek,
  716. .read_iter = nfs_file_read,
  717. .write_iter = nfs_file_write,
  718. .mmap = nfs_file_mmap,
  719. .open = nfs_file_open,
  720. .flush = nfs_file_flush,
  721. .release = nfs_file_release,
  722. .fsync = nfs_file_fsync,
  723. .lock = nfs_lock,
  724. .flock = nfs_flock,
  725. .splice_read = generic_file_splice_read,
  726. .splice_write = iter_file_splice_write,
  727. .check_flags = nfs_check_flags,
  728. .setlease = simple_nosetlease,
  729. };
  730. EXPORT_SYMBOL_GPL(nfs_file_operations);