Home | History | Annotate | Line # | Download | only in puffs
puffs_vnops.c revision 1.201
      1 /*	$NetBSD: puffs_vnops.c,v 1.201 2015/02/25 14:08:45 manu Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2007  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Google Summer of Code program and the Ulla Tuominen Foundation.
      8  * The Google SoC project was mentored by Bill Studenmund.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: puffs_vnops.c,v 1.201 2015/02/25 14:08:45 manu Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/buf.h>
     37 #include <sys/lockf.h>
     38 #include <sys/malloc.h>
     39 #include <sys/mount.h>
     40 #include <sys/namei.h>
     41 #include <sys/vnode.h>
     42 #include <sys/proc.h>
     43 #include <sys/kernel.h> /* For hz, hardclock_ticks */
     44 
     45 #include <uvm/uvm.h>
     46 
     47 #include <fs/puffs/puffs_msgif.h>
     48 #include <fs/puffs/puffs_sys.h>
     49 
     50 #include <miscfs/fifofs/fifo.h>
     51 #include <miscfs/genfs/genfs.h>
     52 #include <miscfs/specfs/specdev.h>
     53 
     54 int	puffs_vnop_lookup(void *);
     55 int	puffs_vnop_create(void *);
     56 int	puffs_vnop_access(void *);
     57 int	puffs_vnop_mknod(void *);
     58 int	puffs_vnop_open(void *);
     59 int	puffs_vnop_close(void *);
     60 int	puffs_vnop_getattr(void *);
     61 int	puffs_vnop_setattr(void *);
     62 int	puffs_vnop_reclaim(void *);
     63 int	puffs_vnop_readdir(void *);
     64 int	puffs_vnop_poll(void *);
     65 int	puffs_vnop_fsync(void *);
     66 int	puffs_vnop_seek(void *);
     67 int	puffs_vnop_remove(void *);
     68 int	puffs_vnop_mkdir(void *);
     69 int	puffs_vnop_rmdir(void *);
     70 int	puffs_vnop_link(void *);
     71 int	puffs_vnop_readlink(void *);
     72 int	puffs_vnop_symlink(void *);
     73 int	puffs_vnop_rename(void *);
     74 int	puffs_vnop_read(void *);
     75 int	puffs_vnop_write(void *);
     76 int	puffs_vnop_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 	char zbuf[PAGE_SIZE];
   1151 	struct iovec iov;
   1152 	struct uio uio;
   1153 	vsize_t len;
   1154 	int error;
   1155 
   1156 	if (trunc_page(off) == off)
   1157 		return;
   1158 
   1159 	if (vp->v_writecount == 0)
   1160 		return;
   1161 
   1162 	len = round_page(off) - off;
   1163 	memset(zbuf, 0, len);
   1164 
   1165 	iov.iov_base = zbuf;
   1166 	iov.iov_len = len;
   1167 	UIO_SETUP_SYSSPACE(&uio);
   1168 	uio.uio_iov = &iov;
   1169 	uio.uio_iovcnt = 1;
   1170 	uio.uio_offset = off;
   1171 	uio.uio_resid = len;
   1172 	uio.uio_rw = UIO_WRITE;
   1173 
   1174 	error = ubc_uiomove(&vp->v_uobj, &uio, len,
   1175 			    UVM_ADV_SEQUENTIAL, UBC_WRITE|UBC_UNMAP_FLAG(vp));
   1176 	if (error) {
   1177 		DPRINTF(("zero-fill 0x%" PRIxVSIZE "@0x%" PRIx64
   1178 			 " failed: error = %d\n", len, off, error));
   1179 	}
   1180 
   1181 	return;
   1182 }
   1183 
   1184 static int
   1185 dosetattr(struct vnode *vp, struct vattr *vap, kauth_cred_t cred, int flags)
   1186 {
   1187 	PUFFS_MSG_VARS(vn, setattr);
   1188 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1189 	struct puffs_node *pn = vp->v_data;
   1190 	vsize_t oldsize = vp->v_size;
   1191 	int error = 0;
   1192 
   1193 	KASSERT(!(flags & SETATTR_CHSIZE) || mutex_owned(&pn->pn_sizemtx));
   1194 
   1195 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) &&
   1196 	    (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL
   1197 	    || vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL
   1198 	    || vap->va_mode != (mode_t)VNOVAL))
   1199 		return EROFS;
   1200 
   1201 	if ((vp->v_mount->mnt_flag & MNT_RDONLY)
   1202 	    && vp->v_type == VREG && vap->va_size != VNOVAL)
   1203 		return EROFS;
   1204 
   1205 	/*
   1206 	 * Flush metacache first.  If we are called with some explicit
   1207 	 * parameters, treat them as information overriding metacache
   1208 	 * information.
   1209 	 */
   1210 	if (PUFFS_USE_METAFLUSH(pmp) && pn->pn_stat & PNODE_METACACHE_MASK) {
   1211 		if ((pn->pn_stat & PNODE_METACACHE_ATIME)
   1212 		    && vap->va_atime.tv_sec == VNOVAL)
   1213 			vap->va_atime = pn->pn_mc_atime;
   1214 		if ((pn->pn_stat & PNODE_METACACHE_CTIME)
   1215 		    && vap->va_ctime.tv_sec == VNOVAL)
   1216 			vap->va_ctime = pn->pn_mc_ctime;
   1217 		if ((pn->pn_stat & PNODE_METACACHE_MTIME)
   1218 		    && vap->va_mtime.tv_sec == VNOVAL)
   1219 			vap->va_mtime = pn->pn_mc_mtime;
   1220 		if ((pn->pn_stat & PNODE_METACACHE_SIZE)
   1221 		    && vap->va_size == VNOVAL)
   1222 			vap->va_size = pn->pn_mc_size;
   1223 
   1224 		pn->pn_stat &= ~PNODE_METACACHE_MASK;
   1225 	}
   1226 
   1227 	/*
   1228 	 * Flush attribute cache so that another thread do
   1229 	 * not get a stale value during the operation.
   1230 	 */
   1231 	if (PUFFS_USE_FS_TTL(pmp))
   1232 		pn->pn_va_timeout = 0;
   1233 
   1234 	PUFFS_MSG_ALLOC(vn, setattr);
   1235 	(void)memcpy(&setattr_msg->pvnr_va, vap, sizeof(struct vattr));
   1236 	puffs_credcvt(&setattr_msg->pvnr_cred, cred);
   1237 	puffs_msg_setinfo(park_setattr, PUFFSOP_VN,
   1238 	    PUFFS_VN_SETATTR, VPTOPNC(vp));
   1239 	if (flags & SETATTR_ASYNC)
   1240 		puffs_msg_setfaf(park_setattr);
   1241 
   1242 	puffs_msg_enqueue(pmp, park_setattr);
   1243 	if ((flags & SETATTR_ASYNC) == 0) {
   1244 		error = puffs_msg_wait2(pmp, park_setattr, vp->v_data, NULL);
   1245 
   1246 		if ((error == 0) && PUFFS_USE_FS_TTL(pmp)) {
   1247 			struct timespec *va_ttl = &setattr_msg->pvnr_va_ttl;
   1248 			struct vattr *rvap = &setattr_msg->pvnr_va;
   1249 
   1250 			update_va(vp, NULL, rvap, va_ttl, NULL, flags);
   1251 		}
   1252 	}
   1253 
   1254 	PUFFS_MSG_RELEASE(setattr);
   1255 	if ((flags & SETATTR_ASYNC) == 0) {
   1256 		error = checkerr(pmp, error, __func__);
   1257 		if (error)
   1258 			return error;
   1259 	} else {
   1260 		error = 0;
   1261 	}
   1262 
   1263 	if (vap->va_size != VNOVAL) {
   1264 		/*
   1265 		 * If we truncated the file, make sure the data beyond
   1266 		 * EOF in last page does not remain in cache, otherwise
   1267 		 * if the file is later truncated to a larger size (creating
   1268 		 * a hole), that area will not return zeroes as it
   1269 		 * should.
   1270 		 */
   1271 		if ((flags & SETATTR_CHSIZE) && PUFFS_USE_PAGECACHE(pmp) &&
   1272 		    (vap->va_size < oldsize))
   1273 			zerofill_lastpage(vp, vap->va_size);
   1274 
   1275 		pn->pn_serversize = vap->va_size;
   1276 		if (flags & SETATTR_CHSIZE)
   1277 			uvm_vnp_setsize(vp, vap->va_size);
   1278 	}
   1279 
   1280 	return 0;
   1281 }
   1282 
   1283 int
   1284 puffs_vnop_setattr(void *v)
   1285 {
   1286 	struct vop_getattr_args /* {
   1287 		const struct vnodeop_desc *a_desc;
   1288 		struct vnode *a_vp;
   1289 		struct vattr *a_vap;
   1290 		kauth_cred_t a_cred;
   1291 	} */ *ap = v;
   1292 	struct puffs_node *pn = ap->a_vp->v_data;
   1293 	int error;
   1294 
   1295 	mutex_enter(&pn->pn_sizemtx);
   1296 	error = dosetattr(ap->a_vp, ap->a_vap, ap->a_cred, SETATTR_CHSIZE);
   1297 	mutex_exit(&pn->pn_sizemtx);
   1298 
   1299 	return error;
   1300 }
   1301 
   1302 static __inline int
   1303 doinact(struct puffs_mount *pmp, int iaflag)
   1304 {
   1305 
   1306 	if (EXISTSOP(pmp, INACTIVE))
   1307 		if (pmp->pmp_flags & PUFFS_KFLAG_IAONDEMAND)
   1308 			if (iaflag || ALLOPS(pmp))
   1309 				return 1;
   1310 			else
   1311 				return 0;
   1312 		else
   1313 			return 1;
   1314 	else
   1315 		return 0;
   1316 }
   1317 
   1318 static void
   1319 callinactive(struct puffs_mount *pmp, puffs_cookie_t ck, int iaflag)
   1320 {
   1321 	PUFFS_MSG_VARS(vn, inactive);
   1322 
   1323 	if (doinact(pmp, iaflag)) {
   1324 		PUFFS_MSG_ALLOC(vn, inactive);
   1325 		puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
   1326 		    PUFFS_VN_INACTIVE, ck);
   1327 		PUFFS_MSG_ENQUEUEWAIT_NOERROR(pmp, park_inactive);
   1328 		PUFFS_MSG_RELEASE(inactive);
   1329 	}
   1330 }
   1331 
   1332 /* XXX: callinactive can't setback */
   1333 int
   1334 puffs_vnop_inactive(void *v)
   1335 {
   1336 	struct vop_inactive_args /* {
   1337 		const struct vnodeop_desc *a_desc;
   1338 		struct vnode *a_vp;
   1339 	} */ *ap = v;
   1340 	PUFFS_MSG_VARS(vn, inactive);
   1341 	struct vnode *vp = ap->a_vp;
   1342 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1343 	struct puffs_node *pnode;
   1344 	bool recycle = false;
   1345 
   1346 	/*
   1347 	 * When puffs_cookie2vnode() misses an entry, vcache_get()
   1348 	 * creates a new node (puffs_vfsop_loadvnode being called to
   1349 	 * initialize the PUFFS part), then it discovers it is VNON,
   1350 	 * and tries to vrele() it. This leads us there, while the
   1351 	 * cookie was stall and the node likely already reclaimed.
   1352 	 */
   1353 	if (vp->v_type == VNON) {
   1354 		VOP_UNLOCK(vp);
   1355 		return 0;
   1356 	}
   1357 
   1358 	pnode = vp->v_data;
   1359 	mutex_enter(&pnode->pn_sizemtx);
   1360 
   1361 	if (doinact(pmp, pnode->pn_stat & PNODE_DOINACT)) {
   1362 		flushvncache(vp, 0, 0, false);
   1363 		PUFFS_MSG_ALLOC(vn, inactive);
   1364 		puffs_msg_setinfo(park_inactive, PUFFSOP_VN,
   1365 		    PUFFS_VN_INACTIVE, VPTOPNC(vp));
   1366 		PUFFS_MSG_ENQUEUEWAIT2_NOERROR(pmp, park_inactive, vp->v_data,
   1367 		    NULL);
   1368 		PUFFS_MSG_RELEASE(inactive);
   1369 	}
   1370 	pnode->pn_stat &= ~PNODE_DOINACT;
   1371 
   1372 	/*
   1373 	 * file server thinks it's gone?  then don't be afraid care,
   1374 	 * node's life was already all it would ever be
   1375 	 */
   1376 	if (pnode->pn_stat & PNODE_NOREFS) {
   1377 		pnode->pn_stat |= PNODE_DYING;
   1378 		recycle = true;
   1379 	}
   1380 
   1381 	/*
   1382 	 * Handle node TTL.
   1383 	 * If grace has already timed out, make it reclaimed.
   1384 	 * Otherwise, we queue its expiration by sop thread, so
   1385 	 * that it does not remain for ages in the freelist,
   1386 	 * holding memory in userspace, while we will have
   1387 	 * to look it up again anyway.
   1388 	 */
   1389 	if (PUFFS_USE_FS_TTL(pmp) && !(vp->v_vflag & VV_ROOT) && !recycle) {
   1390 		bool incache = !TIMED_OUT(pnode->pn_cn_timeout);
   1391 		bool ingrace = !TIMED_OUT(pnode->pn_cn_grace);
   1392 		bool reclaimqueued = pnode->pn_stat & PNODE_SOPEXP;
   1393 
   1394 		if (!incache && !ingrace && !reclaimqueued) {
   1395 			pnode->pn_stat |= PNODE_DYING;
   1396 			recycle = true;
   1397 		}
   1398 
   1399 		if (!recycle && !reclaimqueued) {
   1400 			struct puffs_sopreq *psopr;
   1401 			int at = MAX(pnode->pn_cn_grace, pnode->pn_cn_timeout);
   1402 
   1403 			KASSERT(curlwp != uvm.pagedaemon_lwp);
   1404 			psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP);
   1405 			psopr->psopr_ck = VPTOPNC(pnode->pn_vp);
   1406 			psopr->psopr_sopreq = PUFFS_SOPREQ_EXPIRE;
   1407 			psopr->psopr_at = at;
   1408 
   1409 			mutex_enter(&pmp->pmp_sopmtx);
   1410 
   1411 			/*
   1412 			 * If thread has disapeared, just give up. The
   1413 			 * fs is being unmounted and the node will be
   1414 			 * be reclaimed anyway.
   1415 			 *
   1416 			 * Otherwise, we queue the request but do not
   1417 			 * immediatly signal the thread, as the node
   1418 			 * has not been expired yet.
   1419 			 */
   1420 			if (pmp->pmp_sopthrcount == 0) {
   1421 				kmem_free(psopr, sizeof(*psopr));
   1422 			} else {
   1423 				TAILQ_INSERT_TAIL(&pmp->pmp_sopnodereqs,
   1424 				    psopr, psopr_entries);
   1425 				pnode->pn_stat |= PNODE_SOPEXP;
   1426 			}
   1427 
   1428 			mutex_exit(&pmp->pmp_sopmtx);
   1429 		}
   1430 	}
   1431 
   1432 	/*
   1433 	 * Wipe direct I/O flags
   1434 	 */
   1435 	pnode->pn_stat &= ~(PNODE_RDIRECT|PNODE_WDIRECT);
   1436 
   1437 	*ap->a_recycle = recycle;
   1438 
   1439 	mutex_exit(&pnode->pn_sizemtx);
   1440 	VOP_UNLOCK(vp);
   1441 
   1442 	return 0;
   1443 }
   1444 
   1445 static void
   1446 callreclaim(struct puffs_mount *pmp, puffs_cookie_t ck, int nlookup)
   1447 {
   1448 	PUFFS_MSG_VARS(vn, reclaim);
   1449 
   1450 	if (!EXISTSOP(pmp, RECLAIM))
   1451 		return;
   1452 
   1453 	PUFFS_MSG_ALLOC(vn, reclaim);
   1454 	reclaim_msg->pvnr_nlookup = nlookup;
   1455 	puffs_msg_setfaf(park_reclaim);
   1456 	puffs_msg_setinfo(park_reclaim, PUFFSOP_VN, PUFFS_VN_RECLAIM, ck);
   1457 
   1458 	puffs_msg_enqueue(pmp, park_reclaim);
   1459 	PUFFS_MSG_RELEASE(reclaim);
   1460 	return;
   1461 }
   1462 
   1463 /*
   1464  * always FAF, we don't really care if the server wants to fail to
   1465  * reclaim the node or not
   1466  */
   1467 int
   1468 puffs_vnop_reclaim(void *v)
   1469 {
   1470 	struct vop_reclaim_args /* {
   1471 		const struct vnodeop_desc *a_desc;
   1472 		struct vnode *a_vp;
   1473 	} */ *ap = v;
   1474 	struct vnode *vp = ap->a_vp;
   1475 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1476 	bool notifyserver = true;
   1477 
   1478 	/*
   1479 	 * first things first: check if someone is trying to reclaim the
   1480 	 * root vnode.  do not allow that to travel to userspace.
   1481 	 * Note that we don't need to take the lock similarly to
   1482 	 * puffs_root(), since there is only one of us.
   1483 	 */
   1484 	if (vp->v_vflag & VV_ROOT) {
   1485 		mutex_enter(&pmp->pmp_lock);
   1486 		KASSERT(pmp->pmp_root != NULL);
   1487 		pmp->pmp_root = NULL;
   1488 		mutex_exit(&pmp->pmp_lock);
   1489 		notifyserver = false;
   1490 	}
   1491 
   1492 	/*
   1493 	 * purge info from kernel before issueing FAF, since we
   1494 	 * don't really know when we'll get around to it after
   1495 	 * that and someone might race us into node creation
   1496 	 */
   1497 	mutex_enter(&pmp->pmp_lock);
   1498 	if (PUFFS_USE_NAMECACHE(pmp))
   1499 		cache_purge(vp);
   1500 	mutex_exit(&pmp->pmp_lock);
   1501 
   1502 	if (notifyserver) {
   1503 		int nlookup = VPTOPP(vp)->pn_nlookup;
   1504 
   1505 		callreclaim(MPTOPUFFSMP(vp->v_mount), VPTOPNC(vp), nlookup);
   1506 	}
   1507 
   1508 	if (PUFFS_USE_DOTDOTCACHE(pmp)) {
   1509 		if (__predict_true(VPTOPP(vp)->pn_parent != NULL))
   1510 			vrele(VPTOPP(vp)->pn_parent);
   1511 		else
   1512 			KASSERT(vp->v_type == VNON || (vp->v_vflag & VV_ROOT));
   1513 	}
   1514 
   1515 	puffs_putvnode(vp);
   1516 
   1517 	return 0;
   1518 }
   1519 
   1520 #define CSIZE sizeof(**ap->a_cookies)
   1521 int
   1522 puffs_vnop_readdir(void *v)
   1523 {
   1524 	struct vop_readdir_args /* {
   1525 		const struct vnodeop_desc *a_desc;
   1526 		struct vnode *a_vp;
   1527 		struct uio *a_uio;
   1528 		kauth_cred_t a_cred;
   1529 		int *a_eofflag;
   1530 		off_t **a_cookies;
   1531 		int *a_ncookies;
   1532 	} */ *ap = v;
   1533 	PUFFS_MSG_VARS(vn, readdir);
   1534 	struct vnode *vp = ap->a_vp;
   1535 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1536 	size_t argsize, tomove, cookiemem, cookiesmax;
   1537 	struct uio *uio = ap->a_uio;
   1538 	size_t howmuch, resid;
   1539 	int error;
   1540 
   1541 	/*
   1542 	 * ok, so we need: resid + cookiemem = maxreq
   1543 	 * => resid + cookiesize * (resid/minsize) = maxreq
   1544 	 * => resid + cookiesize/minsize * resid = maxreq
   1545 	 * => (cookiesize/minsize + 1) * resid = maxreq
   1546 	 * => resid = maxreq / (cookiesize/minsize + 1)
   1547 	 *
   1548 	 * Since cookiesize <= minsize and we're not very big on floats,
   1549 	 * we approximate that to be 1.  Therefore:
   1550 	 *
   1551 	 * resid = maxreq / 2;
   1552 	 *
   1553 	 * Well, at least we didn't have to use differential equations
   1554 	 * or the Gram-Schmidt process.
   1555 	 *
   1556 	 * (yes, I'm very afraid of this)
   1557 	 */
   1558 	KASSERT(CSIZE <= _DIRENT_MINSIZE((struct dirent *)0));
   1559 
   1560 	if (ap->a_cookies) {
   1561 		KASSERT(ap->a_ncookies != NULL);
   1562 		if (pmp->pmp_args.pa_fhsize == 0)
   1563 			return EOPNOTSUPP;
   1564 		resid = PUFFS_TOMOVE(uio->uio_resid, pmp) / 2;
   1565 		cookiesmax = resid/_DIRENT_MINSIZE((struct dirent *)0);
   1566 		cookiemem = ALIGN(cookiesmax*CSIZE); /* play safe */
   1567 	} else {
   1568 		resid = PUFFS_TOMOVE(uio->uio_resid, pmp);
   1569 		cookiesmax = 0;
   1570 		cookiemem = 0;
   1571 	}
   1572 
   1573 	argsize = sizeof(struct puffs_vnmsg_readdir);
   1574 	tomove = resid + cookiemem;
   1575 	puffs_msgmem_alloc(argsize + tomove, &park_readdir,
   1576 	    (void *)&readdir_msg, 1);
   1577 
   1578 	puffs_credcvt(&readdir_msg->pvnr_cred, ap->a_cred);
   1579 	readdir_msg->pvnr_offset = uio->uio_offset;
   1580 	readdir_msg->pvnr_resid = resid;
   1581 	readdir_msg->pvnr_ncookies = cookiesmax;
   1582 	readdir_msg->pvnr_eofflag = 0;
   1583 	readdir_msg->pvnr_dentoff = cookiemem;
   1584 	puffs_msg_setinfo(park_readdir, PUFFSOP_VN,
   1585 	    PUFFS_VN_READDIR, VPTOPNC(vp));
   1586 	puffs_msg_setdelta(park_readdir, tomove);
   1587 
   1588 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_readdir, vp->v_data, NULL, error);
   1589 	error = checkerr(pmp, error, __func__);
   1590 	if (error)
   1591 		goto out;
   1592 
   1593 	/* userspace is cheating? */
   1594 	if (readdir_msg->pvnr_resid > resid) {
   1595 		puffs_senderr(pmp, PUFFS_ERR_READDIR, E2BIG,
   1596 		    "resid grew", VPTOPNC(vp));
   1597 		ERROUT(EPROTO);
   1598 	}
   1599 	if (readdir_msg->pvnr_ncookies > cookiesmax) {
   1600 		puffs_senderr(pmp, PUFFS_ERR_READDIR, E2BIG,
   1601 		    "too many cookies", VPTOPNC(vp));
   1602 		ERROUT(EPROTO);
   1603 	}
   1604 
   1605 	/* check eof */
   1606 	if (readdir_msg->pvnr_eofflag)
   1607 		*ap->a_eofflag = 1;
   1608 
   1609 	/* bouncy-wouncy with the directory data */
   1610 	howmuch = resid - readdir_msg->pvnr_resid;
   1611 
   1612 	/* force eof if no data was returned (getcwd() needs this) */
   1613 	if (howmuch == 0) {
   1614 		*ap->a_eofflag = 1;
   1615 		goto out;
   1616 	}
   1617 
   1618 	error = uiomove(readdir_msg->pvnr_data + cookiemem, howmuch, uio);
   1619 	if (error)
   1620 		goto out;
   1621 
   1622 	/* provide cookies to caller if so desired */
   1623 	if (ap->a_cookies) {
   1624 		KASSERT(curlwp != uvm.pagedaemon_lwp);
   1625 		*ap->a_cookies = malloc(readdir_msg->pvnr_ncookies*CSIZE,
   1626 		    M_TEMP, M_WAITOK);
   1627 		*ap->a_ncookies = readdir_msg->pvnr_ncookies;
   1628 		memcpy(*ap->a_cookies, readdir_msg->pvnr_data,
   1629 		    *ap->a_ncookies*CSIZE);
   1630 	}
   1631 
   1632 	/* next readdir starts here */
   1633 	uio->uio_offset = readdir_msg->pvnr_offset;
   1634 
   1635  out:
   1636 	puffs_msgmem_release(park_readdir);
   1637 	return error;
   1638 }
   1639 #undef CSIZE
   1640 
   1641 /*
   1642  * poll works by consuming the bitmask in pn_revents.  If there are
   1643  * events available, poll returns immediately.  If not, it issues a
   1644  * poll to userspace, selrecords itself and returns with no available
   1645  * events.  When the file server returns, it executes puffs_parkdone_poll(),
   1646  * where available events are added to the bitmask.  selnotify() is
   1647  * then also executed by that function causing us to enter here again
   1648  * and hopefully find the missing bits (unless someone got them first,
   1649  * in which case it starts all over again).
   1650  */
   1651 int
   1652 puffs_vnop_poll(void *v)
   1653 {
   1654 	struct vop_poll_args /* {
   1655 		const struct vnodeop_desc *a_desc;
   1656 		struct vnode *a_vp;
   1657 		int a_events;
   1658 	} */ *ap = v;
   1659 	PUFFS_MSG_VARS(vn, poll);
   1660 	struct vnode *vp = ap->a_vp;
   1661 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1662 	struct puffs_node *pn = vp->v_data;
   1663 	int events;
   1664 
   1665 	if (EXISTSOP(pmp, POLL)) {
   1666 		mutex_enter(&pn->pn_mtx);
   1667 		events = pn->pn_revents & ap->a_events;
   1668 		if (events & ap->a_events) {
   1669 			pn->pn_revents &= ~ap->a_events;
   1670 			mutex_exit(&pn->pn_mtx);
   1671 
   1672 			return events;
   1673 		} else {
   1674 			puffs_referencenode(pn);
   1675 			mutex_exit(&pn->pn_mtx);
   1676 
   1677 			PUFFS_MSG_ALLOC(vn, poll);
   1678 			poll_msg->pvnr_events = ap->a_events;
   1679 			puffs_msg_setinfo(park_poll, PUFFSOP_VN,
   1680 			    PUFFS_VN_POLL, VPTOPNC(vp));
   1681 			puffs_msg_setcall(park_poll, puffs_parkdone_poll, pn);
   1682 			selrecord(curlwp, &pn->pn_sel);
   1683 
   1684 			PUFFS_MSG_ENQUEUEWAIT2_NOERROR(pmp, park_poll,
   1685 			    vp->v_data, NULL);
   1686 			PUFFS_MSG_RELEASE(poll);
   1687 
   1688 			return 0;
   1689 		}
   1690 	} else {
   1691 		return genfs_poll(v);
   1692 	}
   1693 }
   1694 
   1695 static int
   1696 flushvncache(struct vnode *vp, off_t offlo, off_t offhi, bool wait)
   1697 {
   1698 	struct puffs_node *pn = VPTOPP(vp);
   1699 	struct vattr va;
   1700 	int pflags, error;
   1701 
   1702 	/* flush out information from our metacache, see vop_setattr */
   1703 	if (pn->pn_stat & PNODE_METACACHE_MASK
   1704 	    && (pn->pn_stat & PNODE_DYING) == 0) {
   1705 		vattr_null(&va);
   1706 		error = dosetattr(vp, &va, FSCRED,
   1707 		    SETATTR_CHSIZE | (wait ? 0 : SETATTR_ASYNC));
   1708 		if (error)
   1709 			return error;
   1710 	}
   1711 
   1712 	/*
   1713 	 * flush pages to avoid being overly dirty
   1714 	 */
   1715 	pflags = PGO_CLEANIT;
   1716 	if (wait)
   1717 		pflags |= PGO_SYNCIO;
   1718 
   1719 	mutex_enter(vp->v_interlock);
   1720 	return VOP_PUTPAGES(vp, trunc_page(offlo), round_page(offhi), pflags);
   1721 }
   1722 
   1723 int
   1724 puffs_vnop_fsync(void *v)
   1725 {
   1726 	struct vop_fsync_args /* {
   1727 		const struct vnodeop_desc *a_desc;
   1728 		struct vnode *a_vp;
   1729 		kauth_cred_t a_cred;
   1730 		int a_flags;
   1731 		off_t a_offlo;
   1732 		off_t a_offhi;
   1733 	} */ *ap = v;
   1734 	PUFFS_MSG_VARS(vn, fsync);
   1735 	struct vnode *vp;
   1736 	struct puffs_node *pn;
   1737 	struct puffs_mount *pmp;
   1738 	int error, dofaf;
   1739 
   1740 	vp = ap->a_vp;
   1741 	KASSERT(vp != NULL);
   1742 	pn = VPTOPP(vp);
   1743 	KASSERT(pn != NULL);
   1744 	pmp = MPTOPUFFSMP(vp->v_mount);
   1745 	if (ap->a_flags & FSYNC_WAIT) {
   1746 		mutex_enter(&pn->pn_sizemtx);
   1747 	} else {
   1748 		if (mutex_tryenter(&pn->pn_sizemtx) == 0)
   1749 			return EDEADLK;
   1750 	}
   1751 
   1752 	error = flushvncache(vp, ap->a_offlo, ap->a_offhi,
   1753 	    (ap->a_flags & FSYNC_WAIT) == FSYNC_WAIT);
   1754 	if (error)
   1755 		goto out;
   1756 
   1757 	/*
   1758 	 * HELLO!  We exit already here if the user server does not
   1759 	 * support fsync OR if we should call fsync for a node which
   1760 	 * has references neither in the kernel or the fs server.
   1761 	 * Otherwise we continue to issue fsync() forward.
   1762 	 */
   1763 	error = 0;
   1764 	if (!EXISTSOP(pmp, FSYNC) || (pn->pn_stat & PNODE_DYING))
   1765 		goto out;
   1766 
   1767 	dofaf = (ap->a_flags & FSYNC_WAIT) == 0 || ap->a_flags == FSYNC_LAZY;
   1768 	/*
   1769 	 * We abuse VXLOCK to mean "vnode is going to die", so we issue
   1770 	 * only FAFs for those.  Otherwise there's a danger of deadlock,
   1771 	 * since the execution context here might be the user server
   1772 	 * doing some operation on another fs, which in turn caused a
   1773 	 * vnode to be reclaimed from the freelist for this fs.
   1774 	 */
   1775 	if (dofaf == 0) {
   1776 		mutex_enter(vp->v_interlock);
   1777 		if (vdead_check(vp, VDEAD_NOWAIT) != 0)
   1778 			dofaf = 1;
   1779 		mutex_exit(vp->v_interlock);
   1780 	}
   1781 
   1782 	PUFFS_MSG_ALLOC(vn, fsync);
   1783 	if (dofaf)
   1784 		puffs_msg_setfaf(park_fsync);
   1785 
   1786 	puffs_credcvt(&fsync_msg->pvnr_cred, ap->a_cred);
   1787 	fsync_msg->pvnr_flags = ap->a_flags;
   1788 	fsync_msg->pvnr_offlo = ap->a_offlo;
   1789 	fsync_msg->pvnr_offhi = ap->a_offhi;
   1790 	puffs_msg_setinfo(park_fsync, PUFFSOP_VN,
   1791 	    PUFFS_VN_FSYNC, VPTOPNC(vp));
   1792 
   1793 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_fsync, vp->v_data, NULL, error);
   1794 	PUFFS_MSG_RELEASE(fsync);
   1795 
   1796 	error = checkerr(pmp, error, __func__);
   1797 
   1798 out:
   1799 	mutex_exit(&pn->pn_sizemtx);
   1800 	return error;
   1801 }
   1802 
   1803 int
   1804 puffs_vnop_seek(void *v)
   1805 {
   1806 	struct vop_seek_args /* {
   1807 		const struct vnodeop_desc *a_desc;
   1808 		struct vnode *a_vp;
   1809 		off_t a_oldoff;
   1810 		off_t a_newoff;
   1811 		kauth_cred_t a_cred;
   1812 	} */ *ap = v;
   1813 	PUFFS_MSG_VARS(vn, seek);
   1814 	struct vnode *vp = ap->a_vp;
   1815 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   1816 	int error;
   1817 
   1818 	PUFFS_MSG_ALLOC(vn, seek);
   1819 	seek_msg->pvnr_oldoff = ap->a_oldoff;
   1820 	seek_msg->pvnr_newoff = ap->a_newoff;
   1821 	puffs_credcvt(&seek_msg->pvnr_cred, ap->a_cred);
   1822 	puffs_msg_setinfo(park_seek, PUFFSOP_VN,
   1823 	    PUFFS_VN_SEEK, VPTOPNC(vp));
   1824 
   1825 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_seek, vp->v_data, NULL, error);
   1826 	PUFFS_MSG_RELEASE(seek);
   1827 	return checkerr(pmp, error, __func__);
   1828 }
   1829 
   1830 static int
   1831 callremove(struct puffs_mount *pmp, puffs_cookie_t dck, puffs_cookie_t ck,
   1832 	struct componentname *cnp)
   1833 {
   1834 	PUFFS_MSG_VARS(vn, remove);
   1835 	int error;
   1836 
   1837 	PUFFS_MSG_ALLOC(vn, remove);
   1838 	remove_msg->pvnr_cookie_targ = ck;
   1839 	puffs_makecn(&remove_msg->pvnr_cn, &remove_msg->pvnr_cn_cred,
   1840 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1841 	puffs_msg_setinfo(park_remove, PUFFSOP_VN, PUFFS_VN_REMOVE, dck);
   1842 
   1843 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_remove, error);
   1844 	PUFFS_MSG_RELEASE(remove);
   1845 
   1846 	return checkerr(pmp, error, __func__);
   1847 }
   1848 
   1849 /*
   1850  * XXX: can't use callremove now because can't catch setbacks with
   1851  * it due to lack of a pnode argument.
   1852  */
   1853 int
   1854 puffs_vnop_remove(void *v)
   1855 {
   1856 	struct vop_remove_args /* {
   1857 		const struct vnodeop_desc *a_desc;
   1858 		struct vnode *a_dvp;
   1859 		struct vnode *a_vp;
   1860 		struct componentname *a_cnp;
   1861 	} */ *ap = v;
   1862 	PUFFS_MSG_VARS(vn, remove);
   1863 	struct vnode *dvp = ap->a_dvp;
   1864 	struct vnode *vp = ap->a_vp;
   1865 	struct puffs_node *dpn = VPTOPP(dvp);
   1866 	struct puffs_node *pn = VPTOPP(vp);
   1867 	struct componentname *cnp = ap->a_cnp;
   1868 	struct mount *mp = dvp->v_mount;
   1869 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
   1870 	int error;
   1871 
   1872 	PUFFS_MSG_ALLOC(vn, remove);
   1873 	remove_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1874 	puffs_makecn(&remove_msg->pvnr_cn, &remove_msg->pvnr_cn_cred,
   1875 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1876 	puffs_msg_setinfo(park_remove, PUFFSOP_VN,
   1877 	    PUFFS_VN_REMOVE, VPTOPNC(dvp));
   1878 
   1879 	puffs_msg_enqueue(pmp, park_remove);
   1880 	REFPN_AND_UNLOCKVP(dvp, dpn);
   1881 	if (dvp == vp)
   1882 		REFPN(pn);
   1883 	else
   1884 		REFPN_AND_UNLOCKVP(vp, pn);
   1885 	error = puffs_msg_wait2(pmp, park_remove, dpn, pn);
   1886 
   1887 	PUFFS_MSG_RELEASE(remove);
   1888 
   1889 	puffs_updatenode(VPTOPP(dvp), PUFFS_UPDATECTIME|PUFFS_UPDATEMTIME, 0);
   1890 
   1891 	RELEPN_AND_VP(dvp, dpn);
   1892 	RELEPN_AND_VP(vp, pn);
   1893 
   1894 	error = checkerr(pmp, error, __func__);
   1895 	return error;
   1896 }
   1897 
   1898 int
   1899 puffs_vnop_mkdir(void *v)
   1900 {
   1901 	struct vop_mkdir_v3_args /* {
   1902 		const struct vnodeop_desc *a_desc;
   1903 		struct vnode *a_dvp;
   1904 		struct vnode **a_vpp;
   1905 		struct componentname *a_cnp;
   1906 		struct vattr *a_vap;
   1907 	} */ *ap = v;
   1908 	PUFFS_MSG_VARS(vn, mkdir);
   1909 	struct vnode *dvp = ap->a_dvp;
   1910 	struct puffs_node *dpn = VPTOPP(dvp);
   1911 	struct componentname *cnp = ap->a_cnp;
   1912 	struct mount *mp = dvp->v_mount;
   1913 	struct puffs_mount *pmp = MPTOPUFFSMP(mp);
   1914 	int error;
   1915 
   1916 	PUFFS_MSG_ALLOC(vn, mkdir);
   1917 	puffs_makecn(&mkdir_msg->pvnr_cn, &mkdir_msg->pvnr_cn_cred,
   1918 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1919 	mkdir_msg->pvnr_va = *ap->a_vap;
   1920 	puffs_msg_setinfo(park_mkdir, PUFFSOP_VN,
   1921 	    PUFFS_VN_MKDIR, VPTOPNC(dvp));
   1922 
   1923 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mkdir, dvp->v_data, NULL, error);
   1924 
   1925 	error = checkerr(pmp, error, __func__);
   1926 	if (error)
   1927 		goto out;
   1928 
   1929 	error = puffs_newnode(mp, dvp, ap->a_vpp,
   1930 	    mkdir_msg->pvnr_newnode, cnp, VDIR, 0);
   1931 	if (error) {
   1932 		puffs_abortbutton(pmp, PUFFS_ABORT_MKDIR, dpn->pn_cookie,
   1933 		    mkdir_msg->pvnr_newnode, cnp);
   1934 		goto out;
   1935 	}
   1936 
   1937 	if (PUFFS_USE_FS_TTL(pmp)) {
   1938 		struct timespec *va_ttl = &mkdir_msg->pvnr_va_ttl;
   1939 		struct timespec *cn_ttl = &mkdir_msg->pvnr_cn_ttl;
   1940 		struct vattr *rvap = &mkdir_msg->pvnr_va;
   1941 
   1942 		update_va(*ap->a_vpp, NULL, rvap,
   1943 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
   1944 	}
   1945 
   1946 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
   1947 
   1948 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   1949 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
   1950 		update_parent(*ap->a_vpp, dvp);
   1951 
   1952  out:
   1953 	PUFFS_MSG_RELEASE(mkdir);
   1954 	return error;
   1955 }
   1956 
   1957 static int
   1958 callrmdir(struct puffs_mount *pmp, puffs_cookie_t dck, puffs_cookie_t ck,
   1959 	struct componentname *cnp)
   1960 {
   1961 	PUFFS_MSG_VARS(vn, rmdir);
   1962 	int error;
   1963 
   1964 	PUFFS_MSG_ALLOC(vn, rmdir);
   1965 	rmdir_msg->pvnr_cookie_targ = ck;
   1966 	puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
   1967 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1968 	puffs_msg_setinfo(park_rmdir, PUFFSOP_VN, PUFFS_VN_RMDIR, dck);
   1969 
   1970 	PUFFS_MSG_ENQUEUEWAIT(pmp, park_rmdir, error);
   1971 	PUFFS_MSG_RELEASE(rmdir);
   1972 
   1973 	return checkerr(pmp, error, __func__);
   1974 }
   1975 
   1976 int
   1977 puffs_vnop_rmdir(void *v)
   1978 {
   1979 	struct vop_rmdir_args /* {
   1980 		const struct vnodeop_desc *a_desc;
   1981 		struct vnode *a_dvp;
   1982 		struct vnode *a_vp;
   1983 		struct componentname *a_cnp;
   1984 	} */ *ap = v;
   1985 	PUFFS_MSG_VARS(vn, rmdir);
   1986 	struct vnode *dvp = ap->a_dvp;
   1987 	struct vnode *vp = ap->a_vp;
   1988 	struct puffs_node *dpn = VPTOPP(dvp);
   1989 	struct puffs_node *pn = VPTOPP(vp);
   1990 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   1991 	struct componentname *cnp = ap->a_cnp;
   1992 	int error;
   1993 
   1994 	PUFFS_MSG_ALLOC(vn, rmdir);
   1995 	rmdir_msg->pvnr_cookie_targ = VPTOPNC(vp);
   1996 	puffs_makecn(&rmdir_msg->pvnr_cn, &rmdir_msg->pvnr_cn_cred,
   1997 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   1998 	puffs_msg_setinfo(park_rmdir, PUFFSOP_VN,
   1999 	    PUFFS_VN_RMDIR, VPTOPNC(dvp));
   2000 
   2001 	puffs_msg_enqueue(pmp, park_rmdir);
   2002 	REFPN_AND_UNLOCKVP(dvp, dpn);
   2003 	REFPN_AND_UNLOCKVP(vp, pn);
   2004 	error = puffs_msg_wait2(pmp, park_rmdir, dpn, pn);
   2005 
   2006 	PUFFS_MSG_RELEASE(rmdir);
   2007 
   2008 	puffs_updatenode(VPTOPP(dvp), PUFFS_UPDATECTIME|PUFFS_UPDATEMTIME, 0);
   2009 
   2010 	/* XXX: some call cache_purge() *for both vnodes* here, investigate */
   2011 	RELEPN_AND_VP(dvp, dpn);
   2012 	RELEPN_AND_VP(vp, pn);
   2013 
   2014 	return error;
   2015 }
   2016 
   2017 int
   2018 puffs_vnop_link(void *v)
   2019 {
   2020 	struct vop_link_args /* {
   2021 		const struct vnodeop_desc *a_desc;
   2022 		struct vnode *a_dvp;
   2023 		struct vnode *a_vp;
   2024 		struct componentname *a_cnp;
   2025 	} */ *ap = v;
   2026 	PUFFS_MSG_VARS(vn, link);
   2027 	struct vnode *dvp = ap->a_dvp;
   2028 	struct vnode *vp = ap->a_vp;
   2029 	struct puffs_node *dpn = VPTOPP(dvp);
   2030 	struct puffs_node *pn = VPTOPP(vp);
   2031 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   2032 	struct componentname *cnp = ap->a_cnp;
   2033 	int error;
   2034 
   2035 	PUFFS_MSG_ALLOC(vn, link);
   2036 	link_msg->pvnr_cookie_targ = VPTOPNC(vp);
   2037 	puffs_makecn(&link_msg->pvnr_cn, &link_msg->pvnr_cn_cred,
   2038 	    cnp, PUFFS_USE_FULLPNBUF(pmp));
   2039 	puffs_msg_setinfo(park_link, PUFFSOP_VN,
   2040 	    PUFFS_VN_LINK, VPTOPNC(dvp));
   2041 
   2042 	puffs_msg_enqueue(pmp, park_link);
   2043 	REFPN_AND_UNLOCKVP(dvp, dpn);
   2044 	REFPN(pn);
   2045 	error = puffs_msg_wait2(pmp, park_link, dpn, pn);
   2046 
   2047 	PUFFS_MSG_RELEASE(link);
   2048 
   2049 	error = checkerr(pmp, error, __func__);
   2050 
   2051 	/*
   2052 	 * XXX: stay in touch with the cache.  I don't like this, but
   2053 	 * don't have a better solution either.  See also puffs_rename().
   2054 	 */
   2055 	if (error == 0) {
   2056 		puffs_updatenode(pn, PUFFS_UPDATECTIME, 0);
   2057 		puffs_updatenode(VPTOPP(dvp),
   2058 				 PUFFS_UPDATECTIME|PUFFS_UPDATEMTIME, 0);
   2059 	}
   2060 
   2061 	RELEPN_AND_VP(dvp, dpn);
   2062 	puffs_releasenode(pn);
   2063 
   2064 	return error;
   2065 }
   2066 
   2067 int
   2068 puffs_vnop_symlink(void *v)
   2069 {
   2070 	struct vop_symlink_v3_args /* {
   2071 		const struct vnodeop_desc *a_desc;
   2072 		struct vnode *a_dvp;
   2073 		struct vnode **a_vpp;
   2074 		struct componentname *a_cnp;
   2075 		struct vattr *a_vap;
   2076 		char *a_target;
   2077 	} */ *ap = v;
   2078 	PUFFS_MSG_VARS(vn, symlink);
   2079 	struct vnode *dvp = ap->a_dvp;
   2080 	struct puffs_node *dpn = VPTOPP(dvp);
   2081 	struct mount *mp = dvp->v_mount;
   2082 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   2083 	struct componentname *cnp = ap->a_cnp;
   2084 	int error;
   2085 
   2086 	*ap->a_vpp = NULL;
   2087 
   2088 	PUFFS_MSG_ALLOC(vn, symlink);
   2089 	puffs_makecn(&symlink_msg->pvnr_cn, &symlink_msg->pvnr_cn_cred,
   2090 		cnp, PUFFS_USE_FULLPNBUF(pmp));
   2091 	symlink_msg->pvnr_va = *ap->a_vap;
   2092 	(void)strlcpy(symlink_msg->pvnr_link, ap->a_target,
   2093 	    sizeof(symlink_msg->pvnr_link));
   2094 	puffs_msg_setinfo(park_symlink, PUFFSOP_VN,
   2095 	    PUFFS_VN_SYMLINK, VPTOPNC(dvp));
   2096 
   2097 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_symlink, dvp->v_data, NULL, error);
   2098 
   2099 	error = checkerr(pmp, error, __func__);
   2100 	if (error)
   2101 		goto out;
   2102 
   2103 	error = puffs_newnode(mp, dvp, ap->a_vpp,
   2104 	    symlink_msg->pvnr_newnode, cnp, VLNK, 0);
   2105 	if (error) {
   2106 		puffs_abortbutton(pmp, PUFFS_ABORT_SYMLINK, dpn->pn_cookie,
   2107 		    symlink_msg->pvnr_newnode, cnp);
   2108 		goto out;
   2109 	}
   2110 
   2111 	if (PUFFS_USE_FS_TTL(pmp)) {
   2112 		struct timespec *va_ttl = &symlink_msg->pvnr_va_ttl;
   2113 		struct timespec *cn_ttl = &symlink_msg->pvnr_cn_ttl;
   2114 		struct vattr *rvap = &symlink_msg->pvnr_va;
   2115 
   2116 		update_va(*ap->a_vpp, NULL, rvap,
   2117 			  va_ttl, cn_ttl, SETATTR_CHSIZE);
   2118 	}
   2119 
   2120 	VPTOPP(*ap->a_vpp)->pn_nlookup++;
   2121 
   2122 	if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   2123 	    (VPTOPP(*ap->a_vpp)->pn_parent != dvp))
   2124 		update_parent(*ap->a_vpp, dvp);
   2125 
   2126  out:
   2127 	PUFFS_MSG_RELEASE(symlink);
   2128 
   2129 	return error;
   2130 }
   2131 
   2132 int
   2133 puffs_vnop_readlink(void *v)
   2134 {
   2135 	struct vop_readlink_args /* {
   2136 		const struct vnodeop_desc *a_desc;
   2137 		struct vnode *a_vp;
   2138 		struct uio *a_uio;
   2139 		kauth_cred_t a_cred;
   2140 	} */ *ap = v;
   2141 	PUFFS_MSG_VARS(vn, readlink);
   2142 	struct vnode *vp = ap->a_vp;
   2143 	struct puffs_mount *pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   2144 	size_t linklen;
   2145 	int error;
   2146 
   2147 	PUFFS_MSG_ALLOC(vn, readlink);
   2148 	puffs_credcvt(&readlink_msg->pvnr_cred, ap->a_cred);
   2149 	linklen = sizeof(readlink_msg->pvnr_link);
   2150 	readlink_msg->pvnr_linklen = linklen;
   2151 	puffs_msg_setinfo(park_readlink, PUFFSOP_VN,
   2152 	    PUFFS_VN_READLINK, VPTOPNC(vp));
   2153 
   2154 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_readlink, vp->v_data, NULL, error);
   2155 	error = checkerr(pmp, error, __func__);
   2156 	if (error)
   2157 		goto out;
   2158 
   2159 	/* bad bad user file server */
   2160 	if (readlink_msg->pvnr_linklen > linklen) {
   2161 		puffs_senderr(pmp, PUFFS_ERR_READLINK, E2BIG,
   2162 		    "linklen too big", VPTOPNC(ap->a_vp));
   2163 		error = EPROTO;
   2164 		goto out;
   2165 	}
   2166 
   2167 	error = uiomove(&readlink_msg->pvnr_link, readlink_msg->pvnr_linklen,
   2168 	    ap->a_uio);
   2169  out:
   2170 	PUFFS_MSG_RELEASE(readlink);
   2171 	return error;
   2172 }
   2173 
   2174 int
   2175 puffs_vnop_rename(void *v)
   2176 {
   2177 	struct vop_rename_args /* {
   2178 		const struct vnodeop_desc *a_desc;
   2179 		struct vnode *a_fdvp;
   2180 		struct vnode *a_fvp;
   2181 		struct componentname *a_fcnp;
   2182 		struct vnode *a_tdvp;
   2183 		struct vnode *a_tvp;
   2184 		struct componentname *a_tcnp;
   2185 	} */ *ap = v;
   2186 	PUFFS_MSG_VARS(vn, rename);
   2187 	struct vnode *fdvp = ap->a_fdvp, *fvp = ap->a_fvp;
   2188 	struct vnode *tdvp = ap->a_tdvp, *tvp = ap->a_tvp;
   2189 	struct puffs_node *fpn = ap->a_fvp->v_data;
   2190 	struct puffs_mount *pmp = MPTOPUFFSMP(fdvp->v_mount);
   2191 	int error;
   2192 	bool doabort = true;
   2193 
   2194 	if ((fvp->v_mount != tdvp->v_mount) ||
   2195 	    (tvp && (fvp->v_mount != tvp->v_mount))) {
   2196 		ERROUT(EXDEV);
   2197 	}
   2198 
   2199 	PUFFS_MSG_ALLOC(vn, rename);
   2200 	rename_msg->pvnr_cookie_src = VPTOPNC(fvp);
   2201 	rename_msg->pvnr_cookie_targdir = VPTOPNC(tdvp);
   2202 	if (tvp)
   2203 		rename_msg->pvnr_cookie_targ = VPTOPNC(tvp);
   2204 	else
   2205 		rename_msg->pvnr_cookie_targ = NULL;
   2206 	puffs_makecn(&rename_msg->pvnr_cn_src, &rename_msg->pvnr_cn_src_cred,
   2207 	    ap->a_fcnp, PUFFS_USE_FULLPNBUF(pmp));
   2208 	puffs_makecn(&rename_msg->pvnr_cn_targ, &rename_msg->pvnr_cn_targ_cred,
   2209 	    ap->a_tcnp, PUFFS_USE_FULLPNBUF(pmp));
   2210 	puffs_msg_setinfo(park_rename, PUFFSOP_VN,
   2211 	    PUFFS_VN_RENAME, VPTOPNC(fdvp));
   2212 
   2213 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rename, fdvp->v_data, NULL, error);
   2214 	doabort = false;
   2215 	PUFFS_MSG_RELEASE(rename);
   2216 	error = checkerr(pmp, error, __func__);
   2217 
   2218 	/*
   2219 	 * XXX: stay in touch with the cache.  I don't like this, but
   2220 	 * don't have a better solution either.  See also puffs_link().
   2221 	 */
   2222 	if (error == 0) {
   2223 		puffs_updatenode(fpn, PUFFS_UPDATECTIME, 0);
   2224 		puffs_updatenode(VPTOPP(fdvp),
   2225 				 PUFFS_UPDATECTIME|PUFFS_UPDATEMTIME, 0);
   2226 		if (fdvp != tdvp)
   2227 			puffs_updatenode(VPTOPP(tdvp),
   2228 					 PUFFS_UPDATECTIME|PUFFS_UPDATEMTIME,
   2229 					 0);
   2230 
   2231 		if (PUFFS_USE_DOTDOTCACHE(pmp) &&
   2232 		    (VPTOPP(fvp)->pn_parent != tdvp))
   2233 			update_parent(fvp, tdvp);
   2234 	}
   2235 
   2236 
   2237  out:
   2238 	if (doabort)
   2239 		VOP_ABORTOP(tdvp, ap->a_tcnp);
   2240 	if (tvp != NULL)
   2241 		vput(tvp);
   2242 	if (tdvp == tvp)
   2243 		vrele(tdvp);
   2244 	else
   2245 		vput(tdvp);
   2246 
   2247 	if (doabort)
   2248 		VOP_ABORTOP(fdvp, ap->a_fcnp);
   2249 	vrele(fdvp);
   2250 	vrele(fvp);
   2251 
   2252 	return error;
   2253 }
   2254 
   2255 #define RWARGS(cont, iofl, move, offset, creds)				\
   2256 	(cont)->pvnr_ioflag = (iofl);					\
   2257 	(cont)->pvnr_resid = (move);					\
   2258 	(cont)->pvnr_offset = (offset);					\
   2259 	puffs_credcvt(&(cont)->pvnr_cred, creds)
   2260 
   2261 int
   2262 puffs_vnop_read(void *v)
   2263 {
   2264 	struct vop_read_args /* {
   2265 		const struct vnodeop_desc *a_desc;
   2266 		struct vnode *a_vp;
   2267 		struct uio *a_uio;
   2268 		int a_ioflag;
   2269 		kauth_cred_t a_cred;
   2270 	} */ *ap = v;
   2271 	PUFFS_MSG_VARS(vn, read);
   2272 	struct vnode *vp = ap->a_vp;
   2273 	struct puffs_node *pn = VPTOPP(vp);
   2274 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2275 	struct uio *uio = ap->a_uio;
   2276 	size_t tomove, argsize;
   2277 	vsize_t bytelen;
   2278 	int error;
   2279 
   2280 	read_msg = NULL;
   2281 	error = 0;
   2282 
   2283 	/* std sanity */
   2284 	if (uio->uio_resid == 0)
   2285 		return 0;
   2286 	if (uio->uio_offset < 0)
   2287 		return EFBIG;
   2288 
   2289 	/*
   2290 	 * On the case of reading empty files and (vp->v_size != 0) below:
   2291 	 * some filesystems (hint: FUSE and distributed filesystems) still
   2292 	 * expect to get the READ in order to update atime. Reading through
   2293 	 * the case filters empty files, therefore we prefer to bypass the
   2294 	 * cache here.
   2295 	 */
   2296 	if (vp->v_type == VREG &&
   2297 	    PUFFS_USE_PAGECACHE(pmp) &&
   2298 	    !(pn->pn_stat & PNODE_RDIRECT) &&
   2299 	    (vp->v_size != 0)) {
   2300 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
   2301 
   2302 		while (uio->uio_resid > 0) {
   2303 			if (vp->v_size <= uio->uio_offset) {
   2304 				break;
   2305 			}
   2306 			bytelen = MIN(uio->uio_resid,
   2307 			    vp->v_size - uio->uio_offset);
   2308 			if (bytelen == 0)
   2309 				break;
   2310 
   2311 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
   2312 			    UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
   2313 			if (error)
   2314 				break;
   2315 		}
   2316 
   2317 		if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
   2318 			puffs_updatenode(VPTOPP(vp), PUFFS_UPDATEATIME, 0);
   2319 	} else {
   2320 		/*
   2321 		 * in case it's not a regular file or we're operating
   2322 		 * uncached, do read in the old-fashioned style,
   2323 		 * i.e. explicit read operations
   2324 		 */
   2325 
   2326 		tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2327 		argsize = sizeof(struct puffs_vnmsg_read);
   2328 		puffs_msgmem_alloc(argsize + tomove, &park_read,
   2329 		    (void *)&read_msg, 1);
   2330 
   2331 		error = 0;
   2332 		while (uio->uio_resid > 0) {
   2333 			tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2334 			memset(read_msg, 0, argsize); /* XXX: touser KASSERT */
   2335 			RWARGS(read_msg, ap->a_ioflag, tomove,
   2336 			    uio->uio_offset, ap->a_cred);
   2337 			puffs_msg_setinfo(park_read, PUFFSOP_VN,
   2338 			    PUFFS_VN_READ, VPTOPNC(vp));
   2339 			puffs_msg_setdelta(park_read, tomove);
   2340 
   2341 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_read, vp->v_data,
   2342 			    NULL, error);
   2343 			error = checkerr(pmp, error, __func__);
   2344 			if (error)
   2345 				break;
   2346 
   2347 			if (read_msg->pvnr_resid > tomove) {
   2348 				puffs_senderr(pmp, PUFFS_ERR_READ,
   2349 				    E2BIG, "resid grew", VPTOPNC(ap->a_vp));
   2350 				error = EPROTO;
   2351 				break;
   2352 			}
   2353 
   2354 			error = uiomove(read_msg->pvnr_data,
   2355 			    tomove - read_msg->pvnr_resid, uio);
   2356 
   2357 			/*
   2358 			 * in case the file is out of juice, resid from
   2359 			 * userspace is != 0.  and the error-case is
   2360 			 * quite obvious
   2361 			 */
   2362 			if (error || read_msg->pvnr_resid)
   2363 				break;
   2364 		}
   2365 
   2366 		puffs_msgmem_release(park_read);
   2367 	}
   2368 
   2369 	return error;
   2370 }
   2371 
   2372 /*
   2373  * XXX: in case of a failure, this leaves uio in a bad state.
   2374  * We could theoretically copy the uio and iovecs and "replay"
   2375  * them the right amount after the userspace trip, but don't
   2376  * bother for now.
   2377  */
   2378 int
   2379 puffs_vnop_write(void *v)
   2380 {
   2381 	struct vop_write_args /* {
   2382 		const struct vnodeop_desc *a_desc;
   2383 		struct vnode *a_vp;
   2384 		struct uio *a_uio;
   2385 		int a_ioflag;
   2386 		kauth_cred_t a_cred;
   2387 	} */ *ap = v;
   2388 	PUFFS_MSG_VARS(vn, write);
   2389 	struct vnode *vp = ap->a_vp;
   2390 	struct puffs_node *pn = VPTOPP(vp);
   2391 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2392 	struct uio *uio = ap->a_uio;
   2393 	size_t tomove, argsize;
   2394 	off_t oldoff, newoff, origoff;
   2395 	vsize_t bytelen;
   2396 	int error, uflags;
   2397 	int ubcflags;
   2398 
   2399 	error = uflags = 0;
   2400 	write_msg = NULL;
   2401 
   2402 	/* std sanity */
   2403 	if (uio->uio_resid == 0)
   2404 		return 0;
   2405 	if (uio->uio_offset < 0)
   2406 		return EFBIG;
   2407 
   2408 	mutex_enter(&pn->pn_sizemtx);
   2409 
   2410 	/*
   2411 	 * userspace *should* be allowed to control this,
   2412 	 * but with UBC it's a bit unclear how to handle it
   2413 	 */
   2414 	if (ap->a_ioflag & IO_APPEND)
   2415 		uio->uio_offset = vp->v_size;
   2416 
   2417 	origoff = uio->uio_offset;
   2418 
   2419 	if (vp->v_type == VREG &&
   2420 	    PUFFS_USE_PAGECACHE(pmp) &&
   2421 	    !(pn->pn_stat & PNODE_WDIRECT)) {
   2422 		ubcflags = UBC_WRITE | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp);
   2423 
   2424 		while (uio->uio_resid > 0) {
   2425 			oldoff = uio->uio_offset;
   2426 			bytelen = uio->uio_resid;
   2427 
   2428 			newoff = oldoff + bytelen;
   2429 			if (vp->v_size < newoff) {
   2430 				uvm_vnp_setwritesize(vp, newoff);
   2431 			}
   2432 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
   2433 			    UVM_ADV_RANDOM, ubcflags);
   2434 
   2435 			/*
   2436 			 * In case of a ubc_uiomove() error,
   2437 			 * opt to not extend the file at all and
   2438 			 * return an error.  Otherwise, if we attempt
   2439 			 * to clear the memory we couldn't fault to,
   2440 			 * we might generate a kernel page fault.
   2441 			 */
   2442 			if (vp->v_size < newoff) {
   2443 				if (error == 0) {
   2444 					uflags |= PUFFS_UPDATESIZE;
   2445 					uvm_vnp_setsize(vp, newoff);
   2446 				} else {
   2447 					uvm_vnp_setwritesize(vp, vp->v_size);
   2448 				}
   2449 			}
   2450 			if (error)
   2451 				break;
   2452 
   2453 			/*
   2454 			 * If we're writing large files, flush to file server
   2455 			 * every 64k.  Otherwise we can very easily exhaust
   2456 			 * kernel and user memory, as the file server cannot
   2457 			 * really keep up with our writing speed.
   2458 			 *
   2459 			 * Note: this does *NOT* honor MNT_ASYNC, because
   2460 			 * that gives userland too much say in the kernel.
   2461 			 */
   2462 			if (oldoff >> 16 != uio->uio_offset >> 16) {
   2463 				mutex_enter(vp->v_interlock);
   2464 				error = VOP_PUTPAGES(vp, oldoff & ~0xffff,
   2465 				    uio->uio_offset & ~0xffff,
   2466 				    PGO_CLEANIT | PGO_SYNCIO);
   2467 				if (error)
   2468 					break;
   2469 			}
   2470 		}
   2471 
   2472 		/* synchronous I/O? */
   2473 		if (error == 0 && ap->a_ioflag & IO_SYNC) {
   2474 			mutex_enter(vp->v_interlock);
   2475 			error = VOP_PUTPAGES(vp, trunc_page(origoff),
   2476 			    round_page(uio->uio_offset),
   2477 			    PGO_CLEANIT | PGO_SYNCIO);
   2478 
   2479 		/* write through page cache? */
   2480 		} else if (error == 0 && pmp->pmp_flags & PUFFS_KFLAG_WTCACHE) {
   2481 			mutex_enter(vp->v_interlock);
   2482 			error = VOP_PUTPAGES(vp, trunc_page(origoff),
   2483 			    round_page(uio->uio_offset), PGO_CLEANIT);
   2484 		}
   2485 	} else {
   2486 		/* tomove is non-increasing */
   2487 		tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2488 		argsize = sizeof(struct puffs_vnmsg_write) + tomove;
   2489 		puffs_msgmem_alloc(argsize, &park_write, (void *)&write_msg,1);
   2490 
   2491 		while (uio->uio_resid > 0) {
   2492 			/* move data to buffer */
   2493 			tomove = PUFFS_TOMOVE(uio->uio_resid, pmp);
   2494 			memset(write_msg, 0, argsize); /* XXX: touser KASSERT */
   2495 			RWARGS(write_msg, ap->a_ioflag, tomove,
   2496 			    uio->uio_offset, ap->a_cred);
   2497 			error = uiomove(write_msg->pvnr_data, tomove, uio);
   2498 			if (error)
   2499 				break;
   2500 
   2501 			/* move buffer to userspace */
   2502 			puffs_msg_setinfo(park_write, PUFFSOP_VN,
   2503 			    PUFFS_VN_WRITE, VPTOPNC(vp));
   2504 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_write, vp->v_data,
   2505 			    NULL, error);
   2506 			error = checkerr(pmp, error, __func__);
   2507 			if (error)
   2508 				break;
   2509 
   2510 			if (write_msg->pvnr_resid > tomove) {
   2511 				puffs_senderr(pmp, PUFFS_ERR_WRITE,
   2512 				    E2BIG, "resid grew", VPTOPNC(ap->a_vp));
   2513 				error = EPROTO;
   2514 				break;
   2515 			}
   2516 
   2517 			/* adjust file size */
   2518 			if (vp->v_size < uio->uio_offset) {
   2519 				uflags |= PUFFS_UPDATESIZE;
   2520 				uvm_vnp_setsize(vp, uio->uio_offset);
   2521 			}
   2522 
   2523 			/* didn't move everything?  bad userspace.  bail */
   2524 			if (write_msg->pvnr_resid != 0) {
   2525 				error = EIO;
   2526 				break;
   2527 			}
   2528 		}
   2529 		puffs_msgmem_release(park_write);
   2530 
   2531 		/*
   2532 		 * Direct I/O on write but not on read: we must
   2533 		 * invlidate the written pages so that we read
   2534 		 * the written data and not the stalled cache.
   2535 		 */
   2536 		if ((error == 0) &&
   2537 		    (vp->v_type == VREG) && PUFFS_USE_PAGECACHE(pmp) &&
   2538 		    (pn->pn_stat & PNODE_WDIRECT) &&
   2539 		    !(pn->pn_stat & PNODE_RDIRECT)) {
   2540 			voff_t off_lo = trunc_page(origoff);
   2541 			voff_t off_hi = round_page(uio->uio_offset);
   2542 
   2543 			mutex_enter(vp->v_uobj.vmobjlock);
   2544 			error = VOP_PUTPAGES(vp, off_lo, off_hi, PGO_FREE);
   2545 		}
   2546 	}
   2547 
   2548 	if (vp->v_mount->mnt_flag & MNT_RELATIME)
   2549 		uflags |= PUFFS_UPDATEATIME;
   2550 	uflags |= PUFFS_UPDATECTIME;
   2551 	uflags |= PUFFS_UPDATEMTIME;
   2552 	puffs_updatenode(VPTOPP(vp), uflags, vp->v_size);
   2553 
   2554 	/*
   2555 	 * If we do not use meta flush, we need to update the
   2556 	 * filesystem now, otherwise we will get a stale value
   2557 	 * on the next GETATTR
   2558 	 */
   2559 	if (!PUFFS_USE_METAFLUSH(pmp) && (uflags & PUFFS_UPDATESIZE)) {
   2560 		struct vattr va;
   2561 		int ret;
   2562 
   2563 		vattr_null(&va);
   2564 		va.va_size = vp->v_size;
   2565 		ret = dosetattr(vp, &va, FSCRED, 0);
   2566 		if (ret) {
   2567 			DPRINTF(("dosetattr set size to %lld failed: %d\n",
   2568 				 vp->v_size, ret));
   2569 		}
   2570 	}
   2571 	mutex_exit(&pn->pn_sizemtx);
   2572 	return error;
   2573 }
   2574 
   2575 int
   2576 puffs_vnop_fallocate(void *v)
   2577 {
   2578 	struct vop_fallocate_args /* {
   2579 		const struct vnodeop_desc *a_desc;
   2580 		struct vnode *a_vp;
   2581 		off_t a_pos;
   2582 		off_t a_len;
   2583 	} */ *ap = v;
   2584 	struct vnode *vp = ap->a_vp;
   2585 	struct puffs_node *pn = VPTOPP(vp);
   2586 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2587 	PUFFS_MSG_VARS(vn, fallocate);
   2588 	int error;
   2589 
   2590 	mutex_enter(&pn->pn_sizemtx);
   2591 
   2592 	PUFFS_MSG_ALLOC(vn, fallocate);
   2593 	fallocate_msg->pvnr_off = ap->a_pos;
   2594 	fallocate_msg->pvnr_len = ap->a_len;
   2595 	puffs_msg_setinfo(park_fallocate, PUFFSOP_VN,
   2596 	    PUFFS_VN_FALLOCATE, VPTOPNC(vp));
   2597 
   2598 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_fallocate, vp->v_data, NULL, error);
   2599 	error = checkerr(pmp, error, __func__);
   2600 	PUFFS_MSG_RELEASE(fallocate);
   2601 
   2602 	switch (error) {
   2603 	case 0:
   2604 		break;
   2605 	case EAGAIN:
   2606 		error = EIO;
   2607 		/* FALLTHROUGH */
   2608 	default:
   2609 		goto out;
   2610 	}
   2611 
   2612 	if (ap->a_pos + ap->a_len > vp->v_size) {
   2613 		uvm_vnp_setsize(vp, ap->a_pos + ap->a_len);
   2614 		puffs_updatenode(pn, PUFFS_UPDATESIZE, vp->v_size);
   2615 	}
   2616 out:
   2617  	mutex_exit(&pn->pn_sizemtx);
   2618 
   2619  	return error;
   2620 }
   2621 
   2622 int
   2623 puffs_vnop_fdiscard(void *v)
   2624 {
   2625 	struct vop_fdiscard_args /* {
   2626 		const struct vnodeop_desc *a_desc;
   2627 		struct vnode *a_vp;
   2628 		off_t a_pos;
   2629 		off_t a_len;
   2630 	} */ *ap = v;
   2631 	struct vnode *vp = ap->a_vp;
   2632 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2633 	PUFFS_MSG_VARS(vn, fdiscard);
   2634 	int error;
   2635 
   2636 	PUFFS_MSG_ALLOC(vn, fdiscard);
   2637 	fdiscard_msg->pvnr_off = ap->a_pos;
   2638 	fdiscard_msg->pvnr_len = ap->a_len;
   2639 	puffs_msg_setinfo(park_fdiscard, PUFFSOP_VN,
   2640 	    PUFFS_VN_FALLOCATE, VPTOPNC(vp));
   2641 
   2642 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_fdiscard, vp->v_data, NULL, error);
   2643 	error = checkerr(pmp, error, __func__);
   2644 	PUFFS_MSG_RELEASE(fdiscard);
   2645 
   2646  	return error;
   2647 }
   2648 
   2649 int
   2650 puffs_vnop_print(void *v)
   2651 {
   2652 	struct vop_print_args /* {
   2653 		struct vnode *a_vp;
   2654 	} */ *ap = v;
   2655 	PUFFS_MSG_VARS(vn, print);
   2656 	struct vnode *vp = ap->a_vp;
   2657 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2658 	struct puffs_node *pn = vp->v_data;
   2659 
   2660 	/* kernel portion */
   2661 	printf("tag VT_PUFFS, vnode %p, puffs node: %p,\n"
   2662 	    "\tuserspace cookie: %p", vp, pn, pn->pn_cookie);
   2663 	if (vp->v_type == VFIFO)
   2664 		VOCALL(fifo_vnodeop_p, VOFFSET(vop_print), v);
   2665 	printf("\n");
   2666 
   2667 	/* userspace portion */
   2668 	if (EXISTSOP(pmp, PRINT)) {
   2669 		PUFFS_MSG_ALLOC(vn, print);
   2670 		puffs_msg_setinfo(park_print, PUFFSOP_VN,
   2671 		    PUFFS_VN_PRINT, VPTOPNC(vp));
   2672 		PUFFS_MSG_ENQUEUEWAIT2_NOERROR(pmp, park_print, vp->v_data,
   2673 		    NULL);
   2674 		PUFFS_MSG_RELEASE(print);
   2675 	}
   2676 
   2677 	return 0;
   2678 }
   2679 
   2680 int
   2681 puffs_vnop_pathconf(void *v)
   2682 {
   2683 	struct vop_pathconf_args /* {
   2684 		const struct vnodeop_desc *a_desc;
   2685 		struct vnode *a_vp;
   2686 		int a_name;
   2687 		register_t *a_retval;
   2688 	} */ *ap = v;
   2689 	PUFFS_MSG_VARS(vn, pathconf);
   2690 	struct vnode *vp = ap->a_vp;
   2691 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2692 	int error;
   2693 
   2694 	PUFFS_MSG_ALLOC(vn, pathconf);
   2695 	pathconf_msg->pvnr_name = ap->a_name;
   2696 	puffs_msg_setinfo(park_pathconf, PUFFSOP_VN,
   2697 	    PUFFS_VN_PATHCONF, VPTOPNC(vp));
   2698 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_pathconf, vp->v_data, NULL, error);
   2699 	error = checkerr(pmp, error, __func__);
   2700 	if (!error)
   2701 		*ap->a_retval = pathconf_msg->pvnr_retval;
   2702 	PUFFS_MSG_RELEASE(pathconf);
   2703 
   2704 	return error;
   2705 }
   2706 
   2707 int
   2708 puffs_vnop_advlock(void *v)
   2709 {
   2710 	struct vop_advlock_args /* {
   2711 		const struct vnodeop_desc *a_desc;
   2712 		struct vnode *a_vp;
   2713 		void *a_id;
   2714 		int a_op;
   2715 		struct flock *a_fl;
   2716 		int a_flags;
   2717 	} */ *ap = v;
   2718 	PUFFS_MSG_VARS(vn, advlock);
   2719 	struct vnode *vp = ap->a_vp;
   2720 	struct puffs_node *pn = VPTOPP(vp);
   2721 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2722 	int error;
   2723 
   2724 	if (!EXISTSOP(pmp, ADVLOCK))
   2725 		return lf_advlock(ap, &pn->pn_lockf, vp->v_size);
   2726 
   2727 	PUFFS_MSG_ALLOC(vn, advlock);
   2728 	(void)memcpy(&advlock_msg->pvnr_fl, ap->a_fl,
   2729 		     sizeof(advlock_msg->pvnr_fl));
   2730 	advlock_msg->pvnr_id = ap->a_id;
   2731 	advlock_msg->pvnr_op = ap->a_op;
   2732 	advlock_msg->pvnr_flags = ap->a_flags;
   2733 	puffs_msg_setinfo(park_advlock, PUFFSOP_VN,
   2734 	    PUFFS_VN_ADVLOCK, VPTOPNC(vp));
   2735 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_advlock, vp->v_data, NULL, error);
   2736 	error = checkerr(pmp, error, __func__);
   2737 	PUFFS_MSG_RELEASE(advlock);
   2738 
   2739 	return error;
   2740 }
   2741 
   2742 int
   2743 puffs_vnop_abortop(void *v)
   2744 {
   2745 	struct vop_abortop_args /* {
   2746 		struct vnode *a_dvp;
   2747 		struct componentname *a_cnp;
   2748 	}; */ *ap = v;
   2749 	PUFFS_MSG_VARS(vn, abortop);
   2750 	struct vnode *dvp = ap->a_dvp;
   2751 	struct puffs_mount *pmp = MPTOPUFFSMP(dvp->v_mount);
   2752 	struct componentname *cnp = ap->a_cnp;
   2753 
   2754 	if (EXISTSOP(pmp, ABORTOP)) {
   2755 		PUFFS_MSG_ALLOC(vn, abortop);
   2756 		puffs_makecn(&abortop_msg->pvnr_cn, &abortop_msg->pvnr_cn_cred,
   2757 		    cnp, PUFFS_USE_FULLPNBUF(pmp));
   2758 		puffs_msg_setfaf(park_abortop);
   2759 		puffs_msg_setinfo(park_abortop, PUFFSOP_VN,
   2760 		    PUFFS_VN_ABORTOP, VPTOPNC(dvp));
   2761 
   2762 		puffs_msg_enqueue(pmp, park_abortop);
   2763 		PUFFS_MSG_RELEASE(abortop);
   2764 	}
   2765 
   2766 	return genfs_abortop(v);
   2767 }
   2768 
   2769 #define BIOASYNC(bp) (bp->b_flags & B_ASYNC)
   2770 
   2771 /*
   2772  * This maps itself to PUFFS_VN_READ/WRITE for data transfer.
   2773  */
   2774 int
   2775 puffs_vnop_strategy(void *v)
   2776 {
   2777 	struct vop_strategy_args /* {
   2778 		const struct vnodeop_desc *a_desc;
   2779 		struct vnode *a_vp;
   2780 		struct buf *a_bp;
   2781 	} */ *ap = v;
   2782 	PUFFS_MSG_VARS(vn, rw);
   2783 	struct vnode *vp = ap->a_vp;
   2784 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2785 	struct puffs_node *pn;
   2786 	struct buf *bp;
   2787 	size_t argsize;
   2788 	size_t tomove, moved;
   2789 	int error, dofaf, cansleep, dobiodone;
   2790 
   2791 	pmp = MPTOPUFFSMP(vp->v_mount);
   2792 	bp = ap->a_bp;
   2793 	error = 0;
   2794 	dofaf = 0;
   2795 	cansleep = 0;
   2796 	pn = VPTOPP(vp);
   2797 	park_rw = NULL; /* explicit */
   2798 	dobiodone = 1;
   2799 
   2800 	if ((BUF_ISREAD(bp) && !EXISTSOP(pmp, READ))
   2801 	    || (BUF_ISWRITE(bp) && !EXISTSOP(pmp, WRITE)))
   2802 		ERROUT(EOPNOTSUPP);
   2803 
   2804 	/*
   2805 	 * Short-circuit optimization: don't flush buffer in between
   2806 	 * VOP_INACTIVE and VOP_RECLAIM in case the node has no references.
   2807 	 */
   2808 	if (pn->pn_stat & PNODE_DYING) {
   2809 		KASSERT(BUF_ISWRITE(bp));
   2810 		bp->b_resid = 0;
   2811 		goto out;
   2812 	}
   2813 
   2814 #ifdef DIAGNOSTIC
   2815 	if (bp->b_bcount > pmp->pmp_msg_maxsize - PUFFS_MSGSTRUCT_MAX)
   2816 		panic("puffs_strategy: wildly inappropriate buf bcount %d",
   2817 		    bp->b_bcount);
   2818 #endif
   2819 
   2820 	/*
   2821 	 * See explanation for the necessity of a FAF in puffs_fsync.
   2822 	 *
   2823 	 * Also, do FAF in case we're suspending.
   2824 	 * See puffs_vfsops.c:pageflush()
   2825 	 */
   2826 	if (BUF_ISWRITE(bp)) {
   2827 		mutex_enter(vp->v_interlock);
   2828 		if (vdead_check(vp, VDEAD_NOWAIT) != 0)
   2829 			dofaf = 1;
   2830 		if (pn->pn_stat & PNODE_FAF)
   2831 			dofaf = 1;
   2832 		mutex_exit(vp->v_interlock);
   2833 	}
   2834 
   2835 	cansleep = (curlwp == uvm.pagedaemon_lwp || dofaf) ? 0 : 1;
   2836 
   2837 	KASSERT(curlwp != uvm.pagedaemon_lwp || dofaf || BIOASYNC(bp));
   2838 
   2839 	/* allocate transport structure */
   2840 	tomove = PUFFS_TOMOVE(bp->b_bcount, pmp);
   2841 	argsize = sizeof(struct puffs_vnmsg_rw);
   2842 	error = puffs_msgmem_alloc(argsize + tomove, &park_rw,
   2843 	    (void *)&rw_msg, cansleep);
   2844 	if (error)
   2845 		goto out;
   2846 	RWARGS(rw_msg, 0, tomove, bp->b_blkno << DEV_BSHIFT, FSCRED);
   2847 
   2848 	/* 2x2 cases: read/write, faf/nofaf */
   2849 	if (BUF_ISREAD(bp)) {
   2850 		puffs_msg_setinfo(park_rw, PUFFSOP_VN,
   2851 		    PUFFS_VN_READ, VPTOPNC(vp));
   2852 		puffs_msg_setdelta(park_rw, tomove);
   2853 		if (BIOASYNC(bp)) {
   2854 			puffs_msg_setcall(park_rw,
   2855 			    puffs_parkdone_asyncbioread, bp);
   2856 			puffs_msg_enqueue(pmp, park_rw);
   2857 			dobiodone = 0;
   2858 		} else {
   2859 			PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data,
   2860 			    NULL, error);
   2861 			error = checkerr(pmp, error, __func__);
   2862 			if (error)
   2863 				goto out;
   2864 
   2865 			if (rw_msg->pvnr_resid > tomove) {
   2866 				puffs_senderr(pmp, PUFFS_ERR_READ,
   2867 				    E2BIG, "resid grew", VPTOPNC(vp));
   2868 				ERROUT(EPROTO);
   2869 			}
   2870 
   2871 			moved = tomove - rw_msg->pvnr_resid;
   2872 
   2873 			(void)memcpy(bp->b_data, rw_msg->pvnr_data, moved);
   2874 			bp->b_resid = bp->b_bcount - moved;
   2875 		}
   2876 	} else {
   2877 		puffs_msg_setinfo(park_rw, PUFFSOP_VN,
   2878 		    PUFFS_VN_WRITE, VPTOPNC(vp));
   2879 		/*
   2880 		 * make pages read-only before we write them if we want
   2881 		 * write caching info
   2882 		 */
   2883 		if (PUFFS_WCACHEINFO(pmp)) {
   2884 			struct uvm_object *uobj = &vp->v_uobj;
   2885 			int npages = (bp->b_bcount + PAGE_SIZE-1) >> PAGE_SHIFT;
   2886 			struct vm_page *vmp;
   2887 			int i;
   2888 
   2889 			for (i = 0; i < npages; i++) {
   2890 				vmp= uvm_pageratop((vaddr_t)bp->b_data
   2891 				    + (i << PAGE_SHIFT));
   2892 				DPRINTF(("puffs_strategy: write-protecting "
   2893 				    "vp %p page %p, offset %" PRId64"\n",
   2894 				    vp, vmp, vmp->offset));
   2895 				mutex_enter(uobj->vmobjlock);
   2896 				vmp->flags |= PG_RDONLY;
   2897 				pmap_page_protect(vmp, VM_PROT_READ);
   2898 				mutex_exit(uobj->vmobjlock);
   2899 			}
   2900 		}
   2901 
   2902 		(void)memcpy(&rw_msg->pvnr_data, bp->b_data, tomove);
   2903 		if (dofaf) {
   2904 			puffs_msg_setfaf(park_rw);
   2905 		} else if (BIOASYNC(bp)) {
   2906 			puffs_msg_setcall(park_rw,
   2907 			    puffs_parkdone_asyncbiowrite, bp);
   2908 			dobiodone = 0;
   2909 		}
   2910 
   2911 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_rw, vp->v_data, NULL, error);
   2912 
   2913 		if (dobiodone == 0)
   2914 			goto out;
   2915 
   2916 		error = checkerr(pmp, error, __func__);
   2917 		if (error)
   2918 			goto out;
   2919 
   2920 		if (rw_msg->pvnr_resid > tomove) {
   2921 			puffs_senderr(pmp, PUFFS_ERR_WRITE,
   2922 			    E2BIG, "resid grew", VPTOPNC(vp));
   2923 			ERROUT(EPROTO);
   2924 		}
   2925 
   2926 		/*
   2927 		 * FAF moved everything.  Frankly, we don't
   2928 		 * really have a choice.
   2929 		 */
   2930 		if (dofaf && error == 0)
   2931 			moved = tomove;
   2932 		else
   2933 			moved = tomove - rw_msg->pvnr_resid;
   2934 
   2935 		bp->b_resid = bp->b_bcount - moved;
   2936 		if (bp->b_resid != 0) {
   2937 			ERROUT(EIO);
   2938 		}
   2939 	}
   2940 
   2941  out:
   2942 	if (park_rw)
   2943 		puffs_msgmem_release(park_rw);
   2944 
   2945 	if (error)
   2946 		bp->b_error = error;
   2947 
   2948 	if (error || dobiodone)
   2949 		biodone(bp);
   2950 
   2951 	return error;
   2952 }
   2953 
   2954 int
   2955 puffs_vnop_mmap(void *v)
   2956 {
   2957 	struct vop_mmap_args /* {
   2958 		const struct vnodeop_desc *a_desc;
   2959 		struct vnode *a_vp;
   2960 		vm_prot_t a_prot;
   2961 		kauth_cred_t a_cred;
   2962 	} */ *ap = v;
   2963 	PUFFS_MSG_VARS(vn, mmap);
   2964 	struct vnode *vp = ap->a_vp;
   2965 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   2966 	int error;
   2967 
   2968 	if (!PUFFS_USE_PAGECACHE(pmp))
   2969 		return genfs_eopnotsupp(v);
   2970 
   2971 	if (EXISTSOP(pmp, MMAP)) {
   2972 		PUFFS_MSG_ALLOC(vn, mmap);
   2973 		mmap_msg->pvnr_prot = ap->a_prot;
   2974 		puffs_credcvt(&mmap_msg->pvnr_cred, ap->a_cred);
   2975 		puffs_msg_setinfo(park_mmap, PUFFSOP_VN,
   2976 		    PUFFS_VN_MMAP, VPTOPNC(vp));
   2977 
   2978 		PUFFS_MSG_ENQUEUEWAIT2(pmp, park_mmap, vp->v_data, NULL, error);
   2979 		error = checkerr(pmp, error, __func__);
   2980 		PUFFS_MSG_RELEASE(mmap);
   2981 	} else {
   2982 		error = genfs_mmap(v);
   2983 	}
   2984 
   2985 	return error;
   2986 }
   2987 
   2988 
   2989 /*
   2990  * The rest don't get a free trip to userspace and back, they
   2991  * have to stay within the kernel.
   2992  */
   2993 
   2994 /*
   2995  * bmap doesn't really make any sense for puffs, so just 1:1 map it.
   2996  * well, maybe somehow, somewhere, some day ....
   2997  */
   2998 int
   2999 puffs_vnop_bmap(void *v)
   3000 {
   3001 	struct vop_bmap_args /* {
   3002 		const struct vnodeop_desc *a_desc;
   3003 		struct vnode *a_vp;
   3004 		daddr_t a_bn;
   3005 		struct vnode **a_vpp;
   3006 		daddr_t *a_bnp;
   3007 		int *a_runp;
   3008 	} */ *ap = v;
   3009 	struct puffs_mount *pmp;
   3010 
   3011 	pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   3012 
   3013 	if (ap->a_vpp)
   3014 		*ap->a_vpp = ap->a_vp;
   3015 	if (ap->a_bnp)
   3016 		*ap->a_bnp = ap->a_bn;
   3017 	if (ap->a_runp)
   3018 		*ap->a_runp
   3019 		    = (PUFFS_TOMOVE(pmp->pmp_msg_maxsize, pmp)>>DEV_BSHIFT) - 1;
   3020 
   3021 	return 0;
   3022 }
   3023 
   3024 /*
   3025  * Handle getpages faults in puffs.  We let genfs_getpages() do most
   3026  * of the dirty work, but we come in this route to do accounting tasks.
   3027  * If the user server has specified functions for cache notifications
   3028  * about reads and/or writes, we record which type of operation we got,
   3029  * for which page range, and proceed to issue a FAF notification to the
   3030  * server about it.
   3031  */
   3032 int
   3033 puffs_vnop_getpages(void *v)
   3034 {
   3035 	struct vop_getpages_args /* {
   3036 		const struct vnodeop_desc *a_desc;
   3037 		struct vnode *a_vp;
   3038 		voff_t a_offset;
   3039 		struct vm_page **a_m;
   3040 		int *a_count;
   3041 		int a_centeridx;
   3042 		vm_prot_t a_access_type;
   3043 		int a_advice;
   3044 		int a_flags;
   3045 	} */ *ap = v;
   3046 	struct puffs_mount *pmp;
   3047 	struct puffs_node *pn;
   3048 	struct vnode *vp;
   3049 	struct vm_page **pgs;
   3050 	struct puffs_cacheinfo *pcinfo = NULL;
   3051 	struct puffs_cacherun *pcrun;
   3052 	void *parkmem = NULL;
   3053 	size_t runsizes;
   3054 	int i, npages, si, streakon;
   3055 	int error, locked, write;
   3056 
   3057 	pmp = MPTOPUFFSMP(ap->a_vp->v_mount);
   3058 	npages = *ap->a_count;
   3059 	pgs = ap->a_m;
   3060 	vp = ap->a_vp;
   3061 	pn = vp->v_data;
   3062 	locked = (ap->a_flags & PGO_LOCKED) != 0;
   3063 	write = (ap->a_access_type & VM_PROT_WRITE) != 0;
   3064 
   3065 	/* ccg xnaht - gets Wuninitialized wrong */
   3066 	pcrun = NULL;
   3067 	runsizes = 0;
   3068 
   3069 	/*
   3070 	 * Check that we aren't trying to fault in pages which our file
   3071 	 * server doesn't know about.  This happens if we extend a file by
   3072 	 * skipping some pages and later try to fault in pages which
   3073 	 * are between pn_serversize and vp_size.  This check optimizes
   3074 	 * away the common case where a file is being extended.
   3075 	 */
   3076 	if (ap->a_offset >= pn->pn_serversize && ap->a_offset < vp->v_size) {
   3077 		struct vattr va;
   3078 
   3079 		/* try again later when we can block */
   3080 		if (locked)
   3081 			ERROUT(EBUSY);
   3082 
   3083 		mutex_exit(vp->v_interlock);
   3084 		vattr_null(&va);
   3085 		va.va_size = vp->v_size;
   3086 		error = dosetattr(vp, &va, FSCRED, 0);
   3087 		if (error)
   3088 			ERROUT(error);
   3089 		mutex_enter(vp->v_interlock);
   3090 	}
   3091 
   3092 	if (write && PUFFS_WCACHEINFO(pmp)) {
   3093 #ifdef notnowjohn
   3094 		/* allocate worst-case memory */
   3095 		runsizes = ((npages / 2) + 1) * sizeof(struct puffs_cacherun);
   3096 		KASSERT(curlwp != uvm.pagedaemon_lwp || locked);
   3097 		pcinfo = kmem_zalloc(sizeof(struct puffs_cacheinfo) + runsize,
   3098 		    locked ? KM_NOSLEEP : KM_SLEEP);
   3099 
   3100 		/*
   3101 		 * can't block if we're locked and can't mess up caching
   3102 		 * information for fs server.  so come back later, please
   3103 		 */
   3104 		if (pcinfo == NULL)
   3105 			ERROUT(ENOMEM);
   3106 
   3107 		parkmem = puffs_park_alloc(locked == 0);
   3108 		if (parkmem == NULL)
   3109 			ERROUT(ENOMEM);
   3110 
   3111 		pcrun = pcinfo->pcache_runs;
   3112 #else
   3113 		(void)parkmem;
   3114 #endif
   3115 	}
   3116 
   3117 	error = genfs_getpages(v);
   3118 	if (error)
   3119 		goto out;
   3120 
   3121 	if (PUFFS_WCACHEINFO(pmp) == 0)
   3122 		goto out;
   3123 
   3124 	/*
   3125 	 * Let's see whose fault it was and inform the user server of
   3126 	 * possibly read/written pages.  Map pages from read faults
   3127 	 * strictly read-only, since otherwise we might miss info on
   3128 	 * when the page is actually write-faulted to.
   3129 	 */
   3130 	if (!locked)
   3131 		mutex_enter(vp->v_uobj.vmobjlock);
   3132 	for (i = 0, si = 0, streakon = 0; i < npages; i++) {
   3133 		if (pgs[i] == NULL || pgs[i] == PGO_DONTCARE) {
   3134 			if (streakon && write) {
   3135 				streakon = 0;
   3136 				pcrun[si].pcache_runend
   3137 				    = trunc_page(pgs[i]->offset) + PAGE_MASK;
   3138 				si++;
   3139 			}
   3140 			continue;
   3141 		}
   3142 		if (streakon == 0 && write) {
   3143 			streakon = 1;
   3144 			pcrun[si].pcache_runstart = pgs[i]->offset;
   3145 		}
   3146 
   3147 		if (!write)
   3148 			pgs[i]->flags |= PG_RDONLY;
   3149 	}
   3150 	/* was the last page part of our streak? */
   3151 	if (streakon) {
   3152 		pcrun[si].pcache_runend
   3153 		    = trunc_page(pgs[i-1]->offset) + PAGE_MASK;
   3154 		si++;
   3155 	}
   3156 	if (!locked)
   3157 		mutex_exit(vp->v_uobj.vmobjlock);
   3158 
   3159 	KASSERT(si <= (npages / 2) + 1);
   3160 
   3161 #ifdef notnowjohn
   3162 	/* send results to userspace */
   3163 	if (write)
   3164 		puffs_cacheop(pmp, parkmem, pcinfo,
   3165 		    sizeof(struct puffs_cacheinfo) + runsizes, VPTOPNC(vp));
   3166 #endif
   3167 
   3168  out:
   3169 	if (error) {
   3170 		if (pcinfo != NULL)
   3171 			kmem_free(pcinfo,
   3172 			    sizeof(struct puffs_cacheinfo) + runsizes);
   3173 #ifdef notnowjohn
   3174 		if (parkmem != NULL)
   3175 			puffs_park_release(parkmem, 1);
   3176 #endif
   3177 	}
   3178 
   3179 	return error;
   3180 }
   3181 
   3182 /*
   3183  * Extended attribute support.
   3184  */
   3185 
   3186 int
   3187 puffs_vnop_getextattr(void *v)
   3188 {
   3189 	struct vop_getextattr_args /*
   3190 		struct vnode *a_vp;
   3191 		int a_attrnamespace;
   3192 		const char *a_name;
   3193 		struct uio *a_uio;
   3194 		size_t *a_size;
   3195 		kauth_cred_t a_cred;
   3196 	}; */ *ap = v;
   3197 	PUFFS_MSG_VARS(vn, getextattr);
   3198 	struct vnode *vp = ap->a_vp;
   3199 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3200 	int attrnamespace = ap->a_attrnamespace;
   3201 	const char *name = ap->a_name;
   3202 	struct uio *uio = ap->a_uio;
   3203 	size_t *sizep = ap->a_size;
   3204 	size_t tomove, resid;
   3205 	int error;
   3206 
   3207 	if (uio)
   3208 		resid = uio->uio_resid;
   3209 	else
   3210 		resid = 0;
   3211 
   3212 	tomove = PUFFS_TOMOVE(resid, pmp);
   3213 	if (tomove != resid) {
   3214 		error = E2BIG;
   3215 		goto out;
   3216 	}
   3217 
   3218 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_getextattr) + tomove,
   3219 	    &park_getextattr, (void *)&getextattr_msg, 1);
   3220 
   3221 	getextattr_msg->pvnr_attrnamespace = attrnamespace;
   3222 	strlcpy(getextattr_msg->pvnr_attrname, name,
   3223 	    sizeof(getextattr_msg->pvnr_attrname));
   3224 	puffs_credcvt(&getextattr_msg->pvnr_cred, ap->a_cred);
   3225 	if (sizep)
   3226 		getextattr_msg->pvnr_datasize = 1;
   3227 	getextattr_msg->pvnr_resid = tomove;
   3228 
   3229 	puffs_msg_setinfo(park_getextattr,
   3230 	    PUFFSOP_VN, PUFFS_VN_GETEXTATTR, VPTOPNC(vp));
   3231 	puffs_msg_setdelta(park_getextattr, tomove);
   3232 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_getextattr, vp->v_data, NULL, error);
   3233 
   3234 	error = checkerr(pmp, error, __func__);
   3235 	if (error)
   3236 		goto out;
   3237 
   3238 	resid = getextattr_msg->pvnr_resid;
   3239 	if (resid > tomove) {
   3240 		puffs_senderr(pmp, PUFFS_ERR_GETEXTATTR, E2BIG,
   3241 		    "resid grew", VPTOPNC(vp));
   3242 		error = EPROTO;
   3243 		goto out;
   3244 	}
   3245 
   3246 	if (sizep)
   3247 		*sizep = getextattr_msg->pvnr_datasize;
   3248 	if (uio)
   3249 		error = uiomove(getextattr_msg->pvnr_data, tomove - resid, uio);
   3250 
   3251  out:
   3252 	PUFFS_MSG_RELEASE(getextattr);
   3253 	return error;
   3254 }
   3255 
   3256 int
   3257 puffs_vnop_setextattr(void *v)
   3258 {
   3259 	struct vop_setextattr_args /* {
   3260 		struct vnode *a_vp;
   3261 		int a_attrnamespace;
   3262 		const char *a_name;
   3263 		struct uio *a_uio;
   3264 		kauth_cred_t a_cred;
   3265 	}; */ *ap = v;
   3266 	PUFFS_MSG_VARS(vn, setextattr);
   3267 	struct vnode *vp = ap->a_vp;
   3268 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3269 	int attrnamespace = ap->a_attrnamespace;
   3270 	const char *name = ap->a_name;
   3271 	struct uio *uio = ap->a_uio;
   3272 	size_t tomove, resid;
   3273 	int error;
   3274 
   3275 	if (uio)
   3276 		resid = uio->uio_resid;
   3277 	else
   3278 		resid = 0;
   3279 
   3280 	tomove = PUFFS_TOMOVE(resid, pmp);
   3281 	if (tomove != resid) {
   3282 		error = E2BIG;
   3283 		goto out;
   3284 	}
   3285 
   3286 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_setextattr) + tomove,
   3287 	    &park_setextattr, (void *)&setextattr_msg, 1);
   3288 
   3289 	setextattr_msg->pvnr_attrnamespace = attrnamespace;
   3290 	strlcpy(setextattr_msg->pvnr_attrname, name,
   3291 	    sizeof(setextattr_msg->pvnr_attrname));
   3292 	puffs_credcvt(&setextattr_msg->pvnr_cred, ap->a_cred);
   3293 	setextattr_msg->pvnr_resid = tomove;
   3294 
   3295 	if (uio) {
   3296 		error = uiomove(setextattr_msg->pvnr_data, tomove, uio);
   3297 		if (error)
   3298 			goto out;
   3299 	}
   3300 
   3301 	puffs_msg_setinfo(park_setextattr,
   3302 	    PUFFSOP_VN, PUFFS_VN_SETEXTATTR, VPTOPNC(vp));
   3303 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_setextattr, vp->v_data, NULL, error);
   3304 
   3305 	error = checkerr(pmp, error, __func__);
   3306 	if (error)
   3307 		goto out;
   3308 
   3309 	if (setextattr_msg->pvnr_resid != 0)
   3310 		error = EIO;
   3311 
   3312  out:
   3313 	PUFFS_MSG_RELEASE(setextattr);
   3314 
   3315 	return error;
   3316 }
   3317 
   3318 int
   3319 puffs_vnop_listextattr(void *v)
   3320 {
   3321 	struct vop_listextattr_args /* {
   3322 		struct vnode *a_vp;
   3323 		int a_attrnamespace;
   3324 		struct uio *a_uio;
   3325 		size_t *a_size;
   3326 		int a_flag,
   3327 		kauth_cred_t a_cred;
   3328 	}; */ *ap = v;
   3329 	PUFFS_MSG_VARS(vn, listextattr);
   3330 	struct vnode *vp = ap->a_vp;
   3331 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3332 	int attrnamespace = ap->a_attrnamespace;
   3333 	struct uio *uio = ap->a_uio;
   3334 	size_t *sizep = ap->a_size;
   3335 	int flag = ap->a_flag;
   3336 	size_t tomove, resid;
   3337 	int error;
   3338 
   3339 	if (uio)
   3340 		resid = uio->uio_resid;
   3341 	else
   3342 		resid = 0;
   3343 
   3344 	tomove = PUFFS_TOMOVE(resid, pmp);
   3345 	if (tomove != resid) {
   3346 		error = E2BIG;
   3347 		goto out;
   3348 	}
   3349 
   3350 	puffs_msgmem_alloc(sizeof(struct puffs_vnmsg_listextattr) + tomove,
   3351 	    &park_listextattr, (void *)&listextattr_msg, 1);
   3352 
   3353 	listextattr_msg->pvnr_attrnamespace = attrnamespace;
   3354 	listextattr_msg->pvnr_flag = flag;
   3355 	puffs_credcvt(&listextattr_msg->pvnr_cred, ap->a_cred);
   3356 	listextattr_msg->pvnr_resid = tomove;
   3357 	if (sizep)
   3358 		listextattr_msg->pvnr_datasize = 1;
   3359 
   3360 	puffs_msg_setinfo(park_listextattr,
   3361 	    PUFFSOP_VN, PUFFS_VN_LISTEXTATTR, VPTOPNC(vp));
   3362 	puffs_msg_setdelta(park_listextattr, tomove);
   3363 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_listextattr, vp->v_data, NULL, error);
   3364 
   3365 	error = checkerr(pmp, error, __func__);
   3366 	if (error)
   3367 		goto out;
   3368 
   3369 	resid = listextattr_msg->pvnr_resid;
   3370 	if (resid > tomove) {
   3371 		puffs_senderr(pmp, PUFFS_ERR_LISTEXTATTR, E2BIG,
   3372 		    "resid grew", VPTOPNC(vp));
   3373 		error = EPROTO;
   3374 		goto out;
   3375 	}
   3376 
   3377 	if (sizep)
   3378 		*sizep = listextattr_msg->pvnr_datasize;
   3379 	if (uio)
   3380 		error = uiomove(listextattr_msg->pvnr_data, tomove-resid, uio);
   3381 
   3382  out:
   3383 	PUFFS_MSG_RELEASE(listextattr);
   3384 	return error;
   3385 }
   3386 
   3387 int
   3388 puffs_vnop_deleteextattr(void *v)
   3389 {
   3390 	struct vop_deleteextattr_args /* {
   3391 		struct vnode *a_vp;
   3392 		int a_attrnamespace;
   3393 		const char *a_name;
   3394 		kauth_cred_t a_cred;
   3395 	}; */ *ap = v;
   3396 	PUFFS_MSG_VARS(vn, deleteextattr);
   3397 	struct vnode *vp = ap->a_vp;
   3398 	struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount);
   3399 	int attrnamespace = ap->a_attrnamespace;
   3400 	const char *name = ap->a_name;
   3401 	int error;
   3402 
   3403 	PUFFS_MSG_ALLOC(vn, deleteextattr);
   3404 	deleteextattr_msg->pvnr_attrnamespace = attrnamespace;
   3405 	strlcpy(deleteextattr_msg->pvnr_attrname, name,
   3406 	    sizeof(deleteextattr_msg->pvnr_attrname));
   3407 	puffs_credcvt(&deleteextattr_msg->pvnr_cred, ap->a_cred);
   3408 
   3409 	puffs_msg_setinfo(park_deleteextattr,
   3410 	    PUFFSOP_VN, PUFFS_VN_DELETEEXTATTR, VPTOPNC(vp));
   3411 	PUFFS_MSG_ENQUEUEWAIT2(pmp, park_deleteextattr,
   3412 	    vp->v_data, NULL, error);
   3413 
   3414 	error = checkerr(pmp, error, __func__);
   3415 
   3416 	PUFFS_MSG_RELEASE(deleteextattr);
   3417 	return error;
   3418 }
   3419 
   3420 /*
   3421  * spec & fifo.  These call the miscfs spec and fifo vectors, but issue
   3422  * FAF update information for the puffs node first.
   3423  */
   3424 int
   3425 puffs_vnop_spec_read(void *v)
   3426 {
   3427 	struct vop_read_args /* {
   3428 		const struct vnodeop_desc *a_desc;
   3429 		struct vnode *a_vp;
   3430 		struct uio *a_uio;
   3431 		int a_ioflag;
   3432 		kauth_cred_t a_cred;
   3433 	} */ *ap = v;
   3434 
   3435 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
   3436 	return VOCALL(spec_vnodeop_p, VOFFSET(vop_read), v);
   3437 }
   3438 
   3439 int
   3440 puffs_vnop_spec_write(void *v)
   3441 {
   3442 	struct vop_write_args /* {
   3443 		const struct vnodeop_desc *a_desc;
   3444 		struct vnode *a_vp;
   3445 		struct uio *a_uio;
   3446 		int a_ioflag;
   3447 		kauth_cred_t a_cred;
   3448 	} */ *ap = v;
   3449 
   3450 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
   3451 	return VOCALL(spec_vnodeop_p, VOFFSET(vop_write), v);
   3452 }
   3453 
   3454 int
   3455 puffs_vnop_fifo_read(void *v)
   3456 {
   3457 	struct vop_read_args /* {
   3458 		const struct vnodeop_desc *a_desc;
   3459 		struct vnode *a_vp;
   3460 		struct uio *a_uio;
   3461 		int a_ioflag;
   3462 		kauth_cred_t a_cred;
   3463 	} */ *ap = v;
   3464 
   3465 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEATIME, 0);
   3466 	return VOCALL(fifo_vnodeop_p, VOFFSET(vop_read), v);
   3467 }
   3468 
   3469 int
   3470 puffs_vnop_fifo_write(void *v)
   3471 {
   3472 	struct vop_write_args /* {
   3473 		const struct vnodeop_desc *a_desc;
   3474 		struct vnode *a_vp;
   3475 		struct uio *a_uio;
   3476 		int a_ioflag;
   3477 		kauth_cred_t a_cred;
   3478 	} */ *ap = v;
   3479 
   3480 	puffs_updatenode(VPTOPP(ap->a_vp), PUFFS_UPDATEMTIME, 0);
   3481 	return VOCALL(fifo_vnodeop_p, VOFFSET(vop_write), v);
   3482 }
   3483