file.c 21 KB

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