file.c 26 KB

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