Home | History | Annotate | Line # | Download | only in common
nfs_lock.c revision 1.1.1.2
      1      1.1  dholland /*	$NetBSD: nfs_lock.c,v 1.1.1.2 2016/11/18 07:49:12 pgoyette Exp $	*/
      2      1.1  dholland /*-
      3      1.1  dholland  * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
      4      1.1  dholland  *
      5      1.1  dholland  * Redistribution and use in source and binary forms, with or without
      6      1.1  dholland  * modification, are permitted provided that the following conditions
      7      1.1  dholland  * are met:
      8      1.1  dholland  * 1. Redistributions of source code must retain the above copyright
      9      1.1  dholland  *    notice, this list of conditions and the following disclaimer.
     10      1.1  dholland  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1  dholland  *    notice, this list of conditions and the following disclaimer in the
     12      1.1  dholland  *    documentation and/or other materials provided with the distribution.
     13      1.1  dholland  * 3. Berkeley Software Design Inc's name may not be used to endorse or
     14      1.1  dholland  *    promote products derived from this software without specific prior
     15      1.1  dholland  *    written permission.
     16      1.1  dholland  *
     17      1.1  dholland  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
     18      1.1  dholland  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19      1.1  dholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20      1.1  dholland  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
     21      1.1  dholland  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22      1.1  dholland  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23      1.1  dholland  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24      1.1  dholland  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25      1.1  dholland  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26      1.1  dholland  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27      1.1  dholland  * SUCH DAMAGE.
     28      1.1  dholland  *
     29      1.1  dholland  *      from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
     30      1.1  dholland  */
     31      1.1  dholland 
     32      1.1  dholland #include <sys/cdefs.h>
     33  1.1.1.2  pgoyette /* __FBSDID("FreeBSD: head/sys/nfs/nfs_lock.c 303382 2016-07-27 11:08:59Z kib "); */
     34      1.1  dholland __RCSID("$NetBSD: nfs_lock.c,v 1.1.1.2 2016/11/18 07:49:12 pgoyette Exp $");
     35      1.1  dholland 
     36      1.1  dholland #include <sys/param.h>
     37      1.1  dholland #include <sys/systm.h>
     38      1.1  dholland #include <sys/conf.h>
     39      1.1  dholland #include <sys/fcntl.h>
     40      1.1  dholland #include <sys/kernel.h>		/* for hz */
     41      1.1  dholland #include <sys/limits.h>
     42      1.1  dholland #include <sys/lock.h>
     43      1.1  dholland #include <sys/malloc.h>
     44      1.1  dholland #include <sys/lockf.h>		/* for hz */ /* Must come after sys/malloc.h */
     45      1.1  dholland #include <sys/mbuf.h>
     46      1.1  dholland #include <sys/mount.h>
     47      1.1  dholland #include <sys/namei.h>
     48      1.1  dholland #include <sys/priv.h>
     49      1.1  dholland #include <sys/proc.h>
     50      1.1  dholland #include <sys/resourcevar.h>
     51      1.1  dholland #include <sys/socket.h>
     52      1.1  dholland #include <sys/socket.h>
     53      1.1  dholland #include <sys/unistd.h>
     54      1.1  dholland #include <sys/vnode.h>
     55      1.1  dholland 
     56      1.1  dholland #include <net/if.h>
     57      1.1  dholland 
     58      1.1  dholland #include <nfs/nfsproto.h>
     59      1.1  dholland #include <nfs/nfs_lock.h>
     60      1.1  dholland #include <nfsclient/nfs.h>
     61      1.1  dholland #include <nfsclient/nfsmount.h>
     62      1.1  dholland #include <nfsclient/nfsnode.h>
     63      1.1  dholland #include <nfsclient/nlminfo.h>
     64      1.1  dholland 
     65      1.1  dholland extern void (*nlminfo_release_p)(struct proc *p);
     66      1.1  dholland 
     67      1.1  dholland vop_advlock_t	*nfs_advlock_p = nfs_dolock;
     68      1.1  dholland vop_reclaim_t	*nfs_reclaim_p = NULL;
     69      1.1  dholland 
     70      1.1  dholland static MALLOC_DEFINE(M_NFSLOCK, "nfsclient_lock", "NFS lock request");
     71      1.1  dholland static MALLOC_DEFINE(M_NLMINFO, "nfsclient_nlminfo",
     72      1.1  dholland     "NFS lock process structure");
     73      1.1  dholland 
     74      1.1  dholland static int nfslockdans(struct thread *td, struct lockd_ans *ansp);
     75      1.1  dholland static void nlminfo_release(struct proc *p);
     76      1.1  dholland /*
     77      1.1  dholland  * --------------------------------------------------------------------
     78      1.1  dholland  * A miniature device driver which the userland uses to talk to us.
     79      1.1  dholland  *
     80      1.1  dholland  */
     81      1.1  dholland 
     82      1.1  dholland static struct cdev *nfslock_dev;
     83      1.1  dholland static struct mtx nfslock_mtx;
     84      1.1  dholland static int nfslock_isopen;
     85      1.1  dholland static TAILQ_HEAD(,__lock_msg)	nfslock_list;
     86      1.1  dholland 
     87      1.1  dholland static int
     88      1.1  dholland nfslock_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
     89      1.1  dholland {
     90      1.1  dholland 	int error;
     91      1.1  dholland 
     92      1.1  dholland 	error = priv_check(td, PRIV_NFS_LOCKD);
     93      1.1  dholland 	if (error)
     94      1.1  dholland 		return (error);
     95      1.1  dholland 
     96      1.1  dholland 	mtx_lock(&nfslock_mtx);
     97      1.1  dholland 	if (!nfslock_isopen) {
     98      1.1  dholland 		error = 0;
     99      1.1  dholland 		nfslock_isopen = 1;
    100      1.1  dholland 	} else {
    101      1.1  dholland 		error = EOPNOTSUPP;
    102      1.1  dholland 	}
    103      1.1  dholland 	mtx_unlock(&nfslock_mtx);
    104      1.1  dholland 
    105      1.1  dholland 	return (error);
    106      1.1  dholland }
    107      1.1  dholland 
    108      1.1  dholland static int
    109      1.1  dholland nfslock_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
    110      1.1  dholland {
    111      1.1  dholland 	struct __lock_msg *lm;
    112      1.1  dholland 
    113      1.1  dholland 	mtx_lock(&nfslock_mtx);
    114      1.1  dholland 	nfslock_isopen = 0;
    115      1.1  dholland 	while (!TAILQ_EMPTY(&nfslock_list)) {
    116      1.1  dholland 		lm = TAILQ_FIRST(&nfslock_list);
    117      1.1  dholland 		/* XXX: answer request */
    118      1.1  dholland 		TAILQ_REMOVE(&nfslock_list, lm, lm_link);
    119      1.1  dholland 		free(lm, M_NFSLOCK);
    120      1.1  dholland 	}
    121      1.1  dholland 	mtx_unlock(&nfslock_mtx);
    122      1.1  dholland 	return (0);
    123      1.1  dholland }
    124      1.1  dholland 
    125      1.1  dholland static int
    126      1.1  dholland nfslock_read(struct cdev *dev, struct uio *uio, int ioflag)
    127      1.1  dholland {
    128      1.1  dholland 	int error;
    129      1.1  dholland 	struct __lock_msg *lm;
    130      1.1  dholland 
    131      1.1  dholland 	if (uio->uio_resid != sizeof *lm)
    132      1.1  dholland 		return (EOPNOTSUPP);
    133      1.1  dholland 	lm = NULL;
    134      1.1  dholland 	error = 0;
    135      1.1  dholland 	mtx_lock(&nfslock_mtx);
    136      1.1  dholland 	while (TAILQ_EMPTY(&nfslock_list)) {
    137      1.1  dholland 		error = msleep(&nfslock_list, &nfslock_mtx, PSOCK | PCATCH,
    138      1.1  dholland 		    "nfslockd", 0);
    139      1.1  dholland 		if (error)
    140      1.1  dholland 			break;
    141      1.1  dholland 	}
    142      1.1  dholland 	if (!error) {
    143      1.1  dholland 		lm = TAILQ_FIRST(&nfslock_list);
    144      1.1  dholland 		TAILQ_REMOVE(&nfslock_list, lm, lm_link);
    145      1.1  dholland 	}
    146      1.1  dholland 	mtx_unlock(&nfslock_mtx);
    147      1.1  dholland 	if (!error) {
    148      1.1  dholland 		error = uiomove(lm, sizeof *lm, uio);
    149      1.1  dholland 		free(lm, M_NFSLOCK);
    150      1.1  dholland 	}
    151      1.1  dholland 	return (error);
    152      1.1  dholland }
    153      1.1  dholland 
    154      1.1  dholland static int
    155      1.1  dholland nfslock_write(struct cdev *dev, struct uio *uio, int ioflag)
    156      1.1  dholland {
    157      1.1  dholland 	struct lockd_ans la;
    158      1.1  dholland 	int error;
    159      1.1  dholland 
    160      1.1  dholland 	if (uio->uio_resid != sizeof la)
    161      1.1  dholland 		return (EOPNOTSUPP);
    162      1.1  dholland 	error = uiomove(&la, sizeof la, uio);
    163      1.1  dholland 	if (!error)
    164      1.1  dholland 		error = nfslockdans(curthread, &la);
    165      1.1  dholland 	return (error);
    166      1.1  dholland }
    167      1.1  dholland 
    168      1.1  dholland static int
    169      1.1  dholland nfslock_send(struct __lock_msg *lm)
    170      1.1  dholland {
    171      1.1  dholland 	struct __lock_msg *lm2;
    172      1.1  dholland 	int error;
    173      1.1  dholland 
    174      1.1  dholland 	error = 0;
    175      1.1  dholland 	lm2 = malloc(sizeof *lm2, M_NFSLOCK, M_WAITOK);
    176      1.1  dholland 	mtx_lock(&nfslock_mtx);
    177      1.1  dholland 	if (nfslock_isopen) {
    178      1.1  dholland 		memcpy(lm2, lm, sizeof *lm2);
    179      1.1  dholland 		TAILQ_INSERT_TAIL(&nfslock_list, lm2, lm_link);
    180      1.1  dholland 		wakeup(&nfslock_list);
    181      1.1  dholland 	} else {
    182      1.1  dholland 		error = EOPNOTSUPP;
    183      1.1  dholland 	}
    184      1.1  dholland 	mtx_unlock(&nfslock_mtx);
    185      1.1  dholland 	if (error)
    186      1.1  dholland 		free(lm2, M_NFSLOCK);
    187      1.1  dholland 	return (error);
    188      1.1  dholland }
    189      1.1  dholland 
    190      1.1  dholland static struct cdevsw nfslock_cdevsw = {
    191      1.1  dholland 	.d_version =	D_VERSION,
    192      1.1  dholland 	.d_open =	nfslock_open,
    193      1.1  dholland 	.d_close =	nfslock_close,
    194      1.1  dholland 	.d_read =	nfslock_read,
    195      1.1  dholland 	.d_write =	nfslock_write,
    196      1.1  dholland 	.d_name =	"nfslock"
    197      1.1  dholland };
    198      1.1  dholland 
    199      1.1  dholland static int
    200      1.1  dholland nfslock_modevent(module_t mod __unused, int type, void *data __unused)
    201      1.1  dholland {
    202      1.1  dholland 
    203      1.1  dholland 	switch (type) {
    204      1.1  dholland 	case MOD_LOAD:
    205      1.1  dholland 		if (bootverbose)
    206      1.1  dholland 			printf("nfslock: pseudo-device\n");
    207      1.1  dholland 		mtx_init(&nfslock_mtx, "nfslock", NULL, MTX_DEF);
    208      1.1  dholland 		TAILQ_INIT(&nfslock_list);
    209      1.1  dholland 		nlminfo_release_p = nlminfo_release;
    210      1.1  dholland 		nfslock_dev = make_dev(&nfslock_cdevsw, 0,
    211      1.1  dholland 		    UID_ROOT, GID_KMEM, 0600, _PATH_NFSLCKDEV);
    212      1.1  dholland 		return (0);
    213      1.1  dholland 	default:
    214      1.1  dholland 		return (EOPNOTSUPP);
    215      1.1  dholland 	}
    216      1.1  dholland }
    217      1.1  dholland 
    218      1.1  dholland DEV_MODULE(nfslock, nfslock_modevent, NULL);
    219      1.1  dholland MODULE_VERSION(nfslock, 1);
    220      1.1  dholland 
    221      1.1  dholland 
    222      1.1  dholland /*
    223      1.1  dholland  * XXX
    224      1.1  dholland  * We have to let the process know if the call succeeded.  I'm using an extra
    225      1.1  dholland  * field in the p_nlminfo field in the proc structure, as it is already for
    226      1.1  dholland  * lockd stuff.
    227      1.1  dholland  */
    228      1.1  dholland 
    229      1.1  dholland /*
    230      1.1  dholland  * nfs_advlock --
    231      1.1  dholland  *      NFS advisory byte-level locks.
    232      1.1  dholland  *
    233      1.1  dholland  * The vnode shall be (shared) locked on the entry, it is
    234      1.1  dholland  * unconditionally unlocked after.
    235      1.1  dholland  */
    236      1.1  dholland int
    237      1.1  dholland nfs_dolock(struct vop_advlock_args *ap)
    238      1.1  dholland {
    239      1.1  dholland 	LOCKD_MSG msg;
    240      1.1  dholland 	struct thread *td;
    241      1.1  dholland 	struct vnode *vp;
    242      1.1  dholland 	int error;
    243      1.1  dholland 	struct flock *fl;
    244      1.1  dholland 	struct proc *p;
    245      1.1  dholland 	struct nfsmount *nmp;
    246  1.1.1.2  pgoyette 	struct timeval boottime;
    247      1.1  dholland 
    248      1.1  dholland 	td = curthread;
    249      1.1  dholland 	p = td->td_proc;
    250      1.1  dholland 
    251      1.1  dholland 	vp = ap->a_vp;
    252      1.1  dholland 	fl = ap->a_fl;
    253      1.1  dholland 	nmp = VFSTONFS(vp->v_mount);
    254      1.1  dholland 
    255      1.1  dholland 	ASSERT_VOP_LOCKED(vp, "nfs_dolock");
    256      1.1  dholland 
    257      1.1  dholland 	nmp->nm_getinfo(vp, msg.lm_fh, &msg.lm_fh_len, &msg.lm_addr,
    258      1.1  dholland 	    &msg.lm_nfsv3, NULL, NULL);
    259      1.1  dholland 	VOP_UNLOCK(vp, 0);
    260      1.1  dholland 
    261      1.1  dholland 	/*
    262      1.1  dholland 	 * the NLM protocol doesn't allow the server to return an error
    263      1.1  dholland 	 * on ranges, so we do it.
    264      1.1  dholland 	 */
    265      1.1  dholland 	if (fl->l_whence != SEEK_END) {
    266      1.1  dholland 		if ((fl->l_whence != SEEK_CUR && fl->l_whence != SEEK_SET) ||
    267      1.1  dholland 		    fl->l_start < 0 ||
    268      1.1  dholland 		    (fl->l_len < 0 &&
    269      1.1  dholland 		     (fl->l_start == 0 || fl->l_start + fl->l_len < 0)))
    270      1.1  dholland 			return (EINVAL);
    271      1.1  dholland 		if (fl->l_len > 0 &&
    272      1.1  dholland 			 (fl->l_len - 1 > OFF_MAX - fl->l_start))
    273      1.1  dholland 			return (EOVERFLOW);
    274      1.1  dholland 	}
    275      1.1  dholland 
    276      1.1  dholland 	/*
    277      1.1  dholland 	 * Fill in the information structure.
    278      1.1  dholland 	 */
    279      1.1  dholland 	msg.lm_version = LOCKD_MSG_VERSION;
    280      1.1  dholland 	msg.lm_msg_ident.pid = p->p_pid;
    281      1.1  dholland 
    282      1.1  dholland 	mtx_lock(&Giant);
    283      1.1  dholland 	/*
    284      1.1  dholland 	 * if there is no nfsowner table yet, allocate one.
    285      1.1  dholland 	 */
    286      1.1  dholland 	if (p->p_nlminfo == NULL) {
    287      1.1  dholland 		p->p_nlminfo = malloc(sizeof(struct nlminfo),
    288      1.1  dholland 		    M_NLMINFO, M_WAITOK | M_ZERO);
    289      1.1  dholland 		p->p_nlminfo->pid_start = p->p_stats->p_start;
    290  1.1.1.2  pgoyette 		getboottime(&boottime);
    291      1.1  dholland 		timevaladd(&p->p_nlminfo->pid_start, &boottime);
    292      1.1  dholland 	}
    293      1.1  dholland 	msg.lm_msg_ident.pid_start = p->p_nlminfo->pid_start;
    294      1.1  dholland 	msg.lm_msg_ident.msg_seq = ++(p->p_nlminfo->msg_seq);
    295      1.1  dholland 
    296      1.1  dholland 	msg.lm_fl = *fl;
    297      1.1  dholland 	msg.lm_wait = ap->a_flags & F_WAIT;
    298      1.1  dholland 	msg.lm_getlk = ap->a_op == F_GETLK;
    299      1.1  dholland 	cru2x(td->td_ucred, &msg.lm_cred);
    300      1.1  dholland 
    301      1.1  dholland 	for (;;) {
    302      1.1  dholland 		error = nfslock_send(&msg);
    303      1.1  dholland 		if (error)
    304      1.1  dholland 			goto out;
    305      1.1  dholland 
    306      1.1  dholland 		/* Unlocks succeed immediately.  */
    307      1.1  dholland 		if (fl->l_type == F_UNLCK)
    308      1.1  dholland 			goto out;
    309      1.1  dholland 
    310      1.1  dholland 		/*
    311      1.1  dholland 		 * Retry after 20 seconds if we haven't gotten a response yet.
    312      1.1  dholland 		 * This number was picked out of thin air... but is longer
    313      1.1  dholland 		 * then even a reasonably loaded system should take (at least
    314      1.1  dholland 		 * on a local network).  XXX Probably should use a back-off
    315      1.1  dholland 		 * scheme.
    316      1.1  dholland 		 *
    317      1.1  dholland 		 * XXX: No PCATCH here since we currently have no useful
    318      1.1  dholland 		 * way to signal to the userland rpc.lockd that the request
    319      1.1  dholland 		 * has been aborted.  Once the rpc.lockd implementation
    320      1.1  dholland 		 * can handle aborts, and we report them properly,
    321      1.1  dholland 		 * PCATCH can be put back.  In the mean time, if we did
    322      1.1  dholland 		 * permit aborting, the lock attempt would "get lost"
    323      1.1  dholland 		 * and the lock would get stuck in the locked state.
    324      1.1  dholland 		 */
    325      1.1  dholland 		error = tsleep(p->p_nlminfo, PUSER, "lockd", 20*hz);
    326      1.1  dholland 		if (error != 0) {
    327      1.1  dholland 			if (error == EWOULDBLOCK) {
    328      1.1  dholland 				/*
    329      1.1  dholland 				 * We timed out, so we rewrite the request
    330      1.1  dholland 				 * to the fifo.
    331      1.1  dholland 				 */
    332      1.1  dholland 				continue;
    333      1.1  dholland 			}
    334      1.1  dholland 
    335      1.1  dholland 			break;
    336      1.1  dholland 		}
    337      1.1  dholland 
    338      1.1  dholland 		if (msg.lm_getlk && p->p_nlminfo->retcode == 0) {
    339      1.1  dholland 			if (p->p_nlminfo->set_getlk_pid) {
    340      1.1  dholland 				fl->l_sysid = 0; /* XXX */
    341      1.1  dholland 				fl->l_pid = p->p_nlminfo->getlk_pid;
    342      1.1  dholland 			} else {
    343      1.1  dholland 				fl->l_type = F_UNLCK;
    344      1.1  dholland 			}
    345      1.1  dholland 		}
    346      1.1  dholland 		error = p->p_nlminfo->retcode;
    347      1.1  dholland 		break;
    348      1.1  dholland 	}
    349      1.1  dholland  out:
    350      1.1  dholland 	mtx_unlock(&Giant);
    351      1.1  dholland 	return (error);
    352      1.1  dholland }
    353      1.1  dholland 
    354      1.1  dholland /*
    355      1.1  dholland  * nfslockdans --
    356      1.1  dholland  *      NFS advisory byte-level locks answer from the lock daemon.
    357      1.1  dholland  */
    358      1.1  dholland static int
    359      1.1  dholland nfslockdans(struct thread *td, struct lockd_ans *ansp)
    360      1.1  dholland {
    361      1.1  dholland 	struct proc *targetp;
    362      1.1  dholland 
    363      1.1  dholland 	/* the version should match, or we're out of sync */
    364      1.1  dholland 	if (ansp->la_vers != LOCKD_ANS_VERSION)
    365      1.1  dholland 		return (EINVAL);
    366      1.1  dholland 
    367      1.1  dholland 	/* Find the process, set its return errno and wake it up. */
    368      1.1  dholland 	if ((targetp = pfind(ansp->la_msg_ident.pid)) == NULL)
    369      1.1  dholland 		return (ESRCH);
    370      1.1  dholland 
    371      1.1  dholland 	/* verify the pid hasn't been reused (if we can), and it isn't waiting
    372      1.1  dholland 	 * for an answer from a more recent request.  We return an EPIPE if
    373      1.1  dholland 	 * the match fails, because we've already used ESRCH above, and this
    374      1.1  dholland 	 * is sort of like writing on a pipe after the reader has closed it.
    375      1.1  dholland 	 */
    376      1.1  dholland 	if (targetp->p_nlminfo == NULL ||
    377      1.1  dholland 	    ((ansp->la_msg_ident.msg_seq != -1) &&
    378      1.1  dholland 	      (timevalcmp(&targetp->p_nlminfo->pid_start,
    379      1.1  dholland 			&ansp->la_msg_ident.pid_start, !=) ||
    380      1.1  dholland 	       targetp->p_nlminfo->msg_seq != ansp->la_msg_ident.msg_seq))) {
    381      1.1  dholland 		PROC_UNLOCK(targetp);
    382      1.1  dholland 		return (EPIPE);
    383      1.1  dholland 	}
    384      1.1  dholland 
    385      1.1  dholland 	targetp->p_nlminfo->retcode = ansp->la_errno;
    386      1.1  dholland 	targetp->p_nlminfo->set_getlk_pid = ansp->la_set_getlk_pid;
    387      1.1  dholland 	targetp->p_nlminfo->getlk_pid = ansp->la_getlk_pid;
    388      1.1  dholland 
    389      1.1  dholland 	wakeup(targetp->p_nlminfo);
    390      1.1  dholland 
    391      1.1  dholland 	PROC_UNLOCK(targetp);
    392      1.1  dholland 	return (0);
    393      1.1  dholland }
    394      1.1  dholland 
    395      1.1  dholland /*
    396      1.1  dholland  * Free nlminfo attached to process.
    397      1.1  dholland  */
    398      1.1  dholland void
    399      1.1  dholland nlminfo_release(struct proc *p)
    400      1.1  dholland {
    401      1.1  dholland 	free(p->p_nlminfo, M_NLMINFO);
    402      1.1  dholland 	p->p_nlminfo = NULL;
    403      1.1  dholland }
    404