file.c 23 KB

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