]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_lock.c
MFC: Kernel mode Network Lock Manager.
[FreeBSD/FreeBSD.git] / sys / nfsclient / nfs_lock.c
1 /*-
2  * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Berkeley Software Design Inc's name may not be used to endorse or
13  *    promote products derived from this software without specific prior
14  *    written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *      from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/fcntl.h>
38 #include <sys/kernel.h>         /* for hz */
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/lockf.h>          /* for hz */ /* Must come after sys/malloc.h */
43 #include <sys/mbuf.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/resourcevar.h>
49 #include <sys/socket.h>
50 #include <sys/socket.h>
51 #include <sys/unistd.h>
52 #include <sys/vnode.h>
53
54 #include <net/if.h>
55
56 #include <rpc/rpcclnt.h>
57
58 #include <nfs/rpcv2.h>
59 #include <nfs/nfsproto.h>
60 #include <nfsclient/nfs.h>
61 #include <nfsclient/nfsmount.h>
62 #include <nfsclient/nfsnode.h>
63 #include <nfsclient/nfs_lock.h>
64 #include <nfsclient/nlminfo.h>
65
66 extern void (*nlminfo_release_p)(struct proc *p);
67
68 MALLOC_DEFINE(M_NFSLOCK, "nfsclient_lock", "NFS lock request");
69 MALLOC_DEFINE(M_NLMINFO, "nfsclient_nlminfo", "NFS lock process structure");
70
71 static int nfslockdans(struct thread *td, struct lockd_ans *ansp);
72 static void nlminfo_release(struct proc *p);
73 /*
74  * --------------------------------------------------------------------
75  * A miniature device driver which the userland uses to talk to us.
76  *
77  */
78
79 static struct cdev *nfslock_dev;
80 static struct mtx nfslock_mtx;
81 static int nfslock_isopen;
82 static TAILQ_HEAD(,__lock_msg)  nfslock_list;
83
84 static int
85 nfslock_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
86 {
87         int error;
88
89         error = priv_check(td, PRIV_NFS_LOCKD);
90         if (error)
91                 return (error);
92
93         mtx_lock(&nfslock_mtx);
94         if (!nfslock_isopen) {
95                 error = 0;
96                 nfslock_isopen = 1;
97         } else {
98                 error = EOPNOTSUPP;
99         }
100         mtx_unlock(&nfslock_mtx);
101                 
102         return (error);
103 }
104
105 static int
106 nfslock_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
107 {
108         struct __lock_msg *lm;
109
110         mtx_lock(&nfslock_mtx);
111         nfslock_isopen = 0;
112         while (!TAILQ_EMPTY(&nfslock_list)) {
113                 lm = TAILQ_FIRST(&nfslock_list);
114                 /* XXX: answer request */
115                 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
116                 free(lm, M_NFSLOCK);
117         }
118         mtx_unlock(&nfslock_mtx);
119         return (0);
120 }
121
122 static int
123 nfslock_read(struct cdev *dev, struct uio *uio, int ioflag)
124 {
125         int error;
126         struct __lock_msg *lm;
127
128         if (uio->uio_resid != sizeof *lm)
129                 return (EOPNOTSUPP);
130         lm = NULL;
131         error = 0;
132         mtx_lock(&nfslock_mtx);
133         while (TAILQ_EMPTY(&nfslock_list)) {
134                 error = msleep(&nfslock_list, &nfslock_mtx, PSOCK | PCATCH,
135                     "nfslockd", 0);
136                 if (error)
137                         break;
138         }
139         if (!error) {
140                 lm = TAILQ_FIRST(&nfslock_list);
141                 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
142         }
143         mtx_unlock(&nfslock_mtx);
144         if (!error) {
145                 error = uiomove(lm, sizeof *lm, uio);
146                 free(lm, M_NFSLOCK);
147         }
148         return (error);
149 }
150
151 static int
152 nfslock_write(struct cdev *dev, struct uio *uio, int ioflag)
153 {
154         struct lockd_ans la;
155         int error;
156
157         if (uio->uio_resid != sizeof la)
158                 return (EOPNOTSUPP);
159         error = uiomove(&la, sizeof la, uio);
160         if (!error)
161                 error = nfslockdans(curthread, &la);
162         return (error);
163 }
164
165 static int
166 nfslock_send(struct __lock_msg *lm)
167 {
168         struct __lock_msg *lm2;
169         int error;
170
171         error = 0;
172         lm2 = malloc(sizeof *lm2, M_NFSLOCK, M_WAITOK);
173         mtx_lock(&nfslock_mtx);
174         if (nfslock_isopen) {
175                 memcpy(lm2, lm, sizeof *lm2);
176                 TAILQ_INSERT_TAIL(&nfslock_list, lm2, lm_link);
177                 wakeup(&nfslock_list);
178         } else {
179                 error = EOPNOTSUPP;
180         }
181         mtx_unlock(&nfslock_mtx);
182         if (error)
183                 free(lm2, M_NFSLOCK);
184         return (error);
185 }
186
187 static struct cdevsw nfslock_cdevsw = {
188         .d_version =    D_VERSION,
189         .d_open =       nfslock_open,
190         .d_close =      nfslock_close,
191         .d_read =       nfslock_read,
192         .d_write =      nfslock_write,
193         .d_name =       "nfslock"
194 };
195
196 static int
197 nfslock_modevent(module_t mod __unused, int type, void *data __unused)
198 {
199
200         switch (type) {
201         case MOD_LOAD:
202                 if (bootverbose)
203                         printf("nfslock: pseudo-device\n");
204                 mtx_init(&nfslock_mtx, "nfslock", NULL, MTX_DEF);
205                 TAILQ_INIT(&nfslock_list);
206                 nlminfo_release_p = nlminfo_release;
207                 nfslock_dev = make_dev(&nfslock_cdevsw, 0,
208                     UID_ROOT, GID_KMEM, 0600, _PATH_NFSLCKDEV);
209                 return (0);
210         default:
211                 return (EOPNOTSUPP);
212         }
213 }
214
215 DEV_MODULE(nfslock, nfslock_modevent, NULL);
216 MODULE_VERSION(nfslock, 1);
217
218
219 /*
220  * XXX
221  * We have to let the process know if the call succeeded.  I'm using an extra
222  * field in the p_nlminfo field in the proc structure, as it is already for
223  * lockd stuff.
224  */
225
226 /*
227  * nfs_advlock --
228  *      NFS advisory byte-level locks.
229  */
230 int
231 nfs_dolock(struct vop_advlock_args *ap)
232 {
233         LOCKD_MSG msg;
234         struct thread *td;
235         struct vnode *vp;
236         int error;
237         struct flock *fl;
238         struct proc *p;
239
240         td = curthread;
241         p = td->td_proc;
242
243         vp = ap->a_vp;
244         fl = ap->a_fl;
245
246         /*
247          * the NLM protocol doesn't allow the server to return an error
248          * on ranges, so we do it.
249          */
250         if (fl->l_whence != SEEK_END) {
251                 if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
252                     fl->l_start < 0 ||
253                     (fl->l_len < 0 &&
254                      (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
255                         return (EINVAL);
256                 if (fl->l_len > 0 &&
257                          (fl->l_len - 1 > OFF_MAX - fl->l_start))
258                         return (EOVERFLOW);
259         }
260
261         /*
262          * Fill in the information structure.
263          */
264         msg.lm_version = LOCKD_MSG_VERSION;
265         msg.lm_msg_ident.pid = p->p_pid;
266         /*
267          * if there is no nfsowner table yet, allocate one.
268          */
269         if (p->p_nlminfo == NULL) {
270                 MALLOC(p->p_nlminfo, struct nlminfo *,
271                         sizeof(struct nlminfo), M_NLMINFO, M_WAITOK | M_ZERO);
272                 p->p_nlminfo->pid_start = p->p_stats->p_start;
273                 timevaladd(&p->p_nlminfo->pid_start, &boottime);
274         }
275         msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
276         msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
277
278         msg.lm_fl = *fl;
279         msg.lm_wait = ap->a_flags & F_WAIT;
280         msg.lm_getlk = ap->a_op == F_GETLK;
281         bcopy(VFSTONFS(vp->v_mount)->nm_nam, &msg.lm_addr,
282                 min(sizeof msg.lm_addr, VFSTONFS(vp->v_mount)->nm_nam->sa_len));
283         msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH;
284         bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len);
285         msg.lm_nfsv3 = NFS_ISV3(vp);
286         cru2x(td->td_ucred, &msg.lm_cred);
287
288         for (;;) {
289                 error = nfslock_send(&msg);
290                 if (error)
291                         return (error);
292
293                 /* Unlocks succeed immediately.  */
294                 if (fl->l_type == F_UNLCK)
295                         return (error);
296
297                 /*
298                  * Retry after 20 seconds if we haven't gotten a response yet.
299                  * This number was picked out of thin air... but is longer
300                  * then even a reasonably loaded system should take (at least
301                  * on a local network).  XXX Probably should use a back-off
302                  * scheme.
303                  *
304                  * XXX: No PCATCH here since we currently have no useful
305                  * way to signal to the userland rpc.lockd that the request
306                  * has been aborted.  Once the rpc.lockd implementation
307                  * can handle aborts, and we report them properly,
308                  * PCATCH can be put back.  In the mean time, if we did
309                  * permit aborting, the lock attempt would "get lost"
310                  * and the lock would get stuck in the locked state.
311                  */
312                 error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
313                 if (error != 0) {
314                         if (error == EWOULDBLOCK) {
315                                 /*
316                                  * We timed out, so we rewrite the request
317                                  * to the fifo.
318                                  */
319                                 continue;
320                         }
321
322                         break;
323                 }
324
325                 if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
326                         if (p->p_nlminfo->set_getlk_pid) {
327                                 fl->l_sysid = 0; /* XXX */
328                                 fl->l_pid = p->p_nlminfo->getlk_pid;
329                         } else {
330                                 fl->l_type = F_UNLCK;
331                         }
332                 }
333                 error = p->p_nlminfo->retcode;
334                 break;
335         }
336
337         return (error);
338 }
339
340 /*
341  * nfslockdans --
342  *      NFS advisory byte-level locks answer from the lock daemon.
343  */
344 static int
345 nfslockdans(struct thread *td, struct lockd_ans *ansp)
346 {
347         struct proc *targetp;
348
349         /* the version should match, or we're out of sync */
350         if (ansp->la_vers != LOCKD_ANS_VERSION)
351                 return (EINVAL);
352
353         /* Find the process, set its return errno and wake it up. */
354         if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
355                 return (ESRCH);
356
357         /* verify the pid hasn't been reused (if we can), and it isn't waiting
358          * for an answer from a more recent request.  We return an EPIPE if
359          * the match fails, because we've already used ESRCH above, and this
360          * is sort of like writing on a pipe after the reader has closed it.
361          */
362         if (targetp->p_nlminfo == NULL ||
363             ((ansp->la_msg_ident.msg_seq != -1) &&
364               (timevalcmp(&targetp->p_nlminfo->pid_start,
365                         &ansp->la_msg_ident.pid_start, !=) ||
366                targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
367                 PROC_UNLOCK(targetp);
368                 return (EPIPE);
369         }
370
371         targetp->p_nlminfo->retcode = ansp->la_errno;
372         targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
373         targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
374
375         wakeup(targetp->p_nlminfo);
376
377         PROC_UNLOCK(targetp);
378         return (0);
379 }
380
381 /*
382  * Free nlminfo attached to process.
383  */
384 void        
385 nlminfo_release(struct proc *p)
386 {  
387         free(p->p_nlminfo, M_NLMINFO);
388         p->p_nlminfo = NULL;
389 }