Home | History | Annotate | Line # | Download | only in puffs
puffs_vnops.c revision 1.176
      1 /*	$NetBSD: puffs_vnops.c,v 1.176 2012/11/05 17:27:38 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.176 2012/11/05 17:27:38 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_fsync_desc, puffs_vnop_fsync },		/* REAL fsync */
    117         { &vop_seek_desc, puffs_vnop_checkop },		/* seek */
    118         { &vop_remove_desc, puffs_vnop_checkop },	/* remove */
    119         { &vop_link_desc, puffs_vnop_checkop },		/* link */
    120         { &vop_rename_desc, puffs_vnop_checkop },	/* rename */
    121         { &vop_mkdir_desc, puffs_vnop_checkop },	/* mkdir */
    122         { &vop_rmdir_desc, puffs_vnop_checkop },	/* rmdir */
    123         { &vop_symlink_desc, puffs_vnop_checkop },	/* symlink */
    124         { &vop_readdir_desc, puffs_vnop_checkop },	/* readdir */
    125         { &vop_readlink_desc, puffs_vnop_checkop },	/* readlink */
    126         { &vop_getpages_desc, puffs_vnop_checkop },	/* getpages */
    127         { &vop_putpages_desc, genfs_putpages },		/* REAL putpages */
    128         { &vop_pathconf_desc, puffs_vnop_checkop },	/* pathconf */
    129         { &vop_advlock_desc, puffs_vnop_advlock },	/* advlock */
    130         { &vop_strategy_desc, puffs_vnop_strategy },	/* REAL strategy */
    131         { &vop_revoke_desc, genfs_revoke },		/* REAL revoke */
    132         { &vop_abortop_desc, puffs_vnop_abortop },	/* REAL abortop */
    133         { &vop_inactive_desc, puffs_vnop_inactive },	/* REAL inactive */
    134         { &vop_reclaim_desc, puffs_vnop_reclaim },	/* REAL reclaim */
    135         { &vop_lock_desc, puffs_vnop_lock },		/* REAL lock */
    136         { &vop_unlock_desc, puffs_vnop_unlock },	/* REAL unlock */
    137         { &vop_bmap_desc, puffs_vnop_bmap },		/* REAL bmap */
    138         { &vop_print_desc, puffs_vnop_print },		/* REAL print */
    139         { &vop_islocked_desc, puffs_vnop_islocked },	/* REAL islocked */
    140         { &vop_bwrite_desc, genfs_nullop },		/* REAL bwrite */
    141         { &vop_mmap_desc, puffs_vnop_mmap },		/* REAL mmap */
    142         { &vop_poll_desc, puffs_vnop_poll },		/* REAL poll */
    143 	{ &vop_getextattr_desc, puffs_vnop_getextattr },	/* getextattr */
    144 	{ &vop_setextattr_desc, puffs_vnop_setextattr },	/* setextattr */
    145 	{ &vop_listextattr_desc, puffs_vnop_listextattr },	/* listextattr */
    146 	{ &vop_deleteextattr_desc, puffs_vnop_deleteextattr },/* deleteextattr */
    147 #if 0
    148 	{ &vop_openextattr_desc, puffs_vnop_checkop },	/* openextattr */
    149 	{ &vop_closeextattr_desc, puffs_vnop_checkop },	/* closeextattr */
    150 #endif
    151         { &vop_kqfilter_desc, genfs_eopnotsupp },	/* kqfilter XXX */
    152 	{ NULL, NULL }
    153 };
    154 const struct vnodeopv_desc puffs_vnodeop_opv_desc =
    155 	{ &puffs_vnodeop_p, puffs_vnodeop_entries };
    156 
    157 
    158 int (**puffs_specop_p)(void *);
    159 const struct vnodeopv_entry_desc puffs_specop_entries[] = {
    160 	{ &vop_default_desc, vn_default_error },
    161 	{ &vop_lookup_desc, spec_lookup },		/* lookup, ENOTDIR */
    162 	{ &vop_create_desc, spec_create },		/* genfs_badop */
    163 	{ &vop_mknod_desc, spec_mknod },		/* genfs_badop */
    164 	{ &vop_open_desc, spec_open },			/* spec_open */
    165 	{ &vop_close_desc, spec_close },		/* spec_close */
    166 	{ &vop_access_desc, puffs_vnop_checkop },	/* access */
    167 	{ &vop_getattr_desc, puffs_vnop_checkop },	/* getattr */
    168 	{ &vop_setattr_desc, puffs_vnop_checkop },	/* setattr */
    169 	{ &vop_read_desc, puffs_vnop_spec_read },	/* update, read */
    170 	{ &vop_write_desc, puffs_vnop_spec_write },	/* update, write */
    171 	{ &vop_ioctl_desc, spec_ioctl },		/* spec_ioctl */
    172 	{ &vop_fcntl_desc, genfs_fcntl },		/* dummy */
    173 	{ &vop_poll_desc, spec_poll },			/* spec_poll */
    174 	{ &vop_kqfilter_desc, spec_kqfilter },		/* spec_kqfilter */
    175 	{ &vop_revoke_desc, spec_revoke },		/* genfs_revoke */
    176 	{ &vop_mmap_desc, spec_mmap },			/* spec_mmap */
    177 	{ &vop_fsync_desc, spec_fsync },		/* vflushbuf */
    178 	{ &vop_seek_desc, spec_seek },			/* genfs_nullop */
    179 	{ &vop_remove_desc, spec_remove },		/* genfs_badop */
    180 	{ &vop_link_desc, spec_link },			/* genfs_badop */
    181 	{ &vop_rename_desc, spec_rename },		/* genfs_badop */
    182 	{ &vop_mkdir_desc, spec_mkdir },		/* genfs_badop */
    183 	{ &vop_rmdir_desc, spec_rmdir },		/* genfs_badop */
    184 	{ &vop_symlink_desc, spec_symlink },		/* genfs_badop */
    185 	{ &vop_readdir_desc, spec_readdir },		/* genfs_badop */
    186 	{ &vop_readlink_desc, spec_readlink },		/* genfs_badop */
    187 	{ &vop_abortop_desc, spec_abortop },		/* genfs_badop */
    188 	{ &vop_inactive_desc, puffs_vnop_inactive },	/* REAL inactive */
    189 	{ &vop_reclaim_desc, puffs_vnop_reclaim },	/* REAL reclaim */
    190 	{ &vop_lock_desc, puffs_vnop_lock },		/* REAL lock */
    191 	{ &vop_unlock_desc, puffs_vnop_unlock },	/* REAL unlock */
    192 	{ &vop_bmap_desc, spec_bmap },			/* dummy */
    193 	{ &vop_strategy_desc, spec_strategy },		/* dev strategy */
    194 	{ &vop_print_desc, puffs_vnop_print },		/* REAL print */
    195 	{ &vop_islocked_desc, puffs_vnop_islocked },	/* REAL islocked */
    196 	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */
    197 	{ &vop_advlock_desc, spec_advlock },		/* lf_advlock */
    198 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    199 	{ &vop_getpages_desc, spec_getpages },		/* genfs_getpages */
    200 	{ &vop_putpages_desc, spec_putpages },		/* genfs_putpages */
    201 	{ &vop_getextattr_desc, puffs_vnop_checkop },	/* getextattr */
    202 	{ &vop_setextattr_desc, puffs_vnop_checkop },	/* setextattr */
    203 	{ &vop_listextattr_desc, puffs_vnop_checkop },	/* listextattr */
    204 	{ &vop_deleteextattr_desc, puffs_vnop_checkop },/* deleteextattr */
    205 #if 0
    206 	{ &vop_openextattr_desc, _openextattr },	/* openextattr */
    207 	{ &vop_closeextattr_desc, _closeextattr },	/* closeextattr */
    208 #endif
    209 	{ NULL, NULL }
    210 };
    211 const struct vnodeopv_desc puffs_specop_opv_desc =
    212 	{ &puffs_specop_p, puffs_specop_entries };
    213 
    214 
    215 int (**puffs_fifoop_p)(void *);
    216 const struct vnodeopv_entry_desc puffs_fifoop_entries[] = {
    217 	{ &vop_default_desc, vn_default_error },
    218 	{ &vop_lookup_desc, vn_fifo_bypass },		/* lookup, ENOTDIR */
    219 	{ &vop_create_desc, vn_fifo_bypass },		/* genfs_badop */
    220 	{ &vop_mknod_desc, vn_fifo_bypass },		/* genfs_badop */
    221 	{ &vop_open_desc, vn_fifo_bypass },		/* open */
    222 	{ &vop_close_desc, vn_fifo_bypass },		/* close */
    223 	{ &vop_access_desc, puffs_vnop_checkop },	/* access */
    224 	{ &vop_getattr_desc, puffs_vnop_checkop },	/* getattr */
    225 	{ &vop_setattr_desc, puffs_vnop_checkop },	/* setattr */
    226 	{ &vop_read_desc, puffs_vnop_fifo_read },	/* read, update */
    227 	{ &vop_write_desc, puffs_vnop_fifo_write },	/* write, update */
    228 	{ &vop_ioctl_desc, vn_fifo_bypass },		/* ioctl */
    229 	{ &vop_fcntl_desc, genfs_fcntl },		/* dummy */
    230 	{ &vop_poll_desc, vn_fifo_bypass },		/* poll */
    231 	{ &vop_kqfilter_desc, vn_fifo_bypass },		/* kqfilter */
    232 	{ &vop_revoke_desc, vn_fifo_bypass },		/* genfs_revoke */
    233 	{ &vop_mmap_desc, vn_fifo_bypass },		/* genfs_badop */
    234 	{ &vop_fsync_desc, vn_fifo_bypass },		/* genfs_nullop*/
    235 	{ &vop_seek_desc, vn_fifo_bypass },		/* genfs_badop */
    236 	{ &vop_remove_desc, vn_fifo_bypass },		/* genfs_badop */
    237 	{ &vop_link_desc, vn_fifo_bypass },		/* genfs_badop */
    238 	{ &vop_rename_desc, vn_fifo_bypass },		/* genfs_badop */
    239 	{ &vop_mkdir_desc, vn_fifo_bypass },		/* genfs_badop */
    240 	{ &vop_rmdir_desc, vn_fifo_bypass },		/* genfs_badop */
    241 	{ &vop_symlink_desc, vn_fifo_bypass },		/* genfs_badop */
    242 	{ &vop_readdir_desc, vn_fifo_bypass },		/* genfs_badop */
    243 	{ &vop_readlink_desc, vn_fifo_bypass },		/* genfs_badop */
    244 	{ &vop_abortop_desc, vn_fifo_bypass },		/* genfs_badop */
    245 	{ &vop_inactive_desc, puffs_vnop_inactive },	/* REAL inactive */
    246 	{ &vop_reclaim_desc, puffs_vnop_reclaim },	/* REAL reclaim */
    247 	{ &vop_lock_desc, puffs_vnop_lock },		/* REAL lock */
    248 	{ &vop_unlock_desc, puffs_vnop_unlock },	/* REAL unlock */
    249 	{ &vop_bmap_desc, vn_fifo_bypass },		/* dummy */
    250 	{ &vop_strategy_desc, vn_fifo_bypass },		/* genfs_badop */
    251 	{ &vop_print_desc, puffs_vnop_print },		/* REAL print */
    252 	{ &vop_islocked_desc, puffs_vnop_islocked },	/* REAL islocked */
    253 	{ &vop_pathconf_desc, vn_fifo_bypass },		/* pathconf */
    254 	{ &vop_advlock_desc, vn_fifo_bypass },		/* genfs_einval */
    255 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    256 	{ &vop_putpages_desc, vn_fifo_bypass }, 	/* genfs_null_putpages*/
    257 #if 0
    258 	{ &vop_openextattr_desc, _openextattr },	/* openextattr */
    259 	{ &vop_closeextattr_desc, _closeextattr },	/* closeextattr */
    260 #endif
    261 	{ &vop_getextattr_desc, puffs_vnop_checkop },		/* getextattr */
    262 	{ &vop_setextattr_desc, puffs_vnop_checkop },		/* setextattr */
    263 	{ &vop_listextattr_desc, puffs_vnop_checkop },	/* listextattr */
    264 	{ &vop_deleteextattr_desc, puffs_vnop_checkop },	/* deleteextattr */
    265 	{ NULL, NULL }
    266 };
    267 const struct vnodeopv_desc puffs_fifoop_opv_desc =
    268 	{ &puffs_fifoop_p, puffs_fifoop_entries };
    269 
    270 
    271 /* "real" vnode operations */
    272 int (**puffs_msgop_p)(void *);
    273 const struct vnodeopv_entry_desc puffs_msgop_entries[] = {
    274 	{ &vop_default_desc, vn_default_error },
    275 	{ &vop_create_desc, puffs_vnop_create },	/* create */
    276         { &vop_mknod_desc, puffs_vnop_mknod },		/* mknod */
    277         { &vop_open_desc, puffs_vnop_open },		/* open */
    278         { &vop_close_desc, puffs_vnop_close },		/* close */
    279         { &vop_access_desc, puffs_vnop_access },	/* access */
    280         { &vop_getattr_desc, puffs_vnop_getattr },	/* getattr */
    281         { &vop_setattr_desc, puffs_vnop_setattr },	/* setattr */
    282         { &vop_read_desc, puffs_vnop_read },		/* read */
    283         { &vop_write_desc, puffs_vnop_write },		/* write */
    284         { &vop_seek_desc, puffs_vnop_seek },		/* seek */
    285         { &vop_remove_desc, puffs_vnop_remove },	/* remove */
    286         { &vop_link_desc, puffs_vnop_link },		/* link */
    287         { &vop_rename_desc, puffs_vnop_rename },	/* rename */
    288         { &vop_mkdir_desc, puffs_vnop_mkdir },		/* mkdir */
    289         { &vop_rmdir_desc, puffs_vnop_rmdir },		/* rmdir */
    290         { &vop_symlink_desc, puffs_vnop_symlink },	/* symlink */
    291         { &vop_readdir_desc, puffs_vnop_readdir },	/* readdir */
    292         { &vop_readlink_desc, puffs_vnop_readlink },	/* readlink */
    293         { &vop_print_desc, puffs_vnop_print },		/* print */
    294         { &vop_islocked_desc, puffs_vnop_islocked },	/* islocked */
    295         { &vop_pathconf_desc, puffs_vnop_pathconf },	/* pathconf */
    296         { &vop_getpages_desc, puffs_vnop_getpages },	/* getpages */
    297 	{ NULL, NULL }
    298 };
    299 const struct vnodeopv_desc puffs_msgop_opv_desc =
    300 	{ &puffs_msgop_p, puffs_msgop_entries };
    301 
    302 /*
    303  * for dosetattr / update_va
    304  */
    305 #define SETATTR_CHSIZE	0x01
    306 #define SETATTR_ASYNC	0x02
    307 
    308 #define ERROUT(err)							\
    309 do {									\
    310 	error = err;							\
    311 	goto out;							\
    312 } while (/*CONSTCOND*/0)
    313 
    314 /*
    315  * This is a generic vnode operation handler.  It checks if the necessary
    316  * operations for the called vnode operation are implemented by userspace
    317  * and either returns a dummy return value or proceeds to call the real
    318  * vnode operation from puffs_msgop_v.
    319  *
    320  * XXX: this should described elsewhere and autogenerated, the complexity
    321  * of the vnode operations vectors and their interrelationships is also
    322  * getting a bit out of hand.  Another problem is that we need this same
    323  * information in the fs server code, so keeping the two in sync manually
    324  * is not a viable (long term) plan.
    325  */
    326 
    327 /* not supported, handle locking protocol */
    328 #define CHECKOP_NOTSUPP(op)						\
    329 case VOP_##op##_DESCOFFSET:						\
    330 	if (pmp->pmp_vnopmask[PUFFS_VN_##op] == 0)			\
    331 		return genfs_eopnotsupp(v);				\
    332 	break
    333 
    334 /* always succeed, no locking */
    335 #define CHECKOP_SUCCESS(op)						\
    336 case VOP_##op##_DESCOFFSET:						\
    337 	if (pmp->pmp_vnopmask[PUFFS_VN_##op] == 0)			\
    338 		return 0;						\
    339 	break
    340 
    341 int
    342 puffs_vnop_checkop(void *v)
    343 {
    344 	struct vop_generic_args /* {
    345 		struct vnodeop_desc *a_desc;
    346 		spooky mystery contents;
    347 	} */ *ap = v;
    348 	struct vnodeop_desc *desc = ap->a_desc;
    349 	struct puffs_mount *pmp;
    350 	struct vnode *vp;
    351 	int offset, rv;
    352 
    353 	offset = ap->a_desc->vdesc_vp_offsets[0];
    354 #ifdef DIAGNOSTIC
    355 	if (offset == VDESC_NO_OFFSET)
    356 		panic("puffs_checkop: no vnode, why did you call me?");
    357 #endif
    358 	vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap);
    359 	pmp = MPTOPUFFSMP(vp->v_mount);
    360 
    361 	DPRINTF_VERBOSE(("checkop call %s (%d), vp %p\n",
    362 	    ap->a_desc->vdesc_name, ap->a_desc->vdesc_offset, vp));
    363 
    364 	if (!ALLOPS(pmp)) {
    365 		switch (desc->vdesc_offset) {
    366 			CHECKOP_NOTSUPP(CREATE);
    367 			CHECKOP_NOTSUPP(MKNOD);
    368 			CHECKOP_NOTSUPP(GETATTR);
    369 			CHECKOP_NOTSUPP(SETATTR);
    370 			CHECKOP_NOTSUPP(READ);
    371 			CHECKOP_NOTSUPP(WRITE);
    372 			CHECKOP_NOTSUPP(FCNTL);
    373 			CHECKOP_NOTSUPP(IOCTL);
    374 			CHECKOP_NOTSUPP(REMOVE);
    375 			CHECKOP_NOTSUPP(LINK);
    376 			CHECKOP_NOTSUPP(RENAME);
    377 			CHECKOP_NOTSUPP(MKDIR);
    378 			CHECKOP_NOTSUPP(RMDIR);
    379 			CHECKOP_NOTSUPP(SYMLINK);
    380 			CHECKOP_NOTSUPP(READDIR);
    381 			CHECKOP_NOTSUPP(READLINK);
    382 			CHECKOP_NOTSUPP(PRINT);
    383 			CHECKOP_NOTSUPP(PATHCONF);
    384 			CHECKOP_NOTSUPP(GETEXTATTR);
    385 			CHECKOP_NOTSUPP(SETEXTATTR);
    386 			CHECKOP_NOTSUPP(LISTEXTATTR);
    387 			CHECKOP_NOTSUPP(DELETEEXTATTR);
    388 
    389 			CHECKOP_SUCCESS(ACCESS);
    390 			CHECKOP_SUCCESS(CLOSE);
    391 			CHECKOP_SUCCESS(SEEK);
    392 
    393 		case VOP_GETPAGES_DESCOFFSET:
    394 			if (!EXISTSOP(pmp, READ))
    395 				return genfs_eopnotsupp(v);
    396 			break;
    397 
    398 		default:
    399 			panic("puffs_checkop: unhandled vnop %d",
    400 			    desc->vdesc_offset);
    401 		}
    402 	}
    403 
    404 	rv = VOCALL(puffs_msgop_p, ap->a_desc->vdesc_offset, v);
    405 
    406 	DPRINTF_VERBOSE(("checkop return %s (%d), vp %p: %d\n",
    407 	    ap->a_desc->vdesc_name, ap->a_desc->vdesc_offset, vp, rv));
    408 
    409 	return rv;
    410 }
    411 
    412 static int callremove(struct puffs_mount *, puffs_cookie_t, puffs_cookie_t,
    413 			    struct componentname *);
    414 static int callrmdir(struct puffs_mount *, puffs_cookie_t, puffs_cookie_t,
    415 			   struct componentname *);
    416 static void callinactive(struct puffs_mount *, puffs_cookie_t, int);
    417 static void callreclaim(struct puffs_mount *, puffs_cookie_t, int);
    418 static int  flushvncache(struct vnode *, off_t, off_t, bool);
    419 static void update_va(struct vnode *, struct vattr *, struct vattr *,
    420 		      struct timespec *, struct timespec *, int);
    421 static void update_parent(struct vnode *, struct vnode *);
    422 
    423 
    424 #define PUFFS_ABORT_LOOKUP	1
    425 #define PUFFS_ABORT_CREATE	2
    426 #define PUFFS_ABORT_MKNOD	3
    427 #define PUFFS_ABORT_MKDIR	4
    428 #define PUFFS_ABORT_SYMLINK	5
    429 
    430 /*
    431  * Press the pani^Wabort button!  Kernel resource allocation failed.
    432  */
    433 static void
    434 puffs_abortbutton(struct puffs_mount *pmp, int what,
    435 	puffs_cookie_t dck, puffs_cookie_t ck, struct componentname *cnp)
    436 {
    437 
    438 	switch (what) {
    439 	case PUFFS_ABORT_CREATE:
    440 	case PUFFS_ABORT_MKNOD:
    441 	case PUFFS_ABORT_SYMLINK:
    442 		callremove(pmp, dck, ck, cnp);
    443 		break;
    444 	case PUFFS_ABORT_MKDIR:
    445 		callrmdir(pmp, dck, ck, cnp);
    446 		break;
    447 	}
    448 
    449 	callinactive(pmp, ck, 0);
    450 	callreclaim(pmp, ck, 1);
    451 }
    452 
    453 /*
    454  * Begin vnode operations.
    455  *
    456  * A word from the keymaster about locks: generally we don't want
    457  * to use the vnode locks at all: it creates an ugly dependency between
    458  * the userlandia file server and the kernel.  But we'll play along with
    459  * the kernel vnode locks for now.  However, even currently we attempt
    460  * to release locks as early as possible.  This is possible for some
    461  * operations which a) don't need a locked vnode after the userspace op
    462  * and b) return with the vnode unlocked.  Theoretically we could
    463  * unlock-do op-lock for others and order the graph in userspace, but I
    464  * don't want to think of the consequences for the time being.
    465  */
    466 
    467 #define TTL_TO_TIMEOUT(ts) \
    468     (hardclock_ticks + (ts->tv_sec * hz) + (ts->tv_nsec * hz / 1000000000))
    469 #define TTL_VALID(ts) \
    470     ((ts != NULL) && !((ts->tv_sec == 0) && (ts->tv_nsec == 0)))
    471 #define TIMED_OUT(expire) \
    472     ((int)((unsigned int)hardclock_ticks - (unsigned int)expire) > 0)
    473 int
    474 puffs_vnop_lookup(void *v)
    475 {
    476         struct vop_lookup_args /* {
    477 		const struct vnodeop_desc *a_desc;
    478 		struct vnode *a_dvp;
    479 		struct vnode **a_vpp;
    480 		struct componentname *a_cnp;
    481         } */ *ap = v;
    482 	PUFFS_MSG_VARS(vn, lookup);
    483 	struct puffs_mount *pmp;
    484 	struct componentname *cnp;
    485 	struct vnode *vp, *dvp, *cvp;
    486 	struct puffs_node *dpn, *cpn;
    487 	int isdot;
    488 	int error;
    489 
    490 	pmp = MPTOPUFFSMP(ap->a_dvp->v_mount);
    491 	cnp = ap->a_cnp;
    492 	dvp = ap->a_dvp;
    493 	cvp = NULL;
    494 	cpn = NULL;
    495 	*ap->a_vpp = NULL;
    496 
    497 	/* r/o fs?  we check create later to handle EEXIST */
    498 	if ((cnp->cn_flags & ISLASTCN)
    499 	    && (dvp->v_mount->mnt_flag & MNT_RDONLY)
    500 	    && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
    501 		return EROFS;
    502 
    503 	isdot = cnp->cn_namelen == 1 && *cnp->cn_nameptr == '.';
    504 
    505 	DPRINTF(("puffs_lookup: \"%s\", parent vnode %p, op: %x\n",
    506 	    cnp->cn_nameptr, dvp, cnp->cn_nameiop));
    507 
    508 	/*
    509 	 * If dotdot cache is enabled, unlock parent, lock ..
    510 	 * (grand-parent) and relock parent.
    511 	 */
    512 	if (PUFFS_USE_DOTDOTCACHE(pmp) && (cnp->cn_flags & ISDOTDOT)) {
    513 		VOP_UNLOCK(dvp);
    514 
    515 		vp = VPTOPP(ap->a_dvp)->pn_parent;
    516 		vref(vp);
    517 
    518 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    519 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    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 locked
    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 		mutex_enter(&cpn->pn_sizemtx);
    587 
    588 	PUFFS_MSG_ALLOC(vn, lookup);
    589 	puffs_makecn(&lookup_msg->pvnr_cn, &lookup_msg->pvnr_cn_cred,
    590 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
    591 
    592 	if (cnp->cn_flags & ISDOTDOT)
    593 		VOP_UNLOCK(dvp);
    594 
    595 	puffs_msg_setinfo(park_lookup, PUFFSOP_VN,
    596 	    PUFFS_VN_LOOKUP, VPTOPNC(dvp));
    597 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_lookup, dvp->v_data, NULL, error);
    598 	DPRINTF(("puffs_lookup: return of the userspace, part %d\n", error));
    599 
    600 	/*
    601 	 * In case of error, there is no new vnode to play with, so be
    602 	 * happy with the NULL value given to vpp in the beginning.
    603 	 * Also, check if this really was an error or the target was not
    604 	 * present.  Either treat it as a non-error for CREATE/RENAME or
    605 	 * enter the component into the negative name cache (if desired).
    606 	 */
    607 	if (error) {
    608 		error = checkerr(pmp, error, __func__);
    609 		if (error == ENOENT) {
    610 			/* don't allow to create files on r/o fs */
    611 			if ((dvp->v_mount->mnt_flag & MNT_RDONLY)
    612 			    && cnp->cn_nameiop == CREATE) {
    613 				error = EROFS;
    614 
    615 			/* adjust values if we are creating */
    616 			} else if ((cnp->cn_flags & ISLASTCN)
    617 			    && (cnp->cn_nameiop == CREATE
    618 			      || cnp->cn_nameiop == RENAME)) {
    619 				error = EJUSTRETURN;
    620 
    621 			/* save negative cache entry */
    622 			} else {
    623 				if (PUFFS_USE_NAMECACHE(pmp) &&
    624 				    !PUFFS_USE_FS_TTL(pmp))
    625 					cache_enter(dvp, NULL, cnp->cn_nameptr,
    626 						cnp->cn_namelen, cnp->cn_flags);
    627 			}
    628 		}
    629 		goto out;
    630 	}
    631 
    632 	/*
    633 	 * Check that we don't get our parent node back, that would cause
    634 	 * a pretty obvious deadlock.
    635 	 */
    636 	dpn = dvp->v_data;
    637 	if (lookup_msg->pvnr_newnode == dpn->pn_cookie) {
    638 		puffs_senderr(pmp, PUFFS_ERR_LOOKUP, EINVAL,
    639 		    "lookup produced parent cookie", lookup_msg->pvnr_newnode);
    640 		error = EPROTO;
    641 		goto out;
    642 	}
    643 
    644 	/*
    645 	 * Check if we looked up the cached vnode
    646 	 */
    647 	vp = NULL;
    648 	if (cvp && (VPTOPP(cvp)->pn_cookie == lookup_msg->pvnr_newnode)) {
    649 		int grace;
    650 
    651 		/*
    652 		 * Bump grace time of this node so that it does not get
    653 		 * reclaimed too fast. We try to increase a bit more the
    654 		 * lifetime of busiest * nodes - with some limits.
    655 		 */
    656 		grace = 10 * puffs_sopreq_expire_timeout;
    657 		cpn->pn_cn_grace = hardclock_ticks + grace;
    658 		vp = cvp;
    659 	}
    660 
    661 	/*
    662 	 * No cached vnode available, or the cached vnode does not
    663 	 * match the userland cookie anymore: is the node known?
    664 	 */
    665 	if (vp == NULL) {
    666 		error = puffs_cookie2vnode(pmp, lookup_msg->pvnr_newnode,
    667 					   1, 1, &vp);
    668 	}
    669 
    670 	if (error == PUFFS_NOSUCHCOOKIE) {
    671 		error = puffs_getvnode(dvp->v_mount,
    672 		    lookup_msg->pvnr_newnode, lookup_msg->pvnr_vtype,
    673 		    lookup_msg->pvnr_size, lookup_msg->pvnr_rdev, &vp);
    674 		if (error) {
    675 			puffs_abortbutton(pmp, PUFFS_ABORT_LOOKUP,
    676 			    VPTOPNC(dvp), lookup_msg->pvnr_newnode,
    677 			    ap->a_cnp);
    678 			goto out;
    679 		}
    680 
    681 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    682 	} else if (error) {
    683 		puffs_abortbutton(pmp, PUFFS_ABORT_LOOKUP, VPTOPNC(dvp),
    684 		    lookup_msg->pvnr_newnode, ap->a_cnp);
    685 		goto out;
    686 	}
    687 
    688 	/*
    689 	 * Update cache and TTL
    690 	 */
    691 	if (PUFFS_USE_FS_TTL(pmp)) {
    692 		struct timespec *va_ttl = &lookup_msg->pvnr_va_ttl;
    693 		struct timespec *cn_ttl = &lookup_msg->pvnr_cn_ttl;
    694 		update_va(vp, NULL, &lookup_msg->pvnr_va,
    695 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
    696 	}
    697 
    698 	KASSERT(lookup_msg->pvnr_newnode == VPTOPP(vp)->pn_cookie);
    699 	*ap->a_vpp = vp;
    700 
    701 	if (PUFFS_USE_NAMECACHE(pmp))
    702 		cache_enter(dvp, vp, cnp->cn_nameptr, cnp->cn_namelen,
    703 			    cnp->cn_flags);
    704 
    705 	/* XXX */
    706 	if ((lookup_msg->pvnr_cn.pkcn_flags & REQUIREDIR) == 0)
    707 		cnp->cn_flags &= ~REQUIREDIR;
    708 	if (lookup_msg->pvnr_cn.pkcn_consume)
    709 		cnp->cn_consume = MIN(lookup_msg->pvnr_cn.pkcn_consume,
    710 		    strlen(cnp->cn_nameptr) - cnp->cn_namelen);
    711 
    712 	VPTOPP(vp)->pn_nlookup++;
    713 
    714 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
    715 	    (VPTOPP(vp)->pn_parent != dvp))
    716 		update_parent(vp, dvp);
    717 
    718  out:
    719 	if (cvp != NULL) {
    720 		mutex_exit(&cpn->pn_sizemtx);
    721 
    722 		if (error || (cvp != vp))
    723 			vput(cvp);
    724 	}
    725 
    726 	if (cnp->cn_flags & ISDOTDOT)
    727 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
    728 
    729 	DPRINTF(("puffs_lookup: returning %d %p\n", error, *ap->a_vpp));
    730 	PUFFS_MSG_RELEASE(lookup);
    731 	return error;
    732 }
    733 
    734 #define REFPN_AND_UNLOCKVP(a, b)					\
    735 do {									\
    736 	mutex_enter(&b->pn_mtx);					\
    737 	puffs_referencenode(b);						\
    738 	mutex_exit(&b->pn_mtx);						\
    739 	VOP_UNLOCK(a);						\
    740 } while (/*CONSTCOND*/0)
    741 
    742 #define REFPN(b)							\
    743 do {									\
    744 	mutex_enter(&b->pn_mtx);					\
    745 	puffs_referencenode(b);						\
    746 	mutex_exit(&b->pn_mtx);						\
    747 } while (/*CONSTCOND*/0)
    748 
    749 #define RELEPN_AND_VP(a, b)						\
    750 do {									\
    751 	puffs_releasenode(b);						\
    752 	vrele(a);							\
    753 } while (/*CONSTCOND*/0)
    754 
    755 int
    756 puffs_vnop_create(void *v)
    757 {
    758 	struct vop_create_args /* {
    759 		const struct vnodeop_desc *a_desc;
    760 		struct vnode *a_dvp;
    761 		struct vnode **a_vpp;
    762 		struct componentname *a_cnp;
    763 		struct vattr *a_vap;
    764 	} */ *ap = v;
    765 	PUFFS_MSG_VARS(vn, create);
    766 	struct vnode *dvp = ap->a_dvp;
    767 	struct puffs_node *dpn = VPTOPP(dvp);
    768 	struct componentname *cnp = ap->a_cnp;
    769 	struct mount *mp = dvp->v_mount;
    770 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    771 	int error;
    772 
    773 	DPRINTF(("puffs_create: dvp %p, cnp: %s\n",
    774 	    dvp, ap->a_cnp->cn_nameptr));
    775 
    776 	PUFFS_MSG_ALLOC(vn, create);
    777 	puffs_makecn(&create_msg->pvnr_cn, &create_msg->pvnr_cn_cred,
    778 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
    779 	create_msg->pvnr_va = *ap->a_vap;
    780 	puffs_msg_setinfo(park_create, PUFFSOP_VN,
    781 	    PUFFS_VN_CREATE, VPTOPNC(dvp));
    782 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_create, dvp->v_data, NULL, error);
    783 
    784 	error = checkerr(pmp, error, __func__);
    785 	if (error)
    786 		goto out;
    787 
    788 	error = puffs_newnode(mp, dvp, ap->a_vpp,
    789 	    create_msg->pvnr_newnode, cnp, ap->a_vap->va_type, 0);
    790 	if (error) {
    791 		puffs_abortbutton(pmp, PUFFS_ABORT_CREATE, dpn->pn_cookie,
    792 		    create_msg->pvnr_newnode, cnp);
    793 		goto out;
    794 	}
    795 
    796 	if (PUFFS_USE_FS_TTL(pmp)) {
    797 		struct timespec *va_ttl = &create_msg->pvnr_va_ttl;
    798 		struct timespec *cn_ttl = &create_msg->pvnr_cn_ttl;
    799 		struct vattr *rvap = &create_msg->pvnr_va;
    800 
    801 		update_va(*ap->a_vpp, NULL, rvap,
    802 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
    803 	}
    804 
    805 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
    806 
    807 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
    808 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
    809 		update_parent(*ap->a_vpp, dvp);
    810 
    811  out:
    812 	vput(dvp);
    813 
    814 	DPRINTF(("puffs_create: return %d\n", error));
    815 	PUFFS_MSG_RELEASE(create);
    816 	return error;
    817 }
    818 
    819 int
    820 puffs_vnop_mknod(void *v)
    821 {
    822 	struct vop_mknod_args /* {
    823 		const struct vnodeop_desc *a_desc;
    824 		struct vnode *a_dvp;
    825 		struct vnode **a_vpp;
    826 		struct componentname *a_cnp;
    827 		struct vattr *a_vap;
    828 	} */ *ap = v;
    829 	PUFFS_MSG_VARS(vn, mknod);
    830 	struct vnode *dvp = ap->a_dvp;
    831 	struct puffs_node *dpn = VPTOPP(dvp);
    832 	struct componentname *cnp = ap->a_cnp;
    833 	struct mount *mp = dvp->v_mount;
    834 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
    835 	int error;
    836 
    837 	PUFFS_MSG_ALLOC(vn, mknod);
    838 	puffs_makecn(&mknod_msg->pvnr_cn, &mknod_msg->pvnr_cn_cred,
    839 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
    840 	mknod_msg->pvnr_va = *ap->a_vap;
    841 	puffs_msg_setinfo(park_mknod, PUFFSOP_VN,
    842 	    PUFFS_VN_MKNOD, VPTOPNC(dvp));
    843 
    844 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mknod, dvp->v_data, NULL, error);
    845 
    846 	error = checkerr(pmp, error, __func__);
    847 	if (error)
    848 		goto out;
    849 
    850 	error = puffs_newnode(mp, dvp, ap->a_vpp,
    851 	    mknod_msg->pvnr_newnode, cnp, ap->a_vap->va_type,
    852 	    ap->a_vap->va_rdev);
    853 	if (error) {
    854 		puffs_abortbutton(pmp, PUFFS_ABORT_MKNOD, dpn->pn_cookie,
    855 		    mknod_msg->pvnr_newnode, cnp);
    856 		goto out;
    857 	}
    858 
    859 	if (PUFFS_USE_FS_TTL(pmp)) {
    860 		struct timespec *va_ttl = &mknod_msg->pvnr_va_ttl;
    861 		struct timespec *cn_ttl = &mknod_msg->pvnr_cn_ttl;
    862 		struct vattr *rvap = &mknod_msg->pvnr_va;
    863 
    864 		update_va(*ap->a_vpp, NULL, rvap,
    865 			   va_ttl, cn_ttl, SETATTR_CHSIZE);
    866 	}
    867 
    868 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
    869 
    870 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
    871 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
    872 		update_parent(*ap->a_vpp, dvp);
    873 
    874  out:
    875 	vput(dvp);
    876 	PUFFS_MSG_RELEASE(mknod);
    877 	return error;
    878 }
    879 
    880 int
    881 puffs_vnop_open(void *v)
    882 {
    883 	struct vop_open_args /* {
    884 		const struct vnodeop_desc *a_desc;
    885 		struct vnode *a_vp;
    886 		int a_mode;
    887 		kauth_cred_t a_cred;
    888 	} */ *ap = v;
    889 	PUFFS_MSG_VARS(vn, open);
    890 	struct vnode *vp = ap->a_vp;
    891 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    892 	int mode = ap->a_mode;
    893 	int error;
    894 
    895 	DPRINTF(("puffs_open: vp %p, mode 0x%x\n", vp, mode));
    896 
    897 	if (vp->v_type == VREG && mode & FWRITE && !EXISTSOP(pmp, WRITE))
    898 		ERROUT(EROFS);
    899 
    900 	if (!EXISTSOP(pmp, OPEN))
    901 		ERROUT(0);
    902 
    903 	PUFFS_MSG_ALLOC(vn, open);
    904 	open_msg->pvnr_mode = mode;
    905 	puffs_credcvt(&open_msg->pvnr_cred, ap->a_cred);
    906 	puffs_msg_setinfo(park_open, PUFFSOP_VN,
    907 	    PUFFS_VN_OPEN, VPTOPNC(vp));
    908 
    909 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_open, vp->v_data, NULL, error);
    910 	error = checkerr(pmp, error, __func__);
    911 
    912  out:
    913 	DPRINTF(("puffs_open: returning %d\n", error));
    914 	PUFFS_MSG_RELEASE(open);
    915 	return error;
    916 }
    917 
    918 int
    919 puffs_vnop_close(void *v)
    920 {
    921 	struct vop_close_args /* {
    922 		const struct vnodeop_desc *a_desc;
    923 		struct vnode *a_vp;
    924 		int a_fflag;
    925 		kauth_cred_t a_cred;
    926 	} */ *ap = v;
    927 	PUFFS_MSG_VARS(vn, close);
    928 	struct vnode *vp = ap->a_vp;
    929 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    930 
    931 	PUFFS_MSG_ALLOC(vn, close);
    932 	puffs_msg_setfaf(park_close);
    933 	close_msg->pvnr_fflag = ap->a_fflag;
    934 	puffs_credcvt(&close_msg->pvnr_cred, ap->a_cred);
    935 	puffs_msg_setinfo(park_close, PUFFSOP_VN,
    936 	    PUFFS_VN_CLOSE, VPTOPNC(vp));
    937 
    938 	puffs_msg_enqueue(pmp, park_close);
    939 	PUFFS_MSG_RELEASE(close);
    940 	return 0;
    941 }
    942 
    943 int
    944 puffs_vnop_access(void *v)
    945 {
    946 	struct vop_access_args /* {
    947 		const struct vnodeop_desc *a_desc;
    948 		struct vnode *a_vp;
    949 		int a_mode;
    950 		kauth_cred_t a_cred;
    951 	} */ *ap = v;
    952 	PUFFS_MSG_VARS(vn, access);
    953 	struct vnode *vp = ap->a_vp;
    954 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
    955 	int mode = ap->a_mode;
    956 	int error;
    957 
    958 	if (mode & VWRITE) {
    959 		switch (vp->v_type) {
    960 		case VDIR:
    961 		case VLNK:
    962 		case VREG:
    963 			if ((vp->v_mount->mnt_flag & MNT_RDONLY)
    964 			    || !EXISTSOP(pmp, WRITE))
    965 				return EROFS;
    966 			break;
    967 		default:
    968 			break;
    969 		}
    970 	}
    971 
    972 	if (!EXISTSOP(pmp, ACCESS))
    973 		return 0;
    974 
    975 	PUFFS_MSG_ALLOC(vn, access);
    976 	access_msg->pvnr_mode = ap->a_mode;
    977 	puffs_credcvt(&access_msg->pvnr_cred, ap->a_cred);
    978 	puffs_msg_setinfo(park_access, PUFFSOP_VN,
    979 	    PUFFS_VN_ACCESS, VPTOPNC(vp));
    980 
    981 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_access, vp->v_data, NULL, error);
    982 	error = checkerr(pmp, error, __func__);
    983 	PUFFS_MSG_RELEASE(access);
    984 
    985 	return error;
    986 }
    987 
    988 static void
    989 update_va(struct vnode *vp, struct vattr *vap, struct vattr *rvap,
    990 	  struct timespec *va_ttl, struct timespec *cn_ttl, int flags)
    991 {
    992 	struct puffs_node *pn = VPTOPP(vp);
    993 
    994 	if (TTL_VALID(cn_ttl)) {
    995 		pn->pn_cn_timeout = TTL_TO_TIMEOUT(cn_ttl);
    996 		pn->pn_cn_grace = MAX(pn->pn_cn_timeout, pn->pn_cn_grace);
    997 	}
    998 
    999 	/*
   1000 	 * Don't listen to the file server regarding special device
   1001 	 * size info, the file server doesn't know anything about them.
   1002 	 */
   1003 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1004 		rvap->va_size = vp->v_size;
   1005 
   1006 	/* Ditto for blocksize (ufs comment: this doesn't belong here) */
   1007 	if (vp->v_type == VBLK)
   1008 		rvap->va_blocksize = BLKDEV_IOSIZE;
   1009 	else if (vp->v_type == VCHR)
   1010 		rvap->va_blocksize = MAXBSIZE;
   1011 
   1012 	if (vap != NULL) {
   1013 		(void) memcpy(vap, rvap, sizeof(struct vattr));
   1014 		vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
   1015 
   1016 		if (pn->pn_stat & PNODE_METACACHE_ATIME)
   1017 			vap->va_atime = pn->pn_mc_atime;
   1018 		if (pn->pn_stat & PNODE_METACACHE_CTIME)
   1019 			vap->va_ctime = pn->pn_mc_ctime;
   1020 		if (pn->pn_stat & PNODE_METACACHE_MTIME)
   1021 			vap->va_mtime = pn->pn_mc_mtime;
   1022 		if (pn->pn_stat & PNODE_METACACHE_SIZE)
   1023 			vap->va_size = pn->pn_mc_size;
   1024 	}
   1025 
   1026 	if (!(pn->pn_stat & PNODE_METACACHE_SIZE) && (flags & SETATTR_CHSIZE)) {
   1027 		if (rvap->va_size != VNOVAL
   1028 		    && vp->v_type != VBLK && vp->v_type != VCHR) {
   1029 			uvm_vnp_setsize(vp, rvap->va_size);
   1030 			pn->pn_serversize = rvap->va_size;
   1031 		}
   1032 	}
   1033 
   1034 	if ((va_ttl != NULL) && TTL_VALID(va_ttl)) {
   1035 		if (pn->pn_va_cache == NULL)
   1036 			pn->pn_va_cache = pool_get(&puffs_vapool, PR_WAITOK);
   1037 
   1038 		(void)memcpy(pn->pn_va_cache, rvap, sizeof(*rvap));
   1039 
   1040 		pn->pn_va_timeout = TTL_TO_TIMEOUT(va_ttl);
   1041 	}
   1042 }
   1043 
   1044 static void
   1045 update_parent(struct vnode *vp, struct vnode *dvp)
   1046 {
   1047 	struct puffs_node *pn = VPTOPP(vp);
   1048 
   1049 	if (pn->pn_parent != NULL) {
   1050 		KASSERT(pn->pn_parent != dvp);
   1051 		vrele(pn->pn_parent);
   1052 	}
   1053 
   1054 	vref(dvp);
   1055 	pn->pn_parent = dvp;
   1056 }
   1057 
   1058 int
   1059 puffs_vnop_getattr(void *v)
   1060 {
   1061 	struct vop_getattr_args /* {
   1062 		const struct vnodeop_desc *a_desc;
   1063 		struct vnode *a_vp;
   1064 		struct vattr *a_vap;
   1065 		kauth_cred_t a_cred;
   1066 	} */ *ap = v;
   1067 	PUFFS_MSG_VARS(vn, getattr);
   1068 	struct vnode *vp = ap->a_vp;
   1069 	struct mount *mp = vp->v_mount;
   1070 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
   1071 	struct vattr *vap, *rvap;
   1072 	struct puffs_node *pn = VPTOPP(vp);
   1073 	struct timespec *va_ttl = NULL;
   1074 	int error = 0;
   1075 
   1076 	/*
   1077 	 * A lock is required so that we do not race with
   1078 	 * setattr, write and fsync when changing vp->v_size.
   1079 	 * This is critical, since setting a stall smaler value
   1080 	 * triggers a file truncate in uvm_vnp_setsize(), which
   1081 	 * most of the time means data corruption (a chunk of
   1082 	 * data is replaced by zeroes). This can be removed if
   1083 	 * we decide one day that VOP_GETATTR must operate on
   1084 	 * a locked vnode.
   1085 	 *
   1086 	 * XXX Should be useless now that VOP_GETATTR has been
   1087 	 *     fixed to always require a shared lock at least.
   1088 	 */
   1089 	mutex_enter(&pn->pn_sizemtx);
   1090 
   1091 	REFPN(pn);
   1092 	vap = ap->a_vap;
   1093 
   1094 	if (PUFFS_USE_FS_TTL(pmp)) {
   1095 		if (!TIMED_OUT(pn->pn_va_timeout)) {
   1096 			update_va(vp, vap, pn->pn_va_cache,
   1097 				  NULL, NULL, SETATTR_CHSIZE);
   1098 			goto out2;
   1099 		}
   1100 	}
   1101 
   1102 	PUFFS_MSG_ALLOC(vn, getattr);
   1103 	vattr_null(&getattr_msg->pvnr_va);
   1104 	puffs_credcvt(&getattr_msg->pvnr_cred, ap->a_cred);
   1105 	puffs_msg_setinfo(park_getattr, PUFFSOP_VN,
   1106 	    PUFFS_VN_GETATTR, VPTOPNC(vp));
   1107 
   1108 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_getattr, vp->v_data, NULL, error);
   1109 	error = checkerr(pmp, error, __func__);
   1110 	if (error)
   1111 		goto out;
   1112 
   1113 	rvap = &getattr_msg->pvnr_va;
   1114 
   1115 	if (PUFFS_USE_FS_TTL(pmp))
   1116 		va_ttl = &getattr_msg->pvnr_va_ttl;
   1117 
   1118 	update_va(vp, vap, rvap, va_ttl, NULL, SETATTR_CHSIZE);
   1119 
   1120  out:
   1121 	PUFFS_MSG_RELEASE(getattr);
   1122 
   1123  out2:
   1124 	puffs_releasenode(pn);
   1125 
   1126 	mutex_exit(&pn->pn_sizemtx);
   1127 
   1128 	return error;
   1129 }
   1130 
   1131 static int
   1132 dosetattr(struct vnode *vp, struct vattr *vap, kauth_cred_t cred, int flags)
   1133 {
   1134 	PUFFS_MSG_VARS(vn, setattr);
   1135 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1136 	struct puffs_node *pn = vp->v_data;
   1137 	int error = 0;
   1138 
   1139 	KASSERT(!(flags & SETATTR_CHSIZE) || mutex_owned(&pn->pn_sizemtx));
   1140 
   1141 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) &&
   1142 	    (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL
   1143 	    || vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL
   1144 	    || vap->va_mode != (mode_t)VNOVAL))
   1145 		return EROFS;
   1146 
   1147 	if ((vp->v_mount->mnt_flag & MNT_RDONLY)
   1148 	    && vp->v_type == VREG && vap->va_size != VNOVAL)
   1149 		return EROFS;
   1150 
   1151 	/*
   1152 	 * Flush metacache first.  If we are called with some explicit
   1153 	 * parameters, treat them as information overriding metacache
   1154 	 * information.
   1155 	 */
   1156 	if (pn->pn_stat & PNODE_METACACHE_MASK) {
   1157 		if ((pn->pn_stat & PNODE_METACACHE_ATIME)
   1158 		    && vap->va_atime.tv_sec == VNOVAL)
   1159 			vap->va_atime = pn->pn_mc_atime;
   1160 		if ((pn->pn_stat & PNODE_METACACHE_CTIME)
   1161 		    && vap->va_ctime.tv_sec == VNOVAL)
   1162 			vap->va_ctime = pn->pn_mc_ctime;
   1163 		if ((pn->pn_stat & PNODE_METACACHE_MTIME)
   1164 		    && vap->va_mtime.tv_sec == VNOVAL)
   1165 			vap->va_mtime = pn->pn_mc_mtime;
   1166 		if ((pn->pn_stat & PNODE_METACACHE_SIZE)
   1167 		    && vap->va_size == VNOVAL)
   1168 			vap->va_size = pn->pn_mc_size;
   1169 
   1170 		pn->pn_stat &= ~PNODE_METACACHE_MASK;
   1171 	}
   1172 
   1173 	/*
   1174 	 * Flush attribute cache so that another thread do
   1175 	 * not get a stale value during the operation.
   1176 	 */
   1177 	if (PUFFS_USE_FS_TTL(pmp))
   1178 		pn->pn_va_timeout = 0;
   1179 
   1180 	PUFFS_MSG_ALLOC(vn, setattr);
   1181 	(void)memcpy(&setattr_msg->pvnr_va, vap, sizeof(struct vattr));
   1182 	puffs_credcvt(&setattr_msg->pvnr_cred, cred);
   1183 	puffs_msg_setinfo(park_setattr, PUFFSOP_VN,
   1184 	    PUFFS_VN_SETATTR, VPTOPNC(vp));
   1185 	if (flags & SETATTR_ASYNC)
   1186 		puffs_msg_setfaf(park_setattr);
   1187 
   1188 	puffs_msg_enqueue(pmp, park_setattr);
   1189 	if ((flags & SETATTR_ASYNC) == 0)
   1190 		error = puffs_msg_wait2(pmp, park_setattr, vp->v_data, NULL);
   1191 
   1192 	if ((error == 0) && PUFFS_USE_FS_TTL(pmp)) {
   1193 		struct timespec *va_ttl = &setattr_msg->pvnr_va_ttl;
   1194 		struct vattr *rvap = &setattr_msg->pvnr_va;
   1195 
   1196 		update_va(vp, NULL, rvap, va_ttl, NULL, flags);
   1197 	}
   1198 
   1199 	PUFFS_MSG_RELEASE(setattr);
   1200 	if ((flags & SETATTR_ASYNC) == 0) {
   1201 		error = checkerr(pmp, error, __func__);
   1202 		if (error)
   1203 			return error;
   1204 	} else {
   1205 		error = 0;
   1206 	}
   1207 
   1208 	if (vap->va_size != VNOVAL) {
   1209 		pn->pn_serversize = vap->va_size;
   1210 		if (flags & SETATTR_CHSIZE)
   1211 			uvm_vnp_setsize(vp, vap->va_size);
   1212 	}
   1213 
   1214 	return 0;
   1215 }
   1216 
   1217 int
   1218 puffs_vnop_setattr(void *v)
   1219 {
   1220 	struct vop_getattr_args /* {
   1221 		const struct vnodeop_desc *a_desc;
   1222 		struct vnode *a_vp;
   1223 		struct vattr *a_vap;
   1224 		kauth_cred_t a_cred;
   1225 	} */ *ap = v;
   1226 	struct puffs_node *pn = ap->a_vp->v_data;
   1227 	int error;
   1228 
   1229 	mutex_enter(&pn->pn_sizemtx);
   1230 	error = dosetattr(ap->a_vp, ap->a_vap, ap->a_cred, SETATTR_CHSIZE);
   1231 	mutex_exit(&pn->pn_sizemtx);
   1232 
   1233 	return error;
   1234 }
   1235 
   1236 static __inline int
   1237 doinact(struct puffs_mount *pmp, int iaflag)
   1238 {
   1239 
   1240 	if (EXISTSOP(pmp, INACTIVE))
   1241 		if (pmp->pmp_flags & PUFFS_KFLAG_IAONDEMAND)
   1242 			if (iaflag || ALLOPS(pmp))
   1243 				return 1;
   1244 			else
   1245 				return 0;
   1246 		else
   1247 			return 1;
   1248 	else
   1249 		return 0;
   1250 }
   1251 
   1252 static void
   1253 callinactive(struct puffs_mount *pmp, puffs_cookie_t ck, int iaflag)
   1254 {
   1255 	int error;
   1256 	PUFFS_MSG_VARS(vn, inactive);
   1257 
   1258 	if (doinact(pmp, iaflag)) {
   1259 		PUFFS_MSG_ALLOC(vn, inactive);
   1260 		puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
   1261 		    PUFFS_VN_INACTIVE, ck);
   1262 
   1263 		PUFFS_MSG_ENQUEUEWAIT(pmp, park_inactive, error);
   1264 		PUFFS_MSG_RELEASE(inactive);
   1265 	}
   1266 }
   1267 
   1268 /* XXX: callinactive can't setback */
   1269 int
   1270 puffs_vnop_inactive(void *v)
   1271 {
   1272 	struct vop_inactive_args /* {
   1273 		const struct vnodeop_desc *a_desc;
   1274 		struct vnode *a_vp;
   1275 	} */ *ap = v;
   1276 	PUFFS_MSG_VARS(vn, inactive);
   1277 	struct vnode *vp = ap->a_vp;
   1278 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1279 	struct puffs_node *pnode;
   1280 	bool recycle = false;
   1281 	int error;
   1282 
   1283 	pnode = vp->v_data;
   1284 	mutex_enter(&pnode->pn_sizemtx);
   1285 
   1286 	if (doinact(pmp, pnode->pn_stat & PNODE_DOINACT)) {
   1287 		flushvncache(vp, 0, 0, false);
   1288 		PUFFS_MSG_ALLOC(vn, inactive);
   1289 		puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
   1290 		    PUFFS_VN_INACTIVE, VPTOPNC(vp));
   1291 
   1292 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_inactive, vp->v_data,
   1293 		    NULL, error);
   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, error;
   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(pmp, park_poll, vp->v_data,
   1609 			    NULL, error);
   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 (vp->v_iflag & VI_XLOCK)
   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_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 	vput(dvp);
   1876 	PUFFS_MSG_RELEASE(mkdir);
   1877 	return error;
   1878 }
   1879 
   1880 static int
   1881 callrmdir(struct puffs_mount *pmp, puffs_cookie_t dck, puffs_cookie_t ck,
   1882 	struct componentname *cnp)
   1883 {
   1884 	PUFFS_MSG_VARS(vn, rmdir);
   1885 	int error;
   1886 
   1887 	PUFFS_MSG_ALLOC(vn, rmdir);
   1888 	rmdir_msg->pvnr_cookie_targ = ck;
   1889 	puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
   1890 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1891 	puffs_msg_setinfo(park_rmdir, PUFFSOP_VN, PUFFS_VN_RMDIR, dck);
   1892 
   1893 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_rmdir, error);
   1894 	PUFFS_MSG_RELEASE(rmdir);
   1895 
   1896 	return checkerr(pmp, error, __func__);
   1897 }
   1898 
   1899 int
   1900 puffs_vnop_rmdir(void *v)
   1901 {
   1902 	struct vop_rmdir_args /* {
   1903 		const struct vnodeop_desc *a_desc;
   1904 		struct vnode *a_dvp;
   1905 		struct vnode *a_vp;
   1906 		struct componentname *a_cnp;
   1907 	} */ *ap = v;
   1908 	PUFFS_MSG_VARS(vn, rmdir);
   1909 	struct vnode *dvp = ap->a_dvp;
   1910 	struct vnode *vp = ap->a_vp;
   1911 	struct puffs_node *dpn = VPTOPP(dvp);
   1912 	struct puffs_node *pn = VPTOPP(vp);
   1913 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   1914 	struct componentname *cnp = ap->a_cnp;
   1915 	int error;
   1916 
   1917 	PUFFS_MSG_ALLOC(vn, rmdir);
   1918 	rmdir_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1919 	puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
   1920 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1921 	puffs_msg_setinfo(park_rmdir, PUFFSOP_VN,
   1922 	    PUFFS_VN_RMDIR, VPTOPNC(dvp));
   1923 
   1924 	puffs_msg_enqueue(pmp, park_rmdir);
   1925 	REFPN_AND_UNLOCKVP(dvp, dpn);
   1926 	REFPN_AND_UNLOCKVP(vp, pn);
   1927 	error = puffs_msg_wait2(pmp, park_rmdir, dpn, pn);
   1928 
   1929 	PUFFS_MSG_RELEASE(rmdir);
   1930 
   1931 	/* XXX: some call cache_purge() *for both vnodes* here, investigate */
   1932 	RELEPN_AND_VP(dvp, dpn);
   1933 	RELEPN_AND_VP(vp, pn);
   1934 
   1935 	return error;
   1936 }
   1937 
   1938 int
   1939 puffs_vnop_link(void *v)
   1940 {
   1941 	struct vop_link_args /* {
   1942 		const struct vnodeop_desc *a_desc;
   1943 		struct vnode *a_dvp;
   1944 		struct vnode *a_vp;
   1945 		struct componentname *a_cnp;
   1946 	} */ *ap = v;
   1947 	PUFFS_MSG_VARS(vn, link);
   1948 	struct vnode *dvp = ap->a_dvp;
   1949 	struct vnode *vp = ap->a_vp;
   1950 	struct puffs_node *dpn = VPTOPP(dvp);
   1951 	struct puffs_node *pn = VPTOPP(vp);
   1952 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   1953 	struct componentname *cnp = ap->a_cnp;
   1954 	int error;
   1955 
   1956 	PUFFS_MSG_ALLOC(vn, link);
   1957 	link_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1958 	puffs_makecn(&link_msg->pvnr_cn, &link_msg->pvnr_cn_cred,
   1959 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1960 	puffs_msg_setinfo(park_link, PUFFSOP_VN,
   1961 	    PUFFS_VN_LINK, VPTOPNC(dvp));
   1962 
   1963 	puffs_msg_enqueue(pmp, park_link);
   1964 	REFPN_AND_UNLOCKVP(dvp, dpn);
   1965 	REFPN(pn);
   1966 	error = puffs_msg_wait2(pmp, park_link, dpn, pn);
   1967 
   1968 	PUFFS_MSG_RELEASE(link);
   1969 
   1970 	error = checkerr(pmp, error, __func__);
   1971 
   1972 	/*
   1973 	 * XXX: stay in touch with the cache.  I don't like this, but
   1974 	 * don't have a better solution either.  See also puffs_rename().
   1975 	 */
   1976 	if (error == 0)
   1977 		puffs_updatenode(pn, PUFFS_UPDATECTIME, 0);
   1978 
   1979 	RELEPN_AND_VP(dvp, dpn);
   1980 	puffs_releasenode(pn);
   1981 
   1982 	return error;
   1983 }
   1984 
   1985 int
   1986 puffs_vnop_symlink(void *v)
   1987 {
   1988 	struct vop_symlink_args /* {
   1989 		const struct vnodeop_desc *a_desc;
   1990 		struct vnode *a_dvp;
   1991 		struct vnode **a_vpp;
   1992 		struct componentname *a_cnp;
   1993 		struct vattr *a_vap;
   1994 		char *a_target;
   1995 	} */ *ap = v;
   1996 	PUFFS_MSG_VARS(vn, symlink);
   1997 	struct vnode *dvp = ap->a_dvp;
   1998 	struct puffs_node *dpn = VPTOPP(dvp);
   1999 	struct mount *mp = dvp->v_mount;
   2000 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   2001 	struct componentname *cnp = ap->a_cnp;
   2002 	int error;
   2003 
   2004 	*ap->a_vpp = NULL;
   2005 
   2006 	PUFFS_MSG_ALLOC(vn, symlink);
   2007 	puffs_makecn(&symlink_msg->pvnr_cn, &symlink_msg->pvnr_cn_cred,
   2008 		cnp, PUFFS_USE_FULLPNBUF(pmp));
   2009 	symlink_msg->pvnr_va = *ap->a_vap;
   2010 	(void)strlcpy(symlink_msg->pvnr_link, ap->a_target,
   2011 	    sizeof(symlink_msg->pvnr_link));
   2012 	puffs_msg_setinfo(park_symlink, PUFFSOP_VN,
   2013 	    PUFFS_VN_SYMLINK, VPTOPNC(dvp));
   2014 
   2015 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_symlink, dvp->v_data, NULL, error);
   2016 
   2017 	error = checkerr(pmp, error, __func__);
   2018 	if (error)
   2019 		goto out;
   2020 
   2021 	error = puffs_newnode(mp, dvp, ap->a_vpp,
   2022 	    symlink_msg->pvnr_newnode, cnp, VLNK, 0);
   2023 	if (error) {
   2024 		puffs_abortbutton(pmp, PUFFS_ABORT_SYMLINK, dpn->pn_cookie,
   2025 		    symlink_msg->pvnr_newnode, cnp);
   2026 		goto out;
   2027 	}
   2028 
   2029 	if (PUFFS_USE_FS_TTL(pmp)) {
   2030 		struct timespec *va_ttl = &symlink_msg->pvnr_va_ttl;
   2031 		struct timespec *cn_ttl = &symlink_msg->pvnr_cn_ttl;
   2032 		struct vattr *rvap = &symlink_msg->pvnr_va;
   2033 
   2034 		update_va(*ap->a_vpp, NULL, rvap,
   2035 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
   2036 	}
   2037 
   2038 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
   2039 
   2040 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   2041 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
   2042 		update_parent(*ap->a_vpp, dvp);
   2043 
   2044  out:
   2045 	vput(dvp);
   2046 	PUFFS_MSG_RELEASE(symlink);
   2047 
   2048 	return error;
   2049 }
   2050 
   2051 int
   2052 puffs_vnop_readlink(void *v)
   2053 {
   2054 	struct vop_readlink_args /* {
   2055 		const struct vnodeop_desc *a_desc;
   2056 		struct vnode *a_vp;
   2057 		struct uio *a_uio;
   2058 		kauth_cred_t a_cred;
   2059 	} */ *ap = v;
   2060 	PUFFS_MSG_VARS(vn, readlink);
   2061 	struct vnode *vp = ap->a_vp;
   2062 	struct puffs_mount *pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2063 	size_t linklen;
   2064 	int error;
   2065 
   2066 	PUFFS_MSG_ALLOC(vn, readlink);
   2067 	puffs_credcvt(&readlink_msg->pvnr_cred, ap->a_cred);
   2068 	linklen = sizeof(readlink_msg->pvnr_link);
   2069 	readlink_msg->pvnr_linklen = linklen;
   2070 	puffs_msg_setinfo(park_readlink, PUFFSOP_VN,
   2071 	    PUFFS_VN_READLINK, VPTOPNC(vp));
   2072 
   2073 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_readlink, vp->v_data, NULL, error);
   2074 	error = checkerr(pmp, error, __func__);
   2075 	if (error)
   2076 		goto out;
   2077 
   2078 	/* bad bad user file server */
   2079 	if (readlink_msg->pvnr_linklen > linklen) {
   2080 		puffs_senderr(pmp, PUFFS_ERR_READLINK, E2BIG,
   2081 		    "linklen too big", VPTOPNC(ap->a_vp));
   2082 		error = EPROTO;
   2083 		goto out;
   2084 	}
   2085 
   2086 	error = uiomove(&readlink_msg->pvnr_link, readlink_msg->pvnr_linklen,
   2087 	    ap->a_uio);
   2088  out:
   2089 	PUFFS_MSG_RELEASE(readlink);
   2090 	return error;
   2091 }
   2092 
   2093 int
   2094 puffs_vnop_rename(void *v)
   2095 {
   2096 	struct vop_rename_args /* {
   2097 		const struct vnodeop_desc *a_desc;
   2098 		struct vnode *a_fdvp;
   2099 		struct vnode *a_fvp;
   2100 		struct componentname *a_fcnp;
   2101 		struct vnode *a_tdvp;
   2102 		struct vnode *a_tvp;
   2103 		struct componentname *a_tcnp;
   2104 	} */ *ap = v;
   2105 	PUFFS_MSG_VARS(vn, rename);
   2106 	struct vnode *fdvp = ap->a_fdvp, *fvp = ap->a_fvp;
   2107 	struct vnode *tdvp = ap->a_tdvp, *tvp = ap->a_tvp;
   2108 	struct puffs_node *fpn = ap->a_fvp->v_data;
   2109 	struct puffs_mount *pmp = MPTOPUFFSMP(fdvp->v_mount);
   2110 	int error;
   2111 	bool doabort = true;
   2112 
   2113 	if ((fvp->v_mount != tdvp->v_mount) ||
   2114 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   2115 		ERROUT(EXDEV);
   2116 	}
   2117 
   2118 	PUFFS_MSG_ALLOC(vn, rename);
   2119 	rename_msg->pvnr_cookie_src = VPTOPNC(fvp);
   2120 	rename_msg->pvnr_cookie_targdir = VPTOPNC(tdvp);
   2121 	if (tvp)
   2122 		rename_msg->pvnr_cookie_targ = VPTOPNC(tvp);
   2123 	else
   2124 		rename_msg->pvnr_cookie_targ = NULL;
   2125 	puffs_makecn(&rename_msg->pvnr_cn_src, &rename_msg->pvnr_cn_src_cred,
   2126 	    ap->a_fcnp, PUFFS_USE_FULLPNBUF(pmp));
   2127 	puffs_makecn(&rename_msg->pvnr_cn_targ, &rename_msg->pvnr_cn_targ_cred,
   2128 	    ap->a_tcnp, PUFFS_USE_FULLPNBUF(pmp));
   2129 	puffs_msg_setinfo(park_rename, PUFFSOP_VN,
   2130 	    PUFFS_VN_RENAME, VPTOPNC(fdvp));
   2131 
   2132 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rename, fdvp->v_data, NULL, error);
   2133 	doabort = false;
   2134 	PUFFS_MSG_RELEASE(rename);
   2135 	error = checkerr(pmp, error, __func__);
   2136 
   2137 	/*
   2138 	 * XXX: stay in touch with the cache.  I don't like this, but
   2139 	 * don't have a better solution either.  See also puffs_link().
   2140 	 */
   2141 	if (error == 0) {
   2142 		puffs_updatenode(fpn, PUFFS_UPDATECTIME, 0);
   2143 
   2144 		if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   2145 		    (VPTOPP(fvp)->pn_parent != tdvp))
   2146 			update_parent(fvp, tdvp);
   2147 	}
   2148 
   2149 
   2150  out:
   2151 	if (doabort)
   2152 		VOP_ABORTOP(tdvp, ap->a_tcnp);
   2153 	if (tvp != NULL)
   2154 		vput(tvp);
   2155 	if (tdvp == tvp)
   2156 		vrele(tdvp);
   2157 	else
   2158 		vput(tdvp);
   2159 
   2160 	if (doabort)
   2161 		VOP_ABORTOP(fdvp, ap->a_fcnp);
   2162 	vrele(fdvp);
   2163 	vrele(fvp);
   2164 
   2165 	return error;
   2166 }
   2167 
   2168 #define RWARGS(cont, iofl, move, offset, creds)				\
   2169 	(cont)->pvnr_ioflag = (iofl);					\
   2170 	(cont)->pvnr_resid = (move);					\
   2171 	(cont)->pvnr_offset = (offset);					\
   2172 	puffs_credcvt(&(cont)->pvnr_cred, creds)
   2173 
   2174 int
   2175 puffs_vnop_read(void *v)
   2176 {
   2177 	struct vop_read_args /* {
   2178 		const struct vnodeop_desc *a_desc;
   2179 		struct vnode *a_vp;
   2180 		struct uio *a_uio;
   2181 		int a_ioflag;
   2182 		kauth_cred_t a_cred;
   2183 	} */ *ap = v;
   2184 	PUFFS_MSG_VARS(vn, read);
   2185 	struct vnode *vp = ap->a_vp;
   2186 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2187 	struct uio *uio = ap->a_uio;
   2188 	size_t tomove, argsize;
   2189 	vsize_t bytelen;
   2190 	int error;
   2191 
   2192 	read_msg = NULL;
   2193 	error = 0;
   2194 
   2195 	/* std sanity */
   2196 	if (uio->uio_resid == 0)
   2197 		return 0;
   2198 	if (uio->uio_offset < 0)
   2199 		return EINVAL;
   2200 
   2201 	if (vp->v_type == VREG && PUFFS_USE_PAGECACHE(pmp)) {
   2202 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
   2203 
   2204 		while (uio->uio_resid > 0) {
   2205 			if (vp->v_size <= uio->uio_offset) {
   2206 				break;
   2207 			}
   2208 			bytelen = MIN(uio->uio_resid,
   2209 			    vp->v_size - uio->uio_offset);
   2210 			if (bytelen == 0)
   2211 				break;
   2212 
   2213 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
   2214 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
   2215 			if (error)
   2216 				break;
   2217 		}
   2218 
   2219 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   2220 			puffs_updatenode(VPTOPP(vp), PUFFS_UPDATEATIME, 0);
   2221 	} else {
   2222 		/*
   2223 		 * in case it's not a regular file or we're operating
   2224 		 * uncached, do read in the old-fashioned style,
   2225 		 * i.e. explicit read operations
   2226 		 */
   2227 
   2228 		tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2229 		argsize = sizeof(struct puffs_vnmsg_read);
   2230 		puffs_msgmem_alloc(argsize + tomove, &park_read,
   2231 		    (void *)&read_msg, 1);
   2232 
   2233 		error = 0;
   2234 		while (uio->uio_resid > 0) {
   2235 			tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2236 			memset(read_msg, 0, argsize); /* XXX: touser KASSERT */
   2237 			RWARGS(read_msg, ap->a_ioflag, tomove,
   2238 			    uio->uio_offset, ap->a_cred);
   2239 			puffs_msg_setinfo(park_read, PUFFSOP_VN,
   2240 			    PUFFS_VN_READ, VPTOPNC(vp));
   2241 			puffs_msg_setdelta(park_read, tomove);
   2242 
   2243 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_read, vp->v_data,
   2244 			    NULL, error);
   2245 			error = checkerr(pmp, error, __func__);
   2246 			if (error)
   2247 				break;
   2248 
   2249 			if (read_msg->pvnr_resid > tomove) {
   2250 				puffs_senderr(pmp, PUFFS_ERR_READ,
   2251 				    E2BIG, "resid grew", VPTOPNC(ap->a_vp));
   2252 				error = EPROTO;
   2253 				break;
   2254 			}
   2255 
   2256 			error = uiomove(read_msg->pvnr_data,
   2257 			    tomove - read_msg->pvnr_resid, uio);
   2258 
   2259 			/*
   2260 			 * in case the file is out of juice, resid from
   2261 			 * userspace is != 0.  and the error-case is
   2262 			 * quite obvious
   2263 			 */
   2264 			if (error || read_msg->pvnr_resid)
   2265 				break;
   2266 		}
   2267 
   2268 		puffs_msgmem_release(park_read);
   2269 	}
   2270 
   2271 	return error;
   2272 }
   2273 
   2274 /*
   2275  * XXX: in case of a failure, this leaves uio in a bad state.
   2276  * We could theoretically copy the uio and iovecs and "replay"
   2277  * them the right amount after the userspace trip, but don't
   2278  * bother for now.
   2279  */
   2280 int
   2281 puffs_vnop_write(void *v)
   2282 {
   2283 	struct vop_write_args /* {
   2284 		const struct vnodeop_desc *a_desc;
   2285 		struct vnode *a_vp;
   2286 		struct uio *a_uio;
   2287 		int a_ioflag;
   2288 		kauth_cred_t a_cred;
   2289 	} */ *ap = v;
   2290 	PUFFS_MSG_VARS(vn, write);
   2291 	struct vnode *vp = ap->a_vp;
   2292 	struct puffs_node *pn = VPTOPP(vp);
   2293 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2294 	struct uio *uio = ap->a_uio;
   2295 	size_t tomove, argsize;
   2296 	off_t oldoff, newoff, origoff;
   2297 	vsize_t bytelen;
   2298 	int error, uflags;
   2299 	int ubcflags;
   2300 
   2301 	error = uflags = 0;
   2302 	write_msg = NULL;
   2303 
   2304 	mutex_enter(&pn->pn_sizemtx);
   2305 
   2306 	if (vp->v_type == VREG && PUFFS_USE_PAGECACHE(pmp)) {
   2307 		ubcflags = UBC_WRITE | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp);
   2308 
   2309 		/*
   2310 		 * userspace *should* be allowed to control this,
   2311 		 * but with UBC it's a bit unclear how to handle it
   2312 		 */
   2313 		if (ap->a_ioflag & IO_APPEND)
   2314 			uio->uio_offset = vp->v_size;
   2315 
   2316 		origoff = uio->uio_offset;
   2317 		while (uio->uio_resid > 0) {
   2318 			if (vp->v_mount->mnt_flag & MNT_RELATIME)
   2319 				uflags |= PUFFS_UPDATEATIME;
   2320 			uflags |= PUFFS_UPDATECTIME;
   2321 			uflags |= PUFFS_UPDATEMTIME;
   2322 			oldoff = uio->uio_offset;
   2323 			bytelen = uio->uio_resid;
   2324 
   2325 			newoff = oldoff + bytelen;
   2326 			if (vp->v_size < newoff) {
   2327 				uvm_vnp_setwritesize(vp, newoff);
   2328 			}
   2329 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
   2330 			    UVM_ADV_RANDOM, ubcflags);
   2331 
   2332 			/*
   2333 			 * In case of a ubc_uiomove() error,
   2334 			 * opt to not extend the file at all and
   2335 			 * return an error.  Otherwise, if we attempt
   2336 			 * to clear the memory we couldn't fault to,
   2337 			 * we might generate a kernel page fault.
   2338 			 */
   2339 			if (vp->v_size < newoff) {
   2340 				if (error == 0) {
   2341 					uflags |= PUFFS_UPDATESIZE;
   2342 					uvm_vnp_setsize(vp, newoff);
   2343 				} else {
   2344 					uvm_vnp_setwritesize(vp, vp->v_size);
   2345 				}
   2346 			}
   2347 			if (error)
   2348 				break;
   2349 
   2350 			/*
   2351 			 * If we're writing large files, flush to file server
   2352 			 * every 64k.  Otherwise we can very easily exhaust
   2353 			 * kernel and user memory, as the file server cannot
   2354 			 * really keep up with our writing speed.
   2355 			 *
   2356 			 * Note: this does *NOT* honor MNT_ASYNC, because
   2357 			 * that gives userland too much say in the kernel.
   2358 			 */
   2359 			if (oldoff >> 16 != uio->uio_offset >> 16) {
   2360 				mutex_enter(vp->v_interlock);
   2361 				error = VOP_PUTPAGES(vp, oldoff & ~0xffff,
   2362 				    uio->uio_offset & ~0xffff,
   2363 				    PGO_CLEANIT | PGO_SYNCIO);
   2364 				if (error)
   2365 					break;
   2366 			}
   2367 		}
   2368 
   2369 		/* synchronous I/O? */
   2370 		if (error == 0 && ap->a_ioflag & IO_SYNC) {
   2371 			mutex_enter(vp->v_interlock);
   2372 			error = VOP_PUTPAGES(vp, trunc_page(origoff),
   2373 			    round_page(uio->uio_offset),
   2374 			    PGO_CLEANIT | PGO_SYNCIO);
   2375 
   2376 		/* write through page cache? */
   2377 		} else if (error == 0 && pmp->pmp_flags & PUFFS_KFLAG_WTCACHE) {
   2378 			mutex_enter(vp->v_interlock);
   2379 			error = VOP_PUTPAGES(vp, trunc_page(origoff),
   2380 			    round_page(uio->uio_offset), PGO_CLEANIT);
   2381 		}
   2382 
   2383 		puffs_updatenode(VPTOPP(vp), uflags, vp->v_size);
   2384 	} else {
   2385 		/* tomove is non-increasing */
   2386 		tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2387 		argsize = sizeof(struct puffs_vnmsg_write) + tomove;
   2388 		puffs_msgmem_alloc(argsize, &park_write, (void *)&write_msg,1);
   2389 
   2390 		while (uio->uio_resid > 0) {
   2391 			/* move data to buffer */
   2392 			tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2393 			memset(write_msg, 0, argsize); /* XXX: touser KASSERT */
   2394 			RWARGS(write_msg, ap->a_ioflag, tomove,
   2395 			    uio->uio_offset, ap->a_cred);
   2396 			error = uiomove(write_msg->pvnr_data, tomove, uio);
   2397 			if (error)
   2398 				break;
   2399 
   2400 			/* move buffer to userspace */
   2401 			puffs_msg_setinfo(park_write, PUFFSOP_VN,
   2402 			    PUFFS_VN_WRITE, VPTOPNC(vp));
   2403 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_write, vp->v_data,
   2404 			    NULL, error);
   2405 			error = checkerr(pmp, error, __func__);
   2406 			if (error)
   2407 				break;
   2408 
   2409 			if (write_msg->pvnr_resid > tomove) {
   2410 				puffs_senderr(pmp, PUFFS_ERR_WRITE,
   2411 				    E2BIG, "resid grew", VPTOPNC(ap->a_vp));
   2412 				error = EPROTO;
   2413 				break;
   2414 			}
   2415 
   2416 			/* adjust file size */
   2417 			if (vp->v_size < uio->uio_offset)
   2418 				uvm_vnp_setsize(vp, uio->uio_offset);
   2419 
   2420 			/* didn't move everything?  bad userspace.  bail */
   2421 			if (write_msg->pvnr_resid != 0) {
   2422 				error = EIO;
   2423 				break;
   2424 			}
   2425 		}
   2426 		puffs_msgmem_release(park_write);
   2427 	}
   2428 
   2429 	mutex_exit(&pn->pn_sizemtx);
   2430 	return error;
   2431 }
   2432 
   2433 int
   2434 puffs_vnop_print(void *v)
   2435 {
   2436 	struct vop_print_args /* {
   2437 		struct vnode *a_vp;
   2438 	} */ *ap = v;
   2439 	PUFFS_MSG_VARS(vn, print);
   2440 	struct vnode *vp = ap->a_vp;
   2441 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2442 	struct puffs_node *pn = vp->v_data;
   2443 	int error;
   2444 
   2445 	/* kernel portion */
   2446 	printf("tag VT_PUFFS, vnode %p, puffs node: %p,\n"
   2447 	    "\tuserspace cookie: %p", vp, pn, pn->pn_cookie);
   2448 	if (vp->v_type == VFIFO)
   2449 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   2450 	printf("\n");
   2451 
   2452 	/* userspace portion */
   2453 	if (EXISTSOP(pmp, PRINT)) {
   2454 		PUFFS_MSG_ALLOC(vn, print);
   2455 		puffs_msg_setinfo(park_print, PUFFSOP_VN,
   2456 		    PUFFS_VN_PRINT, VPTOPNC(vp));
   2457 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_print, vp->v_data,
   2458 		    NULL, error);
   2459 		PUFFS_MSG_RELEASE(print);
   2460 	}
   2461 
   2462 	return 0;
   2463 }
   2464 
   2465 int
   2466 puffs_vnop_pathconf(void *v)
   2467 {
   2468 	struct vop_pathconf_args /* {
   2469 		const struct vnodeop_desc *a_desc;
   2470 		struct vnode *a_vp;
   2471 		int a_name;
   2472 		register_t *a_retval;
   2473 	} */ *ap = v;
   2474 	PUFFS_MSG_VARS(vn, pathconf);
   2475 	struct vnode *vp = ap->a_vp;
   2476 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2477 	int error;
   2478 
   2479 	PUFFS_MSG_ALLOC(vn, pathconf);
   2480 	pathconf_msg->pvnr_name = ap->a_name;
   2481 	puffs_msg_setinfo(park_pathconf, PUFFSOP_VN,
   2482 	    PUFFS_VN_PATHCONF, VPTOPNC(vp));
   2483 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_pathconf, vp->v_data, NULL, error);
   2484 	error = checkerr(pmp, error, __func__);
   2485 	if (!error)
   2486 		*ap->a_retval = pathconf_msg->pvnr_retval;
   2487 	PUFFS_MSG_RELEASE(pathconf);
   2488 
   2489 	return error;
   2490 }
   2491 
   2492 int
   2493 puffs_vnop_advlock(void *v)
   2494 {
   2495 	struct vop_advlock_args /* {
   2496 		const struct vnodeop_desc *a_desc;
   2497 		struct vnode *a_vp;
   2498 		void *a_id;
   2499 		int a_op;
   2500 		struct flock *a_fl;
   2501 		int a_flags;
   2502 	} */ *ap = v;
   2503 	PUFFS_MSG_VARS(vn, advlock);
   2504 	struct vnode *vp = ap->a_vp;
   2505 	struct puffs_node *pn = VPTOPP(vp);
   2506 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2507 	int error;
   2508 
   2509 	if (!EXISTSOP(pmp, ADVLOCK))
   2510 		return lf_advlock(ap, &pn->pn_lockf, vp->v_size);
   2511 
   2512 	PUFFS_MSG_ALLOC(vn, advlock);
   2513 	(void)memcpy(&advlock_msg->pvnr_fl, ap->a_fl,
   2514 		     sizeof(advlock_msg->pvnr_fl));
   2515 	advlock_msg->pvnr_id = ap->a_id;
   2516 	advlock_msg->pvnr_op = ap->a_op;
   2517 	advlock_msg->pvnr_flags = ap->a_flags;
   2518 	puffs_msg_setinfo(park_advlock, PUFFSOP_VN,
   2519 	    PUFFS_VN_ADVLOCK, VPTOPNC(vp));
   2520 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_advlock, vp->v_data, NULL, error);
   2521 	error = checkerr(pmp, error, __func__);
   2522 	PUFFS_MSG_RELEASE(advlock);
   2523 
   2524 	return error;
   2525 }
   2526 
   2527 int
   2528 puffs_vnop_abortop(void *v)
   2529 {
   2530 	struct vop_abortop_args /* {
   2531 		struct vnode *a_dvp;
   2532 		struct componentname *a_cnp;
   2533 	}; */ *ap = v;
   2534 	PUFFS_MSG_VARS(vn, abortop);
   2535 	struct vnode *dvp = ap->a_dvp;
   2536 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   2537 	struct componentname *cnp = ap->a_cnp;
   2538 
   2539 	if (EXISTSOP(pmp, ABORTOP)) {
   2540 		PUFFS_MSG_ALLOC(vn, abortop);
   2541 		puffs_makecn(&abortop_msg->pvnr_cn, &abortop_msg->pvnr_cn_cred,
   2542 		    cnp, PUFFS_USE_FULLPNBUF(pmp));
   2543 		puffs_msg_setfaf(park_abortop);
   2544 		puffs_msg_setinfo(park_abortop, PUFFSOP_VN,
   2545 		    PUFFS_VN_ABORTOP, VPTOPNC(dvp));
   2546 
   2547 		puffs_msg_enqueue(pmp, park_abortop);
   2548 		PUFFS_MSG_RELEASE(abortop);
   2549 	}
   2550 
   2551 	return genfs_abortop(v);
   2552 }
   2553 
   2554 #define BIOASYNC(bp) (bp->b_flags & B_ASYNC)
   2555 
   2556 /*
   2557  * This maps itself to PUFFS_VN_READ/WRITE for data transfer.
   2558  */
   2559 int
   2560 puffs_vnop_strategy(void *v)
   2561 {
   2562 	struct vop_strategy_args /* {
   2563 		const struct vnodeop_desc *a_desc;
   2564 		struct vnode *a_vp;
   2565 		struct buf *a_bp;
   2566 	} */ *ap = v;
   2567 	PUFFS_MSG_VARS(vn, rw);
   2568 	struct vnode *vp = ap->a_vp;
   2569 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2570 	struct puffs_node *pn;
   2571 	struct buf *bp;
   2572 	size_t argsize;
   2573 	size_t tomove, moved;
   2574 	int error, dofaf, cansleep, dobiodone;
   2575 
   2576 	pmp = MPTOPUFFSMP(vp->v_mount);
   2577 	bp = ap->a_bp;
   2578 	error = 0;
   2579 	dofaf = 0;
   2580 	cansleep = 0;
   2581 	pn = VPTOPP(vp);
   2582 	park_rw = NULL; /* explicit */
   2583 	dobiodone = 1;
   2584 
   2585 	if ((BUF_ISREAD(bp) && !EXISTSOP(pmp, READ))
   2586 	    || (BUF_ISWRITE(bp) && !EXISTSOP(pmp, WRITE)))
   2587 		ERROUT(EOPNOTSUPP);
   2588 
   2589 	/*
   2590 	 * Short-circuit optimization: don't flush buffer in between
   2591 	 * VOP_INACTIVE and VOP_RECLAIM in case the node has no references.
   2592 	 */
   2593 	if (pn->pn_stat & PNODE_DYING) {
   2594 		KASSERT(BUF_ISWRITE(bp));
   2595 		bp->b_resid = 0;
   2596 		goto out;
   2597 	}
   2598 
   2599 #ifdef DIAGNOSTIC
   2600 	if (bp->b_bcount > pmp->pmp_msg_maxsize - PUFFS_MSGSTRUCT_MAX)
   2601 		panic("puffs_strategy: wildly inappropriate buf bcount %d",
   2602 		    bp->b_bcount);
   2603 #endif
   2604 
   2605 	/*
   2606 	 * See explanation for the necessity of a FAF in puffs_fsync.
   2607 	 *
   2608 	 * Also, do FAF in case we're suspending.
   2609 	 * See puffs_vfsops.c:pageflush()
   2610 	 */
   2611 	if (BUF_ISWRITE(bp)) {
   2612 		mutex_enter(vp->v_interlock);
   2613 		if (vp->v_iflag & VI_XLOCK)
   2614 			dofaf = 1;
   2615 		if (pn->pn_stat & PNODE_FAF)
   2616 			dofaf = 1;
   2617 		mutex_exit(vp->v_interlock);
   2618 	}
   2619 
   2620 	cansleep = (curlwp == uvm.pagedaemon_lwp || dofaf) ? 0 : 1;
   2621 
   2622 	KASSERT(curlwp != uvm.pagedaemon_lwp || dofaf || BIOASYNC(bp));
   2623 
   2624 	/* allocate transport structure */
   2625 	tomove = PUFFS_TOMOVE(bp->b_bcount, pmp);
   2626 	argsize = sizeof(struct puffs_vnmsg_rw);
   2627 	error = puffs_msgmem_alloc(argsize + tomove, &park_rw,
   2628 	    (void *)&rw_msg, cansleep);
   2629 	if (error)
   2630 		goto out;
   2631 	RWARGS(rw_msg, 0, tomove, bp->b_blkno << DEV_BSHIFT, FSCRED);
   2632 
   2633 	/* 2x2 cases: read/write, faf/nofaf */
   2634 	if (BUF_ISREAD(bp)) {
   2635 		puffs_msg_setinfo(park_rw, PUFFSOP_VN,
   2636 		    PUFFS_VN_READ, VPTOPNC(vp));
   2637 		puffs_msg_setdelta(park_rw, tomove);
   2638 		if (BIOASYNC(bp)) {
   2639 			puffs_msg_setcall(park_rw,
   2640 			    puffs_parkdone_asyncbioread, bp);
   2641 			puffs_msg_enqueue(pmp, park_rw);
   2642 			dobiodone = 0;
   2643 		} else {
   2644 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data,
   2645 			    NULL, error);
   2646 			error = checkerr(pmp, error, __func__);
   2647 			if (error)
   2648 				goto out;
   2649 
   2650 			if (rw_msg->pvnr_resid > tomove) {
   2651 				puffs_senderr(pmp, PUFFS_ERR_READ,
   2652 				    E2BIG, "resid grew", VPTOPNC(vp));
   2653 				ERROUT(EPROTO);
   2654 			}
   2655 
   2656 			moved = tomove - rw_msg->pvnr_resid;
   2657 
   2658 			(void)memcpy(bp->b_data, rw_msg->pvnr_data, moved);
   2659 			bp->b_resid = bp->b_bcount - moved;
   2660 		}
   2661 	} else {
   2662 		puffs_msg_setinfo(park_rw, PUFFSOP_VN,
   2663 		    PUFFS_VN_WRITE, VPTOPNC(vp));
   2664 		/*
   2665 		 * make pages read-only before we write them if we want
   2666 		 * write caching info
   2667 		 */
   2668 		if (PUFFS_WCACHEINFO(pmp)) {
   2669 			struct uvm_object *uobj = &vp->v_uobj;
   2670 			int npages = (bp->b_bcount + PAGE_SIZE-1) >> PAGE_SHIFT;
   2671 			struct vm_page *vmp;
   2672 			int i;
   2673 
   2674 			for (i = 0; i < npages; i++) {
   2675 				vmp= uvm_pageratop((vaddr_t)bp->b_data
   2676 				    + (i << PAGE_SHIFT));
   2677 				DPRINTF(("puffs_strategy: write-protecting "
   2678 				    "vp %p page %p, offset %" PRId64"\n",
   2679 				    vp, vmp, vmp->offset));
   2680 				mutex_enter(uobj->vmobjlock);
   2681 				vmp->flags |= PG_RDONLY;
   2682 				pmap_page_protect(vmp, VM_PROT_READ);
   2683 				mutex_exit(uobj->vmobjlock);
   2684 			}
   2685 		}
   2686 
   2687 		(void)memcpy(&rw_msg->pvnr_data, bp->b_data, tomove);
   2688 		if (dofaf) {
   2689 			puffs_msg_setfaf(park_rw);
   2690 		} else if (BIOASYNC(bp)) {
   2691 			puffs_msg_setcall(park_rw,
   2692 			    puffs_parkdone_asyncbiowrite, bp);
   2693 			dobiodone = 0;
   2694 		}
   2695 
   2696 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data, NULL, error);
   2697 
   2698 		if (dobiodone == 0)
   2699 			goto out;
   2700 
   2701 		/*
   2702 		 * XXXXXXXX: wrong, but kernel can't survive strategy
   2703 		 * failure currently.  Here, have one more X: X.
   2704 		 */
   2705 		if (error != ENOMEM)
   2706 			error = 0;
   2707 
   2708 		error = checkerr(pmp, error, __func__);
   2709 		if (error)
   2710 			goto out;
   2711 
   2712 		if (rw_msg->pvnr_resid > tomove) {
   2713 			puffs_senderr(pmp, PUFFS_ERR_WRITE,
   2714 			    E2BIG, "resid grew", VPTOPNC(vp));
   2715 			ERROUT(EPROTO);
   2716 		}
   2717 
   2718 		/*
   2719 		 * FAF moved everything.  Frankly, we don't
   2720 		 * really have a choice.
   2721 		 */
   2722 		if (dofaf && error == 0)
   2723 			moved = tomove;
   2724 		else
   2725 			moved = tomove - rw_msg->pvnr_resid;
   2726 
   2727 		bp->b_resid = bp->b_bcount - moved;
   2728 		if (bp->b_resid != 0) {
   2729 			ERROUT(EIO);
   2730 		}
   2731 	}
   2732 
   2733  out:
   2734 	if (park_rw)
   2735 		puffs_msgmem_release(park_rw);
   2736 
   2737 	if (error)
   2738 		bp->b_error = error;
   2739 
   2740 	if (error || dobiodone)
   2741 		biodone(bp);
   2742 
   2743 	return error;
   2744 }
   2745 
   2746 int
   2747 puffs_vnop_mmap(void *v)
   2748 {
   2749 	struct vop_mmap_args /* {
   2750 		const struct vnodeop_desc *a_desc;
   2751 		struct vnode *a_vp;
   2752 		vm_prot_t a_prot;
   2753 		kauth_cred_t a_cred;
   2754 	} */ *ap = v;
   2755 	PUFFS_MSG_VARS(vn, mmap);
   2756 	struct vnode *vp = ap->a_vp;
   2757 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2758 	int error;
   2759 
   2760 	if (!PUFFS_USE_PAGECACHE(pmp))
   2761 		return genfs_eopnotsupp(v);
   2762 
   2763 	if (EXISTSOP(pmp, MMAP)) {
   2764 		PUFFS_MSG_ALLOC(vn, mmap);
   2765 		mmap_msg->pvnr_prot = ap->a_prot;
   2766 		puffs_credcvt(&mmap_msg->pvnr_cred, ap->a_cred);
   2767 		puffs_msg_setinfo(park_mmap, PUFFSOP_VN,
   2768 		    PUFFS_VN_MMAP, VPTOPNC(vp));
   2769 
   2770 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mmap, vp->v_data, NULL, error);
   2771 		error = checkerr(pmp, error, __func__);
   2772 		PUFFS_MSG_RELEASE(mmap);
   2773 	} else {
   2774 		error = genfs_mmap(v);
   2775 	}
   2776 
   2777 	return error;
   2778 }
   2779 
   2780 
   2781 /*
   2782  * The rest don't get a free trip to userspace and back, they
   2783  * have to stay within the kernel.
   2784  */
   2785 
   2786 /*
   2787  * bmap doesn't really make any sense for puffs, so just 1:1 map it.
   2788  * well, maybe somehow, somewhere, some day ....
   2789  */
   2790 int
   2791 puffs_vnop_bmap(void *v)
   2792 {
   2793 	struct vop_bmap_args /* {
   2794 		const struct vnodeop_desc *a_desc;
   2795 		struct vnode *a_vp;
   2796 		daddr_t a_bn;
   2797 		struct vnode **a_vpp;
   2798 		daddr_t *a_bnp;
   2799 		int *a_runp;
   2800 	} */ *ap = v;
   2801 	struct puffs_mount *pmp;
   2802 
   2803 	pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2804 
   2805 	if (ap->a_vpp)
   2806 		*ap->a_vpp = ap->a_vp;
   2807 	if (ap->a_bnp)
   2808 		*ap->a_bnp = ap->a_bn;
   2809 	if (ap->a_runp)
   2810 		*ap->a_runp
   2811 		    = (PUFFS_TOMOVE(pmp->pmp_msg_maxsize, pmp)>>DEV_BSHIFT) - 1;
   2812 
   2813 	return 0;
   2814 }
   2815 
   2816 /*
   2817  * Handle getpages faults in puffs.  We let genfs_getpages() do most
   2818  * of the dirty work, but we come in this route to do accounting tasks.
   2819  * If the user server has specified functions for cache notifications
   2820  * about reads and/or writes, we record which type of operation we got,
   2821  * for which page range, and proceed to issue a FAF notification to the
   2822  * server about it.
   2823  */
   2824 int
   2825 puffs_vnop_getpages(void *v)
   2826 {
   2827 	struct vop_getpages_args /* {
   2828 		const struct vnodeop_desc *a_desc;
   2829 		struct vnode *a_vp;
   2830 		voff_t a_offset;
   2831 		struct vm_page **a_m;
   2832 		int *a_count;
   2833 		int a_centeridx;
   2834 		vm_prot_t a_access_type;
   2835 		int a_advice;
   2836 		int a_flags;
   2837 	} */ *ap = v;
   2838 	struct puffs_mount *pmp;
   2839 	struct puffs_node *pn;
   2840 	struct vnode *vp;
   2841 	struct vm_page **pgs;
   2842 	struct puffs_cacheinfo *pcinfo = NULL;
   2843 	struct puffs_cacherun *pcrun;
   2844 	void *parkmem = NULL;
   2845 	size_t runsizes;
   2846 	int i, npages, si, streakon;
   2847 	int error, locked, write;
   2848 
   2849 	pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2850 	npages = *ap->a_count;
   2851 	pgs = ap->a_m;
   2852 	vp = ap->a_vp;
   2853 	pn = vp->v_data;
   2854 	locked = (ap->a_flags & PGO_LOCKED) != 0;
   2855 	write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   2856 
   2857 	/* ccg xnaht - gets Wuninitialized wrong */
   2858 	pcrun = NULL;
   2859 	runsizes = 0;
   2860 
   2861 	/*
   2862 	 * Check that we aren't trying to fault in pages which our file
   2863 	 * server doesn't know about.  This happens if we extend a file by
   2864 	 * skipping some pages and later try to fault in pages which
   2865 	 * are between pn_serversize and vp_size.  This check optimizes
   2866 	 * away the common case where a file is being extended.
   2867 	 */
   2868 	if (ap->a_offset >= pn->pn_serversize && ap->a_offset < vp->v_size) {
   2869 		struct vattr va;
   2870 
   2871 		/* try again later when we can block */
   2872 		if (locked)
   2873 			ERROUT(EBUSY);
   2874 
   2875 		mutex_exit(vp->v_interlock);
   2876 		vattr_null(&va);
   2877 		va.va_size = vp->v_size;
   2878 		error = dosetattr(vp, &va, FSCRED, 0);
   2879 		if (error)
   2880 			ERROUT(error);
   2881 		mutex_enter(vp->v_interlock);
   2882 	}
   2883 
   2884 	if (write && PUFFS_WCACHEINFO(pmp)) {
   2885 #ifdef notnowjohn
   2886 		/* allocate worst-case memory */
   2887 		runsizes = ((npages / 2) + 1) * sizeof(struct puffs_cacherun);
   2888 		KASSERT(curlwp != uvm.pagedaemon_lwp || locked);
   2889 		pcinfo = kmem_zalloc(sizeof(struct puffs_cacheinfo) + runsize,
   2890 		    locked ? KM_NOSLEEP : KM_SLEEP);
   2891 
   2892 		/*
   2893 		 * can't block if we're locked and can't mess up caching
   2894 		 * information for fs server.  so come back later, please
   2895 		 */
   2896 		if (pcinfo == NULL)
   2897 			ERROUT(ENOMEM);
   2898 
   2899 		parkmem = puffs_park_alloc(locked == 0);
   2900 		if (parkmem == NULL)
   2901 			ERROUT(ENOMEM);
   2902 
   2903 		pcrun = pcinfo->pcache_runs;
   2904 #else
   2905 		(void)parkmem;
   2906 #endif
   2907 	}
   2908 
   2909 	error = genfs_getpages(v);
   2910 	if (error)
   2911 		goto out;
   2912 
   2913 	if (PUFFS_WCACHEINFO(pmp) == 0)
   2914 		goto out;
   2915 
   2916 	/*
   2917 	 * Let's see whose fault it was and inform the user server of
   2918 	 * possibly read/written pages.  Map pages from read faults
   2919 	 * strictly read-only, since otherwise we might miss info on
   2920 	 * when the page is actually write-faulted to.
   2921 	 */
   2922 	if (!locked)
   2923 		mutex_enter(vp->v_uobj.vmobjlock);
   2924 	for (i = 0, si = 0, streakon = 0; i < npages; i++) {
   2925 		if (pgs[i] == NULL || pgs[i] == PGO_DONTCARE) {
   2926 			if (streakon && write) {
   2927 				streakon = 0;
   2928 				pcrun[si].pcache_runend
   2929 				    = trunc_page(pgs[i]->offset) + PAGE_MASK;
   2930 				si++;
   2931 			}
   2932 			continue;
   2933 		}
   2934 		if (streakon == 0 && write) {
   2935 			streakon = 1;
   2936 			pcrun[si].pcache_runstart = pgs[i]->offset;
   2937 		}
   2938 
   2939 		if (!write)
   2940 			pgs[i]->flags |= PG_RDONLY;
   2941 	}
   2942 	/* was the last page part of our streak? */
   2943 	if (streakon) {
   2944 		pcrun[si].pcache_runend
   2945 		    = trunc_page(pgs[i-1]->offset) + PAGE_MASK;
   2946 		si++;
   2947 	}
   2948 	if (!locked)
   2949 		mutex_exit(vp->v_uobj.vmobjlock);
   2950 
   2951 	KASSERT(si <= (npages / 2) + 1);
   2952 
   2953 #ifdef notnowjohn
   2954 	/* send results to userspace */
   2955 	if (write)
   2956 		puffs_cacheop(pmp, parkmem, pcinfo,
   2957 		    sizeof(struct puffs_cacheinfo) + runsizes, VPTOPNC(vp));
   2958 #endif
   2959 
   2960  out:
   2961 	if (error) {
   2962 		if (pcinfo != NULL)
   2963 			kmem_free(pcinfo,
   2964 			    sizeof(struct puffs_cacheinfo) + runsizes);
   2965 #ifdef notnowjohn
   2966 		if (parkmem != NULL)
   2967 			puffs_park_release(parkmem, 1);
   2968 #endif
   2969 	}
   2970 
   2971 	return error;
   2972 }
   2973 
   2974 /*
   2975  * Extended attribute support.
   2976  */
   2977 
   2978 int
   2979 puffs_vnop_getextattr(void *v)
   2980 {
   2981 	struct vop_getextattr_args /*
   2982 		struct vnode *a_vp;
   2983 		int a_attrnamespace;
   2984 		const char *a_name;
   2985 		struct uio *a_uio;
   2986 		size_t *a_size;
   2987 		kauth_cred_t a_cred;
   2988 	}; */ *ap = v;
   2989 	PUFFS_MSG_VARS(vn, getextattr);
   2990 	struct vnode *vp = ap->a_vp;
   2991 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2992 	int attrnamespace = ap->a_attrnamespace;
   2993 	const char *name = ap->a_name;
   2994 	struct uio *uio = ap->a_uio;
   2995 	size_t *sizep = ap->a_size;
   2996 	size_t tomove, resid;
   2997 	int error;
   2998 
   2999 	if (uio)
   3000 		resid = uio->uio_resid;
   3001 	else
   3002 		resid = 0;
   3003 
   3004 	tomove = PUFFS_TOMOVE(resid, pmp);
   3005 	if (tomove != resid) {
   3006 		error = E2BIG;
   3007 		goto out;
   3008 	}
   3009 
   3010 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_getextattr) + tomove,
   3011 	    &park_getextattr, (void *)&getextattr_msg, 1);
   3012 
   3013 	getextattr_msg->pvnr_attrnamespace = attrnamespace;
   3014 	strlcpy(getextattr_msg->pvnr_attrname, name,
   3015 	    sizeof(getextattr_msg->pvnr_attrname));
   3016 	puffs_credcvt(&getextattr_msg->pvnr_cred, ap->a_cred);
   3017 	if (sizep)
   3018 		getextattr_msg->pvnr_datasize = 1;
   3019 	getextattr_msg->pvnr_resid = tomove;
   3020 
   3021 	puffs_msg_setinfo(park_getextattr,
   3022 	    PUFFSOP_VN, PUFFS_VN_GETEXTATTR, VPTOPNC(vp));
   3023 	puffs_msg_setdelta(park_getextattr, tomove);
   3024 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_getextattr, vp->v_data, NULL, error);
   3025 
   3026 	error = checkerr(pmp, error, __func__);
   3027 	if (error)
   3028 		goto out;
   3029 
   3030 	resid = getextattr_msg->pvnr_resid;
   3031 	if (resid > tomove) {
   3032 		puffs_senderr(pmp, PUFFS_ERR_GETEXTATTR, E2BIG,
   3033 		    "resid grew", VPTOPNC(vp));
   3034 		error = EPROTO;
   3035 		goto out;
   3036 	}
   3037 
   3038 	if (sizep)
   3039 		*sizep = getextattr_msg->pvnr_datasize;
   3040 	if (uio)
   3041 		error = uiomove(getextattr_msg->pvnr_data, tomove - resid, uio);
   3042 
   3043  out:
   3044 	PUFFS_MSG_RELEASE(getextattr);
   3045 	return error;
   3046 }
   3047 
   3048 int
   3049 puffs_vnop_setextattr(void *v)
   3050 {
   3051 	struct vop_setextattr_args /* {
   3052 		struct vnode *a_vp;
   3053 		int a_attrnamespace;
   3054 		const char *a_name;
   3055 		struct uio *a_uio;
   3056 		kauth_cred_t a_cred;
   3057 	}; */ *ap = v;
   3058 	PUFFS_MSG_VARS(vn, setextattr);
   3059 	struct vnode *vp = ap->a_vp;
   3060 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3061 	int attrnamespace = ap->a_attrnamespace;
   3062 	const char *name = ap->a_name;
   3063 	struct uio *uio = ap->a_uio;
   3064 	size_t tomove, resid;
   3065 	int error;
   3066 
   3067 	if (uio)
   3068 		resid = uio->uio_resid;
   3069 	else
   3070 		resid = 0;
   3071 
   3072 	tomove = PUFFS_TOMOVE(resid, pmp);
   3073 	if (tomove != resid) {
   3074 		error = E2BIG;
   3075 		goto out;
   3076 	}
   3077 
   3078 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_setextattr) + tomove,
   3079 	    &park_setextattr, (void *)&setextattr_msg, 1);
   3080 
   3081 	setextattr_msg->pvnr_attrnamespace = attrnamespace;
   3082 	strlcpy(setextattr_msg->pvnr_attrname, name,
   3083 	    sizeof(setextattr_msg->pvnr_attrname));
   3084 	puffs_credcvt(&setextattr_msg->pvnr_cred, ap->a_cred);
   3085 	setextattr_msg->pvnr_resid = tomove;
   3086 
   3087 	if (uio) {
   3088 		error = uiomove(setextattr_msg->pvnr_data, tomove, uio);
   3089 		if (error)
   3090 			goto out;
   3091 	}
   3092 
   3093 	puffs_msg_setinfo(park_setextattr,
   3094 	    PUFFSOP_VN, PUFFS_VN_SETEXTATTR, VPTOPNC(vp));
   3095 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_setextattr, vp->v_data, NULL, error);
   3096 
   3097 	error = checkerr(pmp, error, __func__);
   3098 	if (error)
   3099 		goto out;
   3100 
   3101 	if (setextattr_msg->pvnr_resid != 0)
   3102 		error = EIO;
   3103 
   3104  out:
   3105 	PUFFS_MSG_RELEASE(setextattr);
   3106 
   3107 	return error;
   3108 }
   3109 
   3110 int
   3111 puffs_vnop_listextattr(void *v)
   3112 {
   3113 	struct vop_listextattr_args /* {
   3114 		struct vnode *a_vp;
   3115 		int a_attrnamespace;
   3116 		struct uio *a_uio;
   3117 		size_t *a_size;
   3118 		int a_flag,
   3119 		kauth_cred_t a_cred;
   3120 	}; */ *ap = v;
   3121 	PUFFS_MSG_VARS(vn, listextattr);
   3122 	struct vnode *vp = ap->a_vp;
   3123 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3124 	int attrnamespace = ap->a_attrnamespace;
   3125 	struct uio *uio = ap->a_uio;
   3126 	size_t *sizep = ap->a_size;
   3127 	int flag = ap->a_flag;
   3128 	size_t tomove, resid;
   3129 	int error;
   3130 
   3131 	if (uio)
   3132 		resid = uio->uio_resid;
   3133 	else
   3134 		resid = 0;
   3135 
   3136 	tomove = PUFFS_TOMOVE(resid, pmp);
   3137 	if (tomove != resid) {
   3138 		error = E2BIG;
   3139 		goto out;
   3140 	}
   3141 
   3142 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_listextattr) + tomove,
   3143 	    &park_listextattr, (void *)&listextattr_msg, 1);
   3144 
   3145 	listextattr_msg->pvnr_attrnamespace = attrnamespace;
   3146 	listextattr_msg->pvnr_flag = flag;
   3147 	puffs_credcvt(&listextattr_msg->pvnr_cred, ap->a_cred);
   3148 	listextattr_msg->pvnr_resid = tomove;
   3149 	if (sizep)
   3150 		listextattr_msg->pvnr_datasize = 1;
   3151 
   3152 	puffs_msg_setinfo(park_listextattr,
   3153 	    PUFFSOP_VN, PUFFS_VN_LISTEXTATTR, VPTOPNC(vp));
   3154 	puffs_msg_setdelta(park_listextattr, tomove);
   3155 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_listextattr, vp->v_data, NULL, error);
   3156 
   3157 	error = checkerr(pmp, error, __func__);
   3158 	if (error)
   3159 		goto out;
   3160 
   3161 	resid = listextattr_msg->pvnr_resid;
   3162 	if (resid > tomove) {
   3163 		puffs_senderr(pmp, PUFFS_ERR_LISTEXTATTR, E2BIG,
   3164 		    "resid grew", VPTOPNC(vp));
   3165 		error = EPROTO;
   3166 		goto out;
   3167 	}
   3168 
   3169 	if (sizep)
   3170 		*sizep = listextattr_msg->pvnr_datasize;
   3171 	if (uio)
   3172 		error = uiomove(listextattr_msg->pvnr_data, tomove-resid, uio);
   3173 
   3174  out:
   3175 	PUFFS_MSG_RELEASE(listextattr);
   3176 	return error;
   3177 }
   3178 
   3179 int
   3180 puffs_vnop_deleteextattr(void *v)
   3181 {
   3182 	struct vop_deleteextattr_args /* {
   3183 		struct vnode *a_vp;
   3184 		int a_attrnamespace;
   3185 		const char *a_name;
   3186 		kauth_cred_t a_cred;
   3187 	}; */ *ap = v;
   3188 	PUFFS_MSG_VARS(vn, deleteextattr);
   3189 	struct vnode *vp = ap->a_vp;
   3190 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3191 	int attrnamespace = ap->a_attrnamespace;
   3192 	const char *name = ap->a_name;
   3193 	int error;
   3194 
   3195 	PUFFS_MSG_ALLOC(vn, deleteextattr);
   3196 	deleteextattr_msg->pvnr_attrnamespace = attrnamespace;
   3197 	strlcpy(deleteextattr_msg->pvnr_attrname, name,
   3198 	    sizeof(deleteextattr_msg->pvnr_attrname));
   3199 	puffs_credcvt(&deleteextattr_msg->pvnr_cred, ap->a_cred);
   3200 
   3201 	puffs_msg_setinfo(park_deleteextattr,
   3202 	    PUFFSOP_VN, PUFFS_VN_DELETEEXTATTR, VPTOPNC(vp));
   3203 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_deleteextattr,
   3204 	    vp->v_data, NULL, error);
   3205 
   3206 	error = checkerr(pmp, error, __func__);
   3207 
   3208 	PUFFS_MSG_RELEASE(deleteextattr);
   3209 	return error;
   3210 }
   3211 
   3212 /*
   3213  * spec & fifo.  These call the miscfs spec and fifo vectors, but issue
   3214  * FAF update information for the puffs node first.
   3215  */
   3216 int
   3217 puffs_vnop_spec_read(void *v)
   3218 {
   3219 	struct vop_read_args /* {
   3220 		const struct vnodeop_desc *a_desc;
   3221 		struct vnode *a_vp;
   3222 		struct uio *a_uio;
   3223 		int a_ioflag;
   3224 		kauth_cred_t a_cred;
   3225 	} */ *ap = v;
   3226 
   3227 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
   3228 	return VOCALL(spec_vnodeop_p, VOFFSET(vop_read), v);
   3229 }
   3230 
   3231 int
   3232 puffs_vnop_spec_write(void *v)
   3233 {
   3234 	struct vop_write_args /* {
   3235 		const struct vnodeop_desc *a_desc;
   3236 		struct vnode *a_vp;
   3237 		struct uio *a_uio;
   3238 		int a_ioflag;
   3239 		kauth_cred_t a_cred;
   3240 	} */ *ap = v;
   3241 
   3242 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
   3243 	return VOCALL(spec_vnodeop_p, VOFFSET(vop_write), v);
   3244 }
   3245 
   3246 int
   3247 puffs_vnop_fifo_read(void *v)
   3248 {
   3249 	struct vop_read_args /* {
   3250 		const struct vnodeop_desc *a_desc;
   3251 		struct vnode *a_vp;
   3252 		struct uio *a_uio;
   3253 		int a_ioflag;
   3254 		kauth_cred_t a_cred;
   3255 	} */ *ap = v;
   3256 
   3257 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
   3258 	return VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), v);
   3259 }
   3260 
   3261 int
   3262 puffs_vnop_fifo_write(void *v)
   3263 {
   3264 	struct vop_write_args /* {
   3265 		const struct vnodeop_desc *a_desc;
   3266 		struct vnode *a_vp;
   3267 		struct uio *a_uio;
   3268 		int a_ioflag;
   3269 		kauth_cred_t a_cred;
   3270 	} */ *ap = v;
   3271 
   3272 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
   3273 	return VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), v);
   3274 }
   3275