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