file.c 24 KB

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