file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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, iocb->ki_pos);
  140. dprintk("NFS: read(%pD2, %zu@%lu)\n",
  141. iocb->ki_filp,
  142. iov_iter_count(to), (unsigned long) iocb->ki_pos);
  143. result = nfs_revalidate_mapping_protected(inode, iocb->ki_filp->f_mapping);
  144. if (!result) {
  145. result = generic_file_read_iter(iocb, to);
  146. if (result > 0)
  147. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
  148. }
  149. return result;
  150. }
  151. EXPORT_SYMBOL_GPL(nfs_file_read);
  152. ssize_t
  153. nfs_file_splice_read(struct file *filp, loff_t *ppos,
  154. struct pipe_inode_info *pipe, size_t count,
  155. unsigned int flags)
  156. {
  157. struct inode *inode = file_inode(filp);
  158. ssize_t res;
  159. dprintk("NFS: splice_read(%pD2, %lu@%Lu)\n",
  160. filp, (unsigned long) count, (unsigned long long) *ppos);
  161. res = nfs_revalidate_mapping_protected(inode, filp->f_mapping);
  162. if (!res) {
  163. res = generic_file_splice_read(filp, ppos, pipe, count, flags);
  164. if (res > 0)
  165. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
  166. }
  167. return res;
  168. }
  169. EXPORT_SYMBOL_GPL(nfs_file_splice_read);
  170. int
  171. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  172. {
  173. struct inode *inode = file_inode(file);
  174. int status;
  175. dprintk("NFS: mmap(%pD2)\n", file);
  176. /* Note: generic_file_mmap() returns ENOSYS on nommu systems
  177. * so we call that before revalidating the mapping
  178. */
  179. status = generic_file_mmap(file, vma);
  180. if (!status) {
  181. vma->vm_ops = &nfs_file_vm_ops;
  182. status = nfs_revalidate_mapping(inode, file->f_mapping);
  183. }
  184. return status;
  185. }
  186. EXPORT_SYMBOL_GPL(nfs_file_mmap);
  187. /*
  188. * Flush any dirty pages for this process, and check for write errors.
  189. * The return status from this call provides a reliable indication of
  190. * whether any write errors occurred for this process.
  191. *
  192. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  193. * disk, but it retrieves and clears ctx->error after synching, despite
  194. * the two being set at the same time in nfs_context_set_write_error().
  195. * This is because the former is used to notify the _next_ call to
  196. * nfs_file_write() that a write error occurred, and hence cause it to
  197. * fall back to doing a synchronous write.
  198. */
  199. int
  200. nfs_file_fsync_commit(struct file *file, loff_t start, loff_t end, int datasync)
  201. {
  202. struct nfs_open_context *ctx = nfs_file_open_context(file);
  203. struct inode *inode = file_inode(file);
  204. int have_error, do_resend, status;
  205. int ret = 0;
  206. dprintk("NFS: fsync file(%pD2) datasync %d\n", file, datasync);
  207. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  208. do_resend = test_and_clear_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  209. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  210. status = nfs_commit_inode(inode, FLUSH_SYNC);
  211. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  212. if (have_error) {
  213. ret = xchg(&ctx->error, 0);
  214. if (ret)
  215. goto out;
  216. }
  217. if (status < 0) {
  218. ret = status;
  219. goto out;
  220. }
  221. do_resend |= test_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
  222. if (do_resend)
  223. ret = -EAGAIN;
  224. out:
  225. return ret;
  226. }
  227. EXPORT_SYMBOL_GPL(nfs_file_fsync_commit);
  228. static int
  229. nfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  230. {
  231. int ret;
  232. struct inode *inode = file_inode(file);
  233. trace_nfs_fsync_enter(inode);
  234. nfs_inode_dio_wait(inode);
  235. do {
  236. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  237. if (ret != 0)
  238. break;
  239. mutex_lock(&inode->i_mutex);
  240. ret = nfs_file_fsync_commit(file, start, end, datasync);
  241. mutex_unlock(&inode->i_mutex);
  242. /*
  243. * If nfs_file_fsync_commit detected a server reboot, then
  244. * resend all dirty pages that might have been covered by
  245. * the NFS_CONTEXT_RESEND_WRITES flag
  246. */
  247. start = 0;
  248. end = LLONG_MAX;
  249. } while (ret == -EAGAIN);
  250. trace_nfs_fsync_exit(inode, ret);
  251. return ret;
  252. }
  253. /*
  254. * Decide whether a read/modify/write cycle may be more efficient
  255. * then a modify/write/read cycle when writing to a page in the
  256. * page cache.
  257. *
  258. * The modify/write/read cycle may occur if a page is read before
  259. * being completely filled by the writer. In this situation, the
  260. * page must be completely written to stable storage on the server
  261. * before it can be refilled by reading in the page from the server.
  262. * This can lead to expensive, small, FILE_SYNC mode writes being
  263. * done.
  264. *
  265. * It may be more efficient to read the page first if the file is
  266. * open for reading in addition to writing, the page is not marked
  267. * as Uptodate, it is not dirty or waiting to be committed,
  268. * indicating that it was previously allocated and then modified,
  269. * that there were valid bytes of data in that range of the file,
  270. * and that the new data won't completely replace the old data in
  271. * that range of the file.
  272. */
  273. static int nfs_want_read_modify_write(struct file *file, struct page *page,
  274. loff_t pos, unsigned len)
  275. {
  276. unsigned int pglen = nfs_page_length(page);
  277. unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
  278. unsigned int end = offset + len;
  279. if (pnfs_ld_read_whole_page(file->f_mapping->host)) {
  280. if (!PageUptodate(page))
  281. return 1;
  282. return 0;
  283. }
  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_action(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
  316. nfs_wait_bit_killable, TASK_KILLABLE);
  317. if (ret)
  318. return ret;
  319. /*
  320. * Wait for O_DIRECT to complete
  321. */
  322. nfs_inode_dio_wait(mapping->host);
  323. page = grab_cache_page_write_begin(mapping, index, flags);
  324. if (!page)
  325. return -ENOMEM;
  326. *pagep = page;
  327. ret = nfs_flush_incompatible(file, page);
  328. if (ret) {
  329. unlock_page(page);
  330. page_cache_release(page);
  331. } else if (!once_thru &&
  332. nfs_want_read_modify_write(file, page, pos, len)) {
  333. once_thru = 1;
  334. ret = nfs_readpage(file, page);
  335. page_cache_release(page);
  336. if (!ret)
  337. goto start;
  338. }
  339. return ret;
  340. }
  341. static int nfs_write_end(struct file *file, struct address_space *mapping,
  342. loff_t pos, unsigned len, unsigned copied,
  343. struct page *page, void *fsdata)
  344. {
  345. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  346. struct nfs_open_context *ctx = nfs_file_open_context(file);
  347. int status;
  348. dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
  349. file, mapping->host->i_ino, len, (long long) pos);
  350. /*
  351. * Zero any uninitialised parts of the page, and then mark the page
  352. * as up to date if it turns out that we're extending the file.
  353. */
  354. if (!PageUptodate(page)) {
  355. unsigned pglen = nfs_page_length(page);
  356. unsigned end = offset + copied;
  357. if (pglen == 0) {
  358. zero_user_segments(page, 0, offset,
  359. end, PAGE_CACHE_SIZE);
  360. SetPageUptodate(page);
  361. } else if (end >= pglen) {
  362. zero_user_segment(page, end, PAGE_CACHE_SIZE);
  363. if (offset == 0)
  364. SetPageUptodate(page);
  365. } else
  366. zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
  367. }
  368. status = nfs_updatepage(file, page, offset, copied);
  369. unlock_page(page);
  370. page_cache_release(page);
  371. if (status < 0)
  372. return status;
  373. NFS_I(mapping->host)->write_io += copied;
  374. if (nfs_ctx_key_to_expire(ctx)) {
  375. status = nfs_wb_all(mapping->host);
  376. if (status < 0)
  377. return status;
  378. }
  379. return copied;
  380. }
  381. /*
  382. * Partially or wholly invalidate a page
  383. * - Release the private state associated with a page if undergoing complete
  384. * page invalidation
  385. * - Called if either PG_private or PG_fscache is set on the page
  386. * - Caller holds page lock
  387. */
  388. static void nfs_invalidate_page(struct page *page, unsigned int offset,
  389. unsigned int length)
  390. {
  391. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
  392. page, offset, length);
  393. if (offset != 0 || length < PAGE_CACHE_SIZE)
  394. return;
  395. /* Cancel any unstarted writes on this page */
  396. nfs_wb_page_cancel(page_file_mapping(page)->host, page);
  397. nfs_fscache_invalidate_page(page, page->mapping->host);
  398. }
  399. /*
  400. * Attempt to release the private state associated with a page
  401. * - Called if either PG_private or PG_fscache is set on the page
  402. * - Caller holds page lock
  403. * - Return true (may release page) or false (may not)
  404. */
  405. static int nfs_release_page(struct page *page, gfp_t gfp)
  406. {
  407. struct address_space *mapping = page->mapping;
  408. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  409. /* Always try to initiate a 'commit' if relevant, but only
  410. * wait for it if the caller allows blocking. Even then,
  411. * only wait 1 second and only if the 'bdi' is not congested.
  412. * Waiting indefinitely can cause deadlocks when the NFS
  413. * server is on this machine, when a new TCP connection is
  414. * needed and in other rare cases. There is no particular
  415. * need to wait extensively here. A short wait has the
  416. * benefit that someone else can worry about the freezer.
  417. */
  418. if (mapping) {
  419. struct nfs_server *nfss = NFS_SERVER(mapping->host);
  420. nfs_commit_inode(mapping->host, 0);
  421. if (gfpflags_allow_blocking(gfp) &&
  422. !bdi_write_congested(&nfss->backing_dev_info)) {
  423. wait_on_page_bit_killable_timeout(page, PG_private,
  424. HZ);
  425. if (PagePrivate(page))
  426. set_bdi_congested(&nfss->backing_dev_info,
  427. BLK_RW_ASYNC);
  428. }
  429. }
  430. /* If PagePrivate() is set, then the page is not freeable */
  431. if (PagePrivate(page))
  432. return 0;
  433. return nfs_fscache_release_page(page, gfp);
  434. }
  435. static void nfs_check_dirty_writeback(struct page *page,
  436. bool *dirty, bool *writeback)
  437. {
  438. struct nfs_inode *nfsi;
  439. struct address_space *mapping = page_file_mapping(page);
  440. if (!mapping || PageSwapCache(page))
  441. return;
  442. /*
  443. * Check if an unstable page is currently being committed and
  444. * if so, have the VM treat it as if the page is under writeback
  445. * so it will not block due to pages that will shortly be freeable.
  446. */
  447. nfsi = NFS_I(mapping->host);
  448. if (test_bit(NFS_INO_COMMIT, &nfsi->flags)) {
  449. *writeback = true;
  450. return;
  451. }
  452. /*
  453. * If PagePrivate() is set, then the page is not freeable and as the
  454. * inode is not being committed, it's not going to be cleaned in the
  455. * near future so treat it as dirty
  456. */
  457. if (PagePrivate(page))
  458. *dirty = true;
  459. }
  460. /*
  461. * Attempt to clear the private state associated with a page when an error
  462. * occurs that requires the cached contents of an inode to be written back or
  463. * destroyed
  464. * - Called if either PG_private or fscache is set on the page
  465. * - Caller holds page lock
  466. * - Return 0 if successful, -error otherwise
  467. */
  468. static int nfs_launder_page(struct page *page)
  469. {
  470. struct inode *inode = page_file_mapping(page)->host;
  471. struct nfs_inode *nfsi = NFS_I(inode);
  472. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  473. inode->i_ino, (long long)page_offset(page));
  474. nfs_fscache_wait_on_page_write(nfsi, page);
  475. return nfs_wb_page(inode, page);
  476. }
  477. static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
  478. sector_t *span)
  479. {
  480. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  481. *span = sis->pages;
  482. return rpc_clnt_swap_activate(clnt);
  483. }
  484. static void nfs_swap_deactivate(struct file *file)
  485. {
  486. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  487. rpc_clnt_swap_deactivate(clnt);
  488. }
  489. const struct address_space_operations nfs_file_aops = {
  490. .readpage = nfs_readpage,
  491. .readpages = nfs_readpages,
  492. .set_page_dirty = __set_page_dirty_nobuffers,
  493. .writepage = nfs_writepage,
  494. .writepages = nfs_writepages,
  495. .write_begin = nfs_write_begin,
  496. .write_end = nfs_write_end,
  497. .invalidatepage = nfs_invalidate_page,
  498. .releasepage = nfs_release_page,
  499. .direct_IO = nfs_direct_IO,
  500. .migratepage = nfs_migrate_page,
  501. .launder_page = nfs_launder_page,
  502. .is_dirty_writeback = nfs_check_dirty_writeback,
  503. .error_remove_page = generic_error_remove_page,
  504. .swap_activate = nfs_swap_activate,
  505. .swap_deactivate = nfs_swap_deactivate,
  506. };
  507. /*
  508. * Notification that a PTE pointing to an NFS page is about to be made
  509. * writable, implying that someone is about to modify the page through a
  510. * shared-writable mapping
  511. */
  512. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  513. {
  514. struct page *page = vmf->page;
  515. struct file *filp = vma->vm_file;
  516. struct inode *inode = file_inode(filp);
  517. unsigned pagelen;
  518. int ret = VM_FAULT_NOPAGE;
  519. struct address_space *mapping;
  520. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
  521. filp, filp->f_mapping->host->i_ino,
  522. (long long)page_offset(page));
  523. /* make sure the cache has finished storing the page */
  524. nfs_fscache_wait_on_page_write(NFS_I(inode), page);
  525. wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
  526. nfs_wait_bit_killable, TASK_KILLABLE);
  527. lock_page(page);
  528. mapping = page_file_mapping(page);
  529. if (mapping != inode->i_mapping)
  530. goto out_unlock;
  531. wait_on_page_writeback(page);
  532. pagelen = nfs_page_length(page);
  533. if (pagelen == 0)
  534. goto out_unlock;
  535. ret = VM_FAULT_LOCKED;
  536. if (nfs_flush_incompatible(filp, page) == 0 &&
  537. nfs_updatepage(filp, page, 0, pagelen) == 0)
  538. goto out;
  539. ret = VM_FAULT_SIGBUS;
  540. out_unlock:
  541. unlock_page(page);
  542. out:
  543. return ret;
  544. }
  545. static const struct vm_operations_struct nfs_file_vm_ops = {
  546. .fault = filemap_fault,
  547. .map_pages = filemap_map_pages,
  548. .page_mkwrite = nfs_vm_page_mkwrite,
  549. };
  550. static int nfs_need_check_write(struct file *filp, struct inode *inode)
  551. {
  552. struct nfs_open_context *ctx;
  553. ctx = nfs_file_open_context(filp);
  554. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
  555. nfs_ctx_key_to_expire(ctx))
  556. return 1;
  557. return 0;
  558. }
  559. ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
  560. {
  561. struct file *file = iocb->ki_filp;
  562. struct inode *inode = file_inode(file);
  563. unsigned long written = 0;
  564. ssize_t result;
  565. size_t count = iov_iter_count(from);
  566. result = nfs_key_timeout_notify(file, inode);
  567. if (result)
  568. return result;
  569. if (iocb->ki_flags & IOCB_DIRECT) {
  570. result = generic_write_checks(iocb, from);
  571. if (result <= 0)
  572. return result;
  573. return nfs_file_direct_write(iocb, from);
  574. }
  575. dprintk("NFS: write(%pD2, %zu@%Ld)\n",
  576. file, count, (long long) iocb->ki_pos);
  577. result = -EBUSY;
  578. if (IS_SWAPFILE(inode))
  579. goto out_swapfile;
  580. /*
  581. * O_APPEND implies that we must revalidate the file length.
  582. */
  583. if (iocb->ki_flags & IOCB_APPEND) {
  584. result = nfs_revalidate_file_size(inode, file);
  585. if (result)
  586. goto out;
  587. }
  588. result = count;
  589. if (!count)
  590. goto out;
  591. result = generic_file_write_iter(iocb, from);
  592. if (result > 0)
  593. written = result;
  594. /* Return error values */
  595. if (result >= 0 && nfs_need_check_write(file, inode)) {
  596. int err = vfs_fsync(file, 0);
  597. if (err < 0)
  598. result = err;
  599. }
  600. if (result > 0)
  601. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  602. out:
  603. return result;
  604. out_swapfile:
  605. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  606. goto out;
  607. }
  608. EXPORT_SYMBOL_GPL(nfs_file_write);
  609. static int
  610. do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  611. {
  612. struct inode *inode = filp->f_mapping->host;
  613. int status = 0;
  614. unsigned int saved_type = fl->fl_type;
  615. /* Try local locking first */
  616. posix_test_lock(filp, fl);
  617. if (fl->fl_type != F_UNLCK) {
  618. /* found a conflict */
  619. goto out;
  620. }
  621. fl->fl_type = saved_type;
  622. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  623. goto out_noconflict;
  624. if (is_local)
  625. goto out_noconflict;
  626. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  627. out:
  628. return status;
  629. out_noconflict:
  630. fl->fl_type = F_UNLCK;
  631. goto out;
  632. }
  633. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  634. {
  635. return locks_lock_file_wait(file, fl);
  636. }
  637. static int
  638. do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  639. {
  640. struct inode *inode = filp->f_mapping->host;
  641. struct nfs_lock_context *l_ctx;
  642. int status;
  643. /*
  644. * Flush all pending writes before doing anything
  645. * with locks..
  646. */
  647. vfs_fsync(filp, 0);
  648. l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
  649. if (!IS_ERR(l_ctx)) {
  650. status = nfs_iocounter_wait(&l_ctx->io_count);
  651. nfs_put_lock_context(l_ctx);
  652. if (status < 0)
  653. return status;
  654. }
  655. /* NOTE: special case
  656. * If we're signalled while cleaning up locks on process exit, we
  657. * still need to complete the unlock.
  658. */
  659. /*
  660. * Use local locking if mounted with "-onolock" or with appropriate
  661. * "-olocal_lock="
  662. */
  663. if (!is_local)
  664. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  665. else
  666. status = do_vfs_lock(filp, fl);
  667. return status;
  668. }
  669. static int
  670. is_time_granular(struct timespec *ts) {
  671. return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000));
  672. }
  673. static int
  674. do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  675. {
  676. struct inode *inode = filp->f_mapping->host;
  677. int status;
  678. /*
  679. * Flush all pending writes before doing anything
  680. * with locks..
  681. */
  682. status = nfs_sync_mapping(filp->f_mapping);
  683. if (status != 0)
  684. goto out;
  685. /*
  686. * Use local locking if mounted with "-onolock" or with appropriate
  687. * "-olocal_lock="
  688. */
  689. if (!is_local)
  690. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  691. else
  692. status = do_vfs_lock(filp, fl);
  693. if (status < 0)
  694. goto out;
  695. /*
  696. * Revalidate the cache if the server has time stamps granular
  697. * enough to detect subsecond changes. Otherwise, clear the
  698. * cache to prevent missing any changes.
  699. *
  700. * This makes locking act as a cache coherency point.
  701. */
  702. nfs_sync_mapping(filp->f_mapping);
  703. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
  704. if (is_time_granular(&NFS_SERVER(inode)->time_delta))
  705. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  706. else
  707. nfs_zap_caches(inode);
  708. }
  709. out:
  710. return status;
  711. }
  712. /*
  713. * Lock a (portion of) a file
  714. */
  715. int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  716. {
  717. struct inode *inode = filp->f_mapping->host;
  718. int ret = -ENOLCK;
  719. int is_local = 0;
  720. dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
  721. filp, fl->fl_type, fl->fl_flags,
  722. (long long)fl->fl_start, (long long)fl->fl_end);
  723. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  724. /* No mandatory locks over NFS */
  725. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  726. goto out_err;
  727. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
  728. is_local = 1;
  729. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  730. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  731. if (ret < 0)
  732. goto out_err;
  733. }
  734. if (IS_GETLK(cmd))
  735. ret = do_getlk(filp, cmd, fl, is_local);
  736. else if (fl->fl_type == F_UNLCK)
  737. ret = do_unlk(filp, cmd, fl, is_local);
  738. else
  739. ret = do_setlk(filp, cmd, fl, is_local);
  740. out_err:
  741. return ret;
  742. }
  743. EXPORT_SYMBOL_GPL(nfs_lock);
  744. /*
  745. * Lock a (portion of) a file
  746. */
  747. int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  748. {
  749. struct inode *inode = filp->f_mapping->host;
  750. int is_local = 0;
  751. dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
  752. filp, fl->fl_type, fl->fl_flags);
  753. if (!(fl->fl_flags & FL_FLOCK))
  754. return -ENOLCK;
  755. /*
  756. * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
  757. * any standard. In principle we might be able to support LOCK_MAND
  758. * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
  759. * NFS code is not set up for it.
  760. */
  761. if (fl->fl_type & LOCK_MAND)
  762. return -EINVAL;
  763. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
  764. is_local = 1;
  765. /* We're simulating flock() locks using posix locks on the server */
  766. if (fl->fl_type == F_UNLCK)
  767. return do_unlk(filp, cmd, fl, is_local);
  768. return do_setlk(filp, cmd, fl, is_local);
  769. }
  770. EXPORT_SYMBOL_GPL(nfs_flock);
  771. const struct file_operations nfs_file_operations = {
  772. .llseek = nfs_file_llseek,
  773. .read_iter = nfs_file_read,
  774. .write_iter = nfs_file_write,
  775. .mmap = nfs_file_mmap,
  776. .open = nfs_file_open,
  777. .flush = nfs_file_flush,
  778. .release = nfs_file_release,
  779. .fsync = nfs_file_fsync,
  780. .lock = nfs_lock,
  781. .flock = nfs_flock,
  782. .splice_read = nfs_file_splice_read,
  783. .splice_write = iter_file_splice_write,
  784. .check_flags = nfs_check_flags,
  785. .setlease = simple_nosetlease,
  786. };
  787. EXPORT_SYMBOL_GPL(nfs_file_operations);