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