nfs_lock.c revision 75631
175631Salfred/*-
275631Salfred * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
375631Salfred *
475631Salfred * Redistribution and use in source and binary forms, with or without
575631Salfred * modification, are permitted provided that the following conditions
675631Salfred * are met:
775631Salfred * 1. Redistributions of source code must retain the above copyright
875631Salfred *    notice, this list of conditions and the following disclaimer.
975631Salfred * 2. Redistributions in binary form must reproduce the above copyright
1075631Salfred *    notice, this list of conditions and the following disclaimer in the
1175631Salfred *    documentation and/or other materials provided with the distribution.
1275631Salfred * 3. Berkeley Software Design Inc's name may not be used to endorse or
1375631Salfred *    promote products derived from this software without specific prior
1475631Salfred *    written permission.
1575631Salfred *
1675631Salfred * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
1775631Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1875631Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1975631Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
2075631Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2175631Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2275631Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2375631Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2475631Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2575631Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2675631Salfred * SUCH DAMAGE.
2775631Salfred *
2875631Salfred *      from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
2975631Salfred * $FreeBSD: head/sys/nfsclient/nfs_lock.c 75631 2001-04-17 20:45:23Z alfred $
3075631Salfred */
3175631Salfred
3275631Salfred#include <sys/param.h>
3375631Salfred#include <sys/systm.h>
3475631Salfred#include <sys/fcntl.h>
3575631Salfred#include <sys/malloc.h>
3675631Salfred#include <sys/mbuf.h>
3775631Salfred#include <sys/mount.h>
3875631Salfred#include <sys/namei.h>
3975631Salfred#include <sys/proc.h>
4075631Salfred#include <sys/socket.h>
4175631Salfred#include <sys/unistd.h>
4275631Salfred#include <sys/vnode.h>
4375631Salfred#include <sys/resourcevar.h>
4475631Salfred#include <sys/kernel.h>			/* for hz */
4575631Salfred#include <sys/lockf.h>			/* for hz */
4675631Salfred
4775631Salfred#include <net/if.h>
4875631Salfred
4975631Salfred#include <nfs/rpcv2.h>
5075631Salfred#include <nfs/nfsproto.h>
5175631Salfred#include <nfs/nfs.h>
5275631Salfred#include <nfs/nfsmount.h>
5375631Salfred#include <nfs/nfsnode.h>
5475631Salfred#include <nfs/nfs_lock.h>
5575631Salfred#include <nfs/nlminfo.h>
5675631Salfred
5775631Salfred#define NFSOWNER_1ST_LEVEL_START	1	       /* initial entries */
5875631Salfred#define NFSOWNER_2ND_LEVEL	      256		/* some power of 2 */
5975631Salfred
6075631Salfred#define NFSOWNER(tbl, i)	\
6175631Salfred		(tbl)[(i) / NFSOWNER_2ND_LEVEL][(i) % NFSOWNER_2ND_LEVEL]
6275631Salfred
6375631Salfred/*
6475631Salfred * XXX
6575631Salfred * We have to let the process know if the call succeeded.  I'm using an extra
6675631Salfred * field in the p_nlminfo field in the proc structure, as it is already for
6775631Salfred * lockd stuff.
6875631Salfred */
6975631Salfred
7075631Salfred/*
7175631Salfred * nfs_advlock --
7275631Salfred *      NFS advisory byte-level locks.
7375631Salfred */
7475631Salfredint
7575631Salfrednfs_dolock(ap)
7675631Salfred	struct vop_advlock_args /* {
7775631Salfred		struct vnode *a_vp;
7875631Salfred		caddr_t a_id;
7975631Salfred		int  a_op;
8075631Salfred		struct flock *a_fl;
8175631Salfred		int a_flags;
8275631Salfred	} */ *ap;
8375631Salfred{
8475631Salfred	LOCKD_MSG msg;
8575631Salfred	struct nameidata nd;
8675631Salfred	struct proc *p;
8775631Salfred	uid_t	saved_uid;
8875631Salfred	struct vnode *vp, *wvp;
8975631Salfred	int error, error1;
9075631Salfred	struct flock *fl;
9175631Salfred	int fmode, ioflg;
9275631Salfred
9375631Salfred	p = curproc;
9475631Salfred	vp = ap->a_vp;
9575631Salfred	fl = ap->a_fl;
9675631Salfred
9775631Salfred	/*
9875631Salfred	 * the NLM protocol doesn't allow the server to return an error
9975631Salfred	 * on ranges, so we do it.  Note that we should be returning
10075631Salfred	 * EOVERFLOW in some cases, but we don't have it.
10175631Salfred	 */
10275631Salfred	if (fl->l_start < 0 || fl->l_len < 0 ||
10375631Salfred	    ((fl->l_len != 0 &&
10475631Salfred	     (fl->l_start + fl->l_len - 1) < 0)))
10575631Salfred		return (EINVAL);
10675631Salfred
10775631Salfred	/*
10875631Salfred	 * Fill in the information structure.
10975631Salfred	 */
11075631Salfred	msg.lm_version = LOCKD_MSG_VERSION;
11175631Salfred	msg.lm_msg_ident.pid = p->p_pid;
11275631Salfred	/*
11375631Salfred	 * if there is no nfsowner table yet, allocate one.
11475631Salfred	 */
11575631Salfred	if (p->p_nlminfo == NULL) {
11675631Salfred		MALLOC(p->p_nlminfo, struct nlminfo *,
11775631Salfred			sizeof(struct nlminfo), M_LOCKF, M_WAITOK | M_ZERO);
11875631Salfred		p->p_nlminfo->pid_start = p->p_stats->p_start;
11975631Salfred	}
12075631Salfred	msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
12175631Salfred	msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
12275631Salfred
12375631Salfred	msg.lm_fl = *fl;
12475631Salfred	msg.lm_wait = ap->a_flags & F_WAIT;
12575631Salfred	msg.lm_getlk = ap->a_op == F_GETLK;
12675631Salfred	/*
12775631Salfred	 * XXX  -- I think this is wrong for anything other AF_INET.
12875631Salfred	 */
12975631Salfred	msg.lm_addr = *(VFSTONFS(vp->v_mount)->nm_nam);
13075631Salfred	msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH;
13175631Salfred	bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len);
13275631Salfred	msg.lm_nfsv3 = NFS_ISV3(vp);
13375631Salfred	msg.lm_cred = *(p->p_ucred);
13475631Salfred
13575631Salfred	/*
13675631Salfred	 * Open the lock fifo.  If for any reason we don't find the fifo, it
13775631Salfred	 * means that the lock daemon isn't running.  Translate any missing
13875631Salfred	 * file error message for the user, otherwise the application will
13975631Salfred	 * complain that the user's file is missing, which isn't the case.
14075631Salfred	 * Note that we use proc0's cred, so the fifo is opened as root.
14175631Salfred	 */
14275631Salfred	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, _PATH_LCKFIFO, p);
14375631Salfred
14475631Salfred	/*
14575631Salfred	 * XXX Hack to temporarily allow this process (regardless of it's creds)
14675631Salfred	 * to open the fifo we need to write to. vn_open() really should
14775631Salfred	 * take a ucred (and once it does, this code should be fixed to use
14875631Salfred	 * proc0's ucred.
14975631Salfred	 */
15075631Salfred	saved_uid = p->p_ucred->cr_uid;
15175631Salfred	p->p_ucred->cr_uid = 0;		/* temporarly run the vn_open as root */
15275631Salfred
15375631Salfred	fmode = FFLAGS(O_WRONLY);
15475631Salfred	error = vn_open(&nd, &fmode, 0);
15575631Salfred	p->p_ucred->cr_uid = saved_uid;
15675631Salfred	if (error != 0) {
15775631Salfred		return (error == ENOENT ? EOPNOTSUPP : error);
15875631Salfred	}
15975631Salfred	wvp = nd.ni_vp;
16075631Salfred	VOP_UNLOCK(wvp, 0, p);		/* vn_open leaves it locked */
16175631Salfred
16275631Salfred
16375631Salfred	ioflg = IO_UNIT;
16475631Salfred	for (;;) {
16575631Salfred		VOP_LEASE(wvp, p, proc0.p_ucred, LEASE_WRITE);
16675631Salfred
16775631Salfred		error = vn_rdwr(UIO_WRITE, wvp, (caddr_t)&msg, sizeof(msg), 0,
16875631Salfred		    UIO_SYSSPACE, ioflg, proc0.p_ucred, NULL, p);
16975631Salfred
17075631Salfred		if (error && (((ioflg & IO_NDELAY) == 0) || error != EAGAIN)) {
17175631Salfred			break;
17275631Salfred		}
17375631Salfred		/*
17475631Salfred		 * If we're locking a file, wait for an answer.  Unlocks succeed
17575631Salfred		 * immediately.
17675631Salfred		 */
17775631Salfred		if (fl->l_type == F_UNLCK)
17875631Salfred			/*
17975631Salfred			 * XXX this isn't exactly correct.  The client side
18075631Salfred			 * needs to continue sending it's unlock until
18175631Salfred			 * it gets a responce back.
18275631Salfred			 */
18375631Salfred			break;
18475631Salfred
18575631Salfred		/*
18675631Salfred		 * retry after 20 seconds if we haven't gotten a responce yet.
18775631Salfred		 * This number was picked out of thin air... but is longer
18875631Salfred		 * then even a reasonably loaded system should take (at least
18975631Salfred		 * on a local network).  XXX Probably should use a back-off
19075631Salfred		 * scheme.
19175631Salfred		 */
19275631Salfred		if ((error = tsleep((void *)p->p_nlminfo,
19375631Salfred					PCATCH | PUSER, "lockd", 20*hz)) != 0) {
19475631Salfred			if (error == EWOULDBLOCK) {
19575631Salfred				/*
19675631Salfred				 * We timed out, so we rewrite the request
19775631Salfred				 * to the fifo, but only if it isn't already
19875631Salfred				 * full.
19975631Salfred				 */
20075631Salfred				ioflg |= IO_NDELAY;
20175631Salfred				continue;
20275631Salfred			}
20375631Salfred
20475631Salfred			break;
20575631Salfred		}
20675631Salfred
20775631Salfred		if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
20875631Salfred			if (p->p_nlminfo->set_getlk_pid) {
20975631Salfred				fl->l_pid = p->p_nlminfo->getlk_pid;
21075631Salfred			} else {
21175631Salfred				fl->l_type = F_UNLCK;
21275631Salfred			}
21375631Salfred		}
21475631Salfred		error = p->p_nlminfo->retcode;
21575631Salfred		break;
21675631Salfred	}
21775631Salfred
21875631Salfred	if ((error1 = vn_close(wvp, FWRITE, proc0.p_ucred, p)) && error == 0)
21975631Salfred		return (error1);
22075631Salfred
22175631Salfred	return (error);
22275631Salfred}
22375631Salfred
22475631Salfred/*
22575631Salfred * nfslockdans --
22675631Salfred *      NFS advisory byte-level locks answer from the lock daemon.
22775631Salfred */
22875631Salfredint
22975631Salfrednfslockdans(p, ansp)
23075631Salfred	struct proc *p;
23175631Salfred	struct lockd_ans *ansp;
23275631Salfred{
23375631Salfred	int error;
23475631Salfred
23575631Salfred	/* Let root, or someone who once was root (lockd generally
23675631Salfred	 * switches to the daemon uid once it is done setting up) make
23775631Salfred	 * this call
23875631Salfred	 */
23975631Salfred	if ((error = suser(p)) != 0 && p->p_cred->p_svuid != 0)
24075631Salfred		return (error);
24175631Salfred
24275631Salfred	/* the version should match, or we're out of sync */
24375631Salfred	if (ansp->la_vers != LOCKD_ANS_VERSION)
24475631Salfred		return (EINVAL);
24575631Salfred
24675631Salfred	/* Find the process, set its return errno and wake it up. */
24775631Salfred	if ((p = pfind(ansp->la_msg_ident.pid)) == NULL)
24875631Salfred		return (ESRCH);
24975631Salfred
25075631Salfred	/* verify the pid hasn't been reused (if we can), and it isn't waiting
25175631Salfred	 * for an answer from a more recent request.  We return an EPIPE if
25275631Salfred	 * the match fails, because we've already used ESRCH above, and this
25375631Salfred	 * is sort of like writing on a pipe after the reader has closed it.
25475631Salfred	 */
25575631Salfred	if (p->p_nlminfo == NULL ||
25675631Salfred	    ((ansp->la_msg_ident.msg_seq != -1) &&
25775631Salfred	      (timevalcmp(&p->p_nlminfo->pid_start,
25875631Salfred			&ansp->la_msg_ident.pid_start, !=) ||
25975631Salfred	       p->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq)))
26075631Salfred		return (EPIPE);
26175631Salfred
26275631Salfred	p->p_nlminfo->retcode = ansp->la_errno;
26375631Salfred	p->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
26475631Salfred	p->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
26575631Salfred
26675631Salfred	(void)wakeup((void *)p->p_nlminfo);
26775631Salfred
26875631Salfred	return (0);
26975631Salfred}
270