file.c 23 KB

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