file.c 24 KB

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