file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. #include "pnfs.h"
  37. #define NFSDBG_FACILITY NFSDBG_FILE
  38. static int nfs_file_open(struct inode *, struct file *);
  39. static int nfs_file_release(struct inode *, struct file *);
  40. static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
  41. static int nfs_file_mmap(struct file *, struct vm_area_struct *);
  42. static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos,
  43. struct pipe_inode_info *pipe,
  44. size_t count, unsigned int flags);
  45. static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov,
  46. unsigned long nr_segs, loff_t pos);
  47. static ssize_t nfs_file_splice_write(struct pipe_inode_info *pipe,
  48. struct file *filp, loff_t *ppos,
  49. size_t count, unsigned int flags);
  50. static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
  51. unsigned long nr_segs, loff_t pos);
  52. static int nfs_file_flush(struct file *, fl_owner_t id);
  53. static int nfs_file_fsync(struct file *, int datasync);
  54. static int nfs_check_flags(int flags);
  55. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
  56. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
  57. static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);
  58. static const struct vm_operations_struct nfs_file_vm_ops;
  59. const struct file_operations nfs_file_operations = {
  60. .llseek = nfs_file_llseek,
  61. .read = do_sync_read,
  62. .write = do_sync_write,
  63. .aio_read = nfs_file_read,
  64. .aio_write = nfs_file_write,
  65. .mmap = nfs_file_mmap,
  66. .open = nfs_file_open,
  67. .flush = nfs_file_flush,
  68. .release = nfs_file_release,
  69. .fsync = nfs_file_fsync,
  70. .lock = nfs_lock,
  71. .flock = nfs_flock,
  72. .splice_read = nfs_file_splice_read,
  73. .splice_write = nfs_file_splice_write,
  74. .check_flags = nfs_check_flags,
  75. .setlease = nfs_setlease,
  76. };
  77. const struct inode_operations nfs_file_inode_operations = {
  78. .permission = nfs_permission,
  79. .getattr = nfs_getattr,
  80. .setattr = nfs_setattr,
  81. };
  82. #ifdef CONFIG_NFS_V3
  83. const struct inode_operations nfs3_file_inode_operations = {
  84. .permission = nfs_permission,
  85. .getattr = nfs_getattr,
  86. .setattr = nfs_setattr,
  87. .listxattr = nfs3_listxattr,
  88. .getxattr = nfs3_getxattr,
  89. .setxattr = nfs3_setxattr,
  90. .removexattr = nfs3_removexattr,
  91. };
  92. #endif /* CONFIG_NFS_v3 */
  93. /* Hack for future NFS swap support */
  94. #ifndef IS_SWAPFILE
  95. # define IS_SWAPFILE(inode) (0)
  96. #endif
  97. static int nfs_check_flags(int flags)
  98. {
  99. if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
  100. return -EINVAL;
  101. return 0;
  102. }
  103. /*
  104. * Open file
  105. */
  106. static int
  107. nfs_file_open(struct inode *inode, struct file *filp)
  108. {
  109. int res;
  110. dprintk("NFS: open file(%s/%s)\n",
  111. filp->f_path.dentry->d_parent->d_name.name,
  112. filp->f_path.dentry->d_name.name);
  113. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  114. res = nfs_check_flags(filp->f_flags);
  115. if (res)
  116. return res;
  117. res = nfs_open(inode, filp);
  118. return res;
  119. }
  120. static int
  121. nfs_file_release(struct inode *inode, struct file *filp)
  122. {
  123. struct dentry *dentry = filp->f_path.dentry;
  124. dprintk("NFS: release(%s/%s)\n",
  125. dentry->d_parent->d_name.name,
  126. dentry->d_name.name);
  127. nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
  128. return nfs_release(inode, filp);
  129. }
  130. /**
  131. * nfs_revalidate_size - Revalidate the file size
  132. * @inode - pointer to inode struct
  133. * @file - pointer to struct file
  134. *
  135. * Revalidates the file length. This is basically a wrapper around
  136. * nfs_revalidate_inode() that takes into account the fact that we may
  137. * have cached writes (in which case we don't care about the server's
  138. * idea of what the file length is), or O_DIRECT (in which case we
  139. * shouldn't trust the cache).
  140. */
  141. static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
  142. {
  143. struct nfs_server *server = NFS_SERVER(inode);
  144. struct nfs_inode *nfsi = NFS_I(inode);
  145. if (nfs_have_delegated_attributes(inode))
  146. goto out_noreval;
  147. if (filp->f_flags & O_DIRECT)
  148. goto force_reval;
  149. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  150. goto force_reval;
  151. if (nfs_attribute_timeout(inode))
  152. goto force_reval;
  153. out_noreval:
  154. return 0;
  155. force_reval:
  156. return __nfs_revalidate_inode(server, inode);
  157. }
  158. static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
  159. {
  160. loff_t loff;
  161. dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
  162. filp->f_path.dentry->d_parent->d_name.name,
  163. filp->f_path.dentry->d_name.name,
  164. offset, origin);
  165. /* origin == SEEK_END => we must revalidate the cached file length */
  166. if (origin == SEEK_END) {
  167. struct inode *inode = filp->f_mapping->host;
  168. int retval = nfs_revalidate_file_size(inode, filp);
  169. if (retval < 0)
  170. return (loff_t)retval;
  171. spin_lock(&inode->i_lock);
  172. loff = generic_file_llseek_unlocked(filp, offset, origin);
  173. spin_unlock(&inode->i_lock);
  174. } else
  175. loff = generic_file_llseek_unlocked(filp, offset, origin);
  176. return loff;
  177. }
  178. /*
  179. * Flush all dirty pages, and check for write errors.
  180. */
  181. static int
  182. nfs_file_flush(struct file *file, fl_owner_t id)
  183. {
  184. struct dentry *dentry = file->f_path.dentry;
  185. struct inode *inode = dentry->d_inode;
  186. dprintk("NFS: flush(%s/%s)\n",
  187. dentry->d_parent->d_name.name,
  188. dentry->d_name.name);
  189. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  190. if ((file->f_mode & FMODE_WRITE) == 0)
  191. return 0;
  192. /* Flush writes to the server and return any errors */
  193. return vfs_fsync(file, 0);
  194. }
  195. static ssize_t
  196. nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
  197. unsigned long nr_segs, loff_t pos)
  198. {
  199. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  200. struct inode * inode = dentry->d_inode;
  201. ssize_t result;
  202. size_t count = iov_length(iov, nr_segs);
  203. if (iocb->ki_filp->f_flags & O_DIRECT)
  204. return nfs_file_direct_read(iocb, iov, nr_segs, pos);
  205. dprintk("NFS: read(%s/%s, %lu@%lu)\n",
  206. dentry->d_parent->d_name.name, dentry->d_name.name,
  207. (unsigned long) count, (unsigned long) pos);
  208. result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  209. if (!result) {
  210. result = generic_file_aio_read(iocb, iov, nr_segs, pos);
  211. if (result > 0)
  212. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, result);
  213. }
  214. return result;
  215. }
  216. static ssize_t
  217. nfs_file_splice_read(struct file *filp, loff_t *ppos,
  218. struct pipe_inode_info *pipe, size_t count,
  219. unsigned int flags)
  220. {
  221. struct dentry *dentry = filp->f_path.dentry;
  222. struct inode *inode = dentry->d_inode;
  223. ssize_t res;
  224. dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
  225. dentry->d_parent->d_name.name, dentry->d_name.name,
  226. (unsigned long) count, (unsigned long long) *ppos);
  227. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  228. if (!res) {
  229. res = generic_file_splice_read(filp, ppos, pipe, count, flags);
  230. if (res > 0)
  231. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, res);
  232. }
  233. return res;
  234. }
  235. static int
  236. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  237. {
  238. struct dentry *dentry = file->f_path.dentry;
  239. struct inode *inode = dentry->d_inode;
  240. int status;
  241. dprintk("NFS: mmap(%s/%s)\n",
  242. dentry->d_parent->d_name.name, dentry->d_name.name);
  243. /* Note: generic_file_mmap() returns ENOSYS on nommu systems
  244. * so we call that before revalidating the mapping
  245. */
  246. status = generic_file_mmap(file, vma);
  247. if (!status) {
  248. vma->vm_ops = &nfs_file_vm_ops;
  249. status = nfs_revalidate_mapping(inode, file->f_mapping);
  250. }
  251. return status;
  252. }
  253. /*
  254. * Flush any dirty pages for this process, and check for write errors.
  255. * The return status from this call provides a reliable indication of
  256. * whether any write errors occurred for this process.
  257. *
  258. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  259. * disk, but it retrieves and clears ctx->error after synching, despite
  260. * the two being set at the same time in nfs_context_set_write_error().
  261. * This is because the former is used to notify the _next_ call to
  262. * nfs_file_write() that a write error occured, and hence cause it to
  263. * fall back to doing a synchronous write.
  264. */
  265. static int
  266. nfs_file_fsync(struct file *file, int datasync)
  267. {
  268. struct dentry *dentry = file->f_path.dentry;
  269. struct nfs_open_context *ctx = nfs_file_open_context(file);
  270. struct inode *inode = dentry->d_inode;
  271. int have_error, status;
  272. int ret = 0;
  273. dprintk("NFS: fsync file(%s/%s) datasync %d\n",
  274. dentry->d_parent->d_name.name, dentry->d_name.name,
  275. datasync);
  276. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  277. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  278. status = nfs_commit_inode(inode, FLUSH_SYNC);
  279. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  280. if (have_error)
  281. ret = xchg(&ctx->error, 0);
  282. if (!ret && status < 0)
  283. ret = status;
  284. return ret;
  285. }
  286. /*
  287. * Decide whether a read/modify/write cycle may be more efficient
  288. * then a modify/write/read cycle when writing to a page in the
  289. * page cache.
  290. *
  291. * The modify/write/read cycle may occur if a page is read before
  292. * being completely filled by the writer. In this situation, the
  293. * page must be completely written to stable storage on the server
  294. * before it can be refilled by reading in the page from the server.
  295. * This can lead to expensive, small, FILE_SYNC mode writes being
  296. * done.
  297. *
  298. * It may be more efficient to read the page first if the file is
  299. * open for reading in addition to writing, the page is not marked
  300. * as Uptodate, it is not dirty or waiting to be committed,
  301. * indicating that it was previously allocated and then modified,
  302. * that there were valid bytes of data in that range of the file,
  303. * and that the new data won't completely replace the old data in
  304. * that range of the file.
  305. */
  306. static int nfs_want_read_modify_write(struct file *file, struct page *page,
  307. loff_t pos, unsigned len)
  308. {
  309. unsigned int pglen = nfs_page_length(page);
  310. unsigned int offset = pos & (PAGE_CACHE_SIZE - 1);
  311. unsigned int end = offset + len;
  312. if ((file->f_mode & FMODE_READ) && /* open for read? */
  313. !PageUptodate(page) && /* Uptodate? */
  314. !PagePrivate(page) && /* i/o request already? */
  315. pglen && /* valid bytes of file? */
  316. (end < pglen || offset)) /* replace all valid bytes? */
  317. return 1;
  318. return 0;
  319. }
  320. /*
  321. * This does the "real" work of the write. We must allocate and lock the
  322. * page to be sent back to the generic routine, which then copies the
  323. * data from user space.
  324. *
  325. * If the writer ends up delaying the write, the writer needs to
  326. * increment the page use counts until he is done with the page.
  327. */
  328. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  329. loff_t pos, unsigned len, unsigned flags,
  330. struct page **pagep, void **fsdata)
  331. {
  332. int ret;
  333. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  334. struct page *page;
  335. int once_thru = 0;
  336. dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
  337. file->f_path.dentry->d_parent->d_name.name,
  338. file->f_path.dentry->d_name.name,
  339. mapping->host->i_ino, len, (long long) pos);
  340. start:
  341. /*
  342. * Prevent starvation issues if someone is doing a consistency
  343. * sync-to-disk
  344. */
  345. ret = wait_on_bit(&NFS_I(mapping->host)->flags, NFS_INO_FLUSHING,
  346. nfs_wait_bit_killable, TASK_KILLABLE);
  347. if (ret)
  348. return ret;
  349. page = grab_cache_page_write_begin(mapping, index, flags);
  350. if (!page)
  351. return -ENOMEM;
  352. *pagep = page;
  353. ret = nfs_flush_incompatible(file, page);
  354. if (ret) {
  355. unlock_page(page);
  356. page_cache_release(page);
  357. } else if (!once_thru &&
  358. nfs_want_read_modify_write(file, page, pos, len)) {
  359. once_thru = 1;
  360. ret = nfs_readpage(file, page);
  361. page_cache_release(page);
  362. if (!ret)
  363. goto start;
  364. }
  365. return ret;
  366. }
  367. static int nfs_write_end(struct file *file, struct address_space *mapping,
  368. loff_t pos, unsigned len, unsigned copied,
  369. struct page *page, void *fsdata)
  370. {
  371. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  372. int status;
  373. dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
  374. file->f_path.dentry->d_parent->d_name.name,
  375. file->f_path.dentry->d_name.name,
  376. mapping->host->i_ino, len, (long long) pos);
  377. /*
  378. * Zero any uninitialised parts of the page, and then mark the page
  379. * as up to date if it turns out that we're extending the file.
  380. */
  381. if (!PageUptodate(page)) {
  382. unsigned pglen = nfs_page_length(page);
  383. unsigned end = offset + len;
  384. if (pglen == 0) {
  385. zero_user_segments(page, 0, offset,
  386. end, PAGE_CACHE_SIZE);
  387. SetPageUptodate(page);
  388. } else if (end >= pglen) {
  389. zero_user_segment(page, end, PAGE_CACHE_SIZE);
  390. if (offset == 0)
  391. SetPageUptodate(page);
  392. } else
  393. zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
  394. }
  395. status = nfs_updatepage(file, page, offset, copied);
  396. unlock_page(page);
  397. page_cache_release(page);
  398. if (status < 0)
  399. return status;
  400. return copied;
  401. }
  402. /*
  403. * Partially or wholly invalidate a page
  404. * - Release the private state associated with a page if undergoing complete
  405. * page invalidation
  406. * - Called if either PG_private or PG_fscache is set on the page
  407. * - Caller holds page lock
  408. */
  409. static void nfs_invalidate_page(struct page *page, unsigned long offset)
  410. {
  411. dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
  412. if (offset != 0)
  413. return;
  414. /* Cancel any unstarted writes on this page */
  415. nfs_wb_page_cancel(page->mapping->host, page);
  416. nfs_fscache_invalidate_page(page, page->mapping->host);
  417. }
  418. /*
  419. * Attempt to release the private state associated with a page
  420. * - Called if either PG_private or PG_fscache is set on the page
  421. * - Caller holds page lock
  422. * - Return true (may release page) or false (may not)
  423. */
  424. static int nfs_release_page(struct page *page, gfp_t gfp)
  425. {
  426. struct address_space *mapping = page->mapping;
  427. dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
  428. /* Only do I/O if gfp is a superset of GFP_KERNEL */
  429. if (mapping && (gfp & GFP_KERNEL) == GFP_KERNEL) {
  430. int how = FLUSH_SYNC;
  431. /* Don't let kswapd deadlock waiting for OOM RPC calls */
  432. if (current_is_kswapd())
  433. how = 0;
  434. nfs_commit_inode(mapping->host, how);
  435. }
  436. /* If PagePrivate() is set, then the page is not freeable */
  437. if (PagePrivate(page))
  438. return 0;
  439. return nfs_fscache_release_page(page, gfp);
  440. }
  441. /*
  442. * Attempt to clear the private state associated with a page when an error
  443. * occurs that requires the cached contents of an inode to be written back or
  444. * destroyed
  445. * - Called if either PG_private or fscache is set on the page
  446. * - Caller holds page lock
  447. * - Return 0 if successful, -error otherwise
  448. */
  449. static int nfs_launder_page(struct page *page)
  450. {
  451. struct inode *inode = page->mapping->host;
  452. struct nfs_inode *nfsi = NFS_I(inode);
  453. dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
  454. inode->i_ino, (long long)page_offset(page));
  455. nfs_fscache_wait_on_page_write(nfsi, page);
  456. return nfs_wb_page(inode, page);
  457. }
  458. const struct address_space_operations nfs_file_aops = {
  459. .readpage = nfs_readpage,
  460. .readpages = nfs_readpages,
  461. .set_page_dirty = __set_page_dirty_nobuffers,
  462. .writepage = nfs_writepage,
  463. .writepages = nfs_writepages,
  464. .write_begin = nfs_write_begin,
  465. .write_end = nfs_write_end,
  466. .invalidatepage = nfs_invalidate_page,
  467. .releasepage = nfs_release_page,
  468. .direct_IO = nfs_direct_IO,
  469. .migratepage = nfs_migrate_page,
  470. .launder_page = nfs_launder_page,
  471. .error_remove_page = generic_error_remove_page,
  472. };
  473. /*
  474. * Notification that a PTE pointing to an NFS page is about to be made
  475. * writable, implying that someone is about to modify the page through a
  476. * shared-writable mapping
  477. */
  478. static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  479. {
  480. struct page *page = vmf->page;
  481. struct file *filp = vma->vm_file;
  482. struct dentry *dentry = filp->f_path.dentry;
  483. unsigned pagelen;
  484. int ret = VM_FAULT_NOPAGE;
  485. struct address_space *mapping;
  486. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
  487. dentry->d_parent->d_name.name, dentry->d_name.name,
  488. filp->f_mapping->host->i_ino,
  489. (long long)page_offset(page));
  490. /* make sure the cache has finished storing the page */
  491. nfs_fscache_wait_on_page_write(NFS_I(dentry->d_inode), page);
  492. lock_page(page);
  493. mapping = page->mapping;
  494. if (mapping != dentry->d_inode->i_mapping)
  495. goto out_unlock;
  496. pagelen = nfs_page_length(page);
  497. if (pagelen == 0)
  498. goto out_unlock;
  499. ret = VM_FAULT_LOCKED;
  500. if (nfs_flush_incompatible(filp, page) == 0 &&
  501. nfs_updatepage(filp, page, 0, pagelen) == 0)
  502. goto out;
  503. ret = VM_FAULT_SIGBUS;
  504. out_unlock:
  505. unlock_page(page);
  506. out:
  507. return ret;
  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
  594. do_getlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  595. {
  596. struct inode *inode = filp->f_mapping->host;
  597. int status = 0;
  598. unsigned int saved_type = fl->fl_type;
  599. /* Try local locking first */
  600. posix_test_lock(filp, fl);
  601. if (fl->fl_type != F_UNLCK) {
  602. /* found a conflict */
  603. goto out;
  604. }
  605. fl->fl_type = saved_type;
  606. if (nfs_have_delegation(inode, FMODE_READ))
  607. goto out_noconflict;
  608. if (is_local)
  609. goto out_noconflict;
  610. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  611. out:
  612. return status;
  613. out_noconflict:
  614. fl->fl_type = F_UNLCK;
  615. goto out;
  616. }
  617. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  618. {
  619. int res = 0;
  620. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  621. case FL_POSIX:
  622. res = posix_lock_file_wait(file, fl);
  623. break;
  624. case FL_FLOCK:
  625. res = flock_lock_file_wait(file, fl);
  626. break;
  627. default:
  628. BUG();
  629. }
  630. return res;
  631. }
  632. static int
  633. do_unlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  634. {
  635. struct inode *inode = filp->f_mapping->host;
  636. int status;
  637. /*
  638. * Flush all pending writes before doing anything
  639. * with locks..
  640. */
  641. nfs_sync_mapping(filp->f_mapping);
  642. /* NOTE: special case
  643. * If we're signalled while cleaning up locks on process exit, we
  644. * still need to complete the unlock.
  645. */
  646. /*
  647. * Use local locking if mounted with "-onolock" or with appropriate
  648. * "-olocal_lock="
  649. */
  650. if (!is_local)
  651. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  652. else
  653. status = do_vfs_lock(filp, fl);
  654. return status;
  655. }
  656. static int
  657. is_time_granular(struct timespec *ts) {
  658. return ((ts->tv_sec == 0) && (ts->tv_nsec <= 1000));
  659. }
  660. static int
  661. do_setlk(struct file *filp, int cmd, struct file_lock *fl, int is_local)
  662. {
  663. struct inode *inode = filp->f_mapping->host;
  664. int status;
  665. /*
  666. * Flush all pending writes before doing anything
  667. * with locks..
  668. */
  669. status = nfs_sync_mapping(filp->f_mapping);
  670. if (status != 0)
  671. goto out;
  672. /*
  673. * Use local locking if mounted with "-onolock" or with appropriate
  674. * "-olocal_lock="
  675. */
  676. if (!is_local)
  677. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  678. else
  679. status = do_vfs_lock(filp, fl);
  680. if (status < 0)
  681. goto out;
  682. /*
  683. * Revalidate the cache if the server has time stamps granular
  684. * enough to detect subsecond changes. Otherwise, clear the
  685. * cache to prevent missing any changes.
  686. *
  687. * This makes locking act as a cache coherency point.
  688. */
  689. nfs_sync_mapping(filp->f_mapping);
  690. if (!nfs_have_delegation(inode, FMODE_READ)) {
  691. if (is_time_granular(&NFS_SERVER(inode)->time_delta))
  692. __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  693. else
  694. nfs_zap_caches(inode);
  695. }
  696. out:
  697. return status;
  698. }
  699. /*
  700. * Lock a (portion of) a file
  701. */
  702. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  703. {
  704. struct inode *inode = filp->f_mapping->host;
  705. int ret = -ENOLCK;
  706. int is_local = 0;
  707. dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
  708. filp->f_path.dentry->d_parent->d_name.name,
  709. filp->f_path.dentry->d_name.name,
  710. fl->fl_type, fl->fl_flags,
  711. (long long)fl->fl_start, (long long)fl->fl_end);
  712. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  713. /* No mandatory locks over NFS */
  714. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  715. goto out_err;
  716. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FCNTL)
  717. is_local = 1;
  718. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  719. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  720. if (ret < 0)
  721. goto out_err;
  722. }
  723. if (IS_GETLK(cmd))
  724. ret = do_getlk(filp, cmd, fl, is_local);
  725. else if (fl->fl_type == F_UNLCK)
  726. ret = do_unlk(filp, cmd, fl, is_local);
  727. else
  728. ret = do_setlk(filp, cmd, fl, is_local);
  729. out_err:
  730. return ret;
  731. }
  732. /*
  733. * Lock a (portion of) a file
  734. */
  735. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  736. {
  737. struct inode *inode = filp->f_mapping->host;
  738. int is_local = 0;
  739. dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
  740. filp->f_path.dentry->d_parent->d_name.name,
  741. filp->f_path.dentry->d_name.name,
  742. fl->fl_type, fl->fl_flags);
  743. if (!(fl->fl_flags & FL_FLOCK))
  744. return -ENOLCK;
  745. if (NFS_SERVER(inode)->flags & NFS_MOUNT_LOCAL_FLOCK)
  746. is_local = 1;
  747. /* We're simulating flock() locks using posix locks on the server */
  748. fl->fl_owner = (fl_owner_t)filp;
  749. fl->fl_start = 0;
  750. fl->fl_end = OFFSET_MAX;
  751. if (fl->fl_type == F_UNLCK)
  752. return do_unlk(filp, cmd, fl, is_local);
  753. return do_setlk(filp, cmd, fl, is_local);
  754. }
  755. /*
  756. * There is no protocol support for leases, so we have no way to implement
  757. * them correctly in the face of opens by other clients.
  758. */
  759. static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
  760. {
  761. dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
  762. file->f_path.dentry->d_parent->d_name.name,
  763. file->f_path.dentry->d_name.name, arg);
  764. return -EINVAL;
  765. }