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 <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. const unsigned long force_reval = NFS_INO_REVAL_PAGECACHE|NFS_INO_REVAL_FORCED;
  90. unsigned long cache_validity = nfsi->cache_validity;
  91. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ) &&
  92. (cache_validity & force_reval) != force_reval)
  93. goto out_noreval;
  94. if (filp->f_flags & O_DIRECT)
  95. goto force_reval;
  96. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  97. goto force_reval;
  98. if (nfs_attribute_timeout(inode))
  99. goto force_reval;
  100. out_noreval:
  101. return 0;
  102. force_reval:
  103. return __nfs_revalidate_inode(server, inode);
  104. }
  105. loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
  106. {
  107. dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
  108. filp, offset, whence);
  109. /*
  110. * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
  111. * the cached file length
  112. */
  113. if (whence != SEEK_SET && whence != SEEK_CUR) {
  114. struct inode *inode = filp->f_mapping->host;
  115. int retval = nfs_revalidate_file_size(inode, filp);
  116. if (retval < 0)
  117. return (loff_t)retval;
  118. }
  119. return generic_file_llseek(filp, offset, whence);
  120. }
  121. EXPORT_SYMBOL_GPL(nfs_file_llseek);
  122. /*
  123. * Flush all dirty pages, and check for write errors.
  124. */
  125. static int
  126. nfs_file_flush(struct file *file, fl_owner_t id)
  127. {
  128. struct inode *inode = file_inode(file);
  129. dprintk("NFS: flush(%pD2)\n", file);
  130. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  131. if ((file->f_mode & FMODE_WRITE) == 0)
  132. return 0;
  133. /* Flush writes to the server and return any errors */
  134. return vfs_fsync(file, 0);
  135. }
  136. ssize_t
  137. nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
  138. {
  139. struct inode *inode = file_inode(iocb->ki_filp);
  140. ssize_t result;
  141. if (iocb->ki_flags & IOCB_DIRECT)
  142. return nfs_file_direct_read(iocb, to);
  143. dprintk("NFS: read(%pD2, %zu@%lu)\n",
  144. iocb->ki_filp,
  145. iov_iter_count(to), (unsigned long) iocb->ki_pos);
  146. nfs_start_io_read(inode);
  147. result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  148. if (!result) {
  149. result = generic_file_read_iter(iocb, to);
  150. if (result > 0)
  151. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
  152. }
  153. nfs_end_io_read(inode);
  154. return result;
  155. }
  156. EXPORT_SYMBOL_GPL(nfs_file_read);
  157. int
  158. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  159. {
  160. struct inode *inode = file_inode(file);
  161. int status;
  162. dprintk("NFS: mmap(%pD2)\n", file);
  163. /* Note: generic_file_mmap() returns ENOSYS on nommu systems
  164. * so we call that before revalidating the mapping
  165. */
  166. status = generic_file_mmap(file, vma);
  167. if (!status) {
  168. vma->vm_ops = &nfs_file_vm_ops;
  169. status = nfs_revalidate_mapping(inode, file->f_mapping);
  170. }
  171. return status;
  172. }
  173. EXPORT_SYMBOL_GPL(nfs_file_mmap);
  174. /*
  175. * Flush any dirty pages for this process, and check for write errors.
  176. * The return status from this call provides a reliable indication of
  177. * whether any write errors occurred for this process.
  178. *
  179. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  180. * disk, but it retrieves and clears ctx->error after synching, despite
  181. * the two being set at the same time in nfs_context_set_write_error().
  182. * This is because the former is used to notify the _next_ call to
  183. * nfs_file_write() that a write error occurred, and hence cause it to
  184. * fall back to doing a synchronous write.
  185. */
  186. static int
  187. nfs_file_fsync_commit(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 have_error, do_resend, status;
  192. int ret = 0;
  193. dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
  194. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  195. do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  196. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  197. status = nfs_commit_inode(inode, FLUSH_SYNC);
  198. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  199. if (have_error) {
  200. ret = xchg(&ctx->error, 0);
  201. if (ret)
  202. goto out;
  203. }
  204. if (status < 0) {
  205. ret = status;
  206. goto out;
  207. }
  208. do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  209. if (do_resend)
  210. ret = -EAGAIN;
  211. out:
  212. return ret;
  213. }
  214. int
  215. nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  216. {
  217. int ret;
  218. struct inode *inode = file_inode(file);
  219. trace_nfs_fsync_enter(inode);
  220. do {
  221. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  222. if (ret != 0)
  223. break;
  224. ret = nfs_file_fsync_commit(file, start, end, datasync);
  225. if (!ret)
  226. ret = pnfs_sync_inode(inode, !!datasync);
  227. /*
  228. * If nfs_file_fsync_commit detected a server reboot, then
  229. * resend all dirty pages that might have been covered by
  230. * the NFS_CONTEXT_RESEND_WRITES flag
  231. */
  232. start = 0;
  233. end = LLONG_MAX;
  234. } while (ret == -EAGAIN);
  235. trace_nfs_fsync_exit(inode, ret);
  236. return ret;
  237. }
  238. EXPORT_SYMBOL_GPL(nfs_file_fsync);
  239. /*
  240. * Decide whether a read/modify/write cycle may be more efficient
  241. * then a modify/write/read cycle when writing to a page in the
  242. * page cache.
  243. *
  244. * The modify/write/read cycle may occur if a page is read before
  245. * being completely filled by the writer. In this situation, the
  246. * page must be completely written to stable storage on the server
  247. * before it can be refilled by reading in the page from the server.
  248. * This can lead to expensive, small, FILE_SYNC mode writes being
  249. * done.
  250. *
  251. * It may be more efficient to read the page first if the file is
  252. * open for reading in addition to writing, the page is not marked
  253. * as Uptodate, it is not dirty or waiting to be committed,
  254. * indicating that it was previously allocated and then modified,
  255. * that there were valid bytes of data in that range of the file,
  256. * and that the new data won't completely replace the old data in
  257. * that range of the file.
  258. */
  259. static int nfs_want_read_modify_write(struct file *file, struct page *page,
  260. loff_t pos, unsigned len)
  261. {
  262. unsigned int pglen = nfs_page_length(page);
  263. unsigned int offset = pos & (PAGE_SIZE - 1);
  264. unsigned int end = offset + len;
  265. if (pnfs_ld_read_whole_page(file->f_mapping->host)) {
  266. if (!PageUptodate(page))
  267. return 1;
  268. return 0;
  269. }
  270. if ((file->f_mode & FMODE_READ) && /* open for read? */
  271. !PageUptodate(page) && /* Uptodate? */
  272. !PagePrivate(page) && /* i/o request already? */
  273. pglen && /* valid bytes of file? */
  274. (end < pglen || offset)) /* replace all valid bytes? */
  275. return 1;
  276. return 0;
  277. }
  278. /*
  279. * This does the "real" work of the write. We must allocate and lock the
  280. * page to be sent back to the generic routine, which then copies the
  281. * data from user space.
  282. *
  283. * If the writer ends up delaying the write, the writer needs to
  284. * increment the page use counts until he is done with the page.
  285. */
  286. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  287. loff_t pos, unsigned len, unsigned flags,
  288. struct page **pagep, void **fsdata)
  289. {
  290. int ret;
  291. pgoff_t index = pos >> PAGE_SHIFT;
  292. struct page *page;
  293. int once_thru = 0;
  294. dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
  295. file, mapping->host->i_ino, len, (long long) pos);
  296. start:
  297. page = grab_cache_page_write_begin(mapping, index, flags);
  298. if (!page)
  299. return -ENOMEM;
  300. *pagep = page;
  301. ret = nfs_flush_incompatible(file, page);
  302. if (ret) {
  303. unlock_page(page);
  304. put_page(page);
  305. } else if (!once_thru &&
  306. nfs_want_read_modify_write(file, page, pos, len)) {
  307. once_thru = 1;
  308. ret = nfs_readpage(file, page);
  309. put_page(page);
  310. if (!ret)
  311. goto start;
  312. }
  313. return ret;
  314. }
  315. static int nfs_write_end(struct file *file, struct address_space *mapping,
  316. loff_t pos, unsigned len, unsigned copied,
  317. struct page *page, void *fsdata)
  318. {
  319. unsigned offset = pos & (PAGE_SIZE - 1);
  320. struct nfs_open_context *ctx = nfs_file_open_context(file);
  321. int status;
  322. dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
  323. file, mapping->host->i_ino, len, (long long) pos);
  324. /*
  325. * Zero any uninitialised parts of the page, and then mark the page
  326. * as up to date if it turns out that we're extending the file.
  327. */
  328. if (!PageUptodate(page)) {
  329. unsigned pglen = nfs_page_length(page);
  330. unsigned end = offset + copied;
  331. if (pglen == 0) {
  332. zero_user_segments(page, 0, offset,
  333. end, PAGE_SIZE);
  334. SetPageUptodate(page);
  335. } else if (end >= pglen) {
  336. zero_user_segment(page, end, PAGE_SIZE);
  337. if (offset == 0)
  338. SetPageUptodate(page);
  339. } else
  340. zero_user_segment(page, pglen, PAGE_SIZE);
  341. }
  342. status = nfs_updatepage(file, page, offset, copied);
  343. unlock_page(page);
  344. put_page(page);
  345. if (status < 0)
  346. return status;
  347. NFS_I(mapping->host)->write_io += copied;
  348. if (nfs_ctx_key_to_expire(ctx, mapping->host)) {
  349. status = nfs_wb_all(mapping->host);
  350. if (status < 0)
  351. return status;
  352. }
  353. return copied;
  354. }
  355. /*
  356. * Partially or wholly invalidate a page
  357. * - Release the private state associated with a page if undergoing complete
  358. * page invalidation
  359. * - Called if either PG_private or PG_fscache is set on the page
  360. * - Caller holds page lock
  361. */
  362. static void nfs_invalidate_page(struct page *page, unsigned int offset,
  363. unsigned int length)
  364. {
  365. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
  366. page, offset, length);
  367. if (offset != 0 || length < PAGE_SIZE)
  368. return;
  369. /* Cancel any unstarted writes on this page */
  370. nfs_wb_page_cancel(page_file_mapping(page)->host, page);
  371. nfs_fscache_invalidate_page(page, page->mapping->host);
  372. }
  373. /*
  374. * Attempt to release the private state associated with a page
  375. * - Called if either PG_private or PG_fscache is set on the page
  376. * - Caller holds page lock
  377. * - Return true (may release page) or false (may not)
  378. */
  379. static int nfs_release_page(struct page *page, gfp_t gfp)
  380. {
  381. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  382. /* If PagePrivate() is set, then the page is not freeable */
  383. if (PagePrivate(page))
  384. return 0;
  385. return nfs_fscache_release_page(page, gfp);
  386. }
  387. static void nfs_check_dirty_writeback(struct page *page,
  388. bool *dirty, bool *writeback)
  389. {
  390. struct nfs_inode *nfsi;
  391. struct address_space *mapping = page_file_mapping(page);
  392. if (!mapping || PageSwapCache(page))
  393. return;
  394. /*
  395. * Check if an unstable page is currently being committed and
  396. * if so, have the VM treat it as if the page is under writeback
  397. * so it will not block due to pages that will shortly be freeable.
  398. */
  399. nfsi = NFS_I(mapping->host);
  400. if (atomic_read(&nfsi->commit_info.rpcs_out)) {
  401. *writeback = true;
  402. return;
  403. }
  404. /*
  405. * If PagePrivate() is set, then the page is not freeable and as the
  406. * inode is not being committed, it's not going to be cleaned in the
  407. * near future so treat it as dirty
  408. */
  409. if (PagePrivate(page))
  410. *dirty = true;
  411. }
  412. /*
  413. * Attempt to clear the private state associated with a page when an error
  414. * occurs that requires the cached contents of an inode to be written back or
  415. * destroyed
  416. * - Called if either PG_private or fscache is set on the page
  417. * - Caller holds page lock
  418. * - Return 0 if successful, -error otherwise
  419. */
  420. static int nfs_launder_page(struct page *page)
  421. {
  422. struct inode *inode = page_file_mapping(page)->host;
  423. struct nfs_inode *nfsi = NFS_I(inode);
  424. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  425. inode->i_ino, (long long)page_offset(page));
  426. nfs_fscache_wait_on_page_write(nfsi, page);
  427. return nfs_wb_launder_page(inode, page);
  428. }
  429. static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
  430. sector_t *span)
  431. {
  432. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  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 int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  467. {
  468. struct page *page = vmf->page;
  469. struct file *filp = vma->vm_file;
  470. struct inode *inode = file_inode(filp);
  471. unsigned pagelen;
  472. int 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. {
  508. struct nfs_open_context *ctx;
  509. ctx = nfs_file_open_context(filp);
  510. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
  511. nfs_ctx_key_to_expire(ctx, inode))
  512. return 1;
  513. return 0;
  514. }
  515. ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
  516. {
  517. struct file *file = iocb->ki_filp;
  518. struct inode *inode = file_inode(file);
  519. unsigned long written = 0;
  520. ssize_t result;
  521. result = nfs_key_timeout_notify(file, inode);
  522. if (result)
  523. return result;
  524. if (iocb->ki_flags & IOCB_DIRECT)
  525. return nfs_file_direct_write(iocb, from);
  526. dprintk("NFS: write(%pD2, %zu@%Ld)\n",
  527. file, iov_iter_count(from), (long long) iocb->ki_pos);
  528. if (IS_SWAPFILE(inode))
  529. goto out_swapfile;
  530. /*
  531. * O_APPEND implies that we must revalidate the file length.
  532. */
  533. if (iocb->ki_flags & IOCB_APPEND) {
  534. result = nfs_revalidate_file_size(inode, file);
  535. if (result)
  536. goto out;
  537. }
  538. nfs_start_io_write(inode);
  539. result = generic_write_checks(iocb, from);
  540. if (result > 0) {
  541. current->backing_dev_info = inode_to_bdi(inode);
  542. result = generic_perform_write(file, from, iocb->ki_pos);
  543. current->backing_dev_info = NULL;
  544. }
  545. nfs_end_io_write(inode);
  546. if (result <= 0)
  547. goto out;
  548. result = generic_write_sync(iocb, result);
  549. if (result < 0)
  550. goto out;
  551. written = result;
  552. iocb->ki_pos += written;
  553. /* Return error values */
  554. if (nfs_need_check_write(file, inode)) {
  555. int err = vfs_fsync(file, 0);
  556. if (err < 0)
  557. result = err;
  558. }
  559. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  560. out:
  561. return result;
  562. out_swapfile:
  563. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  564. return -EBUSY;
  565. }
  566. EXPORT_SYMBOL_GPL(nfs_file_write);
  567. static int
  568. do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  569. {
  570. struct inode *inode = filp->f_mapping->host;
  571. int status = 0;
  572. unsigned int saved_type = fl->fl_type;
  573. /* Try local locking first */
  574. posix_test_lock(filp, fl);
  575. if (fl->fl_type != F_UNLCK) {
  576. /* found a conflict */
  577. goto out;
  578. }
  579. fl->fl_type = saved_type;
  580. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  581. goto out_noconflict;
  582. if (is_local)
  583. goto out_noconflict;
  584. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  585. out:
  586. return status;
  587. out_noconflict:
  588. fl->fl_type = F_UNLCK;
  589. goto out;
  590. }
  591. static int
  592. do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  593. {
  594. struct inode *inode = filp->f_mapping->host;
  595. struct nfs_lock_context *l_ctx;
  596. int status;
  597. /*
  598. * Flush all pending writes before doing anything
  599. * with locks..
  600. */
  601. vfs_fsync(filp, 0);
  602. l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
  603. if (!IS_ERR(l_ctx)) {
  604. status = nfs_iocounter_wait(l_ctx);
  605. nfs_put_lock_context(l_ctx);
  606. if (status < 0)
  607. return status;
  608. }
  609. /* NOTE: special case
  610. * If we're signalled while cleaning up locks on process exit, we
  611. * still need to complete the unlock.
  612. */
  613. /*
  614. * Use local locking if mounted with "-onolock" or with appropriate
  615. * "-olocal_lock="
  616. */
  617. if (!is_local)
  618. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  619. else
  620. status = locks_lock_file_wait(filp, fl);
  621. return status;
  622. }
  623. static int
  624. do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  625. {
  626. struct inode *inode = filp->f_mapping->host;
  627. int status;
  628. /*
  629. * Flush all pending writes before doing anything
  630. * with locks..
  631. */
  632. status = nfs_sync_mapping(filp->f_mapping);
  633. if (status != 0)
  634. goto out;
  635. /*
  636. * Use local locking if mounted with "-onolock" or with appropriate
  637. * "-olocal_lock="
  638. */
  639. if (!is_local)
  640. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  641. else
  642. status = locks_lock_file_wait(filp, fl);
  643. if (status < 0)
  644. goto out;
  645. /*
  646. * Revalidate the cache if the server has time stamps granular
  647. * enough to detect subsecond changes. Otherwise, clear the
  648. * cache to prevent missing any changes.
  649. *
  650. * This makes locking act as a cache coherency point.
  651. */
  652. nfs_sync_mapping(filp->f_mapping);
  653. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  654. nfs_zap_mapping(inode, filp->f_mapping);
  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);