file.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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/slab.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/aio.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 *, struct dentry *dentry, 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 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. res = nfs_check_flags(filp->f_flags);
  113. if (res)
  114. return res;
  115. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  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 (server->flags & NFS_MOUNT_NOAC)
  145. goto force_reval;
  146. if (filp->f_flags & O_DIRECT)
  147. goto force_reval;
  148. if (nfsi->npages != 0)
  149. return 0;
  150. if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode))
  151. return 0;
  152. force_reval:
  153. return __nfs_revalidate_inode(server, inode);
  154. }
  155. static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
  156. {
  157. loff_t loff;
  158. dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
  159. filp->f_path.dentry->d_parent->d_name.name,
  160. filp->f_path.dentry->d_name.name,
  161. offset, origin);
  162. /* origin == SEEK_END => we must revalidate the cached file length */
  163. if (origin == SEEK_END) {
  164. struct inode *inode = filp->f_mapping->host;
  165. int retval = nfs_revalidate_file_size(inode, filp);
  166. if (retval < 0)
  167. return (loff_t)retval;
  168. spin_lock(&inode->i_lock);
  169. loff = generic_file_llseek_unlocked(filp, offset, origin);
  170. spin_unlock(&inode->i_lock);
  171. } else
  172. loff = generic_file_llseek_unlocked(filp, offset, origin);
  173. return loff;
  174. }
  175. /*
  176. * Helper for nfs_file_flush() and nfs_file_fsync()
  177. *
  178. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  179. * disk, but it retrieves and clears ctx->error after synching, despite
  180. * the two being set at the same time in nfs_context_set_write_error().
  181. * This is because the former is used to notify the _next_ call to
  182. * nfs_file_write() that a write error occured, and hence cause it to
  183. * fall back to doing a synchronous write.
  184. */
  185. static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
  186. {
  187. int have_error, status;
  188. int ret = 0;
  189. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  190. status = nfs_wb_all(inode);
  191. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  192. if (have_error)
  193. ret = xchg(&ctx->error, 0);
  194. if (!ret)
  195. ret = status;
  196. return ret;
  197. }
  198. /*
  199. * Flush all dirty pages, and check for write errors.
  200. */
  201. static int
  202. nfs_file_flush(struct file *file, fl_owner_t id)
  203. {
  204. struct nfs_open_context *ctx = nfs_file_open_context(file);
  205. struct dentry *dentry = file->f_path.dentry;
  206. struct inode *inode = dentry->d_inode;
  207. dprintk("NFS: flush(%s/%s)\n",
  208. dentry->d_parent->d_name.name,
  209. dentry->d_name.name);
  210. if ((file->f_mode & FMODE_WRITE) == 0)
  211. return 0;
  212. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  213. /* Flush writes to the server and return any errors */
  214. return nfs_do_fsync(ctx, inode);
  215. }
  216. static ssize_t
  217. nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
  218. unsigned long nr_segs, loff_t pos)
  219. {
  220. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  221. struct inode * inode = dentry->d_inode;
  222. ssize_t result;
  223. size_t count = iov_length(iov, nr_segs);
  224. if (iocb->ki_filp->f_flags & O_DIRECT)
  225. return nfs_file_direct_read(iocb, iov, nr_segs, pos);
  226. dprintk("NFS: read(%s/%s, %lu@%lu)\n",
  227. dentry->d_parent->d_name.name, dentry->d_name.name,
  228. (unsigned long) count, (unsigned long) pos);
  229. result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  230. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count);
  231. if (!result)
  232. result = generic_file_aio_read(iocb, iov, nr_segs, pos);
  233. return result;
  234. }
  235. static ssize_t
  236. nfs_file_splice_read(struct file *filp, loff_t *ppos,
  237. struct pipe_inode_info *pipe, size_t count,
  238. unsigned int flags)
  239. {
  240. struct dentry *dentry = filp->f_path.dentry;
  241. struct inode *inode = dentry->d_inode;
  242. ssize_t res;
  243. dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
  244. dentry->d_parent->d_name.name, dentry->d_name.name,
  245. (unsigned long) count, (unsigned long long) *ppos);
  246. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  247. if (!res)
  248. res = generic_file_splice_read(filp, ppos, pipe, count, flags);
  249. return res;
  250. }
  251. static int
  252. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  253. {
  254. struct dentry *dentry = file->f_path.dentry;
  255. struct inode *inode = dentry->d_inode;
  256. int status;
  257. dprintk("NFS: mmap(%s/%s)\n",
  258. dentry->d_parent->d_name.name, dentry->d_name.name);
  259. /* Note: generic_file_mmap() returns ENOSYS on nommu systems
  260. * so we call that before revalidating the mapping
  261. */
  262. status = generic_file_mmap(file, vma);
  263. if (!status) {
  264. vma->vm_ops = &nfs_file_vm_ops;
  265. status = nfs_revalidate_mapping(inode, file->f_mapping);
  266. }
  267. return status;
  268. }
  269. /*
  270. * Flush any dirty pages for this process, and check for write errors.
  271. * The return status from this call provides a reliable indication of
  272. * whether any write errors occurred for this process.
  273. */
  274. static int
  275. nfs_file_fsync(struct file *file, struct dentry *dentry, int datasync)
  276. {
  277. struct nfs_open_context *ctx = nfs_file_open_context(file);
  278. struct inode *inode = dentry->d_inode;
  279. dprintk("NFS: fsync file(%s/%s) datasync %d\n",
  280. dentry->d_parent->d_name.name, dentry->d_name.name,
  281. datasync);
  282. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  283. return nfs_do_fsync(ctx, inode);
  284. }
  285. /*
  286. * This does the "real" work of the write. We must allocate and lock the
  287. * page to be sent back to the generic routine, which then copies the
  288. * data from user space.
  289. *
  290. * If the writer ends up delaying the write, the writer needs to
  291. * increment the page use counts until he is done with the page.
  292. */
  293. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  294. loff_t pos, unsigned len, unsigned flags,
  295. struct page **pagep, void **fsdata)
  296. {
  297. int ret;
  298. pgoff_t index;
  299. struct page *page;
  300. index = pos >> PAGE_CACHE_SHIFT;
  301. dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
  302. file->f_path.dentry->d_parent->d_name.name,
  303. file->f_path.dentry->d_name.name,
  304. mapping->host->i_ino, len, (long long) pos);
  305. /*
  306. * Prevent starvation issues if someone is doing a consistency
  307. * sync-to-disk
  308. */
  309. ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
  310. nfs_wait_bit_killable, TASK_KILLABLE);
  311. if (ret)
  312. return ret;
  313. page = grab_cache_page_write_begin(mapping, index, flags);
  314. if (!page)
  315. return -ENOMEM;
  316. *pagep = page;
  317. ret = nfs_flush_incompatible(file, page);
  318. if (ret) {
  319. unlock_page(page);
  320. page_cache_release(page);
  321. }
  322. return ret;
  323. }
  324. static int nfs_write_end(struct file *file, struct address_space *mapping,
  325. loff_t pos, unsigned len, unsigned copied,
  326. struct page *page, void *fsdata)
  327. {
  328. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  329. int status;
  330. dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
  331. file->f_path.dentry->d_parent->d_name.name,
  332. file->f_path.dentry->d_name.name,
  333. mapping->host->i_ino, len, (long long) pos);
  334. /*
  335. * Zero any uninitialised parts of the page, and then mark the page
  336. * as up to date if it turns out that we're extending the file.
  337. */
  338. if (!PageUptodate(page)) {
  339. unsigned pglen = nfs_page_length(page);
  340. unsigned end = offset + len;
  341. if (pglen == 0) {
  342. zero_user_segments(page, 0, offset,
  343. end, PAGE_CACHE_SIZE);
  344. SetPageUptodate(page);
  345. } else if (end >= pglen) {
  346. zero_user_segment(page, end, PAGE_CACHE_SIZE);
  347. if (offset == 0)
  348. SetPageUptodate(page);
  349. } else
  350. zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
  351. }
  352. status = nfs_updatepage(file, page, offset, copied);
  353. unlock_page(page);
  354. page_cache_release(page);
  355. if (status < 0)
  356. return status;
  357. return copied;
  358. }
  359. /*
  360. * Partially or wholly invalidate a page
  361. * - Release the private state associated with a page if undergoing complete
  362. * page invalidation
  363. * - Called if either PG_private or PG_fscache is set on the page
  364. * - Caller holds page lock
  365. */
  366. static void nfs_invalidate_page(struct page *page, unsigned long offset)
  367. {
  368. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
  369. if (offset != 0)
  370. return;
  371. /* Cancel any unstarted writes on this page */
  372. nfs_wb_page_cancel(page->mapping->host, page);
  373. nfs_fscache_invalidate_page(page, page->mapping->host);
  374. }
  375. /*
  376. * Attempt to release the private state associated with a page
  377. * - Called if either PG_private or PG_fscache is set on the page
  378. * - Caller holds page lock
  379. * - Return true (may release page) or false (may not)
  380. */
  381. static int nfs_release_page(struct page *page, gfp_t gfp)
  382. {
  383. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  384. /* If PagePrivate() is set, then the page is not freeable */
  385. if (PagePrivate(page))
  386. return 0;
  387. return nfs_fscache_release_page(page, gfp);
  388. }
  389. /*
  390. * Attempt to clear the private state associated with a page when an error
  391. * occurs that requires the cached contents of an inode to be written back or
  392. * destroyed
  393. * - Called if either PG_private or fscache is set on the page
  394. * - Caller holds page lock
  395. * - Return 0 if successful, -error otherwise
  396. */
  397. static int nfs_launder_page(struct page *page)
  398. {
  399. struct inode *inode = page->mapping->host;
  400. struct nfs_inode *nfsi = NFS_I(inode);
  401. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  402. inode->i_ino, (long long)page_offset(page));
  403. nfs_fscache_wait_on_page_write(nfsi, page);
  404. return nfs_wb_page(inode, page);
  405. }
  406. const struct address_space_operations nfs_file_aops = {
  407. .readpage = nfs_readpage,
  408. .readpages = nfs_readpages,
  409. .set_page_dirty = __set_page_dirty_nobuffers,
  410. .writepage = nfs_writepage,
  411. .writepages = nfs_writepages,
  412. .write_begin = nfs_write_begin,
  413. .write_end = nfs_write_end,
  414. .invalidatepage = nfs_invalidate_page,
  415. .releasepage = nfs_release_page,
  416. .direct_IO = nfs_direct_IO,
  417. .launder_page = nfs_launder_page,
  418. };
  419. /*
  420. * Notification that a PTE pointing to an NFS page is about to be made
  421. * writable, implying that someone is about to modify the page through a
  422. * shared-writable mapping
  423. */
  424. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  425. {
  426. struct page *page = vmf->page;
  427. struct file *filp = vma->vm_file;
  428. struct dentry *dentry = filp->f_path.dentry;
  429. unsigned pagelen;
  430. int ret = -EINVAL;
  431. struct address_space *mapping;
  432. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
  433. dentry->d_parent->d_name.name, dentry->d_name.name,
  434. filp->f_mapping->host->i_ino,
  435. (long long)page_offset(page));
  436. /* make sure the cache has finished storing the page */
  437. nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
  438. lock_page(page);
  439. mapping = page->mapping;
  440. if (mapping != dentry->d_inode->i_mapping)
  441. goto out_unlock;
  442. ret = 0;
  443. pagelen = nfs_page_length(page);
  444. if (pagelen == 0)
  445. goto out_unlock;
  446. ret = nfs_flush_incompatible(filp, page);
  447. if (ret != 0)
  448. goto out_unlock;
  449. ret = nfs_updatepage(filp, page, 0, pagelen);
  450. out_unlock:
  451. if (!ret)
  452. return VM_FAULT_LOCKED;
  453. unlock_page(page);
  454. return VM_FAULT_SIGBUS;
  455. }
  456. static struct vm_operations_struct nfs_file_vm_ops = {
  457. .fault = filemap_fault,
  458. .page_mkwrite = nfs_vm_page_mkwrite,
  459. };
  460. static int nfs_need_sync_write(struct file *filp, struct inode *inode)
  461. {
  462. struct nfs_open_context *ctx;
  463. if (IS_SYNC(inode) || (filp->f_flags & O_SYNC))
  464. return 1;
  465. ctx = nfs_file_open_context(filp);
  466. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
  467. return 1;
  468. return 0;
  469. }
  470. static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
  471. unsigned long nr_segs, loff_t pos)
  472. {
  473. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  474. struct inode * inode = dentry->d_inode;
  475. ssize_t result;
  476. size_t count = iov_length(iov, nr_segs);
  477. if (iocb->ki_filp->f_flags & O_DIRECT)
  478. return nfs_file_direct_write(iocb, iov, nr_segs, pos);
  479. dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
  480. dentry->d_parent->d_name.name, dentry->d_name.name,
  481. (unsigned long) count, (long long) pos);
  482. result = -EBUSY;
  483. if (IS_SWAPFILE(inode))
  484. goto out_swapfile;
  485. /*
  486. * O_APPEND implies that we must revalidate the file length.
  487. */
  488. if (iocb->ki_filp->f_flags & O_APPEND) {
  489. result = nfs_revalidate_file_size(inode, iocb->ki_filp);
  490. if (result)
  491. goto out;
  492. }
  493. result = count;
  494. if (!count)
  495. goto out;
  496. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
  497. result = generic_file_aio_write(iocb, iov, nr_segs, pos);
  498. /* Return error values for O_SYNC and IS_SYNC() */
  499. if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
  500. int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
  501. if (err < 0)
  502. result = err;
  503. }
  504. out:
  505. return result;
  506. out_swapfile:
  507. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  508. goto out;
  509. }
  510. static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
  511. struct file *filp, loff_t *ppos,
  512. size_t count, unsigned int flags)
  513. {
  514. struct dentry *dentry = filp->f_path.dentry;
  515. struct inode *inode = dentry->d_inode;
  516. ssize_t ret;
  517. dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
  518. dentry->d_parent->d_name.name, dentry->d_name.name,
  519. (unsigned long) count, (unsigned long long) *ppos);
  520. /*
  521. * The combination of splice and an O_APPEND destination is disallowed.
  522. */
  523. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
  524. ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
  525. if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
  526. int err = nfs_do_fsync(nfs_file_open_context(filp), inode);
  527. if (err < 0)
  528. ret = err;
  529. }
  530. return ret;
  531. }
  532. static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
  533. {
  534. struct inode *inode = filp->f_mapping->host;
  535. int status = 0;
  536. /* Try local locking first */
  537. posix_test_lock(filp, fl);
  538. if (fl->fl_type != F_UNLCK) {
  539. /* found a conflict */
  540. goto out;
  541. }
  542. if (nfs_have_delegation(inode, FMODE_READ))
  543. goto out_noconflict;
  544. if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
  545. goto out_noconflict;
  546. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  547. out:
  548. return status;
  549. out_noconflict:
  550. fl->fl_type = F_UNLCK;
  551. goto out;
  552. }
  553. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  554. {
  555. int res = 0;
  556. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  557. case FL_POSIX:
  558. res = posix_lock_file_wait(file, fl);
  559. break;
  560. case FL_FLOCK:
  561. res = flock_lock_file_wait(file, fl);
  562. break;
  563. default:
  564. BUG();
  565. }
  566. if (res < 0)
  567. dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
  568. " - error %d!\n",
  569. __func__, res);
  570. return res;
  571. }
  572. static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
  573. {
  574. struct inode *inode = filp->f_mapping->host;
  575. int status;
  576. /*
  577. * Flush all pending writes before doing anything
  578. * with locks..
  579. */
  580. nfs_sync_mapping(filp->f_mapping);
  581. /* NOTE: special case
  582. * If we're signalled while cleaning up locks on process exit, we
  583. * still need to complete the unlock.
  584. */
  585. /* Use local locking if mounted with "-onolock" */
  586. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  587. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  588. else
  589. status = do_vfs_lock(filp, fl);
  590. return status;
  591. }
  592. static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
  593. {
  594. struct inode *inode = filp->f_mapping->host;
  595. int status;
  596. /*
  597. * Flush all pending writes before doing anything
  598. * with locks..
  599. */
  600. status = nfs_sync_mapping(filp->f_mapping);
  601. if (status != 0)
  602. goto out;
  603. /* Use local locking if mounted with "-onolock" */
  604. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  605. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  606. else
  607. status = do_vfs_lock(filp, fl);
  608. if (status < 0)
  609. goto out;
  610. /*
  611. * Make sure we clear the cache whenever we try to get the lock.
  612. * This makes locking act as a cache coherency point.
  613. */
  614. nfs_sync_mapping(filp->f_mapping);
  615. if (!nfs_have_delegation(inode, FMODE_READ))
  616. nfs_zap_caches(inode);
  617. out:
  618. return status;
  619. }
  620. /*
  621. * Lock a (portion of) a file
  622. */
  623. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  624. {
  625. struct inode *inode = filp->f_mapping->host;
  626. int ret = -ENOLCK;
  627. dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
  628. filp->f_path.dentry->d_parent->d_name.name,
  629. filp->f_path.dentry->d_name.name,
  630. fl->fl_type, fl->fl_flags,
  631. (long long)fl->fl_start, (long long)fl->fl_end);
  632. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  633. /* No mandatory locks over NFS */
  634. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  635. goto out_err;
  636. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  637. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  638. if (ret < 0)
  639. goto out_err;
  640. }
  641. if (IS_GETLK(cmd))
  642. ret = do_getlk(filp, cmd, fl);
  643. else if (fl->fl_type == F_UNLCK)
  644. ret = do_unlk(filp, cmd, fl);
  645. else
  646. ret = do_setlk(filp, cmd, fl);
  647. out_err:
  648. return ret;
  649. }
  650. /*
  651. * Lock a (portion of) a file
  652. */
  653. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  654. {
  655. dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
  656. filp->f_path.dentry->d_parent->d_name.name,
  657. filp->f_path.dentry->d_name.name,
  658. fl->fl_type, fl->fl_flags);
  659. if (!(fl->fl_flags & FL_FLOCK))
  660. return -ENOLCK;
  661. /* We're simulating flock() locks using posix locks on the server */
  662. fl->fl_owner = (fl_owner_t)filp;
  663. fl->fl_start = 0;
  664. fl->fl_end = OFFSET_MAX;
  665. if (fl->fl_type == F_UNLCK)
  666. return do_unlk(filp, cmd, fl);
  667. return do_setlk(filp, cmd, fl);
  668. }
  669. /*
  670. * There is no protocol support for leases, so we have no way to implement
  671. * them correctly in the face of opens by other clients.
  672. */
  673. static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
  674. {
  675. dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
  676. file->f_path.dentry->d_parent->d_name.name,
  677. file->f_path.dentry->d_name.name, arg);
  678. return -EINVAL;
  679. }