file.c 23 KB

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