file.c 24 KB

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