file.c 19 KB

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