NFS: Remove BKL usage from open()
[safe/jmp/linux-2.6] / fs / nfs / file.c
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
19 #include <linux/time.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/fcntl.h>
23 #include <linux/stat.h>
24 #include <linux/nfs_fs.h>
25 #include <linux/nfs_mount.h>
26 #include <linux/mm.h>
27 #include <linux/slab.h>
28 #include <linux/pagemap.h>
29 #include <linux/smp_lock.h>
30 #include <linux/aio.h>
31
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34
35 #include "delegation.h"
36 #include "internal.h"
37 #include "iostat.h"
38
39 #define NFSDBG_FACILITY         NFSDBG_FILE
40
41 static int nfs_file_open(struct inode *, struct file *);
42 static int nfs_file_release(struct inode *, struct file *);
43 static loff_t nfs_file_llseek(struct file *file, loff_t offset, int origin);
44 static int  nfs_file_mmap(struct file *, struct vm_area_struct *);
45 static ssize_t nfs_file_splice_read(struct file *filp, loff_t *ppos,
46                                         struct pipe_inode_info *pipe,
47                                         size_t count, unsigned int flags);
48 static ssize_t nfs_file_read(struct kiocb *, const struct iovec *iov,
49                                 unsigned long nr_segs, loff_t pos);
50 static ssize_t nfs_file_write(struct kiocb *, const struct iovec *iov,
51                                 unsigned long nr_segs, loff_t pos);
52 static int  nfs_file_flush(struct file *, fl_owner_t id);
53 static int  nfs_file_fsync(struct file *, struct dentry *dentry, int datasync);
54 static int nfs_check_flags(int flags);
55 static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl);
56 static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl);
57 static int nfs_setlease(struct file *file, long arg, struct file_lock **fl);
58
59 static struct vm_operations_struct nfs_file_vm_ops;
60
61 const struct file_operations nfs_file_operations = {
62         .llseek         = nfs_file_llseek,
63         .read           = do_sync_read,
64         .write          = do_sync_write,
65         .aio_read       = nfs_file_read,
66         .aio_write      = nfs_file_write,
67 #ifdef CONFIG_MMU
68         .mmap           = nfs_file_mmap,
69 #else
70         .mmap           = generic_file_mmap,
71 #endif
72         .open           = nfs_file_open,
73         .flush          = nfs_file_flush,
74         .release        = nfs_file_release,
75         .fsync          = nfs_file_fsync,
76         .lock           = nfs_lock,
77         .flock          = nfs_flock,
78         .splice_read    = nfs_file_splice_read,
79         .check_flags    = nfs_check_flags,
80         .setlease       = nfs_setlease,
81 };
82
83 const struct inode_operations nfs_file_inode_operations = {
84         .permission     = nfs_permission,
85         .getattr        = nfs_getattr,
86         .setattr        = nfs_setattr,
87 };
88
89 #ifdef CONFIG_NFS_V3
90 const struct inode_operations nfs3_file_inode_operations = {
91         .permission     = nfs_permission,
92         .getattr        = nfs_getattr,
93         .setattr        = nfs_setattr,
94         .listxattr      = nfs3_listxattr,
95         .getxattr       = nfs3_getxattr,
96         .setxattr       = nfs3_setxattr,
97         .removexattr    = nfs3_removexattr,
98 };
99 #endif  /* CONFIG_NFS_v3 */
100
101 /* Hack for future NFS swap support */
102 #ifndef IS_SWAPFILE
103 # define IS_SWAPFILE(inode)     (0)
104 #endif
105
106 static int nfs_check_flags(int flags)
107 {
108         if ((flags & (O_APPEND | O_DIRECT)) == (O_APPEND | O_DIRECT))
109                 return -EINVAL;
110
111         return 0;
112 }
113
114 /*
115  * Open file
116  */
117 static int
118 nfs_file_open(struct inode *inode, struct file *filp)
119 {
120         int res;
121
122         dprintk("NFS: open file(%s/%s)\n",
123                         filp->f_path.dentry->d_parent->d_name.name,
124                         filp->f_path.dentry->d_name.name);
125
126         res = nfs_check_flags(filp->f_flags);
127         if (res)
128                 return res;
129
130         nfs_inc_stats(inode, NFSIOS_VFSOPEN);
131         res = nfs_open(inode, filp);
132         return res;
133 }
134
135 static int
136 nfs_file_release(struct inode *inode, struct file *filp)
137 {
138         struct dentry *dentry = filp->f_path.dentry;
139
140         dprintk("NFS: release(%s/%s)\n",
141                         dentry->d_parent->d_name.name,
142                         dentry->d_name.name);
143
144         /* Ensure that dirty pages are flushed out with the right creds */
145         if (filp->f_mode & FMODE_WRITE)
146                 nfs_wb_all(dentry->d_inode);
147         nfs_inc_stats(inode, NFSIOS_VFSRELEASE);
148         return nfs_release(inode, filp);
149 }
150
151 /**
152  * nfs_revalidate_size - Revalidate the file size
153  * @inode - pointer to inode struct
154  * @file - pointer to struct file
155  *
156  * Revalidates the file length. This is basically a wrapper around
157  * nfs_revalidate_inode() that takes into account the fact that we may
158  * have cached writes (in which case we don't care about the server's
159  * idea of what the file length is), or O_DIRECT (in which case we
160  * shouldn't trust the cache).
161  */
162 static int nfs_revalidate_file_size(struct inode *inode, struct file *filp)
163 {
164         struct nfs_server *server = NFS_SERVER(inode);
165         struct nfs_inode *nfsi = NFS_I(inode);
166
167         if (server->flags & NFS_MOUNT_NOAC)
168                 goto force_reval;
169         if (filp->f_flags & O_DIRECT)
170                 goto force_reval;
171         if (nfsi->npages != 0)
172                 return 0;
173         if (!(nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) && !nfs_attribute_timeout(inode))
174                 return 0;
175 force_reval:
176         return __nfs_revalidate_inode(server, inode);
177 }
178
179 static loff_t nfs_file_llseek(struct file *filp, loff_t offset, int origin)
180 {
181         dprintk("NFS: llseek file(%s/%s, %lld, %d)\n",
182                         filp->f_path.dentry->d_parent->d_name.name,
183                         filp->f_path.dentry->d_name.name,
184                         offset, origin);
185
186         /* origin == SEEK_END => we must revalidate the cached file length */
187         if (origin == SEEK_END) {
188                 struct inode *inode = filp->f_mapping->host;
189                 int retval = nfs_revalidate_file_size(inode, filp);
190                 if (retval < 0)
191                         return (loff_t)retval;
192         }
193         return remote_llseek(filp, offset, origin);
194 }
195
196 /*
197  * Helper for nfs_file_flush() and nfs_file_fsync()
198  *
199  * Notice that it clears the NFS_CONTEXT_ERROR_WRITE before synching to
200  * disk, but it retrieves and clears ctx->error after synching, despite
201  * the two being set at the same time in nfs_context_set_write_error().
202  * This is because the former is used to notify the _next_ call to
203  * nfs_file_write() that a write error occured, and hence cause it to
204  * fall back to doing a synchronous write.
205  */
206 static int nfs_do_fsync(struct nfs_open_context *ctx, struct inode *inode)
207 {
208         int have_error, status;
209         int ret = 0;
210
211         have_error = test_and_clear_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
212         status = nfs_wb_all(inode);
213         have_error |= test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags);
214         if (have_error)
215                 ret = xchg(&ctx->error, 0);
216         if (!ret)
217                 ret = status;
218         return ret;
219 }
220
221 /*
222  * Flush all dirty pages, and check for write errors.
223  */
224 static int
225 nfs_file_flush(struct file *file, fl_owner_t id)
226 {
227         struct nfs_open_context *ctx = nfs_file_open_context(file);
228         struct dentry   *dentry = file->f_path.dentry;
229         struct inode    *inode = dentry->d_inode;
230         int             status;
231
232         dprintk("NFS: flush(%s/%s)\n",
233                         dentry->d_parent->d_name.name,
234                         dentry->d_name.name);
235
236         if ((file->f_mode & FMODE_WRITE) == 0)
237                 return 0;
238         nfs_inc_stats(inode, NFSIOS_VFSFLUSH);
239
240         /* Ensure that data+attribute caches are up to date after close() */
241         status = nfs_do_fsync(ctx, inode);
242         if (!status)
243                 nfs_revalidate_inode(NFS_SERVER(inode), inode);
244         return status;
245 }
246
247 static ssize_t
248 nfs_file_read(struct kiocb *iocb, const struct iovec *iov,
249                 unsigned long nr_segs, loff_t pos)
250 {
251         struct dentry * dentry = iocb->ki_filp->f_path.dentry;
252         struct inode * inode = dentry->d_inode;
253         ssize_t result;
254         size_t count = iov_length(iov, nr_segs);
255
256         if (iocb->ki_filp->f_flags & O_DIRECT)
257                 return nfs_file_direct_read(iocb, iov, nr_segs, pos);
258
259         dprintk("NFS: read(%s/%s, %lu@%lu)\n",
260                 dentry->d_parent->d_name.name, dentry->d_name.name,
261                 (unsigned long) count, (unsigned long) pos);
262
263         result = nfs_revalidate_mapping(inode, iocb->ki_filp->f_mapping);
264         nfs_add_stats(inode, NFSIOS_NORMALREADBYTES, count);
265         if (!result)
266                 result = generic_file_aio_read(iocb, iov, nr_segs, pos);
267         return result;
268 }
269
270 static ssize_t
271 nfs_file_splice_read(struct file *filp, loff_t *ppos,
272                      struct pipe_inode_info *pipe, size_t count,
273                      unsigned int flags)
274 {
275         struct dentry *dentry = filp->f_path.dentry;
276         struct inode *inode = dentry->d_inode;
277         ssize_t res;
278
279         dprintk("NFS: splice_read(%s/%s, %lu@%Lu)\n",
280                 dentry->d_parent->d_name.name, dentry->d_name.name,
281                 (unsigned long) count, (unsigned long long) *ppos);
282
283         res = nfs_revalidate_mapping(inode, filp->f_mapping);
284         if (!res)
285                 res = generic_file_splice_read(filp, ppos, pipe, count, flags);
286         return res;
287 }
288
289 static int
290 nfs_file_mmap(struct file * file, struct vm_area_struct * vma)
291 {
292         struct dentry *dentry = file->f_path.dentry;
293         struct inode *inode = dentry->d_inode;
294         int     status;
295
296         dprintk("NFS: mmap(%s/%s)\n",
297                 dentry->d_parent->d_name.name, dentry->d_name.name);
298
299         status = nfs_revalidate_mapping(inode, file->f_mapping);
300         if (!status) {
301                 vma->vm_ops = &nfs_file_vm_ops;
302                 vma->vm_flags |= VM_CAN_NONLINEAR;
303                 file_accessed(file);
304         }
305         return status;
306 }
307
308 /*
309  * Flush any dirty pages for this process, and check for write errors.
310  * The return status from this call provides a reliable indication of
311  * whether any write errors occurred for this process.
312  */
313 static int
314 nfs_file_fsync(struct file *file, struct dentry *dentry, int datasync)
315 {
316         struct nfs_open_context *ctx = nfs_file_open_context(file);
317         struct inode *inode = dentry->d_inode;
318
319         dprintk("NFS: fsync file(%s/%s) datasync %d\n",
320                         dentry->d_parent->d_name.name, dentry->d_name.name,
321                         datasync);
322
323         nfs_inc_stats(inode, NFSIOS_VFSFSYNC);
324         return nfs_do_fsync(ctx, inode);
325 }
326
327 /*
328  * This does the "real" work of the write. We must allocate and lock the
329  * page to be sent back to the generic routine, which then copies the
330  * data from user space.
331  *
332  * If the writer ends up delaying the write, the writer needs to
333  * increment the page use counts until he is done with the page.
334  */
335 static int nfs_write_begin(struct file *file, struct address_space *mapping,
336                         loff_t pos, unsigned len, unsigned flags,
337                         struct page **pagep, void **fsdata)
338 {
339         int ret;
340         pgoff_t index;
341         struct page *page;
342         index = pos >> PAGE_CACHE_SHIFT;
343
344         dfprintk(PAGECACHE, "NFS: write_begin(%s/%s(%ld), %u@%lld)\n",
345                 file->f_path.dentry->d_parent->d_name.name,
346                 file->f_path.dentry->d_name.name,
347                 mapping->host->i_ino, len, (long long) pos);
348
349         page = __grab_cache_page(mapping, index);
350         if (!page)
351                 return -ENOMEM;
352         *pagep = page;
353
354         ret = nfs_flush_incompatible(file, page);
355         if (ret) {
356                 unlock_page(page);
357                 page_cache_release(page);
358         }
359         return ret;
360 }
361
362 static int nfs_write_end(struct file *file, struct address_space *mapping,
363                         loff_t pos, unsigned len, unsigned copied,
364                         struct page *page, void *fsdata)
365 {
366         unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
367         int status;
368
369         dfprintk(PAGECACHE, "NFS: write_end(%s/%s(%ld), %u@%lld)\n",
370                 file->f_path.dentry->d_parent->d_name.name,
371                 file->f_path.dentry->d_name.name,
372                 mapping->host->i_ino, len, (long long) pos);
373
374         /*
375          * Zero any uninitialised parts of the page, and then mark the page
376          * as up to date if it turns out that we're extending the file.
377          */
378         if (!PageUptodate(page)) {
379                 unsigned pglen = nfs_page_length(page);
380                 unsigned end = offset + len;
381
382                 if (pglen == 0) {
383                         zero_user_segments(page, 0, offset,
384                                         end, PAGE_CACHE_SIZE);
385                         SetPageUptodate(page);
386                 } else if (end >= pglen) {
387                         zero_user_segment(page, end, PAGE_CACHE_SIZE);
388                         if (offset == 0)
389                                 SetPageUptodate(page);
390                 } else
391                         zero_user_segment(page, pglen, PAGE_CACHE_SIZE);
392         }
393
394         status = nfs_updatepage(file, page, offset, copied);
395
396         unlock_page(page);
397         page_cache_release(page);
398
399         if (status < 0)
400                 return status;
401         return copied;
402 }
403
404 static void nfs_invalidate_page(struct page *page, unsigned long offset)
405 {
406         dfprintk(PAGECACHE, "NFS: invalidate_page(%p, %lu)\n", page, offset);
407
408         if (offset != 0)
409                 return;
410         /* Cancel any unstarted writes on this page */
411         nfs_wb_page_cancel(page->mapping->host, page);
412 }
413
414 static int nfs_release_page(struct page *page, gfp_t gfp)
415 {
416         dfprintk(PAGECACHE, "NFS: release_page(%p)\n", page);
417
418         /* If PagePrivate() is set, then the page is not freeable */
419         return 0;
420 }
421
422 static int nfs_launder_page(struct page *page)
423 {
424         struct inode *inode = page->mapping->host;
425
426         dfprintk(PAGECACHE, "NFS: launder_page(%ld, %llu)\n",
427                 inode->i_ino, (long long)page_offset(page));
428
429         return nfs_wb_page(inode, page);
430 }
431
432 const struct address_space_operations nfs_file_aops = {
433         .readpage = nfs_readpage,
434         .readpages = nfs_readpages,
435         .set_page_dirty = __set_page_dirty_nobuffers,
436         .writepage = nfs_writepage,
437         .writepages = nfs_writepages,
438         .write_begin = nfs_write_begin,
439         .write_end = nfs_write_end,
440         .invalidatepage = nfs_invalidate_page,
441         .releasepage = nfs_release_page,
442         .direct_IO = nfs_direct_IO,
443         .launder_page = nfs_launder_page,
444 };
445
446 static int nfs_vm_page_mkwrite(struct vm_area_struct *vma, struct page *page)
447 {
448         struct file *filp = vma->vm_file;
449         struct dentry *dentry = filp->f_path.dentry;
450         unsigned pagelen;
451         int ret = -EINVAL;
452         struct address_space *mapping;
453
454         dfprintk(PAGECACHE, "NFS: vm_page_mkwrite(%s/%s(%ld), offset %lld)\n",
455                 dentry->d_parent->d_name.name, dentry->d_name.name,
456                 filp->f_mapping->host->i_ino,
457                 (long long)page_offset(page));
458
459         lock_page(page);
460         mapping = page->mapping;
461         if (mapping != dentry->d_inode->i_mapping)
462                 goto out_unlock;
463
464         ret = 0;
465         pagelen = nfs_page_length(page);
466         if (pagelen == 0)
467                 goto out_unlock;
468
469         ret = nfs_flush_incompatible(filp, page);
470         if (ret != 0)
471                 goto out_unlock;
472
473         ret = nfs_updatepage(filp, page, 0, pagelen);
474         if (ret == 0)
475                 ret = pagelen;
476 out_unlock:
477         unlock_page(page);
478         return ret;
479 }
480
481 static struct vm_operations_struct nfs_file_vm_ops = {
482         .fault = filemap_fault,
483         .page_mkwrite = nfs_vm_page_mkwrite,
484 };
485
486 static int nfs_need_sync_write(struct file *filp, struct inode *inode)
487 {
488         struct nfs_open_context *ctx;
489
490         if (IS_SYNC(inode) || (filp->f_flags & O_SYNC))
491                 return 1;
492         ctx = nfs_file_open_context(filp);
493         if (test_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags))
494                 return 1;
495         return 0;
496 }
497
498 static ssize_t nfs_file_write(struct kiocb *iocb, const struct iovec *iov,
499                                 unsigned long nr_segs, loff_t pos)
500 {
501         struct dentry * dentry = iocb->ki_filp->f_path.dentry;
502         struct inode * inode = dentry->d_inode;
503         ssize_t result;
504         size_t count = iov_length(iov, nr_segs);
505
506         if (iocb->ki_filp->f_flags & O_DIRECT)
507                 return nfs_file_direct_write(iocb, iov, nr_segs, pos);
508
509         dprintk("NFS: write(%s/%s, %lu@%Ld)\n",
510                 dentry->d_parent->d_name.name, dentry->d_name.name,
511                 (unsigned long) count, (long long) pos);
512
513         result = -EBUSY;
514         if (IS_SWAPFILE(inode))
515                 goto out_swapfile;
516         /*
517          * O_APPEND implies that we must revalidate the file length.
518          */
519         if (iocb->ki_filp->f_flags & O_APPEND) {
520                 result = nfs_revalidate_file_size(inode, iocb->ki_filp);
521                 if (result)
522                         goto out;
523         }
524
525         result = count;
526         if (!count)
527                 goto out;
528
529         nfs_add_stats(inode, NFSIOS_NORMALWRITTENBYTES, count);
530         result = generic_file_aio_write(iocb, iov, nr_segs, pos);
531         /* Return error values for O_SYNC and IS_SYNC() */
532         if (result >= 0 && nfs_need_sync_write(iocb->ki_filp, inode)) {
533                 int err = nfs_do_fsync(nfs_file_open_context(iocb->ki_filp), inode);
534                 if (err < 0)
535                         result = err;
536         }
537 out:
538         return result;
539
540 out_swapfile:
541         printk(KERN_INFO "NFS: attempt to write to active swap file!\n");
542         goto out;
543 }
544
545 static int do_getlk(struct file *filp, int cmd, struct file_lock *fl)
546 {
547         struct inode *inode = filp->f_mapping->host;
548         int status = 0;
549
550         lock_kernel();
551         /* Try local locking first */
552         posix_test_lock(filp, fl);
553         if (fl->fl_type != F_UNLCK) {
554                 /* found a conflict */
555                 goto out;
556         }
557
558         if (nfs_have_delegation(inode, FMODE_READ))
559                 goto out_noconflict;
560
561         if (NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM)
562                 goto out_noconflict;
563
564         status = NFS_PROTO(inode)->lock(filp, cmd, fl);
565 out:
566         unlock_kernel();
567         return status;
568 out_noconflict:
569         fl->fl_type = F_UNLCK;
570         goto out;
571 }
572
573 static int do_vfs_lock(struct file *file, struct file_lock *fl)
574 {
575         int res = 0;
576         switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
577                 case FL_POSIX:
578                         res = posix_lock_file_wait(file, fl);
579                         break;
580                 case FL_FLOCK:
581                         res = flock_lock_file_wait(file, fl);
582                         break;
583                 default:
584                         BUG();
585         }
586         if (res < 0)
587                 dprintk(KERN_WARNING "%s: VFS is out of sync with lock manager"
588                         " - error %d!\n",
589                                 __func__, res);
590         return res;
591 }
592
593 static int do_unlk(struct file *filp, int cmd, struct file_lock *fl)
594 {
595         struct inode *inode = filp->f_mapping->host;
596         int status;
597
598         /*
599          * Flush all pending writes before doing anything
600          * with locks..
601          */
602         nfs_sync_mapping(filp->f_mapping);
603
604         /* NOTE: special case
605          *      If we're signalled while cleaning up locks on process exit, we
606          *      still need to complete the unlock.
607          */
608         lock_kernel();
609         /* Use local locking if mounted with "-onolock" */
610         if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
611                 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
612         else
613                 status = do_vfs_lock(filp, fl);
614         unlock_kernel();
615         return status;
616 }
617
618 static int do_setlk(struct file *filp, int cmd, struct file_lock *fl)
619 {
620         struct inode *inode = filp->f_mapping->host;
621         int status;
622
623         /*
624          * Flush all pending writes before doing anything
625          * with locks..
626          */
627         status = nfs_sync_mapping(filp->f_mapping);
628         if (status != 0)
629                 goto out;
630
631         lock_kernel();
632         /* Use local locking if mounted with "-onolock" */
633         if (!(NFS_SERVER(inode)->flags & NFS_MOUNT_NONLM))
634                 status = NFS_PROTO(inode)->lock(filp, cmd, fl);
635         else
636                 status = do_vfs_lock(filp, fl);
637         unlock_kernel();
638         if (status < 0)
639                 goto out;
640         /*
641          * Make sure we clear the cache whenever we try to get the lock.
642          * This makes locking act as a cache coherency point.
643          */
644         nfs_sync_mapping(filp->f_mapping);
645         if (!nfs_have_delegation(inode, FMODE_READ))
646                 nfs_zap_caches(inode);
647 out:
648         return status;
649 }
650
651 /*
652  * Lock a (portion of) a file
653  */
654 static int nfs_lock(struct file *filp, int cmd, struct file_lock *fl)
655 {
656         struct inode *inode = filp->f_mapping->host;
657         int ret = -ENOLCK;
658
659         dprintk("NFS: lock(%s/%s, t=%x, fl=%x, r=%lld:%lld)\n",
660                         filp->f_path.dentry->d_parent->d_name.name,
661                         filp->f_path.dentry->d_name.name,
662                         fl->fl_type, fl->fl_flags,
663                         (long long)fl->fl_start, (long long)fl->fl_end);
664
665         nfs_inc_stats(inode, NFSIOS_VFSLOCK);
666
667         /* No mandatory locks over NFS */
668         if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
669                 goto out_err;
670
671         if (NFS_PROTO(inode)->lock_check_bounds != NULL) {
672                 ret = NFS_PROTO(inode)->lock_check_bounds(fl);
673                 if (ret < 0)
674                         goto out_err;
675         }
676
677         if (IS_GETLK(cmd))
678                 ret = do_getlk(filp, cmd, fl);
679         else if (fl->fl_type == F_UNLCK)
680                 ret = do_unlk(filp, cmd, fl);
681         else
682                 ret = do_setlk(filp, cmd, fl);
683 out_err:
684         return ret;
685 }
686
687 /*
688  * Lock a (portion of) a file
689  */
690 static int nfs_flock(struct file *filp, int cmd, struct file_lock *fl)
691 {
692         dprintk("NFS: flock(%s/%s, t=%x, fl=%x)\n",
693                         filp->f_path.dentry->d_parent->d_name.name,
694                         filp->f_path.dentry->d_name.name,
695                         fl->fl_type, fl->fl_flags);
696
697         /*
698          * No BSD flocks over NFS allowed.
699          * Note: we could try to fake a POSIX lock request here by
700          * using ((u32) filp | 0x80000000) or some such as the pid.
701          * Not sure whether that would be unique, though, or whether
702          * that would break in other places.
703          */
704         if (!(fl->fl_flags & FL_FLOCK))
705                 return -ENOLCK;
706
707         /* We're simulating flock() locks using posix locks on the server */
708         fl->fl_owner = (fl_owner_t)filp;
709         fl->fl_start = 0;
710         fl->fl_end = OFFSET_MAX;
711
712         if (fl->fl_type == F_UNLCK)
713                 return do_unlk(filp, cmd, fl);
714         return do_setlk(filp, cmd, fl);
715 }
716
717 /*
718  * There is no protocol support for leases, so we have no way to implement
719  * them correctly in the face of opens by other clients.
720  */
721 static int nfs_setlease(struct file *file, long arg, struct file_lock **fl)
722 {
723         dprintk("NFS: setlease(%s/%s, arg=%ld)\n",
724                         file->f_path.dentry->d_parent->d_name.name,
725                         file->f_path.dentry->d_name.name, arg);
726
727         return -EINVAL;
728 }