2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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
30 * from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/fcntl.h>
40 #include <sys/kernel.h> /* for hz */
41 #include <sys/limits.h>
43 #include <sys/malloc.h>
44 #include <sys/lockf.h> /* for hz */ /* Must come after sys/malloc.h */
46 #include <sys/mount.h>
47 #include <sys/namei.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>
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>
65 extern void (*nlminfo_release_p)(struct proc *p);
67 vop_advlock_t *nfs_advlock_p = nfs_dolock;
68 vop_reclaim_t *nfs_reclaim_p = NULL;
70 static MALLOC_DEFINE(M_NFSLOCK, "nfsclient_lock", "NFS lock request");
71 static MALLOC_DEFINE(M_NLMINFO, "nfsclient_nlminfo",
72 "NFS lock process structure");
74 static int nfslockdans(struct thread *td, struct lockd_ans *ansp);
75 static void nlminfo_release(struct proc *p);
77 * --------------------------------------------------------------------
78 * A miniature device driver which the userland uses to talk to us.
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;
88 nfslock_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
92 error = priv_check(td, PRIV_NFS_LOCKD);
96 mtx_lock(&nfslock_mtx);
97 if (!nfslock_isopen) {
103 mtx_unlock(&nfslock_mtx);
109 nfslock_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
111 struct __lock_msg *lm;
113 mtx_lock(&nfslock_mtx);
115 while (!TAILQ_EMPTY(&nfslock_list)) {
116 lm = TAILQ_FIRST(&nfslock_list);
117 /* XXX: answer request */
118 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
121 mtx_unlock(&nfslock_mtx);
126 nfslock_read(struct cdev *dev, struct uio *uio, int ioflag)
129 struct __lock_msg *lm;
131 if (uio->uio_resid != sizeof *lm)
135 mtx_lock(&nfslock_mtx);
136 while (TAILQ_EMPTY(&nfslock_list)) {
137 error = msleep(&nfslock_list, &nfslock_mtx, PSOCK | PCATCH,
143 lm = TAILQ_FIRST(&nfslock_list);
144 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
146 mtx_unlock(&nfslock_mtx);
148 error = uiomove(lm, sizeof *lm, uio);
155 nfslock_write(struct cdev *dev, struct uio *uio, int ioflag)
160 if (uio->uio_resid != sizeof la)
162 error = uiomove(&la, sizeof la, uio);
164 error = nfslockdans(curthread, &la);
169 nfslock_send(struct __lock_msg *lm)
171 struct __lock_msg *lm2;
175 lm2 = malloc(sizeof *lm2, M_NFSLOCK, M_WAITOK);
176 mtx_lock(&nfslock_mtx);
177 if (nfslock_isopen) {
178 memcpy(lm2, lm, sizeof *lm2);
179 TAILQ_INSERT_TAIL(&nfslock_list, lm2, lm_link);
180 wakeup(&nfslock_list);
184 mtx_unlock(&nfslock_mtx);
186 free(lm2, M_NFSLOCK);
190 static struct cdevsw nfslock_cdevsw = {
191 .d_version = D_VERSION,
192 .d_open = nfslock_open,
193 .d_close = nfslock_close,
194 .d_read = nfslock_read,
195 .d_write = nfslock_write,
200 nfslock_modevent(module_t mod __unused, int type, void *data __unused)
206 printf("nfslock: pseudo-device\n");
207 mtx_init(&nfslock_mtx, "nfslock", NULL, MTX_DEF);
208 TAILQ_INIT(&nfslock_list);
209 nlminfo_release_p = nlminfo_release;
210 nfslock_dev = make_dev(&nfslock_cdevsw, 0,
211 UID_ROOT, GID_KMEM, 0600, _PATH_NFSLCKDEV);
218 DEV_MODULE(nfslock, nfslock_modevent, NULL);
219 MODULE_VERSION(nfslock, 1);
224 * We have to let the process know if the call succeeded. I'm using an extra
225 * field in the p_nlminfo field in the proc structure, as it is already for
231 * NFS advisory byte-level locks.
233 * The vnode shall be (shared) locked on the entry, it is
234 * unconditionally unlocked after.
237 nfs_dolock(struct vop_advlock_args *ap)
245 struct nfsmount *nmp;
246 struct timeval boottime;
253 nmp = VFSTONFS(vp->v_mount);
255 ASSERT_VOP_LOCKED(vp, "nfs_dolock");
257 nmp->nm_getinfo(vp, msg.lm_fh, &msg.lm_fh_len, &msg.lm_addr,
258 &msg.lm_nfsv3, NULL, NULL);
262 * the NLM protocol doesn't allow the server to return an error
263 * on ranges, so we do it.
265 if (fl->l_whence != SEEK_END) {
266 if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
269 (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
272 (fl->l_len - 1 > OFF_MAX - fl->l_start))
277 * Fill in the information structure.
279 msg.lm_version = LOCKD_MSG_VERSION;
280 msg.lm_msg_ident.pid = p->p_pid;
284 * if there is no nfsowner table yet, allocate one.
286 if (p->p_nlminfo == NULL) {
287 p->p_nlminfo = malloc(sizeof(struct nlminfo),
288 M_NLMINFO, M_WAITOK | M_ZERO);
289 p->p_nlminfo->pid_start = p->p_stats->p_start;
290 getboottime(&boottime);
291 timevaladd(&p->p_nlminfo->pid_start, &boottime);
293 msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
294 msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
297 msg.lm_wait = ap->a_flags & F_WAIT;
298 msg.lm_getlk = ap->a_op == F_GETLK;
299 cru2x(td->td_ucred, &msg.lm_cred);
302 error = nfslock_send(&msg);
306 /* Unlocks succeed immediately. */
307 if (fl->l_type == F_UNLCK)
311 * Retry after 20 seconds if we haven't gotten a response yet.
312 * This number was picked out of thin air... but is longer
313 * then even a reasonably loaded system should take (at least
314 * on a local network). XXX Probably should use a back-off
317 * XXX: No PCATCH here since we currently have no useful
318 * way to signal to the userland rpc.lockd that the request
319 * has been aborted. Once the rpc.lockd implementation
320 * can handle aborts, and we report them properly,
321 * PCATCH can be put back. In the mean time, if we did
322 * permit aborting, the lock attempt would "get lost"
323 * and the lock would get stuck in the locked state.
325 error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
327 if (error == EWOULDBLOCK) {
329 * We timed out, so we rewrite the request
338 if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
339 if (p->p_nlminfo->set_getlk_pid) {
340 fl->l_sysid = 0; /* XXX */
341 fl->l_pid = p->p_nlminfo->getlk_pid;
343 fl->l_type = F_UNLCK;
346 error = p->p_nlminfo->retcode;
356 * NFS advisory byte-level locks answer from the lock daemon.
359 nfslockdans(struct thread *td, struct lockd_ans *ansp)
361 struct proc *targetp;
363 /* the version should match, or we're out of sync */
364 if (ansp->la_vers != LOCKD_ANS_VERSION)
367 /* Find the process, set its return errno and wake it up. */
368 if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
371 /* verify the pid hasn't been reused (if we can), and it isn't waiting
372 * for an answer from a more recent request. We return an EPIPE if
373 * the match fails, because we've already used ESRCH above, and this
374 * is sort of like writing on a pipe after the reader has closed it.
376 if (targetp->p_nlminfo == NULL ||
377 ((ansp->la_msg_ident.msg_seq != -1) &&
378 (timevalcmp(&targetp->p_nlminfo->pid_start,
379 &ansp->la_msg_ident.pid_start, !=) ||
380 targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
381 PROC_UNLOCK(targetp);
385 targetp->p_nlminfo->retcode = ansp->la_errno;
386 targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
387 targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
389 wakeup(targetp->p_nlminfo);
391 PROC_UNLOCK(targetp);
396 * Free nlminfo attached to process.
399 nlminfo_release(struct proc *p)
401 free(p->p_nlminfo, M_NLMINFO);