file.c 25 KB

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