file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 *, struct dentry *dentry, 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, struct dentry *dentry, int datasync)
  282. {
  283. struct nfs_open_context *ctx = nfs_file_open_context(file);
  284. struct inode *inode = dentry->d_inode;
  285. dprintk("NFS: fsync file(%s/%s) datasync %d\n",
  286. dentry->d_parent->d_name.name, dentry->d_name.name,
  287. datasync);
  288. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  289. return nfs_do_fsync(ctx, inode);
  290. }
  291. /*
  292. * Decide whether a read/modify/write cycle may be more efficient
  293. * then a modify/write/read cycle when writing to a page in the
  294. * page cache.
  295. *
  296. * The modify/write/read cycle may occur if a page is read before
  297. * being completely filled by the writer. In this situation, the
  298. * page must be completely written to stable storage on the server
  299. * before it can be refilled by reading in the page from the server.
  300. * This can lead to expensive, small, FILE_SYNC mode writes being
  301. * done.
  302. *
  303. * It may be more efficient to read the page first if the file is
  304. * open for reading in addition to writing, the page is not marked
  305. * as Uptodate, it is not dirty or waiting to be committed,
  306. * indicating that it was previously allocated and then modified,
  307. * that there were valid bytes of data in that range of the file,
  308. * and that the new data won't completely replace the old data in
  309. * that range of the file.
  310. */
  311. static int nfs_want_read_modify_write(struct file *file, struct page *page,
  312. loff_t pos, unsigned len)
  313. {
  314. unsigned int pglen = nfs_page_length(page);
  315. unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
  316. unsigned int end = offset + len;
  317. if ((file->f_mode & FMODE_READ) && /* open for read? */
  318. !PageUptodate(page) && /* Uptodate? */
  319. !PagePrivate(page) && /* i/o request already? */
  320. pglen && /* valid bytes of file? */
  321. (end < pglen || offset)) /* replace all valid bytes? */
  322. return 1;
  323. return 0;
  324. }
  325. /*
  326. * This does the "real" work of the write. We must allocate and lock the
  327. * page to be sent back to the generic routine, which then copies the
  328. * data from user space.
  329. *
  330. * If the writer ends up delaying the write, the writer needs to
  331. * increment the page use counts until he is done with the page.
  332. */
  333. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  334. loff_t pos, unsigned len, unsigned flags,
  335. struct page **pagep, void **fsdata)
  336. {
  337. int ret;
  338. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  339. struct page *page;
  340. int once_thru = 0;
  341. dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
  342. file->f_path.dentry->d_parent->d_name.name,
  343. file->f_path.dentry->d_name.name,
  344. mapping->host->i_ino, len, (long long) pos);
  345. start:
  346. /*
  347. * Prevent starvation issues if someone is doing a consistency
  348. * sync-to-disk
  349. */
  350. ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
  351. nfs_wait_bit_killable, TASK_KILLABLE);
  352. if (ret)
  353. return ret;
  354. page = grab_cache_page_write_begin(mapping, index, flags);
  355. if (!page)
  356. return -ENOMEM;
  357. *pagep = page;
  358. ret = nfs_flush_incompatible(file, page);
  359. if (ret) {
  360. unlock_page(page);
  361. page_cache_release(page);
  362. } else if (!once_thru &&
  363. nfs_want_read_modify_write(file, page, pos, len)) {
  364. once_thru = 1;
  365. ret = nfs_readpage(file, page);
  366. page_cache_release(page);
  367. if (!ret)
  368. goto start;
  369. }
  370. return ret;
  371. }
  372. static int nfs_write_end(struct file *file, struct address_space *mapping,
  373. loff_t pos, unsigned len, unsigned copied,
  374. struct page *page, void *fsdata)
  375. {
  376. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  377. int status;
  378. dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
  379. file->f_path.dentry->d_parent->d_name.name,
  380. file->f_path.dentry->d_name.name,
  381. mapping->host->i_ino, len, (long long) pos);
  382. /*
  383. * Zero any uninitialised parts of the page, and then mark the page
  384. * as up to date if it turns out that we're extending the file.
  385. */
  386. if (!PageUptodate(page)) {
  387. unsigned pglen = nfs_page_length(page);
  388. unsigned end = offset + len;
  389. if (pglen == 0) {
  390. zero_user_segments(page, 0, offset,
  391. end, PAGE_CACHE_SIZE);
  392. SetPageUptodate(page);
  393. } else if (end >= pglen) {
  394. zero_user_segment(page, end, PAGE_CACHE_SIZE);
  395. if (offset == 0)
  396. SetPageUptodate(page);
  397. } else
  398. zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
  399. }
  400. status = nfs_updatepage(file, page, offset, copied);
  401. unlock_page(page);
  402. page_cache_release(page);
  403. if (status < 0)
  404. return status;
  405. return copied;
  406. }
  407. /*
  408. * Partially or wholly invalidate a page
  409. * - Release the private state associated with a page if undergoing complete
  410. * page invalidation
  411. * - Called if either PG_private or PG_fscache is set on the page
  412. * - Caller holds page lock
  413. */
  414. static void nfs_invalidate_page(struct page *page, unsigned long offset)
  415. {
  416. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
  417. if (offset != 0)
  418. return;
  419. /* Cancel any unstarted writes on this page */
  420. nfs_wb_page_cancel(page->mapping->host, page);
  421. nfs_fscache_invalidate_page(page, page->mapping->host);
  422. }
  423. /*
  424. * Attempt to release the private state associated with a page
  425. * - Called if either PG_private or PG_fscache is set on the page
  426. * - Caller holds page lock
  427. * - Return true (may release page) or false (may not)
  428. */
  429. static int nfs_release_page(struct page *page, gfp_t gfp)
  430. {
  431. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  432. /* Only do I/O if gfp is a superset of GFP_KERNEL */
  433. if ((gfp & GFP_KERNEL) == GFP_KERNEL)
  434. nfs_wb_page(page->mapping->host, page);
  435. /* If PagePrivate() is set, then the page is not freeable */
  436. if (PagePrivate(page))
  437. return 0;
  438. return nfs_fscache_release_page(page, gfp);
  439. }
  440. /*
  441. * Attempt to clear the private state associated with a page when an error
  442. * occurs that requires the cached contents of an inode to be written back or
  443. * destroyed
  444. * - Called if either PG_private or fscache is set on the page
  445. * - Caller holds page lock
  446. * - Return 0 if successful, -error otherwise
  447. */
  448. static int nfs_launder_page(struct page *page)
  449. {
  450. struct inode *inode = page->mapping->host;
  451. struct nfs_inode *nfsi = NFS_I(inode);
  452. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  453. inode->i_ino, (long long)page_offset(page));
  454. nfs_fscache_wait_on_page_write(nfsi, page);
  455. return nfs_wb_page(inode, page);
  456. }
  457. const struct address_space_operations nfs_file_aops = {
  458. .readpage = nfs_readpage,
  459. .readpages = nfs_readpages,
  460. .set_page_dirty = __set_page_dirty_nobuffers,
  461. .writepage = nfs_writepage,
  462. .writepages = nfs_writepages,
  463. .write_begin = nfs_write_begin,
  464. .write_end = nfs_write_end,
  465. .invalidatepage = nfs_invalidate_page,
  466. .releasepage = nfs_release_page,
  467. .direct_IO = nfs_direct_IO,
  468. .migratepage = nfs_migrate_page,
  469. .launder_page = nfs_launder_page,
  470. .error_remove_page = generic_error_remove_page,
  471. };
  472. /*
  473. * Notification that a PTE pointing to an NFS page is about to be made
  474. * writable, implying that someone is about to modify the page through a
  475. * shared-writable mapping
  476. */
  477. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  478. {
  479. struct page *page = vmf->page;
  480. struct file *filp = vma->vm_file;
  481. struct dentry *dentry = filp->f_path.dentry;
  482. unsigned pagelen;
  483. int ret = -EINVAL;
  484. struct address_space *mapping;
  485. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
  486. dentry->d_parent->d_name.name, dentry->d_name.name,
  487. filp->f_mapping->host->i_ino,
  488. (long long)page_offset(page));
  489. /* make sure the cache has finished storing the page */
  490. nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
  491. lock_page(page);
  492. mapping = page->mapping;
  493. if (mapping != dentry->d_inode->i_mapping)
  494. goto out_unlock;
  495. ret = 0;
  496. pagelen = nfs_page_length(page);
  497. if (pagelen == 0)
  498. goto out_unlock;
  499. ret = nfs_flush_incompatible(filp, page);
  500. if (ret != 0)
  501. goto out_unlock;
  502. ret = nfs_updatepage(filp, page, 0, pagelen);
  503. out_unlock:
  504. if (!ret)
  505. return VM_FAULT_LOCKED;
  506. unlock_page(page);
  507. return VM_FAULT_SIGBUS;
  508. }
  509. static const struct vm_operations_struct nfs_file_vm_ops = {
  510. .fault = filemap_fault,
  511. .page_mkwrite = nfs_vm_page_mkwrite,
  512. };
  513. static int nfs_need_sync_write(struct file *filp, struct inode *inode)
  514. {
  515. struct nfs_open_context *ctx;
  516. if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
  517. return 1;
  518. ctx = nfs_file_open_context(filp);
  519. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
  520. return 1;
  521. return 0;
  522. }
  523. static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
  524. unsigned long nr_segs, loff_t pos)
  525. {
  526. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  527. struct inode * inode = dentry->d_inode;
  528. unsigned long written = 0;
  529. ssize_t result;
  530. size_t count = iov_length(iov, nr_segs);
  531. if (iocb->ki_filp->f_flags & O_DIRECT)
  532. return nfs_file_direct_write(iocb, iov, nr_segs, pos);
  533. dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
  534. dentry->d_parent->d_name.name, dentry->d_name.name,
  535. (unsigned long) count, (long long) pos);
  536. result = -EBUSY;
  537. if (IS_SWAPFILE(inode))
  538. goto out_swapfile;
  539. /*
  540. * O_APPEND implies that we must revalidate the file length.
  541. */
  542. if (iocb->ki_filp->f_flags & O_APPEND) {
  543. result = nfs_revalidate_file_size(inode, iocb->ki_filp);
  544. if (result)
  545. goto out;
  546. }
  547. result = count;
  548. if (!count)
  549. goto out;
  550. result = generic_file_aio_write(iocb, iov, nr_segs, pos);
  551. if (result > 0)
  552. written = result;
  553. /* Return error values for O_DSYNC and IS_SYNC() */
  554. if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
  555. int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
  556. if (err < 0)
  557. result = err;
  558. }
  559. if (result > 0)
  560. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  561. out:
  562. return result;
  563. out_swapfile:
  564. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  565. goto out;
  566. }
  567. static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
  568. struct file *filp, loff_t *ppos,
  569. size_t count, unsigned int flags)
  570. {
  571. struct dentry *dentry = filp->f_path.dentry;
  572. struct inode *inode = dentry->d_inode;
  573. unsigned long written = 0;
  574. ssize_t ret;
  575. dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
  576. dentry->d_parent->d_name.name, dentry->d_name.name,
  577. (unsigned long) count, (unsigned long long) *ppos);
  578. /*
  579. * The combination of splice and an O_APPEND destination is disallowed.
  580. */
  581. ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
  582. if (ret > 0)
  583. written = ret;
  584. if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
  585. int err = nfs_do_fsync(nfs_file_open_context(filp), inode);
  586. if (err < 0)
  587. ret = err;
  588. }
  589. if (ret > 0)
  590. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  591. return ret;
  592. }
  593. static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
  594. {
  595. struct inode *inode = filp->f_mapping->host;
  596. int status = 0;
  597. /* Try local locking first */
  598. posix_test_lock(filp, fl);
  599. if (fl->fl_type != F_UNLCK) {
  600. /* found a conflict */
  601. goto out;
  602. }
  603. if (nfs_have_delegation(inode, FMODE_READ))
  604. goto out_noconflict;
  605. if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
  606. goto out_noconflict;
  607. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  608. out:
  609. return status;
  610. out_noconflict:
  611. fl->fl_type = F_UNLCK;
  612. goto out;
  613. }
  614. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  615. {
  616. int res = 0;
  617. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  618. case FL_POSIX:
  619. res = posix_lock_file_wait(file, fl);
  620. break;
  621. case FL_FLOCK:
  622. res = flock_lock_file_wait(file, fl);
  623. break;
  624. default:
  625. BUG();
  626. }
  627. if (res < 0)
  628. dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
  629. " - error %d!\n",
  630. __func__, res);
  631. return res;
  632. }
  633. static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
  634. {
  635. struct inode *inode = filp->f_mapping->host;
  636. int status;
  637. /*
  638. * Flush all pending writes before doing anything
  639. * with locks..
  640. */
  641. nfs_sync_mapping(filp->f_mapping);
  642. /* NOTE: special case
  643. * If we're signalled while cleaning up locks on process exit, we
  644. * still need to complete the unlock.
  645. */
  646. /* Use local locking if mounted with "-onolock" */
  647. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  648. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  649. else
  650. status = do_vfs_lock(filp, fl);
  651. return status;
  652. }
  653. static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
  654. {
  655. struct inode *inode = filp->f_mapping->host;
  656. int status;
  657. /*
  658. * Flush all pending writes before doing anything
  659. * with locks..
  660. */
  661. status = nfs_sync_mapping(filp->f_mapping);
  662. if (status != 0)
  663. goto out;
  664. /* Use local locking if mounted with "-onolock" */
  665. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  666. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  667. else
  668. status = do_vfs_lock(filp, fl);
  669. if (status < 0)
  670. goto out;
  671. /*
  672. * Make sure we clear the cache whenever we try to get the lock.
  673. * This makes locking act as a cache coherency point.
  674. */
  675. nfs_sync_mapping(filp->f_mapping);
  676. if (!nfs_have_delegation(inode, FMODE_READ))
  677. nfs_zap_caches(inode);
  678. out:
  679. return status;
  680. }
  681. /*
  682. * Lock a (portion of) a file
  683. */
  684. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  685. {
  686. struct inode *inode = filp->f_mapping->host;
  687. int ret = -ENOLCK;
  688. dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
  689. filp->f_path.dentry->d_parent->d_name.name,
  690. filp->f_path.dentry->d_name.name,
  691. fl->fl_type, fl->fl_flags,
  692. (long long)fl->fl_start, (long long)fl->fl_end);
  693. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  694. /* No mandatory locks over NFS */
  695. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  696. goto out_err;
  697. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  698. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  699. if (ret < 0)
  700. goto out_err;
  701. }
  702. if (IS_GETLK(cmd))
  703. ret = do_getlk(filp, cmd, fl);
  704. else if (fl->fl_type == F_UNLCK)
  705. ret = do_unlk(filp, cmd, fl);
  706. else
  707. ret = do_setlk(filp, cmd, fl);
  708. out_err:
  709. return ret;
  710. }
  711. /*
  712. * Lock a (portion of) a file
  713. */
  714. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  715. {
  716. dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
  717. filp->f_path.dentry->d_parent->d_name.name,
  718. filp->f_path.dentry->d_name.name,
  719. fl->fl_type, fl->fl_flags);
  720. if (!(fl->fl_flags & FL_FLOCK))
  721. return -ENOLCK;
  722. /* We're simulating flock() locks using posix locks on the server */
  723. fl->fl_owner = (fl_owner_t)filp;
  724. fl->fl_start = 0;
  725. fl->fl_end = OFFSET_MAX;
  726. if (fl->fl_type == F_UNLCK)
  727. return do_unlk(filp, cmd, fl);
  728. return do_setlk(filp, cmd, fl);
  729. }
  730. /*
  731. * There is no protocol support for leases, so we have no way to implement
  732. * them correctly in the face of opens by other clients.
  733. */
  734. static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
  735. {
  736. dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
  737. file->f_path.dentry->d_parent->d_name.name,
  738. file->f_path.dentry->d_name.name, arg);
  739. return -EINVAL;
  740. }