Home | History | Annotate | Line # | Download | only in libperfuse
ops.c revision 1.80
      1 /*  $NetBSD: ops.c,v 1.80 2014/11/04 09:17:31 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  	 * Setting mtime without atime or vice versa leads to
   1789 	 * dates being reset to Epoch on glusterfs. If one
   1790 	 * is missing, use the old value.
   1791  	 */
   1792 	if ((vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) ||
   1793 	    (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL)) {
   1794 
   1795 		if (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) {
   1796 			fsi->atime = vap->va_atime.tv_sec;
   1797 			fsi->atimensec = (uint32_t)vap->va_atime.tv_nsec;
   1798 		} else {
   1799 			fsi->atime = old_va->va_atime.tv_sec;
   1800 			fsi->atimensec = (uint32_t)old_va->va_atime.tv_nsec;
   1801 		}
   1802 
   1803 		if (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) {
   1804 			fsi->mtime = vap->va_mtime.tv_sec;
   1805 			fsi->mtimensec = (uint32_t)vap->va_mtime.tv_nsec;
   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 		fsi->valid |= (FUSE_FATTR_MTIME|FUSE_FATTR_ATIME);
   1812 	}
   1813 
   1814 	if (vap->va_mode != (mode_t)PUFFS_VNOVAL) {
   1815 		fsi->mode = vap->va_mode;
   1816 		fsi->valid |= FUSE_FATTR_MODE;
   1817 	}
   1818 
   1819 	if (vap->va_uid != (uid_t)PUFFS_VNOVAL) {
   1820 		fsi->uid = vap->va_uid;
   1821 		fsi->valid |= FUSE_FATTR_UID;
   1822 	}
   1823 
   1824 	if (vap->va_gid != (gid_t)PUFFS_VNOVAL) {
   1825 		fsi->gid = vap->va_gid;
   1826 		fsi->valid |= FUSE_FATTR_GID;
   1827 	}
   1828 
   1829 	if (pnd->pnd_lock_owner != 0) {
   1830 		fsi->lock_owner = pnd->pnd_lock_owner;
   1831 		fsi->valid |= FUSE_FATTR_LOCKOWNER;
   1832 	}
   1833 
   1834 	/*
   1835 	 * ftruncate() sends only va_size, and metadata cache
   1836 	 * flush adds va_atime and va_mtime. Some FUSE
   1837 	 * filesystems will attempt to detect ftruncate by
   1838 	 * checking for FATTR_SIZE being set without
   1839 	 * FATTR_UID|FATTR_GID|FATTR_ATIME|FATTR_MTIME|FATTR_MODE
   1840 	 *
   1841 	 * Try to adapt and remove FATTR_ATIME|FATTR_MTIME
   1842 	 * if we suspect a ftruncate().
   1843 	 */
   1844 	if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) &&
   1845 	    ((vap->va_mode == (mode_t)PUFFS_VNOVAL) &&
   1846 	     (vap->va_uid == (uid_t)PUFFS_VNOVAL) &&
   1847 	     (vap->va_gid == (gid_t)PUFFS_VNOVAL))) {
   1848 		fsi->atime = 0;
   1849 		fsi->atimensec = 0;
   1850 		fsi->mtime = 0;
   1851 		fsi->mtimensec = 0;
   1852 		fsi->valid &= ~(FUSE_FATTR_ATIME|FUSE_FATTR_MTIME);
   1853 	}
   1854 
   1855 	/*
   1856 	 * If nothing remain, discard the operation.
   1857 	 */
   1858 	if (!(fsi->valid & (FUSE_FATTR_SIZE|FUSE_FATTR_ATIME|FUSE_FATTR_MTIME|
   1859 			    FUSE_FATTR_MODE|FUSE_FATTR_UID|FUSE_FATTR_GID))) {
   1860 		error = 0;
   1861 		ps->ps_destroy_msg(pm);
   1862 		goto out;
   1863 	}
   1864 
   1865 #ifdef PERFUSE_DEBUG
   1866 	old_vap = puffs_pn_getvap((struct puffs_node *)opc);
   1867 
   1868 	if ((perfuse_diagflags & PDF_RESIZE) &&
   1869 	    (old_vap->va_size != (u_quad_t)PUFFS_VNOVAL)) {
   1870 		resize_debug = 1;
   1871 
   1872 		DPRINTF(">> %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__,
   1873 		    (void *)opc,
   1874 		    puffs_pn_getvap((struct puffs_node *)opc)->va_size,
   1875 		    fsi->size);
   1876 	}
   1877 #endif
   1878 
   1879 	/*
   1880 	 * Do not honour FAF when changing size. How do
   1881 	 * you want such a thing to work?
   1882 	 */
   1883 	reply = wait_reply;
   1884 #ifdef PUFFS_SETATTR_FAF
   1885 	if ((xflag & PUFFS_SETATTR_FAF) && !(fsi->valid & FUSE_FATTR_SIZE))
   1886 		reply = no_reply;
   1887 #endif
   1888 	if ((error = xchg_msg(pu, opc, pm, sizeof(*fao), reply)) != 0)
   1889 		goto out;
   1890 
   1891 	if (reply == no_reply)
   1892 		goto out;
   1893 
   1894 	/*
   1895 	 * Copy back the new values
   1896 	 */
   1897 	fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out);
   1898 
   1899 #ifdef PERFUSE_DEBUG
   1900 	if (resize_debug)
   1901 		DPRINTF("<< %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__,
   1902 		    (void *)opc, old_vap->va_size, fao->attr.size);
   1903 #endif
   1904 
   1905 	fuse_attr_to_vap(ps, old_va, &fao->attr);
   1906 
   1907 	if (va_ttl != NULL) {
   1908 		va_ttl->tv_sec = fao->attr_valid;
   1909 		va_ttl->tv_nsec = fao->attr_valid_nsec;
   1910 		(void)memcpy(vap, old_va, sizeof(*vap));
   1911 	}
   1912 
   1913 	ps->ps_destroy_msg(pm);
   1914 	error = 0;
   1915 
   1916 out:
   1917 	if (pnd->pnd_flags & PND_INRESIZE) {
   1918 		pnd->pnd_flags &= ~PND_INRESIZE;
   1919 		(void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL);
   1920 	}
   1921 
   1922 out2:
   1923 	node_rele(opc);
   1924 	return error;
   1925 }
   1926 
   1927 int
   1928 perfuse_node_poll(struct puffs_usermount *pu, puffs_cookie_t opc, int *events)
   1929 {
   1930 	struct perfuse_state *ps;
   1931 	perfuse_msg_t *pm;
   1932 	struct fuse_poll_in *fpi;
   1933 	struct fuse_poll_out *fpo;
   1934 	int error;
   1935 
   1936 	node_ref(opc);
   1937 	ps = puffs_getspecific(pu);
   1938 	/*
   1939 	 * kh is set if FUSE_POLL_SCHEDULE_NOTIFY is set.
   1940 	 *
   1941 	 * XXX ps_new_msg() is called with NULL creds, which will
   1942 	 * be interpreted as FUSE superuser. We have no way to
   1943 	 * know the requesting process' credential, but since poll
   1944 	 * is supposed to operate on a file that has been open,
   1945 	 * permission should have already been checked at open time.
   1946 	 * That still may breaks on filesystems that provides odd
   1947 	 * semantics.
   1948  	 */
   1949 	pm = ps->ps_new_msg(pu, opc, FUSE_POLL, sizeof(*fpi), NULL);
   1950 	fpi = GET_INPAYLOAD(ps, pm, fuse_poll_in);
   1951 	fpi->fh = perfuse_get_fh(opc, FREAD);
   1952 	fpi->kh = 0;
   1953 	fpi->flags = 0;
   1954 
   1955 #ifdef PERFUSE_DEBUG
   1956 	if (perfuse_diagflags & PDF_FH)
   1957 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", "
   1958 			"fh = 0x%"PRIx64"\n", __func__, (void *)opc,
   1959 			PERFUSE_NODE_DATA(opc)->pnd_nodeid, fpi->fh);
   1960 #endif
   1961 	if ((error = xchg_msg(pu, opc, pm, sizeof(*fpo), wait_reply)) != 0)
   1962 		goto out;
   1963 
   1964 	fpo = GET_OUTPAYLOAD(ps, pm, fuse_poll_out);
   1965 	*events = fpo->revents;
   1966 
   1967 	ps->ps_destroy_msg(pm);
   1968 	error = 0;
   1969 
   1970 out:
   1971 	node_rele(opc);
   1972 	return error;
   1973 }
   1974 
   1975 /* ARGSUSED2 */
   1976 int
   1977 perfuse_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
   1978 	const struct puffs_cred *pcr, int flags, off_t offlo, off_t offhi)
   1979 {
   1980 	int op;
   1981 	perfuse_msg_t *pm;
   1982 	struct perfuse_state *ps;
   1983 	struct perfuse_node_data *pnd;
   1984 	struct fuse_fsync_in *ffi;
   1985 	uint64_t fh;
   1986 	int error = 0;
   1987 
   1988 	pm = NULL;
   1989 	ps = puffs_getspecific(pu);
   1990 	pnd = PERFUSE_NODE_DATA(opc);
   1991 
   1992 	/*
   1993 	 * No need to sync a removed node
   1994 	 */
   1995 	if (pnd->pnd_flags & PND_REMOVED)
   1996 		return 0;
   1997 
   1998 	/*
   1999 	 * We do not sync closed files. They have been
   2000 	 * sync at inactive time already.
   2001 	 */
   2002 	if (!(pnd->pnd_flags & PND_OPEN))
   2003 		return 0;
   2004 
   2005 	node_ref(opc);
   2006 
   2007 	if (puffs_pn_getvap((struct puffs_node *)opc)->va_type == VDIR)
   2008 		op = FUSE_FSYNCDIR;
   2009 	else 		/* VREG but also other types such as VLNK */
   2010 		op = FUSE_FSYNC;
   2011 
   2012 	/*
   2013 	 * Do not sync if there are no change to sync
   2014 	 * XXX remove that test on files if we implement mmap
   2015 	 */
   2016 #ifdef PERFUSE_DEBUG
   2017 	if (perfuse_diagflags & PDF_SYNC)
   2018 		DPRINTF("%s: TEST opc = %p, file = \"%s\" is %sdirty\n",
   2019 			__func__, (void*)opc, perfuse_node_path(ps, opc),
   2020 			pnd->pnd_flags & PND_DIRTY ? "" : "not ");
   2021 #endif
   2022 	if (!(pnd->pnd_flags & PND_DIRTY))
   2023 		goto out;
   2024 
   2025 	/*
   2026 	 * It seems NetBSD can call fsync without open first
   2027 	 * glusterfs complain in such a situation:
   2028 	 * "FSYNC() ERR => -1 (Invalid argument)"
   2029 	 * The file will be closed at inactive time.
   2030 	 *
   2031 	 * We open the directory for reading in order to sync.
   2032 	 * This sounds rather counterintuitive, but it works.
   2033 	 */
   2034 	if (!(pnd->pnd_flags & PND_WFH)) {
   2035 		if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0)
   2036 			goto out;
   2037 	}
   2038 
   2039 	if (op == FUSE_FSYNCDIR)
   2040 		fh = perfuse_get_fh(opc, FREAD);
   2041 	else
   2042 		fh = perfuse_get_fh(opc, FWRITE);
   2043 
   2044 	/*
   2045 	 * If fsync_flags  is set, meta data should not be flushed.
   2046 	 */
   2047 	pm = ps->ps_new_msg(pu, opc, op, sizeof(*ffi), pcr);
   2048 	ffi = GET_INPAYLOAD(ps, pm, fuse_fsync_in);
   2049 	ffi->fh = fh;
   2050 	ffi->fsync_flags = (flags & FFILESYNC) ? 0 : 1;
   2051 
   2052 #ifdef PERFUSE_DEBUG
   2053 	if (perfuse_diagflags & PDF_FH)
   2054 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
   2055 			__func__, (void *)opc,
   2056 			PERFUSE_NODE_DATA(opc)->pnd_nodeid, ffi->fh);
   2057 #endif
   2058 
   2059 	if ((error = xchg_msg(pu, opc, pm,
   2060 			      NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0)
   2061 		goto out;
   2062 
   2063 	/*
   2064 	 * No reply beyond fuse_out_header: nothing to do on success
   2065 	 * just clear the dirty flag
   2066 	 */
   2067 	pnd->pnd_flags &= ~PND_DIRTY;
   2068 
   2069 #ifdef PERFUSE_DEBUG
   2070 	if (perfuse_diagflags & PDF_SYNC)
   2071 		DPRINTF("%s: CLEAR opc = %p, file = \"%s\"\n",
   2072 			__func__, (void*)opc, perfuse_node_path(ps, opc));
   2073 #endif
   2074 
   2075 	ps->ps_destroy_msg(pm);
   2076 	error = 0;
   2077 
   2078 out:
   2079 	/*
   2080 	 * ENOSYS is not returned to kernel,
   2081 	 */
   2082 	if (error == ENOSYS)
   2083 		error = 0;
   2084 
   2085 	node_rele(opc);
   2086 	return error;
   2087 }
   2088 
   2089 int
   2090 perfuse_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
   2091 	puffs_cookie_t targ, const struct puffs_cn *pcn)
   2092 {
   2093 	struct perfuse_state *ps;
   2094 	struct perfuse_node_data *pnd;
   2095 	perfuse_msg_t *pm;
   2096 	char *path;
   2097 	const char *name;
   2098 	size_t len;
   2099 	int error;
   2100 
   2101 	pnd = PERFUSE_NODE_DATA(opc);
   2102 
   2103 	if ((pnd->pnd_flags & PND_REMOVED) ||
   2104 	    (PERFUSE_NODE_DATA(targ)->pnd_flags & PND_REMOVED))
   2105 		return ENOENT;
   2106 
   2107 #ifdef PERFUSE_DEBUG
   2108 	if (targ == NULL)
   2109 		DERRX(EX_SOFTWARE, "%s: targ is NULL", __func__);
   2110 
   2111 	if (perfuse_diagflags & (PDF_FH|PDF_FILENAME))
   2112 		DPRINTF("%s: opc = %p, remove opc = %p, file = \"%s\"\n",
   2113 			__func__, (void *)opc, (void *)targ, pcn->pcn_name);
   2114 #endif
   2115 	node_ref(opc);
   2116 	node_ref(targ);
   2117 
   2118 	/*
   2119 	 * Await for all operations on the deleted node to drain,
   2120 	 * as the filesystem may be confused to have it deleted
   2121 	 * during a getattr
   2122 	 */
   2123 	while (PERFUSE_NODE_DATA(targ)->pnd_inxchg)
   2124 		requeue_request(pu, targ, PCQ_AFTERXCHG);
   2125 
   2126 	ps = puffs_getspecific(pu);
   2127 	pnd = PERFUSE_NODE_DATA(opc);
   2128 	name = pcn->pcn_name;
   2129 	len = pcn->pcn_namelen + 1;
   2130 
   2131 	pm = ps->ps_new_msg(pu, opc, FUSE_UNLINK, len, pcn->pcn_cred);
   2132 	path = _GET_INPAYLOAD(ps, pm, char *);
   2133 	(void)strlcpy(path, name, len);
   2134 
   2135 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2136 		goto out;
   2137 
   2138 	perfuse_cache_flush(targ);
   2139 	PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
   2140 
   2141 	if (!(PERFUSE_NODE_DATA(targ)->pnd_flags & PND_OPEN))
   2142 		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
   2143 
   2144 	/*
   2145 	 * The parent directory needs a sync
   2146 	 */
   2147 	PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
   2148 
   2149 #ifdef PERFUSE_DEBUG
   2150 	if (perfuse_diagflags & PDF_FILENAME)
   2151 		DPRINTF("%s: remove nodeid = 0x%"PRIx64" file = \"%s\"\n",
   2152 			__func__, PERFUSE_NODE_DATA(targ)->pnd_nodeid,
   2153 			pcn->pcn_name);
   2154 #endif
   2155 	ps->ps_destroy_msg(pm);
   2156 	error = 0;
   2157 
   2158 out:
   2159 	node_rele(opc);
   2160 	node_rele(targ);
   2161 	return error;
   2162 }
   2163 
   2164 int
   2165 perfuse_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
   2166 	puffs_cookie_t targ, const struct puffs_cn *pcn)
   2167 {
   2168 	struct perfuse_state *ps;
   2169 	perfuse_msg_t *pm;
   2170 	const char *name;
   2171 	size_t len;
   2172 	struct puffs_node *pn;
   2173 	struct fuse_link_in *fli;
   2174 	int error;
   2175 
   2176 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   2177 		return ENOENT;
   2178 
   2179 	node_ref(opc);
   2180 	node_ref(targ);
   2181 	ps = puffs_getspecific(pu);
   2182 	pn = (struct puffs_node *)targ;
   2183 	name = pcn->pcn_name;
   2184 	len =  sizeof(*fli) + pcn->pcn_namelen + 1;
   2185 
   2186 	pm = ps->ps_new_msg(pu, opc, FUSE_LINK, len, pcn->pcn_cred);
   2187 	fli = GET_INPAYLOAD(ps, pm, fuse_link_in);
   2188 	fli->oldnodeid = PERFUSE_NODE_DATA(pn)->pnd_nodeid;
   2189 	(void)strlcpy((char *)(void *)(fli + 1), name, len - sizeof(*fli));
   2190 
   2191 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2192 		goto out;
   2193 
   2194 	ps->ps_destroy_msg(pm);
   2195 	error = 0;
   2196 
   2197 out:
   2198 	node_rele(opc);
   2199 	node_rele(targ);
   2200 	return error;
   2201 }
   2202 
   2203 int
   2204 perfuse_node_rename(struct puffs_usermount *pu, puffs_cookie_t opc,
   2205 	puffs_cookie_t src, const struct puffs_cn *pcn_src,
   2206 	puffs_cookie_t targ_dir, puffs_cookie_t targ,
   2207 	const struct puffs_cn *pcn_targ)
   2208 {
   2209 	struct perfuse_state *ps;
   2210 	struct perfuse_node_data *dstdir_pnd;
   2211 	perfuse_msg_t *pm;
   2212 	struct fuse_rename_in *fri;
   2213 	const char *newname;
   2214 	const char *oldname;
   2215 	char *np;
   2216 	int error;
   2217 	size_t len;
   2218 	size_t newname_len;
   2219 	size_t oldname_len;
   2220 
   2221 	if ((PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) ||
   2222 	    (PERFUSE_NODE_DATA(src)->pnd_flags & PND_REMOVED) ||
   2223 	    (PERFUSE_NODE_DATA(targ_dir)->pnd_flags & PND_REMOVED))
   2224 		return ENOENT;
   2225 
   2226 	node_ref(opc);
   2227 	node_ref(src);
   2228 
   2229 	/*
   2230 	 * Await for all operations on the deleted node to drain,
   2231 	 * as the filesystem may be confused to have it deleted
   2232 	 * during a getattr
   2233 	 */
   2234 	if ((struct puffs_node *)targ != NULL) {
   2235 		node_ref(targ);
   2236 		while (PERFUSE_NODE_DATA(targ)->pnd_inxchg)
   2237 			requeue_request(pu, targ, PCQ_AFTERXCHG);
   2238 	} else {
   2239 		while (PERFUSE_NODE_DATA(src)->pnd_inxchg)
   2240 			requeue_request(pu, src, PCQ_AFTERXCHG);
   2241 	}
   2242 
   2243 	ps = puffs_getspecific(pu);
   2244 	newname =  pcn_targ->pcn_name;
   2245 	newname_len = pcn_targ->pcn_namelen + 1;
   2246 	oldname =  pcn_src->pcn_name;
   2247 	oldname_len = pcn_src->pcn_namelen + 1;
   2248 
   2249 	len = sizeof(*fri) + oldname_len + newname_len;
   2250 	pm = ps->ps_new_msg(pu, opc, FUSE_RENAME, len, pcn_targ->pcn_cred);
   2251 	fri = GET_INPAYLOAD(ps, pm, fuse_rename_in);
   2252 	fri->newdir = PERFUSE_NODE_DATA(targ_dir)->pnd_nodeid;
   2253 	np = (char *)(void *)(fri + 1);
   2254 	(void)strlcpy(np, oldname, oldname_len);
   2255 	np += oldname_len;
   2256 	(void)strlcpy(np, newname, newname_len);
   2257 
   2258 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2259 		goto out;
   2260 
   2261 
   2262 	/*
   2263 	 * Record new parent nodeid
   2264 	 */
   2265 	dstdir_pnd = PERFUSE_NODE_DATA(targ_dir);
   2266 	PERFUSE_NODE_DATA(src)->pnd_parent_nodeid = dstdir_pnd->pnd_nodeid;
   2267 
   2268 	if (opc != targ_dir)
   2269 		dstdir_pnd->pnd_flags |= PND_DIRTY;
   2270 
   2271 	if (strcmp(newname, "..") != 0)
   2272 		(void)strlcpy(PERFUSE_NODE_DATA(src)->pnd_name,
   2273 		    newname, MAXPATHLEN);
   2274 	else
   2275 		PERFUSE_NODE_DATA(src)->pnd_name[0] = 0; /* forget name */
   2276 
   2277 	PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
   2278 
   2279 	if ((struct puffs_node *)targ != NULL) {
   2280 		perfuse_cache_flush(targ);
   2281 		PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
   2282 	}
   2283 
   2284 #ifdef PERFUSE_DEBUG
   2285 	if (perfuse_diagflags & PDF_FILENAME)
   2286 		DPRINTF("%s: nodeid = 0x%"PRIx64" file = \"%s\" renamed \"%s\" "
   2287 			"nodeid = 0x%"PRIx64" -> nodeid = 0x%"PRIx64" \"%s\"\n",
   2288 	 		__func__, PERFUSE_NODE_DATA(src)->pnd_nodeid,
   2289 			pcn_src->pcn_name, pcn_targ->pcn_name,
   2290 			PERFUSE_NODE_DATA(opc)->pnd_nodeid,
   2291 			PERFUSE_NODE_DATA(targ_dir)->pnd_nodeid,
   2292 			perfuse_node_path(ps, targ_dir));
   2293 #endif
   2294 
   2295 	ps->ps_destroy_msg(pm);
   2296 	error = 0;
   2297 
   2298 out:
   2299 	node_rele(opc);
   2300 	node_rele(src);
   2301 	if ((struct puffs_node *)targ != NULL)
   2302 		node_rele(targ);
   2303 
   2304 	return error;
   2305 }
   2306 
   2307 int
   2308 perfuse_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
   2309 	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
   2310 	const struct vattr *vap)
   2311 {
   2312 	struct perfuse_state *ps;
   2313 	perfuse_msg_t *pm;
   2314 	struct fuse_mkdir_in *fmi;
   2315 	const char *path;
   2316 	size_t len;
   2317 	int error;
   2318 
   2319 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   2320 		return ENOENT;
   2321 
   2322 	node_ref(opc);
   2323 	ps = puffs_getspecific(pu);
   2324 	path = pcn->pcn_name;
   2325 	len = sizeof(*fmi) + pcn->pcn_namelen + 1;
   2326 
   2327 	pm = ps->ps_new_msg(pu, opc, FUSE_MKDIR, len, pcn->pcn_cred);
   2328 	fmi = GET_INPAYLOAD(ps, pm, fuse_mkdir_in);
   2329 	fmi->mode = vap->va_mode;
   2330 	fmi->umask = 0; 	/* Seems unused by libfuse? */
   2331 	(void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi));
   2332 
   2333 	error = node_mk_common(pu, opc, pni, pcn, pm);
   2334 
   2335 	node_rele(opc);
   2336 	return error;
   2337 }
   2338 
   2339 
   2340 int
   2341 perfuse_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
   2342 	puffs_cookie_t targ, const struct puffs_cn *pcn)
   2343 {
   2344 	struct perfuse_state *ps;
   2345 	struct perfuse_node_data *pnd;
   2346 	perfuse_msg_t *pm;
   2347 	char *path;
   2348 	const char *name;
   2349 	size_t len;
   2350 	int error;
   2351 
   2352 	pnd = PERFUSE_NODE_DATA(opc);
   2353 
   2354 	if ((pnd->pnd_flags & PND_REMOVED) ||
   2355 	    (PERFUSE_NODE_DATA(targ)->pnd_flags & PND_REMOVED))
   2356 		return ENOENT;
   2357 
   2358 	/*
   2359 	 * Attempt to rmdir dir/.. shoud raise ENOTEMPTY
   2360 	 */
   2361 	if (PERFUSE_NODE_DATA(targ)->pnd_nodeid == pnd->pnd_parent_nodeid)
   2362 		return ENOTEMPTY;
   2363 
   2364 	node_ref(opc);
   2365 	node_ref(targ);
   2366 
   2367 	/*
   2368 	 * Await for all operations on the deleted node to drain,
   2369 	 * as the filesystem may be confused to have it deleted
   2370 	 * during a getattr
   2371 	 */
   2372 	while (PERFUSE_NODE_DATA(targ)->pnd_inxchg)
   2373 		requeue_request(pu, targ, PCQ_AFTERXCHG);
   2374 
   2375 	ps = puffs_getspecific(pu);
   2376 	name = pcn->pcn_name;
   2377 	len = pcn->pcn_namelen + 1;
   2378 
   2379 	pm = ps->ps_new_msg(pu, opc, FUSE_RMDIR, len, pcn->pcn_cred);
   2380 	path = _GET_INPAYLOAD(ps, pm, char *);
   2381 	(void)strlcpy(path, name, len);
   2382 
   2383 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2384 		goto out;
   2385 
   2386 	perfuse_cache_flush(targ);
   2387 	PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED;
   2388 
   2389 	if (!(PERFUSE_NODE_DATA(targ)->pnd_flags & PND_OPEN))
   2390 		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
   2391 
   2392 	/*
   2393 	 * The parent directory needs a sync
   2394 	 */
   2395 	PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY;
   2396 
   2397 #ifdef PERFUSE_DEBUG
   2398 	if (perfuse_diagflags & PDF_FILENAME)
   2399 		DPRINTF("%s: remove nodeid = 0x%"PRIx64" file = \"%s\"\n",
   2400 			__func__, PERFUSE_NODE_DATA(targ)->pnd_nodeid,
   2401 			perfuse_node_path(ps, targ));
   2402 #endif
   2403 	ps->ps_destroy_msg(pm);
   2404 	error = 0;
   2405 
   2406 out:
   2407 	node_rele(opc);
   2408 	node_rele(targ);
   2409 	return error;
   2410 }
   2411 
   2412 /* vap is unused */
   2413 /* ARGSUSED4 */
   2414 int
   2415 perfuse_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
   2416 	struct puffs_newinfo *pni, const struct puffs_cn *pcn_src,
   2417 	const struct vattr *vap, const char *link_target)
   2418 {
   2419 	struct perfuse_state *ps;
   2420 	perfuse_msg_t *pm;
   2421 	char *np;
   2422 	const char *path;
   2423 	size_t path_len;
   2424 	size_t linkname_len;
   2425 	size_t len;
   2426 	int error;
   2427 
   2428 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   2429 		return ENOENT;
   2430 
   2431 	node_ref(opc);
   2432 	ps = puffs_getspecific(pu);
   2433 	path = pcn_src->pcn_name;
   2434 	path_len = pcn_src->pcn_namelen + 1;
   2435 	linkname_len = strlen(link_target) + 1;
   2436 	len = path_len + linkname_len;
   2437 
   2438 	pm = ps->ps_new_msg(pu, opc, FUSE_SYMLINK, len, pcn_src->pcn_cred);
   2439 	np = _GET_INPAYLOAD(ps, pm, char *);
   2440 	(void)strlcpy(np, path, path_len);
   2441 	np += path_len;
   2442 	(void)strlcpy(np, link_target, linkname_len);
   2443 
   2444 	error = node_mk_common(pu, opc, pni, pcn_src, pm);
   2445 
   2446 	node_rele(opc);
   2447 	return error;
   2448 }
   2449 
   2450 /* ARGSUSED4 */
   2451 int
   2452 perfuse_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
   2453 	struct dirent *dent, off_t *readoff, size_t *reslen,
   2454 	const struct puffs_cred *pcr, int *eofflag, off_t *cookies,
   2455 	size_t *ncookies)
   2456 {
   2457 	perfuse_msg_t *pm;
   2458 	uint64_t fh;
   2459 	struct perfuse_state *ps;
   2460 	struct perfuse_node_data *pnd;
   2461 	struct fuse_read_in *fri;
   2462 	struct fuse_out_header *foh;
   2463 	struct fuse_dirent *fd;
   2464 	size_t foh_len;
   2465 	int error;
   2466 	size_t fd_maxlen;
   2467 
   2468 	error = 0;
   2469 	node_ref(opc);
   2470 	ps = puffs_getspecific(pu);
   2471 
   2472 	/*
   2473 	 * readdir state is kept at node level, and several readdir
   2474 	 * requests can be issued at the same time on the same node.
   2475 	 * We need to queue requests so that only one is in readdir
   2476 	 * code at the same time.
   2477 	 */
   2478 	pnd = PERFUSE_NODE_DATA(opc);
   2479 	while (pnd->pnd_flags & PND_INREADDIR)
   2480 		requeue_request(pu, opc, PCQ_READDIR);
   2481 	pnd->pnd_flags |= PND_INREADDIR;
   2482 
   2483 #ifdef PERFUSE_DEBUG
   2484 	if (perfuse_diagflags & PDF_READDIR)
   2485 		DPRINTF("%s: READDIR opc = %p enter critical section\n",
   2486 			__func__, (void *)opc);
   2487 #endif
   2488 	/*
   2489 	 * Re-initialize pnd->pnd_fd_cookie on the first readdir for a node
   2490 	 */
   2491 	if (*readoff == 0)
   2492 		pnd->pnd_fd_cookie = 0;
   2493 
   2494 	/*
   2495 	 * Do we already have the data bufered?
   2496 	 */
   2497 	if (pnd->pnd_dirent != NULL)
   2498 		goto out;
   2499 	pnd->pnd_dirent_len = 0;
   2500 
   2501 	/*
   2502 	 * It seems NetBSD can call readdir without open first
   2503 	 * libfuse will crash if it is done that way, hence open first.
   2504 	 */
   2505 	if (!(pnd->pnd_flags & PND_OPEN)) {
   2506 		if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0)
   2507 			goto out;
   2508 	}
   2509 
   2510 	fh = perfuse_get_fh(opc, FREAD);
   2511 
   2512 #ifdef PERFUSE_DEBUG
   2513 	if (perfuse_diagflags & PDF_FH)
   2514 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", "
   2515 			"rfh = 0x%"PRIx64"\n", __func__, (void *)opc,
   2516 			PERFUSE_NODE_DATA(opc)->pnd_nodeid, fh);
   2517 #endif
   2518 
   2519 	pnd->pnd_all_fd = NULL;
   2520 	pnd->pnd_all_fd_len = 0;
   2521 	fd_maxlen = ps->ps_max_readahead - sizeof(*foh);
   2522 
   2523 	do {
   2524 		size_t fd_len;
   2525 		char *afdp;
   2526 
   2527 		pm = ps->ps_new_msg(pu, opc, FUSE_READDIR, sizeof(*fri), pcr);
   2528 
   2529 		/*
   2530 		 * read_flags, lock_owner and flags are unused in libfuse
   2531 		 */
   2532 		fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
   2533 		fri->fh = fh;
   2534 		fri->offset = pnd->pnd_fd_cookie;
   2535 		fri->size = (uint32_t)fd_maxlen;
   2536 		fri->read_flags = 0;
   2537 		fri->lock_owner = 0;
   2538 		fri->flags = 0;
   2539 
   2540 		if ((error = xchg_msg(pu, opc, pm,
   2541 				      UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2542 			goto out;
   2543 
   2544 		/*
   2545 		 * There are many puffs_framebufs calls later,
   2546 		 * therefore foh will not be valid for a long time.
   2547 		 * Just get the length and forget it.
   2548 		 */
   2549 		foh = GET_OUTHDR(ps, pm);
   2550 		foh_len = foh->len;
   2551 
   2552 		/*
   2553 		 * Empty read: we reached the end of the buffer.
   2554 		 */
   2555 		if (foh_len == sizeof(*foh)) {
   2556 			ps->ps_destroy_msg(pm);
   2557 			*eofflag = 1;
   2558 			break;
   2559 		}
   2560 
   2561 		/*
   2562 		 * Check for corrupted message.
   2563 		 */
   2564 		if (foh_len < sizeof(*foh) + sizeof(*fd)) {
   2565 			ps->ps_destroy_msg(pm);
   2566 			DWARNX("readdir reply too short");
   2567 			error = EIO;
   2568 			goto out;
   2569 		}
   2570 
   2571 
   2572 		fd = GET_OUTPAYLOAD(ps, pm, fuse_dirent);
   2573 		fd_len = foh_len - sizeof(*foh);
   2574 
   2575 		pnd->pnd_all_fd = realloc(pnd->pnd_all_fd,
   2576 					  pnd->pnd_all_fd_len + fd_len);
   2577 		if (pnd->pnd_all_fd  == NULL)
   2578 			DERR(EX_OSERR, "%s: malloc failed", __func__);
   2579 
   2580 		afdp = (char *)(void *)pnd->pnd_all_fd + pnd->pnd_all_fd_len;
   2581 		(void)memcpy(afdp, fd, fd_len);
   2582 
   2583 		pnd->pnd_all_fd_len += fd_len;
   2584 
   2585 		/*
   2586 		 * The fd->off field is used as a cookie for
   2587 		 * resuming the next readdir() where this one was left.
   2588 	 	 */
   2589 		pnd->pnd_fd_cookie = readdir_last_cookie(fd, fd_len);
   2590 
   2591 		ps->ps_destroy_msg(pm);
   2592 	} while (1 /* CONSTCOND */);
   2593 
   2594 	if (pnd->pnd_all_fd != NULL) {
   2595 		if (fuse_to_dirent(pu, opc, pnd->pnd_all_fd,
   2596 				   pnd->pnd_all_fd_len) == -1)
   2597 			error = EIO;
   2598 	}
   2599 
   2600 out:
   2601 	if (pnd->pnd_all_fd != NULL) {
   2602 		free(pnd->pnd_all_fd);
   2603 		pnd->pnd_all_fd = NULL;
   2604 		pnd->pnd_all_fd_len = 0;
   2605 	}
   2606 
   2607 	if (error == 0)
   2608 		readdir_buffered(opc, dent, readoff, reslen);
   2609 
   2610 	/*
   2611 	 * Schedule queued readdir requests
   2612 	 */
   2613 	pnd->pnd_flags &= ~PND_INREADDIR;
   2614 	(void)dequeue_requests(opc, PCQ_READDIR, DEQUEUE_ALL);
   2615 
   2616 #ifdef PERFUSE_DEBUG
   2617 	if (perfuse_diagflags & PDF_READDIR)
   2618 		DPRINTF("%s: READDIR opc = %p exit critical section\n",
   2619 			__func__, (void *)opc);
   2620 #endif
   2621 
   2622 	node_rele(opc);
   2623 	return error;
   2624 }
   2625 
   2626 int
   2627 perfuse_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
   2628 	const struct puffs_cred *pcr, char *linkname, size_t *linklen)
   2629 {
   2630 	struct perfuse_state *ps;
   2631 	perfuse_msg_t *pm;
   2632 	int error;
   2633 	size_t len;
   2634 	struct fuse_out_header *foh;
   2635 
   2636 	if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED)
   2637 		return ENOENT;
   2638 
   2639 	node_ref(opc);
   2640 	ps = puffs_getspecific(pu);
   2641 
   2642 	pm = ps->ps_new_msg(pu, opc, FUSE_READLINK, 0, pcr);
   2643 
   2644 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   2645 		goto out;
   2646 
   2647 	foh = GET_OUTHDR(ps, pm);
   2648 	len = foh->len - sizeof(*foh);
   2649 	if (len > *linklen)
   2650 		DERRX(EX_PROTOCOL, "path len = %zd too long", len);
   2651 	if (len == 0)
   2652 		DERRX(EX_PROTOCOL, "path len = %zd too short", len);
   2653 
   2654 	(void)memcpy(linkname, _GET_OUTPAYLOAD(ps, pm, char *), len);
   2655 
   2656 	/*
   2657 	 * FUSE filesystems return a NUL terminated string, we
   2658 	 * do not want the trailing \0
   2659 	 */
   2660 	while (len > 0 && linkname[len - 1] == '\0')
   2661 		len--;
   2662 
   2663 	*linklen = len;
   2664 
   2665 	ps->ps_destroy_msg(pm);
   2666 	error = 0;
   2667 
   2668 out:
   2669 	node_rele(opc);
   2670 	return error;
   2671 }
   2672 
   2673 int
   2674 perfuse_node_reclaim(struct puffs_usermount *pu, puffs_cookie_t opc)
   2675 {
   2676 	struct perfuse_state *ps;
   2677 	perfuse_msg_t *pm;
   2678 	struct perfuse_node_data *pnd;
   2679 	struct fuse_forget_in *ffi;
   2680 	int nlookup;
   2681 	struct timespec now;
   2682 
   2683 	if (opc == 0)
   2684 		return 0;
   2685 
   2686 	ps = puffs_getspecific(pu);
   2687 	pnd = PERFUSE_NODE_DATA(opc);
   2688 
   2689 	/*
   2690 	 * Never forget the root.
   2691 	 */
   2692 	if (pnd->pnd_nodeid == FUSE_ROOT_ID)
   2693 		return 0;
   2694 
   2695 	/*
   2696 	 * There is a race condition between reclaim and lookup.
   2697 	 * When looking up an already known node, the kernel cannot
   2698 	 * hold a reference on the result until it gets the PUFFS
   2699 	 * reply. It mayy therefore reclaim the node after the
   2700 	 * userland looked it up, and before it gets the reply.
   2701 	 * On rely, the kernel re-creates the node, but at that
   2702 	 * time the node has been reclaimed in userland.
   2703 	 *
   2704 	 * In order to avoid this, we refuse reclaiming nodes that
   2705 	 * are too young since the last lookup - and that we do
   2706 	 * not have removed on our own, of course.
   2707 	 */
   2708 	if (clock_gettime(CLOCK_REALTIME, &now) != 0)
   2709 		DERR(EX_OSERR, "clock_gettime failed");
   2710 
   2711 	if (timespeccmp(&pnd->pnd_cn_expire, &now, >) &&
   2712 	    !(pnd->pnd_flags & PND_REMOVED)) {
   2713 		if (!(pnd->pnd_flags & PND_NODELEAK)) {
   2714 			ps->ps_nodeleakcount++;
   2715 			pnd->pnd_flags |= PND_NODELEAK;
   2716 		}
   2717 		DWARNX("possible leaked node:: opc = %p \"%s\"",
   2718 		       opc, pnd->pnd_name);
   2719 		return 0;
   2720 	}
   2721 
   2722 	node_ref(opc);
   2723 	pnd->pnd_flags |= PND_RECLAIMED;
   2724 	pnd->pnd_puffs_nlookup--;
   2725 	nlookup = pnd->pnd_puffs_nlookup;
   2726 
   2727 #ifdef PERFUSE_DEBUG
   2728 	if (perfuse_diagflags & PDF_RECLAIM)
   2729 		DPRINTF("%s (nodeid %"PRId64") reclaimed\n",
   2730 			perfuse_node_path(ps, opc), pnd->pnd_nodeid);
   2731 #endif
   2732 
   2733 #ifdef PERFUSE_DEBUG
   2734 	if (perfuse_diagflags & PDF_RECLAIM)
   2735 		DPRINTF("%s (nodeid %"PRId64") is %sreclaimed, nlookup = %d "
   2736 			"%s%s%s%s, pending ops:%s%s%s\n",
   2737 		        perfuse_node_path(ps, opc), pnd->pnd_nodeid,
   2738 		        pnd->pnd_flags & PND_RECLAIMED ? "" : "not ",
   2739 			pnd->pnd_puffs_nlookup,
   2740 			pnd->pnd_flags & PND_OPEN ? "open " : "not open",
   2741 			pnd->pnd_flags & PND_RFH ? "r" : "",
   2742 			pnd->pnd_flags & PND_WFH ? "w" : "",
   2743 			pnd->pnd_flags & PND_BUSY ? "" : " none",
   2744 			pnd->pnd_flags & PND_INREADDIR ? " readdir" : "",
   2745 			pnd->pnd_flags & PND_INWRITE ? " write" : "",
   2746 			pnd->pnd_flags & PND_INOPEN ? " open" : "");
   2747 #endif
   2748 	/*
   2749 	 * Make sure it is not looked up again
   2750 	 */
   2751 	if (!(pnd->pnd_flags & PND_REMOVED))
   2752 		perfuse_cache_flush(opc);
   2753 
   2754 	/*
   2755 	 * Purge any activity on the node, while checking
   2756 	 * that it remains eligible for a reclaim.
   2757 	 */
   2758 	while (pnd->pnd_ref > 1)
   2759 		requeue_request(pu, opc, PCQ_REF);
   2760 
   2761 	/*
   2762 	 * reclaim cancel?
   2763 	 */
   2764 	if (pnd->pnd_puffs_nlookup > nlookup) {
   2765 		pnd->pnd_flags &= ~PND_RECLAIMED;
   2766 		perfuse_node_cache(ps, opc);
   2767 		node_rele(opc);
   2768 		return 0;
   2769 	}
   2770 
   2771 
   2772 #ifdef PERFUSE_DEBUG
   2773 	if ((pnd->pnd_flags & PND_OPEN) ||
   2774 	       !TAILQ_EMPTY(&pnd->pnd_pcq))
   2775 		DERRX(EX_SOFTWARE, "%s: opc = %p \"%s\": still open",
   2776 		      __func__, opc, pnd->pnd_name);
   2777 
   2778 	if ((pnd->pnd_flags & PND_BUSY) ||
   2779 	       !TAILQ_EMPTY(&pnd->pnd_pcq))
   2780 		DERRX(EX_SOFTWARE, "%s: opc = %p: queued operations",
   2781 		      __func__, opc);
   2782 
   2783 	if (pnd->pnd_inxchg != 0)
   2784 		DERRX(EX_SOFTWARE, "%s: opc = %p: ongoing operations",
   2785 		      __func__, opc);
   2786 #endif
   2787 
   2788 	/*
   2789 	 * Send the FORGET message
   2790 	 *
   2791 	 * ps_new_msg() is called with NULL creds, which will
   2792 	 * be interpreted as FUSE superuser. This is obviously
   2793 	 * fine since we operate with kernel creds here.
   2794 	 */
   2795 	pm = ps->ps_new_msg(pu, opc, FUSE_FORGET,
   2796 		      sizeof(*ffi), NULL);
   2797 	ffi = GET_INPAYLOAD(ps, pm, fuse_forget_in);
   2798 	ffi->nlookup = pnd->pnd_fuse_nlookup;
   2799 
   2800 	/*
   2801 	 * No reply is expected, pm is freed in xchg_msg
   2802 	 */
   2803 	(void)xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, no_reply);
   2804 
   2805 	perfuse_destroy_pn(pu, opc);
   2806 
   2807 	return 0;
   2808 }
   2809 
   2810 int
   2811 perfuse_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc)
   2812 {
   2813 	struct perfuse_node_data *pnd;
   2814 	int error;
   2815 
   2816 	if (opc == 0)
   2817 		return 0;
   2818 
   2819 	pnd = PERFUSE_NODE_DATA(opc);
   2820 	if (!(pnd->pnd_flags & (PND_OPEN|PND_REMOVED)))
   2821 		return 0;
   2822 
   2823 	node_ref(opc);
   2824 
   2825 	/*
   2826 	 * Make sure all operation are finished
   2827 	 * There can be an ongoing write. Other
   2828 	 * operation wait for all data before
   2829 	 * the close/inactive.
   2830 	 */
   2831 	while (pnd->pnd_flags & PND_INWRITE)
   2832 		requeue_request(pu, opc, PCQ_AFTERWRITE);
   2833 
   2834 	/*
   2835 	 * The inactive operation may be cancelled,
   2836 	 * If no open is in progress, set PND_INOPEN
   2837 	 * so that a new open will be queued.
   2838 	 */
   2839 	if (pnd->pnd_flags & PND_INOPEN)
   2840 		goto out;
   2841 
   2842 	pnd->pnd_flags |= PND_INOPEN;
   2843 
   2844 	/*
   2845 	 * Sync data
   2846 	 */
   2847 	if (pnd->pnd_flags & PND_DIRTY) {
   2848 		if ((error = perfuse_node_fsync(pu, opc, NULL, 0, 0, 0)) != 0)
   2849 			DWARN("%s: perfuse_node_fsync failed error = %d",
   2850 			      __func__, error);
   2851 	}
   2852 
   2853 
   2854 	/*
   2855 	 * Close handles
   2856 	 */
   2857 	if (pnd->pnd_flags & PND_WFH) {
   2858 		if ((error = perfuse_node_close_common(pu, opc, FWRITE)) != 0)
   2859 			DWARN("%s: close write FH failed error = %d",
   2860 			      __func__, error);
   2861 	}
   2862 
   2863 	if (pnd->pnd_flags & PND_RFH) {
   2864 		if ((error = perfuse_node_close_common(pu, opc, FREAD)) != 0)
   2865 			DWARN("%s: close read FH failed error = %d",
   2866 			      __func__, error);
   2867 	}
   2868 
   2869 	/*
   2870 	 * This will cause a reclaim to be sent
   2871 	 */
   2872 	if (pnd->pnd_flags & PND_REMOVED)
   2873 		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1);
   2874 
   2875 	/*
   2876 	 * Schedule awaiting operations
   2877 	 */
   2878 	pnd->pnd_flags &= ~PND_INOPEN;
   2879 	(void)dequeue_requests(opc, PCQ_OPEN, DEQUEUE_ALL);
   2880 
   2881 	/*
   2882 	 * errors are ignored, since the kernel ignores the return code.
   2883 	 */
   2884 out:
   2885 	node_rele(opc);
   2886 	return 0;
   2887 }
   2888 
   2889 
   2890 /* ARGSUSED0 */
   2891 int
   2892 perfuse_node_print(struct puffs_usermount *pu, puffs_cookie_t opc)
   2893 {
   2894 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
   2895 	return 0;
   2896 }
   2897 
   2898 int
   2899 perfuse_node_pathconf(struct puffs_usermount *pu, puffs_cookie_t opc,
   2900 	int name, register_t *retval)
   2901 {
   2902 	perfuse_msg_t *pm;
   2903 	struct perfuse_state *ps;
   2904 	struct fuse_statfs_out *fso;
   2905 	int error = 0;
   2906 
   2907 	/*
   2908 	 * Static values copied from UFS
   2909 	 * in src/sys/ufs/ufs/ufs_vnops.c
   2910 	 */
   2911 	switch (name) {
   2912 	case _PC_LINK_MAX:
   2913 		*retval = LINK_MAX;
   2914 		break;
   2915 	case _PC_PATH_MAX:
   2916 		*retval = PATH_MAX;
   2917 		break;
   2918 	case _PC_PIPE_BUF:
   2919 		*retval = PIPE_BUF;
   2920 		break;
   2921 	case _PC_CHOWN_RESTRICTED:
   2922 		*retval = 1;
   2923 		break;
   2924 	case _PC_NO_TRUNC:
   2925 		*retval = 1;
   2926 		break;
   2927 	case _PC_SYNC_IO:
   2928 		*retval = 1;
   2929 		break;
   2930 	case _PC_FILESIZEBITS:
   2931 		*retval = 42;
   2932 		break;
   2933 	case _PC_SYMLINK_MAX:
   2934 		*retval = MAXPATHLEN;
   2935 		break;
   2936 	case _PC_2_SYMLINKS:
   2937 		*retval = 1;
   2938 		break;
   2939 	case _PC_NAME_MAX:
   2940 		ps = puffs_getspecific(pu);
   2941 		pm = ps->ps_new_msg(pu, opc, FUSE_STATFS, 0, NULL);
   2942 
   2943 		error = xchg_msg(pu, opc, pm, sizeof(*fso), wait_reply);
   2944 		if (error != 0)
   2945 			return error;
   2946 
   2947 		fso = GET_OUTPAYLOAD(ps, pm, fuse_statfs_out);
   2948 		*retval = fso->st.namelen;
   2949 
   2950 		ps->ps_destroy_msg(pm);
   2951 
   2952 		break;
   2953 	default:
   2954 		DWARN("Unimplemented pathconf for name = %d", name);
   2955 		error = ENOSYS;
   2956 		break;
   2957 	}
   2958 
   2959 	return error;
   2960 }
   2961 
   2962 int
   2963 perfuse_node_advlock(struct puffs_usermount *pu, puffs_cookie_t opc,
   2964 	void *id, int op, struct flock *fl, int flags)
   2965 {
   2966 	struct perfuse_state *ps;
   2967 	int fop;
   2968 	perfuse_msg_t *pm;
   2969 	uint64_t fh;
   2970 	struct fuse_lk_in *fli;
   2971 	struct fuse_out_header *foh;
   2972 	struct fuse_lk_out *flo;
   2973 	uint32_t owner;
   2974 	size_t len;
   2975 	int error;
   2976 
   2977 	node_ref(opc);
   2978 
   2979 	/*
   2980 	 * Make sure we do have a filehandle, as the FUSE filesystem
   2981 	 * expect one. E.g.: if we provide none, GlusterFS logs an error
   2982 	 * "0-glusterfs-fuse: xl is NULL"
   2983 	 *
   2984 	 * We need the read file handle if the file is open read only,
   2985 	 * in order to support shared locks on read-only files.
   2986 	 * NB: The kernel always sends advlock for read-only
   2987 	 * files at exit time when the process used lock, see
   2988 	 * sys_exit -> exit1 -> fd_free -> fd_close -> VOP_ADVLOCK
   2989 	 */
   2990 	if ((fh = perfuse_get_fh(opc, FREAD)) == FUSE_UNKNOWN_FH) {
   2991 		error = EBADF;
   2992 		goto out;
   2993 	}
   2994 
   2995 	ps = puffs_getspecific(pu);
   2996 
   2997 	if (op == F_GETLK)
   2998 		fop = FUSE_GETLK;
   2999 	else
   3000 		fop = (flags & F_WAIT) ? FUSE_SETLKW : FUSE_SETLK;
   3001 
   3002 	/*
   3003 	 * XXX ps_new_msg() is called with NULL creds, which will
   3004 	 * be interpreted as FUSE superuser. We have no way to
   3005 	 * know the requesting process' credential, but since advlock()
   3006 	 * is supposed to operate on a file that has been open(),
   3007 	 * permission should have already been checked at open() time.
   3008 	 */
   3009 	pm = ps->ps_new_msg(pu, opc, fop, sizeof(*fli), NULL);
   3010 	fli = GET_INPAYLOAD(ps, pm, fuse_lk_in);
   3011 	fli->fh = fh;
   3012 	fli->owner = (uint64_t)(vaddr_t)id;
   3013 	fli->lk.start = fl->l_start;
   3014 	fli->lk.end = fl->l_start + fl->l_len;
   3015 	fli->lk.type = fl->l_type;
   3016 	fli->lk.pid = fl->l_pid;
   3017 	fli->lk_flags = (flags & F_FLOCK) ? FUSE_LK_FLOCK : 0;
   3018 
   3019 	owner = (uint32_t)(vaddr_t)id;
   3020 
   3021 #ifdef PERFUSE_DEBUG
   3022 	if (perfuse_diagflags & PDF_FH)
   3023 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
   3024 			__func__, (void *)opc,
   3025 			PERFUSE_NODE_DATA(opc)->pnd_nodeid, fli->fh);
   3026 #endif
   3027 
   3028 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   3029 		goto out;
   3030 
   3031 	foh = GET_OUTHDR(ps, pm);
   3032 	len = foh->len - sizeof(*foh);
   3033 
   3034 	/*
   3035 	 * Save or clear the lock
   3036 	 */
   3037 	switch (op) {
   3038 	case F_GETLK:
   3039 		if (len != sizeof(*flo))
   3040 			DERRX(EX_SOFTWARE,
   3041 			      "%s: Unexpected lock reply len %zd",
   3042 			      __func__, len);
   3043 
   3044 		flo = GET_OUTPAYLOAD(ps, pm, fuse_lk_out);
   3045 		fl->l_start = flo->lk.start;
   3046 		fl->l_len = flo->lk.end - flo->lk.start;
   3047 		fl->l_pid = flo->lk.pid;
   3048 		fl->l_type = flo->lk.type;
   3049 		fl->l_whence = SEEK_SET;	/* libfuse hardcodes it */
   3050 
   3051 		PERFUSE_NODE_DATA(opc)->pnd_lock_owner = flo->lk.pid;
   3052 		break;
   3053 	case F_UNLCK:
   3054 		owner = 0;
   3055 		/* FALLTHROUGH */
   3056 	case F_SETLK:
   3057 		/* FALLTHROUGH */
   3058 	case F_SETLKW:
   3059 		if (error != 0)
   3060 			PERFUSE_NODE_DATA(opc)->pnd_lock_owner = owner;
   3061 
   3062 		if (len != 0)
   3063 			DERRX(EX_SOFTWARE,
   3064 			      "%s: Unexpected unlock reply len %zd",
   3065 			      __func__, len);
   3066 
   3067 		break;
   3068 	default:
   3069 		DERRX(EX_SOFTWARE, "%s: Unexpected op %d", __func__, op);
   3070 		break;
   3071 	}
   3072 
   3073 	ps->ps_destroy_msg(pm);
   3074 	error = 0;
   3075 
   3076 out:
   3077 	node_rele(opc);
   3078 	return error;
   3079 }
   3080 
   3081 int
   3082 perfuse_node_read(struct puffs_usermount *pu, puffs_cookie_t opc, uint8_t *buf,
   3083 	off_t offset, size_t *resid, const struct puffs_cred *pcr, int ioflag)
   3084 {
   3085 	struct perfuse_state *ps;
   3086 	struct perfuse_node_data *pnd;
   3087 	const struct vattr *vap;
   3088 	perfuse_msg_t *pm;
   3089 	struct fuse_read_in *fri;
   3090 	struct fuse_out_header *foh;
   3091 	size_t readen;
   3092 	int error;
   3093 
   3094 	ps = puffs_getspecific(pu);
   3095 	pnd = PERFUSE_NODE_DATA(opc);
   3096 	vap = puffs_pn_getvap((struct puffs_node *)opc);
   3097 
   3098 	/*
   3099 	 * NetBSD turns that into a getdents(2) output
   3100 	 * We just do a EISDIR as this feature is of little use.
   3101 	 */
   3102 	if (vap->va_type == VDIR)
   3103 		return EISDIR;
   3104 
   3105 	do {
   3106 		size_t max_read;
   3107 
   3108 		max_read = ps->ps_max_readahead - sizeof(*foh);
   3109 		/*
   3110 		 * flags may be set to FUSE_READ_LOCKOWNER
   3111 		 * if lock_owner is provided.
   3112 		 */
   3113 		pm = ps->ps_new_msg(pu, opc, FUSE_READ, sizeof(*fri), pcr);
   3114 		fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
   3115 		fri->fh = perfuse_get_fh(opc, FREAD);
   3116 		fri->offset = offset;
   3117 		fri->size = (uint32_t)MIN(*resid, max_read);
   3118 		fri->read_flags = 0; /* XXX Unused by libfuse? */
   3119 		fri->lock_owner = pnd->pnd_lock_owner;
   3120 		fri->flags = 0;
   3121 		fri->flags |= (fri->lock_owner != 0) ? FUSE_READ_LOCKOWNER : 0;
   3122 
   3123 #ifdef PERFUSE_DEBUG
   3124 	if (perfuse_diagflags & PDF_FH)
   3125 		DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n",
   3126 			__func__, (void *)opc, pnd->pnd_nodeid, fri->fh);
   3127 #endif
   3128 		error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply);
   3129 		if (error  != 0)
   3130 			return error;
   3131 
   3132 		foh = GET_OUTHDR(ps, pm);
   3133 		readen = foh->len - sizeof(*foh);
   3134 
   3135 #ifdef PERFUSE_DEBUG
   3136 		if (readen > *resid)
   3137 			DERRX(EX_SOFTWARE, "%s: Unexpected big read %zd",
   3138 			      __func__, readen);
   3139 #endif
   3140 
   3141 		(void)memcpy(buf,  _GET_OUTPAYLOAD(ps, pm, char *), readen);
   3142 
   3143 		buf += readen;
   3144 		offset += readen;
   3145 		*resid -= readen;
   3146 
   3147 		ps->ps_destroy_msg(pm);
   3148 	} while ((*resid != 0) && (readen != 0));
   3149 
   3150 	if (ioflag & (IO_SYNC|IO_DSYNC))
   3151 		ps->ps_syncreads++;
   3152 	else
   3153 		ps->ps_asyncreads++;
   3154 
   3155 	return 0;
   3156 }
   3157 
   3158 int
   3159 perfuse_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
   3160 	uint8_t *buf, off_t offset, size_t *resid,
   3161 	const struct puffs_cred *pcr, int ioflag)
   3162 {
   3163 	return perfuse_node_write2(pu, opc, buf, offset, resid, pcr, ioflag, 0);
   3164 }
   3165 
   3166 /* ARGSUSED7 */
   3167 int
   3168 perfuse_node_write2(struct puffs_usermount *pu, puffs_cookie_t opc,
   3169 	uint8_t *buf, off_t offset, size_t *resid,
   3170 	const struct puffs_cred *pcr, int ioflag, int xflag)
   3171 {
   3172 	struct perfuse_state *ps;
   3173 	struct perfuse_node_data *pnd;
   3174 	struct vattr *vap;
   3175 	perfuse_msg_t *pm;
   3176 	struct fuse_write_in *fwi;
   3177 	struct fuse_write_out *fwo;
   3178 	size_t data_len;
   3179 	size_t payload_len;
   3180 	size_t written;
   3181 	int inresize;
   3182 	int error;
   3183 
   3184 	ps = puffs_getspecific(pu);
   3185 	pnd = PERFUSE_NODE_DATA(opc);
   3186 	vap = puffs_pn_getvap((struct puffs_node *)opc);
   3187 	written = 0;
   3188 	inresize = 0;
   3189 	error = 0;
   3190 
   3191 	if (vap->va_type == VDIR)
   3192 		return EISDIR;
   3193 
   3194 	node_ref(opc);
   3195 
   3196 	/*
   3197 	 * We need to queue write requests in order to avoid
   3198 	 * dequeueing PCQ_AFTERWRITE when there are pending writes.
   3199 	 */
   3200 	while (pnd->pnd_flags & PND_INWRITE)
   3201 		requeue_request(pu, opc, PCQ_WRITE);
   3202 	pnd->pnd_flags |= PND_INWRITE;
   3203 
   3204 	/*
   3205 	 * append flag: re-read the file size so that
   3206 	 * we get the latest value.
   3207 	 */
   3208 	if (ioflag & PUFFS_IO_APPEND) {
   3209 		if ((error = perfuse_node_getattr(pu, opc, vap, pcr)) != 0)
   3210 			goto out;
   3211 
   3212 		offset = vap->va_size;
   3213 	}
   3214 
   3215 	/*
   3216 	 * Serialize size access, see comment in perfuse_node_setattr().
   3217 	 */
   3218 	if ((u_quad_t)offset + *resid > vap->va_size) {
   3219 		while (pnd->pnd_flags & PND_INRESIZE)
   3220 			requeue_request(pu, opc, PCQ_RESIZE);
   3221 		pnd->pnd_flags |= PND_INRESIZE;
   3222 		inresize = 1;
   3223 	}
   3224 
   3225 #ifdef PERFUSE_DEBUG
   3226 	if (perfuse_diagflags & PDF_RESIZE)
   3227 		DPRINTF(">> %s %p %" PRIu64 "\n", __func__,
   3228 			(void *)opc, vap->va_size);
   3229 #endif
   3230 
   3231 	do {
   3232 		size_t max_write;
   3233 		/*
   3234 		 * There is a writepage flag when data
   3235 		 * is aligned to page size. Use it for
   3236 		 * everything but the data after the last
   3237 		 * page boundary.
   3238 		 */
   3239 		max_write = ps->ps_max_write - sizeof(*fwi);
   3240 
   3241 		data_len = MIN(*resid, max_write);
   3242 		if (data_len > (size_t)sysconf(_SC_PAGESIZE))
   3243 			data_len = data_len & ~(sysconf(_SC_PAGESIZE) - 1);
   3244 
   3245 		payload_len = data_len + sizeof(*fwi);
   3246 
   3247 		/*
   3248 		 * flags may be set to FUSE_WRITE_CACHE (XXX usage?)
   3249 		 * or FUSE_WRITE_LOCKOWNER, if lock_owner is provided.
   3250 		 * write_flags is set to 1 for writepage.
   3251 		 */
   3252 		pm = ps->ps_new_msg(pu, opc, FUSE_WRITE, payload_len, pcr);
   3253 		fwi = GET_INPAYLOAD(ps, pm, fuse_write_in);
   3254 		fwi->fh = perfuse_get_fh(opc, FWRITE);
   3255 		fwi->offset = offset;
   3256 		fwi->size = (uint32_t)data_len;
   3257 		fwi->write_flags = (fwi->size % sysconf(_SC_PAGESIZE)) ? 0 : 1;
   3258 		fwi->lock_owner = pnd->pnd_lock_owner;
   3259 		fwi->flags = 0;
   3260 		fwi->flags |= (fwi->lock_owner != 0) ? FUSE_WRITE_LOCKOWNER : 0;
   3261 		fwi->flags |= (ioflag & IO_DIRECT) ? 0 : FUSE_WRITE_CACHE;
   3262 		(void)memcpy((fwi + 1), buf, data_len);
   3263 
   3264 
   3265 #ifdef PERFUSE_DEBUG
   3266 		if (perfuse_diagflags & PDF_FH)
   3267 			DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", "
   3268 				"fh = 0x%"PRIx64"\n", __func__,
   3269 				(void *)opc, pnd->pnd_nodeid, fwi->fh);
   3270 #endif
   3271 		if ((error = xchg_msg(pu, opc, pm,
   3272 				      sizeof(*fwo), wait_reply)) != 0)
   3273 			goto out;
   3274 
   3275 		fwo = GET_OUTPAYLOAD(ps, pm, fuse_write_out);
   3276 		written = fwo->size;
   3277 		ps->ps_destroy_msg(pm);
   3278 
   3279 #ifdef PERFUSE_DEBUG
   3280 		if (written > *resid)
   3281 			DERRX(EX_SOFTWARE, "%s: Unexpected big write %zd",
   3282 			      __func__, written);
   3283 #endif
   3284 		*resid -= written;
   3285 		offset += written;
   3286 		buf += written;
   3287 
   3288 	} while (*resid != 0);
   3289 
   3290 	/*
   3291 	 * puffs_ops(3) says
   3292 	 *  "everything must be written or an error will be generated"
   3293 	 */
   3294 	if (*resid != 0)
   3295 		error = EFBIG;
   3296 
   3297 out:
   3298 #ifdef PERFUSE_DEBUG
   3299 	if (perfuse_diagflags & PDF_RESIZE) {
   3300 		if (offset > (off_t)vap->va_size)
   3301 			DPRINTF("<< %s %p %" PRIu64 " -> %lld\n", __func__,
   3302 				(void *)opc, vap->va_size, (long long)offset);
   3303 		else
   3304 			DPRINTF("<< %s %p \n", __func__, (void *)opc);
   3305 	}
   3306 #endif
   3307 
   3308 	/*
   3309 	 * Update file size if we wrote beyond the end
   3310 	 */
   3311 	if (offset > (off_t)vap->va_size)
   3312 		vap->va_size = offset;
   3313 
   3314 	/*
   3315 	 * Statistics
   3316 	 */
   3317 	if (ioflag & (IO_SYNC|IO_DSYNC))
   3318 		ps->ps_syncwrites++;
   3319 	else
   3320 		ps->ps_asyncwrites++;
   3321 
   3322 	/*
   3323 	 * Remember to sync the file
   3324 	 */
   3325 	pnd->pnd_flags |= PND_DIRTY;
   3326 
   3327 #ifdef PERFUSE_DEBUG
   3328 	if (perfuse_diagflags & PDF_SYNC)
   3329 		DPRINTF("%s: DIRTY opc = %p, file = \"%s\"\n",
   3330 			__func__, (void*)opc, perfuse_node_path(ps, opc));
   3331 #endif
   3332 
   3333 	if (inresize) {
   3334 #ifdef PERFUSE_DEBUG
   3335 		if (!(pnd->pnd_flags & PND_INRESIZE))
   3336 			DERRX(EX_SOFTWARE, "file write grow without resize");
   3337 #endif
   3338 		pnd->pnd_flags &= ~PND_INRESIZE;
   3339 		(void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL);
   3340 	}
   3341 
   3342 	/*
   3343 	 * VOP_PUTPAGE causes FAF write where kernel does not
   3344 	 * check operation result. At least warn if it failed.
   3345 	 */
   3346 #ifdef PUFFS_WRITE_FAF
   3347 	if (error && (xflag & PUFFS_WRITE_FAF))
   3348 		DWARN("Data loss caused by FAF write failed on \"%s\"",
   3349 		      pnd->pnd_name);
   3350 #endif /* PUFFS_WRITE_FAF */
   3351 
   3352 	/*
   3353 	 * If there are no more queued write, we can resume
   3354 	 * an operation awaiting write completion.
   3355 	 */
   3356 	pnd->pnd_flags &= ~PND_INWRITE;
   3357 	if (dequeue_requests(opc, PCQ_WRITE, 1) == 0)
   3358 		(void)dequeue_requests(opc, PCQ_AFTERWRITE, DEQUEUE_ALL);
   3359 
   3360 	node_rele(opc);
   3361 	return error;
   3362 }
   3363 
   3364 /* ARGSUSED0 */
   3365 void
   3366 perfuse_cache_write(struct puffs_usermount *pu, puffs_cookie_t opc, size_t size,
   3367 	struct puffs_cacherun *runs)
   3368 {
   3369 	return;
   3370 }
   3371 
   3372 /* ARGSUSED4 */
   3373 int
   3374 perfuse_node_getextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   3375 	int attrns, const char *attrname, size_t *attrsize, uint8_t *attr,
   3376 	size_t *resid, const struct puffs_cred *pcr)
   3377 {
   3378 	struct perfuse_state *ps;
   3379 	char fuse_attrname[LINUX_XATTR_NAME_MAX + 1];
   3380 	perfuse_msg_t *pm;
   3381 	struct fuse_getxattr_in *fgi;
   3382 	struct fuse_getxattr_out *fgo;
   3383 	struct fuse_out_header *foh;
   3384 	size_t attrnamelen;
   3385 	size_t len;
   3386 	char *np;
   3387 	int error;
   3388 
   3389 	/* system namespace attrs are not accessible to non root users */
   3390 	if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
   3391 		return EPERM;
   3392 
   3393 	node_ref(opc);
   3394 	ps = puffs_getspecific(pu);
   3395 	attrname = perfuse_native_ns(attrns, attrname, fuse_attrname);
   3396 	attrnamelen = strlen(attrname) + 1;
   3397 	len = sizeof(*fgi) + attrnamelen;
   3398 
   3399 	pm = ps->ps_new_msg(pu, opc, FUSE_GETXATTR, len, pcr);
   3400 	fgi = GET_INPAYLOAD(ps, pm, fuse_getxattr_in);
   3401 	fgi->size = (unsigned int)((resid != NULL) ? *resid : 0);
   3402 	np = (char *)(void *)(fgi + 1);
   3403 	(void)strlcpy(np, attrname, attrnamelen);
   3404 
   3405 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   3406 		goto out;
   3407 
   3408 	/*
   3409 	 * We just get fuse_getattr_out with list size if we requested
   3410 	 * a null size.
   3411 	 */
   3412 	if (resid == NULL) {
   3413 		fgo = GET_OUTPAYLOAD(ps, pm, fuse_getxattr_out);
   3414 
   3415 		if (attrsize != NULL)
   3416 			*attrsize = fgo->size;
   3417 
   3418 		ps->ps_destroy_msg(pm);
   3419 		error = 0;
   3420 		goto out;
   3421 	}
   3422 
   3423 	/*
   3424 	 * And with a non null requested size, we get the list just
   3425 	 * after the header
   3426 	 */
   3427 	foh = GET_OUTHDR(ps, pm);
   3428 	np = (char *)(void *)(foh + 1);
   3429 	len = foh->len - sizeof(*foh);
   3430 
   3431 	if (attrsize != NULL)
   3432 		*attrsize = len;
   3433 
   3434 	if (resid != NULL) {
   3435 		if (*resid < len) {
   3436 			error = ERANGE;
   3437 			ps->ps_destroy_msg(pm);
   3438 			goto out;
   3439 		}
   3440 
   3441 		(void)memcpy(attr, np, len);
   3442 		*resid -= len;
   3443 	}
   3444 
   3445 	ps->ps_destroy_msg(pm);
   3446 	error = 0;
   3447 
   3448 out:
   3449 	node_rele(opc);
   3450 	return error;
   3451 }
   3452 
   3453 int
   3454 perfuse_node_setextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   3455 	int attrns, const char *attrname, uint8_t *attr, size_t *resid,
   3456 	const struct puffs_cred *pcr)
   3457 {
   3458 	struct perfuse_state *ps;
   3459 	char fuse_attrname[LINUX_XATTR_NAME_MAX + 1];
   3460 	perfuse_msg_t *pm;
   3461 	struct fuse_setxattr_in *fsi;
   3462 	size_t attrnamelen;
   3463 	size_t len;
   3464 	char *np;
   3465 	int error;
   3466 
   3467 	/* system namespace attrs are not accessible to non root users */
   3468 	if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
   3469 		return EPERM;
   3470 
   3471 	node_ref(opc);
   3472 	ps = puffs_getspecific(pu);
   3473 	attrname = perfuse_native_ns(attrns, attrname, fuse_attrname);
   3474 	attrnamelen = strlen(attrname) + 1;
   3475 	len = sizeof(*fsi) + attrnamelen + *resid;
   3476 
   3477 	pm = ps->ps_new_msg(pu, opc, FUSE_SETXATTR, len, pcr);
   3478 	fsi = GET_INPAYLOAD(ps, pm, fuse_setxattr_in);
   3479 	fsi->size = (unsigned int)*resid;
   3480 	fsi->flags = 0;
   3481 	np = (char *)(void *)(fsi + 1);
   3482 	(void)strlcpy(np, attrname, attrnamelen);
   3483 	np += attrnamelen;
   3484 	(void)memcpy(np, (char *)attr, *resid);
   3485 
   3486 	if ((error = xchg_msg(pu, opc, pm,
   3487 			      NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0)
   3488 		goto out;
   3489 
   3490 	ps->ps_destroy_msg(pm);
   3491 	*resid = 0;
   3492 	error = 0;
   3493 
   3494 out:
   3495 	node_rele(opc);
   3496 	return error;
   3497 }
   3498 
   3499 /* ARGSUSED2 */
   3500 int
   3501 perfuse_node_listextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   3502 	int attrns, size_t *attrsize, uint8_t *attrs, size_t *resid, int flag,
   3503 	const struct puffs_cred *pcr)
   3504 {
   3505 	struct perfuse_state *ps;
   3506 	perfuse_msg_t *pm;
   3507 	struct fuse_getxattr_in *fgi;
   3508 	struct fuse_getxattr_out *fgo;
   3509 	struct fuse_out_header *foh;
   3510 	char *np;
   3511 	size_t len, puffs_len, i, attrlen, outlen;
   3512 	int error;
   3513 
   3514 	/* system namespace attrs are not accessible to non root users */
   3515 	if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
   3516 		return EPERM;
   3517 
   3518 	node_ref(opc);
   3519 
   3520 	ps = puffs_getspecific(pu);
   3521 	len = sizeof(*fgi);
   3522 
   3523 	pm = ps->ps_new_msg(pu, opc, FUSE_LISTXATTR, len, pcr);
   3524 	fgi = GET_INPAYLOAD(ps, pm, fuse_getxattr_in);
   3525 	if (resid != NULL)
   3526 		fgi->size = (unsigned int)*resid;
   3527 	else
   3528 		fgi->size = 0;
   3529 
   3530 	if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0)
   3531 		goto out;
   3532 
   3533 	/*
   3534 	 * We just get fuse_getattr_out with list size if we requested
   3535 	 * a null size.
   3536 	 */
   3537 	if (resid == NULL) {
   3538 		fgo = GET_OUTPAYLOAD(ps, pm, fuse_getxattr_out);
   3539 
   3540 		if (attrsize != NULL)
   3541 			*attrsize = fgo->size;
   3542 
   3543 		ps->ps_destroy_msg(pm);
   3544 
   3545 		error = 0;
   3546 		goto out;
   3547 	}
   3548 
   3549 	/*
   3550 	 * And with a non null requested size, we get the list just
   3551 	 * after the header
   3552 	 */
   3553 	foh = GET_OUTHDR(ps, pm);
   3554 	np = (char *)(void *)(foh + 1);
   3555 	puffs_len = foh->len - sizeof(*foh);
   3556 
   3557 	if (attrsize != NULL)
   3558 		*attrsize = puffs_len;
   3559 
   3560 	if (attrs != NULL) {
   3561 		if (*resid < puffs_len) {
   3562 			error = ERANGE;
   3563 			ps->ps_destroy_msg(pm);
   3564 			goto out;
   3565 		}
   3566 
   3567 		outlen = 0;
   3568 
   3569 		for (i = 0; i < puffs_len; i += attrlen + 1) {
   3570 			attrlen = strlen(np + i);
   3571 
   3572 			/*
   3573 			 * Filter attributes per namespace
   3574 			 */
   3575 			if (!perfuse_ns_match(attrns, np + i))
   3576 				continue;
   3577 
   3578 #ifdef PUFFS_EXTATTR_LIST_LENPREFIX
   3579 			/*
   3580 			 * Convert the FUSE reply to length prefixed strings
   3581 			 * if this is what the kernel wants.
   3582 			 */
   3583 			if (flag & PUFFS_EXTATTR_LIST_LENPREFIX) {
   3584 				(void)memcpy(attrs + outlen + 1,
   3585 					     np + i, attrlen);
   3586 				*(attrs + outlen) = (uint8_t)attrlen;
   3587 			} else
   3588 #endif /* PUFFS_EXTATTR_LIST_LENPREFIX */
   3589 			(void)memcpy(attrs + outlen, np + i, attrlen + 1);
   3590 			outlen += attrlen + 1;
   3591 		}
   3592 
   3593 		*resid -= outlen;
   3594 	}
   3595 
   3596 	ps->ps_destroy_msg(pm);
   3597 	error = 0;
   3598 
   3599 out:
   3600 	node_rele(opc);
   3601 	return error;
   3602 }
   3603 
   3604 int
   3605 perfuse_node_deleteextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
   3606 	int attrns, const char *attrname, const struct puffs_cred *pcr)
   3607 {
   3608 	struct perfuse_state *ps;
   3609 	char fuse_attrname[LINUX_XATTR_NAME_MAX + 1];
   3610 	perfuse_msg_t *pm;
   3611 	size_t attrnamelen;
   3612 	char *np;
   3613 	int error;
   3614 
   3615 	/* system namespace attrs are not accessible to non root users */
   3616 	if (attrns == EXTATTR_NAMESPACE_SYSTEM && !puffs_cred_isjuggernaut(pcr))
   3617 		return EPERM;
   3618 
   3619 	node_ref(opc);
   3620 
   3621 	ps = puffs_getspecific(pu);
   3622 	attrname = perfuse_native_ns(attrns, attrname, fuse_attrname);
   3623 	attrnamelen = strlen(attrname) + 1;
   3624 
   3625 	pm = ps->ps_new_msg(pu, opc, FUSE_REMOVEXATTR, attrnamelen, pcr);
   3626 	np = _GET_INPAYLOAD(ps, pm, char *);
   3627 	(void)strlcpy(np, attrname, attrnamelen);
   3628 
   3629 	error = xchg_msg(pu, opc, pm, NO_PAYLOAD_REPLY_LEN, wait_reply);
   3630 	if (error != 0)
   3631 		goto out;
   3632 
   3633 	ps->ps_destroy_msg(pm);
   3634 
   3635 out:
   3636 	node_rele(opc);
   3637 	return error;
   3638 }
   3639 
   3640 int
   3641 perfuse_node_fallocate(struct puffs_usermount *pu, puffs_cookie_t opc,
   3642 	off_t off, off_t len)
   3643 {
   3644 	struct perfuse_state *ps;
   3645 	perfuse_msg_t *pm;
   3646 	struct fuse_fallocate_in *fai;
   3647 	int error;
   3648 
   3649 	ps = puffs_getspecific(pu);
   3650 	if (ps->ps_flags & PS_NO_FALLOCATE)
   3651 		return EOPNOTSUPP;
   3652 
   3653 	node_ref(opc);
   3654 
   3655 	pm = ps->ps_new_msg(pu, opc, FUSE_FALLOCATE, sizeof(*fai), NULL);
   3656 
   3657 	fai = GET_INPAYLOAD(ps, pm, fuse_fallocate_in);
   3658 	fai->fh = perfuse_get_fh(opc, FWRITE);
   3659 	fai->offset = off;
   3660 	fai->length = len;
   3661 	fai->mode = 0;
   3662 
   3663 	error = xchg_msg(pu, opc, pm, NO_PAYLOAD_REPLY_LEN, wait_reply);
   3664 	if (error == EOPNOTSUPP || error == ENOSYS) {
   3665 		ps->ps_flags |= PS_NO_FALLOCATE;
   3666 		error = EOPNOTSUPP;
   3667 	}
   3668 	if (error != 0)
   3669 		goto out;
   3670 
   3671 	ps->ps_destroy_msg(pm);
   3672 
   3673 out:
   3674 	node_rele(opc);
   3675 	return error;
   3676 }
   3677