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