diff --git a/sys/nfsclient/nfs_lock.c b/sys/nfsclient/nfs_lock.c index dee4b6830e63..a239c1c56f7b 100644 --- a/sys/nfsclient/nfs_lock.c +++ b/sys/nfsclient/nfs_lock.c @@ -1,276 +1,278 @@ /*- * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Berkeley Software Design Inc's name may not be used to endorse or * promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp */ #include __FBSDID("$FreeBSD$"); #include #include #include #include /* for hz */ #include #include #include /* for hz */ /* Must come after sys/malloc.h */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define NFSOWNER_1ST_LEVEL_START 1 /* initial entries */ #define NFSOWNER_2ND_LEVEL 256 /* some power of 2 */ #define NFSOWNER(tbl, i) \ (tbl)[(i) / NFSOWNER_2ND_LEVEL][(i) % NFSOWNER_2ND_LEVEL] /* * XXX * We have to let the process know if the call succeeded. I'm using an extra * field in the p_nlminfo field in the proc structure, as it is already for * lockd stuff. */ /* * nfs_advlock -- * NFS advisory byte-level locks. */ int nfs_dolock(struct vop_advlock_args *ap) { LOCKD_MSG msg; struct nameidata nd; struct thread *td; struct vnode *vp, *wvp; int error, error1; struct flock *fl; int fmode, ioflg; struct proc *p; td = curthread; p = td->td_proc; vp = ap->a_vp; fl = ap->a_fl; /* * the NLM protocol doesn't allow the server to return an error * on ranges, so we do it. */ if (fl->l_whence != SEEK_END) { if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) || fl->l_start < 0 || (fl->l_len < 0 && (fl->l_start == 0 || fl->l_start + fl->l_len < 0))) return (EINVAL); if (fl->l_len > 0 && (fl->l_len - 1 > OFF_MAX - fl->l_start)) return (EOVERFLOW); } /* * Fill in the information structure. */ msg.lm_version = LOCKD_MSG_VERSION; msg.lm_msg_ident.pid = p->p_pid; /* * if there is no nfsowner table yet, allocate one. */ if (p->p_nlminfo == NULL) { MALLOC(p->p_nlminfo, struct nlminfo *, sizeof(struct nlminfo), M_LOCKF, M_WAITOK | M_ZERO); p->p_nlminfo->pid_start = p->p_stats->p_start; } msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start; msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq); msg.lm_fl = *fl; msg.lm_wait = ap->a_flags & F_WAIT; msg.lm_getlk = ap->a_op == F_GETLK; /* * XXX -- I think this is wrong for anything other AF_INET. * * XXX: the lm_cred assignment below directly exports a ucred * structure to userland. This is probably wrong, and should at * least be xucred. */ msg.lm_addr = *(VFSTONFS(vp->v_mount)->nm_nam); msg.lm_fh_len = NFS_ISV3(vp) ? VTONFS(vp)->n_fhsize : NFSX_V2FH; bcopy(VTONFS(vp)->n_fhp, msg.lm_fh, msg.lm_fh_len); msg.lm_nfsv3 = NFS_ISV3(vp); msg.lm_cred = *(p->p_ucred); /* * Open the lock fifo. If for any reason we don't find the fifo, it * means that the lock daemon isn't running. Translate any missing * file error message for the user, otherwise the application will * complain that the user's file is missing, which isn't the case. * Note that we use proc0's cred, so the fifo is opened as root. * * XXX: Note that this behavior is relative to the root directory * of the current process, and this may result in a variety of * {functional, security} problems in chroot() environments. */ NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, _PATH_LCKFIFO, td); fmode = FFLAGS(O_WRONLY); error = vn_open_cred(&nd, &fmode, 0, proc0.p_ucred); if (error != 0) { return (error == ENOENT ? EOPNOTSUPP : error); } wvp = nd.ni_vp; VOP_UNLOCK(wvp, 0, td); /* vn_open leaves it locked */ ioflg = IO_UNIT; for (;;) { VOP_LEASE(wvp, td, proc0.p_ucred, LEASE_WRITE); error = vn_rdwr(UIO_WRITE, wvp, (caddr_t)&msg, sizeof(msg), 0, UIO_SYSSPACE, ioflg, proc0.p_ucred, NULL, td); if (error && (((ioflg & IO_NDELAY) == 0) || error != EAGAIN)) { break; } /* * If we're locking a file, wait for an answer. Unlocks succeed * immediately. */ if (fl->l_type == F_UNLCK) /* * XXX this isn't exactly correct. The client side * needs to continue sending it's unlock until * it gets a responce back. */ break; /* * retry after 20 seconds if we haven't gotten a responce yet. * This number was picked out of thin air... but is longer * then even a reasonably loaded system should take (at least * on a local network). XXX Probably should use a back-off * scheme. */ if ((error = tsleep((void *)p->p_nlminfo, PCATCH | PUSER, "lockd", 20*hz)) != 0) { if (error == EWOULDBLOCK) { /* * We timed out, so we rewrite the request * to the fifo, but only if it isn't already * full. */ ioflg |= IO_NDELAY; continue; } break; } if (msg.lm_getlk && p->p_nlminfo->retcode == 0) { if (p->p_nlminfo->set_getlk_pid) { fl->l_pid = p->p_nlminfo->getlk_pid; } else { fl->l_type = F_UNLCK; } } error = p->p_nlminfo->retcode; break; } if ((error1 = vn_close(wvp, FWRITE, proc0.p_ucred, td)) && error == 0) return (error1); return (error); } /* * nfslockdans -- * NFS advisory byte-level locks answer from the lock daemon. */ int -nfslockdans(struct proc *p, struct lockd_ans *ansp) +nfslockdans(struct thread *td, struct lockd_ans *ansp) { + struct proc *targetp; int error; /* Let root, or someone who once was root (lockd generally * switches to the daemon uid once it is done setting up) make * this call. * * XXX This authorization check is probably not right. */ - if ((error = suser(p)) != 0 && p->p_ucred->cr_svuid != 0) + if ((error = suser(td->td_proc)) != 0 && + td->td_proc->p_ucred->cr_svuid != 0) return (error); /* the version should match, or we're out of sync */ if (ansp->la_vers != LOCKD_ANS_VERSION) return (EINVAL); /* Find the process, set its return errno and wake it up. */ - if ((p = pfind(ansp->la_msg_ident.pid)) == NULL) + if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL) return (ESRCH); /* verify the pid hasn't been reused (if we can), and it isn't waiting * for an answer from a more recent request. We return an EPIPE if * the match fails, because we've already used ESRCH above, and this * is sort of like writing on a pipe after the reader has closed it. */ - if (p->p_nlminfo == NULL || + if (targetp->p_nlminfo == NULL || ((ansp->la_msg_ident.msg_seq != -1) && - (timevalcmp(&p->p_nlminfo->pid_start, + (timevalcmp(&targetp->p_nlminfo->pid_start, &ansp->la_msg_ident.pid_start, !=) || - p->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) { - PROC_UNLOCK(p); + targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) { + PROC_UNLOCK(targetp); return (EPIPE); } - p->p_nlminfo->retcode = ansp->la_errno; - p->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid; - p->p_nlminfo->getlk_pid = ansp->la_getlk_pid; + targetp->p_nlminfo->retcode = ansp->la_errno; + targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid; + targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid; - (void)wakeup((void *)p->p_nlminfo); + (void)wakeup((void *)targetp->p_nlminfo); - PROC_UNLOCK(p); + PROC_UNLOCK(targetp); return (0); } diff --git a/sys/nfsclient/nfs_lock.h b/sys/nfsclient/nfs_lock.h index 0d081c46f4f0..9de041d05ef6 100644 --- a/sys/nfsclient/nfs_lock.h +++ b/sys/nfsclient/nfs_lock.h @@ -1,92 +1,92 @@ /*- * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Berkeley Software Design Inc's name may not be used to endorse or * promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * from nfs_lock.h,v 2.2 1998/04/28 19:38:41 don Exp * $FreeBSD$ */ /* * lockd uses the nfssvc system call to get the unique kernel services it needs. * It passes in a request structure with a version number at the start. * This prevents libc from needing to change if the information passed * between lockd and the kernel needs to change. * * If a structure changes, you must bump the version number. */ #include /* * The fifo where the kernel writes requests for locks on remote NFS files, * and where lockd reads these requests. * */ #define _PATH_LCKFIFO "/var/run/lock" /* * This structure is used to uniquely identify the process which originated * a particular message to lockd. A sequence number is used to differentiate * multiple messages from the same process. A process start time is used to * detect the unlikely, but possible, event of the recycling of a pid. */ struct lockd_msg_ident { pid_t pid; /* The process ID. */ struct timeval pid_start; /* Start time of process id */ int msg_seq; /* Sequence number of message */ }; #define LOCKD_MSG_VERSION 1 /* * The structure that the kernel hands us for each lock request. */ typedef struct __lock_msg { int lm_version; /* which version is this */ struct lockd_msg_ident lm_msg_ident; /* originator of the message */ struct flock lm_fl; /* The lock request. */ int lm_wait; /* The F_WAIT flag. */ int lm_getlk; /* is this a F_GETLK request */ struct sockaddr lm_addr; /* The address. */ int lm_nfsv3; /* If NFS version 3. */ size_t lm_fh_len; /* The file handle length. */ struct ucred lm_cred; /* user cred for lock req */ u_int8_t lm_fh[NFS_SMALLFH];/* The file handle. */ } LOCKD_MSG; #define LOCKD_ANS_VERSION 1 struct lockd_ans { int la_vers; struct lockd_msg_ident la_msg_ident; /* originator of the message */ int la_errno; int la_set_getlk_pid; /* use returned pid */ int la_getlk_pid; /* returned pid for F_GETLK */ }; #ifdef _KERNEL int nfs_dolock(struct vop_advlock_args *ap); -int nfslockdans(struct proc *p, struct lockd_ans *ansp); +int nfslockdans(struct thread *td, struct lockd_ans *ansp); #endif diff --git a/sys/nfsclient/nfs_nfsiod.c b/sys/nfsclient/nfs_nfsiod.c index 4e0ac10a0a87..6fe1a5596379 100644 --- a/sys/nfsclient/nfs_nfsiod.c +++ b/sys/nfsclient/nfs_nfsiod.c @@ -1,197 +1,197 @@ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Rick Macklem at The University of Guelph. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)nfs_syscalls.c 8.5 (Berkeley) 3/30/95 */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure"); static void nfssvc_iod(void *); #define TRUE 1 #define FALSE 0 static int nfs_asyncdaemon[NFS_MAXASYNCDAEMON]; SYSCTL_DECL(_vfs_nfs); static void nfsiod_setup(void *dummy) { int i; int error; struct proc *p; for (i = 0; i < 4; i++) { error = kthread_create(nfssvc_iod, NULL, &p, RFHIGHPID, "nfsiod %d", i); if (error) panic("nfsiod_setup: kthread_create error %d", error); } } SYSINIT(nfsiod, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, nfsiod_setup, NULL); static int nfs_defect = 0; SYSCTL_INT(_vfs_nfs, OID_AUTO, defect, CTLFLAG_RW, &nfs_defect, 0, ""); int nfsclnt(struct thread *td, struct nfsclnt_args *uap) { struct lockd_ans la; int error; if ((uap->flag & NFSCLNT_LOCKDANS) != 0) { error = copyin(uap->argp, &la, sizeof(la)); - return (error != 0 ? error : nfslockdans(td->td_proc, &la)); + return (error != 0 ? error : nfslockdans(td, &la)); } return EINVAL; } /* * Asynchronous I/O daemons for client nfs. * They do read-ahead and write-behind operations on the block I/O cache. * Never returns unless it fails or gets killed. */ static void nfssvc_iod(void *dummy) { struct buf *bp; int i, myiod; struct nfsmount *nmp; int error = 0; mtx_lock(&Giant); /* * Assign my position or return error if too many already running */ myiod = -1; for (i = 0; i < NFS_MAXASYNCDAEMON; i++) if (nfs_asyncdaemon[i] == 0) { nfs_asyncdaemon[i]++; myiod = i; break; } if (myiod == -1) return /* XXX (EBUSY) */; nfs_numasync++; /* * Just loop around doin our stuff until SIGKILL */ for (;;) { while (((nmp = nfs_iodmount[myiod]) == NULL || !TAILQ_FIRST(&nmp->nm_bufq)) && error == 0) { if (nmp) nmp->nm_bufqiods--; nfs_iodwant[myiod] = curthread->td_proc; nfs_iodmount[myiod] = NULL; error = tsleep((caddr_t)&nfs_iodwant[myiod], PWAIT | PCATCH, "nfsidl", 0); } if (error) { nfs_asyncdaemon[myiod] = 0; if (nmp) nmp->nm_bufqiods--; nfs_iodwant[myiod] = NULL; nfs_iodmount[myiod] = NULL; nfs_numasync--; return /* XXX (error) */; } while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) { /* Take one off the front of the list */ TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist); nmp->nm_bufqlen--; if (nmp->nm_bufqwant && nmp->nm_bufqlen <= nfs_numasync) { nmp->nm_bufqwant = FALSE; wakeup(&nmp->nm_bufq); } if (bp->b_iocmd == BIO_READ) (void) nfs_doio(bp, bp->b_rcred, (struct thread *)0); else (void) nfs_doio(bp, bp->b_wcred, (struct thread *)0); /* * If there are more than one iod on this mount, then defect * so that the iods can be shared out fairly between the mounts */ if (nfs_defect && nmp->nm_bufqiods > 1) { NFS_DPF(ASYNCIO, ("nfssvc_iod: iod %d defecting from mount %p\n", myiod, nmp)); nfs_iodmount[myiod] = NULL; nmp->nm_bufqiods--; break; } } } }