]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/nfsclient/nfs_lock.c
This commit was generated by cvs2svn to compensate for changes in r154032,
[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/proc.h>
47 #include <sys/resourcevar.h>
48 #include <sys/socket.h>
49 #include <sys/socket.h>
50 #include <sys/unistd.h>
51 #include <sys/vnode.h>
52
53 #include <net/if.h>
54
55 #include <rpc/rpcclnt.h>
56
57 #include <nfs/rpcv2.h>
58 #include <nfs/nfsproto.h>
59 #include <nfsclient/nfs.h>
60 #include <nfsclient/nfsmount.h>
61 #include <nfsclient/nfsnode.h>
62 #include <nfsclient/nfs_lock.h>
63 #include <nfsclient/nlminfo.h>
64
65 extern void (*nlminfo_release_p)(struct proc *p);
66
67 MALLOC_DEFINE(M_NFSLOCK, "nfsclient_lock", "NFS lock request");
68 MALLOC_DEFINE(M_NLMINFO, "nfsclient_nlminfo", "NFS lock process structure");
69
70 static int nfslockdans(struct thread *td, struct lockd_ans *ansp);
71 static void nlminfo_release(struct proc *p);
72 /*
73  * --------------------------------------------------------------------
74  * A miniature device driver which the userland uses to talk to us.
75  *
76  */
77
78 static struct cdev *nfslock_dev;
79 static struct mtx nfslock_mtx;
80 static int nfslock_isopen;
81 static TAILQ_HEAD(,__lock_msg)  nfslock_list;
82
83 static int
84 nfslock_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
85 {
86         int error;
87
88         mtx_lock(&nfslock_mtx);
89         if (!nfslock_isopen) {
90                 error = 0;
91                 nfslock_isopen = 1;
92         } else {
93                 error = EOPNOTSUPP;
94         }
95         mtx_unlock(&nfslock_mtx);
96                 
97         return (error);
98 }
99
100 static int
101 nfslock_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
102 {
103         struct __lock_msg *lm;
104
105         mtx_lock(&nfslock_mtx);
106         nfslock_isopen = 0;
107         while (!TAILQ_EMPTY(&nfslock_list)) {
108                 lm = TAILQ_FIRST(&nfslock_list);
109                 /* XXX: answer request */
110                 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
111                 free(lm, M_NFSLOCK);
112         }
113         mtx_unlock(&nfslock_mtx);
114         return (0);
115 }
116
117 static int
118 nfslock_read(struct cdev *dev, struct uio *uio, int ioflag)
119 {
120         int error;
121         struct __lock_msg *lm;
122
123         if (uio->uio_resid != sizeof *lm)
124                 return (EOPNOTSUPP);
125         lm = NULL;
126         error = 0;
127         mtx_lock(&nfslock_mtx);
128         while (TAILQ_EMPTY(&nfslock_list)) {
129                 error = msleep(&nfslock_list, &nfslock_mtx, PSOCK | PCATCH,
130                     "nfslockd", 0);
131                 if (error)
132                         break;
133         }
134         if (!error) {
135                 lm = TAILQ_FIRST(&nfslock_list);
136                 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
137         }
138         mtx_unlock(&nfslock_mtx);
139         if (!error) {
140                 error = uiomove(lm, sizeof *lm, uio);
141                 free(lm, M_NFSLOCK);
142         }
143         return (error);
144 }
145
146 static int
147 nfslock_write(struct cdev *dev, struct uio *uio, int ioflag)
148 {
149         struct lockd_ans la;
150         int error;
151
152         if (uio->uio_resid != sizeof la)
153                 return (EOPNOTSUPP);
154         error = uiomove(&la, sizeof la, uio);
155         if (!error)
156                 error = nfslockdans(curthread, &la);
157         return (error);
158 }
159
160 static int
161 nfslock_send(struct __lock_msg *lm)
162 {
163         struct __lock_msg *lm2;
164         int error;
165
166         error = 0;
167         lm2 = malloc(sizeof *lm2, M_NFSLOCK, M_WAITOK);
168         mtx_lock(&nfslock_mtx);
169         if (nfslock_isopen) {
170                 memcpy(lm2, lm, sizeof *lm2);
171                 TAILQ_INSERT_TAIL(&nfslock_list, lm2, lm_link);
172                 wakeup(&nfslock_list);
173         } else {
174                 error = EOPNOTSUPP;
175         }
176         mtx_unlock(&nfslock_mtx);
177         if (error)
178                 free(lm2, M_NFSLOCK);
179         return (error);
180 }
181
182 static struct cdevsw nfslock_cdevsw = {
183         .d_version =    D_VERSION,
184         .d_open =       nfslock_open,
185         .d_close =      nfslock_close,
186         .d_read =       nfslock_read,
187         .d_write =      nfslock_write,
188         .d_name =       "nfslock"
189 };
190
191 static int
192 nfslock_modevent(module_t mod __unused, int type, void *data __unused)
193 {
194
195         switch (type) {
196         case MOD_LOAD:
197                 if (bootverbose)
198                         printf("nfslock: pseudo-device\n");
199                 mtx_init(&nfslock_mtx, "nfslock", NULL, MTX_DEF);
200                 TAILQ_INIT(&nfslock_list);
201                 nlminfo_release_p = nlminfo_release;
202                 nfslock_dev = make_dev(&nfslock_cdevsw, 0,
203                     UID_ROOT, GID_KMEM, 0600, _PATH_NFSLCKDEV);
204                 return (0);
205         default:
206                 return (EOPNOTSUPP);
207         }
208 }
209
210 DEV_MODULE(nfslock, nfslock_modevent, NULL);
211 MODULE_VERSION(nfslock, 1);
212
213
214 /*
215  * XXX
216  * We have to let the process know if the call succeeded.  I'm using an extra
217  * field in the p_nlminfo field in the proc structure, as it is already for
218  * lockd stuff.
219  */
220
221 /*
222  * nfs_advlock --
223  *      NFS advisory byte-level locks.
224  */
225 int
226 nfs_dolock(struct vop_advlock_args *ap)
227 {
228         LOCKD_MSG msg;
229         struct thread *td;
230         struct vnode *vp;
231         int error;
232         struct flock *fl;
233         int ioflg;
234         struct proc *p;
235
236         td = curthread;
237         p = td->td_proc;
238
239         vp = ap->a_vp;
240         fl = ap->a_fl;
241
242         /*
243          * the NLM protocol doesn't allow the server to return an error
244          * on ranges, so we do it.
245          */
246         if (fl->l_whence != SEEK_END) {
247                 if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
248                     fl->l_start < 0 ||
249                     (fl->l_len < 0 &&
250                      (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
251                         return (EINVAL);
252                 if (fl->l_len > 0 &&
253                          (fl->l_len - 1 > OFF_MAX - fl->l_start))
254                         return (EOVERFLOW);
255         }
256
257         /*
258          * Fill in the information structure.
259          */
260         msg.lm_version = LOCKD_MSG_VERSION;
261         msg.lm_msg_ident.pid = p->p_pid;
262         /*
263          * if there is no nfsowner table yet, allocate one.
264          */
265         if (p->p_nlminfo == NULL) {
266                 MALLOC(p->p_nlminfo, struct nlminfo *,
267                         sizeof(struct nlminfo), M_NLMINFO, M_WAITOK | M_ZERO);
268                 p->p_nlminfo->pid_start = p->p_stats->p_start;
269                 timevaladd(&p->p_nlminfo->pid_start, &boottime);
270         }
271         msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
272         msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
273
274         msg.lm_fl = *fl;
275         msg.lm_wait = ap->a_flags & F_WAIT;
276         msg.lm_getlk = ap->a_op == F_GETLK;
277         bcopy(VFSTONFS(vp->v_mount)->nm_nam, &msg.lm_addr,
278                 min(sizeof msg.lm_addr, VFSTONFS(vp->v_mount)->nm_nam->sa_len));
279         msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH;
280         bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len);
281         msg.lm_nfsv3 = NFS_ISV3(vp);
282         cru2x(td->td_ucred, &msg.lm_cred);
283
284         for (;;) {
285                 error = nfslock_send(&msg);
286                 if (error)
287                         return (error);
288
289                 /* Unlocks succeed immediately.  */
290                 if (fl->l_type == F_UNLCK)
291                         return (error);
292
293                 /*
294                  * retry after 20 seconds if we haven't gotten a responce yet.
295                  * This number was picked out of thin air... but is longer
296                  * then even a reasonably loaded system should take (at least
297                  * on a local network).  XXX Probably should use a back-off
298                  * scheme.
299                  *
300                  * XXX: No PCATCH here since we currently have no useful
301                  * way to signal to the userland rpc.lockd that the request
302                  * has been aborted.  Once the rpc.lockd implementation
303                  * can handle aborts, and we report them properly,
304                  * PCATCH can be put back.  In the mean time, if we did
305                  * permit aborting, the lock attempt would "get lost"
306                  * and the lock would get stuck in the locked state.
307                  */
308                 error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
309                 if (error != 0) {
310                         if (error == EWOULDBLOCK) {
311                                 /*
312                                  * We timed out, so we rewrite the request
313                                  * to the fifo, but only if it isn't already
314                                  * full.
315                                  */
316                                 ioflg |= IO_NDELAY;
317                                 continue;
318                         }
319
320                         break;
321                 }
322
323                 if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
324                         if (p->p_nlminfo->set_getlk_pid) {
325                                 fl->l_pid = p->p_nlminfo->getlk_pid;
326                         } else {
327                                 fl->l_type = F_UNLCK;
328                         }
329                 }
330                 error = p->p_nlminfo->retcode;
331                 break;
332         }
333
334         return (error);
335 }
336
337 /*
338  * nfslockdans --
339  *      NFS advisory byte-level locks answer from the lock daemon.
340  */
341 static int
342 nfslockdans(struct thread *td, struct lockd_ans *ansp)
343 {
344         struct proc *targetp;
345         int error;
346
347         /* Let root, or someone who once was root (lockd generally
348          * switches to the daemon uid once it is done setting up) make
349          * this call.
350          *
351          * XXX This authorization check is probably not right.
352          */
353         if ((error = suser(td)) != 0 &&
354             td->td_ucred->cr_svuid != 0)
355                 return (error);
356
357         /* the version should match, or we're out of sync */
358         if (ansp->la_vers != LOCKD_ANS_VERSION)
359                 return (EINVAL);
360
361         /* Find the process, set its return errno and wake it up. */
362         if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
363                 return (ESRCH);
364
365         /* verify the pid hasn't been reused (if we can), and it isn't waiting
366          * for an answer from a more recent request.  We return an EPIPE if
367          * the match fails, because we've already used ESRCH above, and this
368          * is sort of like writing on a pipe after the reader has closed it.
369          */
370         if (targetp->p_nlminfo == NULL ||
371             ((ansp->la_msg_ident.msg_seq != -1) &&
372               (timevalcmp(&targetp->p_nlminfo->pid_start,
373                         &ansp->la_msg_ident.pid_start, !=) ||
374                targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
375                 PROC_UNLOCK(targetp);
376                 return (EPIPE);
377         }
378
379         targetp->p_nlminfo->retcode = ansp->la_errno;
380         targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
381         targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
382
383         wakeup(targetp->p_nlminfo);
384
385         PROC_UNLOCK(targetp);
386         return (0);
387 }
388
389 /*
390  * Free nlminfo attached to process.
391  */
392 void        
393 nlminfo_release(struct proc *p)
394 {  
395         free(p->p_nlminfo, M_NLMINFO);
396         p->p_nlminfo = NULL;
397 }