file.c 19 KB

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