file.c 24 KB

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