file.c 19 KB

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