file.c 24 KB

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