Home | History | Annotate | Line # | Download | only in puffs
puffs_vnops.c revision 1.183
      1 /*	$NetBSD: puffs_vnops.c,v 1.183 2014/08/16 16:19:41 manu Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8  * The Google SoC project was mentored by Bill Studenmund.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: puffs_vnops.c,v 1.183 2014/08/16 16:19:41 manu Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/buf.h>
     37 #include <sys/lockf.h>
     38 #include <sys/malloc.h>
     39 #include <sys/mount.h>
     40 #include <sys/namei.h>
     41 #include <sys/vnode.h>
     42 #include <sys/proc.h>
     43 #include <sys/kernel.h> /* For hz, hardclock_ticks */
     44 
     45 #include <uvm/uvm.h>
     46 
     47 #include <fs/puffs/puffs_msgif.h>
     48 #include <fs/puffs/puffs_sys.h>
     49 
     50 #include <miscfs/fifofs/fifo.h>
     51 #include <miscfs/genfs/genfs.h>
     52 #include <miscfs/specfs/specdev.h>
     53 
     54 int	puffs_vnop_lookup(void *);
     55 int	puffs_vnop_create(void *);
     56 int	puffs_vnop_access(void *);
     57 int	puffs_vnop_mknod(void *);
     58 int	puffs_vnop_open(void *);
     59 int	puffs_vnop_close(void *);
     60 int	puffs_vnop_getattr(void *);
     61 int	puffs_vnop_setattr(void *);
     62 int	puffs_vnop_reclaim(void *);
     63 int	puffs_vnop_readdir(void *);
     64 int	puffs_vnop_poll(void *);
     65 int	puffs_vnop_fsync(void *);
     66 int	puffs_vnop_seek(void *);
     67 int	puffs_vnop_remove(void *);
     68 int	puffs_vnop_mkdir(void *);
     69 int	puffs_vnop_rmdir(void *);
     70 int	puffs_vnop_link(void *);
     71 int	puffs_vnop_readlink(void *);
     72 int	puffs_vnop_symlink(void *);
     73 int	puffs_vnop_rename(void *);
     74 int	puffs_vnop_read(void *);
     75 int	puffs_vnop_write(void *);
     76 int	puffs_vnop_fcntl(void *);
     77 int	puffs_vnop_ioctl(void *);
     78 int	puffs_vnop_inactive(void *);
     79 int	puffs_vnop_print(void *);
     80 int	puffs_vnop_pathconf(void *);
     81 int	puffs_vnop_advlock(void *);
     82 int	puffs_vnop_strategy(void *);
     83 int	puffs_vnop_bmap(void *);
     84 int	puffs_vnop_mmap(void *);
     85 int	puffs_vnop_getpages(void *);
     86 int	puffs_vnop_abortop(void *);
     87 int	puffs_vnop_getextattr(void *);
     88 int	puffs_vnop_setextattr(void *);
     89 int	puffs_vnop_listextattr(void *);
     90 int	puffs_vnop_deleteextattr(void *);
     91 
     92 int	puffs_vnop_spec_read(void *);
     93 int	puffs_vnop_spec_write(void *);
     94 int	puffs_vnop_fifo_read(void *);
     95 int	puffs_vnop_fifo_write(void *);
     96 
     97 int	puffs_vnop_checkop(void *);
     98 
     99 #define puffs_vnop_lock genfs_lock
    100 #define puffs_vnop_unlock genfs_unlock
    101 #define puffs_vnop_islocked genfs_islocked
    102 
    103 int (**puffs_vnodeop_p)(void *);
    104 const struct vnodeopv_entry_desc puffs_vnodeop_entries[] = {
    105 	{ &vop_default_desc, vn_default_error },
    106 	{ &vop_lookup_desc, puffs_vnop_lookup },	/* REAL lookup */
    107 	{ &vop_create_desc, puffs_vnop_checkop },	/* create */
    108         { &vop_mknod_desc, puffs_vnop_checkop },	/* mknod */
    109         { &vop_open_desc, puffs_vnop_open },		/* REAL open */
    110         { &vop_close_desc, puffs_vnop_checkop },	/* close */
    111         { &vop_access_desc, puffs_vnop_access },	/* REAL access */
    112         { &vop_getattr_desc, puffs_vnop_checkop },	/* getattr */
    113         { &vop_setattr_desc, puffs_vnop_checkop },	/* setattr */
    114         { &vop_read_desc, puffs_vnop_checkop },		/* read */
    115         { &vop_write_desc, puffs_vnop_checkop },	/* write */
    116 	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
    117 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
    118         { &vop_fsync_desc, puffs_vnop_fsync },		/* REAL fsync */
    119         { &vop_seek_desc, puffs_vnop_checkop },		/* seek */
    120         { &vop_remove_desc, puffs_vnop_checkop },	/* remove */
    121         { &vop_link_desc, puffs_vnop_checkop },		/* link */
    122         { &vop_rename_desc, puffs_vnop_checkop },	/* rename */
    123         { &vop_mkdir_desc, puffs_vnop_checkop },	/* mkdir */
    124         { &vop_rmdir_desc, puffs_vnop_checkop },	/* rmdir */
    125         { &vop_symlink_desc, puffs_vnop_checkop },	/* symlink */
    126         { &vop_readdir_desc, puffs_vnop_checkop },	/* readdir */
    127         { &vop_readlink_desc, puffs_vnop_checkop },	/* readlink */
    128         { &vop_getpages_desc, puffs_vnop_checkop },	/* getpages */
    129         { &vop_putpages_desc, genfs_putpages },		/* REAL putpages */
    130         { &vop_pathconf_desc, puffs_vnop_checkop },	/* pathconf */
    131         { &vop_advlock_desc, puffs_vnop_advlock },	/* advlock */
    132         { &vop_strategy_desc, puffs_vnop_strategy },	/* REAL strategy */
    133         { &vop_revoke_desc, genfs_revoke },		/* REAL revoke */
    134         { &vop_abortop_desc, puffs_vnop_abortop },	/* REAL abortop */
    135         { &vop_inactive_desc, puffs_vnop_inactive },	/* REAL inactive */
    136         { &vop_reclaim_desc, puffs_vnop_reclaim },	/* REAL reclaim */
    137         { &vop_lock_desc, puffs_vnop_lock },		/* REAL lock */
    138         { &vop_unlock_desc, puffs_vnop_unlock },	/* REAL unlock */
    139         { &vop_bmap_desc, puffs_vnop_bmap },		/* REAL bmap */
    140         { &vop_print_desc, puffs_vnop_print },		/* REAL print */
    141         { &vop_islocked_desc, puffs_vnop_islocked },	/* REAL islocked */
    142         { &vop_bwrite_desc, genfs_nullop },		/* REAL bwrite */
    143         { &vop_mmap_desc, puffs_vnop_mmap },		/* REAL mmap */
    144         { &vop_poll_desc, puffs_vnop_poll },		/* REAL poll */
    145 	{ &vop_getextattr_desc, puffs_vnop_getextattr },	/* getextattr */
    146 	{ &vop_setextattr_desc, puffs_vnop_setextattr },	/* setextattr */
    147 	{ &vop_listextattr_desc, puffs_vnop_listextattr },	/* listextattr */
    148 	{ &vop_deleteextattr_desc, puffs_vnop_deleteextattr },/* deleteextattr */
    149 #if 0
    150 	{ &vop_openextattr_desc, puffs_vnop_checkop },	/* openextattr */
    151 	{ &vop_closeextattr_desc, puffs_vnop_checkop },	/* closeextattr */
    152 #endif
    153         { &vop_kqfilter_desc, genfs_eopnotsupp },	/* kqfilter XXX */
    154 	{ NULL, NULL }
    155 };
    156 const struct vnodeopv_desc puffs_vnodeop_opv_desc =
    157 	{ &puffs_vnodeop_p, puffs_vnodeop_entries };
    158 
    159 
    160 int (**puffs_specop_p)(void *);
    161 const struct vnodeopv_entry_desc puffs_specop_entries[] = {
    162 	{ &vop_default_desc, vn_default_error },
    163 	{ &vop_lookup_desc, spec_lookup },		/* lookup, ENOTDIR */
    164 	{ &vop_create_desc, spec_create },		/* genfs_badop */
    165 	{ &vop_mknod_desc, spec_mknod },		/* genfs_badop */
    166 	{ &vop_open_desc, spec_open },			/* spec_open */
    167 	{ &vop_close_desc, spec_close },		/* spec_close */
    168 	{ &vop_access_desc, puffs_vnop_checkop },	/* access */
    169 	{ &vop_getattr_desc, puffs_vnop_checkop },	/* getattr */
    170 	{ &vop_setattr_desc, puffs_vnop_checkop },	/* setattr */
    171 	{ &vop_read_desc, puffs_vnop_spec_read },	/* update, read */
    172 	{ &vop_write_desc, puffs_vnop_spec_write },	/* update, write */
    173 	{ &vop_fallocate_desc, spec_fallocate },	/* fallocate */
    174 	{ &vop_fdiscard_desc, spec_fdiscard },		/* fdiscard */
    175 	{ &vop_ioctl_desc, spec_ioctl },		/* spec_ioctl */
    176 	{ &vop_fcntl_desc, genfs_fcntl },		/* dummy */
    177 	{ &vop_poll_desc, spec_poll },			/* spec_poll */
    178 	{ &vop_kqfilter_desc, spec_kqfilter },		/* spec_kqfilter */
    179 	{ &vop_revoke_desc, spec_revoke },		/* genfs_revoke */
    180 	{ &vop_mmap_desc, spec_mmap },			/* spec_mmap */
    181 	{ &vop_fsync_desc, spec_fsync },		/* vflushbuf */
    182 	{ &vop_seek_desc, spec_seek },			/* genfs_nullop */
    183 	{ &vop_remove_desc, spec_remove },		/* genfs_badop */
    184 	{ &vop_link_desc, spec_link },			/* genfs_badop */
    185 	{ &vop_rename_desc, spec_rename },		/* genfs_badop */
    186 	{ &vop_mkdir_desc, spec_mkdir },		/* genfs_badop */
    187 	{ &vop_rmdir_desc, spec_rmdir },		/* genfs_badop */
    188 	{ &vop_symlink_desc, spec_symlink },		/* genfs_badop */
    189 	{ &vop_readdir_desc, spec_readdir },		/* genfs_badop */
    190 	{ &vop_readlink_desc, spec_readlink },		/* genfs_badop */
    191 	{ &vop_abortop_desc, spec_abortop },		/* genfs_badop */
    192 	{ &vop_inactive_desc, puffs_vnop_inactive },	/* REAL inactive */
    193 	{ &vop_reclaim_desc, puffs_vnop_reclaim },	/* REAL reclaim */
    194 	{ &vop_lock_desc, puffs_vnop_lock },		/* REAL lock */
    195 	{ &vop_unlock_desc, puffs_vnop_unlock },	/* REAL unlock */
    196 	{ &vop_bmap_desc, spec_bmap },			/* dummy */
    197 	{ &vop_strategy_desc, spec_strategy },		/* dev strategy */
    198 	{ &vop_print_desc, puffs_vnop_print },		/* REAL print */
    199 	{ &vop_islocked_desc, puffs_vnop_islocked },	/* REAL islocked */
    200 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    201 	{ &vop_advlock_desc, spec_advlock },		/* lf_advlock */
    202 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    203 	{ &vop_getpages_desc, spec_getpages },		/* genfs_getpages */
    204 	{ &vop_putpages_desc, spec_putpages },		/* genfs_putpages */
    205 	{ &vop_getextattr_desc, puffs_vnop_checkop },	/* getextattr */
    206 	{ &vop_setextattr_desc, puffs_vnop_checkop },	/* setextattr */
    207 	{ &vop_listextattr_desc, puffs_vnop_checkop },	/* listextattr */
    208 	{ &vop_deleteextattr_desc, puffs_vnop_checkop },/* deleteextattr */
    209 #if 0
    210 	{ &vop_openextattr_desc, _openextattr },	/* openextattr */
    211 	{ &vop_closeextattr_desc, _closeextattr },	/* closeextattr */
    212 #endif
    213 	{ NULL, NULL }
    214 };
    215 const struct vnodeopv_desc puffs_specop_opv_desc =
    216 	{ &puffs_specop_p, puffs_specop_entries };
    217 
    218 
    219 int (**puffs_fifoop_p)(void *);
    220 const struct vnodeopv_entry_desc puffs_fifoop_entries[] = {
    221 	{ &vop_default_desc, vn_default_error },
    222 	{ &vop_lookup_desc, vn_fifo_bypass },		/* lookup, ENOTDIR */
    223 	{ &vop_create_desc, vn_fifo_bypass },		/* genfs_badop */
    224 	{ &vop_mknod_desc, vn_fifo_bypass },		/* genfs_badop */
    225 	{ &vop_open_desc, vn_fifo_bypass },		/* open */
    226 	{ &vop_close_desc, vn_fifo_bypass },		/* close */
    227 	{ &vop_access_desc, puffs_vnop_checkop },	/* access */
    228 	{ &vop_getattr_desc, puffs_vnop_checkop },	/* getattr */
    229 	{ &vop_setattr_desc, puffs_vnop_checkop },	/* setattr */
    230 	{ &vop_read_desc, puffs_vnop_fifo_read },	/* read, update */
    231 	{ &vop_write_desc, puffs_vnop_fifo_write },	/* write, update */
    232 	{ &vop_fallocate_desc, vn_fifo_bypass },	/* fallocate */
    233 	{ &vop_fdiscard_desc, vn_fifo_bypass },		/* fdiscard */
    234 	{ &vop_ioctl_desc, vn_fifo_bypass },		/* ioctl */
    235 	{ &vop_fcntl_desc, genfs_fcntl },		/* dummy */
    236 	{ &vop_poll_desc, vn_fifo_bypass },		/* poll */
    237 	{ &vop_kqfilter_desc, vn_fifo_bypass },		/* kqfilter */
    238 	{ &vop_revoke_desc, vn_fifo_bypass },		/* genfs_revoke */
    239 	{ &vop_mmap_desc, vn_fifo_bypass },		/* genfs_badop */
    240 	{ &vop_fsync_desc, vn_fifo_bypass },		/* genfs_nullop*/
    241 	{ &vop_seek_desc, vn_fifo_bypass },		/* genfs_badop */
    242 	{ &vop_remove_desc, vn_fifo_bypass },		/* genfs_badop */
    243 	{ &vop_link_desc, vn_fifo_bypass },		/* genfs_badop */
    244 	{ &vop_rename_desc, vn_fifo_bypass },		/* genfs_badop */
    245 	{ &vop_mkdir_desc, vn_fifo_bypass },		/* genfs_badop */
    246 	{ &vop_rmdir_desc, vn_fifo_bypass },		/* genfs_badop */
    247 	{ &vop_symlink_desc, vn_fifo_bypass },		/* genfs_badop */
    248 	{ &vop_readdir_desc, vn_fifo_bypass },		/* genfs_badop */
    249 	{ &vop_readlink_desc, vn_fifo_bypass },		/* genfs_badop */
    250 	{ &vop_abortop_desc, vn_fifo_bypass },		/* genfs_badop */
    251 	{ &vop_inactive_desc, puffs_vnop_inactive },	/* REAL inactive */
    252 	{ &vop_reclaim_desc, puffs_vnop_reclaim },	/* REAL reclaim */
    253 	{ &vop_lock_desc, puffs_vnop_lock },		/* REAL lock */
    254 	{ &vop_unlock_desc, puffs_vnop_unlock },	/* REAL unlock */
    255 	{ &vop_bmap_desc, vn_fifo_bypass },		/* dummy */
    256 	{ &vop_strategy_desc, vn_fifo_bypass },		/* genfs_badop */
    257 	{ &vop_print_desc, puffs_vnop_print },		/* REAL print */
    258 	{ &vop_islocked_desc, puffs_vnop_islocked },	/* REAL islocked */
    259 	{ &vop_pathconf_desc, vn_fifo_bypass },		/* pathconf */
    260 	{ &vop_advlock_desc, vn_fifo_bypass },		/* genfs_einval */
    261 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    262 	{ &vop_putpages_desc, vn_fifo_bypass }, 	/* genfs_null_putpages*/
    263 #if 0
    264 	{ &vop_openextattr_desc, _openextattr },	/* openextattr */
    265 	{ &vop_closeextattr_desc, _closeextattr },	/* closeextattr */
    266 #endif
    267 	{ &vop_getextattr_desc, puffs_vnop_checkop },		/* getextattr */
    268 	{ &vop_setextattr_desc, puffs_vnop_checkop },		/* setextattr */
    269 	{ &vop_listextattr_desc, puffs_vnop_checkop },	/* listextattr */
    270 	{ &vop_deleteextattr_desc, puffs_vnop_checkop },	/* deleteextattr */
    271 	{ NULL, NULL }
    272 };
    273 const struct vnodeopv_desc puffs_fifoop_opv_desc =
    274 	{ &puffs_fifoop_p, puffs_fifoop_entries };
    275 
    276 
    277 /* "real" vnode operations */
    278 int (**puffs_msgop_p)(void *);
    279 const struct vnodeopv_entry_desc puffs_msgop_entries[] = {
    280 	{ &vop_default_desc, vn_default_error },
    281 	{ &vop_create_desc, puffs_vnop_create },	/* create */
    282         { &vop_mknod_desc, puffs_vnop_mknod },		/* mknod */
    283         { &vop_open_desc, puffs_vnop_open },		/* open */
    284         { &vop_close_desc, puffs_vnop_close },		/* close */
    285         { &vop_access_desc, puffs_vnop_access },	/* access */
    286         { &vop_getattr_desc, puffs_vnop_getattr },	/* getattr */
    287         { &vop_setattr_desc, puffs_vnop_setattr },	/* setattr */
    288         { &vop_read_desc, puffs_vnop_read },		/* read */
    289         { &vop_write_desc, puffs_vnop_write },		/* write */
    290         { &vop_seek_desc, puffs_vnop_seek },		/* seek */
    291         { &vop_remove_desc, puffs_vnop_remove },	/* remove */
    292         { &vop_link_desc, puffs_vnop_link },		/* link */
    293         { &vop_rename_desc, puffs_vnop_rename },	/* rename */
    294         { &vop_mkdir_desc, puffs_vnop_mkdir },		/* mkdir */
    295         { &vop_rmdir_desc, puffs_vnop_rmdir },		/* rmdir */
    296         { &vop_symlink_desc, puffs_vnop_symlink },	/* symlink */
    297         { &vop_readdir_desc, puffs_vnop_readdir },	/* readdir */
    298         { &vop_readlink_desc, puffs_vnop_readlink },	/* readlink */
    299         { &vop_print_desc, puffs_vnop_print },		/* print */
    300         { &vop_islocked_desc, puffs_vnop_islocked },	/* islocked */
    301         { &vop_pathconf_desc, puffs_vnop_pathconf },	/* pathconf */
    302         { &vop_getpages_desc, puffs_vnop_getpages },	/* getpages */
    303 	{ NULL, NULL }
    304 };
    305 const struct vnodeopv_desc puffs_msgop_opv_desc =
    306 	{ &puffs_msgop_p, puffs_msgop_entries };
    307 
    308 /*
    309  * for dosetattr / update_va
    310  */
    311 #define SETATTR_CHSIZE	0x01
    312 #define SETATTR_ASYNC	0x02
    313 
    314 #define ERROUT(err)							\
    315 do {									\
    316 	error = err;							\
    317 	goto out;							\
    318 } while (/*CONSTCOND*/0)
    319 
    320 /*
    321  * This is a generic vnode operation handler.  It checks if the necessary
    322  * operations for the called vnode operation are implemented by userspace
    323  * and either returns a dummy return value or proceeds to call the real
    324  * vnode operation from puffs_msgop_v.
    325  *
    326  * XXX: this should described elsewhere and autogenerated, the complexity
    327  * of the vnode operations vectors and their interrelationships is also
    328  * getting a bit out of hand.  Another problem is that we need this same
    329  * information in the fs server code, so keeping the two in sync manually
    330  * is not a viable (long term) plan.
    331  */
    332 
    333 /* not supported, handle locking protocol */
    334 #define CHECKOP_NOTSUPP(op)						\
    335 case VOP_##op##_DESCOFFSET:						\
    336 	if (pmp->pmp_vnopmask[PUFFS_VN_##op] == 0)			\
    337 		return genfs_eopnotsupp(v);				\
    338 	break
    339 
    340 /* always succeed, no locking */
    341 #define CHECKOP_SUCCESS(op)						\
    342 case VOP_##op##_DESCOFFSET:						\
    343 	if (pmp->pmp_vnopmask[PUFFS_VN_##op] == 0)			\
    344 		return 0;						\
    345 	break
    346 
    347 int
    348 puffs_vnop_checkop(void *v)
    349 {
    350 	struct vop_generic_args /* {
    351 		struct vnodeop_desc *a_desc;
    352 		spooky mystery contents;
    353 	} */ *ap = v;
    354 	struct vnodeop_desc *desc = ap->a_desc;
    355 	struct puffs_mount *pmp;
    356 	struct vnode *vp;
    357 	int offset, rv;
    358 
    359 	offset = ap->a_desc->vdesc_vp_offsets[0];
    360 #ifdef DIAGNOSTIC
    361 	if (offset == VDESC_NO_OFFSET)
    362 		panic("puffs_checkop: no vnode, why did you call me?");
    363 #endif
    364 	vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap);
    365 	pmp = MPTOPUFFSMP(vp->v_mount);
    366 
    367 	DPRINTF_VERBOSE(("checkop call %s (%d), vp %p\n",
    368 	    ap->a_desc->vdesc_name, ap->a_desc->vdesc_offset, vp));
    369 
    370 	if (!ALLOPS(pmp)) {
    371 		switch (desc->vdesc_offset) {
    372 			CHECKOP_NOTSUPP(CREATE);
    373 			CHECKOP_NOTSUPP(MKNOD);
    374 			CHECKOP_NOTSUPP(GETATTR);
    375 			CHECKOP_NOTSUPP(SETATTR);
    376 			CHECKOP_NOTSUPP(READ);
    377 			CHECKOP_NOTSUPP(WRITE);
    378 			CHECKOP_NOTSUPP(FCNTL);
    379 			CHECKOP_NOTSUPP(IOCTL);
    380 			CHECKOP_NOTSUPP(REMOVE);
    381 			CHECKOP_NOTSUPP(LINK);
    382 			CHECKOP_NOTSUPP(RENAME);
    383 			CHECKOP_NOTSUPP(MKDIR);
    384 			CHECKOP_NOTSUPP(RMDIR);
    385 			CHECKOP_NOTSUPP(SYMLINK);
    386 			CHECKOP_NOTSUPP(READDIR);
    387 			CHECKOP_NOTSUPP(READLINK);
    388 			CHECKOP_NOTSUPP(PRINT);
    389 			CHECKOP_NOTSUPP(PATHCONF);
    390 			CHECKOP_NOTSUPP(GETEXTATTR);
    391 			CHECKOP_NOTSUPP(SETEXTATTR);
    392 			CHECKOP_NOTSUPP(LISTEXTATTR);
    393 			CHECKOP_NOTSUPP(DELETEEXTATTR);
    394 
    395 			CHECKOP_SUCCESS(ACCESS);
    396 			CHECKOP_SUCCESS(CLOSE);
    397 			CHECKOP_SUCCESS(SEEK);
    398 
    399 		case VOP_GETPAGES_DESCOFFSET:
    400 			if (!EXISTSOP(pmp, READ))
    401 				return genfs_eopnotsupp(v);
    402 			break;
    403 
    404 		default:
    405 			panic("puffs_checkop: unhandled vnop %d",
    406 			    desc->vdesc_offset);
    407 		}
    408 	}
    409 
    410 	rv = VOCALL(puffs_msgop_p, ap->a_desc->vdesc_offset, v);
    411 
    412 	DPRINTF_VERBOSE(("checkop return %s (%d), vp %p: %d\n",
    413 	    ap->a_desc->vdesc_name, ap->a_desc->vdesc_offset, vp, rv));
    414 
    415 	return rv;
    416 }
    417 
    418 static int callremove(struct puffs_mount *, puffs_cookie_t, puffs_cookie_t,
    419 			    struct componentname *);
    420 static int callrmdir(struct puffs_mount *, puffs_cookie_t, puffs_cookie_t,
    421 			   struct componentname *);
    422 static void callinactive(struct puffs_mount *, puffs_cookie_t, int);
    423 static void callreclaim(struct puffs_mount *, puffs_cookie_t, int);
    424 static int  flushvncache(struct vnode *, off_t, off_t, bool);
    425 static void update_va(struct vnode *, struct vattr *, struct vattr *,
    426 		      struct timespec *, struct timespec *, int);
    427 static void update_parent(struct vnode *, struct vnode *);
    428 
    429 
    430 #define PUFFS_ABORT_LOOKUP	1
    431 #define PUFFS_ABORT_CREATE	2
    432 #define PUFFS_ABORT_MKNOD	3
    433 #define PUFFS_ABORT_MKDIR	4
    434 #define PUFFS_ABORT_SYMLINK	5
    435 
    436 /*
    437  * Press the pani^Wabort button!  Kernel resource allocation failed.
    438  */
    439 static void
    440 puffs_abortbutton(struct puffs_mount *pmp, int what,
    441 	puffs_cookie_t dck, puffs_cookie_t ck, struct componentname *cnp)
    442 {
    443 
    444 	switch (what) {
    445 	case PUFFS_ABORT_CREATE:
    446 	case PUFFS_ABORT_MKNOD:
    447 	case PUFFS_ABORT_SYMLINK:
    448 		callremove(pmp, dck, ck, cnp);
    449 		break;
    450 	case PUFFS_ABORT_MKDIR:
    451 		callrmdir(pmp, dck, ck, cnp);
    452 		break;
    453 	}
    454 
    455 	callinactive(pmp, ck, 0);
    456 	callreclaim(pmp, ck, 1);
    457 }
    458 
    459 /*
    460  * Begin vnode operations.
    461  *
    462  * A word from the keymaster about locks: generally we don't want
    463  * to use the vnode locks at all: it creates an ugly dependency between
    464  * the userlandia file server and the kernel.  But we'll play along with
    465  * the kernel vnode locks for now.  However, even currently we attempt
    466  * to release locks as early as possible.  This is possible for some
    467  * operations which a) don't need a locked vnode after the userspace op
    468  * and b) return with the vnode unlocked.  Theoretically we could
    469  * unlock-do op-lock for others and order the graph in userspace, but I
    470  * don't want to think of the consequences for the time being.
    471  */
    472 
    473 #define TTL_TO_TIMEOUT(ts) \
    474     (hardclock_ticks + (ts->tv_sec * hz) + (ts->tv_nsec * hz / 1000000000))
    475 #define TTL_VALID(ts) \
    476     ((ts != NULL) && !((ts->tv_sec == 0) && (ts->tv_nsec == 0)))
    477 #define TIMED_OUT(expire) \
    478     ((int)((unsigned int)hardclock_ticks - (unsigned int)expire) > 0)
    479 int
    480 puffs_vnop_lookup(void *v)
    481 {
    482         struct vop_lookup_v2_args /* {
    483 		const struct vnodeop_desc *a_desc;
    484 		struct vnode *a_dvp;
    485 		struct vnode **a_vpp;
    486 		struct componentname *a_cnp;
    487         } */ *ap = v;
    488 	PUFFS_MSG_VARS(vn, lookup);
    489 	struct puffs_mount *pmp;
    490 	struct componentname *cnp;
    491 	struct vnode *vp, *dvp, *cvp;
    492 	struct puffs_node *dpn, *cpn;
    493 	int isdot;
    494 	int error;
    495 
    496 	pmp = MPTOPUFFSMP(ap->a_dvp->v_mount);
    497 	cnp = ap->a_cnp;
    498 	dvp = ap->a_dvp;
    499 	cvp = NULL;
    500 	cpn = NULL;
    501 	*ap->a_vpp = NULL;
    502 
    503 	/* r/o fs?  we check create later to handle EEXIST */
    504 	if ((cnp->cn_flags & ISLASTCN)
    505 	    && (dvp->v_mount->mnt_flag & MNT_RDONLY)
    506 	    && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    507 		return EROFS;
    508 
    509 	isdot = cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.';
    510 
    511 	DPRINTF(("puffs_lookup: \"%s\", parent vnode %p, op: %x\n",
    512 	    cnp->cn_nameptr, dvp, cnp->cn_nameiop));
    513 
    514 	/*
    515 	 * If dotdot cache is enabled, add reference to .. and return.
    516 	 */
    517 	if (PUFFS_USE_DOTDOTCACHE(pmp) && (cnp->cn_flags & ISDOTDOT)) {
    518 		vp = VPTOPP(ap->a_dvp)->pn_parent;
    519 		vref(vp);
    520 
    521 		*ap->a_vpp = vp;
    522 		return 0;
    523 	}
    524 
    525 	/*
    526 	 * Check if someone fed it into the cache
    527 	 */
    528 	if (!isdot && PUFFS_USE_NAMECACHE(pmp)) {
    529 		int found, iswhiteout;
    530 
    531 		found = cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
    532 				     cnp->cn_nameiop, cnp->cn_flags,
    533 				     &iswhiteout, ap->a_vpp);
    534 		if (iswhiteout) {
    535 			cnp->cn_flags |= ISWHITEOUT;
    536 		}
    537 
    538 		if (found && *ap->a_vpp != NULLVP && PUFFS_USE_FS_TTL(pmp)) {
    539 			cvp = *ap->a_vpp;
    540 			cpn = VPTOPP(cvp);
    541 
    542 			if (TIMED_OUT(cpn->pn_cn_timeout)) {
    543 				cache_purge(cvp);
    544 				/*
    545 				 * cached vnode (cvp) is still referenced
    546 				 * so that we can reuse it upon a new
    547 				 * successful lookup.
    548 				 */
    549 				*ap->a_vpp = NULL;
    550 				found = 0;
    551 			}
    552 		}
    553 
    554 		/*
    555 		 * Do not use negative caching, since the filesystem
    556 		 * provides no TTL for it.
    557 		 */
    558 		if (found && *ap->a_vpp == NULLVP && PUFFS_USE_FS_TTL(pmp))
    559 			found = 0;
    560 
    561 		if (found) {
    562 			return *ap->a_vpp == NULLVP ? ENOENT : 0;
    563 		}
    564 
    565 		/*
    566 		 * This is what would have been left in ERROR before
    567 		 * the rearrangement of cache_lookup(). What with all
    568 		 * the macros, I am not sure if this is a dead value
    569 		 * below or not.
    570 		 */
    571 		error = -1;
    572 	}
    573 
    574 	if (isdot) {
    575 		/* deal with rename lookup semantics */
    576 		if (cnp->cn_nameiop == RENAME && (cnp->cn_flags & ISLASTCN))
    577 			return EISDIR;
    578 
    579 		vp = ap->a_dvp;
    580 		vref(vp);
    581 		*ap->a_vpp = vp;
    582 		return 0;
    583 	}
    584 
    585 	if (cvp != NULL) {
    586 		if (vn_lock(cvp, LK_EXCLUSIVE) != 0) {
    587 			vrele(cvp);
    588 			cvp = NULL;
    589 		} else
    590 			mutex_enter(&cpn->pn_sizemtx);
    591 	}
    592 
    593 	PUFFS_MSG_ALLOC(vn, lookup);
    594 	puffs_makecn(&lookup_msg->pvnr_cn, &lookup_msg->pvnr_cn_cred,
    595 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
    596 
    597 	if (cnp->cn_flags & ISDOTDOT)
    598 		VOP_UNLOCK(dvp);
    599 
    600 	puffs_msg_setinfo(park_lookup, PUFFSOP_VN,
    601 	    PUFFS_VN_LOOKUP, VPTOPNC(dvp));
    602 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_lookup, dvp->v_data, NULL, error);
    603 	DPRINTF(("puffs_lookup: return of the userspace, part %d\n", error));
    604 
    605 	/*
    606 	 * In case of error, there is no new vnode to play with, so be
    607 	 * happy with the NULL value given to vpp in the beginning.
    608 	 * Also, check if this really was an error or the target was not
    609 	 * present.  Either treat it as a non-error for CREATE/RENAME or
    610 	 * enter the component into the negative name cache (if desired).
    611 	 */
    612 	if (error) {
    613 		error = checkerr(pmp, error, __func__);
    614 		if (error == ENOENT) {
    615 			/* don't allow to create files on r/o fs */
    616 			if ((dvp->v_mount->mnt_flag & MNT_RDONLY)
    617 			    && cnp->cn_nameiop == CREATE) {
    618 				error = EROFS;
    619 
    620 			/* adjust values if we are creating */
    621 			} else if ((cnp->cn_flags & ISLASTCN)
    622 			    && (cnp->cn_nameiop == CREATE
    623 			      || cnp->cn_nameiop == RENAME)) {
    624 				error = EJUSTRETURN;
    625 
    626 			/* save negative cache entry */
    627 			} else {
    628 				if (PUFFS_USE_NAMECACHE(pmp) &&
    629 				    !PUFFS_USE_FS_TTL(pmp))
    630 					cache_enter(dvp, NULL, cnp->cn_nameptr,
    631 						cnp->cn_namelen, cnp->cn_flags);
    632 			}
    633 		}
    634 		goto out;
    635 	}
    636 
    637 	/*
    638 	 * Check that we don't get our parent node back, that would cause
    639 	 * a pretty obvious deadlock.
    640 	 */
    641 	dpn = dvp->v_data;
    642 	if (lookup_msg->pvnr_newnode == dpn->pn_cookie) {
    643 		puffs_senderr(pmp, PUFFS_ERR_LOOKUP, EINVAL,
    644 		    "lookup produced parent cookie", lookup_msg->pvnr_newnode);
    645 		error = EPROTO;
    646 		goto out;
    647 	}
    648 
    649 	/*
    650 	 * Check if we looked up the cached vnode
    651 	 */
    652 	vp = NULL;
    653 	if (cvp && (VPTOPP(cvp)->pn_cookie == lookup_msg->pvnr_newnode)) {
    654 		int grace;
    655 
    656 		/*
    657 		 * Bump grace time of this node so that it does not get
    658 		 * reclaimed too fast. We try to increase a bit more the
    659 		 * lifetime of busiest * nodes - with some limits.
    660 		 */
    661 		grace = 10 * puffs_sopreq_expire_timeout;
    662 		cpn->pn_cn_grace = hardclock_ticks + grace;
    663 		vp = cvp;
    664 	}
    665 
    666 	/*
    667 	 * No cached vnode available, or the cached vnode does not
    668 	 * match the userland cookie anymore: is the node known?
    669 	 */
    670 	if (vp == NULL) {
    671 		error = puffs_cookie2vnode(pmp, lookup_msg->pvnr_newnode,
    672 					   1, 1, &vp);
    673 	}
    674 
    675 	if (error == PUFFS_NOSUCHCOOKIE) {
    676 		error = puffs_getvnode(dvp->v_mount,
    677 		    lookup_msg->pvnr_newnode, lookup_msg->pvnr_vtype,
    678 		    lookup_msg->pvnr_size, lookup_msg->pvnr_rdev, &vp);
    679 		if (error) {
    680 			puffs_abortbutton(pmp, PUFFS_ABORT_LOOKUP,
    681 			    VPTOPNC(dvp), lookup_msg->pvnr_newnode,
    682 			    ap->a_cnp);
    683 			goto out;
    684 		}
    685 
    686 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    687 	} else if (error) {
    688 		puffs_abortbutton(pmp, PUFFS_ABORT_LOOKUP, VPTOPNC(dvp),
    689 		    lookup_msg->pvnr_newnode, ap->a_cnp);
    690 		goto out;
    691 	}
    692 
    693 	/*
    694 	 * Update cache and TTL
    695 	 */
    696 	if (PUFFS_USE_FS_TTL(pmp)) {
    697 		struct timespec *va_ttl = &lookup_msg->pvnr_va_ttl;
    698 		struct timespec *cn_ttl = &lookup_msg->pvnr_cn_ttl;
    699 		update_va(vp, NULL, &lookup_msg->pvnr_va,
    700 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
    701 	}
    702 
    703 	KASSERT(lookup_msg->pvnr_newnode == VPTOPP(vp)->pn_cookie);
    704 	*ap->a_vpp = vp;
    705 
    706 	if (PUFFS_USE_NAMECACHE(pmp))
    707 		cache_enter(dvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
    708 			    cnp->cn_flags);
    709 
    710 	/* XXX */
    711 	if ((lookup_msg->pvnr_cn.pkcn_flags & REQUIREDIR) == 0)
    712 		cnp->cn_flags &= ~REQUIREDIR;
    713 	if (lookup_msg->pvnr_cn.pkcn_consume)
    714 		cnp->cn_consume = MIN(lookup_msg->pvnr_cn.pkcn_consume,
    715 		    strlen(cnp->cn_nameptr) - cnp->cn_namelen);
    716 
    717 	VPTOPP(vp)->pn_nlookup++;
    718 
    719 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
    720 	    (VPTOPP(vp)->pn_parent != dvp))
    721 		update_parent(vp, dvp);
    722 
    723  out:
    724 	if (cvp != NULL) {
    725 		mutex_exit(&cpn->pn_sizemtx);
    726 
    727 		if (error || (cvp != vp))
    728 			vput(cvp);
    729 	}
    730 	if (error == 0)
    731 		VOP_UNLOCK(*ap->a_vpp);
    732 
    733 	if (cnp->cn_flags & ISDOTDOT)
    734 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    735 
    736 	DPRINTF(("puffs_lookup: returning %d %p\n", error, *ap->a_vpp));
    737 	PUFFS_MSG_RELEASE(lookup);
    738 	return error;
    739 }
    740 
    741 #define REFPN_AND_UNLOCKVP(a, b)					\
    742 do {									\
    743 	mutex_enter(&b->pn_mtx);					\
    744 	puffs_referencenode(b);						\
    745 	mutex_exit(&b->pn_mtx);						\
    746 	VOP_UNLOCK(a);						\
    747 } while (/*CONSTCOND*/0)
    748 
    749 #define REFPN(b)							\
    750 do {									\
    751 	mutex_enter(&b->pn_mtx);					\
    752 	puffs_referencenode(b);						\
    753 	mutex_exit(&b->pn_mtx);						\
    754 } while (/*CONSTCOND*/0)
    755 
    756 #define RELEPN_AND_VP(a, b)						\
    757 do {									\
    758 	puffs_releasenode(b);						\
    759 	vrele(a);							\
    760 } while (/*CONSTCOND*/0)
    761 
    762 int
    763 puffs_vnop_create(void *v)
    764 {
    765 	struct vop_create_v3_args /* {
    766 		const struct vnodeop_desc *a_desc;
    767 		struct vnode *a_dvp;
    768 		struct vnode **a_vpp;
    769 		struct componentname *a_cnp;
    770 		struct vattr *a_vap;
    771 	} */ *ap = v;
    772 	PUFFS_MSG_VARS(vn, create);
    773 	struct vnode *dvp = ap->a_dvp;
    774 	struct puffs_node *dpn = VPTOPP(dvp);
    775 	struct componentname *cnp = ap->a_cnp;
    776 	struct mount *mp = dvp->v_mount;
    777 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    778 	int error;
    779 
    780 	DPRINTF(("puffs_create: dvp %p, cnp: %s\n",
    781 	    dvp, ap->a_cnp->cn_nameptr));
    782 
    783 	PUFFS_MSG_ALLOC(vn, create);
    784 	puffs_makecn(&create_msg->pvnr_cn, &create_msg->pvnr_cn_cred,
    785 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
    786 	create_msg->pvnr_va = *ap->a_vap;
    787 	puffs_msg_setinfo(park_create, PUFFSOP_VN,
    788 	    PUFFS_VN_CREATE, VPTOPNC(dvp));
    789 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_create, dvp->v_data, NULL, error);
    790 
    791 	error = checkerr(pmp, error, __func__);
    792 	if (error)
    793 		goto out;
    794 
    795 	error = puffs_newnode(mp, dvp, ap->a_vpp,
    796 	    create_msg->pvnr_newnode, cnp, ap->a_vap->va_type, 0);
    797 	if (error) {
    798 		puffs_abortbutton(pmp, PUFFS_ABORT_CREATE, dpn->pn_cookie,
    799 		    create_msg->pvnr_newnode, cnp);
    800 		goto out;
    801 	}
    802 
    803 	if (PUFFS_USE_FS_TTL(pmp)) {
    804 		struct timespec *va_ttl = &create_msg->pvnr_va_ttl;
    805 		struct timespec *cn_ttl = &create_msg->pvnr_cn_ttl;
    806 		struct vattr *rvap = &create_msg->pvnr_va;
    807 
    808 		update_va(*ap->a_vpp, NULL, rvap,
    809 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
    810 	}
    811 
    812 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
    813 
    814 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
    815 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
    816 		update_parent(*ap->a_vpp, dvp);
    817 
    818  out:
    819 	DPRINTF(("puffs_create: return %d\n", error));
    820 	PUFFS_MSG_RELEASE(create);
    821 	return error;
    822 }
    823 
    824 int
    825 puffs_vnop_mknod(void *v)
    826 {
    827 	struct vop_mknod_v3_args /* {
    828 		const struct vnodeop_desc *a_desc;
    829 		struct vnode *a_dvp;
    830 		struct vnode **a_vpp;
    831 		struct componentname *a_cnp;
    832 		struct vattr *a_vap;
    833 	} */ *ap = v;
    834 	PUFFS_MSG_VARS(vn, mknod);
    835 	struct vnode *dvp = ap->a_dvp;
    836 	struct puffs_node *dpn = VPTOPP(dvp);
    837 	struct componentname *cnp = ap->a_cnp;
    838 	struct mount *mp = dvp->v_mount;
    839 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    840 	int error;
    841 
    842 	PUFFS_MSG_ALLOC(vn, mknod);
    843 	puffs_makecn(&mknod_msg->pvnr_cn, &mknod_msg->pvnr_cn_cred,
    844 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
    845 	mknod_msg->pvnr_va = *ap->a_vap;
    846 	puffs_msg_setinfo(park_mknod, PUFFSOP_VN,
    847 	    PUFFS_VN_MKNOD, VPTOPNC(dvp));
    848 
    849 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mknod, dvp->v_data, NULL, error);
    850 
    851 	error = checkerr(pmp, error, __func__);
    852 	if (error)
    853 		goto out;
    854 
    855 	error = puffs_newnode(mp, dvp, ap->a_vpp,
    856 	    mknod_msg->pvnr_newnode, cnp, ap->a_vap->va_type,
    857 	    ap->a_vap->va_rdev);
    858 	if (error) {
    859 		puffs_abortbutton(pmp, PUFFS_ABORT_MKNOD, dpn->pn_cookie,
    860 		    mknod_msg->pvnr_newnode, cnp);
    861 		goto out;
    862 	}
    863 
    864 	if (PUFFS_USE_FS_TTL(pmp)) {
    865 		struct timespec *va_ttl = &mknod_msg->pvnr_va_ttl;
    866 		struct timespec *cn_ttl = &mknod_msg->pvnr_cn_ttl;
    867 		struct vattr *rvap = &mknod_msg->pvnr_va;
    868 
    869 		update_va(*ap->a_vpp, NULL, rvap,
    870 			   va_ttl, cn_ttl, SETATTR_CHSIZE);
    871 	}
    872 
    873 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
    874 
    875 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
    876 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
    877 		update_parent(*ap->a_vpp, dvp);
    878 
    879  out:
    880 	PUFFS_MSG_RELEASE(mknod);
    881 	return error;
    882 }
    883 
    884 int
    885 puffs_vnop_open(void *v)
    886 {
    887 	struct vop_open_args /* {
    888 		const struct vnodeop_desc *a_desc;
    889 		struct vnode *a_vp;
    890 		int a_mode;
    891 		kauth_cred_t a_cred;
    892 	} */ *ap = v;
    893 	PUFFS_MSG_VARS(vn, open);
    894 	struct vnode *vp = ap->a_vp;
    895 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    896 	struct puffs_node *pn = VPTOPP(vp);
    897 	int mode = ap->a_mode;
    898 	int error;
    899 
    900 	DPRINTF(("puffs_open: vp %p, mode 0x%x\n", vp, mode));
    901 
    902 	if (vp->v_type == VREG && mode & FWRITE && !EXISTSOP(pmp, WRITE))
    903 		ERROUT(EROFS);
    904 
    905 	if (!EXISTSOP(pmp, OPEN))
    906 		ERROUT(0);
    907 
    908 	PUFFS_MSG_ALLOC(vn, open);
    909 	open_msg->pvnr_mode = mode;
    910 	puffs_credcvt(&open_msg->pvnr_cred, ap->a_cred);
    911 	puffs_msg_setinfo(park_open, PUFFSOP_VN,
    912 	    PUFFS_VN_OPEN, VPTOPNC(vp));
    913 
    914 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_open, vp->v_data, NULL, error);
    915 	error = checkerr(pmp, error, __func__);
    916 
    917 	if (open_msg->pvnr_oflags & PUFFS_OPEN_IO_DIRECT) {
    918 		if (mode & FREAD)
    919 			pn->pn_stat |= PNODE_RDIRECT;
    920 		if (mode & FWRITE)
    921 			pn->pn_stat |= PNODE_WDIRECT;
    922 	}
    923  out:
    924 	DPRINTF(("puffs_open: returning %d\n", error));
    925 	PUFFS_MSG_RELEASE(open);
    926 	return error;
    927 }
    928 
    929 int
    930 puffs_vnop_close(void *v)
    931 {
    932 	struct vop_close_args /* {
    933 		const struct vnodeop_desc *a_desc;
    934 		struct vnode *a_vp;
    935 		int a_fflag;
    936 		kauth_cred_t a_cred;
    937 	} */ *ap = v;
    938 	PUFFS_MSG_VARS(vn, close);
    939 	struct vnode *vp = ap->a_vp;
    940 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    941 
    942 	PUFFS_MSG_ALLOC(vn, close);
    943 	puffs_msg_setfaf(park_close);
    944 	close_msg->pvnr_fflag = ap->a_fflag;
    945 	puffs_credcvt(&close_msg->pvnr_cred, ap->a_cred);
    946 	puffs_msg_setinfo(park_close, PUFFSOP_VN,
    947 	    PUFFS_VN_CLOSE, VPTOPNC(vp));
    948 
    949 	puffs_msg_enqueue(pmp, park_close);
    950 	PUFFS_MSG_RELEASE(close);
    951 	return 0;
    952 }
    953 
    954 int
    955 puffs_vnop_access(void *v)
    956 {
    957 	struct vop_access_args /* {
    958 		const struct vnodeop_desc *a_desc;
    959 		struct vnode *a_vp;
    960 		int a_mode;
    961 		kauth_cred_t a_cred;
    962 	} */ *ap = v;
    963 	PUFFS_MSG_VARS(vn, access);
    964 	struct vnode *vp = ap->a_vp;
    965 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    966 	int mode = ap->a_mode;
    967 	int error;
    968 
    969 	if (mode & VWRITE) {
    970 		switch (vp->v_type) {
    971 		case VDIR:
    972 		case VLNK:
    973 		case VREG:
    974 			if ((vp->v_mount->mnt_flag & MNT_RDONLY)
    975 			    || !EXISTSOP(pmp, WRITE))
    976 				return EROFS;
    977 			break;
    978 		default:
    979 			break;
    980 		}
    981 	}
    982 
    983 	if (!EXISTSOP(pmp, ACCESS))
    984 		return 0;
    985 
    986 	PUFFS_MSG_ALLOC(vn, access);
    987 	access_msg->pvnr_mode = ap->a_mode;
    988 	puffs_credcvt(&access_msg->pvnr_cred, ap->a_cred);
    989 	puffs_msg_setinfo(park_access, PUFFSOP_VN,
    990 	    PUFFS_VN_ACCESS, VPTOPNC(vp));
    991 
    992 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_access, vp->v_data, NULL, error);
    993 	error = checkerr(pmp, error, __func__);
    994 	PUFFS_MSG_RELEASE(access);
    995 
    996 	return error;
    997 }
    998 
    999 static void
   1000 update_va(struct vnode *vp, struct vattr *vap, struct vattr *rvap,
   1001 	  struct timespec *va_ttl, struct timespec *cn_ttl, int flags)
   1002 {
   1003 	struct puffs_node *pn = VPTOPP(vp);
   1004 
   1005 	if (TTL_VALID(cn_ttl)) {
   1006 		pn->pn_cn_timeout = TTL_TO_TIMEOUT(cn_ttl);
   1007 		pn->pn_cn_grace = MAX(pn->pn_cn_timeout, pn->pn_cn_grace);
   1008 	}
   1009 
   1010 	/*
   1011 	 * Don't listen to the file server regarding special device
   1012 	 * size info, the file server doesn't know anything about them.
   1013 	 */
   1014 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1015 		rvap->va_size = vp->v_size;
   1016 
   1017 	/* Ditto for blocksize (ufs comment: this doesn't belong here) */
   1018 	if (vp->v_type == VBLK)
   1019 		rvap->va_blocksize = BLKDEV_IOSIZE;
   1020 	else if (vp->v_type == VCHR)
   1021 		rvap->va_blocksize = MAXBSIZE;
   1022 
   1023 	if (vap != NULL) {
   1024 		(void) memcpy(vap, rvap, sizeof(struct vattr));
   1025 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
   1026 
   1027 		if (pn->pn_stat & PNODE_METACACHE_ATIME)
   1028 			vap->va_atime = pn->pn_mc_atime;
   1029 		if (pn->pn_stat & PNODE_METACACHE_CTIME)
   1030 			vap->va_ctime = pn->pn_mc_ctime;
   1031 		if (pn->pn_stat & PNODE_METACACHE_MTIME)
   1032 			vap->va_mtime = pn->pn_mc_mtime;
   1033 		if (pn->pn_stat & PNODE_METACACHE_SIZE)
   1034 			vap->va_size = pn->pn_mc_size;
   1035 	}
   1036 
   1037 	if (!(pn->pn_stat & PNODE_METACACHE_SIZE) && (flags & SETATTR_CHSIZE)) {
   1038 		if (rvap->va_size != VNOVAL
   1039 		    && vp->v_type != VBLK && vp->v_type != VCHR) {
   1040 			uvm_vnp_setsize(vp, rvap->va_size);
   1041 			pn->pn_serversize = rvap->va_size;
   1042 		}
   1043 	}
   1044 
   1045 	if ((va_ttl != NULL) && TTL_VALID(va_ttl)) {
   1046 		if (pn->pn_va_cache == NULL)
   1047 			pn->pn_va_cache = pool_get(&puffs_vapool, PR_WAITOK);
   1048 
   1049 		(void)memcpy(pn->pn_va_cache, rvap, sizeof(*rvap));
   1050 
   1051 		pn->pn_va_timeout = TTL_TO_TIMEOUT(va_ttl);
   1052 	}
   1053 }
   1054 
   1055 static void
   1056 update_parent(struct vnode *vp, struct vnode *dvp)
   1057 {
   1058 	struct puffs_node *pn = VPTOPP(vp);
   1059 
   1060 	if (pn->pn_parent != NULL) {
   1061 		KASSERT(pn->pn_parent != dvp);
   1062 		vrele(pn->pn_parent);
   1063 	}
   1064 
   1065 	vref(dvp);
   1066 	pn->pn_parent = dvp;
   1067 }
   1068 
   1069 int
   1070 puffs_vnop_getattr(void *v)
   1071 {
   1072 	struct vop_getattr_args /* {
   1073 		const struct vnodeop_desc *a_desc;
   1074 		struct vnode *a_vp;
   1075 		struct vattr *a_vap;
   1076 		kauth_cred_t a_cred;
   1077 	} */ *ap = v;
   1078 	PUFFS_MSG_VARS(vn, getattr);
   1079 	struct vnode *vp = ap->a_vp;
   1080 	struct mount *mp = vp->v_mount;
   1081 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
   1082 	struct vattr *vap, *rvap;
   1083 	struct puffs_node *pn = VPTOPP(vp);
   1084 	struct timespec *va_ttl = NULL;
   1085 	int error = 0;
   1086 
   1087 	/*
   1088 	 * A lock is required so that we do not race with
   1089 	 * setattr, write and fsync when changing vp->v_size.
   1090 	 * This is critical, since setting a stall smaler value
   1091 	 * triggers a file truncate in uvm_vnp_setsize(), which
   1092 	 * most of the time means data corruption (a chunk of
   1093 	 * data is replaced by zeroes). This can be removed if
   1094 	 * we decide one day that VOP_GETATTR must operate on
   1095 	 * a locked vnode.
   1096 	 *
   1097 	 * XXX Should be useless now that VOP_GETATTR has been
   1098 	 *     fixed to always require a shared lock at least.
   1099 	 */
   1100 	mutex_enter(&pn->pn_sizemtx);
   1101 
   1102 	REFPN(pn);
   1103 	vap = ap->a_vap;
   1104 
   1105 	if (PUFFS_USE_FS_TTL(pmp)) {
   1106 		if (!TIMED_OUT(pn->pn_va_timeout)) {
   1107 			update_va(vp, vap, pn->pn_va_cache,
   1108 				  NULL, NULL, SETATTR_CHSIZE);
   1109 			goto out2;
   1110 		}
   1111 	}
   1112 
   1113 	PUFFS_MSG_ALLOC(vn, getattr);
   1114 	vattr_null(&getattr_msg->pvnr_va);
   1115 	puffs_credcvt(&getattr_msg->pvnr_cred, ap->a_cred);
   1116 	puffs_msg_setinfo(park_getattr, PUFFSOP_VN,
   1117 	    PUFFS_VN_GETATTR, VPTOPNC(vp));
   1118 
   1119 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_getattr, vp->v_data, NULL, error);
   1120 	error = checkerr(pmp, error, __func__);
   1121 	if (error)
   1122 		goto out;
   1123 
   1124 	rvap = &getattr_msg->pvnr_va;
   1125 
   1126 	if (PUFFS_USE_FS_TTL(pmp))
   1127 		va_ttl = &getattr_msg->pvnr_va_ttl;
   1128 
   1129 	update_va(vp, vap, rvap, va_ttl, NULL, SETATTR_CHSIZE);
   1130 
   1131  out:
   1132 	PUFFS_MSG_RELEASE(getattr);
   1133 
   1134  out2:
   1135 	puffs_releasenode(pn);
   1136 
   1137 	mutex_exit(&pn->pn_sizemtx);
   1138 
   1139 	return error;
   1140 }
   1141 
   1142 static int
   1143 dosetattr(struct vnode *vp, struct vattr *vap, kauth_cred_t cred, int flags)
   1144 {
   1145 	PUFFS_MSG_VARS(vn, setattr);
   1146 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1147 	struct puffs_node *pn = vp->v_data;
   1148 	int error = 0;
   1149 
   1150 	KASSERT(!(flags & SETATTR_CHSIZE) || mutex_owned(&pn->pn_sizemtx));
   1151 
   1152 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) &&
   1153 	    (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL
   1154 	    || vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL
   1155 	    || vap->va_mode != (mode_t)VNOVAL))
   1156 		return EROFS;
   1157 
   1158 	if ((vp->v_mount->mnt_flag & MNT_RDONLY)
   1159 	    && vp->v_type == VREG && vap->va_size != VNOVAL)
   1160 		return EROFS;
   1161 
   1162 	/*
   1163 	 * Flush metacache first.  If we are called with some explicit
   1164 	 * parameters, treat them as information overriding metacache
   1165 	 * information.
   1166 	 */
   1167 	if (pn->pn_stat & PNODE_METACACHE_MASK) {
   1168 		if ((pn->pn_stat & PNODE_METACACHE_ATIME)
   1169 		    && vap->va_atime.tv_sec == VNOVAL)
   1170 			vap->va_atime = pn->pn_mc_atime;
   1171 		if ((pn->pn_stat & PNODE_METACACHE_CTIME)
   1172 		    && vap->va_ctime.tv_sec == VNOVAL)
   1173 			vap->va_ctime = pn->pn_mc_ctime;
   1174 		if ((pn->pn_stat & PNODE_METACACHE_MTIME)
   1175 		    && vap->va_mtime.tv_sec == VNOVAL)
   1176 			vap->va_mtime = pn->pn_mc_mtime;
   1177 		if ((pn->pn_stat & PNODE_METACACHE_SIZE)
   1178 		    && vap->va_size == VNOVAL)
   1179 			vap->va_size = pn->pn_mc_size;
   1180 
   1181 		pn->pn_stat &= ~PNODE_METACACHE_MASK;
   1182 	}
   1183 
   1184 	/*
   1185 	 * Flush attribute cache so that another thread do
   1186 	 * not get a stale value during the operation.
   1187 	 */
   1188 	if (PUFFS_USE_FS_TTL(pmp))
   1189 		pn->pn_va_timeout = 0;
   1190 
   1191 	PUFFS_MSG_ALLOC(vn, setattr);
   1192 	(void)memcpy(&setattr_msg->pvnr_va, vap, sizeof(struct vattr));
   1193 	puffs_credcvt(&setattr_msg->pvnr_cred, cred);
   1194 	puffs_msg_setinfo(park_setattr, PUFFSOP_VN,
   1195 	    PUFFS_VN_SETATTR, VPTOPNC(vp));
   1196 	if (flags & SETATTR_ASYNC)
   1197 		puffs_msg_setfaf(park_setattr);
   1198 
   1199 	puffs_msg_enqueue(pmp, park_setattr);
   1200 	if ((flags & SETATTR_ASYNC) == 0)
   1201 		error = puffs_msg_wait2(pmp, park_setattr, vp->v_data, NULL);
   1202 
   1203 	if ((error == 0) && PUFFS_USE_FS_TTL(pmp)) {
   1204 		struct timespec *va_ttl = &setattr_msg->pvnr_va_ttl;
   1205 		struct vattr *rvap = &setattr_msg->pvnr_va;
   1206 
   1207 		update_va(vp, NULL, rvap, va_ttl, NULL, flags);
   1208 	}
   1209 
   1210 	PUFFS_MSG_RELEASE(setattr);
   1211 	if ((flags & SETATTR_ASYNC) == 0) {
   1212 		error = checkerr(pmp, error, __func__);
   1213 		if (error)
   1214 			return error;
   1215 	} else {
   1216 		error = 0;
   1217 	}
   1218 
   1219 	if (vap->va_size != VNOVAL) {
   1220 		pn->pn_serversize = vap->va_size;
   1221 		if (flags & SETATTR_CHSIZE)
   1222 			uvm_vnp_setsize(vp, vap->va_size);
   1223 	}
   1224 
   1225 	return 0;
   1226 }
   1227 
   1228 int
   1229 puffs_vnop_setattr(void *v)
   1230 {
   1231 	struct vop_getattr_args /* {
   1232 		const struct vnodeop_desc *a_desc;
   1233 		struct vnode *a_vp;
   1234 		struct vattr *a_vap;
   1235 		kauth_cred_t a_cred;
   1236 	} */ *ap = v;
   1237 	struct puffs_node *pn = ap->a_vp->v_data;
   1238 	int error;
   1239 
   1240 	mutex_enter(&pn->pn_sizemtx);
   1241 	error = dosetattr(ap->a_vp, ap->a_vap, ap->a_cred, SETATTR_CHSIZE);
   1242 	mutex_exit(&pn->pn_sizemtx);
   1243 
   1244 	return error;
   1245 }
   1246 
   1247 static __inline int
   1248 doinact(struct puffs_mount *pmp, int iaflag)
   1249 {
   1250 
   1251 	if (EXISTSOP(pmp, INACTIVE))
   1252 		if (pmp->pmp_flags & PUFFS_KFLAG_IAONDEMAND)
   1253 			if (iaflag || ALLOPS(pmp))
   1254 				return 1;
   1255 			else
   1256 				return 0;
   1257 		else
   1258 			return 1;
   1259 	else
   1260 		return 0;
   1261 }
   1262 
   1263 static void
   1264 callinactive(struct puffs_mount *pmp, puffs_cookie_t ck, int iaflag)
   1265 {
   1266 	PUFFS_MSG_VARS(vn, inactive);
   1267 
   1268 	if (doinact(pmp, iaflag)) {
   1269 		PUFFS_MSG_ALLOC(vn, inactive);
   1270 		puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
   1271 		    PUFFS_VN_INACTIVE, ck);
   1272 		PUFFS_MSG_ENQUEUEWAIT_NOERROR(pmp, park_inactive);
   1273 		PUFFS_MSG_RELEASE(inactive);
   1274 	}
   1275 }
   1276 
   1277 /* XXX: callinactive can't setback */
   1278 int
   1279 puffs_vnop_inactive(void *v)
   1280 {
   1281 	struct vop_inactive_args /* {
   1282 		const struct vnodeop_desc *a_desc;
   1283 		struct vnode *a_vp;
   1284 	} */ *ap = v;
   1285 	PUFFS_MSG_VARS(vn, inactive);
   1286 	struct vnode *vp = ap->a_vp;
   1287 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1288 	struct puffs_node *pnode;
   1289 	bool recycle = false;
   1290 
   1291 	pnode = vp->v_data;
   1292 	mutex_enter(&pnode->pn_sizemtx);
   1293 
   1294 	if (doinact(pmp, pnode->pn_stat & PNODE_DOINACT)) {
   1295 		flushvncache(vp, 0, 0, false);
   1296 		PUFFS_MSG_ALLOC(vn, inactive);
   1297 		puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
   1298 		    PUFFS_VN_INACTIVE, VPTOPNC(vp));
   1299 		PUFFS_MSG_ENQUEUEWAIT2_NOERROR(pmp, park_inactive, vp->v_data,
   1300 		    NULL);
   1301 		PUFFS_MSG_RELEASE(inactive);
   1302 	}
   1303 	pnode->pn_stat &= ~PNODE_DOINACT;
   1304 
   1305 	/*
   1306 	 * file server thinks it's gone?  then don't be afraid care,
   1307 	 * node's life was already all it would ever be
   1308 	 */
   1309 	if (pnode->pn_stat & PNODE_NOREFS) {
   1310 		pnode->pn_stat |= PNODE_DYING;
   1311 		recycle = true;
   1312 	}
   1313 
   1314 	/*
   1315 	 * Handle node TTL.
   1316 	 * If grace has already timed out, make it reclaimed.
   1317 	 * Otherwise, we queue its expiration by sop thread, so
   1318 	 * that it does not remain for ages in the freelist,
   1319 	 * holding memory in userspace, while we will have
   1320 	 * to look it up again anyway.
   1321 	 */
   1322 	if (PUFFS_USE_FS_TTL(pmp) && !(vp->v_vflag & VV_ROOT) && !recycle) {
   1323 		bool incache = !TIMED_OUT(pnode->pn_cn_timeout);
   1324 		bool ingrace = !TIMED_OUT(pnode->pn_cn_grace);
   1325 		bool reclaimqueued = pnode->pn_stat & PNODE_SOPEXP;
   1326 
   1327 		if (!incache && !ingrace && !reclaimqueued) {
   1328 			pnode->pn_stat |= PNODE_DYING;
   1329 			recycle = true;
   1330 		}
   1331 
   1332 		if (!recycle && !reclaimqueued) {
   1333 			struct puffs_sopreq *psopr;
   1334 			int at = MAX(pnode->pn_cn_grace, pnode->pn_cn_timeout);
   1335 
   1336 			KASSERT(curlwp != uvm.pagedaemon_lwp);
   1337 			psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
   1338 			psopr->psopr_ck = VPTOPNC(pnode->pn_vp);
   1339 			psopr->psopr_sopreq = PUFFS_SOPREQ_EXPIRE;
   1340 			psopr->psopr_at = at;
   1341 
   1342 			mutex_enter(&pmp->pmp_sopmtx);
   1343 
   1344 			/*
   1345 			 * If thread has disapeared, just give up. The
   1346 			 * fs is being unmounted and the node will be
   1347 			 * be reclaimed anyway.
   1348 			 *
   1349 			 * Otherwise, we queue the request but do not
   1350 			 * immediatly signal the thread, as the node
   1351 			 * has not been expired yet.
   1352 			 */
   1353 			if (pmp->pmp_sopthrcount == 0) {
   1354 				kmem_free(psopr, sizeof(*psopr));
   1355 			} else {
   1356 				TAILQ_INSERT_TAIL(&pmp->pmp_sopnodereqs,
   1357 				    psopr, psopr_entries);
   1358 				pnode->pn_stat |= PNODE_SOPEXP;
   1359 			}
   1360 
   1361 			mutex_exit(&pmp->pmp_sopmtx);
   1362 		}
   1363 	}
   1364 
   1365 	*ap->a_recycle = recycle;
   1366 
   1367 	mutex_exit(&pnode->pn_sizemtx);
   1368 	VOP_UNLOCK(vp);
   1369 
   1370 	return 0;
   1371 }
   1372 
   1373 static void
   1374 callreclaim(struct puffs_mount *pmp, puffs_cookie_t ck, int nlookup)
   1375 {
   1376 	PUFFS_MSG_VARS(vn, reclaim);
   1377 
   1378 	if (!EXISTSOP(pmp, RECLAIM))
   1379 		return;
   1380 
   1381 	PUFFS_MSG_ALLOC(vn, reclaim);
   1382 	reclaim_msg->pvnr_nlookup = nlookup;
   1383 	puffs_msg_setfaf(park_reclaim);
   1384 	puffs_msg_setinfo(park_reclaim, PUFFSOP_VN, PUFFS_VN_RECLAIM, ck);
   1385 
   1386 	puffs_msg_enqueue(pmp, park_reclaim);
   1387 	PUFFS_MSG_RELEASE(reclaim);
   1388 	return;
   1389 }
   1390 
   1391 /*
   1392  * always FAF, we don't really care if the server wants to fail to
   1393  * reclaim the node or not
   1394  */
   1395 int
   1396 puffs_vnop_reclaim(void *v)
   1397 {
   1398 	struct vop_reclaim_args /* {
   1399 		const struct vnodeop_desc *a_desc;
   1400 		struct vnode *a_vp;
   1401 	} */ *ap = v;
   1402 	struct vnode *vp = ap->a_vp;
   1403 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1404 	struct puffs_node *pnode = vp->v_data;
   1405 	bool notifyserver = true;
   1406 
   1407 	/*
   1408 	 * first things first: check if someone is trying to reclaim the
   1409 	 * root vnode.  do not allow that to travel to userspace.
   1410 	 * Note that we don't need to take the lock similarly to
   1411 	 * puffs_root(), since there is only one of us.
   1412 	 */
   1413 	if (vp->v_vflag & VV_ROOT) {
   1414 		mutex_enter(&pmp->pmp_lock);
   1415 		KASSERT(pmp->pmp_root != NULL);
   1416 		pmp->pmp_root = NULL;
   1417 		mutex_exit(&pmp->pmp_lock);
   1418 		notifyserver = false;
   1419 	}
   1420 
   1421 	/*
   1422 	 * purge info from kernel before issueing FAF, since we
   1423 	 * don't really know when we'll get around to it after
   1424 	 * that and someone might race us into node creation
   1425 	 */
   1426 	mutex_enter(&pmp->pmp_lock);
   1427 	LIST_REMOVE(pnode, pn_hashent);
   1428 	if (PUFFS_USE_NAMECACHE(pmp))
   1429 		cache_purge(vp);
   1430 	mutex_exit(&pmp->pmp_lock);
   1431 
   1432 	if (notifyserver) {
   1433 		int nlookup = VPTOPP(vp)->pn_nlookup;
   1434 
   1435 		callreclaim(MPTOPUFFSMP(vp->v_mount), VPTOPNC(vp), nlookup);
   1436 	}
   1437 
   1438 	if (PUFFS_USE_DOTDOTCACHE(pmp)) {
   1439 		if (__predict_true(VPTOPP(vp)->pn_parent != NULL))
   1440 			vrele(VPTOPP(vp)->pn_parent);
   1441 		else
   1442 			KASSERT(vp->v_vflag & VV_ROOT);
   1443 	}
   1444 
   1445 	puffs_putvnode(vp);
   1446 	vp->v_data = NULL;
   1447 
   1448 	return 0;
   1449 }
   1450 
   1451 #define CSIZE sizeof(**ap->a_cookies)
   1452 int
   1453 puffs_vnop_readdir(void *v)
   1454 {
   1455 	struct vop_readdir_args /* {
   1456 		const struct vnodeop_desc *a_desc;
   1457 		struct vnode *a_vp;
   1458 		struct uio *a_uio;
   1459 		kauth_cred_t a_cred;
   1460 		int *a_eofflag;
   1461 		off_t **a_cookies;
   1462 		int *a_ncookies;
   1463 	} */ *ap = v;
   1464 	PUFFS_MSG_VARS(vn, readdir);
   1465 	struct vnode *vp = ap->a_vp;
   1466 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1467 	size_t argsize, tomove, cookiemem, cookiesmax;
   1468 	struct uio *uio = ap->a_uio;
   1469 	size_t howmuch, resid;
   1470 	int error;
   1471 
   1472 	/*
   1473 	 * ok, so we need: resid + cookiemem = maxreq
   1474 	 * => resid + cookiesize * (resid/minsize) = maxreq
   1475 	 * => resid + cookiesize/minsize * resid = maxreq
   1476 	 * => (cookiesize/minsize + 1) * resid = maxreq
   1477 	 * => resid = maxreq / (cookiesize/minsize + 1)
   1478 	 *
   1479 	 * Since cookiesize <= minsize and we're not very big on floats,
   1480 	 * we approximate that to be 1.  Therefore:
   1481 	 *
   1482 	 * resid = maxreq / 2;
   1483 	 *
   1484 	 * Well, at least we didn't have to use differential equations
   1485 	 * or the Gram-Schmidt process.
   1486 	 *
   1487 	 * (yes, I'm very afraid of this)
   1488 	 */
   1489 	KASSERT(CSIZE <= _DIRENT_MINSIZE((struct dirent *)0));
   1490 
   1491 	if (ap->a_cookies) {
   1492 		KASSERT(ap->a_ncookies != NULL);
   1493 		if (pmp->pmp_args.pa_fhsize == 0)
   1494 			return EOPNOTSUPP;
   1495 		resid = PUFFS_TOMOVE(uio->uio_resid, pmp) / 2;
   1496 		cookiesmax = resid/_DIRENT_MINSIZE((struct dirent *)0);
   1497 		cookiemem = ALIGN(cookiesmax*CSIZE); /* play safe */
   1498 	} else {
   1499 		resid = PUFFS_TOMOVE(uio->uio_resid, pmp);
   1500 		cookiesmax = 0;
   1501 		cookiemem = 0;
   1502 	}
   1503 
   1504 	argsize = sizeof(struct puffs_vnmsg_readdir);
   1505 	tomove = resid + cookiemem;
   1506 	puffs_msgmem_alloc(argsize + tomove, &park_readdir,
   1507 	    (void *)&readdir_msg, 1);
   1508 
   1509 	puffs_credcvt(&readdir_msg->pvnr_cred, ap->a_cred);
   1510 	readdir_msg->pvnr_offset = uio->uio_offset;
   1511 	readdir_msg->pvnr_resid = resid;
   1512 	readdir_msg->pvnr_ncookies = cookiesmax;
   1513 	readdir_msg->pvnr_eofflag = 0;
   1514 	readdir_msg->pvnr_dentoff = cookiemem;
   1515 	puffs_msg_setinfo(park_readdir, PUFFSOP_VN,
   1516 	    PUFFS_VN_READDIR, VPTOPNC(vp));
   1517 	puffs_msg_setdelta(park_readdir, tomove);
   1518 
   1519 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_readdir, vp->v_data, NULL, error);
   1520 	error = checkerr(pmp, error, __func__);
   1521 	if (error)
   1522 		goto out;
   1523 
   1524 	/* userspace is cheating? */
   1525 	if (readdir_msg->pvnr_resid > resid) {
   1526 		puffs_senderr(pmp, PUFFS_ERR_READDIR, E2BIG,
   1527 		    "resid grew", VPTOPNC(vp));
   1528 		ERROUT(EPROTO);
   1529 	}
   1530 	if (readdir_msg->pvnr_ncookies > cookiesmax) {
   1531 		puffs_senderr(pmp, PUFFS_ERR_READDIR, E2BIG,
   1532 		    "too many cookies", VPTOPNC(vp));
   1533 		ERROUT(EPROTO);
   1534 	}
   1535 
   1536 	/* check eof */
   1537 	if (readdir_msg->pvnr_eofflag)
   1538 		*ap->a_eofflag = 1;
   1539 
   1540 	/* bouncy-wouncy with the directory data */
   1541 	howmuch = resid - readdir_msg->pvnr_resid;
   1542 
   1543 	/* force eof if no data was returned (getcwd() needs this) */
   1544 	if (howmuch == 0) {
   1545 		*ap->a_eofflag = 1;
   1546 		goto out;
   1547 	}
   1548 
   1549 	error = uiomove(readdir_msg->pvnr_data + cookiemem, howmuch, uio);
   1550 	if (error)
   1551 		goto out;
   1552 
   1553 	/* provide cookies to caller if so desired */
   1554 	if (ap->a_cookies) {
   1555 		KASSERT(curlwp != uvm.pagedaemon_lwp);
   1556 		*ap->a_cookies = malloc(readdir_msg->pvnr_ncookies*CSIZE,
   1557 		    M_TEMP, M_WAITOK);
   1558 		*ap->a_ncookies = readdir_msg->pvnr_ncookies;
   1559 		memcpy(*ap->a_cookies, readdir_msg->pvnr_data,
   1560 		    *ap->a_ncookies*CSIZE);
   1561 	}
   1562 
   1563 	/* next readdir starts here */
   1564 	uio->uio_offset = readdir_msg->pvnr_offset;
   1565 
   1566  out:
   1567 	puffs_msgmem_release(park_readdir);
   1568 	return error;
   1569 }
   1570 #undef CSIZE
   1571 
   1572 /*
   1573  * poll works by consuming the bitmask in pn_revents.  If there are
   1574  * events available, poll returns immediately.  If not, it issues a
   1575  * poll to userspace, selrecords itself and returns with no available
   1576  * events.  When the file server returns, it executes puffs_parkdone_poll(),
   1577  * where available events are added to the bitmask.  selnotify() is
   1578  * then also executed by that function causing us to enter here again
   1579  * and hopefully find the missing bits (unless someone got them first,
   1580  * in which case it starts all over again).
   1581  */
   1582 int
   1583 puffs_vnop_poll(void *v)
   1584 {
   1585 	struct vop_poll_args /* {
   1586 		const struct vnodeop_desc *a_desc;
   1587 		struct vnode *a_vp;
   1588 		int a_events;
   1589 	} */ *ap = v;
   1590 	PUFFS_MSG_VARS(vn, poll);
   1591 	struct vnode *vp = ap->a_vp;
   1592 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1593 	struct puffs_node *pn = vp->v_data;
   1594 	int events;
   1595 
   1596 	if (EXISTSOP(pmp, POLL)) {
   1597 		mutex_enter(&pn->pn_mtx);
   1598 		events = pn->pn_revents & ap->a_events;
   1599 		if (events & ap->a_events) {
   1600 			pn->pn_revents &= ~ap->a_events;
   1601 			mutex_exit(&pn->pn_mtx);
   1602 
   1603 			return events;
   1604 		} else {
   1605 			puffs_referencenode(pn);
   1606 			mutex_exit(&pn->pn_mtx);
   1607 
   1608 			PUFFS_MSG_ALLOC(vn, poll);
   1609 			poll_msg->pvnr_events = ap->a_events;
   1610 			puffs_msg_setinfo(park_poll, PUFFSOP_VN,
   1611 			    PUFFS_VN_POLL, VPTOPNC(vp));
   1612 			puffs_msg_setcall(park_poll, puffs_parkdone_poll, pn);
   1613 			selrecord(curlwp, &pn->pn_sel);
   1614 
   1615 			PUFFS_MSG_ENQUEUEWAIT2_NOERROR(pmp, park_poll,
   1616 			    vp->v_data, NULL);
   1617 			PUFFS_MSG_RELEASE(poll);
   1618 
   1619 			return 0;
   1620 		}
   1621 	} else {
   1622 		return genfs_poll(v);
   1623 	}
   1624 }
   1625 
   1626 static int
   1627 flushvncache(struct vnode *vp, off_t offlo, off_t offhi, bool wait)
   1628 {
   1629 	struct puffs_node *pn = VPTOPP(vp);
   1630 	struct vattr va;
   1631 	int pflags, error;
   1632 
   1633 	/* flush out information from our metacache, see vop_setattr */
   1634 	if (pn->pn_stat & PNODE_METACACHE_MASK
   1635 	    && (pn->pn_stat & PNODE_DYING) == 0) {
   1636 		vattr_null(&va);
   1637 		error = dosetattr(vp, &va, FSCRED,
   1638 		    SETATTR_CHSIZE | (wait ? 0 : SETATTR_ASYNC));
   1639 		if (error)
   1640 			return error;
   1641 	}
   1642 
   1643 	/*
   1644 	 * flush pages to avoid being overly dirty
   1645 	 */
   1646 	pflags = PGO_CLEANIT;
   1647 	if (wait)
   1648 		pflags |= PGO_SYNCIO;
   1649 
   1650 	mutex_enter(vp->v_interlock);
   1651 	return VOP_PUTPAGES(vp, trunc_page(offlo), round_page(offhi), pflags);
   1652 }
   1653 
   1654 int
   1655 puffs_vnop_fsync(void *v)
   1656 {
   1657 	struct vop_fsync_args /* {
   1658 		const struct vnodeop_desc *a_desc;
   1659 		struct vnode *a_vp;
   1660 		kauth_cred_t a_cred;
   1661 		int a_flags;
   1662 		off_t a_offlo;
   1663 		off_t a_offhi;
   1664 	} */ *ap = v;
   1665 	PUFFS_MSG_VARS(vn, fsync);
   1666 	struct vnode *vp;
   1667 	struct puffs_node *pn;
   1668 	struct puffs_mount *pmp;
   1669 	int error, dofaf;
   1670 
   1671 	vp = ap->a_vp;
   1672 	KASSERT(vp != NULL);
   1673 	pn = VPTOPP(vp);
   1674 	KASSERT(pn != NULL);
   1675 	pmp = MPTOPUFFSMP(vp->v_mount);
   1676 	if (ap->a_flags & FSYNC_WAIT) {
   1677 		mutex_enter(&pn->pn_sizemtx);
   1678 	} else {
   1679 		if (mutex_tryenter(&pn->pn_sizemtx) == 0)
   1680 			return EDEADLK;
   1681 	}
   1682 
   1683 	error = flushvncache(vp, ap->a_offlo, ap->a_offhi,
   1684 	    (ap->a_flags & FSYNC_WAIT) == FSYNC_WAIT);
   1685 	if (error)
   1686 		goto out;
   1687 
   1688 	/*
   1689 	 * HELLO!  We exit already here if the user server does not
   1690 	 * support fsync OR if we should call fsync for a node which
   1691 	 * has references neither in the kernel or the fs server.
   1692 	 * Otherwise we continue to issue fsync() forward.
   1693 	 */
   1694 	error = 0;
   1695 	if (!EXISTSOP(pmp, FSYNC) || (pn->pn_stat & PNODE_DYING))
   1696 		goto out;
   1697 
   1698 	dofaf = (ap->a_flags & FSYNC_WAIT) == 0 || ap->a_flags == FSYNC_LAZY;
   1699 	/*
   1700 	 * We abuse VXLOCK to mean "vnode is going to die", so we issue
   1701 	 * only FAFs for those.  Otherwise there's a danger of deadlock,
   1702 	 * since the execution context here might be the user server
   1703 	 * doing some operation on another fs, which in turn caused a
   1704 	 * vnode to be reclaimed from the freelist for this fs.
   1705 	 */
   1706 	if (dofaf == 0) {
   1707 		mutex_enter(vp->v_interlock);
   1708 		if (vdead_check(vp, VDEAD_NOWAIT) != 0)
   1709 			dofaf = 1;
   1710 		mutex_exit(vp->v_interlock);
   1711 	}
   1712 
   1713 	PUFFS_MSG_ALLOC(vn, fsync);
   1714 	if (dofaf)
   1715 		puffs_msg_setfaf(park_fsync);
   1716 
   1717 	puffs_credcvt(&fsync_msg->pvnr_cred, ap->a_cred);
   1718 	fsync_msg->pvnr_flags = ap->a_flags;
   1719 	fsync_msg->pvnr_offlo = ap->a_offlo;
   1720 	fsync_msg->pvnr_offhi = ap->a_offhi;
   1721 	puffs_msg_setinfo(park_fsync, PUFFSOP_VN,
   1722 	    PUFFS_VN_FSYNC, VPTOPNC(vp));
   1723 
   1724 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_fsync, vp->v_data, NULL, error);
   1725 	PUFFS_MSG_RELEASE(fsync);
   1726 
   1727 	error = checkerr(pmp, error, __func__);
   1728 
   1729 out:
   1730 	mutex_exit(&pn->pn_sizemtx);
   1731 	return error;
   1732 }
   1733 
   1734 int
   1735 puffs_vnop_seek(void *v)
   1736 {
   1737 	struct vop_seek_args /* {
   1738 		const struct vnodeop_desc *a_desc;
   1739 		struct vnode *a_vp;
   1740 		off_t a_oldoff;
   1741 		off_t a_newoff;
   1742 		kauth_cred_t a_cred;
   1743 	} */ *ap = v;
   1744 	PUFFS_MSG_VARS(vn, seek);
   1745 	struct vnode *vp = ap->a_vp;
   1746 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1747 	int error;
   1748 
   1749 	PUFFS_MSG_ALLOC(vn, seek);
   1750 	seek_msg->pvnr_oldoff = ap->a_oldoff;
   1751 	seek_msg->pvnr_newoff = ap->a_newoff;
   1752 	puffs_credcvt(&seek_msg->pvnr_cred, ap->a_cred);
   1753 	puffs_msg_setinfo(park_seek, PUFFSOP_VN,
   1754 	    PUFFS_VN_SEEK, VPTOPNC(vp));
   1755 
   1756 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_seek, vp->v_data, NULL, error);
   1757 	PUFFS_MSG_RELEASE(seek);
   1758 	return checkerr(pmp, error, __func__);
   1759 }
   1760 
   1761 static int
   1762 callremove(struct puffs_mount *pmp, puffs_cookie_t dck, puffs_cookie_t ck,
   1763 	struct componentname *cnp)
   1764 {
   1765 	PUFFS_MSG_VARS(vn, remove);
   1766 	int error;
   1767 
   1768 	PUFFS_MSG_ALLOC(vn, remove);
   1769 	remove_msg->pvnr_cookie_targ = ck;
   1770 	puffs_makecn(&remove_msg->pvnr_cn, &remove_msg->pvnr_cn_cred,
   1771 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1772 	puffs_msg_setinfo(park_remove, PUFFSOP_VN, PUFFS_VN_REMOVE, dck);
   1773 
   1774 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_remove, error);
   1775 	PUFFS_MSG_RELEASE(remove);
   1776 
   1777 	return checkerr(pmp, error, __func__);
   1778 }
   1779 
   1780 /*
   1781  * XXX: can't use callremove now because can't catch setbacks with
   1782  * it due to lack of a pnode argument.
   1783  */
   1784 int
   1785 puffs_vnop_remove(void *v)
   1786 {
   1787 	struct vop_remove_args /* {
   1788 		const struct vnodeop_desc *a_desc;
   1789 		struct vnode *a_dvp;
   1790 		struct vnode *a_vp;
   1791 		struct componentname *a_cnp;
   1792 	} */ *ap = v;
   1793 	PUFFS_MSG_VARS(vn, remove);
   1794 	struct vnode *dvp = ap->a_dvp;
   1795 	struct vnode *vp = ap->a_vp;
   1796 	struct puffs_node *dpn = VPTOPP(dvp);
   1797 	struct puffs_node *pn = VPTOPP(vp);
   1798 	struct componentname *cnp = ap->a_cnp;
   1799 	struct mount *mp = dvp->v_mount;
   1800 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
   1801 	int error;
   1802 
   1803 	PUFFS_MSG_ALLOC(vn, remove);
   1804 	remove_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1805 	puffs_makecn(&remove_msg->pvnr_cn, &remove_msg->pvnr_cn_cred,
   1806 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1807 	puffs_msg_setinfo(park_remove, PUFFSOP_VN,
   1808 	    PUFFS_VN_REMOVE, VPTOPNC(dvp));
   1809 
   1810 	puffs_msg_enqueue(pmp, park_remove);
   1811 	REFPN_AND_UNLOCKVP(dvp, dpn);
   1812 	if (dvp == vp)
   1813 		REFPN(pn);
   1814 	else
   1815 		REFPN_AND_UNLOCKVP(vp, pn);
   1816 	error = puffs_msg_wait2(pmp, park_remove, dpn, pn);
   1817 
   1818 	PUFFS_MSG_RELEASE(remove);
   1819 
   1820 	RELEPN_AND_VP(dvp, dpn);
   1821 	RELEPN_AND_VP(vp, pn);
   1822 
   1823 	error = checkerr(pmp, error, __func__);
   1824 	return error;
   1825 }
   1826 
   1827 int
   1828 puffs_vnop_mkdir(void *v)
   1829 {
   1830 	struct vop_mkdir_v3_args /* {
   1831 		const struct vnodeop_desc *a_desc;
   1832 		struct vnode *a_dvp;
   1833 		struct vnode **a_vpp;
   1834 		struct componentname *a_cnp;
   1835 		struct vattr *a_vap;
   1836 	} */ *ap = v;
   1837 	PUFFS_MSG_VARS(vn, mkdir);
   1838 	struct vnode *dvp = ap->a_dvp;
   1839 	struct puffs_node *dpn = VPTOPP(dvp);
   1840 	struct componentname *cnp = ap->a_cnp;
   1841 	struct mount *mp = dvp->v_mount;
   1842 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
   1843 	int error;
   1844 
   1845 	PUFFS_MSG_ALLOC(vn, mkdir);
   1846 	puffs_makecn(&mkdir_msg->pvnr_cn, &mkdir_msg->pvnr_cn_cred,
   1847 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1848 	mkdir_msg->pvnr_va = *ap->a_vap;
   1849 	puffs_msg_setinfo(park_mkdir, PUFFSOP_VN,
   1850 	    PUFFS_VN_MKDIR, VPTOPNC(dvp));
   1851 
   1852 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mkdir, dvp->v_data, NULL, error);
   1853 
   1854 	error = checkerr(pmp, error, __func__);
   1855 	if (error)
   1856 		goto out;
   1857 
   1858 	error = puffs_newnode(mp, dvp, ap->a_vpp,
   1859 	    mkdir_msg->pvnr_newnode, cnp, VDIR, 0);
   1860 	if (error) {
   1861 		puffs_abortbutton(pmp, PUFFS_ABORT_MKDIR, dpn->pn_cookie,
   1862 		    mkdir_msg->pvnr_newnode, cnp);
   1863 		goto out;
   1864 	}
   1865 
   1866 	if (PUFFS_USE_FS_TTL(pmp)) {
   1867 		struct timespec *va_ttl = &mkdir_msg->pvnr_va_ttl;
   1868 		struct timespec *cn_ttl = &mkdir_msg->pvnr_cn_ttl;
   1869 		struct vattr *rvap = &mkdir_msg->pvnr_va;
   1870 
   1871 		update_va(*ap->a_vpp, NULL, rvap,
   1872 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
   1873 	}
   1874 
   1875 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
   1876 
   1877 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   1878 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
   1879 		update_parent(*ap->a_vpp, dvp);
   1880 
   1881  out:
   1882 	PUFFS_MSG_RELEASE(mkdir);
   1883 	return error;
   1884 }
   1885 
   1886 static int
   1887 callrmdir(struct puffs_mount *pmp, puffs_cookie_t dck, puffs_cookie_t ck,
   1888 	struct componentname *cnp)
   1889 {
   1890 	PUFFS_MSG_VARS(vn, rmdir);
   1891 	int error;
   1892 
   1893 	PUFFS_MSG_ALLOC(vn, rmdir);
   1894 	rmdir_msg->pvnr_cookie_targ = ck;
   1895 	puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
   1896 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1897 	puffs_msg_setinfo(park_rmdir, PUFFSOP_VN, PUFFS_VN_RMDIR, dck);
   1898 
   1899 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_rmdir, error);
   1900 	PUFFS_MSG_RELEASE(rmdir);
   1901 
   1902 	return checkerr(pmp, error, __func__);
   1903 }
   1904 
   1905 int
   1906 puffs_vnop_rmdir(void *v)
   1907 {
   1908 	struct vop_rmdir_args /* {
   1909 		const struct vnodeop_desc *a_desc;
   1910 		struct vnode *a_dvp;
   1911 		struct vnode *a_vp;
   1912 		struct componentname *a_cnp;
   1913 	} */ *ap = v;
   1914 	PUFFS_MSG_VARS(vn, rmdir);
   1915 	struct vnode *dvp = ap->a_dvp;
   1916 	struct vnode *vp = ap->a_vp;
   1917 	struct puffs_node *dpn = VPTOPP(dvp);
   1918 	struct puffs_node *pn = VPTOPP(vp);
   1919 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   1920 	struct componentname *cnp = ap->a_cnp;
   1921 	int error;
   1922 
   1923 	PUFFS_MSG_ALLOC(vn, rmdir);
   1924 	rmdir_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1925 	puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
   1926 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1927 	puffs_msg_setinfo(park_rmdir, PUFFSOP_VN,
   1928 	    PUFFS_VN_RMDIR, VPTOPNC(dvp));
   1929 
   1930 	puffs_msg_enqueue(pmp, park_rmdir);
   1931 	REFPN_AND_UNLOCKVP(dvp, dpn);
   1932 	REFPN_AND_UNLOCKVP(vp, pn);
   1933 	error = puffs_msg_wait2(pmp, park_rmdir, dpn, pn);
   1934 
   1935 	PUFFS_MSG_RELEASE(rmdir);
   1936 
   1937 	/* XXX: some call cache_purge() *for both vnodes* here, investigate */
   1938 	RELEPN_AND_VP(dvp, dpn);
   1939 	RELEPN_AND_VP(vp, pn);
   1940 
   1941 	return error;
   1942 }
   1943 
   1944 int
   1945 puffs_vnop_link(void *v)
   1946 {
   1947 	struct vop_link_args /* {
   1948 		const struct vnodeop_desc *a_desc;
   1949 		struct vnode *a_dvp;
   1950 		struct vnode *a_vp;
   1951 		struct componentname *a_cnp;
   1952 	} */ *ap = v;
   1953 	PUFFS_MSG_VARS(vn, link);
   1954 	struct vnode *dvp = ap->a_dvp;
   1955 	struct vnode *vp = ap->a_vp;
   1956 	struct puffs_node *dpn = VPTOPP(dvp);
   1957 	struct puffs_node *pn = VPTOPP(vp);
   1958 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   1959 	struct componentname *cnp = ap->a_cnp;
   1960 	int error;
   1961 
   1962 	PUFFS_MSG_ALLOC(vn, link);
   1963 	link_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1964 	puffs_makecn(&link_msg->pvnr_cn, &link_msg->pvnr_cn_cred,
   1965 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1966 	puffs_msg_setinfo(park_link, PUFFSOP_VN,
   1967 	    PUFFS_VN_LINK, VPTOPNC(dvp));
   1968 
   1969 	puffs_msg_enqueue(pmp, park_link);
   1970 	REFPN_AND_UNLOCKVP(dvp, dpn);
   1971 	REFPN(pn);
   1972 	error = puffs_msg_wait2(pmp, park_link, dpn, pn);
   1973 
   1974 	PUFFS_MSG_RELEASE(link);
   1975 
   1976 	error = checkerr(pmp, error, __func__);
   1977 
   1978 	/*
   1979 	 * XXX: stay in touch with the cache.  I don't like this, but
   1980 	 * don't have a better solution either.  See also puffs_rename().
   1981 	 */
   1982 	if (error == 0)
   1983 		puffs_updatenode(pn, PUFFS_UPDATECTIME, 0);
   1984 
   1985 	RELEPN_AND_VP(dvp, dpn);
   1986 	puffs_releasenode(pn);
   1987 
   1988 	return error;
   1989 }
   1990 
   1991 int
   1992 puffs_vnop_symlink(void *v)
   1993 {
   1994 	struct vop_symlink_v3_args /* {
   1995 		const struct vnodeop_desc *a_desc;
   1996 		struct vnode *a_dvp;
   1997 		struct vnode **a_vpp;
   1998 		struct componentname *a_cnp;
   1999 		struct vattr *a_vap;
   2000 		char *a_target;
   2001 	} */ *ap = v;
   2002 	PUFFS_MSG_VARS(vn, symlink);
   2003 	struct vnode *dvp = ap->a_dvp;
   2004 	struct puffs_node *dpn = VPTOPP(dvp);
   2005 	struct mount *mp = dvp->v_mount;
   2006 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   2007 	struct componentname *cnp = ap->a_cnp;
   2008 	int error;
   2009 
   2010 	*ap->a_vpp = NULL;
   2011 
   2012 	PUFFS_MSG_ALLOC(vn, symlink);
   2013 	puffs_makecn(&symlink_msg->pvnr_cn, &symlink_msg->pvnr_cn_cred,
   2014 		cnp, PUFFS_USE_FULLPNBUF(pmp));
   2015 	symlink_msg->pvnr_va = *ap->a_vap;
   2016 	(void)strlcpy(symlink_msg->pvnr_link, ap->a_target,
   2017 	    sizeof(symlink_msg->pvnr_link));
   2018 	puffs_msg_setinfo(park_symlink, PUFFSOP_VN,
   2019 	    PUFFS_VN_SYMLINK, VPTOPNC(dvp));
   2020 
   2021 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_symlink, dvp->v_data, NULL, error);
   2022 
   2023 	error = checkerr(pmp, error, __func__);
   2024 	if (error)
   2025 		goto out;
   2026 
   2027 	error = puffs_newnode(mp, dvp, ap->a_vpp,
   2028 	    symlink_msg->pvnr_newnode, cnp, VLNK, 0);
   2029 	if (error) {
   2030 		puffs_abortbutton(pmp, PUFFS_ABORT_SYMLINK, dpn->pn_cookie,
   2031 		    symlink_msg->pvnr_newnode, cnp);
   2032 		goto out;
   2033 	}
   2034 
   2035 	if (PUFFS_USE_FS_TTL(pmp)) {
   2036 		struct timespec *va_ttl = &symlink_msg->pvnr_va_ttl;
   2037 		struct timespec *cn_ttl = &symlink_msg->pvnr_cn_ttl;
   2038 		struct vattr *rvap = &symlink_msg->pvnr_va;
   2039 
   2040 		update_va(*ap->a_vpp, NULL, rvap,
   2041 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
   2042 	}
   2043 
   2044 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
   2045 
   2046 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   2047 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
   2048 		update_parent(*ap->a_vpp, dvp);
   2049 
   2050  out:
   2051 	PUFFS_MSG_RELEASE(symlink);
   2052 
   2053 	return error;
   2054 }
   2055 
   2056 int
   2057 puffs_vnop_readlink(void *v)
   2058 {
   2059 	struct vop_readlink_args /* {
   2060 		const struct vnodeop_desc *a_desc;
   2061 		struct vnode *a_vp;
   2062 		struct uio *a_uio;
   2063 		kauth_cred_t a_cred;
   2064 	} */ *ap = v;
   2065 	PUFFS_MSG_VARS(vn, readlink);
   2066 	struct vnode *vp = ap->a_vp;
   2067 	struct puffs_mount *pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2068 	size_t linklen;
   2069 	int error;
   2070 
   2071 	PUFFS_MSG_ALLOC(vn, readlink);
   2072 	puffs_credcvt(&readlink_msg->pvnr_cred, ap->a_cred);
   2073 	linklen = sizeof(readlink_msg->pvnr_link);
   2074 	readlink_msg->pvnr_linklen = linklen;
   2075 	puffs_msg_setinfo(park_readlink, PUFFSOP_VN,
   2076 	    PUFFS_VN_READLINK, VPTOPNC(vp));
   2077 
   2078 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_readlink, vp->v_data, NULL, error);
   2079 	error = checkerr(pmp, error, __func__);
   2080 	if (error)
   2081 		goto out;
   2082 
   2083 	/* bad bad user file server */
   2084 	if (readlink_msg->pvnr_linklen > linklen) {
   2085 		puffs_senderr(pmp, PUFFS_ERR_READLINK, E2BIG,
   2086 		    "linklen too big", VPTOPNC(ap->a_vp));
   2087 		error = EPROTO;
   2088 		goto out;
   2089 	}
   2090 
   2091 	error = uiomove(&readlink_msg->pvnr_link, readlink_msg->pvnr_linklen,
   2092 	    ap->a_uio);
   2093  out:
   2094 	PUFFS_MSG_RELEASE(readlink);
   2095 	return error;
   2096 }
   2097 
   2098 int
   2099 puffs_vnop_rename(void *v)
   2100 {
   2101 	struct vop_rename_args /* {
   2102 		const struct vnodeop_desc *a_desc;
   2103 		struct vnode *a_fdvp;
   2104 		struct vnode *a_fvp;
   2105 		struct componentname *a_fcnp;
   2106 		struct vnode *a_tdvp;
   2107 		struct vnode *a_tvp;
   2108 		struct componentname *a_tcnp;
   2109 	} */ *ap = v;
   2110 	PUFFS_MSG_VARS(vn, rename);
   2111 	struct vnode *fdvp = ap->a_fdvp, *fvp = ap->a_fvp;
   2112 	struct vnode *tdvp = ap->a_tdvp, *tvp = ap->a_tvp;
   2113 	struct puffs_node *fpn = ap->a_fvp->v_data;
   2114 	struct puffs_mount *pmp = MPTOPUFFSMP(fdvp->v_mount);
   2115 	int error;
   2116 	bool doabort = true;
   2117 
   2118 	if ((fvp->v_mount != tdvp->v_mount) ||
   2119 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   2120 		ERROUT(EXDEV);
   2121 	}
   2122 
   2123 	PUFFS_MSG_ALLOC(vn, rename);
   2124 	rename_msg->pvnr_cookie_src = VPTOPNC(fvp);
   2125 	rename_msg->pvnr_cookie_targdir = VPTOPNC(tdvp);
   2126 	if (tvp)
   2127 		rename_msg->pvnr_cookie_targ = VPTOPNC(tvp);
   2128 	else
   2129 		rename_msg->pvnr_cookie_targ = NULL;
   2130 	puffs_makecn(&rename_msg->pvnr_cn_src, &rename_msg->pvnr_cn_src_cred,
   2131 	    ap->a_fcnp, PUFFS_USE_FULLPNBUF(pmp));
   2132 	puffs_makecn(&rename_msg->pvnr_cn_targ, &rename_msg->pvnr_cn_targ_cred,
   2133 	    ap->a_tcnp, PUFFS_USE_FULLPNBUF(pmp));
   2134 	puffs_msg_setinfo(park_rename, PUFFSOP_VN,
   2135 	    PUFFS_VN_RENAME, VPTOPNC(fdvp));
   2136 
   2137 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rename, fdvp->v_data, NULL, error);
   2138 	doabort = false;
   2139 	PUFFS_MSG_RELEASE(rename);
   2140 	error = checkerr(pmp, error, __func__);
   2141 
   2142 	/*
   2143 	 * XXX: stay in touch with the cache.  I don't like this, but
   2144 	 * don't have a better solution either.  See also puffs_link().
   2145 	 */
   2146 	if (error == 0) {
   2147 		puffs_updatenode(fpn, PUFFS_UPDATECTIME, 0);
   2148 
   2149 		if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   2150 		    (VPTOPP(fvp)->pn_parent != tdvp))
   2151 			update_parent(fvp, tdvp);
   2152 	}
   2153 
   2154 
   2155  out:
   2156 	if (doabort)
   2157 		VOP_ABORTOP(tdvp, ap->a_tcnp);
   2158 	if (tvp != NULL)
   2159 		vput(tvp);
   2160 	if (tdvp == tvp)
   2161 		vrele(tdvp);
   2162 	else
   2163 		vput(tdvp);
   2164 
   2165 	if (doabort)
   2166 		VOP_ABORTOP(fdvp, ap->a_fcnp);
   2167 	vrele(fdvp);
   2168 	vrele(fvp);
   2169 
   2170 	return error;
   2171 }
   2172 
   2173 #define RWARGS(cont, iofl, move, offset, creds)				\
   2174 	(cont)->pvnr_ioflag = (iofl);					\
   2175 	(cont)->pvnr_resid = (move);					\
   2176 	(cont)->pvnr_offset = (offset);					\
   2177 	puffs_credcvt(&(cont)->pvnr_cred, creds)
   2178 
   2179 int
   2180 puffs_vnop_read(void *v)
   2181 {
   2182 	struct vop_read_args /* {
   2183 		const struct vnodeop_desc *a_desc;
   2184 		struct vnode *a_vp;
   2185 		struct uio *a_uio;
   2186 		int a_ioflag;
   2187 		kauth_cred_t a_cred;
   2188 	} */ *ap = v;
   2189 	PUFFS_MSG_VARS(vn, read);
   2190 	struct vnode *vp = ap->a_vp;
   2191 	struct puffs_node *pn = VPTOPP(vp);
   2192 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2193 	struct uio *uio = ap->a_uio;
   2194 	size_t tomove, argsize;
   2195 	vsize_t bytelen;
   2196 	int error;
   2197 
   2198 	read_msg = NULL;
   2199 	error = 0;
   2200 
   2201 	/* std sanity */
   2202 	if (uio->uio_resid == 0)
   2203 		return 0;
   2204 	if (uio->uio_offset < 0)
   2205 		return EINVAL;
   2206 
   2207 	if (vp->v_type == VREG &&
   2208 	    PUFFS_USE_PAGECACHE(pmp) &&
   2209 	    !(pn->pn_stat & PNODE_RDIRECT)) {
   2210 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
   2211 
   2212 		while (uio->uio_resid > 0) {
   2213 			if (vp->v_size <= uio->uio_offset) {
   2214 				break;
   2215 			}
   2216 			bytelen = MIN(uio->uio_resid,
   2217 			    vp->v_size - uio->uio_offset);
   2218 			if (bytelen == 0)
   2219 				break;
   2220 
   2221 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
   2222 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
   2223 			if (error)
   2224 				break;
   2225 		}
   2226 
   2227 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   2228 			puffs_updatenode(VPTOPP(vp), PUFFS_UPDATEATIME, 0);
   2229 	} else {
   2230 		/*
   2231 		 * in case it's not a regular file or we're operating
   2232 		 * uncached, do read in the old-fashioned style,
   2233 		 * i.e. explicit read operations
   2234 		 */
   2235 
   2236 		tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2237 		argsize = sizeof(struct puffs_vnmsg_read);
   2238 		puffs_msgmem_alloc(argsize + tomove, &park_read,
   2239 		    (void *)&read_msg, 1);
   2240 
   2241 		error = 0;
   2242 		while (uio->uio_resid > 0) {
   2243 			tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2244 			memset(read_msg, 0, argsize); /* XXX: touser KASSERT */
   2245 			RWARGS(read_msg, ap->a_ioflag, tomove,
   2246 			    uio->uio_offset, ap->a_cred);
   2247 			puffs_msg_setinfo(park_read, PUFFSOP_VN,
   2248 			    PUFFS_VN_READ, VPTOPNC(vp));
   2249 			puffs_msg_setdelta(park_read, tomove);
   2250 
   2251 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_read, vp->v_data,
   2252 			    NULL, error);
   2253 			error = checkerr(pmp, error, __func__);
   2254 			if (error)
   2255 				break;
   2256 
   2257 			if (read_msg->pvnr_resid > tomove) {
   2258 				puffs_senderr(pmp, PUFFS_ERR_READ,
   2259 				    E2BIG, "resid grew", VPTOPNC(ap->a_vp));
   2260 				error = EPROTO;
   2261 				break;
   2262 			}
   2263 
   2264 			error = uiomove(read_msg->pvnr_data,
   2265 			    tomove - read_msg->pvnr_resid, uio);
   2266 
   2267 			/*
   2268 			 * in case the file is out of juice, resid from
   2269 			 * userspace is != 0.  and the error-case is
   2270 			 * quite obvious
   2271 			 */
   2272 			if (error || read_msg->pvnr_resid)
   2273 				break;
   2274 		}
   2275 
   2276 		puffs_msgmem_release(park_read);
   2277 	}
   2278 
   2279 	return error;
   2280 }
   2281 
   2282 /*
   2283  * XXX: in case of a failure, this leaves uio in a bad state.
   2284  * We could theoretically copy the uio and iovecs and "replay"
   2285  * them the right amount after the userspace trip, but don't
   2286  * bother for now.
   2287  */
   2288 int
   2289 puffs_vnop_write(void *v)
   2290 {
   2291 	struct vop_write_args /* {
   2292 		const struct vnodeop_desc *a_desc;
   2293 		struct vnode *a_vp;
   2294 		struct uio *a_uio;
   2295 		int a_ioflag;
   2296 		kauth_cred_t a_cred;
   2297 	} */ *ap = v;
   2298 	PUFFS_MSG_VARS(vn, write);
   2299 	struct vnode *vp = ap->a_vp;
   2300 	struct puffs_node *pn = VPTOPP(vp);
   2301 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2302 	struct uio *uio = ap->a_uio;
   2303 	size_t tomove, argsize;
   2304 	off_t oldoff, newoff, origoff;
   2305 	vsize_t bytelen;
   2306 	int error, uflags;
   2307 	int ubcflags;
   2308 
   2309 	error = uflags = 0;
   2310 	write_msg = NULL;
   2311 
   2312 	mutex_enter(&pn->pn_sizemtx);
   2313 
   2314 	if (vp->v_type == VREG &&
   2315 	    PUFFS_USE_PAGECACHE(pmp) &&
   2316 	    !(pn->pn_stat & PNODE_WDIRECT)) {
   2317 		ubcflags = UBC_WRITE | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp);
   2318 
   2319 		/*
   2320 		 * userspace *should* be allowed to control this,
   2321 		 * but with UBC it's a bit unclear how to handle it
   2322 		 */
   2323 		if (ap->a_ioflag & IO_APPEND)
   2324 			uio->uio_offset = vp->v_size;
   2325 
   2326 		origoff = uio->uio_offset;
   2327 		while (uio->uio_resid > 0) {
   2328 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
   2329 				uflags |= PUFFS_UPDATEATIME;
   2330 			uflags |= PUFFS_UPDATECTIME;
   2331 			uflags |= PUFFS_UPDATEMTIME;
   2332 			oldoff = uio->uio_offset;
   2333 			bytelen = uio->uio_resid;
   2334 
   2335 			newoff = oldoff + bytelen;
   2336 			if (vp->v_size < newoff) {
   2337 				uvm_vnp_setwritesize(vp, newoff);
   2338 			}
   2339 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
   2340 			    UVM_ADV_RANDOM, ubcflags);
   2341 
   2342 			/*
   2343 			 * In case of a ubc_uiomove() error,
   2344 			 * opt to not extend the file at all and
   2345 			 * return an error.  Otherwise, if we attempt
   2346 			 * to clear the memory we couldn't fault to,
   2347 			 * we might generate a kernel page fault.
   2348 			 */
   2349 			if (vp->v_size < newoff) {
   2350 				if (error == 0) {
   2351 					uflags |= PUFFS_UPDATESIZE;
   2352 					uvm_vnp_setsize(vp, newoff);
   2353 				} else {
   2354 					uvm_vnp_setwritesize(vp, vp->v_size);
   2355 				}
   2356 			}
   2357 			if (error)
   2358 				break;
   2359 
   2360 			/*
   2361 			 * If we're writing large files, flush to file server
   2362 			 * every 64k.  Otherwise we can very easily exhaust
   2363 			 * kernel and user memory, as the file server cannot
   2364 			 * really keep up with our writing speed.
   2365 			 *
   2366 			 * Note: this does *NOT* honor MNT_ASYNC, because
   2367 			 * that gives userland too much say in the kernel.
   2368 			 */
   2369 			if (oldoff >> 16 != uio->uio_offset >> 16) {
   2370 				mutex_enter(vp->v_interlock);
   2371 				error = VOP_PUTPAGES(vp, oldoff & ~0xffff,
   2372 				    uio->uio_offset & ~0xffff,
   2373 				    PGO_CLEANIT | PGO_SYNCIO);
   2374 				if (error)
   2375 					break;
   2376 			}
   2377 		}
   2378 
   2379 		/* synchronous I/O? */
   2380 		if (error == 0 && ap->a_ioflag & IO_SYNC) {
   2381 			mutex_enter(vp->v_interlock);
   2382 			error = VOP_PUTPAGES(vp, trunc_page(origoff),
   2383 			    round_page(uio->uio_offset),
   2384 			    PGO_CLEANIT | PGO_SYNCIO);
   2385 
   2386 		/* write through page cache? */
   2387 		} else if (error == 0 && pmp->pmp_flags & PUFFS_KFLAG_WTCACHE) {
   2388 			mutex_enter(vp->v_interlock);
   2389 			error = VOP_PUTPAGES(vp, trunc_page(origoff),
   2390 			    round_page(uio->uio_offset), PGO_CLEANIT);
   2391 		}
   2392 
   2393 		puffs_updatenode(VPTOPP(vp), uflags, vp->v_size);
   2394 	} else {
   2395 		/* tomove is non-increasing */
   2396 		tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2397 		argsize = sizeof(struct puffs_vnmsg_write) + tomove;
   2398 		puffs_msgmem_alloc(argsize, &park_write, (void *)&write_msg,1);
   2399 
   2400 		while (uio->uio_resid > 0) {
   2401 			/* move data to buffer */
   2402 			tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2403 			memset(write_msg, 0, argsize); /* XXX: touser KASSERT */
   2404 			RWARGS(write_msg, ap->a_ioflag, tomove,
   2405 			    uio->uio_offset, ap->a_cred);
   2406 			error = uiomove(write_msg->pvnr_data, tomove, uio);
   2407 			if (error)
   2408 				break;
   2409 
   2410 			/* move buffer to userspace */
   2411 			puffs_msg_setinfo(park_write, PUFFSOP_VN,
   2412 			    PUFFS_VN_WRITE, VPTOPNC(vp));
   2413 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_write, vp->v_data,
   2414 			    NULL, error);
   2415 			error = checkerr(pmp, error, __func__);
   2416 			if (error)
   2417 				break;
   2418 
   2419 			if (write_msg->pvnr_resid > tomove) {
   2420 				puffs_senderr(pmp, PUFFS_ERR_WRITE,
   2421 				    E2BIG, "resid grew", VPTOPNC(ap->a_vp));
   2422 				error = EPROTO;
   2423 				break;
   2424 			}
   2425 
   2426 			/* adjust file size */
   2427 			if (vp->v_size < uio->uio_offset)
   2428 				uvm_vnp_setsize(vp, uio->uio_offset);
   2429 
   2430 			/* didn't move everything?  bad userspace.  bail */
   2431 			if (write_msg->pvnr_resid != 0) {
   2432 				error = EIO;
   2433 				break;
   2434 			}
   2435 		}
   2436 		puffs_msgmem_release(park_write);
   2437 	}
   2438 
   2439 	mutex_exit(&pn->pn_sizemtx);
   2440 	return error;
   2441 }
   2442 
   2443 int
   2444 puffs_vnop_print(void *v)
   2445 {
   2446 	struct vop_print_args /* {
   2447 		struct vnode *a_vp;
   2448 	} */ *ap = v;
   2449 	PUFFS_MSG_VARS(vn, print);
   2450 	struct vnode *vp = ap->a_vp;
   2451 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2452 	struct puffs_node *pn = vp->v_data;
   2453 
   2454 	/* kernel portion */
   2455 	printf("tag VT_PUFFS, vnode %p, puffs node: %p,\n"
   2456 	    "\tuserspace cookie: %p", vp, pn, pn->pn_cookie);
   2457 	if (vp->v_type == VFIFO)
   2458 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   2459 	printf("\n");
   2460 
   2461 	/* userspace portion */
   2462 	if (EXISTSOP(pmp, PRINT)) {
   2463 		PUFFS_MSG_ALLOC(vn, print);
   2464 		puffs_msg_setinfo(park_print, PUFFSOP_VN,
   2465 		    PUFFS_VN_PRINT, VPTOPNC(vp));
   2466 		PUFFS_MSG_ENQUEUEWAIT2_NOERROR(pmp, park_print, vp->v_data,
   2467 		    NULL);
   2468 		PUFFS_MSG_RELEASE(print);
   2469 	}
   2470 
   2471 	return 0;
   2472 }
   2473 
   2474 int
   2475 puffs_vnop_pathconf(void *v)
   2476 {
   2477 	struct vop_pathconf_args /* {
   2478 		const struct vnodeop_desc *a_desc;
   2479 		struct vnode *a_vp;
   2480 		int a_name;
   2481 		register_t *a_retval;
   2482 	} */ *ap = v;
   2483 	PUFFS_MSG_VARS(vn, pathconf);
   2484 	struct vnode *vp = ap->a_vp;
   2485 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2486 	int error;
   2487 
   2488 	PUFFS_MSG_ALLOC(vn, pathconf);
   2489 	pathconf_msg->pvnr_name = ap->a_name;
   2490 	puffs_msg_setinfo(park_pathconf, PUFFSOP_VN,
   2491 	    PUFFS_VN_PATHCONF, VPTOPNC(vp));
   2492 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_pathconf, vp->v_data, NULL, error);
   2493 	error = checkerr(pmp, error, __func__);
   2494 	if (!error)
   2495 		*ap->a_retval = pathconf_msg->pvnr_retval;
   2496 	PUFFS_MSG_RELEASE(pathconf);
   2497 
   2498 	return error;
   2499 }
   2500 
   2501 int
   2502 puffs_vnop_advlock(void *v)
   2503 {
   2504 	struct vop_advlock_args /* {
   2505 		const struct vnodeop_desc *a_desc;
   2506 		struct vnode *a_vp;
   2507 		void *a_id;
   2508 		int a_op;
   2509 		struct flock *a_fl;
   2510 		int a_flags;
   2511 	} */ *ap = v;
   2512 	PUFFS_MSG_VARS(vn, advlock);
   2513 	struct vnode *vp = ap->a_vp;
   2514 	struct puffs_node *pn = VPTOPP(vp);
   2515 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2516 	int error;
   2517 
   2518 	if (!EXISTSOP(pmp, ADVLOCK))
   2519 		return lf_advlock(ap, &pn->pn_lockf, vp->v_size);
   2520 
   2521 	PUFFS_MSG_ALLOC(vn, advlock);
   2522 	(void)memcpy(&advlock_msg->pvnr_fl, ap->a_fl,
   2523 		     sizeof(advlock_msg->pvnr_fl));
   2524 	advlock_msg->pvnr_id = ap->a_id;
   2525 	advlock_msg->pvnr_op = ap->a_op;
   2526 	advlock_msg->pvnr_flags = ap->a_flags;
   2527 	puffs_msg_setinfo(park_advlock, PUFFSOP_VN,
   2528 	    PUFFS_VN_ADVLOCK, VPTOPNC(vp));
   2529 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_advlock, vp->v_data, NULL, error);
   2530 	error = checkerr(pmp, error, __func__);
   2531 	PUFFS_MSG_RELEASE(advlock);
   2532 
   2533 	return error;
   2534 }
   2535 
   2536 int
   2537 puffs_vnop_abortop(void *v)
   2538 {
   2539 	struct vop_abortop_args /* {
   2540 		struct vnode *a_dvp;
   2541 		struct componentname *a_cnp;
   2542 	}; */ *ap = v;
   2543 	PUFFS_MSG_VARS(vn, abortop);
   2544 	struct vnode *dvp = ap->a_dvp;
   2545 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   2546 	struct componentname *cnp = ap->a_cnp;
   2547 
   2548 	if (EXISTSOP(pmp, ABORTOP)) {
   2549 		PUFFS_MSG_ALLOC(vn, abortop);
   2550 		puffs_makecn(&abortop_msg->pvnr_cn, &abortop_msg->pvnr_cn_cred,
   2551 		    cnp, PUFFS_USE_FULLPNBUF(pmp));
   2552 		puffs_msg_setfaf(park_abortop);
   2553 		puffs_msg_setinfo(park_abortop, PUFFSOP_VN,
   2554 		    PUFFS_VN_ABORTOP, VPTOPNC(dvp));
   2555 
   2556 		puffs_msg_enqueue(pmp, park_abortop);
   2557 		PUFFS_MSG_RELEASE(abortop);
   2558 	}
   2559 
   2560 	return genfs_abortop(v);
   2561 }
   2562 
   2563 #define BIOASYNC(bp) (bp->b_flags & B_ASYNC)
   2564 
   2565 /*
   2566  * This maps itself to PUFFS_VN_READ/WRITE for data transfer.
   2567  */
   2568 int
   2569 puffs_vnop_strategy(void *v)
   2570 {
   2571 	struct vop_strategy_args /* {
   2572 		const struct vnodeop_desc *a_desc;
   2573 		struct vnode *a_vp;
   2574 		struct buf *a_bp;
   2575 	} */ *ap = v;
   2576 	PUFFS_MSG_VARS(vn, rw);
   2577 	struct vnode *vp = ap->a_vp;
   2578 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2579 	struct puffs_node *pn;
   2580 	struct buf *bp;
   2581 	size_t argsize;
   2582 	size_t tomove, moved;
   2583 	int error, dofaf, cansleep, dobiodone;
   2584 
   2585 	pmp = MPTOPUFFSMP(vp->v_mount);
   2586 	bp = ap->a_bp;
   2587 	error = 0;
   2588 	dofaf = 0;
   2589 	cansleep = 0;
   2590 	pn = VPTOPP(vp);
   2591 	park_rw = NULL; /* explicit */
   2592 	dobiodone = 1;
   2593 
   2594 	if ((BUF_ISREAD(bp) && !EXISTSOP(pmp, READ))
   2595 	    || (BUF_ISWRITE(bp) && !EXISTSOP(pmp, WRITE)))
   2596 		ERROUT(EOPNOTSUPP);
   2597 
   2598 	/*
   2599 	 * Short-circuit optimization: don't flush buffer in between
   2600 	 * VOP_INACTIVE and VOP_RECLAIM in case the node has no references.
   2601 	 */
   2602 	if (pn->pn_stat & PNODE_DYING) {
   2603 		KASSERT(BUF_ISWRITE(bp));
   2604 		bp->b_resid = 0;
   2605 		goto out;
   2606 	}
   2607 
   2608 #ifdef DIAGNOSTIC
   2609 	if (bp->b_bcount > pmp->pmp_msg_maxsize - PUFFS_MSGSTRUCT_MAX)
   2610 		panic("puffs_strategy: wildly inappropriate buf bcount %d",
   2611 		    bp->b_bcount);
   2612 #endif
   2613 
   2614 	/*
   2615 	 * See explanation for the necessity of a FAF in puffs_fsync.
   2616 	 *
   2617 	 * Also, do FAF in case we're suspending.
   2618 	 * See puffs_vfsops.c:pageflush()
   2619 	 */
   2620 	if (BUF_ISWRITE(bp)) {
   2621 		mutex_enter(vp->v_interlock);
   2622 		if (vdead_check(vp, VDEAD_NOWAIT) != 0)
   2623 			dofaf = 1;
   2624 		if (pn->pn_stat & PNODE_FAF)
   2625 			dofaf = 1;
   2626 		mutex_exit(vp->v_interlock);
   2627 	}
   2628 
   2629 	cansleep = (curlwp == uvm.pagedaemon_lwp || dofaf) ? 0 : 1;
   2630 
   2631 	KASSERT(curlwp != uvm.pagedaemon_lwp || dofaf || BIOASYNC(bp));
   2632 
   2633 	/* allocate transport structure */
   2634 	tomove = PUFFS_TOMOVE(bp->b_bcount, pmp);
   2635 	argsize = sizeof(struct puffs_vnmsg_rw);
   2636 	error = puffs_msgmem_alloc(argsize + tomove, &park_rw,
   2637 	    (void *)&rw_msg, cansleep);
   2638 	if (error)
   2639 		goto out;
   2640 	RWARGS(rw_msg, 0, tomove, bp->b_blkno << DEV_BSHIFT, FSCRED);
   2641 
   2642 	/* 2x2 cases: read/write, faf/nofaf */
   2643 	if (BUF_ISREAD(bp)) {
   2644 		puffs_msg_setinfo(park_rw, PUFFSOP_VN,
   2645 		    PUFFS_VN_READ, VPTOPNC(vp));
   2646 		puffs_msg_setdelta(park_rw, tomove);
   2647 		if (BIOASYNC(bp)) {
   2648 			puffs_msg_setcall(park_rw,
   2649 			    puffs_parkdone_asyncbioread, bp);
   2650 			puffs_msg_enqueue(pmp, park_rw);
   2651 			dobiodone = 0;
   2652 		} else {
   2653 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data,
   2654 			    NULL, error);
   2655 			error = checkerr(pmp, error, __func__);
   2656 			if (error)
   2657 				goto out;
   2658 
   2659 			if (rw_msg->pvnr_resid > tomove) {
   2660 				puffs_senderr(pmp, PUFFS_ERR_READ,
   2661 				    E2BIG, "resid grew", VPTOPNC(vp));
   2662 				ERROUT(EPROTO);
   2663 			}
   2664 
   2665 			moved = tomove - rw_msg->pvnr_resid;
   2666 
   2667 			(void)memcpy(bp->b_data, rw_msg->pvnr_data, moved);
   2668 			bp->b_resid = bp->b_bcount - moved;
   2669 		}
   2670 	} else {
   2671 		puffs_msg_setinfo(park_rw, PUFFSOP_VN,
   2672 		    PUFFS_VN_WRITE, VPTOPNC(vp));
   2673 		/*
   2674 		 * make pages read-only before we write them if we want
   2675 		 * write caching info
   2676 		 */
   2677 		if (PUFFS_WCACHEINFO(pmp)) {
   2678 			struct uvm_object *uobj = &vp->v_uobj;
   2679 			int npages = (bp->b_bcount + PAGE_SIZE-1) >> PAGE_SHIFT;
   2680 			struct vm_page *vmp;
   2681 			int i;
   2682 
   2683 			for (i = 0; i < npages; i++) {
   2684 				vmp= uvm_pageratop((vaddr_t)bp->b_data
   2685 				    + (i << PAGE_SHIFT));
   2686 				DPRINTF(("puffs_strategy: write-protecting "
   2687 				    "vp %p page %p, offset %" PRId64"\n",
   2688 				    vp, vmp, vmp->offset));
   2689 				mutex_enter(uobj->vmobjlock);
   2690 				vmp->flags |= PG_RDONLY;
   2691 				pmap_page_protect(vmp, VM_PROT_READ);
   2692 				mutex_exit(uobj->vmobjlock);
   2693 			}
   2694 		}
   2695 
   2696 		(void)memcpy(&rw_msg->pvnr_data, bp->b_data, tomove);
   2697 		if (dofaf) {
   2698 			puffs_msg_setfaf(park_rw);
   2699 		} else if (BIOASYNC(bp)) {
   2700 			puffs_msg_setcall(park_rw,
   2701 			    puffs_parkdone_asyncbiowrite, bp);
   2702 			dobiodone = 0;
   2703 		}
   2704 
   2705 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data, NULL, error);
   2706 
   2707 		if (dobiodone == 0)
   2708 			goto out;
   2709 
   2710 		/*
   2711 		 * XXXXXXXX: wrong, but kernel can't survive strategy
   2712 		 * failure currently.  Here, have one more X: X.
   2713 		 */
   2714 		if (error != ENOMEM)
   2715 			error = 0;
   2716 
   2717 		error = checkerr(pmp, error, __func__);
   2718 		if (error)
   2719 			goto out;
   2720 
   2721 		if (rw_msg->pvnr_resid > tomove) {
   2722 			puffs_senderr(pmp, PUFFS_ERR_WRITE,
   2723 			    E2BIG, "resid grew", VPTOPNC(vp));
   2724 			ERROUT(EPROTO);
   2725 		}
   2726 
   2727 		/*
   2728 		 * FAF moved everything.  Frankly, we don't
   2729 		 * really have a choice.
   2730 		 */
   2731 		if (dofaf && error == 0)
   2732 			moved = tomove;
   2733 		else
   2734 			moved = tomove - rw_msg->pvnr_resid;
   2735 
   2736 		bp->b_resid = bp->b_bcount - moved;
   2737 		if (bp->b_resid != 0) {
   2738 			ERROUT(EIO);
   2739 		}
   2740 	}
   2741 
   2742  out:
   2743 	if (park_rw)
   2744 		puffs_msgmem_release(park_rw);
   2745 
   2746 	if (error)
   2747 		bp->b_error = error;
   2748 
   2749 	if (error || dobiodone)
   2750 		biodone(bp);
   2751 
   2752 	return error;
   2753 }
   2754 
   2755 int
   2756 puffs_vnop_mmap(void *v)
   2757 {
   2758 	struct vop_mmap_args /* {
   2759 		const struct vnodeop_desc *a_desc;
   2760 		struct vnode *a_vp;
   2761 		vm_prot_t a_prot;
   2762 		kauth_cred_t a_cred;
   2763 	} */ *ap = v;
   2764 	PUFFS_MSG_VARS(vn, mmap);
   2765 	struct vnode *vp = ap->a_vp;
   2766 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2767 	int error;
   2768 
   2769 	if (!PUFFS_USE_PAGECACHE(pmp))
   2770 		return genfs_eopnotsupp(v);
   2771 
   2772 	if (EXISTSOP(pmp, MMAP)) {
   2773 		PUFFS_MSG_ALLOC(vn, mmap);
   2774 		mmap_msg->pvnr_prot = ap->a_prot;
   2775 		puffs_credcvt(&mmap_msg->pvnr_cred, ap->a_cred);
   2776 		puffs_msg_setinfo(park_mmap, PUFFSOP_VN,
   2777 		    PUFFS_VN_MMAP, VPTOPNC(vp));
   2778 
   2779 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mmap, vp->v_data, NULL, error);
   2780 		error = checkerr(pmp, error, __func__);
   2781 		PUFFS_MSG_RELEASE(mmap);
   2782 	} else {
   2783 		error = genfs_mmap(v);
   2784 	}
   2785 
   2786 	return error;
   2787 }
   2788 
   2789 
   2790 /*
   2791  * The rest don't get a free trip to userspace and back, they
   2792  * have to stay within the kernel.
   2793  */
   2794 
   2795 /*
   2796  * bmap doesn't really make any sense for puffs, so just 1:1 map it.
   2797  * well, maybe somehow, somewhere, some day ....
   2798  */
   2799 int
   2800 puffs_vnop_bmap(void *v)
   2801 {
   2802 	struct vop_bmap_args /* {
   2803 		const struct vnodeop_desc *a_desc;
   2804 		struct vnode *a_vp;
   2805 		daddr_t a_bn;
   2806 		struct vnode **a_vpp;
   2807 		daddr_t *a_bnp;
   2808 		int *a_runp;
   2809 	} */ *ap = v;
   2810 	struct puffs_mount *pmp;
   2811 
   2812 	pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2813 
   2814 	if (ap->a_vpp)
   2815 		*ap->a_vpp = ap->a_vp;
   2816 	if (ap->a_bnp)
   2817 		*ap->a_bnp = ap->a_bn;
   2818 	if (ap->a_runp)
   2819 		*ap->a_runp
   2820 		    = (PUFFS_TOMOVE(pmp->pmp_msg_maxsize, pmp)>>DEV_BSHIFT) - 1;
   2821 
   2822 	return 0;
   2823 }
   2824 
   2825 /*
   2826  * Handle getpages faults in puffs.  We let genfs_getpages() do most
   2827  * of the dirty work, but we come in this route to do accounting tasks.
   2828  * If the user server has specified functions for cache notifications
   2829  * about reads and/or writes, we record which type of operation we got,
   2830  * for which page range, and proceed to issue a FAF notification to the
   2831  * server about it.
   2832  */
   2833 int
   2834 puffs_vnop_getpages(void *v)
   2835 {
   2836 	struct vop_getpages_args /* {
   2837 		const struct vnodeop_desc *a_desc;
   2838 		struct vnode *a_vp;
   2839 		voff_t a_offset;
   2840 		struct vm_page **a_m;
   2841 		int *a_count;
   2842 		int a_centeridx;
   2843 		vm_prot_t a_access_type;
   2844 		int a_advice;
   2845 		int a_flags;
   2846 	} */ *ap = v;
   2847 	struct puffs_mount *pmp;
   2848 	struct puffs_node *pn;
   2849 	struct vnode *vp;
   2850 	struct vm_page **pgs;
   2851 	struct puffs_cacheinfo *pcinfo = NULL;
   2852 	struct puffs_cacherun *pcrun;
   2853 	void *parkmem = NULL;
   2854 	size_t runsizes;
   2855 	int i, npages, si, streakon;
   2856 	int error, locked, write;
   2857 
   2858 	pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2859 	npages = *ap->a_count;
   2860 	pgs = ap->a_m;
   2861 	vp = ap->a_vp;
   2862 	pn = vp->v_data;
   2863 	locked = (ap->a_flags & PGO_LOCKED) != 0;
   2864 	write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   2865 
   2866 	/* ccg xnaht - gets Wuninitialized wrong */
   2867 	pcrun = NULL;
   2868 	runsizes = 0;
   2869 
   2870 	/*
   2871 	 * Check that we aren't trying to fault in pages which our file
   2872 	 * server doesn't know about.  This happens if we extend a file by
   2873 	 * skipping some pages and later try to fault in pages which
   2874 	 * are between pn_serversize and vp_size.  This check optimizes
   2875 	 * away the common case where a file is being extended.
   2876 	 */
   2877 	if (ap->a_offset >= pn->pn_serversize && ap->a_offset < vp->v_size) {
   2878 		struct vattr va;
   2879 
   2880 		/* try again later when we can block */
   2881 		if (locked)
   2882 			ERROUT(EBUSY);
   2883 
   2884 		mutex_exit(vp->v_interlock);
   2885 		vattr_null(&va);
   2886 		va.va_size = vp->v_size;
   2887 		error = dosetattr(vp, &va, FSCRED, 0);
   2888 		if (error)
   2889 			ERROUT(error);
   2890 		mutex_enter(vp->v_interlock);
   2891 	}
   2892 
   2893 	if (write && PUFFS_WCACHEINFO(pmp)) {
   2894 #ifdef notnowjohn
   2895 		/* allocate worst-case memory */
   2896 		runsizes = ((npages / 2) + 1) * sizeof(struct puffs_cacherun);
   2897 		KASSERT(curlwp != uvm.pagedaemon_lwp || locked);
   2898 		pcinfo = kmem_zalloc(sizeof(struct puffs_cacheinfo) + runsize,
   2899 		    locked ? KM_NOSLEEP : KM_SLEEP);
   2900 
   2901 		/*
   2902 		 * can't block if we're locked and can't mess up caching
   2903 		 * information for fs server.  so come back later, please
   2904 		 */
   2905 		if (pcinfo == NULL)
   2906 			ERROUT(ENOMEM);
   2907 
   2908 		parkmem = puffs_park_alloc(locked == 0);
   2909 		if (parkmem == NULL)
   2910 			ERROUT(ENOMEM);
   2911 
   2912 		pcrun = pcinfo->pcache_runs;
   2913 #else
   2914 		(void)parkmem;
   2915 #endif
   2916 	}
   2917 
   2918 	error = genfs_getpages(v);
   2919 	if (error)
   2920 		goto out;
   2921 
   2922 	if (PUFFS_WCACHEINFO(pmp) == 0)
   2923 		goto out;
   2924 
   2925 	/*
   2926 	 * Let's see whose fault it was and inform the user server of
   2927 	 * possibly read/written pages.  Map pages from read faults
   2928 	 * strictly read-only, since otherwise we might miss info on
   2929 	 * when the page is actually write-faulted to.
   2930 	 */
   2931 	if (!locked)
   2932 		mutex_enter(vp->v_uobj.vmobjlock);
   2933 	for (i = 0, si = 0, streakon = 0; i < npages; i++) {
   2934 		if (pgs[i] == NULL || pgs[i] == PGO_DONTCARE) {
   2935 			if (streakon && write) {
   2936 				streakon = 0;
   2937 				pcrun[si].pcache_runend
   2938 				    = trunc_page(pgs[i]->offset) + PAGE_MASK;
   2939 				si++;
   2940 			}
   2941 			continue;
   2942 		}
   2943 		if (streakon == 0 && write) {
   2944 			streakon = 1;
   2945 			pcrun[si].pcache_runstart = pgs[i]->offset;
   2946 		}
   2947 
   2948 		if (!write)
   2949 			pgs[i]->flags |= PG_RDONLY;
   2950 	}
   2951 	/* was the last page part of our streak? */
   2952 	if (streakon) {
   2953 		pcrun[si].pcache_runend
   2954 		    = trunc_page(pgs[i-1]->offset) + PAGE_MASK;
   2955 		si++;
   2956 	}
   2957 	if (!locked)
   2958 		mutex_exit(vp->v_uobj.vmobjlock);
   2959 
   2960 	KASSERT(si <= (npages / 2) + 1);
   2961 
   2962 #ifdef notnowjohn
   2963 	/* send results to userspace */
   2964 	if (write)
   2965 		puffs_cacheop(pmp, parkmem, pcinfo,
   2966 		    sizeof(struct puffs_cacheinfo) + runsizes, VPTOPNC(vp));
   2967 #endif
   2968 
   2969  out:
   2970 	if (error) {
   2971 		if (pcinfo != NULL)
   2972 			kmem_free(pcinfo,
   2973 			    sizeof(struct puffs_cacheinfo) + runsizes);
   2974 #ifdef notnowjohn
   2975 		if (parkmem != NULL)
   2976 			puffs_park_release(parkmem, 1);
   2977 #endif
   2978 	}
   2979 
   2980 	return error;
   2981 }
   2982 
   2983 /*
   2984  * Extended attribute support.
   2985  */
   2986 
   2987 int
   2988 puffs_vnop_getextattr(void *v)
   2989 {
   2990 	struct vop_getextattr_args /*
   2991 		struct vnode *a_vp;
   2992 		int a_attrnamespace;
   2993 		const char *a_name;
   2994 		struct uio *a_uio;
   2995 		size_t *a_size;
   2996 		kauth_cred_t a_cred;
   2997 	}; */ *ap = v;
   2998 	PUFFS_MSG_VARS(vn, getextattr);
   2999 	struct vnode *vp = ap->a_vp;
   3000 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3001 	int attrnamespace = ap->a_attrnamespace;
   3002 	const char *name = ap->a_name;
   3003 	struct uio *uio = ap->a_uio;
   3004 	size_t *sizep = ap->a_size;
   3005 	size_t tomove, resid;
   3006 	int error;
   3007 
   3008 	if (uio)
   3009 		resid = uio->uio_resid;
   3010 	else
   3011 		resid = 0;
   3012 
   3013 	tomove = PUFFS_TOMOVE(resid, pmp);
   3014 	if (tomove != resid) {
   3015 		error = E2BIG;
   3016 		goto out;
   3017 	}
   3018 
   3019 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_getextattr) + tomove,
   3020 	    &park_getextattr, (void *)&getextattr_msg, 1);
   3021 
   3022 	getextattr_msg->pvnr_attrnamespace = attrnamespace;
   3023 	strlcpy(getextattr_msg->pvnr_attrname, name,
   3024 	    sizeof(getextattr_msg->pvnr_attrname));
   3025 	puffs_credcvt(&getextattr_msg->pvnr_cred, ap->a_cred);
   3026 	if (sizep)
   3027 		getextattr_msg->pvnr_datasize = 1;
   3028 	getextattr_msg->pvnr_resid = tomove;
   3029 
   3030 	puffs_msg_setinfo(park_getextattr,
   3031 	    PUFFSOP_VN, PUFFS_VN_GETEXTATTR, VPTOPNC(vp));
   3032 	puffs_msg_setdelta(park_getextattr, tomove);
   3033 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_getextattr, vp->v_data, NULL, error);
   3034 
   3035 	error = checkerr(pmp, error, __func__);
   3036 	if (error)
   3037 		goto out;
   3038 
   3039 	resid = getextattr_msg->pvnr_resid;
   3040 	if (resid > tomove) {
   3041 		puffs_senderr(pmp, PUFFS_ERR_GETEXTATTR, E2BIG,
   3042 		    "resid grew", VPTOPNC(vp));
   3043 		error = EPROTO;
   3044 		goto out;
   3045 	}
   3046 
   3047 	if (sizep)
   3048 		*sizep = getextattr_msg->pvnr_datasize;
   3049 	if (uio)
   3050 		error = uiomove(getextattr_msg->pvnr_data, tomove - resid, uio);
   3051 
   3052  out:
   3053 	PUFFS_MSG_RELEASE(getextattr);
   3054 	return error;
   3055 }
   3056 
   3057 int
   3058 puffs_vnop_setextattr(void *v)
   3059 {
   3060 	struct vop_setextattr_args /* {
   3061 		struct vnode *a_vp;
   3062 		int a_attrnamespace;
   3063 		const char *a_name;
   3064 		struct uio *a_uio;
   3065 		kauth_cred_t a_cred;
   3066 	}; */ *ap = v;
   3067 	PUFFS_MSG_VARS(vn, setextattr);
   3068 	struct vnode *vp = ap->a_vp;
   3069 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3070 	int attrnamespace = ap->a_attrnamespace;
   3071 	const char *name = ap->a_name;
   3072 	struct uio *uio = ap->a_uio;
   3073 	size_t tomove, resid;
   3074 	int error;
   3075 
   3076 	if (uio)
   3077 		resid = uio->uio_resid;
   3078 	else
   3079 		resid = 0;
   3080 
   3081 	tomove = PUFFS_TOMOVE(resid, pmp);
   3082 	if (tomove != resid) {
   3083 		error = E2BIG;
   3084 		goto out;
   3085 	}
   3086 
   3087 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_setextattr) + tomove,
   3088 	    &park_setextattr, (void *)&setextattr_msg, 1);
   3089 
   3090 	setextattr_msg->pvnr_attrnamespace = attrnamespace;
   3091 	strlcpy(setextattr_msg->pvnr_attrname, name,
   3092 	    sizeof(setextattr_msg->pvnr_attrname));
   3093 	puffs_credcvt(&setextattr_msg->pvnr_cred, ap->a_cred);
   3094 	setextattr_msg->pvnr_resid = tomove;
   3095 
   3096 	if (uio) {
   3097 		error = uiomove(setextattr_msg->pvnr_data, tomove, uio);
   3098 		if (error)
   3099 			goto out;
   3100 	}
   3101 
   3102 	puffs_msg_setinfo(park_setextattr,
   3103 	    PUFFSOP_VN, PUFFS_VN_SETEXTATTR, VPTOPNC(vp));
   3104 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_setextattr, vp->v_data, NULL, error);
   3105 
   3106 	error = checkerr(pmp, error, __func__);
   3107 	if (error)
   3108 		goto out;
   3109 
   3110 	if (setextattr_msg->pvnr_resid != 0)
   3111 		error = EIO;
   3112 
   3113  out:
   3114 	PUFFS_MSG_RELEASE(setextattr);
   3115 
   3116 	return error;
   3117 }
   3118 
   3119 int
   3120 puffs_vnop_listextattr(void *v)
   3121 {
   3122 	struct vop_listextattr_args /* {
   3123 		struct vnode *a_vp;
   3124 		int a_attrnamespace;
   3125 		struct uio *a_uio;
   3126 		size_t *a_size;
   3127 		int a_flag,
   3128 		kauth_cred_t a_cred;
   3129 	}; */ *ap = v;
   3130 	PUFFS_MSG_VARS(vn, listextattr);
   3131 	struct vnode *vp = ap->a_vp;
   3132 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3133 	int attrnamespace = ap->a_attrnamespace;
   3134 	struct uio *uio = ap->a_uio;
   3135 	size_t *sizep = ap->a_size;
   3136 	int flag = ap->a_flag;
   3137 	size_t tomove, resid;
   3138 	int error;
   3139 
   3140 	if (uio)
   3141 		resid = uio->uio_resid;
   3142 	else
   3143 		resid = 0;
   3144 
   3145 	tomove = PUFFS_TOMOVE(resid, pmp);
   3146 	if (tomove != resid) {
   3147 		error = E2BIG;
   3148 		goto out;
   3149 	}
   3150 
   3151 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_listextattr) + tomove,
   3152 	    &park_listextattr, (void *)&listextattr_msg, 1);
   3153 
   3154 	listextattr_msg->pvnr_attrnamespace = attrnamespace;
   3155 	listextattr_msg->pvnr_flag = flag;
   3156 	puffs_credcvt(&listextattr_msg->pvnr_cred, ap->a_cred);
   3157 	listextattr_msg->pvnr_resid = tomove;
   3158 	if (sizep)
   3159 		listextattr_msg->pvnr_datasize = 1;
   3160 
   3161 	puffs_msg_setinfo(park_listextattr,
   3162 	    PUFFSOP_VN, PUFFS_VN_LISTEXTATTR, VPTOPNC(vp));
   3163 	puffs_msg_setdelta(park_listextattr, tomove);
   3164 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_listextattr, vp->v_data, NULL, error);
   3165 
   3166 	error = checkerr(pmp, error, __func__);
   3167 	if (error)
   3168 		goto out;
   3169 
   3170 	resid = listextattr_msg->pvnr_resid;
   3171 	if (resid > tomove) {
   3172 		puffs_senderr(pmp, PUFFS_ERR_LISTEXTATTR, E2BIG,
   3173 		    "resid grew", VPTOPNC(vp));
   3174 		error = EPROTO;
   3175 		goto out;
   3176 	}
   3177 
   3178 	if (sizep)
   3179 		*sizep = listextattr_msg->pvnr_datasize;
   3180 	if (uio)
   3181 		error = uiomove(listextattr_msg->pvnr_data, tomove-resid, uio);
   3182 
   3183  out:
   3184 	PUFFS_MSG_RELEASE(listextattr);
   3185 	return error;
   3186 }
   3187 
   3188 int
   3189 puffs_vnop_deleteextattr(void *v)
   3190 {
   3191 	struct vop_deleteextattr_args /* {
   3192 		struct vnode *a_vp;
   3193 		int a_attrnamespace;
   3194 		const char *a_name;
   3195 		kauth_cred_t a_cred;
   3196 	}; */ *ap = v;
   3197 	PUFFS_MSG_VARS(vn, deleteextattr);
   3198 	struct vnode *vp = ap->a_vp;
   3199 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3200 	int attrnamespace = ap->a_attrnamespace;
   3201 	const char *name = ap->a_name;
   3202 	int error;
   3203 
   3204 	PUFFS_MSG_ALLOC(vn, deleteextattr);
   3205 	deleteextattr_msg->pvnr_attrnamespace = attrnamespace;
   3206 	strlcpy(deleteextattr_msg->pvnr_attrname, name,
   3207 	    sizeof(deleteextattr_msg->pvnr_attrname));
   3208 	puffs_credcvt(&deleteextattr_msg->pvnr_cred, ap->a_cred);
   3209 
   3210 	puffs_msg_setinfo(park_deleteextattr,
   3211 	    PUFFSOP_VN, PUFFS_VN_DELETEEXTATTR, VPTOPNC(vp));
   3212 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_deleteextattr,
   3213 	    vp->v_data, NULL, error);
   3214 
   3215 	error = checkerr(pmp, error, __func__);
   3216 
   3217 	PUFFS_MSG_RELEASE(deleteextattr);
   3218 	return error;
   3219 }
   3220 
   3221 /*
   3222  * spec & fifo.  These call the miscfs spec and fifo vectors, but issue
   3223  * FAF update information for the puffs node first.
   3224  */
   3225 int
   3226 puffs_vnop_spec_read(void *v)
   3227 {
   3228 	struct vop_read_args /* {
   3229 		const struct vnodeop_desc *a_desc;
   3230 		struct vnode *a_vp;
   3231 		struct uio *a_uio;
   3232 		int a_ioflag;
   3233 		kauth_cred_t a_cred;
   3234 	} */ *ap = v;
   3235 
   3236 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
   3237 	return VOCALL(spec_vnodeop_p, VOFFSET(vop_read), v);
   3238 }
   3239 
   3240 int
   3241 puffs_vnop_spec_write(void *v)
   3242 {
   3243 	struct vop_write_args /* {
   3244 		const struct vnodeop_desc *a_desc;
   3245 		struct vnode *a_vp;
   3246 		struct uio *a_uio;
   3247 		int a_ioflag;
   3248 		kauth_cred_t a_cred;
   3249 	} */ *ap = v;
   3250 
   3251 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
   3252 	return VOCALL(spec_vnodeop_p, VOFFSET(vop_write), v);
   3253 }
   3254 
   3255 int
   3256 puffs_vnop_fifo_read(void *v)
   3257 {
   3258 	struct vop_read_args /* {
   3259 		const struct vnodeop_desc *a_desc;
   3260 		struct vnode *a_vp;
   3261 		struct uio *a_uio;
   3262 		int a_ioflag;
   3263 		kauth_cred_t a_cred;
   3264 	} */ *ap = v;
   3265 
   3266 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
   3267 	return VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), v);
   3268 }
   3269 
   3270 int
   3271 puffs_vnop_fifo_write(void *v)
   3272 {
   3273 	struct vop_write_args /* {
   3274 		const struct vnodeop_desc *a_desc;
   3275 		struct vnode *a_vp;
   3276 		struct uio *a_uio;
   3277 		int a_ioflag;
   3278 		kauth_cred_t a_cred;
   3279 	} */ *ap = v;
   3280 
   3281 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
   3282 	return VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), v);
   3283 }
   3284