Home | History | Annotate | Line # | Download | only in puffs
puffs_vnops.c revision 1.182
      1 /*	$NetBSD: puffs_vnops.c,v 1.182 2014/07/25 08:20:52 dholland 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.182 2014/07/25 08:20:52 dholland 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 	int mode = ap->a_mode;
    897 	int error;
    898 
    899 	DPRINTF(("puffs_open: vp %p, mode 0x%x\n", vp, mode));
    900 
    901 	if (vp->v_type == VREG && mode & FWRITE && !EXISTSOP(pmp, WRITE))
    902 		ERROUT(EROFS);
    903 
    904 	if (!EXISTSOP(pmp, OPEN))
    905 		ERROUT(0);
    906 
    907 	PUFFS_MSG_ALLOC(vn, open);
    908 	open_msg->pvnr_mode = mode;
    909 	puffs_credcvt(&open_msg->pvnr_cred, ap->a_cred);
    910 	puffs_msg_setinfo(park_open, PUFFSOP_VN,
    911 	    PUFFS_VN_OPEN, VPTOPNC(vp));
    912 
    913 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_open, vp->v_data, NULL, error);
    914 	error = checkerr(pmp, error, __func__);
    915 
    916  out:
    917 	DPRINTF(("puffs_open: returning %d\n", error));
    918 	PUFFS_MSG_RELEASE(open);
    919 	return error;
    920 }
    921 
    922 int
    923 puffs_vnop_close(void *v)
    924 {
    925 	struct vop_close_args /* {
    926 		const struct vnodeop_desc *a_desc;
    927 		struct vnode *a_vp;
    928 		int a_fflag;
    929 		kauth_cred_t a_cred;
    930 	} */ *ap = v;
    931 	PUFFS_MSG_VARS(vn, close);
    932 	struct vnode *vp = ap->a_vp;
    933 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    934 
    935 	PUFFS_MSG_ALLOC(vn, close);
    936 	puffs_msg_setfaf(park_close);
    937 	close_msg->pvnr_fflag = ap->a_fflag;
    938 	puffs_credcvt(&close_msg->pvnr_cred, ap->a_cred);
    939 	puffs_msg_setinfo(park_close, PUFFSOP_VN,
    940 	    PUFFS_VN_CLOSE, VPTOPNC(vp));
    941 
    942 	puffs_msg_enqueue(pmp, park_close);
    943 	PUFFS_MSG_RELEASE(close);
    944 	return 0;
    945 }
    946 
    947 int
    948 puffs_vnop_access(void *v)
    949 {
    950 	struct vop_access_args /* {
    951 		const struct vnodeop_desc *a_desc;
    952 		struct vnode *a_vp;
    953 		int a_mode;
    954 		kauth_cred_t a_cred;
    955 	} */ *ap = v;
    956 	PUFFS_MSG_VARS(vn, access);
    957 	struct vnode *vp = ap->a_vp;
    958 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    959 	int mode = ap->a_mode;
    960 	int error;
    961 
    962 	if (mode & VWRITE) {
    963 		switch (vp->v_type) {
    964 		case VDIR:
    965 		case VLNK:
    966 		case VREG:
    967 			if ((vp->v_mount->mnt_flag & MNT_RDONLY)
    968 			    || !EXISTSOP(pmp, WRITE))
    969 				return EROFS;
    970 			break;
    971 		default:
    972 			break;
    973 		}
    974 	}
    975 
    976 	if (!EXISTSOP(pmp, ACCESS))
    977 		return 0;
    978 
    979 	PUFFS_MSG_ALLOC(vn, access);
    980 	access_msg->pvnr_mode = ap->a_mode;
    981 	puffs_credcvt(&access_msg->pvnr_cred, ap->a_cred);
    982 	puffs_msg_setinfo(park_access, PUFFSOP_VN,
    983 	    PUFFS_VN_ACCESS, VPTOPNC(vp));
    984 
    985 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_access, vp->v_data, NULL, error);
    986 	error = checkerr(pmp, error, __func__);
    987 	PUFFS_MSG_RELEASE(access);
    988 
    989 	return error;
    990 }
    991 
    992 static void
    993 update_va(struct vnode *vp, struct vattr *vap, struct vattr *rvap,
    994 	  struct timespec *va_ttl, struct timespec *cn_ttl, int flags)
    995 {
    996 	struct puffs_node *pn = VPTOPP(vp);
    997 
    998 	if (TTL_VALID(cn_ttl)) {
    999 		pn->pn_cn_timeout = TTL_TO_TIMEOUT(cn_ttl);
   1000 		pn->pn_cn_grace = MAX(pn->pn_cn_timeout, pn->pn_cn_grace);
   1001 	}
   1002 
   1003 	/*
   1004 	 * Don't listen to the file server regarding special device
   1005 	 * size info, the file server doesn't know anything about them.
   1006 	 */
   1007 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1008 		rvap->va_size = vp->v_size;
   1009 
   1010 	/* Ditto for blocksize (ufs comment: this doesn't belong here) */
   1011 	if (vp->v_type == VBLK)
   1012 		rvap->va_blocksize = BLKDEV_IOSIZE;
   1013 	else if (vp->v_type == VCHR)
   1014 		rvap->va_blocksize = MAXBSIZE;
   1015 
   1016 	if (vap != NULL) {
   1017 		(void) memcpy(vap, rvap, sizeof(struct vattr));
   1018 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
   1019 
   1020 		if (pn->pn_stat & PNODE_METACACHE_ATIME)
   1021 			vap->va_atime = pn->pn_mc_atime;
   1022 		if (pn->pn_stat & PNODE_METACACHE_CTIME)
   1023 			vap->va_ctime = pn->pn_mc_ctime;
   1024 		if (pn->pn_stat & PNODE_METACACHE_MTIME)
   1025 			vap->va_mtime = pn->pn_mc_mtime;
   1026 		if (pn->pn_stat & PNODE_METACACHE_SIZE)
   1027 			vap->va_size = pn->pn_mc_size;
   1028 	}
   1029 
   1030 	if (!(pn->pn_stat & PNODE_METACACHE_SIZE) && (flags & SETATTR_CHSIZE)) {
   1031 		if (rvap->va_size != VNOVAL
   1032 		    && vp->v_type != VBLK && vp->v_type != VCHR) {
   1033 			uvm_vnp_setsize(vp, rvap->va_size);
   1034 			pn->pn_serversize = rvap->va_size;
   1035 		}
   1036 	}
   1037 
   1038 	if ((va_ttl != NULL) && TTL_VALID(va_ttl)) {
   1039 		if (pn->pn_va_cache == NULL)
   1040 			pn->pn_va_cache = pool_get(&puffs_vapool, PR_WAITOK);
   1041 
   1042 		(void)memcpy(pn->pn_va_cache, rvap, sizeof(*rvap));
   1043 
   1044 		pn->pn_va_timeout = TTL_TO_TIMEOUT(va_ttl);
   1045 	}
   1046 }
   1047 
   1048 static void
   1049 update_parent(struct vnode *vp, struct vnode *dvp)
   1050 {
   1051 	struct puffs_node *pn = VPTOPP(vp);
   1052 
   1053 	if (pn->pn_parent != NULL) {
   1054 		KASSERT(pn->pn_parent != dvp);
   1055 		vrele(pn->pn_parent);
   1056 	}
   1057 
   1058 	vref(dvp);
   1059 	pn->pn_parent = dvp;
   1060 }
   1061 
   1062 int
   1063 puffs_vnop_getattr(void *v)
   1064 {
   1065 	struct vop_getattr_args /* {
   1066 		const struct vnodeop_desc *a_desc;
   1067 		struct vnode *a_vp;
   1068 		struct vattr *a_vap;
   1069 		kauth_cred_t a_cred;
   1070 	} */ *ap = v;
   1071 	PUFFS_MSG_VARS(vn, getattr);
   1072 	struct vnode *vp = ap->a_vp;
   1073 	struct mount *mp = vp->v_mount;
   1074 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
   1075 	struct vattr *vap, *rvap;
   1076 	struct puffs_node *pn = VPTOPP(vp);
   1077 	struct timespec *va_ttl = NULL;
   1078 	int error = 0;
   1079 
   1080 	/*
   1081 	 * A lock is required so that we do not race with
   1082 	 * setattr, write and fsync when changing vp->v_size.
   1083 	 * This is critical, since setting a stall smaler value
   1084 	 * triggers a file truncate in uvm_vnp_setsize(), which
   1085 	 * most of the time means data corruption (a chunk of
   1086 	 * data is replaced by zeroes). This can be removed if
   1087 	 * we decide one day that VOP_GETATTR must operate on
   1088 	 * a locked vnode.
   1089 	 *
   1090 	 * XXX Should be useless now that VOP_GETATTR has been
   1091 	 *     fixed to always require a shared lock at least.
   1092 	 */
   1093 	mutex_enter(&pn->pn_sizemtx);
   1094 
   1095 	REFPN(pn);
   1096 	vap = ap->a_vap;
   1097 
   1098 	if (PUFFS_USE_FS_TTL(pmp)) {
   1099 		if (!TIMED_OUT(pn->pn_va_timeout)) {
   1100 			update_va(vp, vap, pn->pn_va_cache,
   1101 				  NULL, NULL, SETATTR_CHSIZE);
   1102 			goto out2;
   1103 		}
   1104 	}
   1105 
   1106 	PUFFS_MSG_ALLOC(vn, getattr);
   1107 	vattr_null(&getattr_msg->pvnr_va);
   1108 	puffs_credcvt(&getattr_msg->pvnr_cred, ap->a_cred);
   1109 	puffs_msg_setinfo(park_getattr, PUFFSOP_VN,
   1110 	    PUFFS_VN_GETATTR, VPTOPNC(vp));
   1111 
   1112 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_getattr, vp->v_data, NULL, error);
   1113 	error = checkerr(pmp, error, __func__);
   1114 	if (error)
   1115 		goto out;
   1116 
   1117 	rvap = &getattr_msg->pvnr_va;
   1118 
   1119 	if (PUFFS_USE_FS_TTL(pmp))
   1120 		va_ttl = &getattr_msg->pvnr_va_ttl;
   1121 
   1122 	update_va(vp, vap, rvap, va_ttl, NULL, SETATTR_CHSIZE);
   1123 
   1124  out:
   1125 	PUFFS_MSG_RELEASE(getattr);
   1126 
   1127  out2:
   1128 	puffs_releasenode(pn);
   1129 
   1130 	mutex_exit(&pn->pn_sizemtx);
   1131 
   1132 	return error;
   1133 }
   1134 
   1135 static int
   1136 dosetattr(struct vnode *vp, struct vattr *vap, kauth_cred_t cred, int flags)
   1137 {
   1138 	PUFFS_MSG_VARS(vn, setattr);
   1139 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1140 	struct puffs_node *pn = vp->v_data;
   1141 	int error = 0;
   1142 
   1143 	KASSERT(!(flags & SETATTR_CHSIZE) || mutex_owned(&pn->pn_sizemtx));
   1144 
   1145 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) &&
   1146 	    (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL
   1147 	    || vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL
   1148 	    || vap->va_mode != (mode_t)VNOVAL))
   1149 		return EROFS;
   1150 
   1151 	if ((vp->v_mount->mnt_flag & MNT_RDONLY)
   1152 	    && vp->v_type == VREG && vap->va_size != VNOVAL)
   1153 		return EROFS;
   1154 
   1155 	/*
   1156 	 * Flush metacache first.  If we are called with some explicit
   1157 	 * parameters, treat them as information overriding metacache
   1158 	 * information.
   1159 	 */
   1160 	if (pn->pn_stat & PNODE_METACACHE_MASK) {
   1161 		if ((pn->pn_stat & PNODE_METACACHE_ATIME)
   1162 		    && vap->va_atime.tv_sec == VNOVAL)
   1163 			vap->va_atime = pn->pn_mc_atime;
   1164 		if ((pn->pn_stat & PNODE_METACACHE_CTIME)
   1165 		    && vap->va_ctime.tv_sec == VNOVAL)
   1166 			vap->va_ctime = pn->pn_mc_ctime;
   1167 		if ((pn->pn_stat & PNODE_METACACHE_MTIME)
   1168 		    && vap->va_mtime.tv_sec == VNOVAL)
   1169 			vap->va_mtime = pn->pn_mc_mtime;
   1170 		if ((pn->pn_stat & PNODE_METACACHE_SIZE)
   1171 		    && vap->va_size == VNOVAL)
   1172 			vap->va_size = pn->pn_mc_size;
   1173 
   1174 		pn->pn_stat &= ~PNODE_METACACHE_MASK;
   1175 	}
   1176 
   1177 	/*
   1178 	 * Flush attribute cache so that another thread do
   1179 	 * not get a stale value during the operation.
   1180 	 */
   1181 	if (PUFFS_USE_FS_TTL(pmp))
   1182 		pn->pn_va_timeout = 0;
   1183 
   1184 	PUFFS_MSG_ALLOC(vn, setattr);
   1185 	(void)memcpy(&setattr_msg->pvnr_va, vap, sizeof(struct vattr));
   1186 	puffs_credcvt(&setattr_msg->pvnr_cred, cred);
   1187 	puffs_msg_setinfo(park_setattr, PUFFSOP_VN,
   1188 	    PUFFS_VN_SETATTR, VPTOPNC(vp));
   1189 	if (flags & SETATTR_ASYNC)
   1190 		puffs_msg_setfaf(park_setattr);
   1191 
   1192 	puffs_msg_enqueue(pmp, park_setattr);
   1193 	if ((flags & SETATTR_ASYNC) == 0)
   1194 		error = puffs_msg_wait2(pmp, park_setattr, vp->v_data, NULL);
   1195 
   1196 	if ((error == 0) && PUFFS_USE_FS_TTL(pmp)) {
   1197 		struct timespec *va_ttl = &setattr_msg->pvnr_va_ttl;
   1198 		struct vattr *rvap = &setattr_msg->pvnr_va;
   1199 
   1200 		update_va(vp, NULL, rvap, va_ttl, NULL, flags);
   1201 	}
   1202 
   1203 	PUFFS_MSG_RELEASE(setattr);
   1204 	if ((flags & SETATTR_ASYNC) == 0) {
   1205 		error = checkerr(pmp, error, __func__);
   1206 		if (error)
   1207 			return error;
   1208 	} else {
   1209 		error = 0;
   1210 	}
   1211 
   1212 	if (vap->va_size != VNOVAL) {
   1213 		pn->pn_serversize = vap->va_size;
   1214 		if (flags & SETATTR_CHSIZE)
   1215 			uvm_vnp_setsize(vp, vap->va_size);
   1216 	}
   1217 
   1218 	return 0;
   1219 }
   1220 
   1221 int
   1222 puffs_vnop_setattr(void *v)
   1223 {
   1224 	struct vop_getattr_args /* {
   1225 		const struct vnodeop_desc *a_desc;
   1226 		struct vnode *a_vp;
   1227 		struct vattr *a_vap;
   1228 		kauth_cred_t a_cred;
   1229 	} */ *ap = v;
   1230 	struct puffs_node *pn = ap->a_vp->v_data;
   1231 	int error;
   1232 
   1233 	mutex_enter(&pn->pn_sizemtx);
   1234 	error = dosetattr(ap->a_vp, ap->a_vap, ap->a_cred, SETATTR_CHSIZE);
   1235 	mutex_exit(&pn->pn_sizemtx);
   1236 
   1237 	return error;
   1238 }
   1239 
   1240 static __inline int
   1241 doinact(struct puffs_mount *pmp, int iaflag)
   1242 {
   1243 
   1244 	if (EXISTSOP(pmp, INACTIVE))
   1245 		if (pmp->pmp_flags & PUFFS_KFLAG_IAONDEMAND)
   1246 			if (iaflag || ALLOPS(pmp))
   1247 				return 1;
   1248 			else
   1249 				return 0;
   1250 		else
   1251 			return 1;
   1252 	else
   1253 		return 0;
   1254 }
   1255 
   1256 static void
   1257 callinactive(struct puffs_mount *pmp, puffs_cookie_t ck, int iaflag)
   1258 {
   1259 	PUFFS_MSG_VARS(vn, inactive);
   1260 
   1261 	if (doinact(pmp, iaflag)) {
   1262 		PUFFS_MSG_ALLOC(vn, inactive);
   1263 		puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
   1264 		    PUFFS_VN_INACTIVE, ck);
   1265 		PUFFS_MSG_ENQUEUEWAIT_NOERROR(pmp, park_inactive);
   1266 		PUFFS_MSG_RELEASE(inactive);
   1267 	}
   1268 }
   1269 
   1270 /* XXX: callinactive can't setback */
   1271 int
   1272 puffs_vnop_inactive(void *v)
   1273 {
   1274 	struct vop_inactive_args /* {
   1275 		const struct vnodeop_desc *a_desc;
   1276 		struct vnode *a_vp;
   1277 	} */ *ap = v;
   1278 	PUFFS_MSG_VARS(vn, inactive);
   1279 	struct vnode *vp = ap->a_vp;
   1280 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1281 	struct puffs_node *pnode;
   1282 	bool recycle = false;
   1283 
   1284 	pnode = vp->v_data;
   1285 	mutex_enter(&pnode->pn_sizemtx);
   1286 
   1287 	if (doinact(pmp, pnode->pn_stat & PNODE_DOINACT)) {
   1288 		flushvncache(vp, 0, 0, false);
   1289 		PUFFS_MSG_ALLOC(vn, inactive);
   1290 		puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
   1291 		    PUFFS_VN_INACTIVE, VPTOPNC(vp));
   1292 		PUFFS_MSG_ENQUEUEWAIT2_NOERROR(pmp, park_inactive, vp->v_data,
   1293 		    NULL);
   1294 		PUFFS_MSG_RELEASE(inactive);
   1295 	}
   1296 	pnode->pn_stat &= ~PNODE_DOINACT;
   1297 
   1298 	/*
   1299 	 * file server thinks it's gone?  then don't be afraid care,
   1300 	 * node's life was already all it would ever be
   1301 	 */
   1302 	if (pnode->pn_stat & PNODE_NOREFS) {
   1303 		pnode->pn_stat |= PNODE_DYING;
   1304 		recycle = true;
   1305 	}
   1306 
   1307 	/*
   1308 	 * Handle node TTL.
   1309 	 * If grace has already timed out, make it reclaimed.
   1310 	 * Otherwise, we queue its expiration by sop thread, so
   1311 	 * that it does not remain for ages in the freelist,
   1312 	 * holding memory in userspace, while we will have
   1313 	 * to look it up again anyway.
   1314 	 */
   1315 	if (PUFFS_USE_FS_TTL(pmp) && !(vp->v_vflag & VV_ROOT) && !recycle) {
   1316 		bool incache = !TIMED_OUT(pnode->pn_cn_timeout);
   1317 		bool ingrace = !TIMED_OUT(pnode->pn_cn_grace);
   1318 		bool reclaimqueued = pnode->pn_stat & PNODE_SOPEXP;
   1319 
   1320 		if (!incache && !ingrace && !reclaimqueued) {
   1321 			pnode->pn_stat |= PNODE_DYING;
   1322 			recycle = true;
   1323 		}
   1324 
   1325 		if (!recycle && !reclaimqueued) {
   1326 			struct puffs_sopreq *psopr;
   1327 			int at = MAX(pnode->pn_cn_grace, pnode->pn_cn_timeout);
   1328 
   1329 			KASSERT(curlwp != uvm.pagedaemon_lwp);
   1330 			psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
   1331 			psopr->psopr_ck = VPTOPNC(pnode->pn_vp);
   1332 			psopr->psopr_sopreq = PUFFS_SOPREQ_EXPIRE;
   1333 			psopr->psopr_at = at;
   1334 
   1335 			mutex_enter(&pmp->pmp_sopmtx);
   1336 
   1337 			/*
   1338 			 * If thread has disapeared, just give up. The
   1339 			 * fs is being unmounted and the node will be
   1340 			 * be reclaimed anyway.
   1341 			 *
   1342 			 * Otherwise, we queue the request but do not
   1343 			 * immediatly signal the thread, as the node
   1344 			 * has not been expired yet.
   1345 			 */
   1346 			if (pmp->pmp_sopthrcount == 0) {
   1347 				kmem_free(psopr, sizeof(*psopr));
   1348 			} else {
   1349 				TAILQ_INSERT_TAIL(&pmp->pmp_sopnodereqs,
   1350 				    psopr, psopr_entries);
   1351 				pnode->pn_stat |= PNODE_SOPEXP;
   1352 			}
   1353 
   1354 			mutex_exit(&pmp->pmp_sopmtx);
   1355 		}
   1356 	}
   1357 
   1358 	*ap->a_recycle = recycle;
   1359 
   1360 	mutex_exit(&pnode->pn_sizemtx);
   1361 	VOP_UNLOCK(vp);
   1362 
   1363 	return 0;
   1364 }
   1365 
   1366 static void
   1367 callreclaim(struct puffs_mount *pmp, puffs_cookie_t ck, int nlookup)
   1368 {
   1369 	PUFFS_MSG_VARS(vn, reclaim);
   1370 
   1371 	if (!EXISTSOP(pmp, RECLAIM))
   1372 		return;
   1373 
   1374 	PUFFS_MSG_ALLOC(vn, reclaim);
   1375 	reclaim_msg->pvnr_nlookup = nlookup;
   1376 	puffs_msg_setfaf(park_reclaim);
   1377 	puffs_msg_setinfo(park_reclaim, PUFFSOP_VN, PUFFS_VN_RECLAIM, ck);
   1378 
   1379 	puffs_msg_enqueue(pmp, park_reclaim);
   1380 	PUFFS_MSG_RELEASE(reclaim);
   1381 	return;
   1382 }
   1383 
   1384 /*
   1385  * always FAF, we don't really care if the server wants to fail to
   1386  * reclaim the node or not
   1387  */
   1388 int
   1389 puffs_vnop_reclaim(void *v)
   1390 {
   1391 	struct vop_reclaim_args /* {
   1392 		const struct vnodeop_desc *a_desc;
   1393 		struct vnode *a_vp;
   1394 	} */ *ap = v;
   1395 	struct vnode *vp = ap->a_vp;
   1396 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1397 	struct puffs_node *pnode = vp->v_data;
   1398 	bool notifyserver = true;
   1399 
   1400 	/*
   1401 	 * first things first: check if someone is trying to reclaim the
   1402 	 * root vnode.  do not allow that to travel to userspace.
   1403 	 * Note that we don't need to take the lock similarly to
   1404 	 * puffs_root(), since there is only one of us.
   1405 	 */
   1406 	if (vp->v_vflag & VV_ROOT) {
   1407 		mutex_enter(&pmp->pmp_lock);
   1408 		KASSERT(pmp->pmp_root != NULL);
   1409 		pmp->pmp_root = NULL;
   1410 		mutex_exit(&pmp->pmp_lock);
   1411 		notifyserver = false;
   1412 	}
   1413 
   1414 	/*
   1415 	 * purge info from kernel before issueing FAF, since we
   1416 	 * don't really know when we'll get around to it after
   1417 	 * that and someone might race us into node creation
   1418 	 */
   1419 	mutex_enter(&pmp->pmp_lock);
   1420 	LIST_REMOVE(pnode, pn_hashent);
   1421 	if (PUFFS_USE_NAMECACHE(pmp))
   1422 		cache_purge(vp);
   1423 	mutex_exit(&pmp->pmp_lock);
   1424 
   1425 	if (notifyserver) {
   1426 		int nlookup = VPTOPP(vp)->pn_nlookup;
   1427 
   1428 		callreclaim(MPTOPUFFSMP(vp->v_mount), VPTOPNC(vp), nlookup);
   1429 	}
   1430 
   1431 	if (PUFFS_USE_DOTDOTCACHE(pmp)) {
   1432 		if (__predict_true(VPTOPP(vp)->pn_parent != NULL))
   1433 			vrele(VPTOPP(vp)->pn_parent);
   1434 		else
   1435 			KASSERT(vp->v_vflag & VV_ROOT);
   1436 	}
   1437 
   1438 	puffs_putvnode(vp);
   1439 	vp->v_data = NULL;
   1440 
   1441 	return 0;
   1442 }
   1443 
   1444 #define CSIZE sizeof(**ap->a_cookies)
   1445 int
   1446 puffs_vnop_readdir(void *v)
   1447 {
   1448 	struct vop_readdir_args /* {
   1449 		const struct vnodeop_desc *a_desc;
   1450 		struct vnode *a_vp;
   1451 		struct uio *a_uio;
   1452 		kauth_cred_t a_cred;
   1453 		int *a_eofflag;
   1454 		off_t **a_cookies;
   1455 		int *a_ncookies;
   1456 	} */ *ap = v;
   1457 	PUFFS_MSG_VARS(vn, readdir);
   1458 	struct vnode *vp = ap->a_vp;
   1459 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1460 	size_t argsize, tomove, cookiemem, cookiesmax;
   1461 	struct uio *uio = ap->a_uio;
   1462 	size_t howmuch, resid;
   1463 	int error;
   1464 
   1465 	/*
   1466 	 * ok, so we need: resid + cookiemem = maxreq
   1467 	 * => resid + cookiesize * (resid/minsize) = maxreq
   1468 	 * => resid + cookiesize/minsize * resid = maxreq
   1469 	 * => (cookiesize/minsize + 1) * resid = maxreq
   1470 	 * => resid = maxreq / (cookiesize/minsize + 1)
   1471 	 *
   1472 	 * Since cookiesize <= minsize and we're not very big on floats,
   1473 	 * we approximate that to be 1.  Therefore:
   1474 	 *
   1475 	 * resid = maxreq / 2;
   1476 	 *
   1477 	 * Well, at least we didn't have to use differential equations
   1478 	 * or the Gram-Schmidt process.
   1479 	 *
   1480 	 * (yes, I'm very afraid of this)
   1481 	 */
   1482 	KASSERT(CSIZE <= _DIRENT_MINSIZE((struct dirent *)0));
   1483 
   1484 	if (ap->a_cookies) {
   1485 		KASSERT(ap->a_ncookies != NULL);
   1486 		if (pmp->pmp_args.pa_fhsize == 0)
   1487 			return EOPNOTSUPP;
   1488 		resid = PUFFS_TOMOVE(uio->uio_resid, pmp) / 2;
   1489 		cookiesmax = resid/_DIRENT_MINSIZE((struct dirent *)0);
   1490 		cookiemem = ALIGN(cookiesmax*CSIZE); /* play safe */
   1491 	} else {
   1492 		resid = PUFFS_TOMOVE(uio->uio_resid, pmp);
   1493 		cookiesmax = 0;
   1494 		cookiemem = 0;
   1495 	}
   1496 
   1497 	argsize = sizeof(struct puffs_vnmsg_readdir);
   1498 	tomove = resid + cookiemem;
   1499 	puffs_msgmem_alloc(argsize + tomove, &park_readdir,
   1500 	    (void *)&readdir_msg, 1);
   1501 
   1502 	puffs_credcvt(&readdir_msg->pvnr_cred, ap->a_cred);
   1503 	readdir_msg->pvnr_offset = uio->uio_offset;
   1504 	readdir_msg->pvnr_resid = resid;
   1505 	readdir_msg->pvnr_ncookies = cookiesmax;
   1506 	readdir_msg->pvnr_eofflag = 0;
   1507 	readdir_msg->pvnr_dentoff = cookiemem;
   1508 	puffs_msg_setinfo(park_readdir, PUFFSOP_VN,
   1509 	    PUFFS_VN_READDIR, VPTOPNC(vp));
   1510 	puffs_msg_setdelta(park_readdir, tomove);
   1511 
   1512 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_readdir, vp->v_data, NULL, error);
   1513 	error = checkerr(pmp, error, __func__);
   1514 	if (error)
   1515 		goto out;
   1516 
   1517 	/* userspace is cheating? */
   1518 	if (readdir_msg->pvnr_resid > resid) {
   1519 		puffs_senderr(pmp, PUFFS_ERR_READDIR, E2BIG,
   1520 		    "resid grew", VPTOPNC(vp));
   1521 		ERROUT(EPROTO);
   1522 	}
   1523 	if (readdir_msg->pvnr_ncookies > cookiesmax) {
   1524 		puffs_senderr(pmp, PUFFS_ERR_READDIR, E2BIG,
   1525 		    "too many cookies", VPTOPNC(vp));
   1526 		ERROUT(EPROTO);
   1527 	}
   1528 
   1529 	/* check eof */
   1530 	if (readdir_msg->pvnr_eofflag)
   1531 		*ap->a_eofflag = 1;
   1532 
   1533 	/* bouncy-wouncy with the directory data */
   1534 	howmuch = resid - readdir_msg->pvnr_resid;
   1535 
   1536 	/* force eof if no data was returned (getcwd() needs this) */
   1537 	if (howmuch == 0) {
   1538 		*ap->a_eofflag = 1;
   1539 		goto out;
   1540 	}
   1541 
   1542 	error = uiomove(readdir_msg->pvnr_data + cookiemem, howmuch, uio);
   1543 	if (error)
   1544 		goto out;
   1545 
   1546 	/* provide cookies to caller if so desired */
   1547 	if (ap->a_cookies) {
   1548 		KASSERT(curlwp != uvm.pagedaemon_lwp);
   1549 		*ap->a_cookies = malloc(readdir_msg->pvnr_ncookies*CSIZE,
   1550 		    M_TEMP, M_WAITOK);
   1551 		*ap->a_ncookies = readdir_msg->pvnr_ncookies;
   1552 		memcpy(*ap->a_cookies, readdir_msg->pvnr_data,
   1553 		    *ap->a_ncookies*CSIZE);
   1554 	}
   1555 
   1556 	/* next readdir starts here */
   1557 	uio->uio_offset = readdir_msg->pvnr_offset;
   1558 
   1559  out:
   1560 	puffs_msgmem_release(park_readdir);
   1561 	return error;
   1562 }
   1563 #undef CSIZE
   1564 
   1565 /*
   1566  * poll works by consuming the bitmask in pn_revents.  If there are
   1567  * events available, poll returns immediately.  If not, it issues a
   1568  * poll to userspace, selrecords itself and returns with no available
   1569  * events.  When the file server returns, it executes puffs_parkdone_poll(),
   1570  * where available events are added to the bitmask.  selnotify() is
   1571  * then also executed by that function causing us to enter here again
   1572  * and hopefully find the missing bits (unless someone got them first,
   1573  * in which case it starts all over again).
   1574  */
   1575 int
   1576 puffs_vnop_poll(void *v)
   1577 {
   1578 	struct vop_poll_args /* {
   1579 		const struct vnodeop_desc *a_desc;
   1580 		struct vnode *a_vp;
   1581 		int a_events;
   1582 	} */ *ap = v;
   1583 	PUFFS_MSG_VARS(vn, poll);
   1584 	struct vnode *vp = ap->a_vp;
   1585 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1586 	struct puffs_node *pn = vp->v_data;
   1587 	int events;
   1588 
   1589 	if (EXISTSOP(pmp, POLL)) {
   1590 		mutex_enter(&pn->pn_mtx);
   1591 		events = pn->pn_revents & ap->a_events;
   1592 		if (events & ap->a_events) {
   1593 			pn->pn_revents &= ~ap->a_events;
   1594 			mutex_exit(&pn->pn_mtx);
   1595 
   1596 			return events;
   1597 		} else {
   1598 			puffs_referencenode(pn);
   1599 			mutex_exit(&pn->pn_mtx);
   1600 
   1601 			PUFFS_MSG_ALLOC(vn, poll);
   1602 			poll_msg->pvnr_events = ap->a_events;
   1603 			puffs_msg_setinfo(park_poll, PUFFSOP_VN,
   1604 			    PUFFS_VN_POLL, VPTOPNC(vp));
   1605 			puffs_msg_setcall(park_poll, puffs_parkdone_poll, pn);
   1606 			selrecord(curlwp, &pn->pn_sel);
   1607 
   1608 			PUFFS_MSG_ENQUEUEWAIT2_NOERROR(pmp, park_poll,
   1609 			    vp->v_data, NULL);
   1610 			PUFFS_MSG_RELEASE(poll);
   1611 
   1612 			return 0;
   1613 		}
   1614 	} else {
   1615 		return genfs_poll(v);
   1616 	}
   1617 }
   1618 
   1619 static int
   1620 flushvncache(struct vnode *vp, off_t offlo, off_t offhi, bool wait)
   1621 {
   1622 	struct puffs_node *pn = VPTOPP(vp);
   1623 	struct vattr va;
   1624 	int pflags, error;
   1625 
   1626 	/* flush out information from our metacache, see vop_setattr */
   1627 	if (pn->pn_stat & PNODE_METACACHE_MASK
   1628 	    && (pn->pn_stat & PNODE_DYING) == 0) {
   1629 		vattr_null(&va);
   1630 		error = dosetattr(vp, &va, FSCRED,
   1631 		    SETATTR_CHSIZE | (wait ? 0 : SETATTR_ASYNC));
   1632 		if (error)
   1633 			return error;
   1634 	}
   1635 
   1636 	/*
   1637 	 * flush pages to avoid being overly dirty
   1638 	 */
   1639 	pflags = PGO_CLEANIT;
   1640 	if (wait)
   1641 		pflags |= PGO_SYNCIO;
   1642 
   1643 	mutex_enter(vp->v_interlock);
   1644 	return VOP_PUTPAGES(vp, trunc_page(offlo), round_page(offhi), pflags);
   1645 }
   1646 
   1647 int
   1648 puffs_vnop_fsync(void *v)
   1649 {
   1650 	struct vop_fsync_args /* {
   1651 		const struct vnodeop_desc *a_desc;
   1652 		struct vnode *a_vp;
   1653 		kauth_cred_t a_cred;
   1654 		int a_flags;
   1655 		off_t a_offlo;
   1656 		off_t a_offhi;
   1657 	} */ *ap = v;
   1658 	PUFFS_MSG_VARS(vn, fsync);
   1659 	struct vnode *vp;
   1660 	struct puffs_node *pn;
   1661 	struct puffs_mount *pmp;
   1662 	int error, dofaf;
   1663 
   1664 	vp = ap->a_vp;
   1665 	KASSERT(vp != NULL);
   1666 	pn = VPTOPP(vp);
   1667 	KASSERT(pn != NULL);
   1668 	pmp = MPTOPUFFSMP(vp->v_mount);
   1669 	if (ap->a_flags & FSYNC_WAIT) {
   1670 		mutex_enter(&pn->pn_sizemtx);
   1671 	} else {
   1672 		if (mutex_tryenter(&pn->pn_sizemtx) == 0)
   1673 			return EDEADLK;
   1674 	}
   1675 
   1676 	error = flushvncache(vp, ap->a_offlo, ap->a_offhi,
   1677 	    (ap->a_flags & FSYNC_WAIT) == FSYNC_WAIT);
   1678 	if (error)
   1679 		goto out;
   1680 
   1681 	/*
   1682 	 * HELLO!  We exit already here if the user server does not
   1683 	 * support fsync OR if we should call fsync for a node which
   1684 	 * has references neither in the kernel or the fs server.
   1685 	 * Otherwise we continue to issue fsync() forward.
   1686 	 */
   1687 	error = 0;
   1688 	if (!EXISTSOP(pmp, FSYNC) || (pn->pn_stat & PNODE_DYING))
   1689 		goto out;
   1690 
   1691 	dofaf = (ap->a_flags & FSYNC_WAIT) == 0 || ap->a_flags == FSYNC_LAZY;
   1692 	/*
   1693 	 * We abuse VXLOCK to mean "vnode is going to die", so we issue
   1694 	 * only FAFs for those.  Otherwise there's a danger of deadlock,
   1695 	 * since the execution context here might be the user server
   1696 	 * doing some operation on another fs, which in turn caused a
   1697 	 * vnode to be reclaimed from the freelist for this fs.
   1698 	 */
   1699 	if (dofaf == 0) {
   1700 		mutex_enter(vp->v_interlock);
   1701 		if (vdead_check(vp, VDEAD_NOWAIT) != 0)
   1702 			dofaf = 1;
   1703 		mutex_exit(vp->v_interlock);
   1704 	}
   1705 
   1706 	PUFFS_MSG_ALLOC(vn, fsync);
   1707 	if (dofaf)
   1708 		puffs_msg_setfaf(park_fsync);
   1709 
   1710 	puffs_credcvt(&fsync_msg->pvnr_cred, ap->a_cred);
   1711 	fsync_msg->pvnr_flags = ap->a_flags;
   1712 	fsync_msg->pvnr_offlo = ap->a_offlo;
   1713 	fsync_msg->pvnr_offhi = ap->a_offhi;
   1714 	puffs_msg_setinfo(park_fsync, PUFFSOP_VN,
   1715 	    PUFFS_VN_FSYNC, VPTOPNC(vp));
   1716 
   1717 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_fsync, vp->v_data, NULL, error);
   1718 	PUFFS_MSG_RELEASE(fsync);
   1719 
   1720 	error = checkerr(pmp, error, __func__);
   1721 
   1722 out:
   1723 	mutex_exit(&pn->pn_sizemtx);
   1724 	return error;
   1725 }
   1726 
   1727 int
   1728 puffs_vnop_seek(void *v)
   1729 {
   1730 	struct vop_seek_args /* {
   1731 		const struct vnodeop_desc *a_desc;
   1732 		struct vnode *a_vp;
   1733 		off_t a_oldoff;
   1734 		off_t a_newoff;
   1735 		kauth_cred_t a_cred;
   1736 	} */ *ap = v;
   1737 	PUFFS_MSG_VARS(vn, seek);
   1738 	struct vnode *vp = ap->a_vp;
   1739 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1740 	int error;
   1741 
   1742 	PUFFS_MSG_ALLOC(vn, seek);
   1743 	seek_msg->pvnr_oldoff = ap->a_oldoff;
   1744 	seek_msg->pvnr_newoff = ap->a_newoff;
   1745 	puffs_credcvt(&seek_msg->pvnr_cred, ap->a_cred);
   1746 	puffs_msg_setinfo(park_seek, PUFFSOP_VN,
   1747 	    PUFFS_VN_SEEK, VPTOPNC(vp));
   1748 
   1749 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_seek, vp->v_data, NULL, error);
   1750 	PUFFS_MSG_RELEASE(seek);
   1751 	return checkerr(pmp, error, __func__);
   1752 }
   1753 
   1754 static int
   1755 callremove(struct puffs_mount *pmp, puffs_cookie_t dck, puffs_cookie_t ck,
   1756 	struct componentname *cnp)
   1757 {
   1758 	PUFFS_MSG_VARS(vn, remove);
   1759 	int error;
   1760 
   1761 	PUFFS_MSG_ALLOC(vn, remove);
   1762 	remove_msg->pvnr_cookie_targ = ck;
   1763 	puffs_makecn(&remove_msg->pvnr_cn, &remove_msg->pvnr_cn_cred,
   1764 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1765 	puffs_msg_setinfo(park_remove, PUFFSOP_VN, PUFFS_VN_REMOVE, dck);
   1766 
   1767 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_remove, error);
   1768 	PUFFS_MSG_RELEASE(remove);
   1769 
   1770 	return checkerr(pmp, error, __func__);
   1771 }
   1772 
   1773 /*
   1774  * XXX: can't use callremove now because can't catch setbacks with
   1775  * it due to lack of a pnode argument.
   1776  */
   1777 int
   1778 puffs_vnop_remove(void *v)
   1779 {
   1780 	struct vop_remove_args /* {
   1781 		const struct vnodeop_desc *a_desc;
   1782 		struct vnode *a_dvp;
   1783 		struct vnode *a_vp;
   1784 		struct componentname *a_cnp;
   1785 	} */ *ap = v;
   1786 	PUFFS_MSG_VARS(vn, remove);
   1787 	struct vnode *dvp = ap->a_dvp;
   1788 	struct vnode *vp = ap->a_vp;
   1789 	struct puffs_node *dpn = VPTOPP(dvp);
   1790 	struct puffs_node *pn = VPTOPP(vp);
   1791 	struct componentname *cnp = ap->a_cnp;
   1792 	struct mount *mp = dvp->v_mount;
   1793 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
   1794 	int error;
   1795 
   1796 	PUFFS_MSG_ALLOC(vn, remove);
   1797 	remove_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1798 	puffs_makecn(&remove_msg->pvnr_cn, &remove_msg->pvnr_cn_cred,
   1799 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1800 	puffs_msg_setinfo(park_remove, PUFFSOP_VN,
   1801 	    PUFFS_VN_REMOVE, VPTOPNC(dvp));
   1802 
   1803 	puffs_msg_enqueue(pmp, park_remove);
   1804 	REFPN_AND_UNLOCKVP(dvp, dpn);
   1805 	if (dvp == vp)
   1806 		REFPN(pn);
   1807 	else
   1808 		REFPN_AND_UNLOCKVP(vp, pn);
   1809 	error = puffs_msg_wait2(pmp, park_remove, dpn, pn);
   1810 
   1811 	PUFFS_MSG_RELEASE(remove);
   1812 
   1813 	RELEPN_AND_VP(dvp, dpn);
   1814 	RELEPN_AND_VP(vp, pn);
   1815 
   1816 	error = checkerr(pmp, error, __func__);
   1817 	return error;
   1818 }
   1819 
   1820 int
   1821 puffs_vnop_mkdir(void *v)
   1822 {
   1823 	struct vop_mkdir_v3_args /* {
   1824 		const struct vnodeop_desc *a_desc;
   1825 		struct vnode *a_dvp;
   1826 		struct vnode **a_vpp;
   1827 		struct componentname *a_cnp;
   1828 		struct vattr *a_vap;
   1829 	} */ *ap = v;
   1830 	PUFFS_MSG_VARS(vn, mkdir);
   1831 	struct vnode *dvp = ap->a_dvp;
   1832 	struct puffs_node *dpn = VPTOPP(dvp);
   1833 	struct componentname *cnp = ap->a_cnp;
   1834 	struct mount *mp = dvp->v_mount;
   1835 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
   1836 	int error;
   1837 
   1838 	PUFFS_MSG_ALLOC(vn, mkdir);
   1839 	puffs_makecn(&mkdir_msg->pvnr_cn, &mkdir_msg->pvnr_cn_cred,
   1840 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1841 	mkdir_msg->pvnr_va = *ap->a_vap;
   1842 	puffs_msg_setinfo(park_mkdir, PUFFSOP_VN,
   1843 	    PUFFS_VN_MKDIR, VPTOPNC(dvp));
   1844 
   1845 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mkdir, dvp->v_data, NULL, error);
   1846 
   1847 	error = checkerr(pmp, error, __func__);
   1848 	if (error)
   1849 		goto out;
   1850 
   1851 	error = puffs_newnode(mp, dvp, ap->a_vpp,
   1852 	    mkdir_msg->pvnr_newnode, cnp, VDIR, 0);
   1853 	if (error) {
   1854 		puffs_abortbutton(pmp, PUFFS_ABORT_MKDIR, dpn->pn_cookie,
   1855 		    mkdir_msg->pvnr_newnode, cnp);
   1856 		goto out;
   1857 	}
   1858 
   1859 	if (PUFFS_USE_FS_TTL(pmp)) {
   1860 		struct timespec *va_ttl = &mkdir_msg->pvnr_va_ttl;
   1861 		struct timespec *cn_ttl = &mkdir_msg->pvnr_cn_ttl;
   1862 		struct vattr *rvap = &mkdir_msg->pvnr_va;
   1863 
   1864 		update_va(*ap->a_vpp, NULL, rvap,
   1865 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
   1866 	}
   1867 
   1868 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
   1869 
   1870 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   1871 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
   1872 		update_parent(*ap->a_vpp, dvp);
   1873 
   1874  out:
   1875 	PUFFS_MSG_RELEASE(mkdir);
   1876 	return error;
   1877 }
   1878 
   1879 static int
   1880 callrmdir(struct puffs_mount *pmp, puffs_cookie_t dck, puffs_cookie_t ck,
   1881 	struct componentname *cnp)
   1882 {
   1883 	PUFFS_MSG_VARS(vn, rmdir);
   1884 	int error;
   1885 
   1886 	PUFFS_MSG_ALLOC(vn, rmdir);
   1887 	rmdir_msg->pvnr_cookie_targ = ck;
   1888 	puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
   1889 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1890 	puffs_msg_setinfo(park_rmdir, PUFFSOP_VN, PUFFS_VN_RMDIR, dck);
   1891 
   1892 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_rmdir, error);
   1893 	PUFFS_MSG_RELEASE(rmdir);
   1894 
   1895 	return checkerr(pmp, error, __func__);
   1896 }
   1897 
   1898 int
   1899 puffs_vnop_rmdir(void *v)
   1900 {
   1901 	struct vop_rmdir_args /* {
   1902 		const struct vnodeop_desc *a_desc;
   1903 		struct vnode *a_dvp;
   1904 		struct vnode *a_vp;
   1905 		struct componentname *a_cnp;
   1906 	} */ *ap = v;
   1907 	PUFFS_MSG_VARS(vn, rmdir);
   1908 	struct vnode *dvp = ap->a_dvp;
   1909 	struct vnode *vp = ap->a_vp;
   1910 	struct puffs_node *dpn = VPTOPP(dvp);
   1911 	struct puffs_node *pn = VPTOPP(vp);
   1912 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   1913 	struct componentname *cnp = ap->a_cnp;
   1914 	int error;
   1915 
   1916 	PUFFS_MSG_ALLOC(vn, rmdir);
   1917 	rmdir_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1918 	puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
   1919 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1920 	puffs_msg_setinfo(park_rmdir, PUFFSOP_VN,
   1921 	    PUFFS_VN_RMDIR, VPTOPNC(dvp));
   1922 
   1923 	puffs_msg_enqueue(pmp, park_rmdir);
   1924 	REFPN_AND_UNLOCKVP(dvp, dpn);
   1925 	REFPN_AND_UNLOCKVP(vp, pn);
   1926 	error = puffs_msg_wait2(pmp, park_rmdir, dpn, pn);
   1927 
   1928 	PUFFS_MSG_RELEASE(rmdir);
   1929 
   1930 	/* XXX: some call cache_purge() *for both vnodes* here, investigate */
   1931 	RELEPN_AND_VP(dvp, dpn);
   1932 	RELEPN_AND_VP(vp, pn);
   1933 
   1934 	return error;
   1935 }
   1936 
   1937 int
   1938 puffs_vnop_link(void *v)
   1939 {
   1940 	struct vop_link_args /* {
   1941 		const struct vnodeop_desc *a_desc;
   1942 		struct vnode *a_dvp;
   1943 		struct vnode *a_vp;
   1944 		struct componentname *a_cnp;
   1945 	} */ *ap = v;
   1946 	PUFFS_MSG_VARS(vn, link);
   1947 	struct vnode *dvp = ap->a_dvp;
   1948 	struct vnode *vp = ap->a_vp;
   1949 	struct puffs_node *dpn = VPTOPP(dvp);
   1950 	struct puffs_node *pn = VPTOPP(vp);
   1951 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   1952 	struct componentname *cnp = ap->a_cnp;
   1953 	int error;
   1954 
   1955 	PUFFS_MSG_ALLOC(vn, link);
   1956 	link_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1957 	puffs_makecn(&link_msg->pvnr_cn, &link_msg->pvnr_cn_cred,
   1958 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1959 	puffs_msg_setinfo(park_link, PUFFSOP_VN,
   1960 	    PUFFS_VN_LINK, VPTOPNC(dvp));
   1961 
   1962 	puffs_msg_enqueue(pmp, park_link);
   1963 	REFPN_AND_UNLOCKVP(dvp, dpn);
   1964 	REFPN(pn);
   1965 	error = puffs_msg_wait2(pmp, park_link, dpn, pn);
   1966 
   1967 	PUFFS_MSG_RELEASE(link);
   1968 
   1969 	error = checkerr(pmp, error, __func__);
   1970 
   1971 	/*
   1972 	 * XXX: stay in touch with the cache.  I don't like this, but
   1973 	 * don't have a better solution either.  See also puffs_rename().
   1974 	 */
   1975 	if (error == 0)
   1976 		puffs_updatenode(pn, PUFFS_UPDATECTIME, 0);
   1977 
   1978 	RELEPN_AND_VP(dvp, dpn);
   1979 	puffs_releasenode(pn);
   1980 
   1981 	return error;
   1982 }
   1983 
   1984 int
   1985 puffs_vnop_symlink(void *v)
   1986 {
   1987 	struct vop_symlink_v3_args /* {
   1988 		const struct vnodeop_desc *a_desc;
   1989 		struct vnode *a_dvp;
   1990 		struct vnode **a_vpp;
   1991 		struct componentname *a_cnp;
   1992 		struct vattr *a_vap;
   1993 		char *a_target;
   1994 	} */ *ap = v;
   1995 	PUFFS_MSG_VARS(vn, symlink);
   1996 	struct vnode *dvp = ap->a_dvp;
   1997 	struct puffs_node *dpn = VPTOPP(dvp);
   1998 	struct mount *mp = dvp->v_mount;
   1999 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   2000 	struct componentname *cnp = ap->a_cnp;
   2001 	int error;
   2002 
   2003 	*ap->a_vpp = NULL;
   2004 
   2005 	PUFFS_MSG_ALLOC(vn, symlink);
   2006 	puffs_makecn(&symlink_msg->pvnr_cn, &symlink_msg->pvnr_cn_cred,
   2007 		cnp, PUFFS_USE_FULLPNBUF(pmp));
   2008 	symlink_msg->pvnr_va = *ap->a_vap;
   2009 	(void)strlcpy(symlink_msg->pvnr_link, ap->a_target,
   2010 	    sizeof(symlink_msg->pvnr_link));
   2011 	puffs_msg_setinfo(park_symlink, PUFFSOP_VN,
   2012 	    PUFFS_VN_SYMLINK, VPTOPNC(dvp));
   2013 
   2014 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_symlink, dvp->v_data, NULL, error);
   2015 
   2016 	error = checkerr(pmp, error, __func__);
   2017 	if (error)
   2018 		goto out;
   2019 
   2020 	error = puffs_newnode(mp, dvp, ap->a_vpp,
   2021 	    symlink_msg->pvnr_newnode, cnp, VLNK, 0);
   2022 	if (error) {
   2023 		puffs_abortbutton(pmp, PUFFS_ABORT_SYMLINK, dpn->pn_cookie,
   2024 		    symlink_msg->pvnr_newnode, cnp);
   2025 		goto out;
   2026 	}
   2027 
   2028 	if (PUFFS_USE_FS_TTL(pmp)) {
   2029 		struct timespec *va_ttl = &symlink_msg->pvnr_va_ttl;
   2030 		struct timespec *cn_ttl = &symlink_msg->pvnr_cn_ttl;
   2031 		struct vattr *rvap = &symlink_msg->pvnr_va;
   2032 
   2033 		update_va(*ap->a_vpp, NULL, rvap,
   2034 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
   2035 	}
   2036 
   2037 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
   2038 
   2039 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   2040 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
   2041 		update_parent(*ap->a_vpp, dvp);
   2042 
   2043  out:
   2044 	PUFFS_MSG_RELEASE(symlink);
   2045 
   2046 	return error;
   2047 }
   2048 
   2049 int
   2050 puffs_vnop_readlink(void *v)
   2051 {
   2052 	struct vop_readlink_args /* {
   2053 		const struct vnodeop_desc *a_desc;
   2054 		struct vnode *a_vp;
   2055 		struct uio *a_uio;
   2056 		kauth_cred_t a_cred;
   2057 	} */ *ap = v;
   2058 	PUFFS_MSG_VARS(vn, readlink);
   2059 	struct vnode *vp = ap->a_vp;
   2060 	struct puffs_mount *pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2061 	size_t linklen;
   2062 	int error;
   2063 
   2064 	PUFFS_MSG_ALLOC(vn, readlink);
   2065 	puffs_credcvt(&readlink_msg->pvnr_cred, ap->a_cred);
   2066 	linklen = sizeof(readlink_msg->pvnr_link);
   2067 	readlink_msg->pvnr_linklen = linklen;
   2068 	puffs_msg_setinfo(park_readlink, PUFFSOP_VN,
   2069 	    PUFFS_VN_READLINK, VPTOPNC(vp));
   2070 
   2071 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_readlink, vp->v_data, NULL, error);
   2072 	error = checkerr(pmp, error, __func__);
   2073 	if (error)
   2074 		goto out;
   2075 
   2076 	/* bad bad user file server */
   2077 	if (readlink_msg->pvnr_linklen > linklen) {
   2078 		puffs_senderr(pmp, PUFFS_ERR_READLINK, E2BIG,
   2079 		    "linklen too big", VPTOPNC(ap->a_vp));
   2080 		error = EPROTO;
   2081 		goto out;
   2082 	}
   2083 
   2084 	error = uiomove(&readlink_msg->pvnr_link, readlink_msg->pvnr_linklen,
   2085 	    ap->a_uio);
   2086  out:
   2087 	PUFFS_MSG_RELEASE(readlink);
   2088 	return error;
   2089 }
   2090 
   2091 int
   2092 puffs_vnop_rename(void *v)
   2093 {
   2094 	struct vop_rename_args /* {
   2095 		const struct vnodeop_desc *a_desc;
   2096 		struct vnode *a_fdvp;
   2097 		struct vnode *a_fvp;
   2098 		struct componentname *a_fcnp;
   2099 		struct vnode *a_tdvp;
   2100 		struct vnode *a_tvp;
   2101 		struct componentname *a_tcnp;
   2102 	} */ *ap = v;
   2103 	PUFFS_MSG_VARS(vn, rename);
   2104 	struct vnode *fdvp = ap->a_fdvp, *fvp = ap->a_fvp;
   2105 	struct vnode *tdvp = ap->a_tdvp, *tvp = ap->a_tvp;
   2106 	struct puffs_node *fpn = ap->a_fvp->v_data;
   2107 	struct puffs_mount *pmp = MPTOPUFFSMP(fdvp->v_mount);
   2108 	int error;
   2109 	bool doabort = true;
   2110 
   2111 	if ((fvp->v_mount != tdvp->v_mount) ||
   2112 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   2113 		ERROUT(EXDEV);
   2114 	}
   2115 
   2116 	PUFFS_MSG_ALLOC(vn, rename);
   2117 	rename_msg->pvnr_cookie_src = VPTOPNC(fvp);
   2118 	rename_msg->pvnr_cookie_targdir = VPTOPNC(tdvp);
   2119 	if (tvp)
   2120 		rename_msg->pvnr_cookie_targ = VPTOPNC(tvp);
   2121 	else
   2122 		rename_msg->pvnr_cookie_targ = NULL;
   2123 	puffs_makecn(&rename_msg->pvnr_cn_src, &rename_msg->pvnr_cn_src_cred,
   2124 	    ap->a_fcnp, PUFFS_USE_FULLPNBUF(pmp));
   2125 	puffs_makecn(&rename_msg->pvnr_cn_targ, &rename_msg->pvnr_cn_targ_cred,
   2126 	    ap->a_tcnp, PUFFS_USE_FULLPNBUF(pmp));
   2127 	puffs_msg_setinfo(park_rename, PUFFSOP_VN,
   2128 	    PUFFS_VN_RENAME, VPTOPNC(fdvp));
   2129 
   2130 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rename, fdvp->v_data, NULL, error);
   2131 	doabort = false;
   2132 	PUFFS_MSG_RELEASE(rename);
   2133 	error = checkerr(pmp, error, __func__);
   2134 
   2135 	/*
   2136 	 * XXX: stay in touch with the cache.  I don't like this, but
   2137 	 * don't have a better solution either.  See also puffs_link().
   2138 	 */
   2139 	if (error == 0) {
   2140 		puffs_updatenode(fpn, PUFFS_UPDATECTIME, 0);
   2141 
   2142 		if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   2143 		    (VPTOPP(fvp)->pn_parent != tdvp))
   2144 			update_parent(fvp, tdvp);
   2145 	}
   2146 
   2147 
   2148  out:
   2149 	if (doabort)
   2150 		VOP_ABORTOP(tdvp, ap->a_tcnp);
   2151 	if (tvp != NULL)
   2152 		vput(tvp);
   2153 	if (tdvp == tvp)
   2154 		vrele(tdvp);
   2155 	else
   2156 		vput(tdvp);
   2157 
   2158 	if (doabort)
   2159 		VOP_ABORTOP(fdvp, ap->a_fcnp);
   2160 	vrele(fdvp);
   2161 	vrele(fvp);
   2162 
   2163 	return error;
   2164 }
   2165 
   2166 #define RWARGS(cont, iofl, move, offset, creds)				\
   2167 	(cont)->pvnr_ioflag = (iofl);					\
   2168 	(cont)->pvnr_resid = (move);					\
   2169 	(cont)->pvnr_offset = (offset);					\
   2170 	puffs_credcvt(&(cont)->pvnr_cred, creds)
   2171 
   2172 int
   2173 puffs_vnop_read(void *v)
   2174 {
   2175 	struct vop_read_args /* {
   2176 		const struct vnodeop_desc *a_desc;
   2177 		struct vnode *a_vp;
   2178 		struct uio *a_uio;
   2179 		int a_ioflag;
   2180 		kauth_cred_t a_cred;
   2181 	} */ *ap = v;
   2182 	PUFFS_MSG_VARS(vn, read);
   2183 	struct vnode *vp = ap->a_vp;
   2184 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2185 	struct uio *uio = ap->a_uio;
   2186 	size_t tomove, argsize;
   2187 	vsize_t bytelen;
   2188 	int error;
   2189 
   2190 	read_msg = NULL;
   2191 	error = 0;
   2192 
   2193 	/* std sanity */
   2194 	if (uio->uio_resid == 0)
   2195 		return 0;
   2196 	if (uio->uio_offset < 0)
   2197 		return EINVAL;
   2198 
   2199 	if (vp->v_type == VREG && PUFFS_USE_PAGECACHE(pmp)) {
   2200 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
   2201 
   2202 		while (uio->uio_resid > 0) {
   2203 			if (vp->v_size <= uio->uio_offset) {
   2204 				break;
   2205 			}
   2206 			bytelen = MIN(uio->uio_resid,
   2207 			    vp->v_size - uio->uio_offset);
   2208 			if (bytelen == 0)
   2209 				break;
   2210 
   2211 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
   2212 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
   2213 			if (error)
   2214 				break;
   2215 		}
   2216 
   2217 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   2218 			puffs_updatenode(VPTOPP(vp), PUFFS_UPDATEATIME, 0);
   2219 	} else {
   2220 		/*
   2221 		 * in case it's not a regular file or we're operating
   2222 		 * uncached, do read in the old-fashioned style,
   2223 		 * i.e. explicit read operations
   2224 		 */
   2225 
   2226 		tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2227 		argsize = sizeof(struct puffs_vnmsg_read);
   2228 		puffs_msgmem_alloc(argsize + tomove, &park_read,
   2229 		    (void *)&read_msg, 1);
   2230 
   2231 		error = 0;
   2232 		while (uio->uio_resid > 0) {
   2233 			tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2234 			memset(read_msg, 0, argsize); /* XXX: touser KASSERT */
   2235 			RWARGS(read_msg, ap->a_ioflag, tomove,
   2236 			    uio->uio_offset, ap->a_cred);
   2237 			puffs_msg_setinfo(park_read, PUFFSOP_VN,
   2238 			    PUFFS_VN_READ, VPTOPNC(vp));
   2239 			puffs_msg_setdelta(park_read, tomove);
   2240 
   2241 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_read, vp->v_data,
   2242 			    NULL, error);
   2243 			error = checkerr(pmp, error, __func__);
   2244 			if (error)
   2245 				break;
   2246 
   2247 			if (read_msg->pvnr_resid > tomove) {
   2248 				puffs_senderr(pmp, PUFFS_ERR_READ,
   2249 				    E2BIG, "resid grew", VPTOPNC(ap->a_vp));
   2250 				error = EPROTO;
   2251 				break;
   2252 			}
   2253 
   2254 			error = uiomove(read_msg->pvnr_data,
   2255 			    tomove - read_msg->pvnr_resid, uio);
   2256 
   2257 			/*
   2258 			 * in case the file is out of juice, resid from
   2259 			 * userspace is != 0.  and the error-case is
   2260 			 * quite obvious
   2261 			 */
   2262 			if (error || read_msg->pvnr_resid)
   2263 				break;
   2264 		}
   2265 
   2266 		puffs_msgmem_release(park_read);
   2267 	}
   2268 
   2269 	return error;
   2270 }
   2271 
   2272 /*
   2273  * XXX: in case of a failure, this leaves uio in a bad state.
   2274  * We could theoretically copy the uio and iovecs and "replay"
   2275  * them the right amount after the userspace trip, but don't
   2276  * bother for now.
   2277  */
   2278 int
   2279 puffs_vnop_write(void *v)
   2280 {
   2281 	struct vop_write_args /* {
   2282 		const struct vnodeop_desc *a_desc;
   2283 		struct vnode *a_vp;
   2284 		struct uio *a_uio;
   2285 		int a_ioflag;
   2286 		kauth_cred_t a_cred;
   2287 	} */ *ap = v;
   2288 	PUFFS_MSG_VARS(vn, write);
   2289 	struct vnode *vp = ap->a_vp;
   2290 	struct puffs_node *pn = VPTOPP(vp);
   2291 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2292 	struct uio *uio = ap->a_uio;
   2293 	size_t tomove, argsize;
   2294 	off_t oldoff, newoff, origoff;
   2295 	vsize_t bytelen;
   2296 	int error, uflags;
   2297 	int ubcflags;
   2298 
   2299 	error = uflags = 0;
   2300 	write_msg = NULL;
   2301 
   2302 	mutex_enter(&pn->pn_sizemtx);
   2303 
   2304 	if (vp->v_type == VREG && PUFFS_USE_PAGECACHE(pmp)) {
   2305 		ubcflags = UBC_WRITE | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp);
   2306 
   2307 		/*
   2308 		 * userspace *should* be allowed to control this,
   2309 		 * but with UBC it's a bit unclear how to handle it
   2310 		 */
   2311 		if (ap->a_ioflag & IO_APPEND)
   2312 			uio->uio_offset = vp->v_size;
   2313 
   2314 		origoff = uio->uio_offset;
   2315 		while (uio->uio_resid > 0) {
   2316 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
   2317 				uflags |= PUFFS_UPDATEATIME;
   2318 			uflags |= PUFFS_UPDATECTIME;
   2319 			uflags |= PUFFS_UPDATEMTIME;
   2320 			oldoff = uio->uio_offset;
   2321 			bytelen = uio->uio_resid;
   2322 
   2323 			newoff = oldoff + bytelen;
   2324 			if (vp->v_size < newoff) {
   2325 				uvm_vnp_setwritesize(vp, newoff);
   2326 			}
   2327 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
   2328 			    UVM_ADV_RANDOM, ubcflags);
   2329 
   2330 			/*
   2331 			 * In case of a ubc_uiomove() error,
   2332 			 * opt to not extend the file at all and
   2333 			 * return an error.  Otherwise, if we attempt
   2334 			 * to clear the memory we couldn't fault to,
   2335 			 * we might generate a kernel page fault.
   2336 			 */
   2337 			if (vp->v_size < newoff) {
   2338 				if (error == 0) {
   2339 					uflags |= PUFFS_UPDATESIZE;
   2340 					uvm_vnp_setsize(vp, newoff);
   2341 				} else {
   2342 					uvm_vnp_setwritesize(vp, vp->v_size);
   2343 				}
   2344 			}
   2345 			if (error)
   2346 				break;
   2347 
   2348 			/*
   2349 			 * If we're writing large files, flush to file server
   2350 			 * every 64k.  Otherwise we can very easily exhaust
   2351 			 * kernel and user memory, as the file server cannot
   2352 			 * really keep up with our writing speed.
   2353 			 *
   2354 			 * Note: this does *NOT* honor MNT_ASYNC, because
   2355 			 * that gives userland too much say in the kernel.
   2356 			 */
   2357 			if (oldoff >> 16 != uio->uio_offset >> 16) {
   2358 				mutex_enter(vp->v_interlock);
   2359 				error = VOP_PUTPAGES(vp, oldoff & ~0xffff,
   2360 				    uio->uio_offset & ~0xffff,
   2361 				    PGO_CLEANIT | PGO_SYNCIO);
   2362 				if (error)
   2363 					break;
   2364 			}
   2365 		}
   2366 
   2367 		/* synchronous I/O? */
   2368 		if (error == 0 && ap->a_ioflag & IO_SYNC) {
   2369 			mutex_enter(vp->v_interlock);
   2370 			error = VOP_PUTPAGES(vp, trunc_page(origoff),
   2371 			    round_page(uio->uio_offset),
   2372 			    PGO_CLEANIT | PGO_SYNCIO);
   2373 
   2374 		/* write through page cache? */
   2375 		} else if (error == 0 && pmp->pmp_flags & PUFFS_KFLAG_WTCACHE) {
   2376 			mutex_enter(vp->v_interlock);
   2377 			error = VOP_PUTPAGES(vp, trunc_page(origoff),
   2378 			    round_page(uio->uio_offset), PGO_CLEANIT);
   2379 		}
   2380 
   2381 		puffs_updatenode(VPTOPP(vp), uflags, vp->v_size);
   2382 	} else {
   2383 		/* tomove is non-increasing */
   2384 		tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2385 		argsize = sizeof(struct puffs_vnmsg_write) + tomove;
   2386 		puffs_msgmem_alloc(argsize, &park_write, (void *)&write_msg,1);
   2387 
   2388 		while (uio->uio_resid > 0) {
   2389 			/* move data to buffer */
   2390 			tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2391 			memset(write_msg, 0, argsize); /* XXX: touser KASSERT */
   2392 			RWARGS(write_msg, ap->a_ioflag, tomove,
   2393 			    uio->uio_offset, ap->a_cred);
   2394 			error = uiomove(write_msg->pvnr_data, tomove, uio);
   2395 			if (error)
   2396 				break;
   2397 
   2398 			/* move buffer to userspace */
   2399 			puffs_msg_setinfo(park_write, PUFFSOP_VN,
   2400 			    PUFFS_VN_WRITE, VPTOPNC(vp));
   2401 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_write, vp->v_data,
   2402 			    NULL, error);
   2403 			error = checkerr(pmp, error, __func__);
   2404 			if (error)
   2405 				break;
   2406 
   2407 			if (write_msg->pvnr_resid > tomove) {
   2408 				puffs_senderr(pmp, PUFFS_ERR_WRITE,
   2409 				    E2BIG, "resid grew", VPTOPNC(ap->a_vp));
   2410 				error = EPROTO;
   2411 				break;
   2412 			}
   2413 
   2414 			/* adjust file size */
   2415 			if (vp->v_size < uio->uio_offset)
   2416 				uvm_vnp_setsize(vp, uio->uio_offset);
   2417 
   2418 			/* didn't move everything?  bad userspace.  bail */
   2419 			if (write_msg->pvnr_resid != 0) {
   2420 				error = EIO;
   2421 				break;
   2422 			}
   2423 		}
   2424 		puffs_msgmem_release(park_write);
   2425 	}
   2426 
   2427 	mutex_exit(&pn->pn_sizemtx);
   2428 	return error;
   2429 }
   2430 
   2431 int
   2432 puffs_vnop_print(void *v)
   2433 {
   2434 	struct vop_print_args /* {
   2435 		struct vnode *a_vp;
   2436 	} */ *ap = v;
   2437 	PUFFS_MSG_VARS(vn, print);
   2438 	struct vnode *vp = ap->a_vp;
   2439 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2440 	struct puffs_node *pn = vp->v_data;
   2441 
   2442 	/* kernel portion */
   2443 	printf("tag VT_PUFFS, vnode %p, puffs node: %p,\n"
   2444 	    "\tuserspace cookie: %p", vp, pn, pn->pn_cookie);
   2445 	if (vp->v_type == VFIFO)
   2446 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   2447 	printf("\n");
   2448 
   2449 	/* userspace portion */
   2450 	if (EXISTSOP(pmp, PRINT)) {
   2451 		PUFFS_MSG_ALLOC(vn, print);
   2452 		puffs_msg_setinfo(park_print, PUFFSOP_VN,
   2453 		    PUFFS_VN_PRINT, VPTOPNC(vp));
   2454 		PUFFS_MSG_ENQUEUEWAIT2_NOERROR(pmp, park_print, vp->v_data,
   2455 		    NULL);
   2456 		PUFFS_MSG_RELEASE(print);
   2457 	}
   2458 
   2459 	return 0;
   2460 }
   2461 
   2462 int
   2463 puffs_vnop_pathconf(void *v)
   2464 {
   2465 	struct vop_pathconf_args /* {
   2466 		const struct vnodeop_desc *a_desc;
   2467 		struct vnode *a_vp;
   2468 		int a_name;
   2469 		register_t *a_retval;
   2470 	} */ *ap = v;
   2471 	PUFFS_MSG_VARS(vn, pathconf);
   2472 	struct vnode *vp = ap->a_vp;
   2473 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2474 	int error;
   2475 
   2476 	PUFFS_MSG_ALLOC(vn, pathconf);
   2477 	pathconf_msg->pvnr_name = ap->a_name;
   2478 	puffs_msg_setinfo(park_pathconf, PUFFSOP_VN,
   2479 	    PUFFS_VN_PATHCONF, VPTOPNC(vp));
   2480 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_pathconf, vp->v_data, NULL, error);
   2481 	error = checkerr(pmp, error, __func__);
   2482 	if (!error)
   2483 		*ap->a_retval = pathconf_msg->pvnr_retval;
   2484 	PUFFS_MSG_RELEASE(pathconf);
   2485 
   2486 	return error;
   2487 }
   2488 
   2489 int
   2490 puffs_vnop_advlock(void *v)
   2491 {
   2492 	struct vop_advlock_args /* {
   2493 		const struct vnodeop_desc *a_desc;
   2494 		struct vnode *a_vp;
   2495 		void *a_id;
   2496 		int a_op;
   2497 		struct flock *a_fl;
   2498 		int a_flags;
   2499 	} */ *ap = v;
   2500 	PUFFS_MSG_VARS(vn, advlock);
   2501 	struct vnode *vp = ap->a_vp;
   2502 	struct puffs_node *pn = VPTOPP(vp);
   2503 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2504 	int error;
   2505 
   2506 	if (!EXISTSOP(pmp, ADVLOCK))
   2507 		return lf_advlock(ap, &pn->pn_lockf, vp->v_size);
   2508 
   2509 	PUFFS_MSG_ALLOC(vn, advlock);
   2510 	(void)memcpy(&advlock_msg->pvnr_fl, ap->a_fl,
   2511 		     sizeof(advlock_msg->pvnr_fl));
   2512 	advlock_msg->pvnr_id = ap->a_id;
   2513 	advlock_msg->pvnr_op = ap->a_op;
   2514 	advlock_msg->pvnr_flags = ap->a_flags;
   2515 	puffs_msg_setinfo(park_advlock, PUFFSOP_VN,
   2516 	    PUFFS_VN_ADVLOCK, VPTOPNC(vp));
   2517 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_advlock, vp->v_data, NULL, error);
   2518 	error = checkerr(pmp, error, __func__);
   2519 	PUFFS_MSG_RELEASE(advlock);
   2520 
   2521 	return error;
   2522 }
   2523 
   2524 int
   2525 puffs_vnop_abortop(void *v)
   2526 {
   2527 	struct vop_abortop_args /* {
   2528 		struct vnode *a_dvp;
   2529 		struct componentname *a_cnp;
   2530 	}; */ *ap = v;
   2531 	PUFFS_MSG_VARS(vn, abortop);
   2532 	struct vnode *dvp = ap->a_dvp;
   2533 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   2534 	struct componentname *cnp = ap->a_cnp;
   2535 
   2536 	if (EXISTSOP(pmp, ABORTOP)) {
   2537 		PUFFS_MSG_ALLOC(vn, abortop);
   2538 		puffs_makecn(&abortop_msg->pvnr_cn, &abortop_msg->pvnr_cn_cred,
   2539 		    cnp, PUFFS_USE_FULLPNBUF(pmp));
   2540 		puffs_msg_setfaf(park_abortop);
   2541 		puffs_msg_setinfo(park_abortop, PUFFSOP_VN,
   2542 		    PUFFS_VN_ABORTOP, VPTOPNC(dvp));
   2543 
   2544 		puffs_msg_enqueue(pmp, park_abortop);
   2545 		PUFFS_MSG_RELEASE(abortop);
   2546 	}
   2547 
   2548 	return genfs_abortop(v);
   2549 }
   2550 
   2551 #define BIOASYNC(bp) (bp->b_flags & B_ASYNC)
   2552 
   2553 /*
   2554  * This maps itself to PUFFS_VN_READ/WRITE for data transfer.
   2555  */
   2556 int
   2557 puffs_vnop_strategy(void *v)
   2558 {
   2559 	struct vop_strategy_args /* {
   2560 		const struct vnodeop_desc *a_desc;
   2561 		struct vnode *a_vp;
   2562 		struct buf *a_bp;
   2563 	} */ *ap = v;
   2564 	PUFFS_MSG_VARS(vn, rw);
   2565 	struct vnode *vp = ap->a_vp;
   2566 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2567 	struct puffs_node *pn;
   2568 	struct buf *bp;
   2569 	size_t argsize;
   2570 	size_t tomove, moved;
   2571 	int error, dofaf, cansleep, dobiodone;
   2572 
   2573 	pmp = MPTOPUFFSMP(vp->v_mount);
   2574 	bp = ap->a_bp;
   2575 	error = 0;
   2576 	dofaf = 0;
   2577 	cansleep = 0;
   2578 	pn = VPTOPP(vp);
   2579 	park_rw = NULL; /* explicit */
   2580 	dobiodone = 1;
   2581 
   2582 	if ((BUF_ISREAD(bp) && !EXISTSOP(pmp, READ))
   2583 	    || (BUF_ISWRITE(bp) && !EXISTSOP(pmp, WRITE)))
   2584 		ERROUT(EOPNOTSUPP);
   2585 
   2586 	/*
   2587 	 * Short-circuit optimization: don't flush buffer in between
   2588 	 * VOP_INACTIVE and VOP_RECLAIM in case the node has no references.
   2589 	 */
   2590 	if (pn->pn_stat & PNODE_DYING) {
   2591 		KASSERT(BUF_ISWRITE(bp));
   2592 		bp->b_resid = 0;
   2593 		goto out;
   2594 	}
   2595 
   2596 #ifdef DIAGNOSTIC
   2597 	if (bp->b_bcount > pmp->pmp_msg_maxsize - PUFFS_MSGSTRUCT_MAX)
   2598 		panic("puffs_strategy: wildly inappropriate buf bcount %d",
   2599 		    bp->b_bcount);
   2600 #endif
   2601 
   2602 	/*
   2603 	 * See explanation for the necessity of a FAF in puffs_fsync.
   2604 	 *
   2605 	 * Also, do FAF in case we're suspending.
   2606 	 * See puffs_vfsops.c:pageflush()
   2607 	 */
   2608 	if (BUF_ISWRITE(bp)) {
   2609 		mutex_enter(vp->v_interlock);
   2610 		if (vdead_check(vp, VDEAD_NOWAIT) != 0)
   2611 			dofaf = 1;
   2612 		if (pn->pn_stat & PNODE_FAF)
   2613 			dofaf = 1;
   2614 		mutex_exit(vp->v_interlock);
   2615 	}
   2616 
   2617 	cansleep = (curlwp == uvm.pagedaemon_lwp || dofaf) ? 0 : 1;
   2618 
   2619 	KASSERT(curlwp != uvm.pagedaemon_lwp || dofaf || BIOASYNC(bp));
   2620 
   2621 	/* allocate transport structure */
   2622 	tomove = PUFFS_TOMOVE(bp->b_bcount, pmp);
   2623 	argsize = sizeof(struct puffs_vnmsg_rw);
   2624 	error = puffs_msgmem_alloc(argsize + tomove, &park_rw,
   2625 	    (void *)&rw_msg, cansleep);
   2626 	if (error)
   2627 		goto out;
   2628 	RWARGS(rw_msg, 0, tomove, bp->b_blkno << DEV_BSHIFT, FSCRED);
   2629 
   2630 	/* 2x2 cases: read/write, faf/nofaf */
   2631 	if (BUF_ISREAD(bp)) {
   2632 		puffs_msg_setinfo(park_rw, PUFFSOP_VN,
   2633 		    PUFFS_VN_READ, VPTOPNC(vp));
   2634 		puffs_msg_setdelta(park_rw, tomove);
   2635 		if (BIOASYNC(bp)) {
   2636 			puffs_msg_setcall(park_rw,
   2637 			    puffs_parkdone_asyncbioread, bp);
   2638 			puffs_msg_enqueue(pmp, park_rw);
   2639 			dobiodone = 0;
   2640 		} else {
   2641 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data,
   2642 			    NULL, error);
   2643 			error = checkerr(pmp, error, __func__);
   2644 			if (error)
   2645 				goto out;
   2646 
   2647 			if (rw_msg->pvnr_resid > tomove) {
   2648 				puffs_senderr(pmp, PUFFS_ERR_READ,
   2649 				    E2BIG, "resid grew", VPTOPNC(vp));
   2650 				ERROUT(EPROTO);
   2651 			}
   2652 
   2653 			moved = tomove - rw_msg->pvnr_resid;
   2654 
   2655 			(void)memcpy(bp->b_data, rw_msg->pvnr_data, moved);
   2656 			bp->b_resid = bp->b_bcount - moved;
   2657 		}
   2658 	} else {
   2659 		puffs_msg_setinfo(park_rw, PUFFSOP_VN,
   2660 		    PUFFS_VN_WRITE, VPTOPNC(vp));
   2661 		/*
   2662 		 * make pages read-only before we write them if we want
   2663 		 * write caching info
   2664 		 */
   2665 		if (PUFFS_WCACHEINFO(pmp)) {
   2666 			struct uvm_object *uobj = &vp->v_uobj;
   2667 			int npages = (bp->b_bcount + PAGE_SIZE-1) >> PAGE_SHIFT;
   2668 			struct vm_page *vmp;
   2669 			int i;
   2670 
   2671 			for (i = 0; i < npages; i++) {
   2672 				vmp= uvm_pageratop((vaddr_t)bp->b_data
   2673 				    + (i << PAGE_SHIFT));
   2674 				DPRINTF(("puffs_strategy: write-protecting "
   2675 				    "vp %p page %p, offset %" PRId64"\n",
   2676 				    vp, vmp, vmp->offset));
   2677 				mutex_enter(uobj->vmobjlock);
   2678 				vmp->flags |= PG_RDONLY;
   2679 				pmap_page_protect(vmp, VM_PROT_READ);
   2680 				mutex_exit(uobj->vmobjlock);
   2681 			}
   2682 		}
   2683 
   2684 		(void)memcpy(&rw_msg->pvnr_data, bp->b_data, tomove);
   2685 		if (dofaf) {
   2686 			puffs_msg_setfaf(park_rw);
   2687 		} else if (BIOASYNC(bp)) {
   2688 			puffs_msg_setcall(park_rw,
   2689 			    puffs_parkdone_asyncbiowrite, bp);
   2690 			dobiodone = 0;
   2691 		}
   2692 
   2693 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data, NULL, error);
   2694 
   2695 		if (dobiodone == 0)
   2696 			goto out;
   2697 
   2698 		/*
   2699 		 * XXXXXXXX: wrong, but kernel can't survive strategy
   2700 		 * failure currently.  Here, have one more X: X.
   2701 		 */
   2702 		if (error != ENOMEM)
   2703 			error = 0;
   2704 
   2705 		error = checkerr(pmp, error, __func__);
   2706 		if (error)
   2707 			goto out;
   2708 
   2709 		if (rw_msg->pvnr_resid > tomove) {
   2710 			puffs_senderr(pmp, PUFFS_ERR_WRITE,
   2711 			    E2BIG, "resid grew", VPTOPNC(vp));
   2712 			ERROUT(EPROTO);
   2713 		}
   2714 
   2715 		/*
   2716 		 * FAF moved everything.  Frankly, we don't
   2717 		 * really have a choice.
   2718 		 */
   2719 		if (dofaf && error == 0)
   2720 			moved = tomove;
   2721 		else
   2722 			moved = tomove - rw_msg->pvnr_resid;
   2723 
   2724 		bp->b_resid = bp->b_bcount - moved;
   2725 		if (bp->b_resid != 0) {
   2726 			ERROUT(EIO);
   2727 		}
   2728 	}
   2729 
   2730  out:
   2731 	if (park_rw)
   2732 		puffs_msgmem_release(park_rw);
   2733 
   2734 	if (error)
   2735 		bp->b_error = error;
   2736 
   2737 	if (error || dobiodone)
   2738 		biodone(bp);
   2739 
   2740 	return error;
   2741 }
   2742 
   2743 int
   2744 puffs_vnop_mmap(void *v)
   2745 {
   2746 	struct vop_mmap_args /* {
   2747 		const struct vnodeop_desc *a_desc;
   2748 		struct vnode *a_vp;
   2749 		vm_prot_t a_prot;
   2750 		kauth_cred_t a_cred;
   2751 	} */ *ap = v;
   2752 	PUFFS_MSG_VARS(vn, mmap);
   2753 	struct vnode *vp = ap->a_vp;
   2754 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2755 	int error;
   2756 
   2757 	if (!PUFFS_USE_PAGECACHE(pmp))
   2758 		return genfs_eopnotsupp(v);
   2759 
   2760 	if (EXISTSOP(pmp, MMAP)) {
   2761 		PUFFS_MSG_ALLOC(vn, mmap);
   2762 		mmap_msg->pvnr_prot = ap->a_prot;
   2763 		puffs_credcvt(&mmap_msg->pvnr_cred, ap->a_cred);
   2764 		puffs_msg_setinfo(park_mmap, PUFFSOP_VN,
   2765 		    PUFFS_VN_MMAP, VPTOPNC(vp));
   2766 
   2767 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mmap, vp->v_data, NULL, error);
   2768 		error = checkerr(pmp, error, __func__);
   2769 		PUFFS_MSG_RELEASE(mmap);
   2770 	} else {
   2771 		error = genfs_mmap(v);
   2772 	}
   2773 
   2774 	return error;
   2775 }
   2776 
   2777 
   2778 /*
   2779  * The rest don't get a free trip to userspace and back, they
   2780  * have to stay within the kernel.
   2781  */
   2782 
   2783 /*
   2784  * bmap doesn't really make any sense for puffs, so just 1:1 map it.
   2785  * well, maybe somehow, somewhere, some day ....
   2786  */
   2787 int
   2788 puffs_vnop_bmap(void *v)
   2789 {
   2790 	struct vop_bmap_args /* {
   2791 		const struct vnodeop_desc *a_desc;
   2792 		struct vnode *a_vp;
   2793 		daddr_t a_bn;
   2794 		struct vnode **a_vpp;
   2795 		daddr_t *a_bnp;
   2796 		int *a_runp;
   2797 	} */ *ap = v;
   2798 	struct puffs_mount *pmp;
   2799 
   2800 	pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2801 
   2802 	if (ap->a_vpp)
   2803 		*ap->a_vpp = ap->a_vp;
   2804 	if (ap->a_bnp)
   2805 		*ap->a_bnp = ap->a_bn;
   2806 	if (ap->a_runp)
   2807 		*ap->a_runp
   2808 		    = (PUFFS_TOMOVE(pmp->pmp_msg_maxsize, pmp)>>DEV_BSHIFT) - 1;
   2809 
   2810 	return 0;
   2811 }
   2812 
   2813 /*
   2814  * Handle getpages faults in puffs.  We let genfs_getpages() do most
   2815  * of the dirty work, but we come in this route to do accounting tasks.
   2816  * If the user server has specified functions for cache notifications
   2817  * about reads and/or writes, we record which type of operation we got,
   2818  * for which page range, and proceed to issue a FAF notification to the
   2819  * server about it.
   2820  */
   2821 int
   2822 puffs_vnop_getpages(void *v)
   2823 {
   2824 	struct vop_getpages_args /* {
   2825 		const struct vnodeop_desc *a_desc;
   2826 		struct vnode *a_vp;
   2827 		voff_t a_offset;
   2828 		struct vm_page **a_m;
   2829 		int *a_count;
   2830 		int a_centeridx;
   2831 		vm_prot_t a_access_type;
   2832 		int a_advice;
   2833 		int a_flags;
   2834 	} */ *ap = v;
   2835 	struct puffs_mount *pmp;
   2836 	struct puffs_node *pn;
   2837 	struct vnode *vp;
   2838 	struct vm_page **pgs;
   2839 	struct puffs_cacheinfo *pcinfo = NULL;
   2840 	struct puffs_cacherun *pcrun;
   2841 	void *parkmem = NULL;
   2842 	size_t runsizes;
   2843 	int i, npages, si, streakon;
   2844 	int error, locked, write;
   2845 
   2846 	pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2847 	npages = *ap->a_count;
   2848 	pgs = ap->a_m;
   2849 	vp = ap->a_vp;
   2850 	pn = vp->v_data;
   2851 	locked = (ap->a_flags & PGO_LOCKED) != 0;
   2852 	write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   2853 
   2854 	/* ccg xnaht - gets Wuninitialized wrong */
   2855 	pcrun = NULL;
   2856 	runsizes = 0;
   2857 
   2858 	/*
   2859 	 * Check that we aren't trying to fault in pages which our file
   2860 	 * server doesn't know about.  This happens if we extend a file by
   2861 	 * skipping some pages and later try to fault in pages which
   2862 	 * are between pn_serversize and vp_size.  This check optimizes
   2863 	 * away the common case where a file is being extended.
   2864 	 */
   2865 	if (ap->a_offset >= pn->pn_serversize && ap->a_offset < vp->v_size) {
   2866 		struct vattr va;
   2867 
   2868 		/* try again later when we can block */
   2869 		if (locked)
   2870 			ERROUT(EBUSY);
   2871 
   2872 		mutex_exit(vp->v_interlock);
   2873 		vattr_null(&va);
   2874 		va.va_size = vp->v_size;
   2875 		error = dosetattr(vp, &va, FSCRED, 0);
   2876 		if (error)
   2877 			ERROUT(error);
   2878 		mutex_enter(vp->v_interlock);
   2879 	}
   2880 
   2881 	if (write && PUFFS_WCACHEINFO(pmp)) {
   2882 #ifdef notnowjohn
   2883 		/* allocate worst-case memory */
   2884 		runsizes = ((npages / 2) + 1) * sizeof(struct puffs_cacherun);
   2885 		KASSERT(curlwp != uvm.pagedaemon_lwp || locked);
   2886 		pcinfo = kmem_zalloc(sizeof(struct puffs_cacheinfo) + runsize,
   2887 		    locked ? KM_NOSLEEP : KM_SLEEP);
   2888 
   2889 		/*
   2890 		 * can't block if we're locked and can't mess up caching
   2891 		 * information for fs server.  so come back later, please
   2892 		 */
   2893 		if (pcinfo == NULL)
   2894 			ERROUT(ENOMEM);
   2895 
   2896 		parkmem = puffs_park_alloc(locked == 0);
   2897 		if (parkmem == NULL)
   2898 			ERROUT(ENOMEM);
   2899 
   2900 		pcrun = pcinfo->pcache_runs;
   2901 #else
   2902 		(void)parkmem;
   2903 #endif
   2904 	}
   2905 
   2906 	error = genfs_getpages(v);
   2907 	if (error)
   2908 		goto out;
   2909 
   2910 	if (PUFFS_WCACHEINFO(pmp) == 0)
   2911 		goto out;
   2912 
   2913 	/*
   2914 	 * Let's see whose fault it was and inform the user server of
   2915 	 * possibly read/written pages.  Map pages from read faults
   2916 	 * strictly read-only, since otherwise we might miss info on
   2917 	 * when the page is actually write-faulted to.
   2918 	 */
   2919 	if (!locked)
   2920 		mutex_enter(vp->v_uobj.vmobjlock);
   2921 	for (i = 0, si = 0, streakon = 0; i < npages; i++) {
   2922 		if (pgs[i] == NULL || pgs[i] == PGO_DONTCARE) {
   2923 			if (streakon && write) {
   2924 				streakon = 0;
   2925 				pcrun[si].pcache_runend
   2926 				    = trunc_page(pgs[i]->offset) + PAGE_MASK;
   2927 				si++;
   2928 			}
   2929 			continue;
   2930 		}
   2931 		if (streakon == 0 && write) {
   2932 			streakon = 1;
   2933 			pcrun[si].pcache_runstart = pgs[i]->offset;
   2934 		}
   2935 
   2936 		if (!write)
   2937 			pgs[i]->flags |= PG_RDONLY;
   2938 	}
   2939 	/* was the last page part of our streak? */
   2940 	if (streakon) {
   2941 		pcrun[si].pcache_runend
   2942 		    = trunc_page(pgs[i-1]->offset) + PAGE_MASK;
   2943 		si++;
   2944 	}
   2945 	if (!locked)
   2946 		mutex_exit(vp->v_uobj.vmobjlock);
   2947 
   2948 	KASSERT(si <= (npages / 2) + 1);
   2949 
   2950 #ifdef notnowjohn
   2951 	/* send results to userspace */
   2952 	if (write)
   2953 		puffs_cacheop(pmp, parkmem, pcinfo,
   2954 		    sizeof(struct puffs_cacheinfo) + runsizes, VPTOPNC(vp));
   2955 #endif
   2956 
   2957  out:
   2958 	if (error) {
   2959 		if (pcinfo != NULL)
   2960 			kmem_free(pcinfo,
   2961 			    sizeof(struct puffs_cacheinfo) + runsizes);
   2962 #ifdef notnowjohn
   2963 		if (parkmem != NULL)
   2964 			puffs_park_release(parkmem, 1);
   2965 #endif
   2966 	}
   2967 
   2968 	return error;
   2969 }
   2970 
   2971 /*
   2972  * Extended attribute support.
   2973  */
   2974 
   2975 int
   2976 puffs_vnop_getextattr(void *v)
   2977 {
   2978 	struct vop_getextattr_args /*
   2979 		struct vnode *a_vp;
   2980 		int a_attrnamespace;
   2981 		const char *a_name;
   2982 		struct uio *a_uio;
   2983 		size_t *a_size;
   2984 		kauth_cred_t a_cred;
   2985 	}; */ *ap = v;
   2986 	PUFFS_MSG_VARS(vn, getextattr);
   2987 	struct vnode *vp = ap->a_vp;
   2988 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2989 	int attrnamespace = ap->a_attrnamespace;
   2990 	const char *name = ap->a_name;
   2991 	struct uio *uio = ap->a_uio;
   2992 	size_t *sizep = ap->a_size;
   2993 	size_t tomove, resid;
   2994 	int error;
   2995 
   2996 	if (uio)
   2997 		resid = uio->uio_resid;
   2998 	else
   2999 		resid = 0;
   3000 
   3001 	tomove = PUFFS_TOMOVE(resid, pmp);
   3002 	if (tomove != resid) {
   3003 		error = E2BIG;
   3004 		goto out;
   3005 	}
   3006 
   3007 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_getextattr) + tomove,
   3008 	    &park_getextattr, (void *)&getextattr_msg, 1);
   3009 
   3010 	getextattr_msg->pvnr_attrnamespace = attrnamespace;
   3011 	strlcpy(getextattr_msg->pvnr_attrname, name,
   3012 	    sizeof(getextattr_msg->pvnr_attrname));
   3013 	puffs_credcvt(&getextattr_msg->pvnr_cred, ap->a_cred);
   3014 	if (sizep)
   3015 		getextattr_msg->pvnr_datasize = 1;
   3016 	getextattr_msg->pvnr_resid = tomove;
   3017 
   3018 	puffs_msg_setinfo(park_getextattr,
   3019 	    PUFFSOP_VN, PUFFS_VN_GETEXTATTR, VPTOPNC(vp));
   3020 	puffs_msg_setdelta(park_getextattr, tomove);
   3021 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_getextattr, vp->v_data, NULL, error);
   3022 
   3023 	error = checkerr(pmp, error, __func__);
   3024 	if (error)
   3025 		goto out;
   3026 
   3027 	resid = getextattr_msg->pvnr_resid;
   3028 	if (resid > tomove) {
   3029 		puffs_senderr(pmp, PUFFS_ERR_GETEXTATTR, E2BIG,
   3030 		    "resid grew", VPTOPNC(vp));
   3031 		error = EPROTO;
   3032 		goto out;
   3033 	}
   3034 
   3035 	if (sizep)
   3036 		*sizep = getextattr_msg->pvnr_datasize;
   3037 	if (uio)
   3038 		error = uiomove(getextattr_msg->pvnr_data, tomove - resid, uio);
   3039 
   3040  out:
   3041 	PUFFS_MSG_RELEASE(getextattr);
   3042 	return error;
   3043 }
   3044 
   3045 int
   3046 puffs_vnop_setextattr(void *v)
   3047 {
   3048 	struct vop_setextattr_args /* {
   3049 		struct vnode *a_vp;
   3050 		int a_attrnamespace;
   3051 		const char *a_name;
   3052 		struct uio *a_uio;
   3053 		kauth_cred_t a_cred;
   3054 	}; */ *ap = v;
   3055 	PUFFS_MSG_VARS(vn, setextattr);
   3056 	struct vnode *vp = ap->a_vp;
   3057 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3058 	int attrnamespace = ap->a_attrnamespace;
   3059 	const char *name = ap->a_name;
   3060 	struct uio *uio = ap->a_uio;
   3061 	size_t tomove, resid;
   3062 	int error;
   3063 
   3064 	if (uio)
   3065 		resid = uio->uio_resid;
   3066 	else
   3067 		resid = 0;
   3068 
   3069 	tomove = PUFFS_TOMOVE(resid, pmp);
   3070 	if (tomove != resid) {
   3071 		error = E2BIG;
   3072 		goto out;
   3073 	}
   3074 
   3075 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_setextattr) + tomove,
   3076 	    &park_setextattr, (void *)&setextattr_msg, 1);
   3077 
   3078 	setextattr_msg->pvnr_attrnamespace = attrnamespace;
   3079 	strlcpy(setextattr_msg->pvnr_attrname, name,
   3080 	    sizeof(setextattr_msg->pvnr_attrname));
   3081 	puffs_credcvt(&setextattr_msg->pvnr_cred, ap->a_cred);
   3082 	setextattr_msg->pvnr_resid = tomove;
   3083 
   3084 	if (uio) {
   3085 		error = uiomove(setextattr_msg->pvnr_data, tomove, uio);
   3086 		if (error)
   3087 			goto out;
   3088 	}
   3089 
   3090 	puffs_msg_setinfo(park_setextattr,
   3091 	    PUFFSOP_VN, PUFFS_VN_SETEXTATTR, VPTOPNC(vp));
   3092 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_setextattr, vp->v_data, NULL, error);
   3093 
   3094 	error = checkerr(pmp, error, __func__);
   3095 	if (error)
   3096 		goto out;
   3097 
   3098 	if (setextattr_msg->pvnr_resid != 0)
   3099 		error = EIO;
   3100 
   3101  out:
   3102 	PUFFS_MSG_RELEASE(setextattr);
   3103 
   3104 	return error;
   3105 }
   3106 
   3107 int
   3108 puffs_vnop_listextattr(void *v)
   3109 {
   3110 	struct vop_listextattr_args /* {
   3111 		struct vnode *a_vp;
   3112 		int a_attrnamespace;
   3113 		struct uio *a_uio;
   3114 		size_t *a_size;
   3115 		int a_flag,
   3116 		kauth_cred_t a_cred;
   3117 	}; */ *ap = v;
   3118 	PUFFS_MSG_VARS(vn, listextattr);
   3119 	struct vnode *vp = ap->a_vp;
   3120 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3121 	int attrnamespace = ap->a_attrnamespace;
   3122 	struct uio *uio = ap->a_uio;
   3123 	size_t *sizep = ap->a_size;
   3124 	int flag = ap->a_flag;
   3125 	size_t tomove, resid;
   3126 	int error;
   3127 
   3128 	if (uio)
   3129 		resid = uio->uio_resid;
   3130 	else
   3131 		resid = 0;
   3132 
   3133 	tomove = PUFFS_TOMOVE(resid, pmp);
   3134 	if (tomove != resid) {
   3135 		error = E2BIG;
   3136 		goto out;
   3137 	}
   3138 
   3139 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_listextattr) + tomove,
   3140 	    &park_listextattr, (void *)&listextattr_msg, 1);
   3141 
   3142 	listextattr_msg->pvnr_attrnamespace = attrnamespace;
   3143 	listextattr_msg->pvnr_flag = flag;
   3144 	puffs_credcvt(&listextattr_msg->pvnr_cred, ap->a_cred);
   3145 	listextattr_msg->pvnr_resid = tomove;
   3146 	if (sizep)
   3147 		listextattr_msg->pvnr_datasize = 1;
   3148 
   3149 	puffs_msg_setinfo(park_listextattr,
   3150 	    PUFFSOP_VN, PUFFS_VN_LISTEXTATTR, VPTOPNC(vp));
   3151 	puffs_msg_setdelta(park_listextattr, tomove);
   3152 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_listextattr, vp->v_data, NULL, error);
   3153 
   3154 	error = checkerr(pmp, error, __func__);
   3155 	if (error)
   3156 		goto out;
   3157 
   3158 	resid = listextattr_msg->pvnr_resid;
   3159 	if (resid > tomove) {
   3160 		puffs_senderr(pmp, PUFFS_ERR_LISTEXTATTR, E2BIG,
   3161 		    "resid grew", VPTOPNC(vp));
   3162 		error = EPROTO;
   3163 		goto out;
   3164 	}
   3165 
   3166 	if (sizep)
   3167 		*sizep = listextattr_msg->pvnr_datasize;
   3168 	if (uio)
   3169 		error = uiomove(listextattr_msg->pvnr_data, tomove-resid, uio);
   3170 
   3171  out:
   3172 	PUFFS_MSG_RELEASE(listextattr);
   3173 	return error;
   3174 }
   3175 
   3176 int
   3177 puffs_vnop_deleteextattr(void *v)
   3178 {
   3179 	struct vop_deleteextattr_args /* {
   3180 		struct vnode *a_vp;
   3181 		int a_attrnamespace;
   3182 		const char *a_name;
   3183 		kauth_cred_t a_cred;
   3184 	}; */ *ap = v;
   3185 	PUFFS_MSG_VARS(vn, deleteextattr);
   3186 	struct vnode *vp = ap->a_vp;
   3187 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3188 	int attrnamespace = ap->a_attrnamespace;
   3189 	const char *name = ap->a_name;
   3190 	int error;
   3191 
   3192 	PUFFS_MSG_ALLOC(vn, deleteextattr);
   3193 	deleteextattr_msg->pvnr_attrnamespace = attrnamespace;
   3194 	strlcpy(deleteextattr_msg->pvnr_attrname, name,
   3195 	    sizeof(deleteextattr_msg->pvnr_attrname));
   3196 	puffs_credcvt(&deleteextattr_msg->pvnr_cred, ap->a_cred);
   3197 
   3198 	puffs_msg_setinfo(park_deleteextattr,
   3199 	    PUFFSOP_VN, PUFFS_VN_DELETEEXTATTR, VPTOPNC(vp));
   3200 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_deleteextattr,
   3201 	    vp->v_data, NULL, error);
   3202 
   3203 	error = checkerr(pmp, error, __func__);
   3204 
   3205 	PUFFS_MSG_RELEASE(deleteextattr);
   3206 	return error;
   3207 }
   3208 
   3209 /*
   3210  * spec & fifo.  These call the miscfs spec and fifo vectors, but issue
   3211  * FAF update information for the puffs node first.
   3212  */
   3213 int
   3214 puffs_vnop_spec_read(void *v)
   3215 {
   3216 	struct vop_read_args /* {
   3217 		const struct vnodeop_desc *a_desc;
   3218 		struct vnode *a_vp;
   3219 		struct uio *a_uio;
   3220 		int a_ioflag;
   3221 		kauth_cred_t a_cred;
   3222 	} */ *ap = v;
   3223 
   3224 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
   3225 	return VOCALL(spec_vnodeop_p, VOFFSET(vop_read), v);
   3226 }
   3227 
   3228 int
   3229 puffs_vnop_spec_write(void *v)
   3230 {
   3231 	struct vop_write_args /* {
   3232 		const struct vnodeop_desc *a_desc;
   3233 		struct vnode *a_vp;
   3234 		struct uio *a_uio;
   3235 		int a_ioflag;
   3236 		kauth_cred_t a_cred;
   3237 	} */ *ap = v;
   3238 
   3239 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
   3240 	return VOCALL(spec_vnodeop_p, VOFFSET(vop_write), v);
   3241 }
   3242 
   3243 int
   3244 puffs_vnop_fifo_read(void *v)
   3245 {
   3246 	struct vop_read_args /* {
   3247 		const struct vnodeop_desc *a_desc;
   3248 		struct vnode *a_vp;
   3249 		struct uio *a_uio;
   3250 		int a_ioflag;
   3251 		kauth_cred_t a_cred;
   3252 	} */ *ap = v;
   3253 
   3254 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
   3255 	return VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), v);
   3256 }
   3257 
   3258 int
   3259 puffs_vnop_fifo_write(void *v)
   3260 {
   3261 	struct vop_write_args /* {
   3262 		const struct vnodeop_desc *a_desc;
   3263 		struct vnode *a_vp;
   3264 		struct uio *a_uio;
   3265 		int a_ioflag;
   3266 		kauth_cred_t a_cred;
   3267 	} */ *ap = v;
   3268 
   3269 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
   3270 	return VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), v);
   3271 }
   3272