Home | History | Annotate | Line # | Download | only in libperfuse
ops.c revision 1.67
      1 /*  $NetBSD: ops.c,v 1.67 2014/08/16 16:28:43 manu Exp $ */
      2 
      3 /*-
      4  *  Copyright (c) 2010-2011 Emmanuel Dreyfus. All rights reserved.
      5  *
      6  *  Redistribution and use in source and binary forms, with or without
      7  *  modification, are permitted provided that the following conditions
      8  *  are met:
      9  *  1. Redistributions of source code must retain the above copyright
     10  *     notice, this list of conditions and the following disclaimer.
     11  *  2. Redistributions in binary form must reproduce the above copyright
     12  *     notice, this list of conditions and the following disclaimer in the
     13  *     documentation and/or other materials provided with the distribution.
     14  *
     15  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  *  POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <stdio.h>
     29 #include <unistd.h>
     30 #include <stdlib.h>
     31 #include <libgen.h>
     32 #include <errno.h>
     33 #include <err.h>
     34 #include <sysexits.h>
     35 #include <syslog.h>
     36 #include <puffs.h>
     37 #include <sys/socket.h>
     38 #include <sys/socket.h>
     39 #include <sys/extattr.h>
     40 #include <sys/time.h>
     41 #include <machine/vmparam.h>
     42 
     43 #include "perfuse_priv.h"
     44 #include "fuse.h"
     45 
     46 extern int perfuse_diagflags;
     47 
     48 #if 0
     49 static void print_node(const char *, puffs_cookie_t);
     50 #endif
     51 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
     52 static void perfuse_newinfo_setttl(struct puffs_newinfo *,
     53     struct puffs_node *, struct fuse_entry_out *, struct fuse_attr_out *);
     54 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
     55 static int xchg_msg(struct puffs_usermount *, puffs_cookie_t,
     56     perfuse_msg_t *, size_t, enum perfuse_xchg_pb_reply);
     57 static int mode_access(puffs_cookie_t, const struct puffs_cred *, mode_t);
     58 static int sticky_access(puffs_cookie_t, struct puffs_node *,
     59     const struct puffs_cred *);
     60 static void fuse_attr_to_vap(struct perfuse_state *,
     61     struct vattr *, struct fuse_attr *);
     62 static int node_lookup_common(struct puffs_usermount *, puffs_cookie_t,
     63     struct puffs_newinfo *, const char *, const struct puffs_cred *,
     64     struct puffs_node **);
     65 static int node_mk_common(struct puffs_usermount *, puffs_cookie_t,
     66     struct puffs_newinfo *, const struct puffs_cn *pcn, perfuse_msg_t *);
     67 static uint64_t readdir_last_cookie(struct fuse_dirent *, size_t);
     68 static ssize_t fuse_to_dirent(struct puffs_usermount *, puffs_cookie_t,
     69     struct fuse_dirent *, size_t);
     70 static void readdir_buffered(puffs_cookie_t, struct dirent *, off_t *,
     71     size_t *);
     72 static void node_ref(puffs_cookie_t);
     73 static void node_rele(puffs_cookie_t);
     74 static void requeue_request(struct puffs_usermount *,
     75     puffs_cookie_t opc, enum perfuse_qtype);
     76 static int dequeue_requests(puffs_cookie_t opc, enum perfuse_qtype, int);
     77 #define DEQUEUE_ALL 0
     78 
     79 /*
     80  *  From <sys/vnode>, inside #ifdef _KERNEL section
     81  */
     82 #define IO_SYNC		(0x40|IO_DSYNC)
     83 #define IO_DSYNC	0x00200
     84 #define IO_DIRECT	0x02000
     85 
     86 /*
     87  *  From <fcntl>, inside #ifdef _KERNEL section
     88  */
     89 #define F_WAIT		0x010
     90 #define F_FLOCK		0x020
     91 #define OFLAGS(fflags)  ((fflags) - 1)
     92 
     93 /*
     94  * Borrowed from src/sys/kern/vfs_subr.c and src/sys/sys/vnode.h
     95  */
     96 const enum vtype iftovt_tab[16] = {
     97 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
     98         VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
     99 };
    100 const int vttoif_tab[9] = {
    101 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
    102         S_IFSOCK, S_IFIFO, S_IFMT,
    103 };
    104 
    105 #define IFTOVT(mode) (iftovt_tab[((mode) & S_IFMT) >> 12])
    106 #define VTTOIF(indx) (vttoif_tab[(int)(indx)])
    107 
    108 #if 0
    109 static void
    110 print_node(const char *func, puffs_cookie_t opc)
    111 {
    112 	struct puffs_node *pn;
    113 	struct perfuse_node_data *pnd;
    114 	struct vattr *vap;
    115 
    116 	pn = (struct puffs_node *)opc;
    117 	pnd = PERFUSE_NODE_DATA(opc);
    118 	vap = &pn->pn_va;
    119 
    120 	printf("%s: \"%s\", opc = %p, nodeid = 0x%"PRIx64" ino = %"PRIu64"\n",
    121 	       func, pnd->pnd_name, opc, pnd->pnd_nodeid, vap->va_fileid);
    122 
    123 	return;
    124 }
    125 #endif /* PERFUSE_DEBUG */
    126 
    127 int
    128 perfuse_node_close_common(struct puffs_usermount *pu, puffs_cookie_t opc,
    129 	int mode)
    130 {
    131 	struct perfuse_state *ps;
    132 	perfuse_msg_t *pm;
    133 	int op;
    134 	uint64_t fh;
    135 	struct fuse_release_in *fri;
    136 	struct perfuse_node_data *pnd;
    137 	struct puffs_node *pn;
    138 	int error;
    139 
    140 	ps = puffs_getspecific(pu);
    141 	pn = (struct puffs_node *)opc;
    142 	pnd = PERFUSE_NODE_DATA(pn);
    143 
    144 	if (puffs_pn_getvap(pn)->va_type == VDIR) {
    145 		op = FUSE_RELEASEDIR;
    146 		mode = FREAD;
    147 	} else {
    148 		op = FUSE_RELEASE;
    149 	}
    150 
    151 	/*
    152 	 * Destroy the filehandle before sending the
    153 	 * request to the FUSE filesystem, otherwise
    154 	 * we may get a second close() while we wait
    155 	 * for the reply, and we would end up closing
    156 	 * the same fh twice instead of closng both.
    157 	 */
    158 	fh = perfuse_get_fh(opc, mode);
    159 	perfuse_destroy_fh(pn, fh);
    160 
    161 	/*
    162 	 * release_flags may be set to FUSE_RELEASE_FLUSH
    163 	 * to flush locks. lock_owner must be set in that case
    164 	 *
    165 	 * ps_new_msg() is called with NULL creds, which will
    166 	 * be interpreted as FUSE superuser. We come here from the
    167 	 * inactive method, which provides no creds, but obviously
    168 	 * runs with kernel privilege.
    169 	 */
    170 	pm = ps->ps_new_msg(pu, opc, op, sizeof(*fri), NULL);
    171 	fri = GET_INPAYLOAD(ps, pm, fuse_release_in);
    172 	fri->fh = fh;
    173 	fri->flags = 0;
    174 	fri->release_flags = 0;
    175 	fri->lock_owner = pnd->pnd_lock_owner;
    176 	fri->flags = (fri->lock_owner != 0) ? FUSE_RELEASE_FLUSH : 0;
    177 
    178 #ifdef PERFUSE_DEBUG
    179 	if (perfuse_diagflags & PDF_FH)
    180 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
    181 			 __func__, (void *)opc, pnd->pnd_nodeid, fri->fh);
    182 #endif
    183 
    184 	if ((error = xchg_msg(pu, opc, pm,
    185 			      NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0)
    186 		DERRX(EX_SOFTWARE, "%s: freed fh = 0x%"PRIx64" but filesystem "
    187 		      "returned error = %d", __func__, fh, error);
    188 
    189 	ps->ps_destroy_msg(pm);
    190 
    191 	return 0;
    192 }
    193 
    194 static int
    195 xchg_msg(struct puffs_usermount *pu, puffs_cookie_t opc, perfuse_msg_t *pm,
    196 	size_t len, enum perfuse_xchg_pb_reply wait)
    197 {
    198 	struct perfuse_state *ps;
    199 	struct perfuse_node_data *pnd;
    200 	struct perfuse_trace *pt = NULL;
    201 	int error;
    202 
    203 	ps = puffs_getspecific(pu);
    204 	pnd = NULL;
    205 	if ((struct puffs_node *)opc != NULL)
    206 		pnd = PERFUSE_NODE_DATA(opc);
    207 
    208 #ifdef PERFUSE_DEBUG
    209 	if ((perfuse_diagflags & PDF_FILENAME) && (opc != 0))
    210 		DPRINTF("file = \"%s\", ino = %"PRIu64" flags = 0x%x\n",
    211 			perfuse_node_path(ps, opc),
    212 			((struct puffs_node *)opc)->pn_va.va_fileid,
    213 			PERFUSE_NODE_DATA(opc)->pnd_flags);
    214 #endif
    215 	ps->ps_xchgcount++;
    216 	if (pnd)
    217 		pnd->pnd_inxchg++;
    218 
    219 	/*
    220 	 * Record FUSE call start if requested
    221 	 */
    222 	if (perfuse_diagflags & PDF_TRACE)
    223 		pt = perfuse_trace_begin(ps, opc, pm);
    224 
    225 	/*
    226 	 * Do actual FUSE exchange
    227 	 */
    228 	if ((error = ps->ps_xchg_msg(pu, pm, len, wait)) != 0)
    229 		ps->ps_destroy_msg(pm);
    230 
    231 	/*
    232 	 * Record FUSE call end if requested
    233 	 */
    234 	if (pt != NULL)
    235 		perfuse_trace_end(ps, pt, error);
    236 
    237 	ps->ps_xchgcount--;
    238 	if (pnd) {
    239 		pnd->pnd_inxchg--;
    240 		(void)dequeue_requests(opc, PCQ_AFTERXCHG, DEQUEUE_ALL);
    241 	}
    242 
    243 	return error;
    244 }
    245 
    246 static int
    247 mode_access(puffs_cookie_t opc, const struct puffs_cred *pcr, mode_t mode)
    248 {
    249 	struct puffs_node *pn;
    250 	struct vattr *va;
    251 
    252 	/*
    253 	 * pcr is NULL for self open through fsync or readdir.
    254 	 * In both case, access control is useless, as it was
    255 	 * done before, at open time.
    256 	 */
    257 	if (pcr == NULL)
    258 		return 0;
    259 
    260 	pn = (struct puffs_node *)opc;
    261 	va = puffs_pn_getvap(pn);
    262 	return puffs_access(va->va_type, va->va_mode,
    263 			    va->va_uid, va->va_gid,
    264 			    mode, pcr);
    265 }
    266 
    267 static int
    268 sticky_access(puffs_cookie_t opc, struct puffs_node *targ,
    269 	      const struct puffs_cred *pcr)
    270 {
    271 	uid_t uid;
    272 	int sticky, owner;
    273 
    274 	/*
    275 	 * This covers the case where the kernel requests a DELETE
    276 	 * or RENAME on its own, and where puffs_cred_getuid would
    277 	 * return -1. While such a situation should not happen,
    278 	 * we allow it here.
    279 	 *
    280 	 * This also allows root to tamper with other users' files
    281 	 * that have the sticky bit.
    282 	 */
    283 	if (puffs_cred_isjuggernaut(pcr))
    284 		return 0;
    285 
    286 	if (puffs_cred_getuid(pcr, &uid) != 0)
    287 		DERRX(EX_SOFTWARE, "puffs_cred_getuid fails in %s", __func__);
    288 
    289 	sticky = puffs_pn_getvap(opc)->va_mode & S_ISTXT;
    290 	owner = puffs_pn_getvap(targ)->va_uid == uid;
    291 
    292 	if (sticky && !owner)
    293 		return EACCES;
    294 
    295 	return 0;
    296 }
    297 
    298 
    299 static void
    300 fuse_attr_to_vap(struct perfuse_state *ps, struct vattr *vap,
    301 	struct fuse_attr *fa)
    302 {
    303 	vap->va_type = IFTOVT(fa->mode);
    304 	vap->va_mode = fa->mode & ALLPERMS;
    305 	vap->va_nlink = fa->nlink;
    306 	vap->va_uid = fa->uid;
    307 	vap->va_gid = fa->gid;
    308 	vap->va_fsid = (long)ps->ps_fsid;
    309 	vap->va_fileid = fa->ino;
    310 	vap->va_size = fa->size;
    311 	vap->va_blocksize = fa->blksize;
    312 	vap->va_atime.tv_sec = (time_t)fa->atime;
    313 	vap->va_atime.tv_nsec = (long) fa->atimensec;
    314 	vap->va_mtime.tv_sec = (time_t)fa->mtime;
    315 	vap->va_mtime.tv_nsec = (long)fa->mtimensec;
    316 	vap->va_ctime.tv_sec = (time_t)fa->ctime;
    317 	vap->va_ctime.tv_nsec = (long)fa->ctimensec;
    318 	vap->va_birthtime.tv_sec = 0;
    319 	vap->va_birthtime.tv_nsec = 0;
    320 	vap->va_gen = 0;
    321 	vap->va_flags = 0;
    322 	vap->va_rdev = fa->rdev;
    323 	vap->va_bytes = fa->size;
    324 	vap->va_filerev = (u_quad_t)PUFFS_VNOVAL;
    325 	vap->va_vaflags = 0;
    326 
    327 	if (vap->va_blocksize == 0)
    328 		vap->va_blocksize = DEV_BSIZE;
    329 
    330 	if (vap->va_size == (size_t)PUFFS_VNOVAL) /* XXX */
    331 		vap->va_size = 0;
    332 
    333 	return;
    334 }
    335 
    336 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
    337 static void
    338 perfuse_newinfo_setttl(struct puffs_newinfo *pni,
    339     struct puffs_node *pn, struct fuse_entry_out *feo,
    340     struct fuse_attr_out *fao)
    341 {
    342 #ifdef PERFUSE_DEBUG
    343 	if ((feo == NULL) && (fao == NULL))
    344 		DERRX(EX_SOFTWARE, "%s: feo and fao NULL", __func__);
    345 
    346 	if ((feo != NULL) && (fao != NULL))
    347 		DERRX(EX_SOFTWARE, "%s: feo and fao != NULL", __func__);
    348 #endif /* PERFUSE_DEBUG */
    349 
    350 	if (fao != NULL) {
    351 		struct timespec va_ttl;
    352 
    353 		va_ttl.tv_sec = fao->attr_valid;
    354 		va_ttl.tv_nsec = fao->attr_valid_nsec;
    355 
    356 		puffs_newinfo_setvattl(pni, &va_ttl);
    357 	}
    358 
    359 	if (feo != NULL) {
    360 		struct timespec va_ttl;
    361 		struct timespec cn_ttl;
    362 		struct timespec now;
    363 		struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(pn);
    364 
    365 		va_ttl.tv_sec = feo->attr_valid;
    366 		va_ttl.tv_nsec = feo->attr_valid_nsec;
    367 		cn_ttl.tv_sec = feo->entry_valid;
    368 		cn_ttl.tv_nsec = feo->entry_valid_nsec;
    369 
    370 		puffs_newinfo_setvattl(pni, &va_ttl);
    371 		puffs_newinfo_setcnttl(pni, &cn_ttl);
    372 
    373 		if (clock_gettime(CLOCK_REALTIME, &now) != 0)
    374 			DERR(EX_OSERR, "clock_gettime failed");
    375 
    376                 timespecadd(&now, &cn_ttl, &pnd->pnd_cn_expire);
    377 	}
    378 
    379 	return;
    380 }
    381 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
    382 
    383 static int
    384 node_lookup_common(struct puffs_usermount *pu, puffs_cookie_t opc,
    385 	struct puffs_newinfo *pni, const char *path,
    386 	const struct puffs_cred *pcr, struct puffs_node **pnp)
    387 {
    388 	struct perfuse_state *ps;
    389 	struct perfuse_node_data *oldpnd;
    390 	perfuse_msg_t *pm;
    391 	struct fuse_entry_out *feo;
    392 	struct puffs_node *pn;
    393 	size_t len;
    394 	int error;
    395 
    396 	/*
    397 	 * Prevent further lookups if the parent was removed
    398 	 */
    399 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
    400 		return ESTALE;
    401 
    402 	if (pnp == NULL)
    403 		DERRX(EX_SOFTWARE, "pnp must be != NULL");
    404 
    405 	ps = puffs_getspecific(pu);
    406 
    407 #ifdef PERFUSE_DEBUG
    408 	if (perfuse_diagflags & PDF_FILENAME)
    409 		DPRINTF("%s: opc = %p, file = \"%s\" looking up \"%s\"\n",
    410 			__func__, (void *)opc,
    411 			perfuse_node_path(ps, opc), path);
    412 
    413 	if (strcmp(path, ".") == 0)
    414 		DERRX(EX_SOFTWARE, "unexpected dot-lookup");
    415 
    416 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_RECLAIMED)
    417 		DERRX(EX_SOFTWARE,
    418 		      "looking up reclaimed node opc = %p, name = \"%s\"",
    419 		      opc, path);
    420 
    421 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_INVALID)
    422 		DERRX(EX_SOFTWARE,
    423 		      "looking up freed node opc = %p, name = \"%s\"",
    424 		      opc, path);
    425 #endif /* PERFUSE_DEBUG */
    426 
    427 	len = strlen(path) + 1;
    428 	pm = ps->ps_new_msg(pu, opc, FUSE_LOOKUP, len, pcr);
    429 	(void)strlcpy(_GET_INPAYLOAD(ps, pm, char *), path, len);
    430 
    431 	if ((error = xchg_msg(pu, opc, pm, sizeof(*feo), wait_reply)) != 0)
    432 		return error;
    433 
    434 	feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
    435 
    436 	/*
    437 	 * Starting with ABI 7.4, inode number 0 means ENOENT,
    438 	 * with entry_valid / entry_valid_nsec giving negative
    439 	 * cache timeout (which we do not implement yet).
    440 	 */
    441 	if (feo->attr.ino == 0) {
    442 		ps->ps_destroy_msg(pm);
    443 		return ENOENT;
    444 	}
    445 
    446 	/*
    447 	 * Check for a known node, not reclaimed, with another name.
    448 	 * It may have been moved, or we can lookup ../
    449 	 */
    450 	if (((oldpnd = perfuse_node_bynodeid(ps, feo->nodeid)) != NULL) &&
    451 	    !(oldpnd->pnd_flags & PND_RECLAIMED)) {
    452 		/*
    453 		 * Save the new node name if not ..
    454 		 */
    455 		if (strncmp(path, "..", len) != 0)
    456 			(void)strlcpy(oldpnd->pnd_name,
    457 				      path, MAXPATHLEN);
    458 		pn = oldpnd->pnd_pn;
    459 
    460 	} else {
    461 		pn = perfuse_new_pn(pu, path, opc);
    462 		PERFUSE_NODE_DATA(pn)->pnd_nodeid = feo->nodeid;
    463 		perfuse_node_cache(ps, pn);
    464 	}
    465 
    466 #ifdef PERFUSE_DEBUG
    467 	if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_RECLAIMED)
    468 		DERRX(EX_SOFTWARE,
    469 		      "reclaimed in lookup opc = %p, name = \"%s\", ck = %p",
    470 		      opc, path, pn);
    471 
    472 	if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_INVALID)
    473 		DERRX(EX_SOFTWARE,
    474 		      "freed in lookup opc = %p, name = \"%s\", ck = %p",
    475 		      opc, path, pn);
    476 #endif /* PERFUSE_DEBUG */
    477 
    478 	fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
    479 	pn->pn_va.va_gen = (u_long)(feo->generation);
    480 	PERFUSE_NODE_DATA(pn)->pnd_fuse_nlookup++;
    481 
    482 	*pnp = pn;
    483 
    484 #ifdef PERFUSE_DEBUG
    485 	if (perfuse_diagflags & PDF_FILENAME)
    486 		DPRINTF("%s: opc = %p, looked up opc = %p, "
    487 			"nodeid = 0x%"PRIx64" file = \"%s\"\n", __func__,
    488 			(void *)opc, pn, feo->nodeid, path);
    489 #endif
    490 
    491 	if (pni != NULL) {
    492 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
    493 		puffs_newinfo_setva(pni, &pn->pn_va);
    494 		perfuse_newinfo_setttl(pni, pn, feo, NULL);
    495 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
    496 		puffs_newinfo_setcookie(pni, pn);
    497 		puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
    498 		puffs_newinfo_setsize(pni, (voff_t)pn->pn_va.va_size);
    499 		puffs_newinfo_setrdev(pni, pn->pn_va.va_rdev);
    500 	}
    501 
    502 	if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_NODELEAK) {
    503 		PERFUSE_NODE_DATA(pn)->pnd_flags &= ~PND_NODELEAK;
    504 		ps->ps_nodeleakcount--;
    505 	}
    506 
    507 	ps->ps_destroy_msg(pm);
    508 
    509 	return 0;
    510 }
    511 
    512 
    513 /*
    514  * Common code for methods that create objects:
    515  * perfuse_node_mkdir
    516  * perfuse_node_mknod
    517  * perfuse_node_symlink
    518  */
    519 static int
    520 node_mk_common(struct puffs_usermount *pu, puffs_cookie_t opc,
    521 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
    522 	perfuse_msg_t *pm)
    523 {
    524 	struct perfuse_state *ps;
    525 	struct puffs_node *pn;
    526 	struct fuse_entry_out *feo;
    527 	int error;
    528 
    529 	ps =  puffs_getspecific(pu);
    530 
    531 	if ((error = xchg_msg(pu, opc, pm, sizeof(*feo), wait_reply)) != 0)
    532 		return error;
    533 
    534 	feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
    535 	if (feo->nodeid == PERFUSE_UNKNOWN_NODEID)
    536 		DERRX(EX_SOFTWARE, "%s: no nodeid", __func__);
    537 
    538 	pn = perfuse_new_pn(pu, pcn->pcn_name, opc);
    539 	PERFUSE_NODE_DATA(pn)->pnd_nodeid = feo->nodeid;
    540 	PERFUSE_NODE_DATA(pn)->pnd_puffs_nlookup++;
    541 	perfuse_node_cache(ps, pn);
    542 
    543 	fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
    544 	pn->pn_va.va_gen = (u_long)(feo->generation);
    545 
    546 	puffs_newinfo_setcookie(pni, pn);
    547 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
    548 	puffs_newinfo_setva(pni, &pn->pn_va);
    549 	perfuse_newinfo_setttl(pni, pn, feo, NULL);
    550 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
    551 
    552 
    553 #ifdef PERFUSE_DEBUG
    554 	if (perfuse_diagflags & PDF_FILENAME)
    555 		DPRINTF("%s: opc = %p, file = \"%s\", flags = 0x%x "
    556 			"nodeid = 0x%"PRIx64"\n",
    557 			__func__, (void *)pn, pcn->pcn_name,
    558 			PERFUSE_NODE_DATA(pn)->pnd_flags, feo->nodeid);
    559 #endif
    560 	ps->ps_destroy_msg(pm);
    561 
    562 	/* Parents is now dirty */
    563 	PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
    564 
    565 	return 0;
    566 }
    567 
    568 static uint64_t
    569 readdir_last_cookie(struct fuse_dirent *fd, size_t fd_len)
    570 {
    571 	size_t len;
    572 	size_t seen = 0;
    573 	char *ndp;
    574 
    575 	do {
    576 		len = FUSE_DIRENT_ALIGN(sizeof(*fd) + fd->namelen);
    577 		seen += len;
    578 
    579 		if (seen >= fd_len)
    580 			break;
    581 
    582 		ndp = (char *)(void *)fd + (size_t)len;
    583 		fd = (struct fuse_dirent *)(void *)ndp;
    584 	} while (1 /* CONSTCOND */);
    585 
    586 	return fd->off;
    587 }
    588 
    589 static ssize_t
    590 fuse_to_dirent(struct puffs_usermount *pu, puffs_cookie_t opc,
    591 	struct fuse_dirent *fd, size_t fd_len)
    592 {
    593 	struct dirent *dents;
    594 	size_t dents_len;
    595 	ssize_t written;
    596 	uint64_t fd_offset;
    597 	struct fuse_dirent *fd_base;
    598 	size_t len;
    599 
    600 	fd_base = fd;
    601 	fd_offset = 0;
    602 	written = 0;
    603 	dents = PERFUSE_NODE_DATA(opc)->pnd_dirent;
    604 	dents_len = (size_t)PERFUSE_NODE_DATA(opc)->pnd_dirent_len;
    605 
    606 	do {
    607 		char *ndp;
    608 		size_t reclen;
    609 
    610 		reclen = _DIRENT_RECLEN(dents, fd->namelen);
    611 
    612 		/*
    613 		 * Check we do not overflow the output buffer
    614 		 * struct fuse_dirent is bigger than struct dirent,
    615 		 * so we should always use fd_len and never reallocate
    616 		 * later.
    617 		 * If we have to reallocate,try to double the buffer
    618 		 * each time so that we do not have to do it too often.
    619 		 */
    620 		if (written + reclen > dents_len) {
    621 			if (dents_len == 0)
    622 				dents_len = fd_len;
    623 			else
    624 				dents_len =
    625 				   MAX(2 * dents_len, written + reclen);
    626 
    627 			dents = PERFUSE_NODE_DATA(opc)->pnd_dirent;
    628 			if ((dents = realloc(dents, dents_len)) == NULL)
    629 				DERR(EX_OSERR, "%s: malloc failed", __func__);
    630 
    631 			PERFUSE_NODE_DATA(opc)->pnd_dirent = dents;
    632 			PERFUSE_NODE_DATA(opc)->pnd_dirent_len = dents_len;
    633 
    634 			/*
    635 			 * (void *) for delint
    636 			 */
    637 			ndp = (char *)(void *)dents + written;
    638 			dents = (struct dirent *)(void *)ndp;
    639 		}
    640 
    641 		/*
    642 		 * Filesystem was mounted without -o use_ino
    643 		 * Perform a lookup to find it.
    644 		 */
    645 		if (fd->ino == PERFUSE_UNKNOWN_INO) {
    646 			struct puffs_node *pn;
    647 			struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
    648 
    649 			/*
    650 			 * Avoid breaking out of fs
    651 			 * by lookup to .. on root
    652 			 */
    653 			if ((strcmp(fd->name, "..") == 0) &&
    654 			    (pnd->pnd_nodeid == FUSE_ROOT_ID)) {
    655 				fd->ino = FUSE_ROOT_ID;
    656 			} else {
    657 				if (node_lookup_common(pu, opc, NULL, fd->name,
    658 						       NULL, &pn) != 0) {
    659 					DWARNX("node_lookup_common failed");
    660 				} else {
    661 					fd->ino = pn->pn_va.va_fileid;
    662 					(void)perfuse_node_reclaim(pu, pn);
    663 				}
    664 			}
    665 		}
    666 
    667 		dents->d_fileno = fd->ino;
    668 		dents->d_reclen = (unsigned short)reclen;
    669 		dents->d_namlen = fd->namelen;
    670 		dents->d_type = fd->type;
    671 		strlcpy(dents->d_name, fd->name, fd->namelen + 1);
    672 
    673 #ifdef PERFUSE_DEBUG
    674 		if (perfuse_diagflags & PDF_READDIR)
    675 			DPRINTF("%s: translated \"%s\" ino = %"PRIu64"\n",
    676 				__func__, dents->d_name, dents->d_fileno);
    677 #endif
    678 
    679 		dents = _DIRENT_NEXT(dents);
    680 		written += reclen;
    681 
    682 		/*
    683 		 * Move to the next record.
    684 		 * fd->off is not the offset, it is an opaque cookie
    685 		 * given by the filesystem to keep state across multiple
    686 		 * readdir() operation.
    687 		 * Use record alignement instead.
    688 		 */
    689 		len = FUSE_DIRENT_ALIGN(sizeof(*fd) + fd->namelen);
    690 #ifdef PERFUSE_DEBUG
    691 		if (perfuse_diagflags & PDF_READDIR)
    692 			DPRINTF("%s: record at %"PRId64"/0x%"PRIx64" "
    693 				"length = %zd/0x%zx. "
    694 				"next record at %"PRId64"/0x%"PRIx64" "
    695 				"max %zd/0x%zx\n",
    696 				__func__, fd_offset, fd_offset, len, len,
    697 				fd_offset + len, fd_offset + len,
    698 				fd_len, fd_len);
    699 #endif
    700 		fd_offset += len;
    701 
    702 		/*
    703 		 * Check if next record is still within the packet
    704 		 * If it is not, we reached the end of the buffer.
    705 		 */
    706 		if (fd_offset >= fd_len)
    707 			break;
    708 
    709 		/*
    710 		 * (void *) for delint
    711 		 */
    712 		ndp = (char *)(void *)fd_base + (size_t)fd_offset;
    713 		fd = (struct fuse_dirent *)(void *)ndp;
    714 
    715 	} while (1 /* CONSTCOND */);
    716 
    717 	/*
    718 	 * Adjust the dirent output length
    719 	 */
    720 	if (written != -1)
    721 		PERFUSE_NODE_DATA(opc)->pnd_dirent_len = written;
    722 
    723 	return written;
    724 }
    725 
    726 static void
    727 readdir_buffered(puffs_cookie_t opc, struct dirent *dent, off_t *readoff,
    728 	size_t *reslen)
    729 {
    730 	struct dirent *fromdent;
    731 	struct perfuse_node_data *pnd;
    732 	char *ndp;
    733 
    734 	pnd = PERFUSE_NODE_DATA(opc);
    735 
    736 	while (*readoff < pnd->pnd_dirent_len) {
    737 		/*
    738 		 * (void *) for delint
    739 		 */
    740 		ndp = (char *)(void *)pnd->pnd_dirent + (size_t)*readoff;
    741 		fromdent = (struct dirent *)(void *)ndp;
    742 
    743 		if (*reslen < _DIRENT_SIZE(fromdent))
    744 			break;
    745 
    746 		memcpy(dent, fromdent, _DIRENT_SIZE(fromdent));
    747 		*readoff += _DIRENT_SIZE(fromdent);
    748 		*reslen -= _DIRENT_SIZE(fromdent);
    749 
    750 		dent = _DIRENT_NEXT(dent);
    751 	}
    752 
    753 #ifdef PERFUSE_DEBUG
    754 	if (perfuse_diagflags & PDF_READDIR)
    755 		DPRINTF("%s: readoff = %"PRId64",  "
    756 			"pnd->pnd_dirent_len = %"PRId64"\n",
    757 			__func__, *readoff, pnd->pnd_dirent_len);
    758 #endif
    759 	if (*readoff >=  pnd->pnd_dirent_len) {
    760 		free(pnd->pnd_dirent);
    761 		pnd->pnd_dirent = NULL;
    762 		pnd->pnd_dirent_len = 0;
    763 	}
    764 
    765 	return;
    766 }
    767 
    768 
    769 static void
    770 node_ref(puffs_cookie_t opc)
    771 {
    772 	struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
    773 
    774 #ifdef PERFUSE_DEBUG
    775 	if (pnd->pnd_flags & PND_INVALID)
    776 		DERRX(EX_SOFTWARE, "Use of freed node opc = %p", opc);
    777 #endif /* PERFUSE_DEBUG */
    778 
    779 	pnd->pnd_ref++;
    780 	return;
    781 }
    782 
    783 static void
    784 node_rele(puffs_cookie_t opc)
    785 {
    786 	struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
    787 
    788 #ifdef PERFUSE_DEBUG
    789 	if (pnd->pnd_flags & PND_INVALID)
    790 		DERRX(EX_SOFTWARE, "Use of freed node opc = %p", opc);
    791 #endif /* PERFUSE_DEBUG */
    792 
    793 	pnd->pnd_ref--;
    794 
    795 	if (pnd->pnd_ref == 0)
    796 		(void)dequeue_requests(opc, PCQ_REF, DEQUEUE_ALL);
    797 
    798 	return;
    799 }
    800 
    801 static void
    802 requeue_request(struct puffs_usermount *pu, puffs_cookie_t opc,
    803 	enum perfuse_qtype type)
    804 {
    805 	struct perfuse_cc_queue pcq;
    806 	struct perfuse_node_data *pnd;
    807 #ifdef PERFUSE_DEBUG
    808 	struct perfuse_state *ps;
    809 
    810 	ps = perfuse_getspecific(pu);
    811 #endif
    812 
    813 	pnd = PERFUSE_NODE_DATA(opc);
    814 	pcq.pcq_type = type;
    815 	pcq.pcq_cc = puffs_cc_getcc(pu);
    816 	TAILQ_INSERT_TAIL(&pnd->pnd_pcq, &pcq, pcq_next);
    817 
    818 #ifdef PERFUSE_DEBUG
    819 	if (perfuse_diagflags & PDF_REQUEUE)
    820 		DPRINTF("%s: REQUEUE opc = %p, pcc = %p (%s)\n",
    821 		        __func__, (void *)opc, pcq.pcq_cc,
    822 			perfuse_qtypestr[type]);
    823 #endif
    824 
    825 	puffs_cc_yield(pcq.pcq_cc);
    826 	TAILQ_REMOVE(&pnd->pnd_pcq, &pcq, pcq_next);
    827 
    828 #ifdef PERFUSE_DEBUG
    829 	if (perfuse_diagflags & PDF_REQUEUE)
    830 		DPRINTF("%s: RESUME opc = %p, pcc = %p (%s)\n",
    831 		        __func__, (void *)opc, pcq.pcq_cc,
    832 			perfuse_qtypestr[type]);
    833 #endif
    834 
    835 	return;
    836 }
    837 
    838 static int
    839 dequeue_requests(puffs_cookie_t opc, enum perfuse_qtype type, int max)
    840 {
    841 	struct perfuse_cc_queue *pcq;
    842 	struct perfuse_node_data *pnd;
    843 	int dequeued;
    844 
    845 	pnd = PERFUSE_NODE_DATA(opc);
    846 	dequeued = 0;
    847 	TAILQ_FOREACH(pcq, &pnd->pnd_pcq, pcq_next) {
    848 		if (pcq->pcq_type != type)
    849 			continue;
    850 
    851 #ifdef PERFUSE_DEBUG
    852 		if (perfuse_diagflags & PDF_REQUEUE)
    853 			DPRINTF("%s: SCHEDULE opc = %p, pcc = %p (%s)\n",
    854 				__func__, (void *)opc, pcq->pcq_cc,
    855 				 perfuse_qtypestr[type]);
    856 #endif
    857 		puffs_cc_schedule(pcq->pcq_cc);
    858 
    859 		if (++dequeued == max)
    860 			break;
    861 	}
    862 
    863 #ifdef PERFUSE_DEBUG
    864 	if (perfuse_diagflags & PDF_REQUEUE)
    865 		DPRINTF("%s: DONE  opc = %p\n", __func__, (void *)opc);
    866 #endif
    867 
    868 	return dequeued;
    869 }
    870 
    871 void
    872 perfuse_fs_init(struct puffs_usermount *pu)
    873 {
    874 	struct perfuse_state *ps;
    875 	perfuse_msg_t *pm;
    876 	struct fuse_init_in *fii;
    877 	struct fuse_init_out *fio;
    878 	int error;
    879 
    880 	ps = puffs_getspecific(pu);
    881 
    882         if (puffs_mount(pu, ps->ps_target, ps->ps_mountflags, ps->ps_root) != 0)
    883                 DERR(EX_OSERR, "%s: puffs_mount failed", __func__);
    884 
    885 	/*
    886 	 * Linux 2.6.34.1 sends theses flags:
    887 	 * FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC
    888 	 * FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK
    889 	 *
    890 	 * Linux also sets max_readahead at 32 pages (128 kB)
    891 	 *
    892 	 * ps_new_msg() is called with NULL creds, which will
    893 	 * be interpreted as FUSE superuser.
    894 	 */
    895 	pm = ps->ps_new_msg(pu, 0, FUSE_INIT, sizeof(*fii), NULL);
    896 	fii = GET_INPAYLOAD(ps, pm, fuse_init_in);
    897 	fii->major = FUSE_KERNEL_VERSION;
    898 	fii->minor = FUSE_KERNEL_MINOR_VERSION;
    899 	fii->max_readahead = (unsigned int)(32 * sysconf(_SC_PAGESIZE));
    900 	fii->flags = (FUSE_ASYNC_READ|FUSE_POSIX_LOCKS|FUSE_ATOMIC_O_TRUNC);
    901 
    902 	if ((error = xchg_msg(pu, 0, pm, sizeof(*fio), wait_reply)) != 0)
    903 		DERRX(EX_SOFTWARE, "init message exchange failed (%d)", error);
    904 
    905 	fio = GET_OUTPAYLOAD(ps, pm, fuse_init_out);
    906 	ps->ps_max_readahead = fio->max_readahead;
    907 	ps->ps_max_write = fio->max_write;
    908 
    909 	ps->ps_destroy_msg(pm);
    910 
    911 	return;
    912 }
    913 
    914 int
    915 perfuse_fs_unmount(struct puffs_usermount *pu, int flags)
    916 {
    917 	perfuse_msg_t *pm;
    918 	struct perfuse_state *ps;
    919 	puffs_cookie_t opc;
    920 	int error;
    921 
    922 	ps = puffs_getspecific(pu);
    923 	opc = (puffs_cookie_t)puffs_getroot(pu);
    924 
    925 	/*
    926 	 * ps_new_msg() is called with NULL creds, which will
    927 	 * be interpreted as FUSE superuser.
    928 	 */
    929 	pm = ps->ps_new_msg(pu, opc, FUSE_DESTROY, 0, NULL);
    930 
    931 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0){
    932 		DWARN("unmount %s", ps->ps_target);
    933 		if (!(flags & MNT_FORCE))
    934 			return error;
    935 		else
    936 			error = 0;
    937 	} else {
    938 		ps->ps_destroy_msg(pm);
    939 	}
    940 
    941 	ps->ps_umount(pu);
    942 
    943 	if (perfuse_diagflags & PDF_MISC)
    944 		DPRINTF("%s unmounted, exit\n", ps->ps_target);
    945 
    946 	return 0;
    947 }
    948 
    949 int
    950 perfuse_fs_statvfs(struct puffs_usermount *pu, struct statvfs *svfsb)
    951 {
    952 	struct perfuse_state *ps;
    953 	perfuse_msg_t *pm;
    954 	puffs_cookie_t opc;
    955 	struct fuse_statfs_out *fso;
    956 	int error;
    957 
    958 	ps = puffs_getspecific(pu);
    959 	opc = (puffs_cookie_t)puffs_getroot(pu);
    960 
    961 	/*
    962 	 * ps_new_msg() is called with NULL creds, which will
    963 	 * be interpreted as FUSE superuser.
    964 	 */
    965 	pm = ps->ps_new_msg(pu, opc, FUSE_STATFS, 0, NULL);
    966 
    967 	if ((error = xchg_msg(pu, opc, pm, sizeof(*fso), wait_reply)) != 0)
    968 		return error;
    969 
    970 	fso = GET_OUTPAYLOAD(ps, pm, fuse_statfs_out);
    971 	svfsb->f_flag = ps->ps_mountflags;
    972 	svfsb->f_bsize = fso->st.bsize;
    973 	svfsb->f_frsize = fso->st.frsize;
    974 	svfsb->f_iosize = ((struct puffs_node *)opc)->pn_va.va_blocksize;
    975 	svfsb->f_blocks = fso->st.blocks;
    976 	svfsb->f_bfree = fso->st.bfree;
    977 	svfsb->f_bavail = fso->st.bavail;
    978 	svfsb->f_bresvd = fso->st.bfree - fso->st.bavail;
    979 	svfsb->f_files = fso->st.files;
    980 	svfsb->f_ffree = fso->st.ffree;
    981 	svfsb->f_favail = fso->st.ffree;/* files not reserved for root */
    982 	svfsb->f_fresvd = 0;		/* files reserved for root */
    983 
    984 	svfsb->f_syncreads = ps->ps_syncreads;
    985 	svfsb->f_syncwrites = ps->ps_syncwrites;
    986 
    987 	svfsb->f_asyncreads = ps->ps_asyncreads;
    988 	svfsb->f_asyncwrites = ps->ps_asyncwrites;
    989 
    990 	(void)memcpy(&svfsb->f_fsidx, &ps->ps_fsid, sizeof(ps->ps_fsid));
    991 	svfsb->f_fsid = (unsigned long)ps->ps_fsid;
    992 	svfsb->f_namemax = MAXPATHLEN;	/* XXX */
    993 	svfsb->f_owner = ps->ps_owner_uid;
    994 
    995 	(void)strlcpy(svfsb->f_mntonname, ps->ps_target, _VFS_NAMELEN);
    996 
    997 	if (ps->ps_filesystemtype != NULL)
    998 		(void)strlcpy(svfsb->f_fstypename,
    999 			      ps->ps_filesystemtype, _VFS_NAMELEN);
   1000 	else
   1001 		(void)strlcpy(svfsb->f_fstypename, "fuse", _VFS_NAMELEN);
   1002 
   1003 	if (ps->ps_source != NULL)
   1004 		strlcpy(svfsb->f_mntfromname, ps->ps_source, _VFS_NAMELEN);
   1005 	else
   1006 		strlcpy(svfsb->f_mntfromname, _PATH_FUSE, _VFS_NAMELEN);
   1007 
   1008 	ps->ps_destroy_msg(pm);
   1009 
   1010 	return 0;
   1011 }
   1012 
   1013 int
   1014 perfuse_fs_sync(struct puffs_usermount *pu, int waitfor,
   1015 	const struct puffs_cred *pcr)
   1016 {
   1017 	/*
   1018 	 * FUSE does not seem to have a FS sync callback.
   1019 	 * Maybe do not even register this callback
   1020 	 */
   1021 	return puffs_fsnop_sync(pu, waitfor, pcr);
   1022 }
   1023 
   1024 /* ARGSUSED0 */
   1025 int
   1026 perfuse_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
   1027 	struct puffs_newinfo *pni)
   1028 {
   1029 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
   1030 	return 0;
   1031 }
   1032 
   1033 /* ARGSUSED0 */
   1034 int
   1035 perfuse_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie,
   1036 	void *fid, size_t *fidsize)
   1037 {
   1038 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
   1039 	return 0;
   1040 }
   1041 
   1042 #if 0
   1043 /* ARGSUSED0 */
   1044 void
   1045 perfuse_fs_extattrctl(struct puffs_usermount *pu, int cmd,
   1046 	puffs_cookie_t *cookie, int flags, int namespace, const char *attrname)
   1047 {
   1048 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
   1049 	return 0;
   1050 }
   1051 #endif /* 0 */
   1052 
   1053 /* ARGSUSED0 */
   1054 void
   1055 perfuse_fs_suspend(struct puffs_usermount *pu, int status)
   1056 {
   1057 	return;
   1058 }
   1059 
   1060 
   1061 int
   1062 perfuse_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
   1063 	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
   1064 {
   1065 	struct perfuse_state *ps;
   1066 	struct puffs_node *pn;
   1067 	mode_t mode;
   1068 	int error;
   1069 
   1070 	ps = puffs_getspecific(pu);
   1071 	node_ref(opc);
   1072 
   1073 	/*
   1074 	 * Check permissions
   1075 	 */
   1076 	switch(pcn->pcn_nameiop) {
   1077 	case NAMEI_DELETE: /* FALLTHROUGH */
   1078 	case NAMEI_RENAME: /* FALLTHROUGH */
   1079 	case NAMEI_CREATE:
   1080 		if (pcn->pcn_flags & NAMEI_ISLASTCN)
   1081 			mode = PUFFS_VEXEC|PUFFS_VWRITE;
   1082 		else
   1083 			mode = PUFFS_VEXEC;
   1084 		break;
   1085 	case NAMEI_LOOKUP: /* FALLTHROUGH */
   1086 	default:
   1087 		mode = PUFFS_VEXEC;
   1088 		break;
   1089 	}
   1090 
   1091 	if ((error = mode_access(opc, pcn->pcn_cred, mode)) != 0)
   1092 		goto out;
   1093 
   1094 	error = node_lookup_common(pu, (puffs_cookie_t)opc, pni,
   1095 				   pcn->pcn_name, pcn->pcn_cred, &pn);
   1096 
   1097 	if (error != 0)
   1098 		goto out;
   1099 
   1100 	/*
   1101 	 * Kernel would kill us if the filesystem returned the parent
   1102 	 * itself. If we want to live, hide that!
   1103 	 */
   1104 	if ((opc == (puffs_cookie_t)pn) && (strcmp(pcn->pcn_name, ".") != 0)) {
   1105 		DERRX(EX_SOFTWARE, "lookup \"%s\" in \"%s\" returned parent",
   1106 		      pcn->pcn_name, perfuse_node_path(ps, opc));
   1107 		/* NOTREACHED */
   1108 		error = ESTALE;
   1109 		goto out;
   1110 	}
   1111 
   1112 	/*
   1113 	 * Removed node
   1114 	 */
   1115 	if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_REMOVED) {
   1116 		error = ENOENT;
   1117 		goto out;
   1118 	}
   1119 
   1120 	/*
   1121 	 * Check for sticky bit. Unfortunately there is no way to
   1122 	 * do this before creating the puffs_node, since we require
   1123 	 * this operation to get the node owner.
   1124 	 */
   1125 	switch (pcn->pcn_nameiop) {
   1126 	case NAMEI_DELETE: /* FALLTHROUGH */
   1127 	case NAMEI_RENAME:
   1128 		error = sticky_access(opc, pn, pcn->pcn_cred);
   1129 		if (error != 0) {
   1130 			(void)perfuse_node_reclaim(pu, pn);
   1131 			goto out;
   1132 		}
   1133 		break;
   1134 	default:
   1135 		break;
   1136 	}
   1137 
   1138 	PERFUSE_NODE_DATA(pn)->pnd_puffs_nlookup++;
   1139 
   1140 	error = 0;
   1141 
   1142 out:
   1143 	node_rele(opc);
   1144 	return error;
   1145 }
   1146 
   1147 int
   1148 perfuse_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
   1149 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
   1150 	const struct vattr *vap)
   1151 {
   1152 	perfuse_msg_t *pm;
   1153 	struct perfuse_state *ps;
   1154 	struct fuse_create_in *fci;
   1155 	struct fuse_entry_out *feo;
   1156 	struct fuse_open_out *foo;
   1157 	struct puffs_node *pn;
   1158 	const char *name;
   1159 	size_t namelen;
   1160 	size_t len;
   1161 	int error;
   1162 
   1163 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   1164 		return ENOENT;
   1165 
   1166 	node_ref(opc);
   1167 
   1168 	/*
   1169 	 * If create is unimplemented: Check that it does not
   1170 	 * already exists, and if not, do mknod and open
   1171 	 */
   1172 	ps = puffs_getspecific(pu);
   1173 	if (ps->ps_flags & PS_NO_CREAT) {
   1174 		error = node_lookup_common(pu, opc, NULL, pcn->pcn_name,
   1175 					   pcn->pcn_cred, &pn);
   1176 		if (error == 0)	{
   1177 			(void)perfuse_node_reclaim(pu, pn);
   1178 			error = EEXIST;
   1179 			goto out;
   1180 		}
   1181 
   1182 		error = perfuse_node_mknod(pu, opc, pni, pcn, vap);
   1183 		if (error != 0)
   1184 			goto out;
   1185 
   1186 		error = node_lookup_common(pu, opc, NULL, pcn->pcn_name,
   1187 					   pcn->pcn_cred, &pn);
   1188 		if (error != 0)
   1189 			goto out;
   1190 
   1191 		/*
   1192 		 * FUSE does the open at create time, while
   1193 		 * NetBSD will open in a subsequent operation.
   1194 		 * We need to open now, in order to retain FUSE
   1195 		 * semantics. The calling process will not get
   1196 		 * a file descriptor before the kernel sends
   1197 		 * the open operation.
   1198 		 */
   1199 		error = perfuse_node_open(pu, (puffs_cookie_t)pn,
   1200 					  FWRITE, pcn->pcn_cred);
   1201 		goto out;
   1202 	}
   1203 
   1204 	name = pcn->pcn_name;
   1205 	namelen = pcn->pcn_namelen + 1;
   1206 	len = sizeof(*fci) + namelen;
   1207 
   1208 	/*
   1209 	 * flags should use O_WRONLY instead of O_RDWR, but it
   1210 	 * breaks when the caller tries to read from file.
   1211 	 *
   1212 	 * mode must contain file type (ie: S_IFREG), use VTTOIF(vap->va_type)
   1213 	 */
   1214 	pm = ps->ps_new_msg(pu, opc, FUSE_CREATE, len, pcn->pcn_cred);
   1215 	fci = GET_INPAYLOAD(ps, pm, fuse_create_in);
   1216 	fci->flags = O_CREAT | O_TRUNC | O_RDWR;
   1217 	fci->mode = vap->va_mode | VTTOIF(vap->va_type);
   1218 	fci->umask = 0; 	/* Seems unused by libfuse */
   1219 	(void)strlcpy((char*)(void *)(fci + 1), name, namelen);
   1220 
   1221 	len = sizeof(*feo) + sizeof(*foo);
   1222 	if ((error = xchg_msg(pu, opc, pm, len, wait_reply)) != 0) {
   1223 		/*
   1224 		 * create is unimplmented, remember it for later,
   1225 		 * and start over using mknod and open instead.
   1226 		 */
   1227 		if (error == ENOSYS) {
   1228 			ps->ps_flags |= PS_NO_CREAT;
   1229 			error = perfuse_node_create(pu, opc, pni, pcn, vap);
   1230 		}
   1231 
   1232 		goto out;
   1233 	}
   1234 
   1235 	feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
   1236 	foo = (struct fuse_open_out *)(void *)(feo + 1);
   1237 	if (feo->nodeid == PERFUSE_UNKNOWN_NODEID)
   1238 		DERRX(EX_SOFTWARE, "%s: no nodeid", __func__);
   1239 
   1240 	/*
   1241 	 * Save the file handle and inode in node private data
   1242 	 * so that we can reuse it later
   1243 	 */
   1244 	pn = perfuse_new_pn(pu, name, opc);
   1245 	perfuse_new_fh((puffs_cookie_t)pn, foo->fh, FWRITE);
   1246 	PERFUSE_NODE_DATA(pn)->pnd_nodeid = feo->nodeid;
   1247 	PERFUSE_NODE_DATA(pn)->pnd_puffs_nlookup++;
   1248 	perfuse_node_cache(ps, pn);
   1249 
   1250 	fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
   1251 	pn->pn_va.va_gen = (u_long)(feo->generation);
   1252 
   1253 	puffs_newinfo_setcookie(pni, pn);
   1254 #ifdef PUFFS_KFLAG_CACHE_FS_TTL
   1255 	puffs_newinfo_setva(pni, &pn->pn_va);
   1256 	perfuse_newinfo_setttl(pni, pn, feo, NULL);
   1257 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */
   1258 
   1259 #ifdef PERFUSE_DEBUG
   1260 	if (perfuse_diagflags & (PDF_FH|PDF_FILENAME))
   1261 		DPRINTF("%s: opc = %p, file = \"%s\", flags = 0x%x "
   1262 			"nodeid = 0x%"PRIx64", wfh = 0x%"PRIx64"\n",
   1263 			__func__, (void *)pn, pcn->pcn_name,
   1264 			PERFUSE_NODE_DATA(pn)->pnd_flags, feo->nodeid,
   1265 			foo->fh);
   1266 #endif
   1267 
   1268 	ps->ps_destroy_msg(pm);
   1269 	error = 0;
   1270 
   1271 out:
   1272 	node_rele(opc);
   1273 	return error;
   1274 }
   1275 
   1276 
   1277 int
   1278 perfuse_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
   1279 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
   1280 	const struct vattr *vap)
   1281 {
   1282 	struct perfuse_state *ps;
   1283 	perfuse_msg_t *pm;
   1284 	struct fuse_mknod_in *fmi;
   1285 	const char* path;
   1286 	size_t len;
   1287 	int error;
   1288 
   1289 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   1290 		return ENOENT;
   1291 
   1292 	node_ref(opc);
   1293 
   1294 	/*
   1295 	 * Only superuser can mknod objects other than
   1296 	 * directories, files, socks, fifo and links.
   1297 	 *
   1298 	 * Create an object require -WX permission in the parent directory
   1299 	 */
   1300 	switch (vap->va_type) {
   1301 	case VDIR:	/* FALLTHROUGH */
   1302 	case VREG:	/* FALLTHROUGH */
   1303 	case VFIFO:	/* FALLTHROUGH */
   1304 	case VSOCK:
   1305 		break;
   1306 	default:	/* VNON, VBLK, VCHR, VBAD */
   1307 		if (!puffs_cred_isjuggernaut(pcn->pcn_cred)) {
   1308 			error = EACCES;
   1309 			goto out;
   1310 		}
   1311 		break;
   1312 	}
   1313 
   1314 
   1315 	ps = puffs_getspecific(pu);
   1316 	path = pcn->pcn_name;
   1317 	len = sizeof(*fmi) + pcn->pcn_namelen + 1;
   1318 
   1319 	/*
   1320 	 * mode must contain file type (ie: S_IFREG), use VTTOIF(vap->va_type)
   1321 	 */
   1322 	pm = ps->ps_new_msg(pu, opc, FUSE_MKNOD, len, pcn->pcn_cred);
   1323 	fmi = GET_INPAYLOAD(ps, pm, fuse_mknod_in);
   1324 	fmi->mode = vap->va_mode | VTTOIF(vap->va_type);
   1325 	fmi->rdev = (uint32_t)vap->va_rdev;
   1326 	fmi->umask = 0; 	/* Seems unused bu libfuse */
   1327 	(void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi));
   1328 
   1329 	error = node_mk_common(pu, opc, pni, pcn, pm);
   1330 
   1331 out:
   1332 	node_rele(opc);
   1333 	return error;
   1334 }
   1335 
   1336 
   1337 int
   1338 perfuse_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
   1339 	const struct puffs_cred *pcr)
   1340 {
   1341 	return perfuse_node_open2(pu, opc, mode, pcr, NULL);
   1342 }
   1343 
   1344 int
   1345 perfuse_node_open2(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
   1346 	const struct puffs_cred *pcr, int *oflags)
   1347 {
   1348 	struct perfuse_state *ps;
   1349 	struct perfuse_node_data *pnd;
   1350 	perfuse_msg_t *pm;
   1351 	mode_t fmode;
   1352 	int op;
   1353 	struct fuse_open_in *foi;
   1354 	struct fuse_open_out *foo;
   1355 	struct puffs_node *pn;
   1356 	int error;
   1357 
   1358 	ps = puffs_getspecific(pu);
   1359 	pn = (struct puffs_node *)opc;
   1360 	pnd = PERFUSE_NODE_DATA(opc);
   1361 	error = 0;
   1362 
   1363 	if (pnd->pnd_flags & PND_REMOVED)
   1364 		return ENOENT;
   1365 
   1366 	node_ref(opc);
   1367 
   1368 	if (puffs_pn_getvap(pn)->va_type == VDIR)
   1369 		op = FUSE_OPENDIR;
   1370 	else
   1371 		op = FUSE_OPEN;
   1372 
   1373 	/*
   1374 	 * libfuse docs says
   1375 	 * - O_CREAT and O_EXCL should never be set.
   1376 	 * - O_TRUNC may be used if mount option atomic_o_trunc is used XXX
   1377 	 *
   1378 	 * O_APPEND makes no sense since FUSE always sends
   1379 	 * the file offset for write operations. If the
   1380 	 * filesystem uses pwrite(), O_APPEND would cause
   1381 	 * the offset to be ignored and cause file corruption.
   1382 	 */
   1383 	mode &= ~(O_CREAT|O_EXCL|O_APPEND);
   1384 
   1385 	/*
   1386 	 * Do not open twice, and do not reopen for reading
   1387 	 * if we already have write handle.
   1388 	 */
   1389 	switch (mode & (FREAD|FWRITE)) {
   1390 	case FREAD:
   1391 		if (pnd->pnd_flags & (PND_RFH|PND_WFH))
   1392 			goto out;
   1393 		break;
   1394 	case FWRITE:
   1395 		if (pnd->pnd_flags & PND_WFH)
   1396 			goto out;
   1397 		break;
   1398 	case FREAD|FWRITE:
   1399 		if (pnd->pnd_flags & PND_WFH)
   1400 			goto out;
   1401 
   1402 		/*
   1403 		 * Corner case: if already open for reading (PND_RFH)
   1404 		 * and re-opening FREAD|FWRITE, we need to reopen,
   1405 		 * but only for writing. Note the change on mode
   1406 		 * will only affect perfuse_new_fh()
   1407 		 */
   1408 		if (pnd->pnd_flags & PND_RFH)
   1409 			mode &= ~FREAD;
   1410 		break;
   1411 	default:
   1412 		DWARNX("open without either FREAD nor FWRITE");
   1413 		error = EPERM;
   1414 		goto out;
   1415 	}
   1416 
   1417 	/*
   1418 	 * Queue open on a node so that we do not open
   1419 	 * twice. This would be better with read and
   1420 	 * write distinguished.
   1421 	 */
   1422 	while (pnd->pnd_flags & PND_INOPEN)
   1423 		requeue_request(pu, opc, PCQ_OPEN);
   1424 	pnd->pnd_flags |= PND_INOPEN;
   1425 
   1426 	/*
   1427 	 * Convert PUFFS mode to FUSE mode: convert FREAD/FWRITE
   1428 	 * to O_RDONLY/O_WRONLY while perserving the other options.
   1429 	 */
   1430 	fmode = mode & ~(FREAD|FWRITE);
   1431 	fmode |= (mode & FWRITE) ? O_RDWR : O_RDONLY;
   1432 
   1433 	pm = ps->ps_new_msg(pu, opc, op, sizeof(*foi), pcr);
   1434 	foi = GET_INPAYLOAD(ps, pm, fuse_open_in);
   1435 	foi->flags = fmode;
   1436 	foi->unused = 0;
   1437 
   1438 	if ((error = xchg_msg(pu, opc, pm, sizeof(*foo), wait_reply)) != 0)
   1439 		goto out;
   1440 
   1441 	foo = GET_OUTPAYLOAD(ps, pm, fuse_open_out);
   1442 
   1443 	/*
   1444 	 * Save the file handle in node private data
   1445 	 * so that we can reuse it later
   1446 	 */
   1447 	perfuse_new_fh(opc, foo->fh, mode);
   1448 
   1449 	/*
   1450 	 * Set direct I/O if the filesystems forces it
   1451 	 */
   1452 	if ((foo->open_flags & FUSE_FOPEN_DIRECT_IO) && (oflags != NULL))
   1453 		*oflags |= PUFFS_OPEN_IO_DIRECT;
   1454 
   1455 #ifdef PERFUSE_DEBUG
   1456 	if (perfuse_diagflags & (PDF_FH|PDF_FILENAME))
   1457 		DPRINTF("%s: opc = %p, file = \"%s\", "
   1458 			"nodeid = 0x%"PRIx64", %s%sfh = 0x%"PRIx64"\n",
   1459 			__func__, (void *)opc, perfuse_node_path(ps, opc),
   1460 			pnd->pnd_nodeid, mode & FREAD ? "r" : "",
   1461 			mode & FWRITE ? "w" : "", foo->fh);
   1462 #endif
   1463 
   1464 	ps->ps_destroy_msg(pm);
   1465 out:
   1466 
   1467 	pnd->pnd_flags &= ~PND_INOPEN;
   1468 	(void)dequeue_requests(opc, PCQ_OPEN, DEQUEUE_ALL);
   1469 
   1470 	node_rele(opc);
   1471 	return error;
   1472 }
   1473 
   1474 /* ARGSUSED0 */
   1475 int
   1476 perfuse_node_close(struct puffs_usermount *pu, puffs_cookie_t opc, int flags,
   1477 	const struct puffs_cred *pcr)
   1478 {
   1479 	struct perfuse_node_data *pnd;
   1480 
   1481 	pnd = PERFUSE_NODE_DATA(opc);
   1482 
   1483 	if (!(pnd->pnd_flags & PND_OPEN))
   1484 		return EBADF;
   1485 
   1486 	/*
   1487 	 * Actual close is postponed at inactive time.
   1488 	 */
   1489 	return 0;
   1490 }
   1491 
   1492 int
   1493 perfuse_node_access(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
   1494 	const struct puffs_cred *pcr)
   1495 {
   1496 	perfuse_msg_t *pm;
   1497 	struct perfuse_state *ps;
   1498 	struct fuse_access_in *fai;
   1499 	int error;
   1500 
   1501 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   1502 		return ENOENT;
   1503 
   1504 	node_ref(opc);
   1505 
   1506 	/*
   1507 	 * If we previously detected the filesystem does not
   1508 	 * implement access(), short-circuit the call and skip
   1509 	 * to libpuffs access() emulation.
   1510 	 */
   1511 	ps = puffs_getspecific(pu);
   1512 	if (ps->ps_flags & PS_NO_ACCESS) {
   1513 		const struct vattr *vap;
   1514 
   1515 		vap = puffs_pn_getvap((struct puffs_node *)opc);
   1516 
   1517 		error = puffs_access(IFTOVT(vap->va_mode),
   1518 				     vap->va_mode & ACCESSPERMS,
   1519 				     vap->va_uid, vap->va_gid,
   1520 				     (mode_t)mode, pcr);
   1521 		goto out;
   1522 	}
   1523 
   1524 	/*
   1525 	 * Plain access call
   1526 	 */
   1527 	pm = ps->ps_new_msg(pu, opc, FUSE_ACCESS, sizeof(*fai), pcr);
   1528 	fai = GET_INPAYLOAD(ps, pm, fuse_access_in);
   1529 	fai->mask = 0;
   1530 	fai->mask |= (mode & PUFFS_VREAD) ? R_OK : 0;
   1531 	fai->mask |= (mode & PUFFS_VWRITE) ? W_OK : 0;
   1532 	fai->mask |= (mode & PUFFS_VEXEC) ? X_OK : 0;
   1533 
   1534 	error = xchg_msg(pu, opc, pm, NO_PAYLOAD_REPLY_LEN, wait_reply);
   1535 
   1536 	ps->ps_destroy_msg(pm);
   1537 
   1538 	/*
   1539 	 * If unimplemented, start over with emulation
   1540 	 */
   1541 	if (error == ENOSYS) {
   1542 		ps->ps_flags |= PS_NO_ACCESS;
   1543 		error = perfuse_node_access(pu, opc, mode, pcr);
   1544 	}
   1545 
   1546 out:
   1547 	node_rele(opc);
   1548 	return error;
   1549 }
   1550 
   1551 int
   1552 perfuse_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   1553 	struct vattr *vap, const struct puffs_cred *pcr)
   1554 {
   1555 	return perfuse_node_getattr_ttl(pu, opc, vap, pcr, NULL);
   1556 }
   1557 
   1558 int
   1559 perfuse_node_getattr_ttl(struct puffs_usermount *pu, puffs_cookie_t opc,
   1560 	struct vattr *vap, const struct puffs_cred *pcr,
   1561 	struct timespec *va_ttl)
   1562 {
   1563 	perfuse_msg_t *pm = NULL;
   1564 	struct perfuse_state *ps;
   1565 	struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
   1566 	struct fuse_getattr_in *fgi;
   1567 	struct fuse_attr_out *fao;
   1568 	int error = 0;
   1569 
   1570 	if ((pnd->pnd_flags & PND_REMOVED) && !(pnd->pnd_flags & PND_OPEN))
   1571 		return ENOENT;
   1572 
   1573 	node_ref(opc);
   1574 
   1575 	/*
   1576 	 * Serialize size access, see comment in perfuse_node_setattr().
   1577 	 */
   1578 	while (pnd->pnd_flags & PND_INRESIZE)
   1579 		requeue_request(pu, opc, PCQ_RESIZE);
   1580 	pnd->pnd_flags |= PND_INRESIZE;
   1581 
   1582 	ps = puffs_getspecific(pu);
   1583 
   1584 	/*
   1585 	 * FUSE_GETATTR_FH must be set in fgi->flags
   1586 	 * if we use for fgi->fh
   1587 	 */
   1588 	pm = ps->ps_new_msg(pu, opc, FUSE_GETATTR, sizeof(*fgi), pcr);
   1589 	fgi = GET_INPAYLOAD(ps, pm, fuse_getattr_in);
   1590 	fgi->getattr_flags = 0;
   1591 	fgi->dummy = 0;
   1592 	fgi->fh = 0;
   1593 
   1594 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_OPEN) {
   1595 		fgi->fh = perfuse_get_fh(opc, FREAD);
   1596 		fgi->getattr_flags |= FUSE_GETATTR_FH;
   1597 	}
   1598 
   1599 #ifdef PERFUSE_DEBUG
   1600 	if (perfuse_diagflags & PDF_RESIZE)
   1601 		DPRINTF(">> %s %p %" PRIu64 "\n", __func__, (void *)opc,
   1602 		    vap->va_size);
   1603 #endif
   1604 
   1605 	if ((error = xchg_msg(pu, opc, pm, sizeof(*fao), wait_reply)) != 0)
   1606 		goto out;
   1607 
   1608 	fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out);
   1609 
   1610 #ifdef PERFUSE_DEBUG
   1611 	if (perfuse_diagflags & PDF_RESIZE)
   1612 		DPRINTF("<< %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__,
   1613 		    (void *)opc, vap->va_size, fao->attr.size);
   1614 #endif
   1615 
   1616 	/*
   1617 	 * We set birthtime, flags, filerev,vaflags to 0.
   1618 	 * This seems the best bet, since the information is
   1619 	 * not available from filesystem.
   1620 	 */
   1621 	fuse_attr_to_vap(ps, vap, &fao->attr);
   1622 
   1623 	if (va_ttl != NULL) {
   1624 		va_ttl->tv_sec = fao->attr_valid;
   1625 		va_ttl->tv_nsec = fao->attr_valid_nsec;
   1626 	}
   1627 
   1628 	ps->ps_destroy_msg(pm);
   1629 	error = 0;
   1630 out:
   1631 
   1632 	pnd->pnd_flags &= ~PND_INRESIZE;
   1633 	(void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL);
   1634 
   1635 	node_rele(opc);
   1636 	return error;
   1637 }
   1638 
   1639 int
   1640 perfuse_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   1641 	const struct vattr *vap, const struct puffs_cred *pcr)
   1642 {
   1643 	return perfuse_node_setattr_ttl(pu, opc,
   1644 					__UNCONST(vap), pcr, NULL, 0);
   1645 }
   1646 
   1647 int
   1648 perfuse_node_setattr_ttl(struct puffs_usermount *pu, puffs_cookie_t opc,
   1649 	struct vattr *vap, const struct puffs_cred *pcr,
   1650 	struct timespec *va_ttl, int xflag)
   1651 {
   1652 	perfuse_msg_t *pm;
   1653 	uint64_t fh;
   1654 	struct perfuse_state *ps;
   1655 	struct perfuse_node_data *pnd;
   1656 	struct fuse_setattr_in *fsi;
   1657 	struct fuse_attr_out *fao;
   1658 	struct vattr *old_va;
   1659 	enum perfuse_xchg_pb_reply reply;
   1660 	int error;
   1661 #ifdef PERFUSE_DEBUG
   1662 	struct vattr *old_vap;
   1663 	int resize_debug = 0;
   1664 #endif
   1665 	ps = puffs_getspecific(pu);
   1666 	pnd = PERFUSE_NODE_DATA(opc);
   1667 
   1668 	/*
   1669 	 * The only operation we can do once the file is removed
   1670 	 * is to resize it, and we can do it only if it is open.
   1671 	 * Do not even send the operation to the filesystem: the
   1672 	 * file is not there anymore.
   1673 	 */
   1674 	if (pnd->pnd_flags & PND_REMOVED) {
   1675 		if (!(pnd->pnd_flags & PND_OPEN))
   1676 			return ENOENT;
   1677 
   1678 		return 0;
   1679 	}
   1680 
   1681 	old_va = puffs_pn_getvap((struct puffs_node *)opc);
   1682 
   1683 	/*
   1684 	 * Check for permission to change size
   1685 	 * It is always allowed if we already have a write file handle
   1686 	 */
   1687 	if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) &&
   1688 	    !(pnd->pnd_flags & PND_WFH) &&
   1689 	    (error = mode_access(opc, pcr, PUFFS_VWRITE)) != 0)
   1690 		return error;
   1691 
   1692 	/*
   1693 	 * Check for permission to change dates
   1694 	 */
   1695 	if (((vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) ||
   1696 	     (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL)) &&
   1697 	    (puffs_access_times(old_va->va_uid, old_va->va_gid,
   1698 				old_va->va_mode, 0, pcr) != 0))
   1699 		return EACCES;
   1700 
   1701 	/*
   1702 	 * Check for permission to change owner and group
   1703 	 */
   1704 	if (((vap->va_uid != (uid_t)PUFFS_VNOVAL) ||
   1705 	     (vap->va_gid != (gid_t)PUFFS_VNOVAL)) &&
   1706 	    (puffs_access_chown(old_va->va_uid, old_va->va_gid,
   1707 				vap->va_uid, vap->va_gid, pcr)) != 0)
   1708 		return EACCES;
   1709 
   1710 	/*
   1711 	 * Check for permission to change permissions
   1712 	 */
   1713 	if ((vap->va_mode != (mode_t)PUFFS_VNOVAL) &&
   1714 	    (puffs_access_chmod(old_va->va_uid, old_va->va_gid,
   1715 				old_va->va_type, vap->va_mode, pcr)) != 0)
   1716 		return EACCES;
   1717 
   1718 	node_ref(opc);
   1719 
   1720 	if (pnd->pnd_flags & PND_WFH)
   1721 		fh = perfuse_get_fh(opc, FWRITE);
   1722 	else
   1723 		fh = FUSE_UNKNOWN_FH;
   1724 
   1725 	/*
   1726 	 * fchmod() sets mode and fh, and it may carry
   1727 	 * a resize as well. That may break if the
   1728 	 * filesystem does chmod then resize, and fails
   1729 	 * because it does not have permission anymore.
   1730 	 * We work this around by splitting into two setattr.
   1731 	 */
   1732 	if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) &&
   1733 	    (vap->va_mode != (mode_t)PUFFS_VNOVAL) &&
   1734 	    (fh != FUSE_UNKNOWN_FH)) {
   1735 		struct vattr resize_va;
   1736 
   1737 		(void)memcpy(&resize_va, vap, sizeof(resize_va));
   1738 		resize_va.va_mode = (mode_t)PUFFS_VNOVAL;
   1739 		if ((error = perfuse_node_setattr_ttl(pu, opc, &resize_va,
   1740 						      pcr, va_ttl, xflag)) != 0)
   1741 			goto out2;
   1742 
   1743 		vap->va_size = (u_quad_t)PUFFS_VNOVAL;
   1744 	}
   1745 
   1746 	pm = ps->ps_new_msg(pu, opc, FUSE_SETATTR, sizeof(*fsi), pcr);
   1747 	fsi = GET_INPAYLOAD(ps, pm, fuse_setattr_in);
   1748 	fsi->valid = 0;
   1749 
   1750 	/*
   1751 	 * Get a fh if the node is open for writing
   1752 	 */
   1753 	if (fh != FUSE_UNKNOWN_FH) {
   1754 		fsi->fh = fh;
   1755 		fsi->valid |= FUSE_FATTR_FH;
   1756 	}
   1757 
   1758 
   1759 	if (vap->va_size != (u_quad_t)PUFFS_VNOVAL) {
   1760 		fsi->size = vap->va_size;
   1761 		fsi->valid |= FUSE_FATTR_SIZE;
   1762 
   1763 		/*
   1764 		 * Serialize anything that can touch file size
   1765 		 * to avoid reordered GETATTR and SETATTR.
   1766 		 * Out of order SETATTR can report stale size,
   1767 		 * which will cause the kernel to truncate the file.
   1768 		 * XXX Probably useless now we have a lock on GETATTR
   1769 		 */
   1770 		while (pnd->pnd_flags & PND_INRESIZE)
   1771 			requeue_request(pu, opc, PCQ_RESIZE);
   1772 		pnd->pnd_flags |= PND_INRESIZE;
   1773 	}
   1774 
   1775 	/*
   1776  	 * Setting mtime without atime or vice versa leads to
   1777 	 * dates being reset to Epoch on glusterfs. If one
   1778 	 * is missing, use the old value.
   1779  	 */
   1780 	if ((vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) ||
   1781 	    (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL)) {
   1782 
   1783 		if (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) {
   1784 			fsi->atime = vap->va_atime.tv_sec;
   1785 			fsi->atimensec = (uint32_t)vap->va_atime.tv_nsec;
   1786 		} else {
   1787 			fsi->atime = old_va->va_atime.tv_sec;
   1788 			fsi->atimensec = (uint32_t)old_va->va_atime.tv_nsec;
   1789 		}
   1790 
   1791 		if (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) {
   1792 			fsi->mtime = vap->va_mtime.tv_sec;
   1793 			fsi->mtimensec = (uint32_t)vap->va_mtime.tv_nsec;
   1794 		} else {
   1795 			fsi->mtime = old_va->va_mtime.tv_sec;
   1796 			fsi->mtimensec = (uint32_t)old_va->va_mtime.tv_nsec;
   1797 		}
   1798 
   1799 		fsi->valid |= (FUSE_FATTR_MTIME|FUSE_FATTR_ATIME);
   1800 	}
   1801 
   1802 	if (vap->va_mode != (mode_t)PUFFS_VNOVAL) {
   1803 		fsi->mode = vap->va_mode;
   1804 		fsi->valid |= FUSE_FATTR_MODE;
   1805 	}
   1806 
   1807 	if (vap->va_uid != (uid_t)PUFFS_VNOVAL) {
   1808 		fsi->uid = vap->va_uid;
   1809 		fsi->valid |= FUSE_FATTR_UID;
   1810 	}
   1811 
   1812 	if (vap->va_gid != (gid_t)PUFFS_VNOVAL) {
   1813 		fsi->gid = vap->va_gid;
   1814 		fsi->valid |= FUSE_FATTR_GID;
   1815 	}
   1816 
   1817 	if (pnd->pnd_lock_owner != 0) {
   1818 		fsi->lock_owner = pnd->pnd_lock_owner;
   1819 		fsi->valid |= FUSE_FATTR_LOCKOWNER;
   1820 	}
   1821 
   1822 	/*
   1823 	 * ftruncate() sends only va_size, and metadata cache
   1824 	 * flush adds va_atime and va_mtime. Some FUSE
   1825 	 * filesystems will attempt to detect ftruncate by
   1826 	 * checking for FATTR_SIZE being set without
   1827 	 * FATTR_UID|FATTR_GID|FATTR_ATIME|FATTR_MTIME|FATTR_MODE
   1828 	 *
   1829 	 * Try to adapt and remove FATTR_ATIME|FATTR_MTIME
   1830 	 * if we suspect a ftruncate().
   1831 	 */
   1832 	if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) &&
   1833 	    ((vap->va_mode == (mode_t)PUFFS_VNOVAL) &&
   1834 	     (vap->va_uid == (uid_t)PUFFS_VNOVAL) &&
   1835 	     (vap->va_gid == (gid_t)PUFFS_VNOVAL))) {
   1836 		fsi->atime = 0;
   1837 		fsi->atimensec = 0;
   1838 		fsi->mtime = 0;
   1839 		fsi->mtimensec = 0;
   1840 		fsi->valid &= ~(FUSE_FATTR_ATIME|FUSE_FATTR_MTIME);
   1841 	}
   1842 
   1843 	/*
   1844 	 * If nothing remain, discard the operation.
   1845 	 */
   1846 	if (!(fsi->valid & (FUSE_FATTR_SIZE|FUSE_FATTR_ATIME|FUSE_FATTR_MTIME|
   1847 			    FUSE_FATTR_MODE|FUSE_FATTR_UID|FUSE_FATTR_GID))) {
   1848 		error = 0;
   1849 		ps->ps_destroy_msg(pm);
   1850 		goto out;
   1851 	}
   1852 
   1853 #ifdef PERFUSE_DEBUG
   1854 	old_vap = puffs_pn_getvap((struct puffs_node *)opc);
   1855 
   1856 	if ((perfuse_diagflags & PDF_RESIZE) &&
   1857 	    (old_vap->va_size != (u_quad_t)PUFFS_VNOVAL)) {
   1858 		resize_debug = 1;
   1859 
   1860 		DPRINTF(">> %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__,
   1861 		    (void *)opc,
   1862 		    puffs_pn_getvap((struct puffs_node *)opc)->va_size,
   1863 		    fsi->size);
   1864 	}
   1865 #endif
   1866 
   1867 	/*
   1868 	 * Do not honour FAF when changing size. How do
   1869 	 * you want such a thing to work?
   1870 	 */
   1871 	reply = wait_reply;
   1872 #ifdef PUFFS_SETATTR_FAF
   1873 	if ((xflag & PUFFS_SETATTR_FAF) && !(fsi->valid & FUSE_FATTR_SIZE))
   1874 		reply = no_reply;
   1875 #endif
   1876 	if ((error = xchg_msg(pu, opc, pm, sizeof(*fao), reply)) != 0)
   1877 		goto out;
   1878 
   1879 	if (reply == no_reply)
   1880 		goto out;
   1881 
   1882 	/*
   1883 	 * Copy back the new values
   1884 	 */
   1885 	fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out);
   1886 
   1887 #ifdef PERFUSE_DEBUG
   1888 	if (resize_debug)
   1889 		DPRINTF("<< %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__,
   1890 		    (void *)opc, old_vap->va_size, fao->attr.size);
   1891 #endif
   1892 
   1893 	fuse_attr_to_vap(ps, old_va, &fao->attr);
   1894 
   1895 	if (va_ttl != NULL) {
   1896 		va_ttl->tv_sec = fao->attr_valid;
   1897 		va_ttl->tv_nsec = fao->attr_valid_nsec;
   1898 		(void)memcpy(vap, old_va, sizeof(*vap));
   1899 	}
   1900 
   1901 	ps->ps_destroy_msg(pm);
   1902 	error = 0;
   1903 
   1904 out:
   1905 	if (pnd->pnd_flags & PND_INRESIZE) {
   1906 		pnd->pnd_flags &= ~PND_INRESIZE;
   1907 		(void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL);
   1908 	}
   1909 
   1910 out2:
   1911 	node_rele(opc);
   1912 	return error;
   1913 }
   1914 
   1915 int
   1916 perfuse_node_poll(struct puffs_usermount *pu, puffs_cookie_t opc, int *events)
   1917 {
   1918 	struct perfuse_state *ps;
   1919 	perfuse_msg_t *pm;
   1920 	struct fuse_poll_in *fpi;
   1921 	struct fuse_poll_out *fpo;
   1922 	int error;
   1923 
   1924 	node_ref(opc);
   1925 	ps = puffs_getspecific(pu);
   1926 	/*
   1927 	 * kh is set if FUSE_POLL_SCHEDULE_NOTIFY is set.
   1928 	 *
   1929 	 * XXX ps_new_msg() is called with NULL creds, which will
   1930 	 * be interpreted as FUSE superuser. We have no way to
   1931 	 * know the requesting process' credential, but since poll
   1932 	 * is supposed to operate on a file that has been open,
   1933 	 * permission should have already been checked at open time.
   1934 	 * That still may breaks on filesystems that provides odd
   1935 	 * semantics.
   1936  	 */
   1937 	pm = ps->ps_new_msg(pu, opc, FUSE_POLL, sizeof(*fpi), NULL);
   1938 	fpi = GET_INPAYLOAD(ps, pm, fuse_poll_in);
   1939 	fpi->fh = perfuse_get_fh(opc, FREAD);
   1940 	fpi->kh = 0;
   1941 	fpi->flags = 0;
   1942 
   1943 #ifdef PERFUSE_DEBUG
   1944 	if (perfuse_diagflags & PDF_FH)
   1945 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", "
   1946 			"fh = 0x%"PRIx64"\n", __func__, (void *)opc,
   1947 			PERFUSE_NODE_DATA(opc)->pnd_nodeid, fpi->fh);
   1948 #endif
   1949 	if ((error = xchg_msg(pu, opc, pm, sizeof(*fpo), wait_reply)) != 0)
   1950 		goto out;
   1951 
   1952 	fpo = GET_OUTPAYLOAD(ps, pm, fuse_poll_out);
   1953 	*events = fpo->revents;
   1954 
   1955 	ps->ps_destroy_msg(pm);
   1956 	error = 0;
   1957 
   1958 out:
   1959 	node_rele(opc);
   1960 	return error;
   1961 }
   1962 
   1963 /* ARGSUSED0 */
   1964 int
   1965 perfuse_node_mmap(struct puffs_usermount *pu, puffs_cookie_t opc, int flags,
   1966 	const struct puffs_cred *pcr)
   1967 {
   1968 	/*
   1969 	 * Not implemented anymore in libfuse
   1970 	 */
   1971 	return ENOSYS;
   1972 }
   1973 
   1974 /* ARGSUSED2 */
   1975 int
   1976 perfuse_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
   1977 	const struct puffs_cred *pcr, int flags, off_t offlo, off_t offhi)
   1978 {
   1979 	int op;
   1980 	perfuse_msg_t *pm;
   1981 	struct perfuse_state *ps;
   1982 	struct perfuse_node_data *pnd;
   1983 	struct fuse_fsync_in *ffi;
   1984 	uint64_t fh;
   1985 	int error = 0;
   1986 
   1987 	pm = NULL;
   1988 	ps = puffs_getspecific(pu);
   1989 	pnd = PERFUSE_NODE_DATA(opc);
   1990 
   1991 	/*
   1992 	 * No need to sync a removed node
   1993 	 */
   1994 	if (pnd->pnd_flags & PND_REMOVED)
   1995 		return 0;
   1996 
   1997 	/*
   1998 	 * We do not sync closed files. They have been
   1999 	 * sync at inactive time already.
   2000 	 */
   2001 	if (!(pnd->pnd_flags & PND_OPEN))
   2002 		return 0;
   2003 
   2004 	node_ref(opc);
   2005 
   2006 	if (puffs_pn_getvap((struct puffs_node *)opc)->va_type == VDIR)
   2007 		op = FUSE_FSYNCDIR;
   2008 	else 		/* VREG but also other types such as VLNK */
   2009 		op = FUSE_FSYNC;
   2010 
   2011 	/*
   2012 	 * Do not sync if there are no change to sync
   2013 	 * XXX remove that test on files if we implement mmap
   2014 	 */
   2015 #ifdef PERFUSE_DEBUG
   2016 	if (perfuse_diagflags & PDF_SYNC)
   2017 		DPRINTF("%s: TEST opc = %p, file = \"%s\" is %sdirty\n",
   2018 			__func__, (void*)opc, perfuse_node_path(ps, opc),
   2019 			pnd->pnd_flags & PND_DIRTY ? "" : "not ");
   2020 #endif
   2021 	if (!(pnd->pnd_flags & PND_DIRTY))
   2022 		goto out;
   2023 
   2024 	/*
   2025 	 * It seems NetBSD can call fsync without open first
   2026 	 * glusterfs complain in such a situation:
   2027 	 * "FSYNC() ERR => -1 (Invalid argument)"
   2028 	 * The file will be closed at inactive time.
   2029 	 *
   2030 	 * We open the directory for reading in order to sync.
   2031 	 * This sounds rather counterintuitive, but it works.
   2032 	 */
   2033 	if (!(pnd->pnd_flags & PND_WFH)) {
   2034 		if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0)
   2035 			goto out;
   2036 	}
   2037 
   2038 	if (op == FUSE_FSYNCDIR)
   2039 		fh = perfuse_get_fh(opc, FREAD);
   2040 	else
   2041 		fh = perfuse_get_fh(opc, FWRITE);
   2042 
   2043 	/*
   2044 	 * If fsync_flags  is set, meta data should not be flushed.
   2045 	 */
   2046 	pm = ps->ps_new_msg(pu, opc, op, sizeof(*ffi), pcr);
   2047 	ffi = GET_INPAYLOAD(ps, pm, fuse_fsync_in);
   2048 	ffi->fh = fh;
   2049 	ffi->fsync_flags = (flags & FFILESYNC) ? 0 : 1;
   2050 
   2051 #ifdef PERFUSE_DEBUG
   2052 	if (perfuse_diagflags & PDF_FH)
   2053 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
   2054 			__func__, (void *)opc,
   2055 			PERFUSE_NODE_DATA(opc)->pnd_nodeid, ffi->fh);
   2056 #endif
   2057 
   2058 	if ((error = xchg_msg(pu, opc, pm,
   2059 			      NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0)
   2060 		goto out;
   2061 
   2062 	/*
   2063 	 * No reply beyond fuse_out_header: nothing to do on success
   2064 	 * just clear the dirty flag
   2065 	 */
   2066 	pnd->pnd_flags &= ~PND_DIRTY;
   2067 
   2068 #ifdef PERFUSE_DEBUG
   2069 	if (perfuse_diagflags & PDF_SYNC)
   2070 		DPRINTF("%s: CLEAR opc = %p, file = \"%s\"\n",
   2071 			__func__, (void*)opc, perfuse_node_path(ps, opc));
   2072 #endif
   2073 
   2074 	ps->ps_destroy_msg(pm);
   2075 	error = 0;
   2076 
   2077 out:
   2078 	/*
   2079 	 * ENOSYS is not returned to kernel,
   2080 	 */
   2081 	if (error == ENOSYS)
   2082 		error = 0;
   2083 
   2084 	node_rele(opc);
   2085 	return error;
   2086 }
   2087 
   2088 /* ARGSUSED0 */
   2089 int
   2090 perfuse_node_seek(struct puffs_usermount *pu, puffs_cookie_t opc,
   2091 	off_t oldoff, off_t newoff, const struct puffs_cred *pcr)
   2092 {
   2093 	return 0;
   2094 }
   2095 
   2096 int
   2097 perfuse_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
   2098 	puffs_cookie_t targ, const struct puffs_cn *pcn)
   2099 {
   2100 	struct perfuse_state *ps;
   2101 	struct perfuse_node_data *pnd;
   2102 	perfuse_msg_t *pm;
   2103 	char *path;
   2104 	const char *name;
   2105 	size_t len;
   2106 	int error;
   2107 
   2108 	pnd = PERFUSE_NODE_DATA(opc);
   2109 
   2110 	if ((pnd->pnd_flags & PND_REMOVED) ||
   2111 	    (PERFUSE_NODE_DATA(targ)->pnd_flags & PND_REMOVED))
   2112 		return ENOENT;
   2113 
   2114 #ifdef PERFUSE_DEBUG
   2115 	if (targ == NULL)
   2116 		DERRX(EX_SOFTWARE, "%s: targ is NULL", __func__);
   2117 
   2118 	if (perfuse_diagflags & (PDF_FH|PDF_FILENAME))
   2119 		DPRINTF("%s: opc = %p, remove opc = %p, file = \"%s\"\n",
   2120 			__func__, (void *)opc, (void *)targ, pcn->pcn_name);
   2121 #endif
   2122 	node_ref(opc);
   2123 	node_ref(targ);
   2124 
   2125 	/*
   2126 	 * Await for all operations on the deleted node to drain,
   2127 	 * as the filesystem may be confused to have it deleted
   2128 	 * during a getattr
   2129 	 */
   2130 	while (PERFUSE_NODE_DATA(targ)->pnd_inxchg)
   2131 		requeue_request(pu, targ, PCQ_AFTERXCHG);
   2132 
   2133 	ps = puffs_getspecific(pu);
   2134 	pnd = PERFUSE_NODE_DATA(opc);
   2135 	name = pcn->pcn_name;
   2136 	len = pcn->pcn_namelen + 1;
   2137 
   2138 	pm = ps->ps_new_msg(pu, opc, FUSE_UNLINK, len, pcn->pcn_cred);
   2139 	path = _GET_INPAYLOAD(ps, pm, char *);
   2140 	(void)strlcpy(path, name, len);
   2141 
   2142 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2143 		goto out;
   2144 
   2145 	perfuse_cache_flush(targ);
   2146 	PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
   2147 
   2148 	if (!(PERFUSE_NODE_DATA(targ)->pnd_flags & PND_OPEN))
   2149 		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
   2150 
   2151 	/*
   2152 	 * The parent directory needs a sync
   2153 	 */
   2154 	PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
   2155 
   2156 #ifdef PERFUSE_DEBUG
   2157 	if (perfuse_diagflags & PDF_FILENAME)
   2158 		DPRINTF("%s: remove nodeid = 0x%"PRIx64" file = \"%s\"\n",
   2159 			__func__, PERFUSE_NODE_DATA(targ)->pnd_nodeid,
   2160 			pcn->pcn_name);
   2161 #endif
   2162 	ps->ps_destroy_msg(pm);
   2163 	error = 0;
   2164 
   2165 out:
   2166 	node_rele(opc);
   2167 	node_rele(targ);
   2168 	return error;
   2169 }
   2170 
   2171 int
   2172 perfuse_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
   2173 	puffs_cookie_t targ, const struct puffs_cn *pcn)
   2174 {
   2175 	struct perfuse_state *ps;
   2176 	perfuse_msg_t *pm;
   2177 	const char *name;
   2178 	size_t len;
   2179 	struct puffs_node *pn;
   2180 	struct fuse_link_in *fli;
   2181 	int error;
   2182 
   2183 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   2184 		return ENOENT;
   2185 
   2186 	node_ref(opc);
   2187 	node_ref(targ);
   2188 	ps = puffs_getspecific(pu);
   2189 	pn = (struct puffs_node *)targ;
   2190 	name = pcn->pcn_name;
   2191 	len =  sizeof(*fli) + pcn->pcn_namelen + 1;
   2192 
   2193 	pm = ps->ps_new_msg(pu, opc, FUSE_LINK, len, pcn->pcn_cred);
   2194 	fli = GET_INPAYLOAD(ps, pm, fuse_link_in);
   2195 	fli->oldnodeid = PERFUSE_NODE_DATA(pn)->pnd_nodeid;
   2196 	(void)strlcpy((char *)(void *)(fli + 1), name, len - sizeof(*fli));
   2197 
   2198 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2199 		goto out;
   2200 
   2201 	ps->ps_destroy_msg(pm);
   2202 	error = 0;
   2203 
   2204 out:
   2205 	node_rele(opc);
   2206 	node_rele(targ);
   2207 	return error;
   2208 }
   2209 
   2210 int
   2211 perfuse_node_rename(struct puffs_usermount *pu, puffs_cookie_t opc,
   2212 	puffs_cookie_t src, const struct puffs_cn *pcn_src,
   2213 	puffs_cookie_t targ_dir, puffs_cookie_t targ,
   2214 	const struct puffs_cn *pcn_targ)
   2215 {
   2216 	struct perfuse_state *ps;
   2217 	struct perfuse_node_data *dstdir_pnd;
   2218 	perfuse_msg_t *pm;
   2219 	struct fuse_rename_in *fri;
   2220 	const char *newname;
   2221 	const char *oldname;
   2222 	char *np;
   2223 	int error;
   2224 	size_t len;
   2225 	size_t newname_len;
   2226 	size_t oldname_len;
   2227 
   2228 	if ((PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) ||
   2229 	    (PERFUSE_NODE_DATA(src)->pnd_flags & PND_REMOVED) ||
   2230 	    (PERFUSE_NODE_DATA(targ_dir)->pnd_flags & PND_REMOVED))
   2231 		return ENOENT;
   2232 
   2233 	node_ref(opc);
   2234 	node_ref(src);
   2235 
   2236 	/*
   2237 	 * Await for all operations on the deleted node to drain,
   2238 	 * as the filesystem may be confused to have it deleted
   2239 	 * during a getattr
   2240 	 */
   2241 	if ((struct puffs_node *)targ != NULL) {
   2242 		node_ref(targ);
   2243 		while (PERFUSE_NODE_DATA(targ)->pnd_inxchg)
   2244 			requeue_request(pu, targ, PCQ_AFTERXCHG);
   2245 	} else {
   2246 		while (PERFUSE_NODE_DATA(src)->pnd_inxchg)
   2247 			requeue_request(pu, src, PCQ_AFTERXCHG);
   2248 	}
   2249 
   2250 	ps = puffs_getspecific(pu);
   2251 	newname =  pcn_targ->pcn_name;
   2252 	newname_len = pcn_targ->pcn_namelen + 1;
   2253 	oldname =  pcn_src->pcn_name;
   2254 	oldname_len = pcn_src->pcn_namelen + 1;
   2255 
   2256 	len = sizeof(*fri) + oldname_len + newname_len;
   2257 	pm = ps->ps_new_msg(pu, opc, FUSE_RENAME, len, pcn_targ->pcn_cred);
   2258 	fri = GET_INPAYLOAD(ps, pm, fuse_rename_in);
   2259 	fri->newdir = PERFUSE_NODE_DATA(targ_dir)->pnd_nodeid;
   2260 	np = (char *)(void *)(fri + 1);
   2261 	(void)strlcpy(np, oldname, oldname_len);
   2262 	np += oldname_len;
   2263 	(void)strlcpy(np, newname, newname_len);
   2264 
   2265 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2266 		goto out;
   2267 
   2268 
   2269 	/*
   2270 	 * Record new parent nodeid
   2271 	 */
   2272 	dstdir_pnd = PERFUSE_NODE_DATA(targ_dir);
   2273 	PERFUSE_NODE_DATA(src)->pnd_parent_nodeid = dstdir_pnd->pnd_nodeid;
   2274 
   2275 	if (opc != targ_dir)
   2276 		dstdir_pnd->pnd_flags |= PND_DIRTY;
   2277 
   2278 	if (strcmp(newname, "..") != 0)
   2279 		(void)strlcpy(PERFUSE_NODE_DATA(src)->pnd_name,
   2280 		    newname, MAXPATHLEN);
   2281 	else
   2282 		PERFUSE_NODE_DATA(src)->pnd_name[0] = 0; /* forget name */
   2283 
   2284 	PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
   2285 
   2286 	if ((struct puffs_node *)targ != NULL) {
   2287 		perfuse_cache_flush(targ);
   2288 		PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
   2289 	}
   2290 
   2291 #ifdef PERFUSE_DEBUG
   2292 	if (perfuse_diagflags & PDF_FILENAME)
   2293 		DPRINTF("%s: nodeid = 0x%"PRIx64" file = \"%s\" renamed \"%s\" "
   2294 			"nodeid = 0x%"PRIx64" -> nodeid = 0x%"PRIx64" \"%s\"\n",
   2295 	 		__func__, PERFUSE_NODE_DATA(src)->pnd_nodeid,
   2296 			pcn_src->pcn_name, pcn_targ->pcn_name,
   2297 			PERFUSE_NODE_DATA(opc)->pnd_nodeid,
   2298 			PERFUSE_NODE_DATA(targ_dir)->pnd_nodeid,
   2299 			perfuse_node_path(ps, targ_dir));
   2300 #endif
   2301 
   2302 	ps->ps_destroy_msg(pm);
   2303 	error = 0;
   2304 
   2305 out:
   2306 	node_rele(opc);
   2307 	node_rele(src);
   2308 	if ((struct puffs_node *)targ != NULL)
   2309 		node_rele(targ);
   2310 
   2311 	return error;
   2312 }
   2313 
   2314 int
   2315 perfuse_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
   2316 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
   2317 	const struct vattr *vap)
   2318 {
   2319 	struct perfuse_state *ps;
   2320 	perfuse_msg_t *pm;
   2321 	struct fuse_mkdir_in *fmi;
   2322 	const char *path;
   2323 	size_t len;
   2324 	int error;
   2325 
   2326 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   2327 		return ENOENT;
   2328 
   2329 	node_ref(opc);
   2330 	ps = puffs_getspecific(pu);
   2331 	path = pcn->pcn_name;
   2332 	len = sizeof(*fmi) + pcn->pcn_namelen + 1;
   2333 
   2334 	pm = ps->ps_new_msg(pu, opc, FUSE_MKDIR, len, pcn->pcn_cred);
   2335 	fmi = GET_INPAYLOAD(ps, pm, fuse_mkdir_in);
   2336 	fmi->mode = vap->va_mode;
   2337 	fmi->umask = 0; 	/* Seems unused by libfuse? */
   2338 	(void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi));
   2339 
   2340 	error = node_mk_common(pu, opc, pni, pcn, pm);
   2341 
   2342 	node_rele(opc);
   2343 	return error;
   2344 }
   2345 
   2346 
   2347 int
   2348 perfuse_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
   2349 	puffs_cookie_t targ, const struct puffs_cn *pcn)
   2350 {
   2351 	struct perfuse_state *ps;
   2352 	struct perfuse_node_data *pnd;
   2353 	perfuse_msg_t *pm;
   2354 	char *path;
   2355 	const char *name;
   2356 	size_t len;
   2357 	int error;
   2358 
   2359 	pnd = PERFUSE_NODE_DATA(opc);
   2360 
   2361 	if ((pnd->pnd_flags & PND_REMOVED) ||
   2362 	    (PERFUSE_NODE_DATA(targ)->pnd_flags & PND_REMOVED))
   2363 		return ENOENT;
   2364 
   2365 	node_ref(opc);
   2366 	node_ref(targ);
   2367 
   2368 	/*
   2369 	 * Await for all operations on the deleted node to drain,
   2370 	 * as the filesystem may be confused to have it deleted
   2371 	 * during a getattr
   2372 	 */
   2373 	while (PERFUSE_NODE_DATA(targ)->pnd_inxchg)
   2374 		requeue_request(pu, targ, PCQ_AFTERXCHG);
   2375 
   2376 	ps = puffs_getspecific(pu);
   2377 	name = pcn->pcn_name;
   2378 	len = pcn->pcn_namelen + 1;
   2379 
   2380 	pm = ps->ps_new_msg(pu, opc, FUSE_RMDIR, len, pcn->pcn_cred);
   2381 	path = _GET_INPAYLOAD(ps, pm, char *);
   2382 	(void)strlcpy(path, name, len);
   2383 
   2384 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2385 		goto out;
   2386 
   2387 	perfuse_cache_flush(targ);
   2388 	PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
   2389 
   2390 	if (!(PERFUSE_NODE_DATA(targ)->pnd_flags & PND_OPEN))
   2391 		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
   2392 
   2393 	/*
   2394 	 * The parent directory needs a sync
   2395 	 */
   2396 	PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
   2397 
   2398 #ifdef PERFUSE_DEBUG
   2399 	if (perfuse_diagflags & PDF_FILENAME)
   2400 		DPRINTF("%s: remove nodeid = 0x%"PRIx64" file = \"%s\"\n",
   2401 			__func__, PERFUSE_NODE_DATA(targ)->pnd_nodeid,
   2402 			perfuse_node_path(ps, targ));
   2403 #endif
   2404 	ps->ps_destroy_msg(pm);
   2405 	error = 0;
   2406 
   2407 out:
   2408 	node_rele(opc);
   2409 	node_rele(targ);
   2410 	return error;
   2411 }
   2412 
   2413 /* vap is unused */
   2414 /* ARGSUSED4 */
   2415 int
   2416 perfuse_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
   2417 	struct puffs_newinfo *pni, const struct puffs_cn *pcn_src,
   2418 	const struct vattr *vap, const char *link_target)
   2419 {
   2420 	struct perfuse_state *ps;
   2421 	perfuse_msg_t *pm;
   2422 	char *np;
   2423 	const char *path;
   2424 	size_t path_len;
   2425 	size_t linkname_len;
   2426 	size_t len;
   2427 	int error;
   2428 
   2429 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   2430 		return ENOENT;
   2431 
   2432 	node_ref(opc);
   2433 	ps = puffs_getspecific(pu);
   2434 	path = pcn_src->pcn_name;
   2435 	path_len = pcn_src->pcn_namelen + 1;
   2436 	linkname_len = strlen(link_target) + 1;
   2437 	len = path_len + linkname_len;
   2438 
   2439 	pm = ps->ps_new_msg(pu, opc, FUSE_SYMLINK, len, pcn_src->pcn_cred);
   2440 	np = _GET_INPAYLOAD(ps, pm, char *);
   2441 	(void)strlcpy(np, path, path_len);
   2442 	np += path_len;
   2443 	(void)strlcpy(np, link_target, linkname_len);
   2444 
   2445 	error = node_mk_common(pu, opc, pni, pcn_src, pm);
   2446 
   2447 	node_rele(opc);
   2448 	return error;
   2449 }
   2450 
   2451 /* ARGSUSED4 */
   2452 int
   2453 perfuse_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
   2454 	struct dirent *dent, off_t *readoff, size_t *reslen,
   2455 	const struct puffs_cred *pcr, int *eofflag, off_t *cookies,
   2456 	size_t *ncookies)
   2457 {
   2458 	perfuse_msg_t *pm;
   2459 	uint64_t fh;
   2460 	struct perfuse_state *ps;
   2461 	struct perfuse_node_data *pnd;
   2462 	struct fuse_read_in *fri;
   2463 	struct fuse_out_header *foh;
   2464 	struct fuse_dirent *fd;
   2465 	size_t foh_len;
   2466 	int error;
   2467 	size_t fd_maxlen;
   2468 
   2469 	error = 0;
   2470 	node_ref(opc);
   2471 	ps = puffs_getspecific(pu);
   2472 
   2473 	/*
   2474 	 * readdir state is kept at node level, and several readdir
   2475 	 * requests can be issued at the same time on the same node.
   2476 	 * We need to queue requests so that only one is in readdir
   2477 	 * code at the same time.
   2478 	 */
   2479 	pnd = PERFUSE_NODE_DATA(opc);
   2480 	while (pnd->pnd_flags & PND_INREADDIR)
   2481 		requeue_request(pu, opc, PCQ_READDIR);
   2482 	pnd->pnd_flags |= PND_INREADDIR;
   2483 
   2484 #ifdef PERFUSE_DEBUG
   2485 	if (perfuse_diagflags & PDF_READDIR)
   2486 		DPRINTF("%s: READDIR opc = %p enter critical section\n",
   2487 			__func__, (void *)opc);
   2488 #endif
   2489 	/*
   2490 	 * Re-initialize pnd->pnd_fd_cookie on the first readdir for a node
   2491 	 */
   2492 	if (*readoff == 0)
   2493 		pnd->pnd_fd_cookie = 0;
   2494 
   2495 	/*
   2496 	 * Do we already have the data bufered?
   2497 	 */
   2498 	if (pnd->pnd_dirent != NULL)
   2499 		goto out;
   2500 	pnd->pnd_dirent_len = 0;
   2501 
   2502 	/*
   2503 	 * It seems NetBSD can call readdir without open first
   2504 	 * libfuse will crash if it is done that way, hence open first.
   2505 	 */
   2506 	if (!(pnd->pnd_flags & PND_OPEN)) {
   2507 		if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0)
   2508 			goto out;
   2509 	}
   2510 
   2511 	fh = perfuse_get_fh(opc, FREAD);
   2512 
   2513 #ifdef PERFUSE_DEBUG
   2514 	if (perfuse_diagflags & PDF_FH)
   2515 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", "
   2516 			"rfh = 0x%"PRIx64"\n", __func__, (void *)opc,
   2517 			PERFUSE_NODE_DATA(opc)->pnd_nodeid, fh);
   2518 #endif
   2519 
   2520 	pnd->pnd_all_fd = NULL;
   2521 	pnd->pnd_all_fd_len = 0;
   2522 	fd_maxlen = ps->ps_max_readahead - sizeof(*foh);
   2523 
   2524 	do {
   2525 		size_t fd_len;
   2526 		char *afdp;
   2527 
   2528 		pm = ps->ps_new_msg(pu, opc, FUSE_READDIR, sizeof(*fri), pcr);
   2529 
   2530 		/*
   2531 		 * read_flags, lock_owner and flags are unused in libfuse
   2532 		 */
   2533 		fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
   2534 		fri->fh = fh;
   2535 		fri->offset = pnd->pnd_fd_cookie;
   2536 		fri->size = (uint32_t)fd_maxlen;
   2537 		fri->read_flags = 0;
   2538 		fri->lock_owner = 0;
   2539 		fri->flags = 0;
   2540 
   2541 		if ((error = xchg_msg(pu, opc, pm,
   2542 				      UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2543 			goto out;
   2544 
   2545 		/*
   2546 		 * There are many puffs_framebufs calls later,
   2547 		 * therefore foh will not be valid for a long time.
   2548 		 * Just get the length and forget it.
   2549 		 */
   2550 		foh = GET_OUTHDR(ps, pm);
   2551 		foh_len = foh->len;
   2552 
   2553 		/*
   2554 		 * Empty read: we reached the end of the buffer.
   2555 		 */
   2556 		if (foh_len == sizeof(*foh)) {
   2557 			ps->ps_destroy_msg(pm);
   2558 			*eofflag = 1;
   2559 			break;
   2560 		}
   2561 
   2562 		/*
   2563 		 * Check for corrupted message.
   2564 		 */
   2565 		if (foh_len < sizeof(*foh) + sizeof(*fd)) {
   2566 			ps->ps_destroy_msg(pm);
   2567 			DWARNX("readdir reply too short");
   2568 			error = EIO;
   2569 			goto out;
   2570 		}
   2571 
   2572 
   2573 		fd = GET_OUTPAYLOAD(ps, pm, fuse_dirent);
   2574 		fd_len = foh_len - sizeof(*foh);
   2575 
   2576 		pnd->pnd_all_fd = realloc(pnd->pnd_all_fd,
   2577 					  pnd->pnd_all_fd_len + fd_len);
   2578 		if (pnd->pnd_all_fd  == NULL)
   2579 			DERR(EX_OSERR, "%s: malloc failed", __func__);
   2580 
   2581 		afdp = (char *)(void *)pnd->pnd_all_fd + pnd->pnd_all_fd_len;
   2582 		(void)memcpy(afdp, fd, fd_len);
   2583 
   2584 		pnd->pnd_all_fd_len += fd_len;
   2585 
   2586 		/*
   2587 		 * The fd->off field is used as a cookie for
   2588 		 * resuming the next readdir() where this one was left.
   2589 	 	 */
   2590 		pnd->pnd_fd_cookie = readdir_last_cookie(fd, fd_len);
   2591 
   2592 		ps->ps_destroy_msg(pm);
   2593 	} while (1 /* CONSTCOND */);
   2594 
   2595 	if (pnd->pnd_all_fd != NULL) {
   2596 		if (fuse_to_dirent(pu, opc, pnd->pnd_all_fd,
   2597 				   pnd->pnd_all_fd_len) == -1)
   2598 			error = EIO;
   2599 	}
   2600 
   2601 out:
   2602 	if (pnd->pnd_all_fd != NULL) {
   2603 		free(pnd->pnd_all_fd);
   2604 		pnd->pnd_all_fd = NULL;
   2605 		pnd->pnd_all_fd_len = 0;
   2606 	}
   2607 
   2608 	if (error == 0)
   2609 		readdir_buffered(opc, dent, readoff, reslen);
   2610 
   2611 	/*
   2612 	 * Schedule queued readdir requests
   2613 	 */
   2614 	pnd->pnd_flags &= ~PND_INREADDIR;
   2615 	(void)dequeue_requests(opc, PCQ_READDIR, DEQUEUE_ALL);
   2616 
   2617 #ifdef PERFUSE_DEBUG
   2618 	if (perfuse_diagflags & PDF_READDIR)
   2619 		DPRINTF("%s: READDIR opc = %p exit critical section\n",
   2620 			__func__, (void *)opc);
   2621 #endif
   2622 
   2623 	node_rele(opc);
   2624 	return error;
   2625 }
   2626 
   2627 int
   2628 perfuse_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
   2629 	const struct puffs_cred *pcr, char *linkname, size_t *linklen)
   2630 {
   2631 	struct perfuse_state *ps;
   2632 	perfuse_msg_t *pm;
   2633 	int error;
   2634 	size_t len;
   2635 	struct fuse_out_header *foh;
   2636 
   2637 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   2638 		return ENOENT;
   2639 
   2640 	node_ref(opc);
   2641 	ps = puffs_getspecific(pu);
   2642 
   2643 	pm = ps->ps_new_msg(pu, opc, FUSE_READLINK, 0, pcr);
   2644 
   2645 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2646 		goto out;
   2647 
   2648 	foh = GET_OUTHDR(ps, pm);
   2649 	len = foh->len - sizeof(*foh);
   2650 	if (len > *linklen)
   2651 		DERRX(EX_PROTOCOL, "path len = %zd too long", len);
   2652 	if (len == 0)
   2653 		DERRX(EX_PROTOCOL, "path len = %zd too short", len);
   2654 
   2655 	/*
   2656 	 * FUSE filesystems return a NUL terminated string, we
   2657 	 * do not want to trailing \0
   2658 	 */
   2659 	*linklen = len - 1;
   2660 	(void)memcpy(linkname, _GET_OUTPAYLOAD(ps, pm, char *), len);
   2661 
   2662 	ps->ps_destroy_msg(pm);
   2663 	error = 0;
   2664 
   2665 out:
   2666 	node_rele(opc);
   2667 	return error;
   2668 }
   2669 
   2670 int
   2671 perfuse_node_reclaim(struct puffs_usermount *pu, puffs_cookie_t opc)
   2672 {
   2673 	struct perfuse_state *ps;
   2674 	perfuse_msg_t *pm;
   2675 	struct perfuse_node_data *pnd;
   2676 	struct fuse_forget_in *ffi;
   2677 	int nlookup;
   2678 	struct timespec now;
   2679 
   2680 	if (opc == 0)
   2681 		return 0;
   2682 
   2683 	ps = puffs_getspecific(pu);
   2684 	pnd = PERFUSE_NODE_DATA(opc);
   2685 
   2686 	/*
   2687 	 * Never forget the root.
   2688 	 */
   2689 	if (pnd->pnd_nodeid == FUSE_ROOT_ID)
   2690 		return 0;
   2691 
   2692 	/*
   2693 	 * There is a race condition between reclaim and lookup.
   2694 	 * When looking up an already known node, the kernel cannot
   2695 	 * hold a reference on the result until it gets the PUFFS
   2696 	 * reply. It mayy therefore reclaim the node after the
   2697 	 * userland looked it up, and before it gets the reply.
   2698 	 * On rely, the kernel re-creates the node, but at that
   2699 	 * time the node has been reclaimed in userland.
   2700 	 *
   2701 	 * In order to avoid this, we refuse reclaiming nodes that
   2702 	 * are too young since the last lookup - and that we do
   2703 	 * not have removed on our own, of course.
   2704 	 */
   2705 	if (clock_gettime(CLOCK_REALTIME, &now) != 0)
   2706 		DERR(EX_OSERR, "clock_gettime failed");
   2707 
   2708 	if (timespeccmp(&pnd->pnd_cn_expire, &now, >) &&
   2709 	    !(pnd->pnd_flags & PND_REMOVED)) {
   2710 		if (!(pnd->pnd_flags & PND_NODELEAK)) {
   2711 			ps->ps_nodeleakcount++;
   2712 			pnd->pnd_flags |= PND_NODELEAK;
   2713 		}
   2714 		DWARNX("possible leaked node:: opc = %p \"%s\"",
   2715 		       opc, pnd->pnd_name);
   2716 		return 0;
   2717 	}
   2718 
   2719 	node_ref(opc);
   2720 	pnd->pnd_flags |= PND_RECLAIMED;
   2721 	pnd->pnd_puffs_nlookup--;
   2722 	nlookup = pnd->pnd_puffs_nlookup;
   2723 
   2724 #ifdef PERFUSE_DEBUG
   2725 	if (perfuse_diagflags & PDF_RECLAIM)
   2726 		DPRINTF("%s (nodeid %"PRId64") reclaimed\n",
   2727 			perfuse_node_path(ps, opc), pnd->pnd_nodeid);
   2728 #endif
   2729 
   2730 #ifdef PERFUSE_DEBUG
   2731 	if (perfuse_diagflags & PDF_RECLAIM)
   2732 		DPRINTF("%s (nodeid %"PRId64") is %sreclaimed, nlookup = %d "
   2733 			"%s%s%s%s, pending ops:%s%s%s\n",
   2734 		        perfuse_node_path(ps, opc), pnd->pnd_nodeid,
   2735 		        pnd->pnd_flags & PND_RECLAIMED ? "" : "not ",
   2736 			pnd->pnd_puffs_nlookup,
   2737 			pnd->pnd_flags & PND_OPEN ? "open " : "not open",
   2738 			pnd->pnd_flags & PND_RFH ? "r" : "",
   2739 			pnd->pnd_flags & PND_WFH ? "w" : "",
   2740 			pnd->pnd_flags & PND_BUSY ? "" : " none",
   2741 			pnd->pnd_flags & PND_INREADDIR ? " readdir" : "",
   2742 			pnd->pnd_flags & PND_INWRITE ? " write" : "",
   2743 			pnd->pnd_flags & PND_INOPEN ? " open" : "");
   2744 #endif
   2745 	/*
   2746 	 * Make sure it is not looked up again
   2747 	 */
   2748 	if (!(pnd->pnd_flags & PND_REMOVED))
   2749 		perfuse_cache_flush(opc);
   2750 
   2751 	/*
   2752 	 * Purge any activity on the node, while checking
   2753 	 * that it remains eligible for a reclaim.
   2754 	 */
   2755 	while (pnd->pnd_ref > 1)
   2756 		requeue_request(pu, opc, PCQ_REF);
   2757 
   2758 	/*
   2759 	 * reclaim cancel?
   2760 	 */
   2761 	if (pnd->pnd_puffs_nlookup > nlookup) {
   2762 		pnd->pnd_flags &= ~PND_RECLAIMED;
   2763 		perfuse_node_cache(ps, opc);
   2764 		node_rele(opc);
   2765 		return 0;
   2766 	}
   2767 
   2768 
   2769 #ifdef PERFUSE_DEBUG
   2770 	if ((pnd->pnd_flags & PND_OPEN) ||
   2771 	       !TAILQ_EMPTY(&pnd->pnd_pcq))
   2772 		DERRX(EX_SOFTWARE, "%s: opc = %p \"%s\": still open",
   2773 		      __func__, opc, pnd->pnd_name);
   2774 
   2775 	if ((pnd->pnd_flags & PND_BUSY) ||
   2776 	       !TAILQ_EMPTY(&pnd->pnd_pcq))
   2777 		DERRX(EX_SOFTWARE, "%s: opc = %p: queued operations",
   2778 		      __func__, opc);
   2779 
   2780 	if (pnd->pnd_inxchg != 0)
   2781 		DERRX(EX_SOFTWARE, "%s: opc = %p: ongoing operations",
   2782 		      __func__, opc);
   2783 #endif
   2784 
   2785 	/*
   2786 	 * Send the FORGET message
   2787 	 *
   2788 	 * ps_new_msg() is called with NULL creds, which will
   2789 	 * be interpreted as FUSE superuser. This is obviously
   2790 	 * fine since we operate with kernel creds here.
   2791 	 */
   2792 	pm = ps->ps_new_msg(pu, opc, FUSE_FORGET,
   2793 		      sizeof(*ffi), NULL);
   2794 	ffi = GET_INPAYLOAD(ps, pm, fuse_forget_in);
   2795 	ffi->nlookup = pnd->pnd_fuse_nlookup;
   2796 
   2797 	/*
   2798 	 * No reply is expected, pm is freed in xchg_msg
   2799 	 */
   2800 	(void)xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, no_reply);
   2801 
   2802 	perfuse_destroy_pn(pu, opc);
   2803 
   2804 	return 0;
   2805 }
   2806 
   2807 int
   2808 perfuse_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc)
   2809 {
   2810 	struct perfuse_node_data *pnd;
   2811 	int error;
   2812 
   2813 	if (opc == 0)
   2814 		return 0;
   2815 
   2816 	node_ref(opc);
   2817 	pnd = PERFUSE_NODE_DATA(opc);
   2818 
   2819 	if (!(pnd->pnd_flags & (PND_OPEN|PND_REMOVED)))
   2820 		goto out;
   2821 
   2822 	/*
   2823 	 * Make sure all operation are finished
   2824 	 * There can be an ongoing write. Other
   2825 	 * operation wait for all data before
   2826 	 * the close/inactive.
   2827 	 */
   2828 	while (pnd->pnd_flags & PND_INWRITE)
   2829 		requeue_request(pu, opc, PCQ_AFTERWRITE);
   2830 
   2831 	/*
   2832 	 * The inactive operation may be cancelled,
   2833 	 * If no open is in progress, set PND_INOPEN
   2834 	 * so that a new open will be queued.
   2835 	 */
   2836 	if (pnd->pnd_flags & PND_INOPEN)
   2837 		goto out;
   2838 
   2839 	pnd->pnd_flags |= PND_INOPEN;
   2840 
   2841 	/*
   2842 	 * Sync data
   2843 	 */
   2844 	if (pnd->pnd_flags & PND_DIRTY) {
   2845 		if ((error = perfuse_node_fsync(pu, opc, NULL, 0, 0, 0)) != 0)
   2846 			DWARN("%s: perfuse_node_fsync failed error = %d",
   2847 			      __func__, error);
   2848 	}
   2849 
   2850 
   2851 	/*
   2852 	 * Close handles
   2853 	 */
   2854 	if (pnd->pnd_flags & PND_WFH) {
   2855 		if ((error = perfuse_node_close_common(pu, opc, FWRITE)) != 0)
   2856 			DWARN("%s: close write FH failed error = %d",
   2857 			      __func__, error);
   2858 	}
   2859 
   2860 	if (pnd->pnd_flags & PND_RFH) {
   2861 		if ((error = perfuse_node_close_common(pu, opc, FREAD)) != 0)
   2862 			DWARN("%s: close read FH failed error = %d",
   2863 			      __func__, error);
   2864 	}
   2865 
   2866 	/*
   2867 	 * This will cause a reclaim to be sent
   2868 	 */
   2869 	if (pnd->pnd_flags & PND_REMOVED)
   2870 		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1);
   2871 
   2872 	/*
   2873 	 * Schedule awaiting operations
   2874 	 */
   2875 	pnd->pnd_flags &= ~PND_INOPEN;
   2876 	(void)dequeue_requests(opc, PCQ_OPEN, DEQUEUE_ALL);
   2877 
   2878 	/*
   2879 	 * errors are ignored, since the kernel ignores the return code.
   2880 	 */
   2881 out:
   2882 	node_rele(opc);
   2883 	return 0;
   2884 }
   2885 
   2886 
   2887 /* ARGSUSED0 */
   2888 int
   2889 perfuse_node_print(struct puffs_usermount *pu, puffs_cookie_t opc)
   2890 {
   2891 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
   2892 	return 0;
   2893 }
   2894 
   2895 /* ARGSUSED0 */
   2896 int
   2897 perfuse_node_pathconf(struct puffs_usermount *pu, puffs_cookie_t opc,
   2898 	int name, int *retval)
   2899 {
   2900 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
   2901 	return 0;
   2902 }
   2903 
   2904 int
   2905 perfuse_node_advlock(struct puffs_usermount *pu, puffs_cookie_t opc,
   2906 	void *id, int op, struct flock *fl, int flags)
   2907 {
   2908 	struct perfuse_state *ps;
   2909 	int fop;
   2910 	perfuse_msg_t *pm;
   2911 	uint64_t fh;
   2912 	struct fuse_lk_in *fli;
   2913 	struct fuse_out_header *foh;
   2914 	struct fuse_lk_out *flo;
   2915 	uint32_t owner;
   2916 	size_t len;
   2917 	int error;
   2918 
   2919 	node_ref(opc);
   2920 
   2921 	/*
   2922 	 * Make sure we do have a filehandle, as the FUSE filesystem
   2923 	 * expect one. E.g.: if we provide none, GlusterFS logs an error
   2924 	 * "0-glusterfs-fuse: xl is NULL"
   2925 	 *
   2926 	 * We need the read file handle if the file is open read only,
   2927 	 * in order to support shared locks on read-only files.
   2928 	 * NB: The kernel always sends advlock for read-only
   2929 	 * files at exit time when the process used lock, see
   2930 	 * sys_exit -> exit1 -> fd_free -> fd_close -> VOP_ADVLOCK
   2931 	 */
   2932 	if ((fh = perfuse_get_fh(opc, FREAD)) == FUSE_UNKNOWN_FH) {
   2933 		error = EBADF;
   2934 		goto out;
   2935 	}
   2936 
   2937 	ps = puffs_getspecific(pu);
   2938 
   2939 	if (op == F_GETLK)
   2940 		fop = FUSE_GETLK;
   2941 	else
   2942 		fop = (flags & F_WAIT) ? FUSE_SETLKW : FUSE_SETLK;
   2943 
   2944 	/*
   2945 	 * XXX ps_new_msg() is called with NULL creds, which will
   2946 	 * be interpreted as FUSE superuser. We have no way to
   2947 	 * know the requesting process' credential, but since advlock()
   2948 	 * is supposed to operate on a file that has been open(),
   2949 	 * permission should have already been checked at open() time.
   2950 	 */
   2951 	pm = ps->ps_new_msg(pu, opc, fop, sizeof(*fli), NULL);
   2952 	fli = GET_INPAYLOAD(ps, pm, fuse_lk_in);
   2953 	fli->fh = fh;
   2954 	fli->owner = (uint64_t)(vaddr_t)id;
   2955 	fli->lk.start = fl->l_start;
   2956 	fli->lk.end = fl->l_start + fl->l_len;
   2957 	fli->lk.type = fl->l_type;
   2958 	fli->lk.pid = fl->l_pid;
   2959 	fli->lk_flags = (flags & F_FLOCK) ? FUSE_LK_FLOCK : 0;
   2960 
   2961 	owner = (uint32_t)(vaddr_t)id;
   2962 
   2963 #ifdef PERFUSE_DEBUG
   2964 	if (perfuse_diagflags & PDF_FH)
   2965 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
   2966 			__func__, (void *)opc,
   2967 			PERFUSE_NODE_DATA(opc)->pnd_nodeid, fli->fh);
   2968 #endif
   2969 
   2970 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2971 		goto out;
   2972 
   2973 	foh = GET_OUTHDR(ps, pm);
   2974 	len = foh->len - sizeof(*foh);
   2975 
   2976 	/*
   2977 	 * Save or clear the lock
   2978 	 */
   2979 	switch (op) {
   2980 	case F_GETLK:
   2981 		if (len != sizeof(*flo))
   2982 			DERRX(EX_SOFTWARE,
   2983 			      "%s: Unexpected lock reply len %zd",
   2984 			      __func__, len);
   2985 
   2986 		flo = GET_OUTPAYLOAD(ps, pm, fuse_lk_out);
   2987 		fl->l_start = flo->lk.start;
   2988 		fl->l_len = flo->lk.end - flo->lk.start;
   2989 		fl->l_pid = flo->lk.pid;
   2990 		fl->l_type = flo->lk.type;
   2991 		fl->l_whence = SEEK_SET;	/* libfuse hardcodes it */
   2992 
   2993 		PERFUSE_NODE_DATA(opc)->pnd_lock_owner = flo->lk.pid;
   2994 		break;
   2995 	case F_UNLCK:
   2996 		owner = 0;
   2997 		/* FALLTHROUGH */
   2998 	case F_SETLK:
   2999 		/* FALLTHROUGH */
   3000 	case F_SETLKW:
   3001 		if (error != 0)
   3002 			PERFUSE_NODE_DATA(opc)->pnd_lock_owner = owner;
   3003 
   3004 		if (len != 0)
   3005 			DERRX(EX_SOFTWARE,
   3006 			      "%s: Unexpected unlock reply len %zd",
   3007 			      __func__, len);
   3008 
   3009 		break;
   3010 	default:
   3011 		DERRX(EX_SOFTWARE, "%s: Unexpected op %d", __func__, op);
   3012 		break;
   3013 	}
   3014 
   3015 	ps->ps_destroy_msg(pm);
   3016 	error = 0;
   3017 
   3018 out:
   3019 	node_rele(opc);
   3020 	return error;
   3021 }
   3022 
   3023 int
   3024 perfuse_node_read(struct puffs_usermount *pu, puffs_cookie_t opc, uint8_t *buf,
   3025 	off_t offset, size_t *resid, const struct puffs_cred *pcr, int ioflag)
   3026 {
   3027 	struct perfuse_state *ps;
   3028 	struct perfuse_node_data *pnd;
   3029 	const struct vattr *vap;
   3030 	perfuse_msg_t *pm;
   3031 	struct fuse_read_in *fri;
   3032 	struct fuse_out_header *foh;
   3033 	size_t readen;
   3034 	int error;
   3035 
   3036 	ps = puffs_getspecific(pu);
   3037 	pnd = PERFUSE_NODE_DATA(opc);
   3038 	vap = puffs_pn_getvap((struct puffs_node *)opc);
   3039 
   3040 	/*
   3041 	 * NetBSD turns that into a getdents(2) output
   3042 	 * We just do a EISDIR as this feature is of little use.
   3043 	 */
   3044 	if (vap->va_type == VDIR)
   3045 		return EISDIR;
   3046 
   3047 	do {
   3048 		size_t max_read;
   3049 
   3050 		max_read = ps->ps_max_readahead - sizeof(*foh);
   3051 		/*
   3052 		 * flags may be set to FUSE_READ_LOCKOWNER
   3053 		 * if lock_owner is provided.
   3054 		 */
   3055 		pm = ps->ps_new_msg(pu, opc, FUSE_READ, sizeof(*fri), pcr);
   3056 		fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
   3057 		fri->fh = perfuse_get_fh(opc, FREAD);
   3058 		fri->offset = offset;
   3059 		fri->size = (uint32_t)MIN(*resid, max_read);
   3060 		fri->read_flags = 0; /* XXX Unused by libfuse? */
   3061 		fri->lock_owner = pnd->pnd_lock_owner;
   3062 		fri->flags = 0;
   3063 		fri->flags |= (fri->lock_owner != 0) ? FUSE_READ_LOCKOWNER : 0;
   3064 
   3065 #ifdef PERFUSE_DEBUG
   3066 	if (perfuse_diagflags & PDF_FH)
   3067 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
   3068 			__func__, (void *)opc, pnd->pnd_nodeid, fri->fh);
   3069 #endif
   3070 		error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply);
   3071 		if (error  != 0)
   3072 			return error;
   3073 
   3074 		foh = GET_OUTHDR(ps, pm);
   3075 		readen = foh->len - sizeof(*foh);
   3076 
   3077 #ifdef PERFUSE_DEBUG
   3078 		if (readen > *resid)
   3079 			DERRX(EX_SOFTWARE, "%s: Unexpected big read %zd",
   3080 			      __func__, readen);
   3081 #endif
   3082 
   3083 		(void)memcpy(buf,  _GET_OUTPAYLOAD(ps, pm, char *), readen);
   3084 
   3085 		buf += readen;
   3086 		offset += readen;
   3087 		*resid -= readen;
   3088 
   3089 		ps->ps_destroy_msg(pm);
   3090 	} while ((*resid != 0) && (readen != 0));
   3091 
   3092 	if (ioflag & (IO_SYNC|IO_DSYNC))
   3093 		ps->ps_syncreads++;
   3094 	else
   3095 		ps->ps_asyncreads++;
   3096 
   3097 	return 0;
   3098 }
   3099 
   3100 int
   3101 perfuse_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
   3102 	uint8_t *buf, off_t offset, size_t *resid,
   3103 	const struct puffs_cred *pcr, int ioflag)
   3104 {
   3105 	return perfuse_node_write2(pu, opc, buf, offset, resid, pcr, ioflag, 0);
   3106 }
   3107 
   3108 /* ARGSUSED7 */
   3109 int
   3110 perfuse_node_write2(struct puffs_usermount *pu, puffs_cookie_t opc,
   3111 	uint8_t *buf, off_t offset, size_t *resid,
   3112 	const struct puffs_cred *pcr, int ioflag, int xflag)
   3113 {
   3114 	struct perfuse_state *ps;
   3115 	struct perfuse_node_data *pnd;
   3116 	struct vattr *vap;
   3117 	perfuse_msg_t *pm;
   3118 	struct fuse_write_in *fwi;
   3119 	struct fuse_write_out *fwo;
   3120 	size_t data_len;
   3121 	size_t payload_len;
   3122 	size_t written;
   3123 	int inresize;
   3124 	int error;
   3125 
   3126 	ps = puffs_getspecific(pu);
   3127 	pnd = PERFUSE_NODE_DATA(opc);
   3128 	vap = puffs_pn_getvap((struct puffs_node *)opc);
   3129 	written = 0;
   3130 	inresize = 0;
   3131 	error = 0;
   3132 
   3133 	if (vap->va_type == VDIR)
   3134 		return EISDIR;
   3135 
   3136 	node_ref(opc);
   3137 
   3138 	/*
   3139 	 * We need to queue write requests in order to avoid
   3140 	 * dequeueing PCQ_AFTERWRITE when there are pending writes.
   3141 	 */
   3142 	while (pnd->pnd_flags & PND_INWRITE)
   3143 		requeue_request(pu, opc, PCQ_WRITE);
   3144 	pnd->pnd_flags |= PND_INWRITE;
   3145 
   3146 	/*
   3147 	 * Serialize size access, see comment in perfuse_node_setattr().
   3148 	 */
   3149 	if ((u_quad_t)offset + *resid > vap->va_size) {
   3150 		while (pnd->pnd_flags & PND_INRESIZE)
   3151 			requeue_request(pu, opc, PCQ_RESIZE);
   3152 		pnd->pnd_flags |= PND_INRESIZE;
   3153 		inresize = 1;
   3154 	}
   3155 
   3156 	/*
   3157 	 * append flag: re-read the file size so that
   3158 	 * we get the latest value.
   3159 	 */
   3160 	if (ioflag & PUFFS_IO_APPEND) {
   3161 		DWARNX("%s: PUFFS_IO_APPEND set, untested code", __func__);
   3162 
   3163 		if ((error = perfuse_node_getattr(pu, opc, vap, pcr)) != 0)
   3164 			goto out;
   3165 
   3166 		offset = vap->va_size;
   3167 	}
   3168 
   3169 #ifdef PERFUSE_DEBUG
   3170 	if (perfuse_diagflags & PDF_RESIZE)
   3171 		DPRINTF(">> %s %p %" PRIu64 "\n", __func__,
   3172 			(void *)opc, vap->va_size);
   3173 #endif
   3174 
   3175 	do {
   3176 		size_t max_write;
   3177 		/*
   3178 		 * There is a writepage flag when data
   3179 		 * is aligned to page size. Use it for
   3180 		 * everything but the data after the last
   3181 		 * page boundary.
   3182 		 */
   3183 		max_write = ps->ps_max_write - sizeof(*fwi);
   3184 
   3185 		data_len = MIN(*resid, max_write);
   3186 		if (data_len > (size_t)sysconf(_SC_PAGESIZE))
   3187 			data_len = data_len & ~(sysconf(_SC_PAGESIZE) - 1);
   3188 
   3189 		payload_len = data_len + sizeof(*fwi);
   3190 
   3191 		/*
   3192 		 * flags may be set to FUSE_WRITE_CACHE (XXX usage?)
   3193 		 * or FUSE_WRITE_LOCKOWNER, if lock_owner is provided.
   3194 		 * write_flags is set to 1 for writepage.
   3195 		 */
   3196 		pm = ps->ps_new_msg(pu, opc, FUSE_WRITE, payload_len, pcr);
   3197 		fwi = GET_INPAYLOAD(ps, pm, fuse_write_in);
   3198 		fwi->fh = perfuse_get_fh(opc, FWRITE);
   3199 		fwi->offset = offset;
   3200 		fwi->size = (uint32_t)data_len;
   3201 		fwi->write_flags = (fwi->size % sysconf(_SC_PAGESIZE)) ? 0 : 1;
   3202 		fwi->lock_owner = pnd->pnd_lock_owner;
   3203 		fwi->flags = 0;
   3204 		fwi->flags |= (fwi->lock_owner != 0) ? FUSE_WRITE_LOCKOWNER : 0;
   3205 		fwi->flags |= (ioflag & IO_DIRECT) ? 0 : FUSE_WRITE_CACHE;
   3206 		(void)memcpy((fwi + 1), buf, data_len);
   3207 
   3208 
   3209 #ifdef PERFUSE_DEBUG
   3210 		if (perfuse_diagflags & PDF_FH)
   3211 			DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", "
   3212 				"fh = 0x%"PRIx64"\n", __func__,
   3213 				(void *)opc, pnd->pnd_nodeid, fwi->fh);
   3214 #endif
   3215 		if ((error = xchg_msg(pu, opc, pm,
   3216 				      sizeof(*fwo), wait_reply)) != 0)
   3217 			goto out;
   3218 
   3219 		fwo = GET_OUTPAYLOAD(ps, pm, fuse_write_out);
   3220 		written = fwo->size;
   3221 		ps->ps_destroy_msg(pm);
   3222 
   3223 #ifdef PERFUSE_DEBUG
   3224 		if (written > *resid)
   3225 			DERRX(EX_SOFTWARE, "%s: Unexpected big write %zd",
   3226 			      __func__, written);
   3227 #endif
   3228 		*resid -= written;
   3229 		offset += written;
   3230 		buf += written;
   3231 
   3232 	} while (*resid != 0);
   3233 
   3234 	/*
   3235 	 * puffs_ops(3) says
   3236 	 *  "everything must be written or an error will be generated"
   3237 	 */
   3238 	if (*resid != 0)
   3239 		error = EFBIG;
   3240 
   3241 #ifdef PERFUSE_DEBUG
   3242 	if (perfuse_diagflags & PDF_RESIZE) {
   3243 		if (offset > (off_t)vap->va_size)
   3244 			DPRINTF("<< %s %p %" PRIu64 " -> %lld\n", __func__,
   3245 				(void *)opc, vap->va_size, (long long)offset);
   3246 		else
   3247 			DPRINTF("<< %s %p \n", __func__, (void *)opc);
   3248 	}
   3249 #endif
   3250 
   3251 	/*
   3252 	 * Update file size if we wrote beyond the end
   3253 	 */
   3254 	if (offset > (off_t)vap->va_size)
   3255 		vap->va_size = offset;
   3256 
   3257 	if (inresize) {
   3258 #ifdef PERFUSE_DEBUG
   3259 		if (!(pnd->pnd_flags & PND_INRESIZE))
   3260 			DERRX(EX_SOFTWARE, "file write grow without resize");
   3261 #endif
   3262 		pnd->pnd_flags &= ~PND_INRESIZE;
   3263 		(void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL);
   3264 	}
   3265 
   3266 
   3267 	/*
   3268 	 * Statistics
   3269 	 */
   3270 	if (ioflag & (IO_SYNC|IO_DSYNC))
   3271 		ps->ps_syncwrites++;
   3272 	else
   3273 		ps->ps_asyncwrites++;
   3274 
   3275 	/*
   3276 	 * Remember to sync the file
   3277 	 */
   3278 	pnd->pnd_flags |= PND_DIRTY;
   3279 
   3280 #ifdef PERFUSE_DEBUG
   3281 	if (perfuse_diagflags & PDF_SYNC)
   3282 		DPRINTF("%s: DIRTY opc = %p, file = \"%s\"\n",
   3283 			__func__, (void*)opc, perfuse_node_path(ps, opc));
   3284 #endif
   3285 
   3286 out:
   3287 	/*
   3288 	 * VOP_PUTPAGE causes FAF write where kernel does not
   3289 	 * check operation result. At least warn if it failed.
   3290 	 */
   3291 #ifdef PUFFS_WRITE_FAF
   3292 	if (error && (xflag & PUFFS_WRITE_FAF))
   3293 		DWARN("Data loss caused by FAF write failed on \"%s\"",
   3294 		      pnd->pnd_name);
   3295 #endif /* PUFFS_WRITE_FAF */
   3296 
   3297 	/*
   3298 	 * If there are no more queued write, we can resume
   3299 	 * an operation awaiting write completion.
   3300 	 */
   3301 	pnd->pnd_flags &= ~PND_INWRITE;
   3302 	if (dequeue_requests(opc, PCQ_WRITE, 1) == 0)
   3303 		(void)dequeue_requests(opc, PCQ_AFTERWRITE, DEQUEUE_ALL);
   3304 
   3305 	node_rele(opc);
   3306 	return error;
   3307 }
   3308 
   3309 /* ARGSUSED0 */
   3310 void
   3311 perfuse_cache_write(struct puffs_usermount *pu, puffs_cookie_t opc, size_t size,
   3312 	struct puffs_cacherun *runs)
   3313 {
   3314 	return;
   3315 }
   3316 
   3317 /* ARGSUSED4 */
   3318 int
   3319 perfuse_node_getextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   3320 	int attrns, const char *attrname, size_t *attrsize, uint8_t *attr,
   3321 	size_t *resid, const struct puffs_cred *pcr)
   3322 {
   3323 	struct perfuse_state *ps;
   3324 	char fuse_attrname[LINUX_XATTR_NAME_MAX + 1];
   3325 	perfuse_msg_t *pm;
   3326 	struct fuse_getxattr_in *fgi;
   3327 	struct fuse_getxattr_out *fgo;
   3328 	struct fuse_out_header *foh;
   3329 	size_t attrnamelen;
   3330 	size_t len;
   3331 	char *np;
   3332 	int error;
   3333 
   3334 	/* system namespace attrs are not accessible to non root users */
   3335 	if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
   3336 		return EPERM;
   3337 
   3338 	node_ref(opc);
   3339 	ps = puffs_getspecific(pu);
   3340 	attrname = perfuse_native_ns(attrns, attrname, fuse_attrname);
   3341 	attrnamelen = strlen(attrname) + 1;
   3342 	len = sizeof(*fgi) + attrnamelen;
   3343 
   3344 	pm = ps->ps_new_msg(pu, opc, FUSE_GETXATTR, len, pcr);
   3345 	fgi = GET_INPAYLOAD(ps, pm, fuse_getxattr_in);
   3346 	fgi->size = (unsigned int)((resid != NULL) ? *resid : 0);
   3347 	np = (char *)(void *)(fgi + 1);
   3348 	(void)strlcpy(np, attrname, attrnamelen);
   3349 
   3350 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   3351 		goto out;
   3352 
   3353 	/*
   3354 	 * We just get fuse_getattr_out with list size if we requested
   3355 	 * a null size.
   3356 	 */
   3357 	if (resid == NULL) {
   3358 		fgo = GET_OUTPAYLOAD(ps, pm, fuse_getxattr_out);
   3359 
   3360 		if (attrsize != NULL)
   3361 			*attrsize = fgo->size;
   3362 
   3363 		ps->ps_destroy_msg(pm);
   3364 		error = 0;
   3365 		goto out;
   3366 	}
   3367 
   3368 	/*
   3369 	 * And with a non null requested size, we get the list just
   3370 	 * after the header
   3371 	 */
   3372 	foh = GET_OUTHDR(ps, pm);
   3373 	np = (char *)(void *)(foh + 1);
   3374 	len = foh->len - sizeof(*foh);
   3375 
   3376 	if (attrsize != NULL)
   3377 		*attrsize = len;
   3378 
   3379 	if (resid != NULL) {
   3380 		if (*resid < len) {
   3381 			error = ERANGE;
   3382 			ps->ps_destroy_msg(pm);
   3383 			goto out;
   3384 		}
   3385 
   3386 		(void)memcpy(attr, np, len);
   3387 		*resid -= len;
   3388 	}
   3389 
   3390 	ps->ps_destroy_msg(pm);
   3391 	error = 0;
   3392 
   3393 out:
   3394 	node_rele(opc);
   3395 	return error;
   3396 }
   3397 
   3398 int
   3399 perfuse_node_setextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   3400 	int attrns, const char *attrname, uint8_t *attr, size_t *resid,
   3401 	const struct puffs_cred *pcr)
   3402 {
   3403 	struct perfuse_state *ps;
   3404 	char fuse_attrname[LINUX_XATTR_NAME_MAX + 1];
   3405 	perfuse_msg_t *pm;
   3406 	struct fuse_setxattr_in *fsi;
   3407 	size_t attrnamelen;
   3408 	size_t len;
   3409 	char *np;
   3410 	int error;
   3411 
   3412 	/* system namespace attrs are not accessible to non root users */
   3413 	if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
   3414 		return EPERM;
   3415 
   3416 	node_ref(opc);
   3417 	ps = puffs_getspecific(pu);
   3418 	attrname = perfuse_native_ns(attrns, attrname, fuse_attrname);
   3419 	attrnamelen = strlen(attrname) + 1;
   3420 	len = sizeof(*fsi) + attrnamelen + *resid;
   3421 
   3422 	pm = ps->ps_new_msg(pu, opc, FUSE_SETXATTR, len, pcr);
   3423 	fsi = GET_INPAYLOAD(ps, pm, fuse_setxattr_in);
   3424 	fsi->size = (unsigned int)*resid;
   3425 	fsi->flags = 0;
   3426 	np = (char *)(void *)(fsi + 1);
   3427 	(void)strlcpy(np, attrname, attrnamelen);
   3428 	np += attrnamelen;
   3429 	(void)memcpy(np, (char *)attr, *resid);
   3430 
   3431 	if ((error = xchg_msg(pu, opc, pm,
   3432 			      NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0)
   3433 		goto out;
   3434 
   3435 	ps->ps_destroy_msg(pm);
   3436 	*resid = 0;
   3437 	error = 0;
   3438 
   3439 out:
   3440 	node_rele(opc);
   3441 	return error;
   3442 }
   3443 
   3444 /* ARGSUSED2 */
   3445 int
   3446 perfuse_node_listextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   3447 	int attrns, size_t *attrsize, uint8_t *attrs, size_t *resid, int flag,
   3448 	const struct puffs_cred *pcr)
   3449 {
   3450 	struct perfuse_state *ps;
   3451 	perfuse_msg_t *pm;
   3452 	struct fuse_getxattr_in *fgi;
   3453 	struct fuse_getxattr_out *fgo;
   3454 	struct fuse_out_header *foh;
   3455 	char *np;
   3456 	size_t len, puffs_len, i, attrlen, outlen;
   3457 	int error;
   3458 
   3459 	/* system namespace attrs are not accessible to non root users */
   3460 	if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
   3461 		return EPERM;
   3462 
   3463 	node_ref(opc);
   3464 
   3465 	ps = puffs_getspecific(pu);
   3466 	len = sizeof(*fgi);
   3467 
   3468 	pm = ps->ps_new_msg(pu, opc, FUSE_LISTXATTR, len, pcr);
   3469 	fgi = GET_INPAYLOAD(ps, pm, fuse_getxattr_in);
   3470 	if (resid != NULL)
   3471 		fgi->size = (unsigned int)*resid;
   3472 	else
   3473 		fgi->size = 0;
   3474 
   3475 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   3476 		goto out;
   3477 
   3478 	/*
   3479 	 * We just get fuse_getattr_out with list size if we requested
   3480 	 * a null size.
   3481 	 */
   3482 	if (resid == NULL) {
   3483 		fgo = GET_OUTPAYLOAD(ps, pm, fuse_getxattr_out);
   3484 
   3485 		if (attrsize != NULL)
   3486 			*attrsize = fgo->size;
   3487 
   3488 		ps->ps_destroy_msg(pm);
   3489 
   3490 		error = 0;
   3491 		goto out;
   3492 	}
   3493 
   3494 	/*
   3495 	 * And with a non null requested size, we get the list just
   3496 	 * after the header
   3497 	 */
   3498 	foh = GET_OUTHDR(ps, pm);
   3499 	np = (char *)(void *)(foh + 1);
   3500 	puffs_len = foh->len - sizeof(*foh);
   3501 
   3502 	if (attrsize != NULL)
   3503 		*attrsize = puffs_len;
   3504 
   3505 	if (attrs != NULL) {
   3506 		if (*resid < puffs_len) {
   3507 			error = ERANGE;
   3508 			ps->ps_destroy_msg(pm);
   3509 			goto out;
   3510 		}
   3511 
   3512 		outlen = 0;
   3513 
   3514 		for (i = 0; i < puffs_len; i += attrlen + 1) {
   3515 			attrlen = strlen(np + i);
   3516 
   3517 			/*
   3518 			 * Filter attributes per namespace
   3519 			 */
   3520 			if (!perfuse_ns_match(attrns, np + i))
   3521 				continue;
   3522 
   3523 #ifdef PUFFS_EXTATTR_LIST_LENPREFIX
   3524 			/*
   3525 			 * Convert the FUSE reply to length prefixed strings
   3526 			 * if this is what the kernel wants.
   3527 			 */
   3528 			if (flag & PUFFS_EXTATTR_LIST_LENPREFIX) {
   3529 				(void)memcpy(attrs + outlen + 1,
   3530 					     np + i, attrlen);
   3531 				*(attrs + outlen) = (uint8_t)attrlen;
   3532 			} else
   3533 #endif /* PUFFS_EXTATTR_LIST_LENPREFIX */
   3534 			(void)memcpy(attrs + outlen, np + i, attrlen + 1);
   3535 			outlen += attrlen + 1;
   3536 		}
   3537 
   3538 		*resid -= outlen;
   3539 	}
   3540 
   3541 	ps->ps_destroy_msg(pm);
   3542 	error = 0;
   3543 
   3544 out:
   3545 	node_rele(opc);
   3546 	return error;
   3547 }
   3548 
   3549 int
   3550 perfuse_node_deleteextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   3551 	int attrns, const char *attrname, const struct puffs_cred *pcr)
   3552 {
   3553 	struct perfuse_state *ps;
   3554 	char fuse_attrname[LINUX_XATTR_NAME_MAX + 1];
   3555 	perfuse_msg_t *pm;
   3556 	size_t attrnamelen;
   3557 	char *np;
   3558 	int error;
   3559 
   3560 	/* system namespace attrs are not accessible to non root users */
   3561 	if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
   3562 		return EPERM;
   3563 
   3564 	node_ref(opc);
   3565 
   3566 	ps = puffs_getspecific(pu);
   3567 	attrname = perfuse_native_ns(attrns, attrname, fuse_attrname);
   3568 	attrnamelen = strlen(attrname) + 1;
   3569 
   3570 	pm = ps->ps_new_msg(pu, opc, FUSE_REMOVEXATTR, attrnamelen, pcr);
   3571 	np = _GET_INPAYLOAD(ps, pm, char *);
   3572 	(void)strlcpy(np, attrname, attrnamelen);
   3573 
   3574 	error = xchg_msg(pu, opc, pm, NO_PAYLOAD_REPLY_LEN, wait_reply);
   3575 
   3576 	ps->ps_destroy_msg(pm);
   3577 
   3578 	node_rele(opc);
   3579 	return error;
   3580 }
   3581