Home | History | Annotate | Line # | Download | only in puffs
puffs_vfsops.c revision 1.28.6.12
      1  1.28.6.12       ad /*	$NetBSD: puffs_vfsops.c,v 1.28.6.12 2007/10/28 22:01:25 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  *
     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.28.6.12       ad __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.28.6.12 2007/10/28 22:01:25 ad 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.28.6.3       ad #include <sys/proc.h>
     45        1.1    pooka 
     46        1.1    pooka #include <lib/libkern/libkern.h>
     47        1.1    pooka 
     48        1.1    pooka #include <fs/puffs/puffs_msgif.h>
     49        1.1    pooka #include <fs/puffs/puffs_sys.h>
     50        1.1    pooka 
     51   1.28.6.5       ad #include <nfs/nfsproto.h> /* for fh sizes */
     52   1.28.6.5       ad 
     53        1.1    pooka VFS_PROTOS(puffs);
     54        1.1    pooka 
     55   1.28.6.4       ad MALLOC_JUSTDEFINE(M_PUFFS, "puffs", "Pass-to-Userspace Framework File System");
     56        1.1    pooka 
     57       1.22    pooka #ifndef PUFFS_PNODEBUCKETS
     58       1.22    pooka #define PUFFS_PNODEBUCKETS 256
     59       1.22    pooka #endif
     60       1.22    pooka #ifndef PUFFS_MAXPNODEBUCKETS
     61   1.28.6.5       ad #define PUFFS_MAXPNODEBUCKETS 8192
     62       1.22    pooka #endif
     63   1.28.6.5       ad int puffs_pnodebuckets_default = PUFFS_PNODEBUCKETS;
     64   1.28.6.5       ad int puffs_maxpnodebuckets = PUFFS_MAXPNODEBUCKETS;
     65       1.22    pooka 
     66        1.1    pooka int
     67   1.28.6.8       ad puffs_mount(struct mount *mp, const char *path, void *data, size_t *data_len,
     68   1.28.6.9       ad 	    struct lwp *l)
     69        1.1    pooka {
     70       1.22    pooka 	struct puffs_mount *pmp = NULL;
     71   1.28.6.5       ad 	struct puffs_kargs *args;
     72   1.28.6.9       ad 	char fstype[_VFS_NAMELEN];
     73   1.28.6.9       ad 	char *p;
     74       1.22    pooka 	int error = 0, i;
     75        1.1    pooka 
     76   1.28.6.8       ad 	if (*data_len < sizeof *args)
     77   1.28.6.8       ad 		return EINVAL;
     78   1.28.6.8       ad 
     79        1.1    pooka 	if (mp->mnt_flag & MNT_GETARGS) {
     80        1.1    pooka 		pmp = MPTOPUFFSMP(mp);
     81   1.28.6.8       ad 		*(struct puffs_kargs *)data = pmp->pmp_args;
     82   1.28.6.8       ad 		*data_len = sizeof *args;
     83   1.28.6.8       ad 		return 0;
     84        1.1    pooka 	}
     85        1.1    pooka 
     86        1.1    pooka 	/* update is not supported currently */
     87        1.1    pooka 	if (mp->mnt_flag & MNT_UPDATE)
     88        1.1    pooka 		return EOPNOTSUPP;
     89        1.1    pooka 
     90        1.1    pooka 	/*
     91        1.1    pooka 	 * We need the file system name
     92        1.1    pooka 	 */
     93        1.1    pooka 	if (!data)
     94        1.1    pooka 		return EINVAL;
     95        1.1    pooka 
     96   1.28.6.5       ad 	MALLOC(args, struct puffs_kargs *, sizeof(struct puffs_kargs),
     97       1.17    pooka 	    M_PUFFS, M_WAITOK);
     98       1.17    pooka 
     99   1.28.6.8       ad 	*args = *(struct puffs_kargs *)data;
    100       1.17    pooka 
    101       1.17    pooka 	/* devel phase */
    102       1.17    pooka 	if (args->pa_vers != (PUFFSVERSION | PUFFSDEVELVERS)) {
    103  1.28.6.11       ad 		printf("puffs_mount: development version mismatch: "
    104  1.28.6.11       ad 		    "kernel %d, lib %d\n",
    105  1.28.6.11       ad 		    PUFFSVERSION, args->pa_vers & ~PUFFSDEVELVERS);
    106       1.17    pooka 		error = EINVAL;
    107       1.17    pooka 		goto out;
    108       1.17    pooka 	}
    109        1.1    pooka 
    110  1.28.6.10       ad 	if ((args->pa_flags & ~PUFFS_KFLAG_MASK) != 0) {
    111  1.28.6.10       ad 		printf("puffs_mount: invalid KFLAGs 0x%x\n", args->pa_flags);
    112  1.28.6.10       ad 		error = EINVAL;
    113  1.28.6.10       ad 		goto out;
    114  1.28.6.10       ad 	}
    115  1.28.6.10       ad 	if ((args->pa_fhflags & ~PUFFS_FHFLAG_MASK) != 0) {
    116  1.28.6.10       ad 		printf("puffs_mount: invalid FHFLAGs 0x%x\n", args->pa_fhflags);
    117  1.28.6.10       ad 		error = EINVAL;
    118  1.28.6.10       ad 		goto out;
    119  1.28.6.10       ad 	}
    120  1.28.6.10       ad 
    121  1.28.6.10       ad 	/* use dummy value for passthrough */
    122  1.28.6.10       ad 	if (args->pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    123  1.28.6.10       ad 		args->pa_fhsize = sizeof(struct fid);
    124        1.8    pooka 
    125   1.28.6.5       ad 	/* sanitize file handle length */
    126   1.28.6.5       ad 	if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) {
    127   1.28.6.5       ad 		printf("puffs_mount: handle size %zu too large\n",
    128   1.28.6.5       ad 		    args->pa_fhsize);
    129   1.28.6.5       ad 		error = EINVAL;
    130   1.28.6.5       ad 		goto out;
    131   1.28.6.5       ad 	}
    132   1.28.6.5       ad 	/* sanity check file handle max sizes */
    133   1.28.6.5       ad 	if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) {
    134   1.28.6.5       ad 		size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize);
    135   1.28.6.5       ad 
    136   1.28.6.5       ad 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) {
    137   1.28.6.5       ad 			if (NFSX_FHTOOBIG_P(kfhsize, 0)) {
    138   1.28.6.5       ad 				printf("puffs_mount: fhsize larger than "
    139   1.28.6.5       ad 				    "NFSv2 max %d\n",
    140   1.28.6.5       ad 				    PUFFS_FROMFHSIZE(NFSX_V2FH));
    141   1.28.6.5       ad 				error = EINVAL;
    142   1.28.6.5       ad 				goto out;
    143   1.28.6.5       ad 			}
    144   1.28.6.5       ad 		}
    145   1.28.6.5       ad 
    146   1.28.6.5       ad 		if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) {
    147   1.28.6.5       ad 			if (NFSX_FHTOOBIG_P(kfhsize, 1)) {
    148   1.28.6.5       ad 				printf("puffs_mount: fhsize larger than "
    149   1.28.6.5       ad 				    "NFSv3 max %d\n",
    150   1.28.6.5       ad 				    PUFFS_FROMFHSIZE(NFSX_V3FHMAX));
    151   1.28.6.5       ad 				error = EINVAL;
    152   1.28.6.5       ad 				goto out;
    153   1.28.6.5       ad 			}
    154   1.28.6.5       ad 		}
    155   1.28.6.5       ad 	}
    156   1.28.6.5       ad 
    157   1.28.6.9       ad 	/* don't allow non-printing characters (like my sweet umlauts.. snif) */
    158   1.28.6.9       ad 	args->pa_typename[sizeof(args->pa_typename)-1] = '\0';
    159   1.28.6.9       ad 	for (p = args->pa_typename; *p; p++)
    160   1.28.6.9       ad 		if (*p < ' ' || *p > '~')
    161   1.28.6.9       ad 			*p = '.';
    162   1.28.6.9       ad 
    163   1.28.6.9       ad 	args->pa_mntfromname[sizeof(args->pa_mntfromname)-1] = '\0';
    164   1.28.6.9       ad 	for (p = args->pa_mntfromname; *p; p++)
    165   1.28.6.9       ad 		if (*p < ' ' || *p > '~')
    166   1.28.6.9       ad 			*p = '.';
    167   1.28.6.9       ad 
    168        1.1    pooka 	/* build real name */
    169   1.28.6.9       ad 	(void)strlcpy(fstype, PUFFS_TYPEPREFIX, sizeof(fstype));
    170   1.28.6.9       ad 	(void)strlcat(fstype, args->pa_typename, sizeof(fstype));
    171        1.1    pooka 
    172        1.1    pooka 	/* inform user server if it got the max request size it wanted */
    173  1.28.6.11       ad 	if (args->pa_maxmsglen == 0 || args->pa_maxmsglen > PUFFS_MSG_MAXSIZE)
    174  1.28.6.11       ad 		args->pa_maxmsglen = PUFFS_MSG_MAXSIZE;
    175  1.28.6.11       ad 	else if (args->pa_maxmsglen < 2*PUFFS_MSGSTRUCT_MAX)
    176  1.28.6.11       ad 		args->pa_maxmsglen = 2*PUFFS_MSGSTRUCT_MAX;
    177  1.28.6.11       ad 
    178   1.28.6.9       ad 	(void)strlcpy(args->pa_typename, fstype, sizeof(args->pa_typename));
    179        1.1    pooka 
    180   1.28.6.5       ad 	if (args->pa_nhashbuckets == 0)
    181   1.28.6.5       ad 		args->pa_nhashbuckets = puffs_pnodebuckets_default;
    182   1.28.6.5       ad 	if (args->pa_nhashbuckets < 1)
    183   1.28.6.5       ad 		args->pa_nhashbuckets = 1;
    184   1.28.6.5       ad 	if (args->pa_nhashbuckets > PUFFS_MAXPNODEBUCKETS) {
    185   1.28.6.5       ad 		args->pa_nhashbuckets = puffs_maxpnodebuckets;
    186   1.28.6.5       ad 		printf("puffs_mount: using %d hash buckets. "
    187   1.28.6.5       ad 		    "adjust puffs_maxpnodebuckets for more\n",
    188   1.28.6.5       ad 		    puffs_maxpnodebuckets);
    189   1.28.6.5       ad 	}
    190   1.28.6.5       ad 
    191   1.28.6.9       ad 	error = set_statvfs_info(path, UIO_USERSPACE, args->pa_mntfromname,
    192   1.28.6.9       ad 	    UIO_SYSSPACE, fstype, mp, l);
    193        1.1    pooka 	if (error)
    194       1.17    pooka 		goto out;
    195       1.10    pooka 	mp->mnt_stat.f_iosize = DEV_BSIZE;
    196        1.1    pooka 
    197   1.28.6.5       ad 	/*
    198   1.28.6.5       ad 	 * We can't handle the VFS_STATVFS() mount_domount() does
    199   1.28.6.5       ad 	 * after VFS_MOUNT() because we'd deadlock, so handle it
    200   1.28.6.5       ad 	 * here already.
    201   1.28.6.5       ad 	 */
    202   1.28.6.5       ad 	copy_statvfs_info(&args->pa_svfsb, mp);
    203   1.28.6.5       ad 	(void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat));
    204   1.28.6.5       ad 
    205        1.1    pooka 	MALLOC(pmp, struct puffs_mount *, sizeof(struct puffs_mount),
    206        1.1    pooka 	    M_PUFFS, M_WAITOK | M_ZERO);
    207        1.1    pooka 
    208        1.6    pooka 	mp->mnt_fs_bshift = DEV_BSHIFT;
    209        1.6    pooka 	mp->mnt_dev_bshift = DEV_BSHIFT;
    210        1.6    pooka 	mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */
    211        1.1    pooka 	mp->mnt_data = pmp;
    212       1.26    pooka 	mp->mnt_iflag |= IMNT_HAS_TRANS;
    213        1.6    pooka 
    214        1.1    pooka 	pmp->pmp_status = PUFFSTAT_MOUNTING;
    215        1.1    pooka 	pmp->pmp_mp = mp;
    216  1.28.6.11       ad 	pmp->pmp_msg_maxsize = args->pa_maxmsglen;
    217       1.17    pooka 	pmp->pmp_args = *args;
    218        1.1    pooka 
    219   1.28.6.5       ad 	pmp->pmp_npnodehash = args->pa_nhashbuckets;
    220       1.22    pooka 	pmp->pmp_pnodehash = malloc
    221       1.22    pooka 	    (sizeof(struct puffs_pnode_hashlist *) * pmp->pmp_npnodehash,
    222       1.22    pooka 	    M_PUFFS, M_WAITOK);
    223       1.22    pooka 	for (i = 0; i < pmp->pmp_npnodehash; i++)
    224       1.22    pooka 		LIST_INIT(&pmp->pmp_pnodehash[i]);
    225  1.28.6.10       ad 	LIST_INIT(&pmp->pmp_newcookie);
    226       1.22    pooka 
    227        1.1    pooka 	/*
    228        1.1    pooka 	 * Inform the fileops processing code that we have a mountpoint.
    229        1.1    pooka 	 * If it doesn't know about anyone with our pid/fd having the
    230        1.1    pooka 	 * device open, punt
    231        1.1    pooka 	 */
    232       1.17    pooka 	if (puffs_setpmp(l->l_proc->p_pid, args->pa_fd, pmp)) {
    233       1.17    pooka 		error = ENOENT;
    234       1.17    pooka 		goto out;
    235        1.1    pooka 	}
    236        1.1    pooka 
    237   1.28.6.5       ad 	/* XXX: check parameters */
    238   1.28.6.5       ad 	pmp->pmp_root_cookie = args->pa_root_cookie;
    239   1.28.6.5       ad 	pmp->pmp_root_vtype = args->pa_root_vtype;
    240   1.28.6.5       ad 	pmp->pmp_root_vsize = args->pa_root_vsize;
    241   1.28.6.5       ad 	pmp->pmp_root_rdev = args->pa_root_rdev;
    242   1.28.6.5       ad 
    243   1.28.6.4       ad 	mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE);
    244  1.28.6.11       ad 	cv_init(&pmp->pmp_msg_waiter_cv, "puffsget");
    245   1.28.6.5       ad 	cv_init(&pmp->pmp_refcount_cv, "puffsref");
    246   1.28.6.4       ad 	cv_init(&pmp->pmp_unmounting_cv, "puffsum");
    247  1.28.6.11       ad 	TAILQ_INIT(&pmp->pmp_msg_touser);
    248  1.28.6.11       ad 	TAILQ_INIT(&pmp->pmp_msg_replywait);
    249        1.1    pooka 
    250        1.1    pooka 	DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n",
    251        1.1    pooka 	    mp, MPTOPUFFSMP(mp)));
    252        1.1    pooka 
    253        1.1    pooka 	vfs_getnewfsid(mp);
    254        1.1    pooka 
    255       1.17    pooka  out:
    256       1.22    pooka 	if (error && pmp && pmp->pmp_pnodehash)
    257       1.22    pooka 		free(pmp->pmp_pnodehash, M_PUFFS);
    258       1.22    pooka 	if (error && pmp)
    259       1.22    pooka 		FREE(pmp, M_PUFFS);
    260       1.17    pooka 	FREE(args, M_PUFFS);
    261       1.17    pooka 	return error;
    262        1.1    pooka }
    263        1.1    pooka 
    264        1.1    pooka int
    265   1.28.6.5       ad puffs_start(struct mount *mp, int flags, struct lwp *l)
    266        1.1    pooka {
    267   1.28.6.5       ad 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    268        1.1    pooka 
    269   1.28.6.5       ad 	KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING);
    270        1.1    pooka 	pmp->pmp_status = PUFFSTAT_RUNNING;
    271        1.1    pooka 
    272        1.1    pooka 	return 0;
    273        1.1    pooka }
    274        1.1    pooka 
    275        1.1    pooka int
    276        1.1    pooka puffs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    277        1.1    pooka {
    278  1.28.6.11       ad 	PUFFS_MSG_VARS(vfs, unmount);
    279        1.1    pooka 	struct puffs_mount *pmp;
    280        1.1    pooka 	int error, force;
    281        1.1    pooka 
    282        1.1    pooka 	error = 0;
    283        1.1    pooka 	force = mntflags & MNT_FORCE;
    284        1.1    pooka 	pmp = MPTOPUFFSMP(mp);
    285        1.1    pooka 
    286        1.1    pooka 	DPRINTF(("puffs_unmount: detach filesystem from vfs, current "
    287        1.1    pooka 	    "status 0x%x\n", pmp->pmp_status));
    288        1.1    pooka 
    289        1.1    pooka 	/*
    290        1.1    pooka 	 * flush all the vnodes.  VOP_RECLAIM() takes care that the
    291        1.1    pooka 	 * root vnode does not get flushed until unmount.  The
    292        1.1    pooka 	 * userspace root node cookie is stored in the mount
    293        1.1    pooka 	 * structure, so we can always re-instantiate a root vnode,
    294        1.1    pooka 	 * should userspace unmount decide it doesn't want to
    295        1.1    pooka 	 * cooperate.
    296        1.1    pooka 	 */
    297        1.1    pooka 	error = vflush(mp, NULLVP, force ? FORCECLOSE : 0);
    298        1.1    pooka 	if (error)
    299        1.1    pooka 		goto out;
    300        1.1    pooka 
    301        1.1    pooka 	/*
    302        1.1    pooka 	 * If we are not DYING, we should ask userspace's opinion
    303        1.1    pooka 	 * about the situation
    304        1.1    pooka 	 */
    305   1.28.6.4       ad 	mutex_enter(&pmp->pmp_lock);
    306        1.1    pooka 	if (pmp->pmp_status != PUFFSTAT_DYING) {
    307       1.16    pooka 		pmp->pmp_unmounting = 1;
    308   1.28.6.4       ad 		mutex_exit(&pmp->pmp_lock);
    309       1.16    pooka 
    310  1.28.6.11       ad 		PUFFS_MSG_ALLOC(vfs, unmount);
    311  1.28.6.11       ad 		unmount_msg->pvfsr_flags = mntflags;
    312  1.28.6.11       ad 		puffs_cidcvt(&unmount_msg->pvfsr_cid, l);
    313  1.28.6.11       ad 
    314  1.28.6.11       ad 		error = puffs_msg_vfs(pmp, park_unmount, PUFFS_VFS_UNMOUNT);
    315  1.28.6.11       ad 		PUFFS_MSG_RELEASE(unmount);
    316       1.16    pooka 
    317  1.28.6.10       ad 		error = checkerr(pmp, error, __func__);
    318       1.16    pooka 		DPRINTF(("puffs_unmount: error %d force %d\n", error, force));
    319       1.16    pooka 
    320   1.28.6.4       ad 		mutex_enter(&pmp->pmp_lock);
    321       1.16    pooka 		pmp->pmp_unmounting = 0;
    322   1.28.6.4       ad 		cv_broadcast(&pmp->pmp_unmounting_cv);
    323        1.1    pooka 	}
    324        1.1    pooka 
    325        1.1    pooka 	/*
    326        1.1    pooka 	 * if userspace cooperated or we really need to die,
    327        1.1    pooka 	 * screw what userland thinks and just die.
    328        1.1    pooka 	 */
    329        1.1    pooka 	if (error == 0 || force) {
    330       1.26    pooka 		/* tell waiters & other resources to go unwait themselves */
    331       1.26    pooka 		puffs_userdead(pmp);
    332        1.1    pooka 		puffs_nukebypmp(pmp);
    333       1.26    pooka 
    334       1.26    pooka 		/*
    335   1.28.6.5       ad 		 * Wait until there are no more users for the mount resource.
    336   1.28.6.5       ad 		 * Notice that this is hooked against transport_close
    337   1.28.6.5       ad 		 * and return from touser.  In an ideal world, it would
    338   1.28.6.5       ad 		 * be hooked against final return from all operations.
    339   1.28.6.5       ad 		 * But currently it works well enough, since nobody
    340   1.28.6.5       ad 		 * does weird blocking voodoo after return from touser().
    341       1.26    pooka 		 */
    342   1.28.6.5       ad 		while (pmp->pmp_refcount != 0)
    343   1.28.6.5       ad 			cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock);
    344   1.28.6.4       ad 		mutex_exit(&pmp->pmp_lock);
    345       1.26    pooka 
    346       1.26    pooka 		/* free resources now that we hopefully have no waiters left */
    347   1.28.6.4       ad 		cv_destroy(&pmp->pmp_unmounting_cv);
    348   1.28.6.5       ad 		cv_destroy(&pmp->pmp_refcount_cv);
    349  1.28.6.11       ad 		cv_destroy(&pmp->pmp_msg_waiter_cv);
    350   1.28.6.4       ad 		mutex_destroy(&pmp->pmp_lock);
    351   1.28.6.4       ad 
    352       1.22    pooka 		free(pmp->pmp_pnodehash, M_PUFFS);
    353        1.1    pooka 		FREE(pmp, M_PUFFS);
    354        1.1    pooka 		error = 0;
    355       1.16    pooka 	} else {
    356   1.28.6.4       ad 		mutex_exit(&pmp->pmp_lock);
    357        1.1    pooka 	}
    358        1.1    pooka 
    359        1.1    pooka  out:
    360        1.2    pooka 	DPRINTF(("puffs_unmount: return %d\n", error));
    361        1.1    pooka 	return error;
    362        1.1    pooka }
    363        1.1    pooka 
    364        1.1    pooka /*
    365        1.1    pooka  * This doesn't need to travel to userspace
    366        1.1    pooka  */
    367        1.1    pooka int
    368        1.1    pooka puffs_root(struct mount *mp, struct vnode **vpp)
    369        1.1    pooka {
    370   1.28.6.8       ad 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    371  1.28.6.10       ad 	int rv;
    372        1.1    pooka 
    373  1.28.6.10       ad 	rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, 1, 1, vpp);
    374  1.28.6.10       ad 	KASSERT(rv != PUFFS_NOSUCHCOOKIE);
    375  1.28.6.10       ad 	return rv;
    376        1.1    pooka }
    377        1.1    pooka 
    378        1.1    pooka int
    379        1.1    pooka puffs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    380        1.1    pooka {
    381  1.28.6.11       ad 	PUFFS_MSG_VARS(vfs, statvfs);
    382        1.1    pooka 	struct puffs_mount *pmp;
    383        1.1    pooka 	int error = 0;
    384        1.1    pooka 
    385        1.1    pooka 	pmp = MPTOPUFFSMP(mp);
    386        1.1    pooka 
    387        1.1    pooka 	/*
    388        1.1    pooka 	 * If we are mounting, it means that the userspace counterpart
    389        1.1    pooka 	 * is calling mount(2), but mount(2) also calls statvfs.  So
    390        1.1    pooka 	 * requesting statvfs from userspace would mean a deadlock.
    391        1.1    pooka 	 * Compensate.
    392        1.1    pooka 	 */
    393        1.1    pooka 	if (pmp->pmp_status == PUFFSTAT_MOUNTING)
    394        1.1    pooka 		return EINPROGRESS;
    395        1.1    pooka 
    396  1.28.6.11       ad 	PUFFS_MSG_ALLOC(vfs, statvfs);
    397  1.28.6.11       ad 	puffs_cidcvt(&statvfs_msg->pvfsr_cid, l);
    398        1.1    pooka 
    399  1.28.6.11       ad 	error = puffs_msg_vfs(pmp, park_statvfs, PUFFS_VFS_STATVFS);
    400  1.28.6.10       ad 	error = checkerr(pmp, error, __func__);
    401  1.28.6.11       ad 	statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE;
    402       1.10    pooka 
    403        1.1    pooka 	/*
    404        1.1    pooka 	 * Try to produce a sensible result even in the event
    405        1.1    pooka 	 * of userspace error.
    406        1.1    pooka 	 *
    407        1.1    pooka 	 * XXX: cache the copy in non-error case
    408        1.1    pooka 	 */
    409        1.1    pooka 	if (!error) {
    410  1.28.6.11       ad 		copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp);
    411  1.28.6.11       ad 		(void)memcpy(sbp, &statvfs_msg->pvfsr_sb,
    412        1.1    pooka 		    sizeof(struct statvfs));
    413        1.1    pooka 	} else {
    414        1.1    pooka 		copy_statvfs_info(sbp, mp);
    415        1.1    pooka 	}
    416        1.1    pooka 
    417  1.28.6.11       ad 	PUFFS_MSG_RELEASE(statvfs);
    418       1.14    pooka 	return error;
    419        1.1    pooka }
    420        1.1    pooka 
    421       1.26    pooka static int
    422   1.28.6.4       ad pageflush(struct mount *mp, kauth_cred_t cred,
    423   1.28.6.4       ad 	int waitfor, int suspending, struct lwp *l)
    424        1.1    pooka {
    425       1.26    pooka 	struct puffs_node *pn;
    426  1.28.6.12       ad 	struct vnode *vp, *mvp;
    427   1.28.6.4       ad 	int error, rv;
    428        1.1    pooka 
    429       1.26    pooka 	KASSERT(((waitfor == MNT_WAIT) && suspending) == 0);
    430       1.26    pooka 	KASSERT((suspending == 0)
    431       1.26    pooka 	    || (fstrans_is_owner(mp)
    432       1.27  hannken 	      && fstrans_getstate(mp) == FSTRANS_SUSPENDING));
    433        1.1    pooka 
    434       1.18    pooka 	error = 0;
    435       1.18    pooka 
    436  1.28.6.12       ad 	/* Allocate a marker vnode. */
    437  1.28.6.12       ad 	if ((mvp = valloc(mp)) == NULL)
    438  1.28.6.12       ad 		return ENOMEM;
    439  1.28.6.12       ad 
    440       1.18    pooka 	/*
    441       1.24    pooka 	 * Sync all cached data from regular vnodes (which are not
    442       1.24    pooka 	 * currently locked, see below).  After this we call VFS_SYNC
    443       1.24    pooka 	 * for the fs server, which should handle data and metadata for
    444       1.24    pooka 	 * all the nodes it knows to exist.
    445       1.18    pooka 	 */
    446   1.28.6.2       ad 	mutex_enter(&mntvnode_lock);
    447       1.18    pooka  loop:
    448  1.28.6.12       ad 	for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
    449  1.28.6.12       ad 		vmark(mvp, vp);
    450  1.28.6.12       ad 		if (vp->v_mount != mp || vismarker(vp))
    451  1.28.6.12       ad 			continue;
    452       1.18    pooka 
    453   1.28.6.2       ad 		mutex_enter(&vp->v_interlock);
    454       1.26    pooka 		pn = VPTOPP(vp);
    455       1.19    pooka 		if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) {
    456   1.28.6.2       ad 			mutex_exit(&vp->v_interlock);
    457       1.18    pooka 			continue;
    458       1.18    pooka 		}
    459       1.18    pooka 
    460   1.28.6.2       ad 		mutex_exit(&mntvnode_lock);
    461       1.21    pooka 
    462       1.21    pooka 		/*
    463       1.21    pooka 		 * Here we try to get a reference to the vnode and to
    464       1.21    pooka 		 * lock it.  This is mostly cargo-culted, but I will
    465       1.21    pooka 		 * offer an explanation to why I believe this might
    466       1.21    pooka 		 * actually do the right thing.
    467       1.21    pooka 		 *
    468       1.21    pooka 		 * If the vnode is a goner, we quite obviously don't need
    469       1.21    pooka 		 * to sync it.
    470       1.21    pooka 		 *
    471       1.21    pooka 		 * If the vnode was busy, we don't need to sync it because
    472       1.21    pooka 		 * this is never called with MNT_WAIT except from
    473       1.21    pooka 		 * dounmount(), when we are wait-flushing all the dirty
    474       1.21    pooka 		 * vnodes through other routes in any case.  So there,
    475       1.21    pooka 		 * sync() doesn't actually sync.  Happy now?
    476       1.26    pooka 		 *
    477       1.26    pooka 		 * NOTE: if we're suspending, vget() does NOT lock.
    478       1.26    pooka 		 * See puffs_lock() for details.
    479       1.21    pooka 		 */
    480       1.18    pooka 		rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
    481       1.18    pooka 		if (rv) {
    482   1.28.6.2       ad 			mutex_enter(&mntvnode_lock);
    483  1.28.6.12       ad 			if (rv == ENOENT) {
    484  1.28.6.12       ad 				(void)vunmark(mvp);
    485       1.18    pooka 				goto loop;
    486  1.28.6.12       ad 			}
    487       1.18    pooka 			continue;
    488       1.18    pooka 		}
    489       1.18    pooka 
    490       1.26    pooka 		/*
    491       1.26    pooka 		 * Thread information to puffs_strategy() through the
    492       1.26    pooka 		 * pnode flags: we want to issue the putpages operations
    493       1.26    pooka 		 * as FAF if we're suspending, since it's very probable
    494       1.26    pooka 		 * that our execution context is that of the userspace
    495       1.26    pooka 		 * daemon.  We can do this because:
    496       1.26    pooka 		 *   + we send the "going to suspend" prior to this part
    497       1.26    pooka 		 *   + if any of the writes fails in userspace, it's the
    498       1.26    pooka 		 *     file system server's problem to decide if this was a
    499       1.26    pooka 		 *     failed snapshot when it gets the "snapshot complete"
    500       1.26    pooka 		 *     notification.
    501       1.26    pooka 		 *   + if any of the writes fail in the kernel already, we
    502       1.26    pooka 		 *     immediately fail *and* notify the user server of
    503       1.26    pooka 		 *     failure.
    504       1.26    pooka 		 *
    505       1.26    pooka 		 * We also do FAFs if we're called from the syncer.  This
    506       1.26    pooka 		 * is just general optimization for trickle sync: no need
    507       1.26    pooka 		 * to really guarantee that the stuff ended on backing
    508       1.26    pooka 		 * storage.
    509       1.26    pooka 		 * TODO: Maybe also hint the user server of this twist?
    510       1.26    pooka 		 */
    511   1.28.6.4       ad 		if (suspending || waitfor == MNT_LAZY) {
    512   1.28.6.4       ad 			mutex_enter(&vp->v_interlock);
    513       1.26    pooka 			pn->pn_stat |= PNODE_SUSPEND;
    514   1.28.6.4       ad 			mutex_exit(&vp->v_interlock);
    515   1.28.6.4       ad 		}
    516   1.28.6.4       ad 		rv = VOP_FSYNC(vp, cred, waitfor, 0, 0, l);
    517       1.26    pooka 		if (suspending || waitfor == MNT_LAZY) {
    518   1.28.6.2       ad 			mutex_enter(&vp->v_interlock);
    519       1.26    pooka 			pn->pn_stat &= ~PNODE_SUSPEND;
    520   1.28.6.2       ad 			mutex_exit(&vp->v_interlock);
    521       1.26    pooka 		}
    522       1.21    pooka 		if (rv)
    523       1.18    pooka 			error = rv;
    524       1.18    pooka 		vput(vp);
    525   1.28.6.2       ad 		mutex_enter(&mntvnode_lock);
    526       1.18    pooka 	}
    527   1.28.6.2       ad 	mutex_exit(&mntvnode_lock);
    528  1.28.6.12       ad 	vfree(mvp);
    529       1.18    pooka 
    530       1.26    pooka 	return error;
    531       1.26    pooka }
    532       1.26    pooka 
    533       1.26    pooka int
    534       1.26    pooka puffs_sync(struct mount *mp, int waitfor, struct kauth_cred *cred,
    535       1.26    pooka 	struct lwp *l)
    536       1.26    pooka {
    537  1.28.6.11       ad 	PUFFS_MSG_VARS(vfs, sync);
    538  1.28.6.11       ad 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    539       1.26    pooka 	int error, rv;
    540       1.26    pooka 
    541   1.28.6.4       ad 	error = pageflush(mp, cred, waitfor, 0, l);
    542       1.26    pooka 
    543       1.18    pooka 	/* sync fs */
    544  1.28.6.11       ad 	PUFFS_MSG_ALLOC(vfs, sync);
    545  1.28.6.11       ad 	sync_msg->pvfsr_waitfor = waitfor;
    546  1.28.6.11       ad 	puffs_credcvt(&sync_msg->pvfsr_cred, cred);
    547  1.28.6.11       ad 	puffs_cidcvt(&sync_msg->pvfsr_cid, l);
    548  1.28.6.11       ad 
    549  1.28.6.11       ad 	rv = puffs_msg_vfs(pmp, park_sync, PUFFS_VFS_SYNC);
    550  1.28.6.11       ad 	rv = checkerr(pmp, rv, __func__);
    551       1.18    pooka 	if (rv)
    552       1.18    pooka 		error = rv;
    553        1.1    pooka 
    554  1.28.6.11       ad 	PUFFS_MSG_RELEASE(sync);
    555        1.1    pooka 	return error;
    556        1.1    pooka }
    557        1.1    pooka 
    558        1.1    pooka int
    559   1.28.6.5       ad puffs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
    560        1.1    pooka {
    561  1.28.6.11       ad 	PUFFS_MSG_VARS(vfs, fhtonode);
    562   1.28.6.5       ad 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    563   1.28.6.5       ad 	struct vnode *vp;
    564  1.28.6.10       ad 	void *fhdata;
    565  1.28.6.10       ad 	size_t argsize, fhlen;
    566   1.28.6.5       ad 	int error;
    567        1.1    pooka 
    568   1.28.6.5       ad 	if (pmp->pmp_args.pa_fhsize == 0)
    569   1.28.6.5       ad 		return EOPNOTSUPP;
    570        1.1    pooka 
    571  1.28.6.10       ad 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    572  1.28.6.10       ad 		fhlen = fhp->fid_len;
    573  1.28.6.10       ad 		fhdata = fhp;
    574   1.28.6.5       ad 	} else {
    575  1.28.6.10       ad 		fhlen = PUFFS_FROMFHSIZE(fhp->fid_len);
    576  1.28.6.10       ad 		fhdata = fhp->fid_data;
    577  1.28.6.10       ad 
    578  1.28.6.10       ad 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) {
    579  1.28.6.10       ad 			if (pmp->pmp_args.pa_fhsize < fhlen)
    580  1.28.6.10       ad 				return EINVAL;
    581  1.28.6.10       ad 		} else {
    582  1.28.6.10       ad 			if (pmp->pmp_args.pa_fhsize != fhlen)
    583  1.28.6.10       ad 				return EINVAL;
    584  1.28.6.10       ad 		}
    585   1.28.6.5       ad 	}
    586        1.1    pooka 
    587  1.28.6.11       ad 	argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen;
    588  1.28.6.11       ad 	puffs_msgmem_alloc(argsize, &park_fhtonode, (void **)&fhtonode_msg, 1);
    589  1.28.6.11       ad 	fhtonode_msg->pvfsr_dsize = fhlen;
    590  1.28.6.11       ad 	memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen);
    591   1.28.6.5       ad 
    592  1.28.6.11       ad 	error = puffs_msg_vfs(pmp, park_fhtonode, PUFFS_VFS_FHTOVP);
    593  1.28.6.10       ad 	error = checkerr(pmp, error, __func__);
    594   1.28.6.5       ad 	if (error)
    595   1.28.6.5       ad 		goto out;
    596   1.28.6.5       ad 
    597  1.28.6.11       ad 	error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp);
    598   1.28.6.5       ad 	DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n",
    599  1.28.6.11       ad 	    fhtonode_msg->pvfsr_fhcookie, vp));
    600  1.28.6.10       ad 	if (error == PUFFS_NOSUCHCOOKIE) {
    601  1.28.6.11       ad 		error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie,
    602  1.28.6.11       ad 		    fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size,
    603  1.28.6.11       ad 		    fhtonode_msg->pvfsr_rdev, &vp);
    604   1.28.6.5       ad 		if (error)
    605   1.28.6.5       ad 			goto out;
    606   1.28.6.5       ad 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    607  1.28.6.10       ad 	} else if (error) {
    608  1.28.6.10       ad 		goto out;
    609   1.28.6.5       ad 	}
    610   1.28.6.5       ad 
    611   1.28.6.5       ad 	*vpp = vp;
    612   1.28.6.5       ad  out:
    613  1.28.6.11       ad 	puffs_msgmem_release(park_fhtonode);
    614   1.28.6.5       ad 	return error;
    615        1.1    pooka }
    616        1.1    pooka 
    617        1.1    pooka int
    618   1.28.6.5       ad puffs_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
    619        1.1    pooka {
    620  1.28.6.11       ad 	PUFFS_MSG_VARS(vfs, nodetofh);
    621   1.28.6.5       ad 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    622  1.28.6.10       ad 	size_t argsize, fhlen;
    623   1.28.6.5       ad 	int error;
    624        1.1    pooka 
    625   1.28.6.5       ad 	if (pmp->pmp_args.pa_fhsize == 0)
    626   1.28.6.5       ad 		return EOPNOTSUPP;
    627   1.28.6.5       ad 
    628  1.28.6.10       ad 	/* if file handles are static len, we can test len immediately */
    629   1.28.6.5       ad 	if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0)
    630  1.28.6.10       ad 	    && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0)
    631   1.28.6.5       ad 	    && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) {
    632   1.28.6.5       ad 		*fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    633   1.28.6.5       ad 		return E2BIG;
    634   1.28.6.5       ad 	}
    635   1.28.6.5       ad 
    636  1.28.6.10       ad 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    637  1.28.6.10       ad 		fhlen = *fh_size;
    638  1.28.6.10       ad 	else
    639  1.28.6.10       ad 		fhlen = PUFFS_FROMFHSIZE(*fh_size);
    640  1.28.6.10       ad 
    641  1.28.6.11       ad 	argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen;
    642  1.28.6.11       ad 	puffs_msgmem_alloc(argsize, &park_nodetofh, (void **)&nodetofh_msg, 1);
    643  1.28.6.11       ad 	nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp);
    644  1.28.6.11       ad 	nodetofh_msg->pvfsr_dsize = fhlen;
    645   1.28.6.5       ad 
    646  1.28.6.11       ad 	error = puffs_msg_vfs(pmp, park_nodetofh, PUFFS_VFS_VPTOFH);
    647  1.28.6.10       ad 	error = checkerr(pmp, error, __func__);
    648  1.28.6.10       ad 
    649  1.28.6.10       ad 	if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH)
    650  1.28.6.11       ad 		fhlen = nodetofh_msg->pvfsr_dsize;
    651  1.28.6.10       ad 	else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC)
    652  1.28.6.11       ad 		fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize);
    653  1.28.6.10       ad 	else
    654  1.28.6.10       ad 		fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize);
    655  1.28.6.10       ad 
    656   1.28.6.5       ad 	if (error) {
    657   1.28.6.5       ad 		if (error == E2BIG)
    658  1.28.6.10       ad 			*fh_size = fhlen;
    659   1.28.6.5       ad 		goto out;
    660   1.28.6.5       ad 	}
    661   1.28.6.5       ad 
    662  1.28.6.10       ad 	if (fhlen > FHANDLE_SIZE_MAX) {
    663  1.28.6.11       ad 		puffs_msg_errnotify(pmp, PUFFS_ERR_VPTOFH, E2BIG,
    664  1.28.6.10       ad 		    "file handle too big", VPTOPNC(vp));
    665  1.28.6.10       ad 		error = EPROTO;
    666   1.28.6.5       ad 		goto out;
    667   1.28.6.5       ad 	}
    668   1.28.6.5       ad 
    669  1.28.6.10       ad 	if (*fh_size < fhlen) {
    670  1.28.6.10       ad 		*fh_size = fhlen;
    671   1.28.6.5       ad 		error = E2BIG;
    672   1.28.6.5       ad 		goto out;
    673   1.28.6.5       ad 	}
    674  1.28.6.10       ad 	*fh_size = fhlen;
    675   1.28.6.5       ad 
    676   1.28.6.5       ad 	if (fhp) {
    677  1.28.6.10       ad 		if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) {
    678  1.28.6.11       ad 			memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen);
    679  1.28.6.10       ad 		} else {
    680  1.28.6.10       ad 			fhp->fid_len = *fh_size;
    681  1.28.6.11       ad 			memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data,
    682  1.28.6.11       ad 			    nodetofh_msg->pvfsr_dsize);
    683  1.28.6.10       ad 		}
    684   1.28.6.5       ad 	}
    685   1.28.6.5       ad 
    686   1.28.6.5       ad  out:
    687  1.28.6.11       ad 	puffs_msgmem_release(park_nodetofh);
    688   1.28.6.5       ad 	return error;
    689        1.1    pooka }
    690        1.1    pooka 
    691        1.1    pooka void
    692        1.1    pooka puffs_init()
    693        1.1    pooka {
    694        1.1    pooka 
    695  1.28.6.10       ad 	/* some checks depend on this */
    696  1.28.6.10       ad 	KASSERT(VNOVAL == VSIZENOTSET);
    697  1.28.6.10       ad 
    698        1.5    pooka 	malloc_type_attach(M_PUFFS);
    699        1.5    pooka 
    700   1.28.6.4       ad 	pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0,
    701   1.28.6.4       ad 	    "puffpnpl", &pool_allocator_nointr, IPL_NONE);
    702   1.28.6.4       ad 	puffs_transport_init();
    703   1.28.6.4       ad 	puffs_msgif_init();
    704        1.1    pooka }
    705        1.1    pooka 
    706        1.1    pooka void
    707        1.1    pooka puffs_done()
    708        1.1    pooka {
    709        1.1    pooka 
    710   1.28.6.4       ad 	puffs_msgif_destroy();
    711   1.28.6.4       ad 	puffs_transport_destroy();
    712        1.5    pooka 	pool_destroy(&puffs_pnpool);
    713        1.5    pooka 
    714   1.28.6.4       ad 	malloc_type_detach(M_PUFFS);
    715        1.1    pooka }
    716        1.1    pooka 
    717        1.1    pooka int
    718        1.1    pooka puffs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts)
    719        1.1    pooka {
    720        1.1    pooka 
    721        1.1    pooka 	return EOPNOTSUPP;
    722        1.1    pooka }
    723        1.1    pooka 
    724       1.26    pooka int
    725       1.26    pooka puffs_suspendctl(struct mount *mp, int cmd)
    726       1.26    pooka {
    727  1.28.6.11       ad 	PUFFS_MSG_VARS(vfs, suspend);
    728       1.26    pooka 	struct puffs_mount *pmp;
    729       1.26    pooka 	int error;
    730       1.26    pooka 
    731       1.26    pooka 	pmp = MPTOPUFFSMP(mp);
    732       1.26    pooka 	switch (cmd) {
    733       1.26    pooka 	case SUSPEND_SUSPEND:
    734       1.26    pooka 		DPRINTF(("puffs_suspendctl: suspending\n"));
    735       1.27  hannken 		if ((error = fstrans_setstate(mp, FSTRANS_SUSPENDING)) != 0)
    736       1.26    pooka 			break;
    737  1.28.6.11       ad 		PUFFS_MSG_ALLOC(vfs, suspend);
    738  1.28.6.11       ad 		puffs_msg_setfaf(park_suspend);
    739  1.28.6.11       ad 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_START;
    740  1.28.6.11       ad 		puffs_msg_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    741  1.28.6.11       ad 		PUFFS_MSG_RELEASE(suspend);
    742       1.26    pooka 
    743   1.28.6.4       ad 		error = pageflush(mp, FSCRED, 0, 1, curlwp);
    744       1.26    pooka 		if (error == 0)
    745       1.27  hannken 			error = fstrans_setstate(mp, FSTRANS_SUSPENDED);
    746       1.26    pooka 
    747       1.26    pooka 		if (error != 0) {
    748  1.28.6.11       ad 			PUFFS_MSG_ALLOC(vfs, suspend);
    749  1.28.6.11       ad 			puffs_msg_setfaf(park_suspend);
    750  1.28.6.11       ad 			suspend_msg->pvfsr_status = PUFFS_SUSPEND_ERROR;
    751  1.28.6.11       ad 			puffs_msg_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    752  1.28.6.11       ad 			PUFFS_MSG_RELEASE(suspend);
    753       1.27  hannken 			(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    754       1.26    pooka 			break;
    755       1.26    pooka 		}
    756       1.26    pooka 
    757  1.28.6.11       ad 		PUFFS_MSG_ALLOC(vfs, suspend);
    758  1.28.6.11       ad 		puffs_msg_setfaf(park_suspend);
    759  1.28.6.11       ad 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_SUSPENDED;
    760  1.28.6.11       ad 		puffs_msg_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    761  1.28.6.11       ad 		PUFFS_MSG_RELEASE(suspend);
    762       1.26    pooka 
    763       1.26    pooka 		break;
    764       1.26    pooka 
    765       1.26    pooka 	case SUSPEND_RESUME:
    766       1.26    pooka 		DPRINTF(("puffs_suspendctl: resume\n"));
    767       1.26    pooka 		error = 0;
    768       1.27  hannken 		(void) fstrans_setstate(mp, FSTRANS_NORMAL);
    769  1.28.6.11       ad 		PUFFS_MSG_ALLOC(vfs, suspend);
    770  1.28.6.11       ad 		puffs_msg_setfaf(park_suspend);
    771  1.28.6.11       ad 		suspend_msg->pvfsr_status = PUFFS_SUSPEND_RESUME;
    772  1.28.6.11       ad 		puffs_msg_vfs(pmp, park_suspend, PUFFS_VFS_SUSPEND);
    773  1.28.6.11       ad 		PUFFS_MSG_RELEASE(suspend);
    774       1.26    pooka 		break;
    775       1.26    pooka 
    776       1.26    pooka 	default:
    777       1.26    pooka 		error = EINVAL;
    778       1.26    pooka 		break;
    779       1.26    pooka 	}
    780       1.26    pooka 
    781       1.26    pooka 	DPRINTF(("puffs_suspendctl: return %d\n", error));
    782       1.26    pooka 	return error;
    783       1.26    pooka }
    784       1.26    pooka 
    785        1.1    pooka const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = {
    786        1.1    pooka 	&puffs_vnodeop_opv_desc,
    787        1.3    pooka 	&puffs_specop_opv_desc,
    788        1.4    pooka 	&puffs_fifoop_opv_desc,
    789       1.12    pooka 	&puffs_msgop_opv_desc,
    790        1.1    pooka 	NULL,
    791        1.1    pooka };
    792        1.1    pooka 
    793        1.1    pooka struct vfsops puffs_vfsops = {
    794        1.1    pooka 	MOUNT_PUFFS,
    795   1.28.6.8       ad 	sizeof (struct puffs_kargs),
    796        1.1    pooka 	puffs_mount,		/* mount	*/
    797        1.1    pooka 	puffs_start,		/* start	*/
    798        1.1    pooka 	puffs_unmount,		/* unmount	*/
    799        1.1    pooka 	puffs_root,		/* root		*/
    800   1.28.6.5       ad 	(void *)eopnotsupp,	/* quotactl	*/
    801        1.1    pooka 	puffs_statvfs,		/* statvfs	*/
    802        1.1    pooka 	puffs_sync,		/* sync		*/
    803   1.28.6.5       ad 	(void *)eopnotsupp,	/* vget		*/
    804   1.28.6.5       ad 	puffs_fhtovp,		/* fhtovp	*/
    805   1.28.6.5       ad 	puffs_vptofh,		/* vptofh	*/
    806        1.1    pooka 	puffs_init,		/* init		*/
    807        1.1    pooka 	NULL,			/* reinit	*/
    808        1.1    pooka 	puffs_done,		/* done		*/
    809        1.1    pooka 	NULL,			/* mountroot	*/
    810        1.1    pooka 	puffs_snapshot,		/* snapshot	*/
    811        1.1    pooka 	vfs_stdextattrctl,	/* extattrctl	*/
    812       1.26    pooka 	puffs_suspendctl,	/* suspendctl	*/
    813        1.1    pooka 	puffs_vnodeopv_descs,	/* vnodeops	*/
    814        1.1    pooka 	0,			/* refcount	*/
    815        1.1    pooka 	{ NULL, NULL }
    816        1.1    pooka };
    817        1.1    pooka VFS_ATTACH(puffs_vfsops);
    818