file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*
  2. * linux/fs/nfs/file.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * Changes Copyright (C) 1994 by Florian La Roche
  7. * - Do not copy data too often around in the kernel.
  8. * - In nfs_file_read the return value of kmalloc wasn't checked.
  9. * - Put in a better version of read look-ahead buffering. Original idea
  10. * and implementation by Wai S Kok elekokws@ee.nus.sg.
  11. *
  12. * Expire cache on write to a file by Wai S Kok (Oct 1994).
  13. *
  14. * Total rewrite of read side for new NFS buffer cache.. Linus.
  15. *
  16. * nfs regular file handling functions
  17. */
  18. #include <linux/time.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/stat.h>
  23. #include <linux/nfs_fs.h>
  24. #include <linux/nfs_mount.h>
  25. #include <linux/mm.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/aio.h>
  28. #include <linux/gfp.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/system.h>
  31. #include "delegation.h"
  32. #include "internal.h"
  33. #include "iostat.h"
  34. #include "fscache.h"
  35. #define NFSDBG_FACILITY NFSDBG_FILE
  36. static int nfs_file_open(struct inode *, struct file *);
  37. static int nfs_file_release(struct inode *, struct file *);
  38. static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
  39. static int nfs_file_mmap(struct file *, struct vm_area_struct *);
  40. static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos,
  41. struct pipe_inode_info *pipe,
  42. size_t count, unsigned int flags);
  43. static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov,
  44. unsigned long nr_segs, loff_t pos);
  45. static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
  46. struct file *filp, loff_t *ppos,
  47. size_t count, unsigned int flags);
  48. static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
  49. unsigned long nr_segs, loff_t pos);
  50. static int nfs_file_flush(struct file *, fl_owner_t id);
  51. static int nfs_file_fsync(struct file *, int datasync);
  52. static int nfs_check_flags(int flags);
  53. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
  54. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
  55. static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);
  56. static const struct vm_operations_struct nfs_file_vm_ops;
  57. const struct file_operations nfs_file_operations = {
  58. .llseek = nfs_file_llseek,
  59. .read = do_sync_read,
  60. .write = do_sync_write,
  61. .aio_read = nfs_file_read,
  62. .aio_write = nfs_file_write,
  63. .mmap = nfs_file_mmap,
  64. .open = nfs_file_open,
  65. .flush = nfs_file_flush,
  66. .release = nfs_file_release,
  67. .fsync = nfs_file_fsync,
  68. .lock = nfs_lock,
  69. .flock = nfs_flock,
  70. .splice_read = nfs_file_splice_read,
  71. .splice_write = nfs_file_splice_write,
  72. .check_flags = nfs_check_flags,
  73. .setlease = nfs_setlease,
  74. };
  75. const struct inode_operations nfs_file_inode_operations = {
  76. .permission = nfs_permission,
  77. .getattr = nfs_getattr,
  78. .setattr = nfs_setattr,
  79. };
  80. #ifdef CONFIG_NFS_V3
  81. const struct inode_operations nfs3_file_inode_operations = {
  82. .permission = nfs_permission,
  83. .getattr = nfs_getattr,
  84. .setattr = nfs_setattr,
  85. .listxattr = nfs3_listxattr,
  86. .getxattr = nfs3_getxattr,
  87. .setxattr = nfs3_setxattr,
  88. .removexattr = nfs3_removexattr,
  89. };
  90. #endif /* CONFIG_NFS_v3 */
  91. /* Hack for future NFS swap support */
  92. #ifndef IS_SWAPFILE
  93. # define IS_SWAPFILE(inode) (0)
  94. #endif
  95. static int nfs_check_flags(int flags)
  96. {
  97. if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
  98. return -EINVAL;
  99. return 0;
  100. }
  101. /*
  102. * Open file
  103. */
  104. static int
  105. nfs_file_open(struct inode *inode, struct file *filp)
  106. {
  107. int res;
  108. dprintk("NFS: open file(%s/%s)\n",
  109. filp->f_path.dentry->d_parent->d_name.name,
  110. filp->f_path.dentry->d_name.name);
  111. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  112. res = nfs_check_flags(filp->f_flags);
  113. if (res)
  114. return res;
  115. res = nfs_open(inode, filp);
  116. return res;
  117. }
  118. static int
  119. nfs_file_release(struct inode *inode, struct file *filp)
  120. {
  121. struct dentry *dentry = filp->f_path.dentry;
  122. dprintk("NFS: release(%s/%s)\n",
  123. dentry->d_parent->d_name.name,
  124. dentry->d_name.name);
  125. nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
  126. return nfs_release(inode, filp);
  127. }
  128. /**
  129. * nfs_revalidate_size - Revalidate the file size
  130. * @inode - pointer to inode struct
  131. * @file - pointer to struct file
  132. *
  133. * Revalidates the file length. This is basically a wrapper around
  134. * nfs_revalidate_inode() that takes into account the fact that we may
  135. * have cached writes (in which case we don't care about the server's
  136. * idea of what the file length is), or O_DIRECT (in which case we
  137. * shouldn't trust the cache).
  138. */
  139. static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
  140. {
  141. struct nfs_server *server = NFS_SERVER(inode);
  142. struct nfs_inode *nfsi = NFS_I(inode);
  143. if (nfs_have_delegated_attributes(inode))
  144. goto out_noreval;
  145. if (filp->f_flags & O_DIRECT)
  146. goto force_reval;
  147. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  148. goto force_reval;
  149. if (nfs_attribute_timeout(inode))
  150. goto force_reval;
  151. out_noreval:
  152. return 0;
  153. force_reval:
  154. return __nfs_revalidate_inode(server, inode);
  155. }
  156. static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
  157. {
  158. loff_t loff;
  159. dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
  160. filp->f_path.dentry->d_parent->d_name.name,
  161. filp->f_path.dentry->d_name.name,
  162. offset, origin);
  163. /* origin == SEEK_END => we must revalidate the cached file length */
  164. if (origin == SEEK_END) {
  165. struct inode *inode = filp->f_mapping->host;
  166. int retval = nfs_revalidate_file_size(inode, filp);
  167. if (retval < 0)
  168. return (loff_t)retval;
  169. spin_lock(&inode->i_lock);
  170. loff = generic_file_llseek_unlocked(filp, offset, origin);
  171. spin_unlock(&inode->i_lock);
  172. } else
  173. loff = generic_file_llseek_unlocked(filp, offset, origin);
  174. return loff;
  175. }
  176. /*
  177. * Flush all dirty pages, and check for write errors.
  178. */
  179. static int
  180. nfs_file_flush(struct file *file, fl_owner_t id)
  181. {
  182. struct dentry *dentry = file->f_path.dentry;
  183. struct inode *inode = dentry->d_inode;
  184. dprintk("NFS: flush(%s/%s)\n",
  185. dentry->d_parent->d_name.name,
  186. dentry->d_name.name);
  187. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  188. if ((file->f_mode & FMODE_WRITE) == 0)
  189. return 0;
  190. /* Flush writes to the server and return any errors */
  191. return vfs_fsync(file, 0);
  192. }
  193. static ssize_t
  194. nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
  195. unsigned long nr_segs, loff_t pos)
  196. {
  197. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  198. struct inode * inode = dentry->d_inode;
  199. ssize_t result;
  200. size_t count = iov_length(iov, nr_segs);
  201. if (iocb->ki_filp->f_flags & O_DIRECT)
  202. return nfs_file_direct_read(iocb, iov, nr_segs, pos);
  203. dprintk("NFS: read(%s/%s, %lu@%lu)\n",
  204. dentry->d_parent->d_name.name, dentry->d_name.name,
  205. (unsigned long) count, (unsigned long) pos);
  206. result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  207. if (!result) {
  208. result = generic_file_aio_read(iocb, iov, nr_segs, pos);
  209. if (result > 0)
  210. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
  211. }
  212. return result;
  213. }
  214. static ssize_t
  215. nfs_file_splice_read(struct file *filp, loff_t *ppos,
  216. struct pipe_inode_info *pipe, size_t count,
  217. unsigned int flags)
  218. {
  219. struct dentry *dentry = filp->f_path.dentry;
  220. struct inode *inode = dentry->d_inode;
  221. ssize_t res;
  222. dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
  223. dentry->d_parent->d_name.name, dentry->d_name.name,
  224. (unsigned long) count, (unsigned long long) *ppos);
  225. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  226. if (!res) {
  227. res = generic_file_splice_read(filp, ppos, pipe, count, flags);
  228. if (res > 0)
  229. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
  230. }
  231. return res;
  232. }
  233. static int
  234. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  235. {
  236. struct dentry *dentry = file->f_path.dentry;
  237. struct inode *inode = dentry->d_inode;
  238. int status;
  239. dprintk("NFS: mmap(%s/%s)\n",
  240. dentry->d_parent->d_name.name, dentry->d_name.name);
  241. /* Note: generic_file_mmap() returns ENOSYS on nommu systems
  242. * so we call that before revalidating the mapping
  243. */
  244. status = generic_file_mmap(file, vma);
  245. if (!status) {
  246. vma->vm_ops = &nfs_file_vm_ops;
  247. status = nfs_revalidate_mapping(inode, file->f_mapping);
  248. }
  249. return status;
  250. }
  251. /*
  252. * Flush any dirty pages for this process, and check for write errors.
  253. * The return status from this call provides a reliable indication of
  254. * whether any write errors occurred for this process.
  255. *
  256. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  257. * disk, but it retrieves and clears ctx->error after synching, despite
  258. * the two being set at the same time in nfs_context_set_write_error().
  259. * This is because the former is used to notify the _next_ call to
  260. * nfs_file_write() that a write error occured, and hence cause it to
  261. * fall back to doing a synchronous write.
  262. */
  263. static int
  264. nfs_file_fsync(struct file *file, int datasync)
  265. {
  266. struct dentry *dentry = file->f_path.dentry;
  267. struct nfs_open_context *ctx = nfs_file_open_context(file);
  268. struct inode *inode = dentry->d_inode;
  269. int have_error, status;
  270. int ret = 0;
  271. dprintk("NFS: fsync file(%s/%s) datasync %d\n",
  272. dentry->d_parent->d_name.name, dentry->d_name.name,
  273. datasync);
  274. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  275. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  276. status = nfs_commit_inode(inode, FLUSH_SYNC);
  277. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  278. if (have_error)
  279. ret = xchg(&ctx->error, 0);
  280. if (!ret)
  281. ret = status;
  282. return ret;
  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. /* Only do I/O if gfp is a superset of GFP_KERNEL */
  426. if ((gfp & GFP_KERNEL) == GFP_KERNEL)
  427. nfs_wb_page(page->mapping->host, page);
  428. /* If PagePrivate() is set, then the page is not freeable */
  429. if (PagePrivate(page))
  430. return 0;
  431. return nfs_fscache_release_page(page, gfp);
  432. }
  433. /*
  434. * Attempt to clear the private state associated with a page when an error
  435. * occurs that requires the cached contents of an inode to be written back or
  436. * destroyed
  437. * - Called if either PG_private or fscache is set on the page
  438. * - Caller holds page lock
  439. * - Return 0 if successful, -error otherwise
  440. */
  441. static int nfs_launder_page(struct page *page)
  442. {
  443. struct inode *inode = page->mapping->host;
  444. struct nfs_inode *nfsi = NFS_I(inode);
  445. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  446. inode->i_ino, (long long)page_offset(page));
  447. nfs_fscache_wait_on_page_write(nfsi, page);
  448. return nfs_wb_page(inode, page);
  449. }
  450. const struct address_space_operations nfs_file_aops = {
  451. .readpage = nfs_readpage,
  452. .readpages = nfs_readpages,
  453. .set_page_dirty = __set_page_dirty_nobuffers,
  454. .writepage = nfs_writepage,
  455. .writepages = nfs_writepages,
  456. .write_begin = nfs_write_begin,
  457. .write_end = nfs_write_end,
  458. .invalidatepage = nfs_invalidate_page,
  459. .releasepage = nfs_release_page,
  460. .direct_IO = nfs_direct_IO,
  461. .migratepage = nfs_migrate_page,
  462. .launder_page = nfs_launder_page,
  463. .error_remove_page = generic_error_remove_page,
  464. };
  465. /*
  466. * Notification that a PTE pointing to an NFS page is about to be made
  467. * writable, implying that someone is about to modify the page through a
  468. * shared-writable mapping
  469. */
  470. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  471. {
  472. struct page *page = vmf->page;
  473. struct file *filp = vma->vm_file;
  474. struct dentry *dentry = filp->f_path.dentry;
  475. unsigned pagelen;
  476. int ret = -EINVAL;
  477. struct address_space *mapping;
  478. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
  479. dentry->d_parent->d_name.name, dentry->d_name.name,
  480. filp->f_mapping->host->i_ino,
  481. (long long)page_offset(page));
  482. /* make sure the cache has finished storing the page */
  483. nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
  484. lock_page(page);
  485. mapping = page->mapping;
  486. if (mapping != dentry->d_inode->i_mapping)
  487. goto out_unlock;
  488. ret = 0;
  489. pagelen = nfs_page_length(page);
  490. if (pagelen == 0)
  491. goto out_unlock;
  492. ret = nfs_flush_incompatible(filp, page);
  493. if (ret != 0)
  494. goto out_unlock;
  495. ret = nfs_updatepage(filp, page, 0, pagelen);
  496. out_unlock:
  497. if (!ret)
  498. return VM_FAULT_LOCKED;
  499. unlock_page(page);
  500. return VM_FAULT_SIGBUS;
  501. }
  502. static const struct vm_operations_struct nfs_file_vm_ops = {
  503. .fault = filemap_fault,
  504. .page_mkwrite = nfs_vm_page_mkwrite,
  505. };
  506. static int nfs_need_sync_write(struct file *filp, struct inode *inode)
  507. {
  508. struct nfs_open_context *ctx;
  509. if (IS_SYNC(inode) || (filp->f_flags & O_DSYNC))
  510. return 1;
  511. ctx = nfs_file_open_context(filp);
  512. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
  513. return 1;
  514. return 0;
  515. }
  516. static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
  517. unsigned long nr_segs, loff_t pos)
  518. {
  519. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  520. struct inode * inode = dentry->d_inode;
  521. unsigned long written = 0;
  522. ssize_t result;
  523. size_t count = iov_length(iov, nr_segs);
  524. if (iocb->ki_filp->f_flags & O_DIRECT)
  525. return nfs_file_direct_write(iocb, iov, nr_segs, pos);
  526. dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
  527. dentry->d_parent->d_name.name, dentry->d_name.name,
  528. (unsigned long) count, (long long) pos);
  529. result = -EBUSY;
  530. if (IS_SWAPFILE(inode))
  531. goto out_swapfile;
  532. /*
  533. * O_APPEND implies that we must revalidate the file length.
  534. */
  535. if (iocb->ki_filp->f_flags & O_APPEND) {
  536. result = nfs_revalidate_file_size(inode, iocb->ki_filp);
  537. if (result)
  538. goto out;
  539. }
  540. result = count;
  541. if (!count)
  542. goto out;
  543. result = generic_file_aio_write(iocb, iov, nr_segs, pos);
  544. if (result > 0)
  545. written = result;
  546. /* Return error values for O_DSYNC and IS_SYNC() */
  547. if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
  548. int err = vfs_fsync(iocb->ki_filp, 0);
  549. if (err < 0)
  550. result = err;
  551. }
  552. if (result > 0)
  553. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  554. out:
  555. return result;
  556. out_swapfile:
  557. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  558. goto out;
  559. }
  560. static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
  561. struct file *filp, loff_t *ppos,
  562. size_t count, unsigned int flags)
  563. {
  564. struct dentry *dentry = filp->f_path.dentry;
  565. struct inode *inode = dentry->d_inode;
  566. unsigned long written = 0;
  567. ssize_t ret;
  568. dprintk("NFS splice_write(%s/%s, %lu@%llu)\n",
  569. dentry->d_parent->d_name.name, dentry->d_name.name,
  570. (unsigned long) count, (unsigned long long) *ppos);
  571. /*
  572. * The combination of splice and an O_APPEND destination is disallowed.
  573. */
  574. ret = generic_file_splice_write(pipe, filp, ppos, count, flags);
  575. if (ret > 0)
  576. written = ret;
  577. if (ret >= 0 && nfs_need_sync_write(filp, inode)) {
  578. int err = vfs_fsync(filp, 0);
  579. if (err < 0)
  580. ret = err;
  581. }
  582. if (ret > 0)
  583. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, written);
  584. return ret;
  585. }
  586. static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
  587. {
  588. struct inode *inode = filp->f_mapping->host;
  589. int status = 0;
  590. /* Try local locking first */
  591. posix_test_lock(filp, fl);
  592. if (fl->fl_type != F_UNLCK) {
  593. /* found a conflict */
  594. goto out;
  595. }
  596. if (nfs_have_delegation(inode, FMODE_READ))
  597. goto out_noconflict;
  598. if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
  599. goto out_noconflict;
  600. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  601. out:
  602. return status;
  603. out_noconflict:
  604. fl->fl_type = F_UNLCK;
  605. goto out;
  606. }
  607. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  608. {
  609. int res = 0;
  610. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  611. case FL_POSIX:
  612. res = posix_lock_file_wait(file, fl);
  613. break;
  614. case FL_FLOCK:
  615. res = flock_lock_file_wait(file, fl);
  616. break;
  617. default:
  618. BUG();
  619. }
  620. if (res < 0)
  621. dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
  622. " - error %d!\n",
  623. __func__, res);
  624. return res;
  625. }
  626. static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
  627. {
  628. struct inode *inode = filp->f_mapping->host;
  629. int status;
  630. /*
  631. * Flush all pending writes before doing anything
  632. * with locks..
  633. */
  634. nfs_sync_mapping(filp->f_mapping);
  635. /* NOTE: special case
  636. * If we're signalled while cleaning up locks on process exit, we
  637. * still need to complete the unlock.
  638. */
  639. /* Use local locking if mounted with "-onolock" */
  640. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  641. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  642. else
  643. status = do_vfs_lock(filp, fl);
  644. return status;
  645. }
  646. static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
  647. {
  648. struct inode *inode = filp->f_mapping->host;
  649. int status;
  650. /*
  651. * Flush all pending writes before doing anything
  652. * with locks..
  653. */
  654. status = nfs_sync_mapping(filp->f_mapping);
  655. if (status != 0)
  656. goto out;
  657. /* Use local locking if mounted with "-onolock" */
  658. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  659. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  660. else
  661. status = do_vfs_lock(filp, fl);
  662. if (status < 0)
  663. goto out;
  664. /*
  665. * Make sure we clear the cache whenever we try to get the lock.
  666. * This makes locking act as a cache coherency point.
  667. */
  668. nfs_sync_mapping(filp->f_mapping);
  669. if (!nfs_have_delegation(inode, FMODE_READ))
  670. nfs_zap_caches(inode);
  671. out:
  672. return status;
  673. }
  674. /*
  675. * Lock a (portion of) a file
  676. */
  677. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  678. {
  679. struct inode *inode = filp->f_mapping->host;
  680. int ret = -ENOLCK;
  681. dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
  682. filp->f_path.dentry->d_parent->d_name.name,
  683. filp->f_path.dentry->d_name.name,
  684. fl->fl_type, fl->fl_flags,
  685. (long long)fl->fl_start, (long long)fl->fl_end);
  686. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  687. /* No mandatory locks over NFS */
  688. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  689. goto out_err;
  690. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  691. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  692. if (ret < 0)
  693. goto out_err;
  694. }
  695. if (IS_GETLK(cmd))
  696. ret = do_getlk(filp, cmd, fl);
  697. else if (fl->fl_type == F_UNLCK)
  698. ret = do_unlk(filp, cmd, fl);
  699. else
  700. ret = do_setlk(filp, cmd, fl);
  701. out_err:
  702. return ret;
  703. }
  704. /*
  705. * Lock a (portion of) a file
  706. */
  707. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  708. {
  709. dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
  710. filp->f_path.dentry->d_parent->d_name.name,
  711. filp->f_path.dentry->d_name.name,
  712. fl->fl_type, fl->fl_flags);
  713. if (!(fl->fl_flags & FL_FLOCK))
  714. return -ENOLCK;
  715. /* We're simulating flock() locks using posix locks on the server */
  716. fl->fl_owner = (fl_owner_t)filp;
  717. fl->fl_start = 0;
  718. fl->fl_end = OFFSET_MAX;
  719. if (fl->fl_type == F_UNLCK)
  720. return do_unlk(filp, cmd, fl);
  721. return do_setlk(filp, cmd, fl);
  722. }
  723. /*
  724. * There is no protocol support for leases, so we have no way to implement
  725. * them correctly in the face of opens by other clients.
  726. */
  727. static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
  728. {
  729. dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
  730. file->f_path.dentry->d_parent->d_name.name,
  731. file->f_path.dentry->d_name.name, arg);
  732. return -EINVAL;
  733. }