file.c 26 KB

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