ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / nfsd / lockd.c
1 /*
2  * linux/fs/nfsd/lockd.c
3  *
4  * This file contains all the stubs needed when communicating with lockd.
5  * This level of indirection is necessary so we can run nfsd+lockd without
6  * requiring the nfs client to be compiled in/loaded, and vice versa.
7  *
8  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9  */
10
11 #include <linux/types.h>
12 #include <linux/fs.h>
13 #include <linux/mount.h>
14 #include <linux/sunrpc/clnt.h>
15 #include <linux/sunrpc/svc.h>
16 #include <linux/nfsd/nfsd.h>
17 #include <linux/lockd/bind.h>
18
19 #define NFSDDBG_FACILITY                NFSDDBG_LOCKD
20
21 /*
22  * Note: we hold the dentry use count while the file is open.
23  */
24 static u32
25 nlm_fopen(struct svc_rqst *rqstp, struct nfs_fh *f, struct file *filp)
26 {
27         u32             nfserr;
28         struct svc_fh   fh;
29
30         /* must initialize before using! but maxsize doesn't matter */
31         fh_init(&fh,0);
32         fh.fh_handle.fh_size = f->size;
33         memcpy((char*)&fh.fh_handle.fh_base, f->data, f->size);
34         fh.fh_export = NULL;
35
36         exp_readlock();
37         nfserr = nfsd_open(rqstp, &fh, S_IFREG, MAY_LOCK, filp);
38         if (!nfserr) {
39                 dget(filp->f_dentry);
40                 mntget(filp->f_vfsmnt);
41         }
42         fh_put(&fh);
43         rqstp->rq_client = NULL;
44         exp_readunlock();
45         /* nlm and nfsd don't share error codes.
46          * we invent: 0 = no error
47          *            1 = stale file handle
48          *            2 = other error
49          */
50         switch (nfserr) {
51         case nfs_ok:
52                 return 0;
53         case nfserr_stale:
54                 return 1;
55         default:
56                 return 2;
57         }
58 }
59
60 static void
61 nlm_fclose(struct file *filp)
62 {
63         nfsd_close(filp);
64         dput(filp->f_dentry);
65         mntput(filp->f_vfsmnt);
66 }
67
68 struct nlmsvc_binding           nfsd_nlm_ops = {
69         .fopen          = nlm_fopen,            /* open file for locking */
70         .fclose         = nlm_fclose,           /* close file */
71 };
72
73 void
74 nfsd_lockd_init(void)
75 {
76         dprintk("nfsd: initializing lockd\n");
77         nlmsvc_ops = &nfsd_nlm_ops;
78 }
79
80 void
81 nfsd_lockd_shutdown(void)
82 {
83         nlmsvc_ops = NULL;
84 }