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