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