Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.81.8.3
      1  1.81.8.3       riz /*	$NetBSD: puffs_vfsops.c,v 1.81.8.3 2011/11/02 20:11:12 riz 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  *
     19       1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20       1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21       1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22       1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23       1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25       1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1     pooka  * SUCH DAMAGE.
     30       1.1     pooka  */
     31       1.1     pooka 
     32       1.1     pooka #include <sys/cdefs.h>
     33  1.81.8.3       riz __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.81.8.3 2011/11/02 20:11:12 riz Exp $");
     34       1.1     pooka 
     35       1.1     pooka #include <sys/param.h>
     36       1.1     pooka #include <sys/mount.h>
     37       1.1     pooka #include <sys/malloc.h>
     38       1.1     pooka #include <sys/extattr.h>
     39       1.1     pooka #include <sys/queue.h>
     40       1.1     pooka #include <sys/vnode.h>
     41       1.1     pooka #include <sys/dirent.h>
     42       1.1     pooka #include <sys/kauth.h>
     43      1.26     pooka #include <sys/fstrans.h>
     44      1.47        ad #include <sys/proc.h>
     45      1.80    rumble #include <sys/module.h>
     46  1.81.8.1       snj #include <sys/kthread.h>
     47       1.1     pooka 
     48  1.81.8.3       riz #include <uvm/uvm.h>
     49  1.81.8.3       riz 
     50      1.68     pooka #include <dev/putter/putter_sys.h>
     51       1.1     pooka 
     52      1.78  dholland #include <miscfs/genfs/genfs.h>
     53      1.78  dholland 
     54       1.1     pooka #include <fs/puffs/puffs_msgif.h>
     55       1.1     pooka #include <fs/puffs/puffs_sys.h>
     56       1.1     pooka 
     57      1.66     pooka #include <lib/libkern/libkern.h>
     58      1.66     pooka 
     59      1.38     pooka #include <nfs/nfsproto.h> /* for fh sizes */
     60      1.38     pooka 
     61      1.81  jmcneill MODULE(MODULE_CLASS_VFS, puffs, "putter");
     62      1.80    rumble 
     63      1.73     pooka VFS_PROTOS(puffs_vfsop);
     64       1.1     pooka 
     65      1.22     pooka #ifndef PUFFS_PNODEBUCKETS
     66      1.22     pooka #define PUFFS_PNODEBUCKETS 256
     67      1.22     pooka #endif
     68      1.22     pooka #ifndef PUFFS_MAXPNODEBUCKETS
     69      1.35     pooka #define PUFFS_MAXPNODEBUCKETS 8192
     70      1.22     pooka #endif
     71      1.35     pooka int puffs_pnodebuckets_default = PUFFS_PNODEBUCKETS;
     72      1.35     pooka int puffs_maxpnodebuckets = PUFFS_MAXPNODEBUCKETS;
     73      1.22     pooka 
     74      1.70     pooka #define BUCKETALLOC(a) (sizeof(struct puffs_pnode_hashlist *) * (a))
     75      1.70     pooka 
     76      1.66     pooka static struct putter_ops puffs_putter = {
     77      1.66     pooka 	.pop_getout	= puffs_msgif_getout,
     78      1.66     pooka 	.pop_releaseout	= puffs_msgif_releaseout,
     79      1.66     pooka 	.pop_waitcount	= puffs_msgif_waitcount,
     80      1.66     pooka 	.pop_dispatch	= puffs_msgif_dispatch,
     81      1.66     pooka 	.pop_close	= puffs_msgif_close,
     82      1.66     pooka };
     83      1.66     pooka 
     84       1.1     pooka int
     85      1.73     pooka puffs_vfsop_mount(struct mount *mp, const char *path, void *data,
     86      1.73     pooka 	size_t *data_len)
     87       1.1     pooka {
     88      1.22     pooka 	struct puffs_mount *pmp = NULL;
     89      1.34     pooka 	struct puffs_kargs *args;
     90      1.51     pooka 	char fstype[_VFS_NAMELEN];
     91      1.51     pooka 	char *p;
     92      1.22     pooka 	int error = 0, i;
     93      1.71     pooka 	pid_t mntpid = curlwp->l_proc->p_pid;
     94       1.1     pooka 
     95      1.48       dsl 	if (*data_len < sizeof *args)
     96      1.48       dsl 		return EINVAL;
     97      1.48       dsl 
     98       1.1     pooka 	if (mp->mnt_flag & MNT_GETARGS) {
     99       1.1     pooka 		pmp = MPTOPUFFSMP(mp);
    100      1.48       dsl 		*(struct puffs_kargs *)data = pmp->pmp_args;
    101      1.48       dsl 		*data_len = sizeof *args;
    102      1.48       dsl 		return 0;
    103       1.1     pooka 	}
    104       1.1     pooka 
    105       1.1     pooka 	/* update is not supported currently */
    106       1.1     pooka 	if (mp->mnt_flag & MNT_UPDATE)
    107       1.1     pooka 		return EOPNOTSUPP;
    108       1.1     pooka 
    109       1.1     pooka 	/*
    110       1.1     pooka 	 * We need the file system name
    111       1.1     pooka 	 */
    112       1.1     pooka 	if (!data)
    113       1.1     pooka 		return EINVAL;
    114       1.1     pooka 
    115      1.77     pooka 	error = fstrans_mount(mp);
    116      1.77     pooka 	if (error)
    117      1.77     pooka 		return error;
    118      1.70     pooka 	args = (struct puffs_kargs *)data;
    119      1.17     pooka 
    120      1.17     pooka 	/* devel phase */
    121      1.17     pooka 	if (args->pa_vers != (PUFFSVERSION | PUFFSDEVELVERS)) {
    122      1.64     pooka 		printf("puffs_mount: development version mismatch: "
    123      1.64     pooka 		    "kernel %d, lib %d\n",
    124      1.64     pooka 		    PUFFSVERSION, args->pa_vers & ~PUFFSDEVELVERS);
    125      1.17     pooka 		error = EINVAL;
    126      1.17     pooka 		goto out;
    127      1.17     pooka 	}
    128       1.1     pooka 
    129      1.54     pooka 	if ((args->pa_flags & ~PUFFS_KFLAG_MASK) != 0) {
    130      1.54     pooka 		printf("puffs_mount: invalid KFLAGs 0x%x\n", args->pa_flags);
    131      1.54     pooka 		error = EINVAL;
    132      1.54     pooka 		goto out;
    133      1.54     pooka 	}
    134      1.54     pooka 	if ((args->pa_fhflags & ~PUFFS_FHFLAG_MASK) != 0) {
    135      1.54     pooka 		printf("puffs_mount: invalid FHFLAGs 0x%x\n", args->pa_fhflags);
    136      1.54     pooka 		error = EINVAL;
    137      1.54     pooka 		goto out;
    138      1.54     pooka 	}
    139      1.54     pooka 
    140      1.54     pooka 	/* use dummy value for passthrough */
    141      1.54     pooka 	if (args->pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    142      1.54     pooka 		args->pa_fhsize = sizeof(struct fid);
    143       1.8     pooka 
    144      1.38     pooka 	/* sanitize file handle length */
    145      1.38     pooka 	if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) {
    146      1.38     pooka 		printf("puffs_mount: handle size %zu too large\n",
    147      1.38     pooka 		    args->pa_fhsize);
    148      1.38     pooka 		error = EINVAL;
    149      1.38     pooka 		goto out;
    150      1.38     pooka 	}
    151      1.38     pooka 	/* sanity check file handle max sizes */
    152      1.38     pooka 	if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) {
    153      1.38     pooka 		size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize);
    154      1.38     pooka 
    155      1.38     pooka 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) {
    156      1.38     pooka 			if (NFSX_FHTOOBIG_P(kfhsize, 0)) {
    157      1.38     pooka 				printf("puffs_mount: fhsize larger than "
    158      1.38     pooka 				    "NFSv2 max %d\n",
    159      1.38     pooka 				    PUFFS_FROMFHSIZE(NFSX_V2FH));
    160      1.38     pooka 				error = EINVAL;
    161      1.38     pooka 				goto out;
    162      1.38     pooka 			}
    163      1.38     pooka 		}
    164      1.38     pooka 
    165      1.38     pooka 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) {
    166      1.38     pooka 			if (NFSX_FHTOOBIG_P(kfhsize, 1)) {
    167      1.38     pooka 				printf("puffs_mount: fhsize larger than "
    168      1.38     pooka 				    "NFSv3 max %d\n",
    169      1.38     pooka 				    PUFFS_FROMFHSIZE(NFSX_V3FHMAX));
    170      1.38     pooka 				error = EINVAL;
    171      1.38     pooka 				goto out;
    172      1.38     pooka 			}
    173      1.38     pooka 		}
    174      1.38     pooka 	}
    175      1.38     pooka 
    176      1.51     pooka 	/* don't allow non-printing characters (like my sweet umlauts.. snif) */
    177      1.51     pooka 	args->pa_typename[sizeof(args->pa_typename)-1] = '\0';
    178      1.51     pooka 	for (p = args->pa_typename; *p; p++)
    179      1.51     pooka 		if (*p < ' ' || *p > '~')
    180      1.51     pooka 			*p = '.';
    181      1.51     pooka 
    182      1.51     pooka 	args->pa_mntfromname[sizeof(args->pa_mntfromname)-1] = '\0';
    183      1.51     pooka 	for (p = args->pa_mntfromname; *p; p++)
    184      1.51     pooka 		if (*p < ' ' || *p > '~')
    185      1.51     pooka 			*p = '.';
    186      1.51     pooka 
    187       1.1     pooka 	/* build real name */
    188      1.51     pooka 	(void)strlcpy(fstype, PUFFS_TYPEPREFIX, sizeof(fstype));
    189      1.51     pooka 	(void)strlcat(fstype, args->pa_typename, sizeof(fstype));
    190       1.1     pooka 
    191       1.1     pooka 	/* inform user server if it got the max request size it wanted */
    192      1.63     pooka 	if (args->pa_maxmsglen == 0 || args->pa_maxmsglen > PUFFS_MSG_MAXSIZE)
    193      1.63     pooka 		args->pa_maxmsglen = PUFFS_MSG_MAXSIZE;
    194      1.63     pooka 	else if (args->pa_maxmsglen < 2*PUFFS_MSGSTRUCT_MAX)
    195      1.63     pooka 		args->pa_maxmsglen = 2*PUFFS_MSGSTRUCT_MAX;
    196      1.63     pooka 
    197      1.51     pooka 	(void)strlcpy(args->pa_typename, fstype, sizeof(args->pa_typename));
    198       1.1     pooka 
    199      1.40     pooka 	if (args->pa_nhashbuckets == 0)
    200      1.40     pooka 		args->pa_nhashbuckets = puffs_pnodebuckets_default;
    201      1.40     pooka 	if (args->pa_nhashbuckets < 1)
    202      1.40     pooka 		args->pa_nhashbuckets = 1;
    203      1.40     pooka 	if (args->pa_nhashbuckets > PUFFS_MAXPNODEBUCKETS) {
    204      1.40     pooka 		args->pa_nhashbuckets = puffs_maxpnodebuckets;
    205      1.40     pooka 		printf("puffs_mount: using %d hash buckets. "
    206      1.40     pooka 		    "adjust puffs_maxpnodebuckets for more\n",
    207      1.40     pooka 		    puffs_maxpnodebuckets);
    208      1.40     pooka 	}
    209      1.40     pooka 
    210      1.51     pooka 	error = set_statvfs_info(path, UIO_USERSPACE, args->pa_mntfromname,
    211      1.71     pooka 	    UIO_SYSSPACE, fstype, mp, curlwp);
    212       1.1     pooka 	if (error)
    213      1.17     pooka 		goto out;
    214      1.10     pooka 	mp->mnt_stat.f_iosize = DEV_BSIZE;
    215       1.1     pooka 
    216      1.42     pooka 	/*
    217      1.42     pooka 	 * We can't handle the VFS_STATVFS() mount_domount() does
    218      1.42     pooka 	 * after VFS_MOUNT() because we'd deadlock, so handle it
    219      1.42     pooka 	 * here already.
    220      1.42     pooka 	 */
    221      1.42     pooka 	copy_statvfs_info(&args->pa_svfsb, mp);
    222      1.42     pooka 	(void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat));
    223      1.42     pooka 
    224  1.81.8.3       riz 	KASSERT(curlwp != uvm.pagedaemon_lwp);
    225      1.70     pooka 	pmp = kmem_zalloc(sizeof(struct puffs_mount), KM_SLEEP);
    226       1.1     pooka 
    227       1.6     pooka 	mp->mnt_fs_bshift = DEV_BSHIFT;
    228       1.6     pooka 	mp->mnt_dev_bshift = DEV_BSHIFT;
    229       1.6     pooka 	mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
    230       1.1     pooka 	mp->mnt_data = pmp;
    231      1.26     pooka 	mp->mnt_iflag |= IMNT_HAS_TRANS;
    232       1.6     pooka 
    233       1.1     pooka 	pmp->pmp_status = PUFFSTAT_MOUNTING;
    234       1.1     pooka 	pmp->pmp_mp = mp;
    235      1.63     pooka 	pmp->pmp_msg_maxsize = args->pa_maxmsglen;
    236      1.17     pooka 	pmp->pmp_args = *args;
    237       1.1     pooka 
    238      1.40     pooka 	pmp->pmp_npnodehash = args->pa_nhashbuckets;
    239      1.70     pooka 	pmp->pmp_pnodehash = kmem_alloc(BUCKETALLOC(pmp->pmp_npnodehash), KM_SLEEP);
    240      1.22     pooka 	for (i = 0; i < pmp->pmp_npnodehash; i++)
    241      1.22     pooka 		LIST_INIT(&pmp->pmp_pnodehash[i]);
    242      1.57     pooka 	LIST_INIT(&pmp->pmp_newcookie);
    243      1.22     pooka 
    244       1.1     pooka 	/*
    245       1.1     pooka 	 * Inform the fileops processing code that we have a mountpoint.
    246       1.1     pooka 	 * If it doesn't know about anyone with our pid/fd having the
    247       1.1     pooka 	 * device open, punt
    248       1.1     pooka 	 */
    249      1.66     pooka 	if ((pmp->pmp_pi
    250      1.66     pooka 	    = putter_attach(mntpid, args->pa_fd, pmp, &puffs_putter)) == NULL) {
    251      1.17     pooka 		error = ENOENT;
    252      1.17     pooka 		goto out;
    253       1.1     pooka 	}
    254       1.1     pooka 
    255      1.42     pooka 	/* XXX: check parameters */
    256      1.42     pooka 	pmp->pmp_root_cookie = args->pa_root_cookie;
    257      1.42     pooka 	pmp->pmp_root_vtype = args->pa_root_vtype;
    258      1.42     pooka 	pmp->pmp_root_vsize = args->pa_root_vsize;
    259      1.42     pooka 	pmp->pmp_root_rdev = args->pa_root_rdev;
    260      1.42     pooka 
    261      1.31     pooka 	mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
    262  1.81.8.1       snj 	mutex_init(&pmp->pmp_sopmtx, MUTEX_DEFAULT, IPL_NONE);
    263      1.63     pooka 	cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
    264      1.41     pooka 	cv_init(&pmp->pmp_refcount_cv, "puffsref");
    265      1.31     pooka 	cv_init(&pmp->pmp_unmounting_cv, "puffsum");
    266  1.81.8.1       snj 	cv_init(&pmp->pmp_sopcv, "puffsop");
    267      1.63     pooka 	TAILQ_INIT(&pmp->pmp_msg_touser);
    268      1.63     pooka 	TAILQ_INIT(&pmp->pmp_msg_replywait);
    269  1.81.8.1       snj 	TAILQ_INIT(&pmp->pmp_sopreqs);
    270  1.81.8.1       snj 
    271  1.81.8.1       snj 	if ((error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    272  1.81.8.1       snj 	    puffs_sop_thread, pmp, NULL, "puffsop")) != 0)
    273  1.81.8.1       snj 		goto out;
    274  1.81.8.1       snj 	pmp->pmp_sopthrcount = 1;
    275       1.1     pooka 
    276       1.1     pooka 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    277       1.1     pooka 	    mp, MPTOPUFFSMP(mp)));
    278       1.1     pooka 
    279       1.1     pooka 	vfs_getnewfsid(mp);
    280       1.1     pooka 
    281      1.17     pooka  out:
    282      1.77     pooka 	if (error)
    283      1.77     pooka 		fstrans_unmount(mp);
    284  1.81.8.1       snj 	if (error && pmp && pmp->pmp_pi)
    285  1.81.8.1       snj 		putter_detach(pmp->pmp_pi);
    286      1.22     pooka 	if (error && pmp && pmp->pmp_pnodehash)
    287      1.70     pooka 		kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash));
    288      1.22     pooka 	if (error && pmp)
    289      1.70     pooka 		kmem_free(pmp, sizeof(struct puffs_mount));
    290      1.17     pooka 	return error;
    291       1.1     pooka }
    292       1.1     pooka 
    293       1.1     pooka int
    294      1.73     pooka puffs_vfsop_start(struct mount *mp, int flags)
    295       1.1     pooka {
    296      1.42     pooka 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    297       1.1     pooka 
    298      1.42     pooka 	KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
    299       1.1     pooka 	pmp->pmp_status = PUFFSTAT_RUNNING;
    300       1.1     pooka 
    301       1.1     pooka 	return 0;
    302       1.1     pooka }
    303       1.1     pooka 
    304       1.1     pooka int
    305      1.73     pooka puffs_vfsop_unmount(struct mount *mp, int mntflags)
    306       1.1     pooka {
    307      1.63     pooka 	PUFFS_MSG_VARS(vfs, unmount);
    308       1.1     pooka 	struct puffs_mount *pmp;
    309       1.1     pooka 	int error, force;
    310       1.1     pooka 
    311       1.1     pooka 	error = 0;
    312       1.1     pooka 	force = mntflags & MNT_FORCE;
    313       1.1     pooka 	pmp = MPTOPUFFSMP(mp);
    314       1.1     pooka 
    315       1.1     pooka 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    316       1.1     pooka 	    "status 0x%x\n", pmp->pmp_status));
    317       1.1     pooka 
    318       1.1     pooka 	/*
    319       1.1     pooka 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    320       1.1     pooka 	 * root vnode does not get flushed until unmount.  The
    321       1.1     pooka 	 * userspace root node cookie is stored in the mount
    322       1.1     pooka 	 * structure, so we can always re-instantiate a root vnode,
    323       1.1     pooka 	 * should userspace unmount decide it doesn't want to
    324       1.1     pooka 	 * cooperate.
    325       1.1     pooka 	 */
    326       1.1     pooka 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    327       1.1     pooka 	if (error)
    328       1.1     pooka 		goto out;
    329       1.1     pooka 
    330       1.1     pooka 	/*
    331       1.1     pooka 	 * If we are not DYING, we should ask userspace's opinion
    332       1.1     pooka 	 * about the situation
    333       1.1     pooka 	 */
    334      1.31     pooka 	mutex_enter(&pmp->pmp_lock);
    335       1.1     pooka 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    336      1.16     pooka 		pmp->pmp_unmounting = 1;
    337      1.31     pooka 		mutex_exit(&pmp->pmp_lock);
    338      1.16     pooka 
    339      1.63     pooka 		PUFFS_MSG_ALLOC(vfs, unmount);
    340      1.69     pooka 		puffs_msg_setinfo(park_unmount,
    341      1.69     pooka 		    PUFFSOP_VFS, PUFFS_VFS_UNMOUNT, NULL);
    342      1.63     pooka 		unmount_msg->pvfsr_flags = mntflags;
    343      1.63     pooka 
    344      1.69     pooka 		PUFFS_MSG_ENQUEUEWAIT(pmp, park_unmount, error);
    345      1.63     pooka 		PUFFS_MSG_RELEASE(unmount);
    346      1.16     pooka 
    347      1.60     pooka 		error = checkerr(pmp, error, __func__);
    348      1.16     pooka 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    349      1.16     pooka 
    350      1.31     pooka 		mutex_enter(&pmp->pmp_lock);
    351      1.16     pooka 		pmp->pmp_unmounting = 0;
    352      1.31     pooka 		cv_broadcast(&pmp->pmp_unmounting_cv);
    353       1.1     pooka 	}
    354       1.1     pooka 
    355       1.1     pooka 	/*
    356       1.1     pooka 	 * if userspace cooperated or we really need to die,
    357       1.1     pooka 	 * screw what userland thinks and just die.
    358       1.1     pooka 	 */
    359       1.1     pooka 	if (error == 0 || force) {
    360  1.81.8.1       snj 		struct puffs_sopreq *psopr;
    361  1.81.8.1       snj 
    362      1.26     pooka 		/* tell waiters & other resources to go unwait themselves */
    363      1.26     pooka 		puffs_userdead(pmp);
    364      1.66     pooka 		putter_detach(pmp->pmp_pi);
    365      1.26     pooka 
    366      1.26     pooka 		/*
    367      1.41     pooka 		 * Wait until there are no more users for the mount resource.
    368      1.41     pooka 		 * Notice that this is hooked against transport_close
    369      1.41     pooka 		 * and return from touser.  In an ideal world, it would
    370      1.41     pooka 		 * be hooked against final return from all operations.
    371      1.41     pooka 		 * But currently it works well enough, since nobody
    372      1.41     pooka 		 * does weird blocking voodoo after return from touser().
    373      1.26     pooka 		 */
    374      1.41     pooka 		while (pmp->pmp_refcount != 0)
    375      1.41     pooka 			cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
    376      1.31     pooka 		mutex_exit(&pmp->pmp_lock);
    377      1.26     pooka 
    378  1.81.8.1       snj 		/*
    379  1.81.8.1       snj 		 * Release kernel thread now that there is nothing
    380  1.81.8.1       snj 		 * it would be wanting to lock.
    381  1.81.8.1       snj 		 */
    382  1.81.8.3       riz 		KASSERT(curlwp != uvm.pagedaemon_lwp);
    383  1.81.8.1       snj 		psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
    384  1.81.8.1       snj 		psopr->psopr_sopreq = PUFFS_SOPREQ_EXIT;
    385  1.81.8.1       snj 		mutex_enter(&pmp->pmp_sopmtx);
    386  1.81.8.1       snj 		TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs, psopr, psopr_entries);
    387  1.81.8.1       snj 		cv_signal(&pmp->pmp_sopcv);
    388  1.81.8.1       snj 		while (pmp->pmp_sopthrcount > 0)
    389  1.81.8.1       snj 			cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx);
    390  1.81.8.1       snj 		mutex_exit(&pmp->pmp_sopmtx);
    391  1.81.8.1       snj 
    392      1.26     pooka 		/* free resources now that we hopefully have no waiters left */
    393      1.41     pooka 		cv_destroy(&pmp->pmp_unmounting_cv);
    394      1.41     pooka 		cv_destroy(&pmp->pmp_refcount_cv);
    395      1.63     pooka 		cv_destroy(&pmp->pmp_msg_waiter_cv);
    396  1.81.8.1       snj 		cv_destroy(&pmp->pmp_sopcv);
    397      1.31     pooka 		mutex_destroy(&pmp->pmp_lock);
    398  1.81.8.1       snj 		mutex_destroy(&pmp->pmp_sopmtx);
    399      1.31     pooka 
    400      1.77     pooka 		fstrans_unmount(mp);
    401      1.70     pooka 		kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash));
    402      1.70     pooka 		kmem_free(pmp, sizeof(struct puffs_mount));
    403       1.1     pooka 		error = 0;
    404      1.16     pooka 	} else {
    405      1.31     pooka 		mutex_exit(&pmp->pmp_lock);
    406       1.1     pooka 	}
    407       1.1     pooka 
    408       1.1     pooka  out:
    409       1.2     pooka 	DPRINTF(("puffs_unmount: return %d\n", error));
    410       1.1     pooka 	return error;
    411       1.1     pooka }
    412       1.1     pooka 
    413       1.1     pooka /*
    414       1.1     pooka  * This doesn't need to travel to userspace
    415       1.1     pooka  */
    416       1.1     pooka int
    417      1.73     pooka puffs_vfsop_root(struct mount *mp, struct vnode **vpp)
    418       1.1     pooka {
    419      1.45     pooka 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    420      1.59     pooka 	int rv;
    421       1.1     pooka 
    422      1.59     pooka 	rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, 1, 1, vpp);
    423      1.59     pooka 	KASSERT(rv != PUFFS_NOSUCHCOOKIE);
    424      1.59     pooka 	return rv;
    425       1.1     pooka }
    426       1.1     pooka 
    427       1.1     pooka int
    428      1.73     pooka puffs_vfsop_statvfs(struct mount *mp, struct statvfs *sbp)
    429       1.1     pooka {
    430      1.63     pooka 	PUFFS_MSG_VARS(vfs, statvfs);
    431       1.1     pooka 	struct puffs_mount *pmp;
    432       1.1     pooka 	int error = 0;
    433       1.1     pooka 
    434       1.1     pooka 	pmp = MPTOPUFFSMP(mp);
    435       1.1     pooka 
    436       1.1     pooka 	/*
    437       1.1     pooka 	 * If we are mounting, it means that the userspace counterpart
    438       1.1     pooka 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    439       1.1     pooka 	 * requesting statvfs from userspace would mean a deadlock.
    440       1.1     pooka 	 * Compensate.
    441       1.1     pooka 	 */
    442       1.1     pooka 	if (pmp->pmp_status == PUFFSTAT_MOUNTING)
    443       1.1     pooka 		return EINPROGRESS;
    444       1.1     pooka 
    445      1.63     pooka 	PUFFS_MSG_ALLOC(vfs, statvfs);
    446      1.69     pooka 	puffs_msg_setinfo(park_statvfs, PUFFSOP_VFS, PUFFS_VFS_STATVFS, NULL);
    447       1.1     pooka 
    448      1.69     pooka 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_statvfs, error);
    449      1.60     pooka 	error = checkerr(pmp, error, __func__);
    450      1.63     pooka 	statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
    451      1.10     pooka 
    452       1.1     pooka 	/*
    453       1.1     pooka 	 * Try to produce a sensible result even in the event
    454       1.1     pooka 	 * of userspace error.
    455       1.1     pooka 	 *
    456       1.1     pooka 	 * XXX: cache the copy in non-error case
    457       1.1     pooka 	 */
    458       1.1     pooka 	if (!error) {
    459      1.63     pooka 		copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
    460      1.63     pooka 		(void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
    461       1.1     pooka 		    sizeof(struct statvfs));
    462       1.1     pooka 	} else {
    463       1.1     pooka 		copy_statvfs_info(sbp, mp);
    464       1.1     pooka 	}
    465       1.1     pooka 
    466      1.63     pooka 	PUFFS_MSG_RELEASE(statvfs);
    467      1.14     pooka 	return error;
    468       1.1     pooka }
    469       1.1     pooka 
    470      1.26     pooka static int
    471      1.71     pooka pageflush(struct mount *mp, kauth_cred_t cred, int waitfor, int suspending)
    472       1.1     pooka {
    473      1.26     pooka 	struct puffs_node *pn;
    474      1.74        ad 	struct vnode *vp, *mvp;
    475  1.81.8.3       riz 	int error, rv, fsyncwait;
    476       1.1     pooka 
    477      1.26     pooka 	KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
    478      1.26     pooka 	KASSERT((suspending == 0)
    479      1.26     pooka 	    || (fstrans_is_owner(mp)
    480      1.27   hannken 	      && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
    481       1.1     pooka 
    482      1.18     pooka 	error = 0;
    483  1.81.8.3       riz 	fsyncwait = (waitfor == MNT_WAIT) ? FSYNC_WAIT : 0;
    484      1.18     pooka 
    485      1.74        ad 	/* Allocate a marker vnode. */
    486      1.76     pooka 	if ((mvp = vnalloc(mp)) == NULL)
    487      1.74        ad 		return ENOMEM;
    488      1.74        ad 
    489      1.18     pooka 	/*
    490      1.24     pooka 	 * Sync all cached data from regular vnodes (which are not
    491      1.24     pooka 	 * currently locked, see below).  After this we call VFS_SYNC
    492      1.24     pooka 	 * for the fs server, which should handle data and metadata for
    493      1.24     pooka 	 * all the nodes it knows to exist.
    494      1.18     pooka 	 */
    495      1.74        ad 	mutex_enter(&mntvnode_lock);
    496      1.18     pooka  loop:
    497      1.74        ad 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    498      1.74        ad 		vmark(mvp, vp);
    499      1.74        ad 		if (vp->v_mount != mp || vismarker(vp))
    500      1.74        ad 			continue;
    501      1.18     pooka 
    502      1.74        ad 		mutex_enter(&vp->v_interlock);
    503      1.26     pooka 		pn = VPTOPP(vp);
    504      1.19     pooka 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    505      1.74        ad 			mutex_exit(&vp->v_interlock);
    506      1.18     pooka 			continue;
    507      1.18     pooka 		}
    508      1.18     pooka 
    509      1.74        ad 		mutex_exit(&mntvnode_lock);
    510      1.21     pooka 
    511      1.21     pooka 		/*
    512      1.21     pooka 		 * Here we try to get a reference to the vnode and to
    513      1.21     pooka 		 * lock it.  This is mostly cargo-culted, but I will
    514      1.21     pooka 		 * offer an explanation to why I believe this might
    515      1.21     pooka 		 * actually do the right thing.
    516      1.21     pooka 		 *
    517      1.21     pooka 		 * If the vnode is a goner, we quite obviously don't need
    518      1.21     pooka 		 * to sync it.
    519      1.21     pooka 		 *
    520      1.21     pooka 		 * If the vnode was busy, we don't need to sync it because
    521      1.21     pooka 		 * this is never called with MNT_WAIT except from
    522      1.21     pooka 		 * dounmount(), when we are wait-flushing all the dirty
    523      1.21     pooka 		 * vnodes through other routes in any case.  So there,
    524      1.21     pooka 		 * sync() doesn't actually sync.  Happy now?
    525      1.26     pooka 		 *
    526      1.26     pooka 		 * NOTE: if we're suspending, vget() does NOT lock.
    527      1.26     pooka 		 * See puffs_lock() for details.
    528      1.21     pooka 		 */
    529      1.18     pooka 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    530      1.18     pooka 		if (rv) {
    531      1.74        ad 			mutex_enter(&mntvnode_lock);
    532      1.74        ad 			if (rv == ENOENT) {
    533      1.74        ad 				(void)vunmark(mvp);
    534      1.18     pooka 				goto loop;
    535      1.74        ad 			}
    536      1.18     pooka 			continue;
    537      1.18     pooka 		}
    538      1.18     pooka 
    539      1.26     pooka 		/*
    540      1.26     pooka 		 * Thread information to puffs_strategy() through the
    541      1.26     pooka 		 * pnode flags: we want to issue the putpages operations
    542      1.26     pooka 		 * as FAF if we're suspending, since it's very probable
    543      1.26     pooka 		 * that our execution context is that of the userspace
    544      1.26     pooka 		 * daemon.  We can do this because:
    545      1.26     pooka 		 *   + we send the "going to suspend" prior to this part
    546      1.26     pooka 		 *   + if any of the writes fails in userspace, it's the
    547      1.26     pooka 		 *     file system server's problem to decide if this was a
    548      1.26     pooka 		 *     failed snapshot when it gets the "snapshot complete"
    549      1.26     pooka 		 *     notification.
    550      1.26     pooka 		 *   + if any of the writes fail in the kernel already, we
    551      1.26     pooka 		 *     immediately fail *and* notify the user server of
    552      1.26     pooka 		 *     failure.
    553      1.26     pooka 		 *
    554      1.26     pooka 		 * We also do FAFs if we're called from the syncer.  This
    555      1.26     pooka 		 * is just general optimization for trickle sync: no need
    556      1.26     pooka 		 * to really guarantee that the stuff ended on backing
    557      1.26     pooka 		 * storage.
    558      1.26     pooka 		 * TODO: Maybe also hint the user server of this twist?
    559      1.26     pooka 		 */
    560      1.30     pooka 		if (suspending || waitfor == MNT_LAZY) {
    561      1.74        ad 			mutex_enter(&vp->v_interlock);
    562      1.26     pooka 			pn->pn_stat |= PNODE_SUSPEND;
    563      1.74        ad 			mutex_exit(&vp->v_interlock);
    564      1.30     pooka 		}
    565  1.81.8.3       riz 		rv = VOP_FSYNC(vp, cred, fsyncwait, 0, 0);
    566      1.26     pooka 		if (suspending || waitfor == MNT_LAZY) {
    567      1.74        ad 			mutex_enter(&vp->v_interlock);
    568      1.26     pooka 			pn->pn_stat &= ~PNODE_SUSPEND;
    569      1.74        ad 			mutex_exit(&vp->v_interlock);
    570      1.26     pooka 		}
    571      1.21     pooka 		if (rv)
    572      1.18     pooka 			error = rv;
    573      1.18     pooka 		vput(vp);
    574      1.74        ad 		mutex_enter(&mntvnode_lock);
    575      1.18     pooka 	}
    576      1.74        ad 	mutex_exit(&mntvnode_lock);
    577      1.76     pooka 	vnfree(mvp);
    578      1.18     pooka 
    579      1.26     pooka 	return error;
    580      1.26     pooka }
    581      1.26     pooka 
    582      1.26     pooka int
    583      1.73     pooka puffs_vfsop_sync(struct mount *mp, int waitfor, struct kauth_cred *cred)
    584      1.26     pooka {
    585      1.63     pooka 	PUFFS_MSG_VARS(vfs, sync);
    586      1.62     pooka 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    587      1.26     pooka 	int error, rv;
    588      1.26     pooka 
    589      1.71     pooka 	error = pageflush(mp, cred, waitfor, 0);
    590      1.26     pooka 
    591      1.18     pooka 	/* sync fs */
    592      1.63     pooka 	PUFFS_MSG_ALLOC(vfs, sync);
    593      1.63     pooka 	sync_msg->pvfsr_waitfor = waitfor;
    594      1.63     pooka 	puffs_credcvt(&sync_msg->pvfsr_cred, cred);
    595      1.69     pooka 	puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL);
    596       1.1     pooka 
    597      1.69     pooka 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv);
    598      1.62     pooka 	rv = checkerr(pmp, rv, __func__);
    599      1.18     pooka 	if (rv)
    600      1.18     pooka 		error = rv;
    601       1.1     pooka 
    602      1.63     pooka 	PUFFS_MSG_RELEASE(sync);
    603       1.1     pooka 	return error;
    604       1.1     pooka }
    605       1.1     pooka 
    606       1.1     pooka int
    607      1.73     pooka puffs_vfsop_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    608       1.1     pooka {
    609      1.63     pooka 	PUFFS_MSG_VARS(vfs, fhtonode);
    610      1.33     pooka 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    611      1.33     pooka 	struct vnode *vp;
    612      1.54     pooka 	void *fhdata;
    613      1.54     pooka 	size_t argsize, fhlen;
    614      1.33     pooka 	int error;
    615      1.33     pooka 
    616      1.38     pooka 	if (pmp->pmp_args.pa_fhsize == 0)
    617      1.33     pooka 		return EOPNOTSUPP;
    618      1.33     pooka 
    619      1.54     pooka 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    620      1.54     pooka 		fhlen = fhp->fid_len;
    621      1.54     pooka 		fhdata = fhp;
    622      1.39     pooka 	} else {
    623      1.54     pooka 		fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
    624      1.54     pooka 		fhdata = fhp->fid_data;
    625      1.54     pooka 
    626      1.54     pooka 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    627      1.54     pooka 			if (pmp->pmp_args.pa_fhsize < fhlen)
    628      1.54     pooka 				return EINVAL;
    629      1.54     pooka 		} else {
    630      1.54     pooka 			if (pmp->pmp_args.pa_fhsize != fhlen)
    631      1.54     pooka 				return EINVAL;
    632      1.54     pooka 		}
    633      1.39     pooka 	}
    634      1.33     pooka 
    635      1.63     pooka 	argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
    636      1.75     pooka 	puffs_msgmem_alloc(argsize, &park_fhtonode, (void *)&fhtonode_msg, 1);
    637      1.63     pooka 	fhtonode_msg->pvfsr_dsize = fhlen;
    638      1.63     pooka 	memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
    639      1.69     pooka 	puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL);
    640      1.33     pooka 
    641      1.69     pooka 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error);
    642      1.60     pooka 	error = checkerr(pmp, error, __func__);
    643      1.33     pooka 	if (error)
    644      1.38     pooka 		goto out;
    645      1.33     pooka 
    646      1.63     pooka 	error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp);
    647      1.33     pooka 	DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
    648      1.63     pooka 	    fhtonode_msg->pvfsr_fhcookie, vp));
    649      1.59     pooka 	if (error == PUFFS_NOSUCHCOOKIE) {
    650      1.63     pooka 		error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
    651      1.63     pooka 		    fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
    652      1.63     pooka 		    fhtonode_msg->pvfsr_rdev, &vp);
    653      1.33     pooka 		if (error)
    654      1.38     pooka 			goto out;
    655      1.33     pooka 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    656      1.59     pooka 	} else if (error) {
    657      1.59     pooka 		goto out;
    658      1.33     pooka 	}
    659       1.1     pooka 
    660      1.33     pooka 	*vpp = vp;
    661      1.38     pooka  out:
    662      1.63     pooka 	puffs_msgmem_release(park_fhtonode);
    663      1.38     pooka 	return error;
    664       1.1     pooka }
    665       1.1     pooka 
    666       1.1     pooka int
    667      1.73     pooka puffs_vfsop_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    668       1.1     pooka {
    669      1.63     pooka 	PUFFS_MSG_VARS(vfs, nodetofh);
    670      1.33     pooka 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    671      1.54     pooka 	size_t argsize, fhlen;
    672      1.33     pooka 	int error;
    673      1.33     pooka 
    674      1.38     pooka 	if (pmp->pmp_args.pa_fhsize == 0)
    675      1.33     pooka 		return EOPNOTSUPP;
    676      1.33     pooka 
    677      1.54     pooka 	/* if file handles are static len, we can test len immediately */
    678      1.38     pooka 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    679      1.54     pooka 	    && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
    680      1.38     pooka 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    681      1.38     pooka 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    682      1.33     pooka 		return E2BIG;
    683      1.33     pooka 	}
    684       1.1     pooka 
    685      1.54     pooka 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    686      1.54     pooka 		fhlen = *fh_size;
    687      1.54     pooka 	else
    688      1.54     pooka 		fhlen = PUFFS_FROMFHSIZE(*fh_size);
    689      1.54     pooka 
    690      1.63     pooka 	argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
    691      1.75     pooka 	puffs_msgmem_alloc(argsize, &park_nodetofh, (void *)&nodetofh_msg, 1);
    692      1.63     pooka 	nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
    693      1.63     pooka 	nodetofh_msg->pvfsr_dsize = fhlen;
    694      1.69     pooka 	puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL);
    695      1.38     pooka 
    696      1.69     pooka 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error);
    697      1.60     pooka 	error = checkerr(pmp, error, __func__);
    698      1.54     pooka 
    699      1.54     pooka 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    700      1.63     pooka 		fhlen = nodetofh_msg->pvfsr_dsize;
    701      1.54     pooka 	else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
    702      1.63     pooka 		fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
    703      1.54     pooka 	else
    704      1.54     pooka 		fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    705      1.54     pooka 
    706      1.38     pooka 	if (error) {
    707      1.38     pooka 		if (error == E2BIG)
    708      1.54     pooka 			*fh_size = fhlen;
    709      1.38     pooka 		goto out;
    710      1.38     pooka 	}
    711      1.38     pooka 
    712      1.54     pooka 	if (fhlen > FHANDLE_SIZE_MAX) {
    713      1.69     pooka 		puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG,
    714      1.60     pooka 		    "file handle too big", VPTOPNC(vp));
    715      1.58     pooka 		error = EPROTO;
    716      1.38     pooka 		goto out;
    717      1.38     pooka 	}
    718      1.33     pooka 
    719      1.54     pooka 	if (*fh_size < fhlen) {
    720      1.54     pooka 		*fh_size = fhlen;
    721      1.38     pooka 		error = E2BIG;
    722      1.38     pooka 		goto out;
    723      1.38     pooka 	}
    724      1.54     pooka 	*fh_size = fhlen;
    725       1.1     pooka 
    726      1.38     pooka 	if (fhp) {
    727      1.54     pooka 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    728      1.63     pooka 			memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
    729      1.54     pooka 		} else {
    730      1.54     pooka 			fhp->fid_len = *fh_size;
    731      1.63     pooka 			memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
    732      1.63     pooka 			    nodetofh_msg->pvfsr_dsize);
    733      1.54     pooka 		}
    734      1.38     pooka 	}
    735       1.1     pooka 
    736      1.38     pooka  out:
    737      1.63     pooka 	puffs_msgmem_release(park_nodetofh);
    738      1.38     pooka 	return error;
    739       1.1     pooka }
    740       1.1     pooka 
    741       1.1     pooka void
    742      1.73     pooka puffs_vfsop_init()
    743       1.1     pooka {
    744       1.1     pooka 
    745      1.56     pooka 	/* some checks depend on this */
    746      1.56     pooka 	KASSERT(VNOVAL == VSIZENOTSET);
    747      1.56     pooka 
    748      1.31     pooka 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    749      1.31     pooka 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    750      1.31     pooka 	puffs_msgif_init();
    751       1.1     pooka }
    752       1.1     pooka 
    753       1.1     pooka void
    754      1.73     pooka puffs_vfsop_done()
    755       1.1     pooka {
    756       1.1     pooka 
    757      1.31     pooka 	puffs_msgif_destroy();
    758      1.31     pooka 	pool_destroy(&puffs_pnpool);
    759       1.1     pooka }
    760       1.1     pooka 
    761       1.1     pooka int
    762      1.73     pooka puffs_vfsop_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    763       1.1     pooka {
    764       1.1     pooka 
    765       1.1     pooka 	return EOPNOTSUPP;
    766       1.1     pooka }
    767       1.1     pooka 
    768      1.26     pooka int
    769      1.73     pooka puffs_vfsop_suspendctl(struct mount *mp, int cmd)
    770      1.26     pooka {
    771      1.63     pooka 	PUFFS_MSG_VARS(vfs, suspend);
    772      1.26     pooka 	struct puffs_mount *pmp;
    773      1.26     pooka 	int error;
    774      1.26     pooka 
    775      1.26     pooka 	pmp = MPTOPUFFSMP(mp);
    776      1.26     pooka 	switch (cmd) {
    777      1.26     pooka 	case SUSPEND_SUSPEND:
    778      1.26     pooka 		DPRINTF(("puffs_suspendctl: suspending\n"));
    779      1.27   hannken 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
    780      1.26     pooka 			break;
    781      1.65     pooka 		PUFFS_MSG_ALLOC(vfs, suspend);
    782      1.65     pooka 		puffs_msg_setfaf(park_suspend);
    783      1.65     pooka 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_START;
    784      1.69     pooka 		puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    785      1.69     pooka 		    PUFFS_VFS_SUSPEND, NULL);
    786      1.69     pooka 
    787      1.69     pooka 		puffs_msg_enqueue(pmp, park_suspend);
    788      1.65     pooka 		PUFFS_MSG_RELEASE(suspend);
    789      1.26     pooka 
    790      1.71     pooka 		error = pageflush(mp, FSCRED, 0, 1);
    791      1.26     pooka 		if (error == 0)
    792      1.27   hannken 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
    793      1.26     pooka 
    794      1.26     pooka 		if (error != 0) {
    795      1.65     pooka 			PUFFS_MSG_ALLOC(vfs, suspend);
    796      1.65     pooka 			puffs_msg_setfaf(park_suspend);
    797      1.65     pooka 			suspend_msg->pvfsr_status = PUFFS_SUSPEND_ERROR;
    798      1.69     pooka 			puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    799      1.69     pooka 			    PUFFS_VFS_SUSPEND, NULL);
    800      1.69     pooka 
    801      1.69     pooka 			puffs_msg_enqueue(pmp, park_suspend);
    802      1.65     pooka 			PUFFS_MSG_RELEASE(suspend);
    803      1.27   hannken 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    804      1.26     pooka 			break;
    805      1.26     pooka 		}
    806      1.26     pooka 
    807      1.65     pooka 		PUFFS_MSG_ALLOC(vfs, suspend);
    808      1.65     pooka 		puffs_msg_setfaf(park_suspend);
    809      1.65     pooka 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_SUSPENDED;
    810      1.69     pooka 		puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    811      1.69     pooka 		    PUFFS_VFS_SUSPEND, NULL);
    812      1.69     pooka 
    813      1.69     pooka 		puffs_msg_enqueue(pmp, park_suspend);
    814      1.65     pooka 		PUFFS_MSG_RELEASE(suspend);
    815      1.26     pooka 
    816      1.26     pooka 		break;
    817      1.26     pooka 
    818      1.26     pooka 	case SUSPEND_RESUME:
    819      1.26     pooka 		DPRINTF(("puffs_suspendctl: resume\n"));
    820      1.26     pooka 		error = 0;
    821      1.27   hannken 		(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    822      1.65     pooka 		PUFFS_MSG_ALLOC(vfs, suspend);
    823      1.65     pooka 		puffs_msg_setfaf(park_suspend);
    824      1.65     pooka 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_RESUME;
    825      1.69     pooka 		puffs_msg_setinfo(park_suspend, PUFFSOP_VFS,
    826      1.69     pooka 		    PUFFS_VFS_SUSPEND, NULL);
    827      1.69     pooka 
    828      1.69     pooka 		puffs_msg_enqueue(pmp, park_suspend);
    829      1.65     pooka 		PUFFS_MSG_RELEASE(suspend);
    830      1.26     pooka 		break;
    831      1.26     pooka 
    832      1.26     pooka 	default:
    833      1.26     pooka 		error = EINVAL;
    834      1.26     pooka 		break;
    835      1.26     pooka 	}
    836      1.26     pooka 
    837      1.26     pooka 	DPRINTF(("puffs_suspendctl: return %d\n", error));
    838      1.26     pooka 	return error;
    839      1.26     pooka }
    840      1.26     pooka 
    841  1.81.8.2       riz int
    842  1.81.8.2       riz puffs_vfsop_extattrctl(struct mount *mp, int cmd, struct vnode *vp,
    843  1.81.8.2       riz 	int attrnamespace, const char *attrname)
    844  1.81.8.2       riz {
    845  1.81.8.2       riz 	PUFFS_MSG_VARS(vfs, extattrctl);
    846  1.81.8.2       riz 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    847  1.81.8.2       riz 	struct puffs_node *pnp;
    848  1.81.8.2       riz 	puffs_cookie_t pnc;
    849  1.81.8.2       riz 	int error, flags;
    850  1.81.8.2       riz 
    851  1.81.8.2       riz 	if (vp) {
    852  1.81.8.2       riz 		/* doesn't make sense for puffs servers */
    853  1.81.8.2       riz 		if (vp->v_mount != mp)
    854  1.81.8.2       riz 			return EXDEV;
    855  1.81.8.2       riz 		pnp = vp->v_data;
    856  1.81.8.2       riz 		pnc = pnp->pn_cookie;
    857  1.81.8.2       riz 		flags = PUFFS_EXTATTRCTL_HASNODE;
    858  1.81.8.2       riz 	} else {
    859  1.81.8.2       riz 		pnp = pnc = NULL;
    860  1.81.8.2       riz 		flags = 0;
    861  1.81.8.2       riz 	}
    862  1.81.8.2       riz 
    863  1.81.8.2       riz 	PUFFS_MSG_ALLOC(vfs, extattrctl);
    864  1.81.8.2       riz 	extattrctl_msg->pvfsr_cmd = cmd;
    865  1.81.8.2       riz 	extattrctl_msg->pvfsr_attrnamespace = attrnamespace;
    866  1.81.8.2       riz 	extattrctl_msg->pvfsr_flags = flags;
    867  1.81.8.2       riz 	if (attrname) {
    868  1.81.8.2       riz 		strlcpy(extattrctl_msg->pvfsr_attrname, attrname,
    869  1.81.8.2       riz 		    sizeof(extattrctl_msg->pvfsr_attrname));
    870  1.81.8.2       riz 		extattrctl_msg->pvfsr_flags |= PUFFS_EXTATTRCTL_HASATTRNAME;
    871  1.81.8.2       riz 	}
    872  1.81.8.2       riz 	puffs_msg_setinfo(park_extattrctl,
    873  1.81.8.2       riz 	    PUFFSOP_VFS, PUFFS_VFS_EXTATTRCTL, pnc);
    874  1.81.8.2       riz 
    875  1.81.8.2       riz 	puffs_msg_enqueue(pmp, park_extattrctl);
    876  1.81.8.2       riz 	if (vp) {
    877  1.81.8.2       riz 		mutex_enter(&pnp->pn_mtx);
    878  1.81.8.2       riz 		puffs_referencenode(pnp);
    879  1.81.8.2       riz 		mutex_exit(&pnp->pn_mtx);
    880  1.81.8.2       riz 		VOP_UNLOCK(vp, 0);
    881  1.81.8.2       riz 	}
    882  1.81.8.2       riz 	error = puffs_msg_wait2(pmp, park_extattrctl, pnp, NULL);
    883  1.81.8.2       riz 	PUFFS_MSG_RELEASE(extattrctl);
    884  1.81.8.2       riz 	if (vp) {
    885  1.81.8.2       riz 		puffs_releasenode(pnp);
    886  1.81.8.2       riz 	}
    887  1.81.8.2       riz 
    888  1.81.8.2       riz 	return checkerr(pmp, error, __func__);
    889  1.81.8.2       riz }
    890  1.81.8.2       riz 
    891       1.1     pooka const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    892       1.1     pooka 	&puffs_vnodeop_opv_desc,
    893       1.3     pooka 	&puffs_specop_opv_desc,
    894       1.4     pooka 	&puffs_fifoop_opv_desc,
    895      1.12     pooka 	&puffs_msgop_opv_desc,
    896       1.1     pooka 	NULL,
    897       1.1     pooka };
    898       1.1     pooka 
    899       1.1     pooka struct vfsops puffs_vfsops = {
    900       1.1     pooka 	MOUNT_PUFFS,
    901      1.48       dsl 	sizeof (struct puffs_kargs),
    902      1.73     pooka 	puffs_vfsop_mount,		/* mount	*/
    903      1.73     pooka 	puffs_vfsop_start,		/* start	*/
    904      1.73     pooka 	puffs_vfsop_unmount,		/* unmount	*/
    905      1.73     pooka 	puffs_vfsop_root,		/* root		*/
    906      1.73     pooka 	(void *)eopnotsupp,		/* quotactl	*/
    907      1.73     pooka 	puffs_vfsop_statvfs,		/* statvfs	*/
    908      1.73     pooka 	puffs_vfsop_sync,		/* sync		*/
    909      1.73     pooka 	(void *)eopnotsupp,		/* vget		*/
    910      1.73     pooka 	puffs_vfsop_fhtovp,		/* fhtovp	*/
    911      1.73     pooka 	puffs_vfsop_vptofh,		/* vptofh	*/
    912      1.73     pooka 	puffs_vfsop_init,		/* init		*/
    913      1.73     pooka 	NULL,				/* reinit	*/
    914      1.73     pooka 	puffs_vfsop_done,		/* done		*/
    915      1.73     pooka 	NULL,				/* mountroot	*/
    916      1.73     pooka 	puffs_vfsop_snapshot,		/* snapshot	*/
    917  1.81.8.2       riz 	puffs_vfsop_extattrctl,		/* extattrctl	*/
    918      1.73     pooka 	puffs_vfsop_suspendctl,		/* suspendctl	*/
    919      1.78  dholland 	genfs_renamelock_enter,
    920      1.78  dholland 	genfs_renamelock_exit,
    921      1.79        ad 	(void *)eopnotsupp,
    922      1.73     pooka 	puffs_vnodeopv_descs,		/* vnodeops	*/
    923      1.73     pooka 	0,				/* refcount	*/
    924       1.1     pooka 	{ NULL, NULL }
    925       1.1     pooka };
    926      1.80    rumble 
    927      1.80    rumble static int
    928      1.80    rumble puffs_modcmd(modcmd_t cmd, void *arg)
    929      1.80    rumble {
    930      1.80    rumble 
    931      1.80    rumble 	switch (cmd) {
    932      1.80    rumble 	case MODULE_CMD_INIT:
    933      1.80    rumble 		return vfs_attach(&puffs_vfsops);
    934      1.80    rumble 	case MODULE_CMD_FINI:
    935      1.80    rumble 		return vfs_detach(&puffs_vfsops);
    936      1.80    rumble 	default:
    937      1.80    rumble 		return ENOTTY;
    938      1.80    rumble 	}
    939      1.80    rumble }
    940