nfs_lock.c revision 77563
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 77563 2001-06-01 01:30:51Z jake $
3075631Salfred */
3175631Salfred
3275631Salfred#include <sys/param.h>
3375631Salfred#include <sys/systm.h>
3475631Salfred#include <sys/fcntl.h>
3576166Smarkm#include <sys/kernel.h>		/* for hz */
3676166Smarkm#include <sys/lock.h>
3775631Salfred#include <sys/malloc.h>
3876166Smarkm#include <sys/lockf.h>		/* for hz */ /* Must come after sys/malloc.h */
3975631Salfred#include <sys/mbuf.h>
4075631Salfred#include <sys/mount.h>
4175631Salfred#include <sys/namei.h>
4275631Salfred#include <sys/proc.h>
4376166Smarkm#include <sys/resourcevar.h>
4475631Salfred#include <sys/socket.h>
4576166Smarkm#include <sys/socket.h>
4675631Salfred#include <sys/unistd.h>
4775631Salfred#include <sys/vnode.h>
4875631Salfred
4975631Salfred#include <net/if.h>
5075631Salfred
5175631Salfred#include <nfs/rpcv2.h>
5275631Salfred#include <nfs/nfsproto.h>
5375631Salfred#include <nfs/nfs.h>
5475631Salfred#include <nfs/nfsmount.h>
5575631Salfred#include <nfs/nfsnode.h>
5675631Salfred#include <nfs/nfs_lock.h>
5775631Salfred#include <nfs/nlminfo.h>
5875631Salfred
5975631Salfred#define NFSOWNER_1ST_LEVEL_START	1	       /* initial entries */
6075631Salfred#define NFSOWNER_2ND_LEVEL	      256		/* some power of 2 */
6175631Salfred
6275631Salfred#define NFSOWNER(tbl, i)	\
6375631Salfred		(tbl)[(i) / NFSOWNER_2ND_LEVEL][(i) % NFSOWNER_2ND_LEVEL]
6475631Salfred
6575631Salfred/*
6675631Salfred * XXX
6775631Salfred * We have to let the process know if the call succeeded.  I'm using an extra
6875631Salfred * field in the p_nlminfo field in the proc structure, as it is already for
6975631Salfred * lockd stuff.
7075631Salfred */
7175631Salfred
7275631Salfred/*
7375631Salfred * nfs_advlock --
7475631Salfred *      NFS advisory byte-level locks.
7575631Salfred */
7675631Salfredint
7775631Salfrednfs_dolock(ap)
7875631Salfred	struct vop_advlock_args /* {
7975631Salfred		struct vnode *a_vp;
8075631Salfred		caddr_t a_id;
8175631Salfred		int  a_op;
8275631Salfred		struct flock *a_fl;
8375631Salfred		int a_flags;
8475631Salfred	} */ *ap;
8575631Salfred{
8675631Salfred	LOCKD_MSG msg;
8775631Salfred	struct nameidata nd;
8875631Salfred	struct proc *p;
8975631Salfred	uid_t	saved_uid;
9075631Salfred	struct vnode *vp, *wvp;
9175631Salfred	int error, error1;
9275631Salfred	struct flock *fl;
9375631Salfred	int fmode, ioflg;
9475631Salfred
9575631Salfred	p = curproc;
9675631Salfred	vp = ap->a_vp;
9775631Salfred	fl = ap->a_fl;
9875631Salfred
9975631Salfred	/*
10075631Salfred	 * the NLM protocol doesn't allow the server to return an error
10175631Salfred	 * on ranges, so we do it.  Note that we should be returning
10275631Salfred	 * EOVERFLOW in some cases, but we don't have it.
10375631Salfred	 */
10475631Salfred	if (fl->l_start < 0 || fl->l_len < 0 ||
10575631Salfred	    ((fl->l_len != 0 &&
10675631Salfred	     (fl->l_start + fl->l_len - 1) < 0)))
10775631Salfred		return (EINVAL);
10875631Salfred
10975631Salfred	/*
11075631Salfred	 * Fill in the information structure.
11175631Salfred	 */
11275631Salfred	msg.lm_version = LOCKD_MSG_VERSION;
11375631Salfred	msg.lm_msg_ident.pid = p->p_pid;
11475631Salfred	/*
11575631Salfred	 * if there is no nfsowner table yet, allocate one.
11675631Salfred	 */
11775631Salfred	if (p->p_nlminfo == NULL) {
11875631Salfred		MALLOC(p->p_nlminfo, struct nlminfo *,
11975631Salfred			sizeof(struct nlminfo), M_LOCKF, M_WAITOK | M_ZERO);
12075631Salfred		p->p_nlminfo->pid_start = p->p_stats->p_start;
12175631Salfred	}
12275631Salfred	msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
12375631Salfred	msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
12475631Salfred
12575631Salfred	msg.lm_fl = *fl;
12675631Salfred	msg.lm_wait = ap->a_flags & F_WAIT;
12775631Salfred	msg.lm_getlk = ap->a_op == F_GETLK;
12875631Salfred	/*
12975631Salfred	 * XXX  -- I think this is wrong for anything other AF_INET.
13075631Salfred	 */
13175631Salfred	msg.lm_addr = *(VFSTONFS(vp->v_mount)->nm_nam);
13275631Salfred	msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH;
13375631Salfred	bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len);
13475631Salfred	msg.lm_nfsv3 = NFS_ISV3(vp);
13575631Salfred	msg.lm_cred = *(p->p_ucred);
13675631Salfred
13775631Salfred	/*
13875631Salfred	 * Open the lock fifo.  If for any reason we don't find the fifo, it
13975631Salfred	 * means that the lock daemon isn't running.  Translate any missing
14075631Salfred	 * file error message for the user, otherwise the application will
14175631Salfred	 * complain that the user's file is missing, which isn't the case.
14275631Salfred	 * Note that we use proc0's cred, so the fifo is opened as root.
14375631Salfred	 */
14475631Salfred	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, _PATH_LCKFIFO, p);
14575631Salfred
14675631Salfred	/*
14775631Salfred	 * XXX Hack to temporarily allow this process (regardless of it's creds)
14875631Salfred	 * to open the fifo we need to write to. vn_open() really should
14975631Salfred	 * take a ucred (and once it does, this code should be fixed to use
15075631Salfred	 * proc0's ucred.
15175631Salfred	 */
15275631Salfred	saved_uid = p->p_ucred->cr_uid;
15375631Salfred	p->p_ucred->cr_uid = 0;		/* temporarly run the vn_open as root */
15475631Salfred
15575631Salfred	fmode = FFLAGS(O_WRONLY);
15675631Salfred	error = vn_open(&nd, &fmode, 0);
15775631Salfred	p->p_ucred->cr_uid = saved_uid;
15875631Salfred	if (error != 0) {
15975631Salfred		return (error == ENOENT ? EOPNOTSUPP : error);
16075631Salfred	}
16175631Salfred	wvp = nd.ni_vp;
16275631Salfred	VOP_UNLOCK(wvp, 0, p);		/* vn_open leaves it locked */
16375631Salfred
16475631Salfred
16575631Salfred	ioflg = IO_UNIT;
16675631Salfred	for (;;) {
16775631Salfred		VOP_LEASE(wvp, p, proc0.p_ucred, LEASE_WRITE);
16875631Salfred
16975631Salfred		error = vn_rdwr(UIO_WRITE, wvp, (caddr_t)&msg, sizeof(msg), 0,
17075631Salfred		    UIO_SYSSPACE, ioflg, proc0.p_ucred, NULL, p);
17175631Salfred
17275631Salfred		if (error && (((ioflg & IO_NDELAY) == 0) || error != EAGAIN)) {
17375631Salfred			break;
17475631Salfred		}
17575631Salfred		/*
17675631Salfred		 * If we're locking a file, wait for an answer.  Unlocks succeed
17775631Salfred		 * immediately.
17875631Salfred		 */
17975631Salfred		if (fl->l_type == F_UNLCK)
18075631Salfred			/*
18175631Salfred			 * XXX this isn't exactly correct.  The client side
18275631Salfred			 * needs to continue sending it's unlock until
18375631Salfred			 * it gets a responce back.
18475631Salfred			 */
18575631Salfred			break;
18675631Salfred
18775631Salfred		/*
18875631Salfred		 * retry after 20 seconds if we haven't gotten a responce yet.
18975631Salfred		 * This number was picked out of thin air... but is longer
19075631Salfred		 * then even a reasonably loaded system should take (at least
19175631Salfred		 * on a local network).  XXX Probably should use a back-off
19275631Salfred		 * scheme.
19375631Salfred		 */
19475631Salfred		if ((error = tsleep((void *)p->p_nlminfo,
19575631Salfred					PCATCH | PUSER, "lockd", 20*hz)) != 0) {
19675631Salfred			if (error == EWOULDBLOCK) {
19775631Salfred				/*
19875631Salfred				 * We timed out, so we rewrite the request
19975631Salfred				 * to the fifo, but only if it isn't already
20075631Salfred				 * full.
20175631Salfred				 */
20275631Salfred				ioflg |= IO_NDELAY;
20375631Salfred				continue;
20475631Salfred			}
20575631Salfred
20675631Salfred			break;
20775631Salfred		}
20875631Salfred
20975631Salfred		if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
21075631Salfred			if (p->p_nlminfo->set_getlk_pid) {
21175631Salfred				fl->l_pid = p->p_nlminfo->getlk_pid;
21275631Salfred			} else {
21375631Salfred				fl->l_type = F_UNLCK;
21475631Salfred			}
21575631Salfred		}
21675631Salfred		error = p->p_nlminfo->retcode;
21775631Salfred		break;
21875631Salfred	}
21975631Salfred
22075631Salfred	if ((error1 = vn_close(wvp, FWRITE, proc0.p_ucred, p)) && error == 0)
22175631Salfred		return (error1);
22275631Salfred
22375631Salfred	return (error);
22475631Salfred}
22575631Salfred
22675631Salfred/*
22775631Salfred * nfslockdans --
22875631Salfred *      NFS advisory byte-level locks answer from the lock daemon.
22975631Salfred */
23075631Salfredint
23175631Salfrednfslockdans(p, ansp)
23275631Salfred	struct proc *p;
23375631Salfred	struct lockd_ans *ansp;
23475631Salfred{
23575631Salfred	int error;
23675631Salfred
23775631Salfred	/* Let root, or someone who once was root (lockd generally
23875631Salfred	 * switches to the daemon uid once it is done setting up) make
23977183Srwatson	 * this call.
24077183Srwatson	 *
24177183Srwatson	 * XXX This authorization check is probably not right.
24275631Salfred	 */
24377183Srwatson	if ((error = suser(p)) != 0 && p->p_ucred->cr_svuid != 0)
24475631Salfred		return (error);
24575631Salfred
24675631Salfred	/* the version should match, or we're out of sync */
24775631Salfred	if (ansp->la_vers != LOCKD_ANS_VERSION)
24875631Salfred		return (EINVAL);
24975631Salfred
25075631Salfred	/* Find the process, set its return errno and wake it up. */
25175631Salfred	if ((p = pfind(ansp->la_msg_ident.pid)) == NULL)
25275631Salfred		return (ESRCH);
25375631Salfred
25475631Salfred	/* verify the pid hasn't been reused (if we can), and it isn't waiting
25575631Salfred	 * for an answer from a more recent request.  We return an EPIPE if
25675631Salfred	 * the match fails, because we've already used ESRCH above, and this
25775631Salfred	 * is sort of like writing on a pipe after the reader has closed it.
25875631Salfred	 */
25975631Salfred	if (p->p_nlminfo == NULL ||
26075631Salfred	    ((ansp->la_msg_ident.msg_seq != -1) &&
26175631Salfred	      (timevalcmp(&p->p_nlminfo->pid_start,
26275631Salfred			&ansp->la_msg_ident.pid_start, !=) ||
26377563Sjake	       p->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
26477563Sjake		PROC_UNLOCK(p);
26575631Salfred		return (EPIPE);
26677563Sjake	}
26775631Salfred
26875631Salfred	p->p_nlminfo->retcode = ansp->la_errno;
26975631Salfred	p->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
27075631Salfred	p->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
27175631Salfred
27275631Salfred	(void)wakeup((void *)p->p_nlminfo);
27375631Salfred
27477563Sjake	PROC_UNLOCK(p);
27575631Salfred	return (0);
27675631Salfred}
277