file.c 20 KB

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