file.c 24 KB

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