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