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