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