file.c 18 KB

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