file.c 25 KB

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