file.c 24 KB

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