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