Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.28.6.4
      1  1.28.6.4       ad /*	$NetBSD: puffs_vfsops.c,v 1.28.6.4 2007/04/10 13:26:36 ad Exp $	*/
      2       1.1    pooka 
      3       1.1    pooka /*
      4       1.1    pooka  * Copyright (c) 2005, 2006  Antti Kantee.  All Rights Reserved.
      5       1.1    pooka  *
      6       1.1    pooka  * Development of this software was supported by the
      7       1.1    pooka  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8       1.1    pooka  * The Google SoC project was mentored by Bill Studenmund.
      9       1.1    pooka  *
     10       1.1    pooka  * Redistribution and use in source and binary forms, with or without
     11       1.1    pooka  * modification, are permitted provided that the following conditions
     12       1.1    pooka  * are met:
     13       1.1    pooka  * 1. Redistributions of source code must retain the above copyright
     14       1.1    pooka  *    notice, this list of conditions and the following disclaimer.
     15       1.1    pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1    pooka  *    notice, this list of conditions and the following disclaimer in the
     17       1.1    pooka  *    documentation and/or other materials provided with the distribution.
     18       1.1    pooka  * 3. The name of the company nor the name of the author may be used to
     19       1.1    pooka  *    endorse or promote products derived from this software without specific
     20       1.1    pooka  *    prior written permission.
     21       1.1    pooka  *
     22       1.1    pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23       1.1    pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24       1.1    pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25       1.1    pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     26       1.1    pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1    pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28       1.1    pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1    pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1    pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1    pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1    pooka  * SUCH DAMAGE.
     33       1.1    pooka  */
     34       1.1    pooka 
     35       1.1    pooka #include <sys/cdefs.h>
     36  1.28.6.4       ad __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.28.6.4 2007/04/10 13:26:36 ad Exp $");
     37       1.1    pooka 
     38       1.1    pooka #include <sys/param.h>
     39       1.1    pooka #include <sys/mount.h>
     40       1.1    pooka #include <sys/malloc.h>
     41       1.1    pooka #include <sys/extattr.h>
     42       1.1    pooka #include <sys/queue.h>
     43       1.1    pooka #include <sys/vnode.h>
     44       1.1    pooka #include <sys/dirent.h>
     45       1.1    pooka #include <sys/kauth.h>
     46      1.26    pooka #include <sys/fstrans.h>
     47  1.28.6.3       ad #include <sys/proc.h>
     48       1.1    pooka 
     49       1.1    pooka #include <lib/libkern/libkern.h>
     50       1.1    pooka 
     51       1.1    pooka #include <fs/puffs/puffs_msgif.h>
     52       1.1    pooka #include <fs/puffs/puffs_sys.h>
     53       1.1    pooka 
     54       1.1    pooka VFS_PROTOS(puffs);
     55       1.1    pooka 
     56  1.28.6.4       ad MALLOC_JUSTDEFINE(M_PUFFS, "puffs", "Pass-to-Userspace Framework File System");
     57       1.1    pooka 
     58      1.22    pooka #ifndef PUFFS_PNODEBUCKETS
     59      1.22    pooka #define PUFFS_PNODEBUCKETS 256
     60      1.22    pooka #endif
     61      1.22    pooka #ifndef PUFFS_MAXPNODEBUCKETS
     62      1.22    pooka #define PUFFS_MAXPNODEBUCKETS 65536
     63      1.22    pooka #endif
     64      1.22    pooka int puffs_pnodebuckets = PUFFS_PNODEBUCKETS;
     65      1.22    pooka 
     66       1.1    pooka int
     67       1.1    pooka puffs_mount(struct mount *mp, const char *path, void *data,
     68       1.1    pooka 	    struct nameidata *ndp, struct lwp *l)
     69       1.1    pooka {
     70      1.22    pooka 	struct puffs_mount *pmp = NULL;
     71      1.17    pooka 	struct puffs_args *args;
     72      1.17    pooka 	char namebuf[PUFFSNAMESIZE+sizeof(PUFFS_NAMEPREFIX)+1]; /* spooky */
     73      1.22    pooka 	int error = 0, i;
     74       1.1    pooka 
     75       1.1    pooka 	if (mp->mnt_flag & MNT_GETARGS) {
     76       1.1    pooka 		pmp = MPTOPUFFSMP(mp);
     77       1.1    pooka 		return copyout(&pmp->pmp_args, data, sizeof(struct puffs_args));
     78       1.1    pooka 	}
     79       1.1    pooka 
     80       1.1    pooka 	/* update is not supported currently */
     81       1.1    pooka 	if (mp->mnt_flag & MNT_UPDATE)
     82       1.1    pooka 		return EOPNOTSUPP;
     83       1.1    pooka 
     84       1.1    pooka 	/*
     85       1.1    pooka 	 * We need the file system name
     86       1.1    pooka 	 */
     87       1.1    pooka 	if (!data)
     88       1.1    pooka 		return EINVAL;
     89       1.1    pooka 
     90      1.17    pooka 	MALLOC(args, struct puffs_args *, sizeof(struct puffs_args),
     91      1.17    pooka 	    M_PUFFS, M_WAITOK);
     92      1.17    pooka 
     93      1.17    pooka 	error = copyin(data, args, sizeof(struct puffs_args));
     94       1.1    pooka 	if (error)
     95      1.17    pooka 		goto out;
     96      1.17    pooka 
     97      1.17    pooka 	/* devel phase */
     98      1.17    pooka 	if (args->pa_vers != (PUFFSVERSION | PUFFSDEVELVERS)) {
     99      1.17    pooka 		printf("puffs_mount: development version mismatch\n");
    100      1.17    pooka 		error = EINVAL;
    101      1.17    pooka 		goto out;
    102      1.17    pooka 	}
    103       1.1    pooka 
    104       1.8    pooka 	/* nuke spy bits */
    105      1.17    pooka 	args->pa_flags &= PUFFS_KFLAG_MASK;
    106       1.8    pooka 
    107       1.1    pooka 	/* build real name */
    108      1.17    pooka 	(void)strlcpy(namebuf, PUFFS_NAMEPREFIX, sizeof(namebuf));
    109      1.17    pooka 	(void)strlcat(namebuf, args->pa_name, sizeof(namebuf));
    110       1.1    pooka 
    111       1.1    pooka 	/* inform user server if it got the max request size it wanted */
    112      1.17    pooka 	if (args->pa_maxreqlen == 0 || args->pa_maxreqlen > PUFFS_REQ_MAXSIZE)
    113      1.17    pooka 		args->pa_maxreqlen = PUFFS_REQ_MAXSIZE;
    114      1.17    pooka 	else if (args->pa_maxreqlen < PUFFS_REQSTRUCT_MAX)
    115      1.17    pooka 		args->pa_maxreqlen = PUFFS_REQSTRUCT_MAX;
    116      1.17    pooka 	(void)strlcpy(args->pa_name, namebuf, sizeof(args->pa_name));
    117       1.1    pooka 
    118      1.17    pooka 	error = copyout(args, data, sizeof(struct puffs_args));
    119       1.1    pooka 	if (error)
    120      1.17    pooka 		goto out;
    121       1.1    pooka 
    122       1.1    pooka 	error = set_statvfs_info(path, UIO_USERSPACE, namebuf,
    123       1.1    pooka 	    UIO_SYSSPACE, mp, l);
    124       1.1    pooka 	if (error)
    125      1.17    pooka 		goto out;
    126      1.10    pooka 	mp->mnt_stat.f_iosize = DEV_BSIZE;
    127       1.1    pooka 
    128       1.1    pooka 	MALLOC(pmp, struct puffs_mount *, sizeof(struct puffs_mount),
    129       1.1    pooka 	    M_PUFFS, M_WAITOK | M_ZERO);
    130       1.1    pooka 
    131       1.6    pooka 	mp->mnt_fs_bshift = DEV_BSHIFT;
    132       1.6    pooka 	mp->mnt_dev_bshift = DEV_BSHIFT;
    133       1.6    pooka 	mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
    134       1.1    pooka 	mp->mnt_data = pmp;
    135      1.26    pooka 	mp->mnt_iflag |= IMNT_HAS_TRANS;
    136       1.6    pooka 
    137       1.1    pooka 	pmp->pmp_status = PUFFSTAT_MOUNTING;
    138       1.1    pooka 	pmp->pmp_nextreq = 0;
    139       1.1    pooka 	pmp->pmp_mp = mp;
    140      1.17    pooka 	pmp->pmp_req_maxsize = args->pa_maxreqlen;
    141      1.17    pooka 	pmp->pmp_args = *args;
    142       1.1    pooka 
    143      1.22    pooka 	/* puffs_node hash buckets */
    144      1.22    pooka 	pmp->pmp_npnodehash = puffs_pnodebuckets;
    145      1.22    pooka 	if (pmp->pmp_npnodehash < 1)
    146      1.22    pooka 		pmp->pmp_npnodehash = 1;
    147      1.22    pooka 	if (pmp->pmp_npnodehash > PUFFS_MAXPNODEBUCKETS)
    148      1.22    pooka 		pmp->pmp_npnodehash = PUFFS_MAXPNODEBUCKETS;
    149      1.22    pooka 	pmp->pmp_pnodehash = malloc
    150      1.22    pooka 	    (sizeof(struct puffs_pnode_hashlist *) * pmp->pmp_npnodehash,
    151      1.22    pooka 	    M_PUFFS, M_WAITOK);
    152      1.22    pooka 	for (i = 0; i < pmp->pmp_npnodehash; i++)
    153      1.22    pooka 		LIST_INIT(&pmp->pmp_pnodehash[i]);
    154      1.22    pooka 
    155       1.1    pooka 	/*
    156       1.1    pooka 	 * Inform the fileops processing code that we have a mountpoint.
    157       1.1    pooka 	 * If it doesn't know about anyone with our pid/fd having the
    158       1.1    pooka 	 * device open, punt
    159       1.1    pooka 	 */
    160      1.17    pooka 	if (puffs_setpmp(l->l_proc->p_pid, args->pa_fd, pmp)) {
    161      1.17    pooka 		error = ENOENT;
    162      1.17    pooka 		goto out;
    163       1.1    pooka 	}
    164       1.1    pooka 
    165  1.28.6.4       ad 	mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
    166  1.28.6.4       ad 	cv_init(&pmp->pmp_req_waiter_cv, "puffsget");
    167  1.28.6.4       ad 	cv_init(&pmp->pmp_req_waitersink_cv, "puffsink");
    168  1.28.6.4       ad 	cv_init(&pmp->pmp_unmounting_cv, "puffsum");
    169  1.28.6.4       ad 	cv_init(&pmp->pmp_suspend_cv, "pufsusum");
    170       1.1    pooka 	TAILQ_INIT(&pmp->pmp_req_touser);
    171       1.1    pooka 	TAILQ_INIT(&pmp->pmp_req_replywait);
    172       1.1    pooka 	TAILQ_INIT(&pmp->pmp_req_sizepark);
    173       1.1    pooka 
    174       1.1    pooka 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    175       1.1    pooka 	    mp, MPTOPUFFSMP(mp)));
    176       1.1    pooka 
    177       1.1    pooka 	vfs_getnewfsid(mp);
    178       1.1    pooka 
    179      1.17    pooka  out:
    180      1.22    pooka 	if (error && pmp && pmp->pmp_pnodehash)
    181      1.22    pooka 		free(pmp->pmp_pnodehash, M_PUFFS);
    182      1.22    pooka 	if (error && pmp)
    183      1.22    pooka 		FREE(pmp, M_PUFFS);
    184      1.17    pooka 	FREE(args, M_PUFFS);
    185      1.17    pooka 	return error;
    186       1.1    pooka }
    187       1.1    pooka 
    188       1.1    pooka /*
    189       1.1    pooka  * This is called from the first "Hello, I'm alive" ioctl
    190       1.1    pooka  * from userspace.
    191       1.1    pooka  */
    192       1.1    pooka int
    193       1.7    pooka puffs_start2(struct puffs_mount *pmp, struct puffs_startreq *sreq)
    194       1.1    pooka {
    195       1.1    pooka 	struct puffs_node *pn;
    196       1.1    pooka 	struct mount *mp;
    197       1.1    pooka 
    198       1.1    pooka 	mp = PMPTOMP(pmp);
    199       1.1    pooka 
    200  1.28.6.4       ad 	mutex_enter(&pmp->pmp_lock);
    201       1.1    pooka 
    202       1.1    pooka 	/*
    203       1.1    pooka 	 * if someone has issued a VFS_ROOT() already, fill in the
    204       1.1    pooka 	 * vnode cookie.
    205       1.1    pooka 	 */
    206       1.2    pooka 	pn = NULL;
    207       1.1    pooka 	if (pmp->pmp_root) {
    208       1.1    pooka 		pn = VPTOPP(pmp->pmp_root);
    209       1.1    pooka 		pn->pn_cookie = sreq->psr_cookie;
    210       1.1    pooka 	}
    211       1.1    pooka 
    212       1.1    pooka 	/* We're good to fly */
    213       1.1    pooka 	pmp->pmp_rootcookie = sreq->psr_cookie;
    214       1.1    pooka 	pmp->pmp_status = PUFFSTAT_RUNNING;
    215  1.28.6.4       ad 	mutex_exit(&pmp->pmp_lock);
    216       1.1    pooka 
    217       1.9    pooka 	/* do the VFS_STATVFS() we missed out on in sys_mount() */
    218       1.9    pooka 	copy_statvfs_info(&sreq->psr_sb, mp);
    219       1.9    pooka 	(void)memcpy(&mp->mnt_stat, &sreq->psr_sb, sizeof(mp->mnt_stat));
    220      1.11    pooka 	mp->mnt_stat.f_iosize = DEV_BSIZE;
    221       1.1    pooka 
    222       1.2    pooka 	DPRINTF(("puffs_start2: root vp %p, cur root pnode %p, cookie %p\n",
    223       1.1    pooka 	    pmp->pmp_root, pn, sreq->psr_cookie));
    224       1.1    pooka 
    225       1.1    pooka 	return 0;
    226       1.1    pooka }
    227       1.1    pooka 
    228       1.1    pooka int
    229       1.1    pooka puffs_start(struct mount *mp, int flags, struct lwp *l)
    230       1.1    pooka {
    231       1.1    pooka 
    232       1.1    pooka 	/*
    233       1.1    pooka 	 * This cannot travel to userspace, as this is called from
    234       1.1    pooka 	 * the kernel context of the process doing mount(2).  But
    235       1.1    pooka 	 * it's probably a safe bet that the process doing mount(2)
    236       1.1    pooka 	 * realizes it needs to start the filesystem also...
    237       1.1    pooka 	 */
    238       1.1    pooka 	return 0;
    239       1.1    pooka }
    240       1.1    pooka 
    241       1.1    pooka int
    242       1.1    pooka puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    243       1.1    pooka {
    244       1.1    pooka 	struct puffs_mount *pmp;
    245       1.1    pooka 	int error, force;
    246       1.1    pooka 
    247       1.1    pooka 	PUFFS_VFSREQ(unmount);
    248       1.1    pooka 
    249       1.1    pooka 	error = 0;
    250       1.1    pooka 	force = mntflags & MNT_FORCE;
    251       1.1    pooka 	pmp = MPTOPUFFSMP(mp);
    252       1.1    pooka 
    253       1.1    pooka 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    254       1.1    pooka 	    "status 0x%x\n", pmp->pmp_status));
    255       1.1    pooka 
    256       1.1    pooka 	/*
    257       1.1    pooka 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    258       1.1    pooka 	 * root vnode does not get flushed until unmount.  The
    259       1.1    pooka 	 * userspace root node cookie is stored in the mount
    260       1.1    pooka 	 * structure, so we can always re-instantiate a root vnode,
    261       1.1    pooka 	 * should userspace unmount decide it doesn't want to
    262       1.1    pooka 	 * cooperate.
    263       1.1    pooka 	 */
    264       1.1    pooka 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    265       1.1    pooka 	if (error)
    266       1.1    pooka 		goto out;
    267       1.1    pooka 
    268       1.1    pooka 	/*
    269       1.1    pooka 	 * If we are not DYING, we should ask userspace's opinion
    270       1.1    pooka 	 * about the situation
    271       1.1    pooka 	 */
    272  1.28.6.4       ad 	mutex_enter(&pmp->pmp_lock);
    273       1.1    pooka 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    274      1.16    pooka 		pmp->pmp_unmounting = 1;
    275  1.28.6.4       ad 		mutex_exit(&pmp->pmp_lock);
    276      1.16    pooka 
    277       1.1    pooka 		unmount_arg.pvfsr_flags = mntflags;
    278       1.1    pooka 		unmount_arg.pvfsr_pid = puffs_lwp2pid(l);
    279      1.16    pooka 
    280       1.1    pooka 		error = puffs_vfstouser(pmp, PUFFS_VFS_UNMOUNT,
    281       1.1    pooka 		     &unmount_arg, sizeof(unmount_arg));
    282      1.16    pooka 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    283      1.16    pooka 
    284  1.28.6.4       ad 		mutex_enter(&pmp->pmp_lock);
    285      1.16    pooka 		pmp->pmp_unmounting = 0;
    286  1.28.6.4       ad 		cv_broadcast(&pmp->pmp_unmounting_cv);
    287       1.1    pooka 	}
    288       1.1    pooka 
    289       1.1    pooka 	/*
    290       1.1    pooka 	 * if userspace cooperated or we really need to die,
    291       1.1    pooka 	 * screw what userland thinks and just die.
    292       1.1    pooka 	 */
    293       1.1    pooka 	if (error == 0 || force) {
    294      1.26    pooka 		/* tell waiters & other resources to go unwait themselves */
    295      1.26    pooka 		puffs_userdead(pmp);
    296       1.1    pooka 		puffs_nukebypmp(pmp);
    297      1.26    pooka 
    298      1.26    pooka 		/*
    299      1.26    pooka 		 * Sink waiters.  This is still not perfect, since the
    300      1.26    pooka 		 * draining is done after userret, not when they really
    301      1.26    pooka 		 * exit the file system.  It will probably work as almost
    302      1.26    pooka 		 * no call will block and therefore cause a context switch
    303      1.26    pooka 		 * and therefore will protected by the biglock after
    304      1.26    pooka 		 * exiting userspace.  But ... it's an imperfect world.
    305      1.26    pooka 		 */
    306  1.28.6.4       ad 		while (pmp->pmp_req_waiters != 0)
    307  1.28.6.4       ad 			cv_wait(&pmp->pmp_req_waitersink_cv, &pmp->pmp_lock);
    308  1.28.6.4       ad 		mutex_exit(&pmp->pmp_lock);
    309      1.26    pooka 
    310      1.26    pooka 		/* free resources now that we hopefully have no waiters left */
    311  1.28.6.4       ad 		cv_destroy(&pmp->pmp_req_waiter_cv);
    312  1.28.6.4       ad 		cv_destroy(&pmp->pmp_req_waitersink_cv);
    313  1.28.6.4       ad 		cv_destroy(&pmp->pmp_unmounting_cv);
    314  1.28.6.4       ad 		cv_destroy(&pmp->pmp_suspend_cv);
    315  1.28.6.4       ad 		mutex_destroy(&pmp->pmp_lock);
    316  1.28.6.4       ad 
    317      1.22    pooka 		free(pmp->pmp_pnodehash, M_PUFFS);
    318       1.1    pooka 		FREE(pmp, M_PUFFS);
    319       1.1    pooka 		error = 0;
    320      1.16    pooka 	} else {
    321  1.28.6.4       ad 		mutex_exit(&pmp->pmp_lock);
    322       1.1    pooka 	}
    323       1.1    pooka 
    324       1.1    pooka  out:
    325       1.2    pooka 	DPRINTF(("puffs_unmount: return %d\n", error));
    326       1.1    pooka 	return error;
    327       1.1    pooka }
    328       1.1    pooka 
    329       1.1    pooka /*
    330       1.1    pooka  * This doesn't need to travel to userspace
    331       1.1    pooka  */
    332       1.1    pooka int
    333       1.1    pooka puffs_root(struct mount *mp, struct vnode **vpp)
    334       1.1    pooka {
    335       1.1    pooka 	struct puffs_mount *pmp;
    336       1.1    pooka 	struct puffs_node *pn;
    337       1.1    pooka 	struct vnode *vp;
    338       1.1    pooka 
    339       1.1    pooka 	pmp = MPTOPUFFSMP(mp);
    340       1.1    pooka 
    341       1.1    pooka 	/*
    342       1.1    pooka 	 * pmp_lock must be held if vref()'ing or vrele()'ing the
    343      1.20    pooka 	 * root vnode.  the latter is controlled by puffs_inactive().
    344       1.1    pooka 	 */
    345  1.28.6.4       ad 	mutex_enter(&pmp->pmp_lock);
    346       1.1    pooka 	vp = pmp->pmp_root;
    347       1.1    pooka 	if (vp) {
    348  1.28.6.3       ad 		mutex_enter(&vp->v_interlock);
    349  1.28.6.4       ad 		mutex_exit(&pmp->pmp_lock);
    350       1.1    pooka 		pn = VPTOPP(vp);
    351      1.25    pooka 		if (vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK))
    352      1.20    pooka 			goto grabnew;
    353       1.1    pooka 		*vpp = vp;
    354       1.1    pooka 		return 0;
    355      1.25    pooka 	} else
    356  1.28.6.4       ad 		mutex_exit(&pmp->pmp_lock);
    357      1.25    pooka 
    358      1.25    pooka 	/* XXX: this is wrong, so FIXME */
    359       1.1    pooka  grabnew:
    360       1.1    pooka 
    361       1.1    pooka 	/*
    362       1.1    pooka 	 * So, didn't have the magic root vnode available.
    363       1.1    pooka 	 * No matter, grab another an stuff it with the cookie.
    364       1.1    pooka 	 */
    365       1.6    pooka 	if (puffs_getvnode(mp, pmp->pmp_rootcookie, VDIR, 0, 0, &vp))
    366       1.1    pooka 		panic("sloppy programming");
    367       1.1    pooka 
    368  1.28.6.4       ad 	mutex_enter(&pmp->pmp_lock);
    369       1.1    pooka 	/*
    370       1.1    pooka 	 * check if by mysterious force someone else created a root
    371       1.1    pooka 	 * vnode while we were executing.
    372       1.1    pooka 	 */
    373       1.1    pooka 	if (pmp->pmp_root) {
    374       1.1    pooka 		vref(pmp->pmp_root);
    375  1.28.6.4       ad 		mutex_exit(&pmp->pmp_lock);
    376       1.1    pooka 		puffs_putvnode(vp);
    377       1.1    pooka 		vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
    378       1.1    pooka 		*vpp = pmp->pmp_root;
    379       1.1    pooka 		return 0;
    380       1.1    pooka 	}
    381       1.1    pooka 
    382       1.1    pooka 	/* store cache */
    383       1.1    pooka 	vp->v_flag = VROOT;
    384       1.1    pooka 	pmp->pmp_root = vp;
    385  1.28.6.4       ad 	mutex_exit(&pmp->pmp_lock);
    386       1.1    pooka 
    387       1.1    pooka 	vn_lock(pmp->pmp_root, LK_EXCLUSIVE | LK_RETRY);
    388       1.1    pooka 
    389       1.1    pooka 	*vpp = vp;
    390       1.1    pooka 	return 0;
    391       1.1    pooka }
    392       1.1    pooka 
    393       1.1    pooka int
    394       1.1    pooka puffs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg, struct lwp *l)
    395       1.1    pooka {
    396       1.1    pooka 
    397       1.1    pooka 	return EOPNOTSUPP;
    398       1.1    pooka }
    399       1.1    pooka 
    400       1.1    pooka int
    401       1.1    pooka puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    402       1.1    pooka {
    403       1.1    pooka 	struct puffs_vfsreq_statvfs *statvfs_arg; /* too big for stack */
    404       1.1    pooka 	struct puffs_mount *pmp;
    405       1.1    pooka 	int error = 0;
    406       1.1    pooka 
    407       1.1    pooka 	pmp = MPTOPUFFSMP(mp);
    408       1.1    pooka 
    409       1.1    pooka 	/*
    410       1.1    pooka 	 * If we are mounting, it means that the userspace counterpart
    411       1.1    pooka 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    412       1.1    pooka 	 * requesting statvfs from userspace would mean a deadlock.
    413       1.1    pooka 	 * Compensate.
    414       1.1    pooka 	 */
    415       1.1    pooka 	if (pmp->pmp_status == PUFFSTAT_MOUNTING)
    416       1.1    pooka 		return EINPROGRESS;
    417       1.1    pooka 
    418       1.1    pooka 	/* too big for stack */
    419       1.1    pooka 	MALLOC(statvfs_arg, struct puffs_vfsreq_statvfs *,
    420       1.1    pooka 	    sizeof(struct puffs_vfsreq_statvfs), M_PUFFS, M_WAITOK | M_ZERO);
    421       1.1    pooka 	statvfs_arg->pvfsr_pid = puffs_lwp2pid(l);
    422       1.1    pooka 
    423       1.1    pooka 	error = puffs_vfstouser(pmp, PUFFS_VFS_STATVFS,
    424       1.1    pooka 	    statvfs_arg, sizeof(*statvfs_arg));
    425      1.11    pooka 	statvfs_arg->pvfsr_sb.f_iosize = DEV_BSIZE;
    426      1.10    pooka 
    427       1.1    pooka 	/*
    428       1.1    pooka 	 * Try to produce a sensible result even in the event
    429       1.1    pooka 	 * of userspace error.
    430       1.1    pooka 	 *
    431       1.1    pooka 	 * XXX: cache the copy in non-error case
    432       1.1    pooka 	 */
    433       1.1    pooka 	if (!error) {
    434       1.1    pooka 		copy_statvfs_info(&statvfs_arg->pvfsr_sb, mp);
    435       1.1    pooka 		(void)memcpy(sbp, &statvfs_arg->pvfsr_sb,
    436       1.1    pooka 		    sizeof(struct statvfs));
    437       1.1    pooka 	} else {
    438       1.1    pooka 		copy_statvfs_info(sbp, mp);
    439       1.1    pooka 	}
    440       1.1    pooka 
    441       1.1    pooka 	FREE(statvfs_arg, M_PUFFS);
    442      1.14    pooka 	return error;
    443       1.1    pooka }
    444       1.1    pooka 
    445      1.26    pooka static int
    446  1.28.6.4       ad pageflush(struct mount *mp, kauth_cred_t cred,
    447  1.28.6.4       ad 	int waitfor, int suspending, struct lwp *l)
    448       1.1    pooka {
    449      1.26    pooka 	struct puffs_node *pn;
    450      1.18    pooka 	struct vnode *vp, *nvp;
    451  1.28.6.4       ad 	int error, rv;
    452       1.1    pooka 
    453      1.26    pooka 	KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
    454      1.26    pooka 	KASSERT((suspending == 0)
    455      1.26    pooka 	    || (fstrans_is_owner(mp)
    456      1.27  hannken 	      && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
    457       1.1    pooka 
    458      1.18    pooka 	error = 0;
    459      1.18    pooka 
    460      1.18    pooka 	/*
    461      1.24    pooka 	 * Sync all cached data from regular vnodes (which are not
    462      1.24    pooka 	 * currently locked, see below).  After this we call VFS_SYNC
    463      1.24    pooka 	 * for the fs server, which should handle data and metadata for
    464      1.24    pooka 	 * all the nodes it knows to exist.
    465      1.18    pooka 	 */
    466  1.28.6.2       ad 	mutex_enter(&mntvnode_lock);
    467      1.18    pooka  loop:
    468      1.18    pooka 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
    469      1.18    pooka 		/* check if we're on the right list */
    470      1.18    pooka 		if (vp->v_mount != mp)
    471      1.18    pooka 			goto loop;
    472      1.18    pooka 
    473  1.28.6.2       ad 		mutex_enter(&vp->v_interlock);
    474      1.26    pooka 		pn = VPTOPP(vp);
    475      1.18    pooka 		nvp = TAILQ_NEXT(vp, v_mntvnodes);
    476      1.18    pooka 
    477      1.19    pooka 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    478  1.28.6.2       ad 			mutex_exit(&vp->v_interlock);
    479      1.18    pooka 			continue;
    480      1.18    pooka 		}
    481      1.18    pooka 
    482  1.28.6.2       ad 		mutex_exit(&mntvnode_lock);
    483      1.21    pooka 
    484      1.21    pooka 		/*
    485      1.21    pooka 		 * Here we try to get a reference to the vnode and to
    486      1.21    pooka 		 * lock it.  This is mostly cargo-culted, but I will
    487      1.21    pooka 		 * offer an explanation to why I believe this might
    488      1.21    pooka 		 * actually do the right thing.
    489      1.21    pooka 		 *
    490      1.21    pooka 		 * If the vnode is a goner, we quite obviously don't need
    491      1.21    pooka 		 * to sync it.
    492      1.21    pooka 		 *
    493      1.21    pooka 		 * If the vnode was busy, we don't need to sync it because
    494      1.21    pooka 		 * this is never called with MNT_WAIT except from
    495      1.21    pooka 		 * dounmount(), when we are wait-flushing all the dirty
    496      1.21    pooka 		 * vnodes through other routes in any case.  So there,
    497      1.21    pooka 		 * sync() doesn't actually sync.  Happy now?
    498      1.26    pooka 		 *
    499      1.26    pooka 		 * NOTE: if we're suspending, vget() does NOT lock.
    500      1.26    pooka 		 * See puffs_lock() for details.
    501      1.21    pooka 		 */
    502      1.18    pooka 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    503      1.18    pooka 		if (rv) {
    504  1.28.6.2       ad 			mutex_enter(&mntvnode_lock);
    505      1.18    pooka 			if (rv == ENOENT)
    506      1.18    pooka 				goto loop;
    507      1.18    pooka 			continue;
    508      1.18    pooka 		}
    509      1.18    pooka 
    510      1.26    pooka 		/*
    511      1.26    pooka 		 * Thread information to puffs_strategy() through the
    512      1.26    pooka 		 * pnode flags: we want to issue the putpages operations
    513      1.26    pooka 		 * as FAF if we're suspending, since it's very probable
    514      1.26    pooka 		 * that our execution context is that of the userspace
    515      1.26    pooka 		 * daemon.  We can do this because:
    516      1.26    pooka 		 *   + we send the "going to suspend" prior to this part
    517      1.26    pooka 		 *   + if any of the writes fails in userspace, it's the
    518      1.26    pooka 		 *     file system server's problem to decide if this was a
    519      1.26    pooka 		 *     failed snapshot when it gets the "snapshot complete"
    520      1.26    pooka 		 *     notification.
    521      1.26    pooka 		 *   + if any of the writes fail in the kernel already, we
    522      1.26    pooka 		 *     immediately fail *and* notify the user server of
    523      1.26    pooka 		 *     failure.
    524      1.26    pooka 		 *
    525      1.26    pooka 		 * We also do FAFs if we're called from the syncer.  This
    526      1.26    pooka 		 * is just general optimization for trickle sync: no need
    527      1.26    pooka 		 * to really guarantee that the stuff ended on backing
    528      1.26    pooka 		 * storage.
    529      1.26    pooka 		 * TODO: Maybe also hint the user server of this twist?
    530      1.26    pooka 		 */
    531  1.28.6.4       ad 		if (suspending || waitfor == MNT_LAZY) {
    532  1.28.6.4       ad 			mutex_enter(&vp->v_interlock);
    533      1.26    pooka 			pn->pn_stat |= PNODE_SUSPEND;
    534  1.28.6.4       ad 			mutex_exit(&vp->v_interlock);
    535  1.28.6.4       ad 		}
    536  1.28.6.4       ad 		rv = VOP_FSYNC(vp, cred, waitfor, 0, 0, l);
    537      1.26    pooka 		if (suspending || waitfor == MNT_LAZY) {
    538  1.28.6.2       ad 			mutex_enter(&vp->v_interlock);
    539      1.26    pooka 			pn->pn_stat &= ~PNODE_SUSPEND;
    540  1.28.6.2       ad 			mutex_exit(&vp->v_interlock);
    541      1.26    pooka 		}
    542      1.21    pooka 		if (rv)
    543      1.18    pooka 			error = rv;
    544      1.18    pooka 		vput(vp);
    545  1.28.6.2       ad 		mutex_enter(&mntvnode_lock);
    546      1.18    pooka 	}
    547  1.28.6.2       ad 	mutex_exit(&mntvnode_lock);
    548      1.18    pooka 
    549      1.26    pooka 	return error;
    550      1.26    pooka }
    551      1.26    pooka 
    552      1.26    pooka int
    553      1.26    pooka puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
    554      1.26    pooka 	struct lwp *l)
    555      1.26    pooka {
    556      1.26    pooka 	int error, rv;
    557      1.26    pooka 
    558      1.26    pooka 	PUFFS_VFSREQ(sync);
    559      1.26    pooka 
    560  1.28.6.4       ad 	error = pageflush(mp, cred, waitfor, 0, l);
    561      1.26    pooka 
    562      1.18    pooka 	/* sync fs */
    563       1.1    pooka 	sync_arg.pvfsr_waitfor = waitfor;
    564       1.1    pooka 	puffs_credcvt(&sync_arg.pvfsr_cred, cred);
    565       1.1    pooka 	sync_arg.pvfsr_pid = puffs_lwp2pid(l);
    566       1.1    pooka 
    567      1.18    pooka 	rv = puffs_vfstouser(MPTOPUFFSMP(mp), PUFFS_VFS_SYNC,
    568       1.1    pooka 	    &sync_arg, sizeof(sync_arg));
    569      1.18    pooka 	if (rv)
    570      1.18    pooka 		error = rv;
    571       1.1    pooka 
    572       1.1    pooka 	return error;
    573       1.1    pooka }
    574       1.1    pooka 
    575       1.1    pooka int
    576       1.1    pooka puffs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    577       1.1    pooka {
    578       1.1    pooka 
    579       1.1    pooka 	return EOPNOTSUPP;
    580       1.1    pooka }
    581       1.1    pooka 
    582       1.1    pooka #if 0
    583       1.1    pooka /*ARGSUSED*/
    584       1.1    pooka int
    585       1.1    pooka puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    586       1.1    pooka {
    587       1.1    pooka 
    588       1.1    pooka 	return EOPNOTSUPP;
    589       1.1    pooka }
    590       1.1    pooka 
    591       1.1    pooka /*ARGSUSED*/
    592       1.1    pooka int
    593       1.1    pooka puffs_vptofh(struct vnode *vp, struct fid *fhp)
    594       1.1    pooka {
    595       1.1    pooka 
    596       1.1    pooka 	return EOPNOTSUPP;
    597       1.1    pooka }
    598       1.1    pooka #endif
    599       1.1    pooka 
    600       1.1    pooka void
    601       1.1    pooka puffs_init()
    602       1.1    pooka {
    603       1.1    pooka 
    604       1.5    pooka 	malloc_type_attach(M_PUFFS);
    605       1.5    pooka 
    606  1.28.6.4       ad 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    607  1.28.6.4       ad 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    608  1.28.6.4       ad 	puffs_transport_init();
    609  1.28.6.4       ad 	puffs_msgif_init();
    610       1.1    pooka }
    611       1.1    pooka 
    612       1.1    pooka void
    613       1.1    pooka puffs_done()
    614       1.1    pooka {
    615       1.1    pooka 
    616  1.28.6.4       ad 	puffs_msgif_destroy();
    617  1.28.6.4       ad 	puffs_transport_destroy();
    618       1.5    pooka 	pool_destroy(&puffs_pnpool);
    619       1.5    pooka 
    620  1.28.6.4       ad 	malloc_type_detach(M_PUFFS);
    621       1.1    pooka }
    622       1.1    pooka 
    623       1.1    pooka int
    624       1.1    pooka puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    625       1.1    pooka {
    626       1.1    pooka 
    627       1.1    pooka 	return EOPNOTSUPP;
    628       1.1    pooka }
    629       1.1    pooka 
    630      1.26    pooka int
    631      1.26    pooka puffs_suspendctl(struct mount *mp, int cmd)
    632      1.26    pooka {
    633      1.26    pooka 	struct puffs_mount *pmp;
    634      1.26    pooka 	int error;
    635      1.26    pooka 
    636      1.26    pooka 	pmp = MPTOPUFFSMP(mp);
    637      1.26    pooka 	switch (cmd) {
    638      1.26    pooka 	case SUSPEND_SUSPEND:
    639      1.26    pooka 		DPRINTF(("puffs_suspendctl: suspending\n"));
    640      1.27  hannken 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
    641      1.26    pooka 			break;
    642      1.26    pooka 		puffs_suspendtouser(pmp, PUFFS_SUSPEND_START);
    643      1.26    pooka 
    644  1.28.6.4       ad 		error = pageflush(mp, FSCRED, 0, 1, curlwp);
    645      1.26    pooka 		if (error == 0)
    646      1.27  hannken 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
    647      1.26    pooka 
    648      1.26    pooka 		if (error != 0) {
    649      1.26    pooka 			puffs_suspendtouser(pmp, PUFFS_SUSPEND_ERROR);
    650      1.27  hannken 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    651      1.26    pooka 			break;
    652      1.26    pooka 		}
    653      1.26    pooka 
    654      1.26    pooka 		puffs_suspendtouser(pmp, PUFFS_SUSPEND_SUSPENDED);
    655      1.26    pooka 
    656      1.26    pooka 		break;
    657      1.26    pooka 
    658      1.26    pooka 	case SUSPEND_RESUME:
    659      1.26    pooka 		DPRINTF(("puffs_suspendctl: resume\n"));
    660      1.26    pooka 		error = 0;
    661      1.27  hannken 		(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    662      1.26    pooka 		puffs_suspendtouser(pmp, PUFFS_SUSPEND_RESUME);
    663      1.26    pooka 		break;
    664      1.26    pooka 
    665      1.26    pooka 	default:
    666      1.26    pooka 		error = EINVAL;
    667      1.26    pooka 		break;
    668      1.26    pooka 	}
    669      1.26    pooka 
    670      1.26    pooka 	DPRINTF(("puffs_suspendctl: return %d\n", error));
    671      1.26    pooka 	return error;
    672      1.26    pooka }
    673      1.26    pooka 
    674       1.1    pooka const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    675       1.1    pooka 	&puffs_vnodeop_opv_desc,
    676       1.3    pooka 	&puffs_specop_opv_desc,
    677       1.4    pooka 	&puffs_fifoop_opv_desc,
    678      1.12    pooka 	&puffs_msgop_opv_desc,
    679       1.1    pooka 	NULL,
    680       1.1    pooka };
    681       1.1    pooka 
    682       1.1    pooka struct vfsops puffs_vfsops = {
    683       1.1    pooka 	MOUNT_PUFFS,
    684       1.1    pooka 	puffs_mount,		/* mount	*/
    685       1.1    pooka 	puffs_start,		/* start	*/
    686       1.1    pooka 	puffs_unmount,		/* unmount	*/
    687       1.1    pooka 	puffs_root,		/* root		*/
    688       1.1    pooka 	puffs_quotactl,		/* quotactl	*/
    689       1.1    pooka 	puffs_statvfs,		/* statvfs	*/
    690       1.1    pooka 	puffs_sync,		/* sync		*/
    691       1.1    pooka 	puffs_vget,		/* vget		*/
    692      1.15      chs 	(void *)eopnotsupp,	/* fhtovp	*/
    693      1.15      chs 	(void *)eopnotsupp,	/* vptofh	*/
    694       1.1    pooka 	puffs_init,		/* init		*/
    695       1.1    pooka 	NULL,			/* reinit	*/
    696       1.1    pooka 	puffs_done,		/* done		*/
    697       1.1    pooka 	NULL,			/* mountroot	*/
    698       1.1    pooka 	puffs_snapshot,		/* snapshot	*/
    699       1.1    pooka 	vfs_stdextattrctl,	/* extattrctl	*/
    700      1.26    pooka 	puffs_suspendctl,	/* suspendctl	*/
    701       1.1    pooka 	puffs_vnodeopv_descs,	/* vnodeops	*/
    702       1.1    pooka 	0,			/* refcount	*/
    703       1.1    pooka 	{ NULL, NULL }
    704       1.1    pooka };
    705       1.1    pooka VFS_ATTACH(puffs_vfsops);
    706