file.c 23 KB

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