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