file.c 18 KB

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