file.c 26 KB

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