file.c 25 KB

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