2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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
28 * from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/fcntl.h>
38 #include <sys/kernel.h> /* for hz */
39 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/lockf.h> /* for hz */ /* Must come after sys/malloc.h */
44 #include <sys/mount.h>
45 #include <sys/namei.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>
56 #include <nfs/nfsproto.h>
57 #include <nfs/nfs_lock.h>
58 #include <nfsclient/nfs.h>
59 #include <nfsclient/nfsmount.h>
60 #include <nfsclient/nfsnode.h>
61 #include <nfsclient/nlminfo.h>
63 extern void (*nlminfo_release_p)(struct proc *p);
65 vop_advlock_t *nfs_advlock_p = nfs_dolock;
66 vop_reclaim_t *nfs_reclaim_p = NULL;
68 static MALLOC_DEFINE(M_NFSLOCK, "nfsclient_lock", "NFS lock request");
69 static MALLOC_DEFINE(M_NLMINFO, "nfsclient_nlminfo",
70 "NFS lock process structure");
72 static int nfslockdans(struct thread *td, struct lockd_ans *ansp);
73 static void nlminfo_release(struct proc *p);
75 * --------------------------------------------------------------------
76 * A miniature device driver which the userland uses to talk to us.
80 static struct cdev *nfslock_dev;
81 static struct mtx nfslock_mtx;
82 static int nfslock_isopen;
83 static TAILQ_HEAD(,__lock_msg) nfslock_list;
86 nfslock_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
90 error = priv_check(td, PRIV_NFS_LOCKD);
94 mtx_lock(&nfslock_mtx);
95 if (!nfslock_isopen) {
101 mtx_unlock(&nfslock_mtx);
107 nfslock_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
109 struct __lock_msg *lm;
111 mtx_lock(&nfslock_mtx);
113 while (!TAILQ_EMPTY(&nfslock_list)) {
114 lm = TAILQ_FIRST(&nfslock_list);
115 /* XXX: answer request */
116 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
119 mtx_unlock(&nfslock_mtx);
124 nfslock_read(struct cdev *dev, struct uio *uio, int ioflag)
127 struct __lock_msg *lm;
129 if (uio->uio_resid != sizeof *lm)
133 mtx_lock(&nfslock_mtx);
134 while (TAILQ_EMPTY(&nfslock_list)) {
135 error = msleep(&nfslock_list, &nfslock_mtx, PSOCK | PCATCH,
141 lm = TAILQ_FIRST(&nfslock_list);
142 TAILQ_REMOVE(&nfslock_list, lm, lm_link);
144 mtx_unlock(&nfslock_mtx);
146 error = uiomove(lm, sizeof *lm, uio);
153 nfslock_write(struct cdev *dev, struct uio *uio, int ioflag)
158 if (uio->uio_resid != sizeof la)
160 error = uiomove(&la, sizeof la, uio);
162 error = nfslockdans(curthread, &la);
167 nfslock_send(struct __lock_msg *lm)
169 struct __lock_msg *lm2;
173 lm2 = malloc(sizeof *lm2, M_NFSLOCK, M_WAITOK);
174 mtx_lock(&nfslock_mtx);
175 if (nfslock_isopen) {
176 memcpy(lm2, lm, sizeof *lm2);
177 TAILQ_INSERT_TAIL(&nfslock_list, lm2, lm_link);
178 wakeup(&nfslock_list);
182 mtx_unlock(&nfslock_mtx);
184 free(lm2, M_NFSLOCK);
188 static struct cdevsw nfslock_cdevsw = {
189 .d_version = D_VERSION,
190 .d_open = nfslock_open,
191 .d_close = nfslock_close,
192 .d_read = nfslock_read,
193 .d_write = nfslock_write,
198 nfslock_modevent(module_t mod __unused, int type, void *data __unused)
204 printf("nfslock: pseudo-device\n");
205 mtx_init(&nfslock_mtx, "nfslock", NULL, MTX_DEF);
206 TAILQ_INIT(&nfslock_list);
207 nlminfo_release_p = nlminfo_release;
208 nfslock_dev = make_dev(&nfslock_cdevsw, 0,
209 UID_ROOT, GID_KMEM, 0600, _PATH_NFSLCKDEV);
216 DEV_MODULE(nfslock, nfslock_modevent, NULL);
217 MODULE_VERSION(nfslock, 1);
222 * We have to let the process know if the call succeeded. I'm using an extra
223 * field in the p_nlminfo field in the proc structure, as it is already for
229 * NFS advisory byte-level locks.
231 * The vnode shall be (shared) locked on the entry, it is
232 * unconditionally unlocked after.
235 nfs_dolock(struct vop_advlock_args *ap)
243 struct nfsmount *nmp;
244 struct timeval boottime;
251 nmp = VFSTONFS(vp->v_mount);
253 ASSERT_VOP_LOCKED(vp, "nfs_dolock");
255 nmp->nm_getinfo(vp, msg.lm_fh, &msg.lm_fh_len, &msg.lm_addr,
256 &msg.lm_nfsv3, NULL, NULL);
260 * the NLM protocol doesn't allow the server to return an error
261 * on ranges, so we do it.
263 if (fl->l_whence != SEEK_END) {
264 if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
267 (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
270 (fl->l_len - 1 > OFF_MAX - fl->l_start))
275 * Fill in the information structure.
277 msg.lm_version = LOCKD_MSG_VERSION;
278 msg.lm_msg_ident.pid = p->p_pid;
282 * if there is no nfsowner table yet, allocate one.
284 if (p->p_nlminfo == NULL) {
285 p->p_nlminfo = malloc(sizeof(struct nlminfo),
286 M_NLMINFO, M_WAITOK | M_ZERO);
287 p->p_nlminfo->pid_start = p->p_stats->p_start;
288 getboottime(&boottime);
289 timevaladd(&p->p_nlminfo->pid_start, &boottime);
291 msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
292 msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
295 msg.lm_wait = ap->a_flags & F_WAIT;
296 msg.lm_getlk = ap->a_op == F_GETLK;
297 cru2x(td->td_ucred, &msg.lm_cred);
300 error = nfslock_send(&msg);
304 /* Unlocks succeed immediately. */
305 if (fl->l_type == F_UNLCK)
309 * Retry after 20 seconds if we haven't gotten a response yet.
310 * This number was picked out of thin air... but is longer
311 * then even a reasonably loaded system should take (at least
312 * on a local network). XXX Probably should use a back-off
315 * XXX: No PCATCH here since we currently have no useful
316 * way to signal to the userland rpc.lockd that the request
317 * has been aborted. Once the rpc.lockd implementation
318 * can handle aborts, and we report them properly,
319 * PCATCH can be put back. In the mean time, if we did
320 * permit aborting, the lock attempt would "get lost"
321 * and the lock would get stuck in the locked state.
323 error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
325 if (error == EWOULDBLOCK) {
327 * We timed out, so we rewrite the request
336 if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
337 if (p->p_nlminfo->set_getlk_pid) {
338 fl->l_sysid = 0; /* XXX */
339 fl->l_pid = p->p_nlminfo->getlk_pid;
341 fl->l_type = F_UNLCK;
344 error = p->p_nlminfo->retcode;
354 * NFS advisory byte-level locks answer from the lock daemon.
357 nfslockdans(struct thread *td, struct lockd_ans *ansp)
359 struct proc *targetp;
361 /* the version should match, or we're out of sync */
362 if (ansp->la_vers != LOCKD_ANS_VERSION)
365 /* Find the process, set its return errno and wake it up. */
366 if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
369 /* verify the pid hasn't been reused (if we can), and it isn't waiting
370 * for an answer from a more recent request. We return an EPIPE if
371 * the match fails, because we've already used ESRCH above, and this
372 * is sort of like writing on a pipe after the reader has closed it.
374 if (targetp->p_nlminfo == NULL ||
375 ((ansp->la_msg_ident.msg_seq != -1) &&
376 (timevalcmp(&targetp->p_nlminfo->pid_start,
377 &ansp->la_msg_ident.pid_start, !=) ||
378 targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
379 PROC_UNLOCK(targetp);
383 targetp->p_nlminfo->retcode = ansp->la_errno;
384 targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
385 targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
387 wakeup(targetp->p_nlminfo);
389 PROC_UNLOCK(targetp);
394 * Free nlminfo attached to process.
397 nlminfo_release(struct proc *p)
399 free(p->p_nlminfo, M_NLMINFO);