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