file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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/aio.h>
  29. #include <linux/gfp.h>
  30. #include <linux/swap.h>
  31. #include <asm/uaccess.h>
  32. #include "delegation.h"
  33. #include "internal.h"
  34. #include "iostat.h"
  35. #include "fscache.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. return nfs_release(inode, filp);
  71. }
  72. EXPORT_SYMBOL_GPL(nfs_file_release);
  73. /**
  74. * nfs_revalidate_size - Revalidate the file size
  75. * @inode - pointer to inode struct
  76. * @file - pointer to struct file
  77. *
  78. * Revalidates the file length. This is basically a wrapper around
  79. * nfs_revalidate_inode() that takes into account the fact that we may
  80. * have cached writes (in which case we don't care about the server's
  81. * idea of what the file length is), or O_DIRECT (in which case we
  82. * shouldn't trust the cache).
  83. */
  84. static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
  85. {
  86. struct nfs_server *server = NFS_SERVER(inode);
  87. struct nfs_inode *nfsi = NFS_I(inode);
  88. if (nfs_have_delegated_attributes(inode))
  89. goto out_noreval;
  90. if (filp->f_flags & O_DIRECT)
  91. goto force_reval;
  92. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  93. goto force_reval;
  94. if (nfs_attribute_timeout(inode))
  95. goto force_reval;
  96. out_noreval:
  97. return 0;
  98. force_reval:
  99. return __nfs_revalidate_inode(server, inode);
  100. }
  101. loff_t nfs_file_llseek(struct file *filp, loff_t offset, int whence)
  102. {
  103. dprintk("NFS: llseek file(%pD2, %lld, %d)\n",
  104. filp, offset, whence);
  105. /*
  106. * whence == SEEK_END || SEEK_DATA || SEEK_HOLE => we must revalidate
  107. * the cached file length
  108. */
  109. if (whence != SEEK_SET && whence != SEEK_CUR) {
  110. struct inode *inode = filp->f_mapping->host;
  111. int retval = nfs_revalidate_file_size(inode, filp);
  112. if (retval < 0)
  113. return (loff_t)retval;
  114. }
  115. return generic_file_llseek(filp, offset, whence);
  116. }
  117. EXPORT_SYMBOL_GPL(nfs_file_llseek);
  118. /*
  119. * Flush all dirty pages, and check for write errors.
  120. */
  121. int
  122. nfs_file_flush(struct file *file, fl_owner_t id)
  123. {
  124. struct inode *inode = file_inode(file);
  125. dprintk("NFS: flush(%pD2)\n", file);
  126. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  127. if ((file->f_mode & FMODE_WRITE) == 0)
  128. return 0;
  129. /*
  130. * If we're holding a write delegation, then just start the i/o
  131. * but don't wait for completion (or send a commit).
  132. */
  133. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  134. return filemap_fdatawrite(file->f_mapping);
  135. /* Flush writes to the server and return any errors */
  136. return vfs_fsync(file, 0);
  137. }
  138. EXPORT_SYMBOL_GPL(nfs_file_flush);
  139. ssize_t
  140. nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
  141. {
  142. struct inode *inode = file_inode(iocb->ki_filp);
  143. ssize_t result;
  144. if (iocb->ki_filp->f_flags & O_DIRECT)
  145. return nfs_file_direct_read(iocb, to, iocb->ki_pos, true);
  146. dprintk("NFS: read(%pD2, %zu@%lu)\n",
  147. iocb->ki_filp,
  148. iov_iter_count(to), (unsigned long) iocb->ki_pos);
  149. result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  150. if (!result) {
  151. result = generic_file_read_iter(iocb, to);
  152. if (result > 0)
  153. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
  154. }
  155. return result;
  156. }
  157. EXPORT_SYMBOL_GPL(nfs_file_read);
  158. ssize_t
  159. nfs_file_splice_read(struct file *filp, loff_t *ppos,
  160. struct pipe_inode_info *pipe, size_t count,
  161. unsigned int flags)
  162. {
  163. struct inode *inode = file_inode(filp);
  164. ssize_t res;
  165. dprintk("NFS: splice_read(%pD2, %lu@%Lu)\n",
  166. filp, (unsigned long) count, (unsigned long long) *ppos);
  167. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  168. if (!res) {
  169. res = generic_file_splice_read(filp, ppos, pipe, count, flags);
  170. if (res > 0)
  171. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
  172. }
  173. return res;
  174. }
  175. EXPORT_SYMBOL_GPL(nfs_file_splice_read);
  176. int
  177. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  178. {
  179. struct inode *inode = file_inode(file);
  180. int status;
  181. dprintk("NFS: mmap(%pD2)\n", file);
  182. /* Note: generic_file_mmap() returns ENOSYS on nommu systems
  183. * so we call that before revalidating the mapping
  184. */
  185. status = generic_file_mmap(file, vma);
  186. if (!status) {
  187. vma->vm_ops = &nfs_file_vm_ops;
  188. status = nfs_revalidate_mapping(inode, file->f_mapping);
  189. }
  190. return status;
  191. }
  192. EXPORT_SYMBOL_GPL(nfs_file_mmap);
  193. /*
  194. * Flush any dirty pages for this process, and check for write errors.
  195. * The return status from this call provides a reliable indication of
  196. * whether any write errors occurred for this process.
  197. *
  198. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  199. * disk, but it retrieves and clears ctx->error after synching, despite
  200. * the two being set at the same time in nfs_context_set_write_error().
  201. * This is because the former is used to notify the _next_ call to
  202. * nfs_file_write() that a write error occurred, and hence cause it to
  203. * fall back to doing a synchronous write.
  204. */
  205. int
  206. nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
  207. {
  208. struct nfs_open_context *ctx = nfs_file_open_context(file);
  209. struct inode *inode = file_inode(file);
  210. int have_error, do_resend, status;
  211. int ret = 0;
  212. dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
  213. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  214. do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  215. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  216. status = nfs_commit_inode(inode, FLUSH_SYNC);
  217. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  218. if (have_error) {
  219. ret = xchg(&ctx->error, 0);
  220. if (ret)
  221. goto out;
  222. }
  223. if (status < 0) {
  224. ret = status;
  225. goto out;
  226. }
  227. do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  228. if (do_resend)
  229. ret = -EAGAIN;
  230. out:
  231. return ret;
  232. }
  233. EXPORT_SYMBOL_GPL(nfs_file_fsync_commit);
  234. static int
  235. nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  236. {
  237. int ret;
  238. struct inode *inode = file_inode(file);
  239. trace_nfs_fsync_enter(inode);
  240. do {
  241. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  242. if (ret != 0)
  243. break;
  244. mutex_lock(&inode->i_mutex);
  245. ret = nfs_file_fsync_commit(file, start, end, datasync);
  246. mutex_unlock(&inode->i_mutex);
  247. /*
  248. * If nfs_file_fsync_commit detected a server reboot, then
  249. * resend all dirty pages that might have been covered by
  250. * the NFS_CONTEXT_RESEND_WRITES flag
  251. */
  252. start = 0;
  253. end = LLONG_MAX;
  254. } while (ret == -EAGAIN);
  255. trace_nfs_fsync_exit(inode, ret);
  256. return ret;
  257. }
  258. /*
  259. * Decide whether a read/modify/write cycle may be more efficient
  260. * then a modify/write/read cycle when writing to a page in the
  261. * page cache.
  262. *
  263. * The modify/write/read cycle may occur if a page is read before
  264. * being completely filled by the writer. In this situation, the
  265. * page must be completely written to stable storage on the server
  266. * before it can be refilled by reading in the page from the server.
  267. * This can lead to expensive, small, FILE_SYNC mode writes being
  268. * done.
  269. *
  270. * It may be more efficient to read the page first if the file is
  271. * open for reading in addition to writing, the page is not marked
  272. * as Uptodate, it is not dirty or waiting to be committed,
  273. * indicating that it was previously allocated and then modified,
  274. * that there were valid bytes of data in that range of the file,
  275. * and that the new data won't completely replace the old data in
  276. * that range of the file.
  277. */
  278. static int nfs_want_read_modify_write(struct file *file, struct page *page,
  279. loff_t pos, unsigned len)
  280. {
  281. unsigned int pglen = nfs_page_length(page);
  282. unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
  283. unsigned int end = offset + len;
  284. if ((file->f_mode & FMODE_READ) && /* open for read? */
  285. !PageUptodate(page) && /* Uptodate? */
  286. !PagePrivate(page) && /* i/o request already? */
  287. pglen && /* valid bytes of file? */
  288. (end < pglen || offset)) /* replace all valid bytes? */
  289. return 1;
  290. return 0;
  291. }
  292. /*
  293. * This does the "real" work of the write. We must allocate and lock the
  294. * page to be sent back to the generic routine, which then copies the
  295. * data from user space.
  296. *
  297. * If the writer ends up delaying the write, the writer needs to
  298. * increment the page use counts until he is done with the page.
  299. */
  300. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  301. loff_t pos, unsigned len, unsigned flags,
  302. struct page **pagep, void **fsdata)
  303. {
  304. int ret;
  305. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  306. struct page *page;
  307. int once_thru = 0;
  308. dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
  309. file, mapping->host->i_ino, len, (long long) pos);
  310. start:
  311. /*
  312. * Prevent starvation issues if someone is doing a consistency
  313. * sync-to-disk
  314. */
  315. ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
  316. nfs_wait_bit_killable, TASK_KILLABLE);
  317. if (ret)
  318. return ret;
  319. page = grab_cache_page_write_begin(mapping, index, flags);
  320. if (!page)
  321. return -ENOMEM;
  322. *pagep = page;
  323. ret = nfs_flush_incompatible(file, page);
  324. if (ret) {
  325. unlock_page(page);
  326. page_cache_release(page);
  327. } else if (!once_thru &&
  328. nfs_want_read_modify_write(file, page, pos, len)) {
  329. once_thru = 1;
  330. ret = nfs_readpage(file, page);
  331. page_cache_release(page);
  332. if (!ret)
  333. goto start;
  334. }
  335. return ret;
  336. }
  337. static int nfs_write_end(struct file *file, struct address_space *mapping,
  338. loff_t pos, unsigned len, unsigned copied,
  339. struct page *page, void *fsdata)
  340. {
  341. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  342. struct nfs_open_context *ctx = nfs_file_open_context(file);
  343. int status;
  344. dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
  345. file, mapping->host->i_ino, len, (long long) pos);
  346. /*
  347. * Zero any uninitialised parts of the page, and then mark the page
  348. * as up to date if it turns out that we're extending the file.
  349. */
  350. if (!PageUptodate(page)) {
  351. unsigned pglen = nfs_page_length(page);
  352. unsigned end = offset + len;
  353. if (pglen == 0) {
  354. zero_user_segments(page, 0, offset,
  355. end, PAGE_CACHE_SIZE);
  356. SetPageUptodate(page);
  357. } else if (end >= pglen) {
  358. zero_user_segment(page, end, PAGE_CACHE_SIZE);
  359. if (offset == 0)
  360. SetPageUptodate(page);
  361. } else
  362. zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
  363. }
  364. status = nfs_updatepage(file, page, offset, copied);
  365. unlock_page(page);
  366. page_cache_release(page);
  367. if (status < 0)
  368. return status;
  369. NFS_I(mapping->host)->write_io += copied;
  370. if (nfs_ctx_key_to_expire(ctx)) {
  371. status = nfs_wb_all(mapping->host);
  372. if (status < 0)
  373. return status;
  374. }
  375. return copied;
  376. }
  377. /*
  378. * Partially or wholly invalidate a page
  379. * - Release the private state associated with a page if undergoing complete
  380. * page invalidation
  381. * - Called if either PG_private or PG_fscache is set on the page
  382. * - Caller holds page lock
  383. */
  384. static void nfs_invalidate_page(struct page *page, unsigned int offset,
  385. unsigned int length)
  386. {
  387. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
  388. page, offset, length);
  389. if (offset != 0 || length < PAGE_CACHE_SIZE)
  390. return;
  391. /* Cancel any unstarted writes on this page */
  392. nfs_wb_page_cancel(page_file_mapping(page)->host, page);
  393. nfs_fscache_invalidate_page(page, page->mapping->host);
  394. }
  395. /*
  396. * Attempt to release the private state associated with a page
  397. * - Called if either PG_private or PG_fscache is set on the page
  398. * - Caller holds page lock
  399. * - Return true (may release page) or false (may not)
  400. */
  401. static int nfs_release_page(struct page *page, gfp_t gfp)
  402. {
  403. struct address_space *mapping = page->mapping;
  404. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  405. /* Only do I/O if gfp is a superset of GFP_KERNEL, and we're not
  406. * doing this memory reclaim for a fs-related allocation.
  407. */
  408. if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL &&
  409. !(current->flags & PF_FSTRANS)) {
  410. int how = FLUSH_SYNC;
  411. /* Don't let kswapd deadlock waiting for OOM RPC calls */
  412. if (current_is_kswapd())
  413. how = 0;
  414. nfs_commit_inode(mapping->host, how);
  415. }
  416. /* If PagePrivate() is set, then the page is not freeable */
  417. if (PagePrivate(page))
  418. return 0;
  419. return nfs_fscache_release_page(page, gfp);
  420. }
  421. static void nfs_check_dirty_writeback(struct page *page,
  422. bool *dirty, bool *writeback)
  423. {
  424. struct nfs_inode *nfsi;
  425. struct address_space *mapping = page_file_mapping(page);
  426. if (!mapping || PageSwapCache(page))
  427. return;
  428. /*
  429. * Check if an unstable page is currently being committed and
  430. * if so, have the VM treat it as if the page is under writeback
  431. * so it will not block due to pages that will shortly be freeable.
  432. */
  433. nfsi = NFS_I(mapping->host);
  434. if (test_bit(NFS_INO_COMMIT, &nfsi->flags)) {
  435. *writeback = true;
  436. return;
  437. }
  438. /*
  439. * If PagePrivate() is set, then the page is not freeable and as the
  440. * inode is not being committed, it's not going to be cleaned in the
  441. * near future so treat it as dirty
  442. */
  443. if (PagePrivate(page))
  444. *dirty = true;
  445. }
  446. /*
  447. * Attempt to clear the private state associated with a page when an error
  448. * occurs that requires the cached contents of an inode to be written back or
  449. * destroyed
  450. * - Called if either PG_private or fscache is set on the page
  451. * - Caller holds page lock
  452. * - Return 0 if successful, -error otherwise
  453. */
  454. static int nfs_launder_page(struct page *page)
  455. {
  456. struct inode *inode = page_file_mapping(page)->host;
  457. struct nfs_inode *nfsi = NFS_I(inode);
  458. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  459. inode->i_ino, (long long)page_offset(page));
  460. nfs_fscache_wait_on_page_write(nfsi, page);
  461. return nfs_wb_page(inode, page);
  462. }
  463. #ifdef CONFIG_NFS_SWAP
  464. static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
  465. sector_t *span)
  466. {
  467. *span = sis->pages;
  468. return xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 1);
  469. }
  470. static void nfs_swap_deactivate(struct file *file)
  471. {
  472. xs_swapper(NFS_CLIENT(file->f_mapping->host)->cl_xprt, 0);
  473. }
  474. #endif
  475. const struct address_space_operations nfs_file_aops = {
  476. .readpage = nfs_readpage,
  477. .readpages = nfs_readpages,
  478. .set_page_dirty = __set_page_dirty_nobuffers,
  479. .writepage = nfs_writepage,
  480. .writepages = nfs_writepages,
  481. .write_begin = nfs_write_begin,
  482. .write_end = nfs_write_end,
  483. .invalidatepage = nfs_invalidate_page,
  484. .releasepage = nfs_release_page,
  485. .direct_IO = nfs_direct_IO,
  486. .migratepage = nfs_migrate_page,
  487. .launder_page = nfs_launder_page,
  488. .is_dirty_writeback = nfs_check_dirty_writeback,
  489. .error_remove_page = generic_error_remove_page,
  490. #ifdef CONFIG_NFS_SWAP
  491. .swap_activate = nfs_swap_activate,
  492. .swap_deactivate = nfs_swap_deactivate,
  493. #endif
  494. };
  495. /*
  496. * Notification that a PTE pointing to an NFS page is about to be made
  497. * writable, implying that someone is about to modify the page through a
  498. * shared-writable mapping
  499. */
  500. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  501. {
  502. struct page *page = vmf->page;
  503. struct file *filp = vma->vm_file;
  504. struct inode *inode = file_inode(filp);
  505. unsigned pagelen;
  506. int ret = VM_FAULT_NOPAGE;
  507. struct address_space *mapping;
  508. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
  509. filp, filp->f_mapping->host->i_ino,
  510. (long long)page_offset(page));
  511. /* make sure the cache has finished storing the page */
  512. nfs_fscache_wait_on_page_write(NFS_I(inode), page);
  513. lock_page(page);
  514. mapping = page_file_mapping(page);
  515. if (mapping != inode->i_mapping)
  516. goto out_unlock;
  517. wait_on_page_writeback(page);
  518. pagelen = nfs_page_length(page);
  519. if (pagelen == 0)
  520. goto out_unlock;
  521. ret = VM_FAULT_LOCKED;
  522. if (nfs_flush_incompatible(filp, page) == 0 &&
  523. nfs_updatepage(filp, page, 0, pagelen) == 0)
  524. goto out;
  525. ret = VM_FAULT_SIGBUS;
  526. out_unlock:
  527. unlock_page(page);
  528. out:
  529. return ret;
  530. }
  531. static const struct vm_operations_struct nfs_file_vm_ops = {
  532. .fault = filemap_fault,
  533. .map_pages = filemap_map_pages,
  534. .page_mkwrite = nfs_vm_page_mkwrite,
  535. .remap_pages = generic_file_remap_pages,
  536. };
  537. static int nfs_need_sync_write(struct file *filp, struct inode *inode)
  538. {
  539. struct nfs_open_context *ctx;
  540. if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
  541. return 1;
  542. ctx = nfs_file_open_context(filp);
  543. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
  544. nfs_ctx_key_to_expire(ctx))
  545. return 1;
  546. return 0;
  547. }
  548. ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
  549. {
  550. struct file *file = iocb->ki_filp;
  551. struct inode *inode = file_inode(file);
  552. unsigned long written = 0;
  553. ssize_t result;
  554. size_t count = iov_iter_count(from);
  555. loff_t pos = iocb->ki_pos;
  556. result = nfs_key_timeout_notify(file, inode);
  557. if (result)
  558. return result;
  559. if (file->f_flags & O_DIRECT)
  560. return nfs_file_direct_write(iocb, from, pos, true);
  561. dprintk("NFS: write(%pD2, %zu@%Ld)\n",
  562. file, count, (long long) pos);
  563. result = -EBUSY;
  564. if (IS_SWAPFILE(inode))
  565. goto out_swapfile;
  566. /*
  567. * O_APPEND implies that we must revalidate the file length.
  568. */
  569. if (file->f_flags & O_APPEND) {
  570. result = nfs_revalidate_file_size(inode, file);
  571. if (result)
  572. goto out;
  573. }
  574. result = count;
  575. if (!count)
  576. goto out;
  577. result = generic_file_write_iter(iocb, from);
  578. if (result > 0)
  579. written = result;
  580. /* Return error values for O_DSYNC and IS_SYNC() */
  581. if (result >= 0 && nfs_need_sync_write(file, inode)) {
  582. int err = vfs_fsync(file, 0);
  583. if (err < 0)
  584. result = err;
  585. }
  586. if (result > 0)
  587. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  588. out:
  589. return result;
  590. out_swapfile:
  591. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  592. goto out;
  593. }
  594. EXPORT_SYMBOL_GPL(nfs_file_write);
  595. static int
  596. do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  597. {
  598. struct inode *inode = filp->f_mapping->host;
  599. int status = 0;
  600. unsigned int saved_type = fl->fl_type;
  601. /* Try local locking first */
  602. posix_test_lock(filp, fl);
  603. if (fl->fl_type != F_UNLCK) {
  604. /* found a conflict */
  605. goto out;
  606. }
  607. fl->fl_type = saved_type;
  608. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  609. goto out_noconflict;
  610. if (is_local)
  611. goto out_noconflict;
  612. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  613. out:
  614. return status;
  615. out_noconflict:
  616. fl->fl_type = F_UNLCK;
  617. goto out;
  618. }
  619. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  620. {
  621. int res = 0;
  622. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  623. case FL_POSIX:
  624. res = posix_lock_file_wait(file, fl);
  625. break;
  626. case FL_FLOCK:
  627. res = flock_lock_file_wait(file, fl);
  628. break;
  629. default:
  630. BUG();
  631. }
  632. return res;
  633. }
  634. static int
  635. do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  636. {
  637. struct inode *inode = filp->f_mapping->host;
  638. struct nfs_lock_context *l_ctx;
  639. int status;
  640. /*
  641. * Flush all pending writes before doing anything
  642. * with locks..
  643. */
  644. nfs_sync_mapping(filp->f_mapping);
  645. l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
  646. if (!IS_ERR(l_ctx)) {
  647. status = nfs_iocounter_wait(&l_ctx->io_count);
  648. nfs_put_lock_context(l_ctx);
  649. if (status < 0)
  650. return status;
  651. }
  652. /* NOTE: special case
  653. * If we're signalled while cleaning up locks on process exit, we
  654. * still need to complete the unlock.
  655. */
  656. /*
  657. * Use local locking if mounted with "-onolock" or with appropriate
  658. * "-olocal_lock="
  659. */
  660. if (!is_local)
  661. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  662. else
  663. status = do_vfs_lock(filp, fl);
  664. return status;
  665. }
  666. static int
  667. is_time_granular(struct timespec *ts) {
  668. return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000));
  669. }
  670. static int
  671. do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  672. {
  673. struct inode *inode = filp->f_mapping->host;
  674. int status;
  675. /*
  676. * Flush all pending writes before doing anything
  677. * with locks..
  678. */
  679. status = nfs_sync_mapping(filp->f_mapping);
  680. if (status != 0)
  681. goto out;
  682. /*
  683. * Use local locking if mounted with "-onolock" or with appropriate
  684. * "-olocal_lock="
  685. */
  686. if (!is_local)
  687. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  688. else
  689. status = do_vfs_lock(filp, fl);
  690. if (status < 0)
  691. goto out;
  692. /*
  693. * Revalidate the cache if the server has time stamps granular
  694. * enough to detect subsecond changes. Otherwise, clear the
  695. * cache to prevent missing any changes.
  696. *
  697. * This makes locking act as a cache coherency point.
  698. */
  699. nfs_sync_mapping(filp->f_mapping);
  700. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
  701. if (is_time_granular(&NFS_SERVER(inode)->time_delta))
  702. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  703. else
  704. nfs_zap_caches(inode);
  705. }
  706. out:
  707. return status;
  708. }
  709. /*
  710. * Lock a (portion of) a file
  711. */
  712. int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  713. {
  714. struct inode *inode = filp->f_mapping->host;
  715. int ret = -ENOLCK;
  716. int is_local = 0;
  717. dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
  718. filp, fl->fl_type, fl->fl_flags,
  719. (long long)fl->fl_start, (long long)fl->fl_end);
  720. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  721. /* No mandatory locks over NFS */
  722. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  723. goto out_err;
  724. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
  725. is_local = 1;
  726. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  727. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  728. if (ret < 0)
  729. goto out_err;
  730. }
  731. if (IS_GETLK(cmd))
  732. ret = do_getlk(filp, cmd, fl, is_local);
  733. else if (fl->fl_type == F_UNLCK)
  734. ret = do_unlk(filp, cmd, fl, is_local);
  735. else
  736. ret = do_setlk(filp, cmd, fl, is_local);
  737. out_err:
  738. return ret;
  739. }
  740. EXPORT_SYMBOL_GPL(nfs_lock);
  741. /*
  742. * Lock a (portion of) a file
  743. */
  744. int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  745. {
  746. struct inode *inode = filp->f_mapping->host;
  747. int is_local = 0;
  748. dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
  749. filp, fl->fl_type, fl->fl_flags);
  750. if (!(fl->fl_flags & FL_FLOCK))
  751. return -ENOLCK;
  752. /*
  753. * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
  754. * any standard. In principle we might be able to support LOCK_MAND
  755. * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
  756. * NFS code is not set up for it.
  757. */
  758. if (fl->fl_type & LOCK_MAND)
  759. return -EINVAL;
  760. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
  761. is_local = 1;
  762. /* We're simulating flock() locks using posix locks on the server */
  763. if (fl->fl_type == F_UNLCK)
  764. return do_unlk(filp, cmd, fl, is_local);
  765. return do_setlk(filp, cmd, fl, is_local);
  766. }
  767. EXPORT_SYMBOL_GPL(nfs_flock);
  768. /*
  769. * There is no protocol support for leases, so we have no way to implement
  770. * them correctly in the face of opens by other clients.
  771. */
  772. int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
  773. {
  774. dprintk("NFS: setlease(%pD2, arg=%ld)\n", file, arg);
  775. return -EINVAL;
  776. }
  777. EXPORT_SYMBOL_GPL(nfs_setlease);
  778. const struct file_operations nfs_file_operations = {
  779. .llseek = nfs_file_llseek,
  780. .read = new_sync_read,
  781. .write = new_sync_write,
  782. .read_iter = nfs_file_read,
  783. .write_iter = nfs_file_write,
  784. .mmap = nfs_file_mmap,
  785. .open = nfs_file_open,
  786. .flush = nfs_file_flush,
  787. .release = nfs_file_release,
  788. .fsync = nfs_file_fsync,
  789. .lock = nfs_lock,
  790. .flock = nfs_flock,
  791. .splice_read = nfs_file_splice_read,
  792. .splice_write = iter_file_splice_write,
  793. .check_flags = nfs_check_flags,
  794. .setlease = nfs_setlease,
  795. };
  796. EXPORT_SYMBOL_GPL(nfs_file_operations);