file.c 23 KB

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