nfs_lock.c revision 83651
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 */ 3075631Salfred 3183651Speter#include <sys/cdefs.h> 3283651Speter__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_lock.c 83651 2001-09-18 23:32:09Z peter $"); 3383651Speter 3475631Salfred#include <sys/param.h> 3575631Salfred#include <sys/systm.h> 3675631Salfred#include <sys/fcntl.h> 3776166Smarkm#include <sys/kernel.h> /* for hz */ 3876166Smarkm#include <sys/lock.h> 3975631Salfred#include <sys/malloc.h> 4076166Smarkm#include <sys/lockf.h> /* for hz */ /* Must come after sys/malloc.h */ 4175631Salfred#include <sys/mbuf.h> 4275631Salfred#include <sys/mount.h> 4375631Salfred#include <sys/namei.h> 4475631Salfred#include <sys/proc.h> 4576166Smarkm#include <sys/resourcevar.h> 4675631Salfred#include <sys/socket.h> 4776166Smarkm#include <sys/socket.h> 4875631Salfred#include <sys/unistd.h> 4975631Salfred#include <sys/vnode.h> 5075631Salfred 5182190Sache#include <machine/limits.h> 5282190Sache 5375631Salfred#include <net/if.h> 5475631Salfred 5575631Salfred#include <nfs/rpcv2.h> 5675631Salfred#include <nfs/nfsproto.h> 5783651Speter#include <nfsclient/nfs.h> 5883651Speter#include <nfsclient/nfsmount.h> 5983651Speter#include <nfsclient/nfsnode.h> 6083651Speter#include <nfsclient/nfs_lock.h> 6183651Speter#include <nfsclient/nlminfo.h> 6275631Salfred 6375631Salfred#define NFSOWNER_1ST_LEVEL_START 1 /* initial entries */ 6475631Salfred#define NFSOWNER_2ND_LEVEL 256 /* some power of 2 */ 6575631Salfred 6675631Salfred#define NFSOWNER(tbl, i) \ 6775631Salfred (tbl)[(i) / NFSOWNER_2ND_LEVEL][(i) % NFSOWNER_2ND_LEVEL] 6875631Salfred 6975631Salfred/* 7075631Salfred * XXX 7175631Salfred * We have to let the process know if the call succeeded. I'm using an extra 7283651Speter * field in the p_nlminfo field in the proc structure, as it is already for 7375631Salfred * lockd stuff. 7475631Salfred */ 7575631Salfred 7675631Salfred/* 7775631Salfred * nfs_advlock -- 7875631Salfred * NFS advisory byte-level locks. 7975631Salfred */ 8075631Salfredint 8183651Speternfs_dolock(struct vop_advlock_args *ap) 8275631Salfred{ 8375631Salfred LOCKD_MSG msg; 8475631Salfred struct nameidata nd; 8583366Sjulian struct thread *td; 8675631Salfred uid_t saved_uid; 8775631Salfred struct vnode *vp, *wvp; 8875631Salfred int error, error1; 8975631Salfred struct flock *fl; 9075631Salfred int fmode, ioflg; 9183366Sjulian struct proc *p; 9275631Salfred 9383366Sjulian td = curthread; 9483366Sjulian p = td->td_proc; 9583366Sjulian 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 10182174Sache * on ranges, so we do it. 10275631Salfred */ 10382194Sache if (fl->l_whence != SEEK_END) { 10482213Sache if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) || 10582204Sache fl->l_start < 0 || 10682204Sache (fl->l_len < 0 && 10782204Sache (fl->l_start == 0 || fl->l_start + fl->l_len < 0))) 10882194Sache return (EINVAL); 10982204Sache if (fl->l_len > 0 && 11082204Sache (fl->l_len - 1 > OFF_MAX - fl->l_start)) 11182194Sache return (EOVERFLOW); 11282194Sache } 11375631Salfred 11475631Salfred /* 11575631Salfred * Fill in the information structure. 11675631Salfred */ 11775631Salfred msg.lm_version = LOCKD_MSG_VERSION; 11875631Salfred msg.lm_msg_ident.pid = p->p_pid; 11975631Salfred /* 12075631Salfred * if there is no nfsowner table yet, allocate one. 12175631Salfred */ 12275631Salfred if (p->p_nlminfo == NULL) { 12375631Salfred MALLOC(p->p_nlminfo, struct nlminfo *, 12475631Salfred sizeof(struct nlminfo), M_LOCKF, M_WAITOK | M_ZERO); 12575631Salfred p->p_nlminfo->pid_start = p->p_stats->p_start; 12675631Salfred } 12775631Salfred msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start; 12875631Salfred msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq); 12975631Salfred 13075631Salfred msg.lm_fl = *fl; 13175631Salfred msg.lm_wait = ap->a_flags & F_WAIT; 13275631Salfred msg.lm_getlk = ap->a_op == F_GETLK; 13375631Salfred /* 13475631Salfred * XXX -- I think this is wrong for anything other AF_INET. 13575631Salfred */ 13675631Salfred msg.lm_addr = *(VFSTONFS(vp->v_mount)->nm_nam); 13775631Salfred msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH; 13875631Salfred bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len); 13975631Salfred msg.lm_nfsv3 = NFS_ISV3(vp); 14075631Salfred msg.lm_cred = *(p->p_ucred); 14175631Salfred 14275631Salfred /* 14375631Salfred * Open the lock fifo. If for any reason we don't find the fifo, it 14475631Salfred * means that the lock daemon isn't running. Translate any missing 14575631Salfred * file error message for the user, otherwise the application will 14675631Salfred * complain that the user's file is missing, which isn't the case. 14775631Salfred * Note that we use proc0's cred, so the fifo is opened as root. 14875631Salfred */ 14983366Sjulian NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, _PATH_LCKFIFO, td); 15075631Salfred 15175631Salfred /* 15275631Salfred * XXX Hack to temporarily allow this process (regardless of it's creds) 15375631Salfred * to open the fifo we need to write to. vn_open() really should 15475631Salfred * take a ucred (and once it does, this code should be fixed to use 15575631Salfred * proc0's ucred. 15675631Salfred */ 15775631Salfred saved_uid = p->p_ucred->cr_uid; 15875631Salfred p->p_ucred->cr_uid = 0; /* temporarly run the vn_open as root */ 15975631Salfred 16075631Salfred fmode = FFLAGS(O_WRONLY); 16175631Salfred error = vn_open(&nd, &fmode, 0); 16275631Salfred p->p_ucred->cr_uid = saved_uid; 16375631Salfred if (error != 0) { 16475631Salfred return (error == ENOENT ? EOPNOTSUPP : error); 16575631Salfred } 16675631Salfred wvp = nd.ni_vp; 16783366Sjulian VOP_UNLOCK(wvp, 0, td); /* vn_open leaves it locked */ 16875631Salfred 16975631Salfred 17075631Salfred ioflg = IO_UNIT; 17175631Salfred for (;;) { 17283366Sjulian VOP_LEASE(wvp, td, proc0.p_ucred, LEASE_WRITE); 17375631Salfred 17475631Salfred error = vn_rdwr(UIO_WRITE, wvp, (caddr_t)&msg, sizeof(msg), 0, 17583366Sjulian UIO_SYSSPACE, ioflg, proc0.p_ucred, NULL, td); 17675631Salfred 17775631Salfred if (error && (((ioflg & IO_NDELAY) == 0) || error != EAGAIN)) { 17875631Salfred break; 17975631Salfred } 18075631Salfred /* 18175631Salfred * If we're locking a file, wait for an answer. Unlocks succeed 18275631Salfred * immediately. 18375631Salfred */ 18475631Salfred if (fl->l_type == F_UNLCK) 18575631Salfred /* 18675631Salfred * XXX this isn't exactly correct. The client side 18775631Salfred * needs to continue sending it's unlock until 18875631Salfred * it gets a responce back. 18975631Salfred */ 19075631Salfred break; 19175631Salfred 19275631Salfred /* 19375631Salfred * retry after 20 seconds if we haven't gotten a responce yet. 19475631Salfred * This number was picked out of thin air... but is longer 19575631Salfred * then even a reasonably loaded system should take (at least 19683651Speter * on a local network). XXX Probably should use a back-off 19775631Salfred * scheme. 19875631Salfred */ 19983651Speter if ((error = tsleep((void *)p->p_nlminfo, 20075631Salfred PCATCH | PUSER, "lockd", 20*hz)) != 0) { 20175631Salfred if (error == EWOULDBLOCK) { 20275631Salfred /* 20375631Salfred * We timed out, so we rewrite the request 20475631Salfred * to the fifo, but only if it isn't already 20575631Salfred * full. 20675631Salfred */ 20775631Salfred ioflg |= IO_NDELAY; 20875631Salfred continue; 20975631Salfred } 21075631Salfred 21175631Salfred break; 21275631Salfred } 21375631Salfred 21475631Salfred if (msg.lm_getlk && p->p_nlminfo->retcode == 0) { 21575631Salfred if (p->p_nlminfo->set_getlk_pid) { 21675631Salfred fl->l_pid = p->p_nlminfo->getlk_pid; 21775631Salfred } else { 21875631Salfred fl->l_type = F_UNLCK; 21975631Salfred } 22075631Salfred } 22175631Salfred error = p->p_nlminfo->retcode; 22275631Salfred break; 22375631Salfred } 22475631Salfred 22583366Sjulian if ((error1 = vn_close(wvp, FWRITE, proc0.p_ucred, td)) && error == 0) 22675631Salfred return (error1); 22775631Salfred 22875631Salfred return (error); 22975631Salfred} 23075631Salfred 23175631Salfred/* 23275631Salfred * nfslockdans -- 23375631Salfred * NFS advisory byte-level locks answer from the lock daemon. 23475631Salfred */ 23575631Salfredint 23683651Speternfslockdans(struct proc *p, struct lockd_ans *ansp) 23775631Salfred{ 23875631Salfred int error; 23975631Salfred 24075631Salfred /* Let root, or someone who once was root (lockd generally 24183651Speter * switches to the daemon uid once it is done setting up) make 24277183Srwatson * this call. 24377183Srwatson * 24477183Srwatson * XXX This authorization check is probably not right. 24575631Salfred */ 24677183Srwatson if ((error = suser(p)) != 0 && p->p_ucred->cr_svuid != 0) 24775631Salfred return (error); 24875631Salfred 24975631Salfred /* the version should match, or we're out of sync */ 25075631Salfred if (ansp->la_vers != LOCKD_ANS_VERSION) 25175631Salfred return (EINVAL); 25275631Salfred 25375631Salfred /* Find the process, set its return errno and wake it up. */ 25475631Salfred if ((p = pfind(ansp->la_msg_ident.pid)) == NULL) 25575631Salfred return (ESRCH); 25675631Salfred 25783651Speter /* verify the pid hasn't been reused (if we can), and it isn't waiting 25875631Salfred * for an answer from a more recent request. We return an EPIPE if 25975631Salfred * the match fails, because we've already used ESRCH above, and this 26075631Salfred * is sort of like writing on a pipe after the reader has closed it. 26175631Salfred */ 26275631Salfred if (p->p_nlminfo == NULL || 26375631Salfred ((ansp->la_msg_ident.msg_seq != -1) && 26475631Salfred (timevalcmp(&p->p_nlminfo->pid_start, 26575631Salfred &ansp->la_msg_ident.pid_start, !=) || 26677563Sjake p->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) { 26777563Sjake PROC_UNLOCK(p); 26875631Salfred return (EPIPE); 26977563Sjake } 27075631Salfred 27175631Salfred p->p_nlminfo->retcode = ansp->la_errno; 27275631Salfred p->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid; 27375631Salfred p->p_nlminfo->getlk_pid = ansp->la_getlk_pid; 27475631Salfred 27575631Salfred (void)wakeup((void *)p->p_nlminfo); 27675631Salfred 27777563Sjake PROC_UNLOCK(p); 27875631Salfred return (0); 27975631Salfred} 280