file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. 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_flags & IOCB_DIRECT)
  145. return nfs_file_direct_read(iocb, to, iocb->ki_pos);
  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_protected(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_protected(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 (pnfs_ld_read_whole_page(file->f_mapping->host)) {
  285. if (!PageUptodate(page))
  286. return 1;
  287. return 0;
  288. }
  289. if ((file->f_mode & FMODE_READ) && /* open for read? */
  290. !PageUptodate(page) && /* Uptodate? */
  291. !PagePrivate(page) && /* i/o request already? */
  292. pglen && /* valid bytes of file? */
  293. (end < pglen || offset)) /* replace all valid bytes? */
  294. return 1;
  295. return 0;
  296. }
  297. /*
  298. * This does the "real" work of the write. We must allocate and lock the
  299. * page to be sent back to the generic routine, which then copies the
  300. * data from user space.
  301. *
  302. * If the writer ends up delaying the write, the writer needs to
  303. * increment the page use counts until he is done with the page.
  304. */
  305. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  306. loff_t pos, unsigned len, unsigned flags,
  307. struct page **pagep, void **fsdata)
  308. {
  309. int ret;
  310. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  311. struct page *page;
  312. int once_thru = 0;
  313. dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
  314. file, mapping->host->i_ino, len, (long long) pos);
  315. start:
  316. /*
  317. * Prevent starvation issues if someone is doing a consistency
  318. * sync-to-disk
  319. */
  320. ret = wait_on_bit_action(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
  321. nfs_wait_bit_killable, TASK_KILLABLE);
  322. if (ret)
  323. return ret;
  324. /*
  325. * Wait for O_DIRECT to complete
  326. */
  327. nfs_inode_dio_wait(mapping->host);
  328. page = grab_cache_page_write_begin(mapping, index, flags);
  329. if (!page)
  330. return -ENOMEM;
  331. *pagep = page;
  332. ret = nfs_flush_incompatible(file, page);
  333. if (ret) {
  334. unlock_page(page);
  335. page_cache_release(page);
  336. } else if (!once_thru &&
  337. nfs_want_read_modify_write(file, page, pos, len)) {
  338. once_thru = 1;
  339. ret = nfs_readpage(file, page);
  340. page_cache_release(page);
  341. if (!ret)
  342. goto start;
  343. }
  344. return ret;
  345. }
  346. static int nfs_write_end(struct file *file, struct address_space *mapping,
  347. loff_t pos, unsigned len, unsigned copied,
  348. struct page *page, void *fsdata)
  349. {
  350. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  351. struct nfs_open_context *ctx = nfs_file_open_context(file);
  352. int status;
  353. dfprintk(PAGECACHE, "NFS: write_end(%pD2(%lu), %u@%lld)\n",
  354. file, mapping->host->i_ino, len, (long long) pos);
  355. /*
  356. * Zero any uninitialised parts of the page, and then mark the page
  357. * as up to date if it turns out that we're extending the file.
  358. */
  359. if (!PageUptodate(page)) {
  360. unsigned pglen = nfs_page_length(page);
  361. unsigned end = offset + len;
  362. if (pglen == 0) {
  363. zero_user_segments(page, 0, offset,
  364. end, PAGE_CACHE_SIZE);
  365. SetPageUptodate(page);
  366. } else if (end >= pglen) {
  367. zero_user_segment(page, end, PAGE_CACHE_SIZE);
  368. if (offset == 0)
  369. SetPageUptodate(page);
  370. } else
  371. zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
  372. }
  373. status = nfs_updatepage(file, page, offset, copied);
  374. unlock_page(page);
  375. page_cache_release(page);
  376. if (status < 0)
  377. return status;
  378. NFS_I(mapping->host)->write_io += copied;
  379. if (nfs_ctx_key_to_expire(ctx)) {
  380. status = nfs_wb_all(mapping->host);
  381. if (status < 0)
  382. return status;
  383. }
  384. return copied;
  385. }
  386. /*
  387. * Partially or wholly invalidate a page
  388. * - Release the private state associated with a page if undergoing complete
  389. * page invalidation
  390. * - Called if either PG_private or PG_fscache is set on the page
  391. * - Caller holds page lock
  392. */
  393. static void nfs_invalidate_page(struct page *page, unsigned int offset,
  394. unsigned int length)
  395. {
  396. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %u, %u)\n",
  397. page, offset, length);
  398. if (offset != 0 || length < PAGE_CACHE_SIZE)
  399. return;
  400. /* Cancel any unstarted writes on this page */
  401. nfs_wb_page_cancel(page_file_mapping(page)->host, page);
  402. nfs_fscache_invalidate_page(page, page->mapping->host);
  403. }
  404. /*
  405. * Attempt to release the private state associated with a page
  406. * - Called if either PG_private or PG_fscache is set on the page
  407. * - Caller holds page lock
  408. * - Return true (may release page) or false (may not)
  409. */
  410. static int nfs_release_page(struct page *page, gfp_t gfp)
  411. {
  412. struct address_space *mapping = page->mapping;
  413. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  414. /* Always try to initiate a 'commit' if relevant, but only
  415. * wait for it if __GFP_WAIT is set. Even then, only wait 1
  416. * second and only if the 'bdi' is not congested.
  417. * Waiting indefinitely can cause deadlocks when the NFS
  418. * server is on this machine, when a new TCP connection is
  419. * needed and in other rare cases. There is no particular
  420. * need to wait extensively here. A short wait has the
  421. * benefit that someone else can worry about the freezer.
  422. */
  423. if (mapping) {
  424. struct nfs_server *nfss = NFS_SERVER(mapping->host);
  425. nfs_commit_inode(mapping->host, 0);
  426. if ((gfp & __GFP_WAIT) &&
  427. !bdi_write_congested(&nfss->backing_dev_info)) {
  428. wait_on_page_bit_killable_timeout(page, PG_private,
  429. HZ);
  430. if (PagePrivate(page))
  431. set_bdi_congested(&nfss->backing_dev_info,
  432. BLK_RW_ASYNC);
  433. }
  434. }
  435. /* If PagePrivate() is set, then the page is not freeable */
  436. if (PagePrivate(page))
  437. return 0;
  438. return nfs_fscache_release_page(page, gfp);
  439. }
  440. static void nfs_check_dirty_writeback(struct page *page,
  441. bool *dirty, bool *writeback)
  442. {
  443. struct nfs_inode *nfsi;
  444. struct address_space *mapping = page_file_mapping(page);
  445. if (!mapping || PageSwapCache(page))
  446. return;
  447. /*
  448. * Check if an unstable page is currently being committed and
  449. * if so, have the VM treat it as if the page is under writeback
  450. * so it will not block due to pages that will shortly be freeable.
  451. */
  452. nfsi = NFS_I(mapping->host);
  453. if (test_bit(NFS_INO_COMMIT, &nfsi->flags)) {
  454. *writeback = true;
  455. return;
  456. }
  457. /*
  458. * If PagePrivate() is set, then the page is not freeable and as the
  459. * inode is not being committed, it's not going to be cleaned in the
  460. * near future so treat it as dirty
  461. */
  462. if (PagePrivate(page))
  463. *dirty = true;
  464. }
  465. /*
  466. * Attempt to clear the private state associated with a page when an error
  467. * occurs that requires the cached contents of an inode to be written back or
  468. * destroyed
  469. * - Called if either PG_private or fscache is set on the page
  470. * - Caller holds page lock
  471. * - Return 0 if successful, -error otherwise
  472. */
  473. static int nfs_launder_page(struct page *page)
  474. {
  475. struct inode *inode = page_file_mapping(page)->host;
  476. struct nfs_inode *nfsi = NFS_I(inode);
  477. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  478. inode->i_ino, (long long)page_offset(page));
  479. nfs_fscache_wait_on_page_write(nfsi, page);
  480. return nfs_wb_page(inode, page);
  481. }
  482. #ifdef CONFIG_NFS_SWAP
  483. static int nfs_swap_activate(struct swap_info_struct *sis, struct file *file,
  484. sector_t *span)
  485. {
  486. int ret;
  487. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  488. *span = sis->pages;
  489. rcu_read_lock();
  490. ret = xs_swapper(rcu_dereference(clnt->cl_xprt), 1);
  491. rcu_read_unlock();
  492. return ret;
  493. }
  494. static void nfs_swap_deactivate(struct file *file)
  495. {
  496. struct rpc_clnt *clnt = NFS_CLIENT(file->f_mapping->host);
  497. rcu_read_lock();
  498. xs_swapper(rcu_dereference(clnt->cl_xprt), 0);
  499. rcu_read_unlock();
  500. }
  501. #endif
  502. const struct address_space_operations nfs_file_aops = {
  503. .readpage = nfs_readpage,
  504. .readpages = nfs_readpages,
  505. .set_page_dirty = __set_page_dirty_nobuffers,
  506. .writepage = nfs_writepage,
  507. .writepages = nfs_writepages,
  508. .write_begin = nfs_write_begin,
  509. .write_end = nfs_write_end,
  510. .invalidatepage = nfs_invalidate_page,
  511. .releasepage = nfs_release_page,
  512. .direct_IO = nfs_direct_IO,
  513. .migratepage = nfs_migrate_page,
  514. .launder_page = nfs_launder_page,
  515. .is_dirty_writeback = nfs_check_dirty_writeback,
  516. .error_remove_page = generic_error_remove_page,
  517. #ifdef CONFIG_NFS_SWAP
  518. .swap_activate = nfs_swap_activate,
  519. .swap_deactivate = nfs_swap_deactivate,
  520. #endif
  521. };
  522. /*
  523. * Notification that a PTE pointing to an NFS page is about to be made
  524. * writable, implying that someone is about to modify the page through a
  525. * shared-writable mapping
  526. */
  527. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  528. {
  529. struct page *page = vmf->page;
  530. struct file *filp = vma->vm_file;
  531. struct inode *inode = file_inode(filp);
  532. unsigned pagelen;
  533. int ret = VM_FAULT_NOPAGE;
  534. struct address_space *mapping;
  535. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%pD2(%lu), offset %lld)\n",
  536. filp, filp->f_mapping->host->i_ino,
  537. (long long)page_offset(page));
  538. /* make sure the cache has finished storing the page */
  539. nfs_fscache_wait_on_page_write(NFS_I(inode), page);
  540. wait_on_bit_action(&NFS_I(inode)->flags, NFS_INO_INVALIDATING,
  541. nfs_wait_bit_killable, TASK_KILLABLE);
  542. lock_page(page);
  543. mapping = page_file_mapping(page);
  544. if (mapping != inode->i_mapping)
  545. goto out_unlock;
  546. wait_on_page_writeback(page);
  547. pagelen = nfs_page_length(page);
  548. if (pagelen == 0)
  549. goto out_unlock;
  550. ret = VM_FAULT_LOCKED;
  551. if (nfs_flush_incompatible(filp, page) == 0 &&
  552. nfs_updatepage(filp, page, 0, pagelen) == 0)
  553. goto out;
  554. ret = VM_FAULT_SIGBUS;
  555. out_unlock:
  556. unlock_page(page);
  557. out:
  558. return ret;
  559. }
  560. static const struct vm_operations_struct nfs_file_vm_ops = {
  561. .fault = filemap_fault,
  562. .map_pages = filemap_map_pages,
  563. .page_mkwrite = nfs_vm_page_mkwrite,
  564. };
  565. static int nfs_need_sync_write(struct file *filp, struct inode *inode)
  566. {
  567. struct nfs_open_context *ctx;
  568. if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
  569. return 1;
  570. ctx = nfs_file_open_context(filp);
  571. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags) ||
  572. nfs_ctx_key_to_expire(ctx))
  573. return 1;
  574. return 0;
  575. }
  576. ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
  577. {
  578. struct file *file = iocb->ki_filp;
  579. struct inode *inode = file_inode(file);
  580. unsigned long written = 0;
  581. ssize_t result;
  582. size_t count = iov_iter_count(from);
  583. result = nfs_key_timeout_notify(file, inode);
  584. if (result)
  585. return result;
  586. if (iocb->ki_flags & IOCB_DIRECT) {
  587. result = generic_write_checks(iocb, from);
  588. if (result <= 0)
  589. return result;
  590. return nfs_file_direct_write(iocb, from);
  591. }
  592. dprintk("NFS: write(%pD2, %zu@%Ld)\n",
  593. file, count, (long long) iocb->ki_pos);
  594. result = -EBUSY;
  595. if (IS_SWAPFILE(inode))
  596. goto out_swapfile;
  597. /*
  598. * O_APPEND implies that we must revalidate the file length.
  599. */
  600. if (iocb->ki_flags & IOCB_APPEND) {
  601. result = nfs_revalidate_file_size(inode, file);
  602. if (result)
  603. goto out;
  604. }
  605. result = count;
  606. if (!count)
  607. goto out;
  608. result = generic_file_write_iter(iocb, from);
  609. if (result > 0)
  610. written = result;
  611. /* Return error values for O_DSYNC and IS_SYNC() */
  612. if (result >= 0 && nfs_need_sync_write(file, inode)) {
  613. int err = vfs_fsync(file, 0);
  614. if (err < 0)
  615. result = err;
  616. }
  617. if (result > 0)
  618. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  619. out:
  620. return result;
  621. out_swapfile:
  622. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  623. goto out;
  624. }
  625. EXPORT_SYMBOL_GPL(nfs_file_write);
  626. static int
  627. do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  628. {
  629. struct inode *inode = filp->f_mapping->host;
  630. int status = 0;
  631. unsigned int saved_type = fl->fl_type;
  632. /* Try local locking first */
  633. posix_test_lock(filp, fl);
  634. if (fl->fl_type != F_UNLCK) {
  635. /* found a conflict */
  636. goto out;
  637. }
  638. fl->fl_type = saved_type;
  639. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_READ))
  640. goto out_noconflict;
  641. if (is_local)
  642. goto out_noconflict;
  643. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  644. out:
  645. return status;
  646. out_noconflict:
  647. fl->fl_type = F_UNLCK;
  648. goto out;
  649. }
  650. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  651. {
  652. int res = 0;
  653. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  654. case FL_POSIX:
  655. res = posix_lock_file_wait(file, fl);
  656. break;
  657. case FL_FLOCK:
  658. res = flock_lock_file_wait(file, fl);
  659. break;
  660. default:
  661. BUG();
  662. }
  663. return res;
  664. }
  665. static int
  666. do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  667. {
  668. struct inode *inode = filp->f_mapping->host;
  669. struct nfs_lock_context *l_ctx;
  670. int status;
  671. /*
  672. * Flush all pending writes before doing anything
  673. * with locks..
  674. */
  675. nfs_sync_mapping(filp->f_mapping);
  676. l_ctx = nfs_get_lock_context(nfs_file_open_context(filp));
  677. if (!IS_ERR(l_ctx)) {
  678. status = nfs_iocounter_wait(&l_ctx->io_count);
  679. nfs_put_lock_context(l_ctx);
  680. if (status < 0)
  681. return status;
  682. }
  683. /* NOTE: special case
  684. * If we're signalled while cleaning up locks on process exit, we
  685. * still need to complete the unlock.
  686. */
  687. /*
  688. * Use local locking if mounted with "-onolock" or with appropriate
  689. * "-olocal_lock="
  690. */
  691. if (!is_local)
  692. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  693. else
  694. status = do_vfs_lock(filp, fl);
  695. return status;
  696. }
  697. static int
  698. is_time_granular(struct timespec *ts) {
  699. return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000));
  700. }
  701. static int
  702. do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  703. {
  704. struct inode *inode = filp->f_mapping->host;
  705. int status;
  706. /*
  707. * Flush all pending writes before doing anything
  708. * with locks..
  709. */
  710. status = nfs_sync_mapping(filp->f_mapping);
  711. if (status != 0)
  712. goto out;
  713. /*
  714. * Use local locking if mounted with "-onolock" or with appropriate
  715. * "-olocal_lock="
  716. */
  717. if (!is_local)
  718. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  719. else
  720. status = do_vfs_lock(filp, fl);
  721. if (status < 0)
  722. goto out;
  723. /*
  724. * Revalidate the cache if the server has time stamps granular
  725. * enough to detect subsecond changes. Otherwise, clear the
  726. * cache to prevent missing any changes.
  727. *
  728. * This makes locking act as a cache coherency point.
  729. */
  730. nfs_sync_mapping(filp->f_mapping);
  731. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) {
  732. if (is_time_granular(&NFS_SERVER(inode)->time_delta))
  733. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  734. else
  735. nfs_zap_caches(inode);
  736. }
  737. out:
  738. return status;
  739. }
  740. /*
  741. * Lock a (portion of) a file
  742. */
  743. int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  744. {
  745. struct inode *inode = filp->f_mapping->host;
  746. int ret = -ENOLCK;
  747. int is_local = 0;
  748. dprintk("NFS: lock(%pD2, t=%x, fl=%x, r=%lld:%lld)\n",
  749. filp, fl->fl_type, fl->fl_flags,
  750. (long long)fl->fl_start, (long long)fl->fl_end);
  751. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  752. /* No mandatory locks over NFS */
  753. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  754. goto out_err;
  755. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
  756. is_local = 1;
  757. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  758. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  759. if (ret < 0)
  760. goto out_err;
  761. }
  762. if (IS_GETLK(cmd))
  763. ret = do_getlk(filp, cmd, fl, is_local);
  764. else if (fl->fl_type == F_UNLCK)
  765. ret = do_unlk(filp, cmd, fl, is_local);
  766. else
  767. ret = do_setlk(filp, cmd, fl, is_local);
  768. out_err:
  769. return ret;
  770. }
  771. EXPORT_SYMBOL_GPL(nfs_lock);
  772. /*
  773. * Lock a (portion of) a file
  774. */
  775. int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  776. {
  777. struct inode *inode = filp->f_mapping->host;
  778. int is_local = 0;
  779. dprintk("NFS: flock(%pD2, t=%x, fl=%x)\n",
  780. filp, fl->fl_type, fl->fl_flags);
  781. if (!(fl->fl_flags & FL_FLOCK))
  782. return -ENOLCK;
  783. /*
  784. * The NFSv4 protocol doesn't support LOCK_MAND, which is not part of
  785. * any standard. In principle we might be able to support LOCK_MAND
  786. * on NFSv2/3 since NLMv3/4 support DOS share modes, but for now the
  787. * NFS code is not set up for it.
  788. */
  789. if (fl->fl_type & LOCK_MAND)
  790. return -EINVAL;
  791. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
  792. is_local = 1;
  793. /* We're simulating flock() locks using posix locks on the server */
  794. if (fl->fl_type == F_UNLCK)
  795. return do_unlk(filp, cmd, fl, is_local);
  796. return do_setlk(filp, cmd, fl, is_local);
  797. }
  798. EXPORT_SYMBOL_GPL(nfs_flock);
  799. const struct file_operations nfs_file_operations = {
  800. .llseek = nfs_file_llseek,
  801. .read_iter = nfs_file_read,
  802. .write_iter = nfs_file_write,
  803. .mmap = nfs_file_mmap,
  804. .open = nfs_file_open,
  805. .flush = nfs_file_flush,
  806. .release = nfs_file_release,
  807. .fsync = nfs_file_fsync,
  808. .lock = nfs_lock,
  809. .flock = nfs_flock,
  810. .splice_read = nfs_file_splice_read,
  811. .splice_write = iter_file_splice_write,
  812. .check_flags = nfs_check_flags,
  813. .setlease = simple_nosetlease,
  814. };
  815. EXPORT_SYMBOL_GPL(nfs_file_operations);