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