nfs_lock.c revision 116185
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 116185 2003-06-11 03:32:42Z rwatson $"); 3383651Speter 3475631Salfred#include <sys/param.h> 3575631Salfred#include <sys/systm.h> 3675631Salfred#include <sys/fcntl.h> 3776166Smarkm#include <sys/kernel.h> /* for hz */ 38114216Skan#include <sys/limits.h> 3976166Smarkm#include <sys/lock.h> 4075631Salfred#include <sys/malloc.h> 4176166Smarkm#include <sys/lockf.h> /* for hz */ /* Must come after sys/malloc.h */ 4275631Salfred#include <sys/mbuf.h> 4375631Salfred#include <sys/mount.h> 4475631Salfred#include <sys/namei.h> 4575631Salfred#include <sys/proc.h> 4676166Smarkm#include <sys/resourcevar.h> 4775631Salfred#include <sys/socket.h> 4876166Smarkm#include <sys/socket.h> 4975631Salfred#include <sys/unistd.h> 5075631Salfred#include <sys/vnode.h> 5175631Salfred 5275631Salfred#include <net/if.h> 5375631Salfred 5475631Salfred#include <nfs/rpcv2.h> 5575631Salfred#include <nfs/nfsproto.h> 5683651Speter#include <nfsclient/nfs.h> 5783651Speter#include <nfsclient/nfsmount.h> 5883651Speter#include <nfsclient/nfsnode.h> 5983651Speter#include <nfsclient/nfs_lock.h> 6083651Speter#include <nfsclient/nlminfo.h> 6175631Salfred 6275631Salfred/* 6375631Salfred * XXX 6475631Salfred * We have to let the process know if the call succeeded. I'm using an extra 6583651Speter * field in the p_nlminfo field in the proc structure, as it is already for 6675631Salfred * lockd stuff. 6775631Salfred */ 6875631Salfred 6975631Salfred/* 7075631Salfred * nfs_advlock -- 7175631Salfred * NFS advisory byte-level locks. 7275631Salfred */ 7375631Salfredint 7483651Speternfs_dolock(struct vop_advlock_args *ap) 7575631Salfred{ 7675631Salfred LOCKD_MSG msg; 7775631Salfred struct nameidata nd; 7883366Sjulian struct thread *td; 7975631Salfred struct vnode *vp, *wvp; 8075631Salfred int error, error1; 8175631Salfred struct flock *fl; 8275631Salfred int fmode, ioflg; 8383366Sjulian struct proc *p; 8475631Salfred 8583366Sjulian td = curthread; 8683366Sjulian p = td->td_proc; 8783366Sjulian 8875631Salfred vp = ap->a_vp; 8975631Salfred fl = ap->a_fl; 9075631Salfred 9175631Salfred /* 9275631Salfred * the NLM protocol doesn't allow the server to return an error 9382174Sache * on ranges, so we do it. 9475631Salfred */ 9582194Sache if (fl->l_whence != SEEK_END) { 9682213Sache if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) || 9782204Sache fl->l_start < 0 || 9882204Sache (fl->l_len < 0 && 9982204Sache (fl->l_start == 0 || fl->l_start + fl->l_len < 0))) 10082194Sache return (EINVAL); 10182204Sache if (fl->l_len > 0 && 10282204Sache (fl->l_len - 1 > OFF_MAX - fl->l_start)) 10382194Sache return (EOVERFLOW); 10482194Sache } 10575631Salfred 10675631Salfred /* 10775631Salfred * Fill in the information structure. 10875631Salfred */ 10975631Salfred msg.lm_version = LOCKD_MSG_VERSION; 11075631Salfred msg.lm_msg_ident.pid = p->p_pid; 11175631Salfred /* 11275631Salfred * if there is no nfsowner table yet, allocate one. 11375631Salfred */ 11475631Salfred if (p->p_nlminfo == NULL) { 11575631Salfred MALLOC(p->p_nlminfo, struct nlminfo *, 116111119Simp sizeof(struct nlminfo), M_LOCKF, M_WAITOK | M_ZERO); 11775631Salfred p->p_nlminfo->pid_start = p->p_stats->p_start; 118114434Sdes timevaladd(&p->p_nlminfo->pid_start, &boottime); 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; 126100134Salfred bcopy(VFSTONFS(vp->v_mount)->nm_nam, &msg.lm_addr, 127100134Salfred min(sizeof msg.lm_addr, VFSTONFS(vp->v_mount)->nm_nam->sa_len)); 12875631Salfred msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH; 12975631Salfred bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len); 13075631Salfred msg.lm_nfsv3 = NFS_ISV3(vp); 131101947Salfred cru2x(td->td_ucred, &msg.lm_cred); 13275631Salfred 13375631Salfred /* 13475631Salfred * Open the lock fifo. If for any reason we don't find the fifo, it 13575631Salfred * means that the lock daemon isn't running. Translate any missing 13675631Salfred * file error message for the user, otherwise the application will 13775631Salfred * complain that the user's file is missing, which isn't the case. 13875631Salfred * Note that we use proc0's cred, so the fifo is opened as root. 13985370Srwatson * 14085370Srwatson * XXX: Note that this behavior is relative to the root directory 14185370Srwatson * of the current process, and this may result in a variety of 14285370Srwatson * {functional, security} problems in chroot() environments. 14375631Salfred */ 14483366Sjulian NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, _PATH_LCKFIFO, td); 14575631Salfred 146112685Srwatson fmode = FFLAGS(O_WRONLY | O_NONBLOCK); 14791420Sjhb error = vn_open_cred(&nd, &fmode, 0, thread0.td_ucred); 148112685Srwatson switch (error) { 149112685Srwatson case ENOENT: 150112685Srwatson case ENXIO: 151112685Srwatson /* 152112685Srwatson * Map a failure to find the fifo or no listener on the 153112685Srwatson * fifo to locking not being supported. 154112685Srwatson */ 155112685Srwatson return (EOPNOTSUPP); 156112685Srwatson case 0: 157112685Srwatson break; 158112685Srwatson default: 159112685Srwatson return (error); 16075631Salfred } 16175631Salfred wvp = nd.ni_vp; 16283366Sjulian VOP_UNLOCK(wvp, 0, td); /* vn_open leaves it locked */ 16375631Salfred 16475631Salfred 165101744Srwatson ioflg = IO_UNIT | IO_NOMACCHECK; 16675631Salfred for (;;) { 16791420Sjhb VOP_LEASE(wvp, td, thread0.td_ucred, LEASE_WRITE); 16875631Salfred 16975631Salfred error = vn_rdwr(UIO_WRITE, wvp, (caddr_t)&msg, sizeof(msg), 0, 170101941Srwatson UIO_SYSSPACE, ioflg, thread0.td_ucred, NOCRED, NULL, td); 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 19183651Speter * on a local network). XXX Probably should use a back-off 19275631Salfred * scheme. 193116185Srwatson * 194116185Srwatson * XXX: No PCATCH here since we currently have no useful 195116185Srwatson * way to signal to the userland rpc.lockd that the request 196116185Srwatson * has been aborted. Once the rpc.lockd implementation 197116185Srwatson * can handle aborts, and we report them properly, 198116185Srwatson * PCATCH can be put back. In the mean time, if we did 199116185Srwatson * permit aborting, the lock attempt would "get lost" 200116185Srwatson * and the lock would get stuck in the locked state. 20175631Salfred */ 202115415Srwatson error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz); 203107104Salfred if (error != 0) { 20475631Salfred if (error == EWOULDBLOCK) { 20575631Salfred /* 20675631Salfred * We timed out, so we rewrite the request 20775631Salfred * to the fifo, but only if it isn't already 20875631Salfred * full. 20975631Salfred */ 21075631Salfred ioflg |= IO_NDELAY; 21175631Salfred continue; 21275631Salfred } 21375631Salfred 21475631Salfred break; 21575631Salfred } 21675631Salfred 21775631Salfred if (msg.lm_getlk && p->p_nlminfo->retcode == 0) { 21875631Salfred if (p->p_nlminfo->set_getlk_pid) { 21975631Salfred fl->l_pid = p->p_nlminfo->getlk_pid; 22075631Salfred } else { 22175631Salfred fl->l_type = F_UNLCK; 22275631Salfred } 22375631Salfred } 22475631Salfred error = p->p_nlminfo->retcode; 22575631Salfred break; 22675631Salfred } 22775631Salfred 228107104Salfred error1 = vn_close(wvp, FWRITE, thread0.td_ucred, td); 229107104Salfred /* prefer any previous 'error' to our vn_close 'error1'. */ 230107104Salfred return (error != 0 ? error : error1); 23175631Salfred} 23275631Salfred 23375631Salfred/* 23475631Salfred * nfslockdans -- 23575631Salfred * NFS advisory byte-level locks answer from the lock daemon. 23675631Salfred */ 23775631Salfredint 23886363Srwatsonnfslockdans(struct thread *td, struct lockd_ans *ansp) 23975631Salfred{ 24086363Srwatson struct proc *targetp; 24175631Salfred int error; 24275631Salfred 24375631Salfred /* Let root, or someone who once was root (lockd generally 24483651Speter * switches to the daemon uid once it is done setting up) make 24577183Srwatson * this call. 24677183Srwatson * 24777183Srwatson * XXX This authorization check is probably not right. 24875631Salfred */ 24993593Sjhb if ((error = suser(td)) != 0 && 25091406Sjhb td->td_ucred->cr_svuid != 0) 25175631Salfred return (error); 25275631Salfred 25375631Salfred /* the version should match, or we're out of sync */ 25475631Salfred if (ansp->la_vers != LOCKD_ANS_VERSION) 25575631Salfred return (EINVAL); 25675631Salfred 25775631Salfred /* Find the process, set its return errno and wake it up. */ 25886363Srwatson if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL) 25975631Salfred return (ESRCH); 26075631Salfred 26183651Speter /* verify the pid hasn't been reused (if we can), and it isn't waiting 26275631Salfred * for an answer from a more recent request. We return an EPIPE if 26375631Salfred * the match fails, because we've already used ESRCH above, and this 26475631Salfred * is sort of like writing on a pipe after the reader has closed it. 26575631Salfred */ 26686363Srwatson if (targetp->p_nlminfo == NULL || 26775631Salfred ((ansp->la_msg_ident.msg_seq != -1) && 26886363Srwatson (timevalcmp(&targetp->p_nlminfo->pid_start, 26975631Salfred &ansp->la_msg_ident.pid_start, !=) || 27086363Srwatson targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) { 27186363Srwatson PROC_UNLOCK(targetp); 27275631Salfred return (EPIPE); 27377563Sjake } 27475631Salfred 27586363Srwatson targetp->p_nlminfo->retcode = ansp->la_errno; 27686363Srwatson targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid; 27786363Srwatson targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid; 27875631Salfred 279107104Salfred wakeup(targetp->p_nlminfo); 28075631Salfred 28186363Srwatson PROC_UNLOCK(targetp); 28275631Salfred return (0); 28375631Salfred} 284