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