file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. }
  171. lock_kernel(); /* BKL needed? */
  172. loff = generic_file_llseek_unlocked(filp, offset, origin);
  173. unlock_kernel();
  174. return loff;
  175. }
  176. /*
  177. * Helper for nfs_file_flush() and nfs_file_fsync()
  178. *
  179. * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
  180. * disk, but it retrieves and clears ctx->error after synching, despite
  181. * the two being set at the same time in nfs_context_set_write_error().
  182. * This is because the former is used to notify the _next_ call to
  183. * nfs_file_write() that a write error occured, and hence cause it to
  184. * fall back to doing a synchronous write.
  185. */
  186. static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
  187. {
  188. int have_error, status;
  189. int ret = 0;
  190. have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  191. status = nfs_wb_all(inode);
  192. have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
  193. if (have_error)
  194. ret = xchg(&ctx->error, 0);
  195. if (!ret)
  196. ret = status;
  197. return ret;
  198. }
  199. /*
  200. * Flush all dirty pages, and check for write errors.
  201. */
  202. static int
  203. nfs_file_flush(struct file *file, fl_owner_t id)
  204. {
  205. struct nfs_open_context *ctx = nfs_file_open_context(file);
  206. struct dentry *dentry = file->f_path.dentry;
  207. struct inode *inode = dentry->d_inode;
  208. int status;
  209. dprintk("NFS: flush(%s/%s)\n",
  210. dentry->d_parent->d_name.name,
  211. dentry->d_name.name);
  212. if ((file->f_mode & FMODE_WRITE) == 0)
  213. return 0;
  214. nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
  215. /* Ensure that data+attribute caches are up to date after close() */
  216. status = nfs_do_fsync(ctx, inode);
  217. if (!status)
  218. nfs_revalidate_inode(NFS_SERVER(inode), inode);
  219. return status;
  220. }
  221. static ssize_t
  222. nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
  223. unsigned long nr_segs, loff_t pos)
  224. {
  225. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  226. struct inode * inode = dentry->d_inode;
  227. ssize_t result;
  228. size_t count = iov_length(iov, nr_segs);
  229. if (iocb->ki_filp->f_flags & O_DIRECT)
  230. return nfs_file_direct_read(iocb, iov, nr_segs, pos);
  231. dprintk("NFS: read(%s/%s, %lu@%lu)\n",
  232. dentry->d_parent->d_name.name, dentry->d_name.name,
  233. (unsigned long) count, (unsigned long) pos);
  234. result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
  235. nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count);
  236. if (!result)
  237. result = generic_file_aio_read(iocb, iov, nr_segs, pos);
  238. return result;
  239. }
  240. static ssize_t
  241. nfs_file_splice_read(struct file *filp, loff_t *ppos,
  242. struct pipe_inode_info *pipe, size_t count,
  243. unsigned int flags)
  244. {
  245. struct dentry *dentry = filp->f_path.dentry;
  246. struct inode *inode = dentry->d_inode;
  247. ssize_t res;
  248. dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
  249. dentry->d_parent->d_name.name, dentry->d_name.name,
  250. (unsigned long) count, (unsigned long long) *ppos);
  251. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  252. if (!res)
  253. res = generic_file_splice_read(filp, ppos, pipe, count, flags);
  254. return res;
  255. }
  256. static int
  257. nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
  258. {
  259. struct dentry *dentry = file->f_path.dentry;
  260. struct inode *inode = dentry->d_inode;
  261. int status;
  262. dprintk("NFS: mmap(%s/%s)\n",
  263. dentry->d_parent->d_name.name, dentry->d_name.name);
  264. status = nfs_revalidate_mapping(inode, file->f_mapping);
  265. if (!status) {
  266. vma->vm_ops = &nfs_file_vm_ops;
  267. vma->vm_flags |= VM_CAN_NONLINEAR;
  268. file_accessed(file);
  269. }
  270. return status;
  271. }
  272. /*
  273. * Flush any dirty pages for this process, and check for write errors.
  274. * The return status from this call provides a reliable indication of
  275. * whether any write errors occurred for this process.
  276. */
  277. static int
  278. nfs_file_fsync(struct file *file, struct dentry *dentry, int datasync)
  279. {
  280. struct nfs_open_context *ctx = nfs_file_open_context(file);
  281. struct inode *inode = dentry->d_inode;
  282. dprintk("NFS: fsync file(%s/%s) datasync %d\n",
  283. dentry->d_parent->d_name.name, dentry->d_name.name,
  284. datasync);
  285. nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
  286. return nfs_do_fsync(ctx, inode);
  287. }
  288. /*
  289. * This does the "real" work of the write. We must allocate and lock the
  290. * page to be sent back to the generic routine, which then copies the
  291. * data from user space.
  292. *
  293. * If the writer ends up delaying the write, the writer needs to
  294. * increment the page use counts until he is done with the page.
  295. */
  296. static int nfs_write_begin(struct file *file, struct address_space *mapping,
  297. loff_t pos, unsigned len, unsigned flags,
  298. struct page **pagep, void **fsdata)
  299. {
  300. int ret;
  301. pgoff_t index;
  302. struct page *page;
  303. index = pos >> PAGE_CACHE_SHIFT;
  304. dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
  305. file->f_path.dentry->d_parent->d_name.name,
  306. file->f_path.dentry->d_name.name,
  307. mapping->host->i_ino, len, (long long) pos);
  308. page = __grab_cache_page(mapping, index);
  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 page *page)
  389. {
  390. struct file *filp = vma->vm_file;
  391. struct dentry *dentry = filp->f_path.dentry;
  392. unsigned pagelen;
  393. int ret = -EINVAL;
  394. struct address_space *mapping;
  395. dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
  396. dentry->d_parent->d_name.name, dentry->d_name.name,
  397. filp->f_mapping->host->i_ino,
  398. (long long)page_offset(page));
  399. lock_page(page);
  400. mapping = page->mapping;
  401. if (mapping != dentry->d_inode->i_mapping)
  402. goto out_unlock;
  403. ret = 0;
  404. pagelen = nfs_page_length(page);
  405. if (pagelen == 0)
  406. goto out_unlock;
  407. ret = nfs_flush_incompatible(filp, page);
  408. if (ret != 0)
  409. goto out_unlock;
  410. ret = nfs_updatepage(filp, page, 0, pagelen);
  411. if (ret == 0)
  412. ret = pagelen;
  413. out_unlock:
  414. unlock_page(page);
  415. return ret;
  416. }
  417. static struct vm_operations_struct nfs_file_vm_ops = {
  418. .fault = filemap_fault,
  419. .page_mkwrite = nfs_vm_page_mkwrite,
  420. };
  421. static int nfs_need_sync_write(struct file *filp, struct inode *inode)
  422. {
  423. struct nfs_open_context *ctx;
  424. if (IS_SYNC(inode) || (filp->f_flags & O_SYNC))
  425. return 1;
  426. ctx = nfs_file_open_context(filp);
  427. if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
  428. return 1;
  429. return 0;
  430. }
  431. static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
  432. unsigned long nr_segs, loff_t pos)
  433. {
  434. struct dentry * dentry = iocb->ki_filp->f_path.dentry;
  435. struct inode * inode = dentry->d_inode;
  436. ssize_t result;
  437. size_t count = iov_length(iov, nr_segs);
  438. if (iocb->ki_filp->f_flags & O_DIRECT)
  439. return nfs_file_direct_write(iocb, iov, nr_segs, pos);
  440. dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
  441. dentry->d_parent->d_name.name, dentry->d_name.name,
  442. (unsigned long) count, (long long) pos);
  443. result = -EBUSY;
  444. if (IS_SWAPFILE(inode))
  445. goto out_swapfile;
  446. /*
  447. * O_APPEND implies that we must revalidate the file length.
  448. */
  449. if (iocb->ki_filp->f_flags & O_APPEND) {
  450. result = nfs_revalidate_file_size(inode, iocb->ki_filp);
  451. if (result)
  452. goto out;
  453. }
  454. result = count;
  455. if (!count)
  456. goto out;
  457. nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
  458. result = generic_file_aio_write(iocb, iov, nr_segs, pos);
  459. /* Return error values for O_SYNC and IS_SYNC() */
  460. if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
  461. int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
  462. if (err < 0)
  463. result = err;
  464. }
  465. out:
  466. return result;
  467. out_swapfile:
  468. printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
  469. goto out;
  470. }
  471. static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
  472. {
  473. struct inode *inode = filp->f_mapping->host;
  474. int status = 0;
  475. lock_kernel();
  476. /* Try local locking first */
  477. posix_test_lock(filp, fl);
  478. if (fl->fl_type != F_UNLCK) {
  479. /* found a conflict */
  480. goto out;
  481. }
  482. if (nfs_have_delegation(inode, FMODE_READ))
  483. goto out_noconflict;
  484. if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
  485. goto out_noconflict;
  486. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  487. out:
  488. unlock_kernel();
  489. return status;
  490. out_noconflict:
  491. fl->fl_type = F_UNLCK;
  492. goto out;
  493. }
  494. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  495. {
  496. int res = 0;
  497. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  498. case FL_POSIX:
  499. res = posix_lock_file_wait(file, fl);
  500. break;
  501. case FL_FLOCK:
  502. res = flock_lock_file_wait(file, fl);
  503. break;
  504. default:
  505. BUG();
  506. }
  507. if (res < 0)
  508. dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
  509. " - error %d!\n",
  510. __func__, res);
  511. return res;
  512. }
  513. static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
  514. {
  515. struct inode *inode = filp->f_mapping->host;
  516. int status;
  517. /*
  518. * Flush all pending writes before doing anything
  519. * with locks..
  520. */
  521. nfs_sync_mapping(filp->f_mapping);
  522. /* NOTE: special case
  523. * If we're signalled while cleaning up locks on process exit, we
  524. * still need to complete the unlock.
  525. */
  526. lock_kernel();
  527. /* Use local locking if mounted with "-onolock" */
  528. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  529. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  530. else
  531. status = do_vfs_lock(filp, fl);
  532. unlock_kernel();
  533. return status;
  534. }
  535. static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
  536. {
  537. struct inode *inode = filp->f_mapping->host;
  538. int status;
  539. /*
  540. * Flush all pending writes before doing anything
  541. * with locks..
  542. */
  543. status = nfs_sync_mapping(filp->f_mapping);
  544. if (status != 0)
  545. goto out;
  546. lock_kernel();
  547. /* Use local locking if mounted with "-onolock" */
  548. if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
  549. status = NFS_PROTO(inode)->lock(filp, cmd, fl);
  550. else
  551. status = do_vfs_lock(filp, fl);
  552. unlock_kernel();
  553. if (status < 0)
  554. goto out;
  555. /*
  556. * Make sure we clear the cache whenever we try to get the lock.
  557. * This makes locking act as a cache coherency point.
  558. */
  559. nfs_sync_mapping(filp->f_mapping);
  560. if (!nfs_have_delegation(inode, FMODE_READ))
  561. nfs_zap_caches(inode);
  562. out:
  563. return status;
  564. }
  565. /*
  566. * Lock a (portion of) a file
  567. */
  568. static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
  569. {
  570. struct inode *inode = filp->f_mapping->host;
  571. int ret = -ENOLCK;
  572. dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
  573. filp->f_path.dentry->d_parent->d_name.name,
  574. filp->f_path.dentry->d_name.name,
  575. fl->fl_type, fl->fl_flags,
  576. (long long)fl->fl_start, (long long)fl->fl_end);
  577. nfs_inc_stats(inode, NFSIOS_VFSLOCK);
  578. /* No mandatory locks over NFS */
  579. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  580. goto out_err;
  581. if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
  582. ret = NFS_PROTO(inode)->lock_check_bounds(fl);
  583. if (ret < 0)
  584. goto out_err;
  585. }
  586. if (IS_GETLK(cmd))
  587. ret = do_getlk(filp, cmd, fl);
  588. else if (fl->fl_type == F_UNLCK)
  589. ret = do_unlk(filp, cmd, fl);
  590. else
  591. ret = do_setlk(filp, cmd, fl);
  592. out_err:
  593. return ret;
  594. }
  595. /*
  596. * Lock a (portion of) a file
  597. */
  598. static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
  599. {
  600. dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
  601. filp->f_path.dentry->d_parent->d_name.name,
  602. filp->f_path.dentry->d_name.name,
  603. fl->fl_type, fl->fl_flags);
  604. /*
  605. * No BSD flocks over NFS allowed.
  606. * Note: we could try to fake a POSIX lock request here by
  607. * using ((u32) filp | 0x80000000) or some such as the pid.
  608. * Not sure whether that would be unique, though, or whether
  609. * that would break in other places.
  610. */
  611. if (!(fl->fl_flags & FL_FLOCK))
  612. return -ENOLCK;
  613. /* We're simulating flock() locks using posix locks on the server */
  614. fl->fl_owner = (fl_owner_t)filp;
  615. fl->fl_start = 0;
  616. fl->fl_end = OFFSET_MAX;
  617. if (fl->fl_type == F_UNLCK)
  618. return do_unlk(filp, cmd, fl);
  619. return do_setlk(filp, cmd, fl);
  620. }
  621. /*
  622. * There is no protocol support for leases, so we have no way to implement
  623. * them correctly in the face of opens by other clients.
  624. */
  625. static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
  626. {
  627. dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
  628. file->f_path.dentry->d_parent->d_name.name,
  629. file->f_path.dentry->d_name.name, arg);
  630. return -EINVAL;
  631. }