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