Home | History | Annotate | Line # | Download | only in libperfuse
ops.c revision 1.7
      1 /*  $NetBSD: ops.c,v 1.7 2010/09/03 07:15:18 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 void 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 = 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",  pnd->pnd_dirent_len = %zd\n",
    573 			__func__, *readoff, pnd->pnd_dirent_len);
    574 #endif
    575 	if (*readoff >=  pnd->pnd_dirent_len) {
    576 		free(pnd->pnd_dirent);
    577 		pnd->pnd_dirent = NULL;
    578 		pnd->pnd_dirent_len = 0;
    579 		*eofflag = 1;
    580 	}
    581 
    582 	return 0;
    583 }
    584 
    585 /* ARGSUSED0 */
    586 static void
    587 requeue_request(pu, opc, type)
    588 	struct puffs_usermount *pu;
    589 	puffs_cookie_t opc;
    590 	enum perfuse_qtype type;
    591 {
    592 	struct perfuse_cc_queue pcq;
    593 	struct perfuse_node_data *pnd;
    594 #ifdef PERFUSE_DEBUG
    595 	struct perfuse_state *ps;
    596 
    597 	ps = perfuse_getspecific(pu);
    598 #endif
    599 
    600 	/*
    601 	 * XXX Add a lock he day we go multithreaded
    602 	 */
    603 	pnd = PERFUSE_NODE_DATA(opc);
    604 	pcq.pcq_type = type;
    605 	pcq.pcq_cc = puffs_cc_getcc(pu);
    606 	TAILQ_INSERT_TAIL(&pnd->pnd_pcq, &pcq, pcq_next);
    607 
    608 #ifdef PERFUSE_DEBUG
    609 
    610 	if (perfuse_diagflags & PDF_REQUEUE)
    611 		DPRINTF("%s: REQUEUE opc = %p, pcc = %p\n",
    612 		       __func__, (void *)opc, pcq.pcq_cc);
    613 #endif
    614 
    615 	puffs_cc_yield(pcq.pcq_cc);
    616 	TAILQ_REMOVE(&pnd->pnd_pcq, &pcq, pcq_next);
    617 
    618 #ifdef PERFUSE_DEBUG
    619 	if (perfuse_diagflags & PDF_REQUEUE)
    620 		DPRINTF("%s: RESUME opc = %p, pcc = %p\n",
    621 		        __func__, (void *)opc, pcq.pcq_cc);
    622 #endif
    623 
    624 	return;
    625 }
    626 
    627 /* ARGSUSED0 */
    628 static void
    629 dequeue_requests(ps, opc, type, max)
    630 	struct perfuse_state *ps;
    631 	puffs_cookie_t opc;
    632 	enum perfuse_qtype type;
    633 	int max;
    634 {
    635 	struct perfuse_cc_queue *pcq;
    636 	struct perfuse_node_data *pnd;
    637 	int dequeued;
    638 
    639 	/*
    640 	 * XXX Add a lock he day we go multithreaded
    641 	 */
    642 	pnd = PERFUSE_NODE_DATA(opc);
    643 	dequeued = 0;
    644 	TAILQ_FOREACH(pcq, &pnd->pnd_pcq, pcq_next) {
    645 		if (pcq->pcq_type != type)
    646 			continue;
    647 
    648 #ifdef PERFUSE_DEBUG
    649 		if (perfuse_diagflags & PDF_REQUEUE)
    650 			DPRINTF("%s: SCHEDULE opc = %p, pcc = %p\n",
    651 				__func__, (void *)opc, pcq->pcq_cc);
    652 #endif
    653 		puffs_cc_schedule(pcq->pcq_cc);
    654 
    655 		if (++dequeued == max)
    656 			break;
    657 	}
    658 
    659 #ifdef PERFUSE_DEBUG
    660 	if (perfuse_diagflags & PDF_REQUEUE)
    661 		DPRINTF("%s: DONE  opc = %p\n", __func__, (void *)opc);
    662 #endif
    663 
    664 	return;
    665 }
    666 
    667 void
    668 perfuse_fs_init(pu)
    669 	struct puffs_usermount *pu;
    670 {
    671 	struct perfuse_state *ps;
    672 	perfuse_msg_t *pm;
    673 	struct fuse_init_in *fii;
    674 	struct fuse_init_out *fio;
    675 	int error;
    676 
    677 	ps = puffs_getspecific(pu);
    678 
    679         if (puffs_mount(pu, ps->ps_target, ps->ps_mountflags, ps->ps_root) != 0)
    680                 DERR(EX_OSERR, "puffs_mount failed");
    681 
    682 	/*
    683 	 * Linux 2.6.34.1 sends theses flags:
    684 	 * FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC
    685 	 * FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK
    686 	 *
    687 	 * Linux also sets max_readahead at 32 pages (128 kB)
    688 	 */
    689 	pm = ps->ps_new_msg(pu, 0, FUSE_INIT, sizeof(*fii), NULL);
    690 	fii = GET_INPAYLOAD(ps, pm, fuse_init_in);
    691 	fii->major = FUSE_KERNEL_VERSION;
    692 	fii->minor = FUSE_KERNEL_MINOR_VERSION;
    693 	fii->max_readahead = 32 * PAGE_SIZE;
    694 	fii->flags = (FUSE_ASYNC_READ|FUSE_POSIX_LOCKS|FUSE_ATOMIC_O_TRUNC);
    695 
    696 	if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fio))) != 0)
    697 		DERRX(EX_SOFTWARE, "init message exchange failed (%d)", error);
    698 
    699 	fio = GET_OUTPAYLOAD(ps, pm, fuse_init_out);
    700 	ps->ps_max_readahead = fio->max_readahead;
    701 	ps->ps_max_write = fio->max_write;
    702 
    703 	ps->ps_destroy_msg(pm);
    704 
    705 	return;
    706 }
    707 
    708 int
    709 perfuse_fs_unmount(pu, flags)
    710 	struct puffs_usermount *pu;
    711 	int flags;
    712 {
    713 	perfuse_msg_t *pm;
    714 	struct perfuse_state *ps;
    715 	puffs_cookie_t opc;
    716 	int error;
    717 
    718 	ps = puffs_getspecific(pu);
    719 
    720 	opc = (puffs_cookie_t)puffs_getroot(pu);
    721 	pm = ps->ps_new_msg(pu, opc, FUSE_DESTROY, 0, NULL);
    722 
    723 	if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0) {
    724 		DWARN("unmount %s", ps->ps_target);
    725 		if (!(flags & MNT_FORCE))
    726 			goto out;
    727 	}
    728 
    729 	DPRINTF("%s unmounted, exit\n", ps->ps_target);
    730 
    731 	exit(0);
    732 out:
    733 	ps->ps_destroy_msg(pm);
    734 
    735 	return error;
    736 }
    737 
    738 int
    739 perfuse_fs_statvfs(pu, svfsb)
    740 	struct puffs_usermount *pu;
    741 	struct statvfs *svfsb;
    742 {
    743 	struct perfuse_state *ps;
    744 	perfuse_msg_t *pm;
    745 	puffs_cookie_t opc;
    746 	struct fuse_statfs_out *fso;
    747 	int error;
    748 
    749 	ps = puffs_getspecific(pu);
    750 	opc = (puffs_cookie_t)puffs_getroot(pu);
    751 	pm = ps->ps_new_msg(pu, opc, FUSE_STATFS, 0, NULL);
    752 
    753 	if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fso))) != 0)
    754 		goto out;
    755 
    756 	fso = GET_OUTPAYLOAD(ps, pm, fuse_statfs_out);
    757 	svfsb->f_flag = ps->ps_mountflags;
    758 	svfsb->f_bsize = fso->st.bsize;
    759 	svfsb->f_frsize = fso->st.frsize;
    760 	svfsb->f_iosize = ((struct puffs_node *)opc)->pn_va.va_blocksize;
    761 	svfsb->f_blocks = fso->st.blocks;
    762 	svfsb->f_bfree = fso->st.bfree;
    763 	svfsb->f_bavail = fso->st.bavail;
    764 	svfsb->f_bresvd = fso->st.bfree - fso->st.bavail;
    765 	svfsb->f_files = fso->st.files;
    766 	svfsb->f_ffree = fso->st.ffree;
    767 	svfsb->f_favail = fso->st.ffree;/* files not reserved for root */
    768 	svfsb->f_fresvd = 0;		/* files reserved for root */
    769 
    770 	svfsb->f_syncreads = ps->ps_syncreads;
    771 	svfsb->f_syncwrites = ps->ps_syncwrites;
    772 
    773 	svfsb->f_asyncreads = ps->ps_asyncreads;
    774 	svfsb->f_asyncwrites = ps->ps_asyncwrites;
    775 
    776 	svfsb->f_fsidx.__fsid_val[0] = (int32_t)ps->ps_fsid;
    777 	svfsb->f_fsidx.__fsid_val[1] = 0;
    778 	svfsb->f_fsid = ps->ps_fsid;
    779 	svfsb->f_namemax = MAXPATHLEN;	/* XXX */
    780 	svfsb->f_owner = ps->ps_owner_uid;
    781 
    782 	(void)strlcpy(svfsb->f_mntonname, ps->ps_target, _VFS_NAMELEN);
    783 
    784 	if (ps->ps_filesystemtype != NULL)
    785 		(void)strlcpy(svfsb->f_fstypename,
    786 			      ps->ps_filesystemtype, _VFS_NAMELEN);
    787 	else
    788 		(void)strlcpy(svfsb->f_fstypename, "fuse", _VFS_NAMELEN);
    789 
    790 	if (ps->ps_source != NULL)
    791 		strlcpy(svfsb->f_mntfromname, ps->ps_source, _VFS_NAMELEN);
    792 	else
    793 		strlcpy(svfsb->f_mntfromname, _PATH_FUSE, _VFS_NAMELEN);
    794 out:
    795 	ps->ps_destroy_msg(pm);
    796 
    797 	return error;
    798 }
    799 
    800 int
    801 perfuse_fs_sync(pu, waitfor, pcr)
    802 	struct puffs_usermount *pu;
    803 	int waitfor;
    804 	const struct puffs_cred *pcr;
    805 {
    806 	/*
    807 	 * FUSE does not seem to have a FS sync callback.
    808 	 * Maybe do not even register this callback
    809 	 */
    810 	return puffs_fsnop_sync(pu, waitfor, pcr);
    811 }
    812 
    813 /* ARGSUSED0 */
    814 int
    815 perfuse_fs_fhtonode(pu, fid, fidsize, pni)
    816 	struct puffs_usermount *pu;
    817 	void *fid;
    818 	size_t fidsize;
    819 	struct puffs_newinfo *pni;
    820 {
    821 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
    822 	return 0;
    823 }
    824 
    825 /* ARGSUSED0 */
    826 int
    827 perfuse_fs_nodetofh(pu, cookie, fid, fidsize)
    828 	struct puffs_usermount *pu;
    829 	puffs_cookie_t cookie;
    830 	void *fid;
    831 	size_t *fidsize;
    832 {
    833 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
    834 	return 0;
    835 }
    836 
    837 #if 0
    838 /* ARGSUSED0 */
    839 void
    840 perfuse_fs_extattrctl(pu, cmd, cookie, flags, namespace, attrname)
    841 	struct puffs_usermount *pu;
    842 	int cmd,
    843 	puffs_cookie_t *cookie;
    844 	int flags;
    845 	int namespace;
    846 	const char *attrname;
    847 {
    848 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
    849 	return 0;
    850 }
    851 #endif /* 0 */
    852 
    853 /* ARGSUSED0 */
    854 void
    855 perfuse_fs_suspend(pu, status)
    856 	struct puffs_usermount *pu;
    857 	int status;
    858 {
    859 	return;
    860 }
    861 
    862 
    863 
    864 int
    865 perfuse_node_lookup(pu, opc, pni, pcn)
    866 	struct puffs_usermount *pu;
    867 	puffs_cookie_t opc;
    868 	struct puffs_newinfo *pni;
    869 	const struct puffs_cn *pcn;
    870 {
    871 	struct puffs_node *pn;
    872 	int error;
    873 
    874 	/*
    875 	 * Special case for ..
    876 	 */
    877 	if (PCNISDOTDOT(pcn)) {
    878 		pn = PERFUSE_NODE_DATA(opc)->pnd_parent;
    879 		PERFUSE_NODE_DATA(pn)->pnd_flags &= ~PND_RECLAIMED;
    880 
    881 		puffs_newinfo_setcookie(pni, pn);
    882 		puffs_newinfo_setvtype(pni, VDIR);
    883 
    884 		return 0;
    885 	}
    886 
    887 	/*
    888 	 * XXX This is borrowed from librefuse,
    889 	 * and __UNCONST is said to be fixed.
    890 	 */
    891         pn = puffs_pn_nodewalk(pu, puffs_path_walkcmp,
    892         		       __UNCONST(&pcn->pcn_po_full));
    893 
    894 	if (pn == NULL) {
    895 		error = node_lookup_common(pu, opc, (char *)PCNPATH(pcn), &pn);
    896 		if (error != 0)
    897 			return error;
    898 	}
    899 
    900 	/*
    901 	 * If that node had a pending reclaim, wipe it out.
    902 	 */
    903 	PERFUSE_NODE_DATA(pn)->pnd_flags &= ~PND_RECLAIMED;
    904 
    905 	puffs_newinfo_setcookie(pni, pn);
    906 	puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
    907 	puffs_newinfo_setsize(pni, (voff_t)pn->pn_va.va_size);
    908 	puffs_newinfo_setrdev(pni, pn->pn_va.va_rdev);
    909 
    910 	return 0;
    911 }
    912 
    913 int
    914 perfuse_node_create(pu, opc, pni, pcn, vap)
    915 	struct puffs_usermount *pu;
    916 	puffs_cookie_t opc;
    917 	struct puffs_newinfo *pni;
    918 	const struct puffs_cn *pcn;
    919 	const struct vattr *vap;
    920 {
    921 	perfuse_msg_t *pm;
    922 	struct perfuse_state *ps;
    923 	struct fuse_create_in *fci;
    924 	struct fuse_entry_out *feo;
    925 	struct fuse_open_out *foo;
    926 	struct puffs_node *pn;
    927 	const char *name;
    928 	size_t namelen;
    929 	size_t len;
    930 	int error;
    931 
    932 	/*
    933 	 * Create an object require -WX permission in the parent directory
    934 	 */
    935 	if (no_access(opc, pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
    936 		return EACCES;
    937 
    938 	/*
    939 	 * If create is unimplemented: Check that it does not
    940 	 * already exists, and if not, do mknod and open
    941 	 */
    942 	ps = puffs_getspecific(pu);
    943 	if (ps->ps_flags & PS_NO_CREAT) {
    944 		error = node_lookup_common(pu, opc, (char*)PCNPATH(pcn), &pn);
    945 		if (error == 0)
    946 			return EEXIST;
    947 
    948 		error = perfuse_node_mknod(pu, opc, pni, pcn, vap);
    949 		if (error != 0)
    950 			return error;
    951 
    952 		error = node_lookup_common(pu, opc, (char*)PCNPATH(pcn), &pn);
    953 		if (error != 0)
    954 			return error;
    955 
    956 		opc = (puffs_cookie_t)pn;
    957 
    958 		error = perfuse_node_open(pu, opc, FREAD|FWRITE, pcn->pcn_cred);
    959 		if (error != 0)
    960 			return error;
    961 
    962 		return 0;
    963 	}
    964 
    965 	name = basename_r((char *)PCNPATH(pcn));
    966 	namelen = strlen(name) + 1;
    967 	len = sizeof(*fci) + namelen;
    968 
    969 	pm = ps->ps_new_msg(pu, opc, FUSE_CREATE, len, pcn->pcn_cred);
    970 	fci = GET_INPAYLOAD(ps, pm, fuse_create_in);
    971 	fci->flags = 0; 	/* No flags seems available */
    972 	fci->mode = vap->va_mode;
    973 	fci->umask = 0; 	/* Seems unused bu libfuse */
    974 	(void)strlcpy((char*)(void *)(fci + 1), name, namelen);
    975 
    976 	len = sizeof(*feo) + sizeof(*foo);
    977 	if ((error = XCHG_MSG(ps, pu, pm, len)) != 0)
    978 		goto out;
    979 
    980 	feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out);
    981 	foo = (struct fuse_open_out *)(void *)(feo + 1);
    982 	if (feo->nodeid == PERFUSE_UNKNOWN_INO)
    983 		DERRX(EX_SOFTWARE, "%s: no ino", __func__);
    984 
    985 	/*
    986 	 * Save the file handle and inode in node private data
    987 	 * so that we can reuse it later
    988 	 */
    989 	pn = perfuse_new_pn(pu, opc);
    990 	perfuse_new_fh((puffs_cookie_t)pn, foo->fh, FWRITE);
    991 	PERFUSE_NODE_DATA(pn)->pnd_ino = feo->nodeid;
    992 
    993 #ifdef PERFUSE_DEBUG
    994 	if (perfuse_diagflags & PDF_FH)
    995 		DPRINTF("%s: opc = %p, file = \"%s\", "
    996 			"ino = %"PRId64", rfh = 0x%"PRIx64"\n",
    997 			__func__, (void *)pn, (char *)PCNPATH(pcn),
    998 			feo->nodeid, foo->fh);
    999 #endif
   1000 
   1001 	fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr);
   1002 	puffs_newinfo_setcookie(pni, pn);
   1003 
   1004 	/*
   1005 	 * It seems we need to do this so that glusterfs gets fully
   1006 	 * aware that the file was created. If we do not do it, we
   1007 	 * get "SETATTR (null) (fuse_loc_fill() failed)"
   1008 	 */
   1009 	(void)node_lookup_common(pu, opc, (char*)PCNPATH(pcn), NULL);
   1010 out:
   1011 	ps->ps_destroy_msg(pm);
   1012 
   1013 	/*
   1014 	 * create is unimplmented, remember it for later,
   1015 	 * and start over using mknod and open instead.
   1016 	 */
   1017 	if (error == ENOSYS) {
   1018 		ps->ps_flags |= PS_NO_CREAT;
   1019 		return perfuse_node_create(pu, opc, pni, pcn, vap);
   1020 	}
   1021 
   1022 	return error;
   1023 }
   1024 
   1025 
   1026 int
   1027 perfuse_node_mknod(pu, opc, pni, pcn, vap)
   1028 	struct puffs_usermount *pu;
   1029 	puffs_cookie_t opc;
   1030 	struct puffs_newinfo *pni;
   1031 	const struct puffs_cn *pcn;
   1032 	const struct vattr *vap;
   1033 {
   1034 	struct perfuse_state *ps;
   1035 	perfuse_msg_t *pm;
   1036 	struct fuse_mknod_in *fmi;
   1037 	const char* path;
   1038 	size_t len;
   1039 
   1040 	/*
   1041 	 * Only superuser can mknod objects other than
   1042 	 * directories, files, socks, fifo and links.
   1043 	 *
   1044 	 * Create an object require -WX permission in the parent directory
   1045 	 */
   1046 	switch (vap->va_type) {
   1047 	case VDIR:	/* FALLTHROUGH */
   1048 	case VREG:	/* FALLTHROUGH */
   1049 	case VFIFO:	/* FALLTHROUGH */
   1050 	case VSOCK:	/* FALLTHROUGH */
   1051 	case VLNK:
   1052 		if (no_access(opc, pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
   1053 			return EACCES;
   1054 		break;
   1055 	default:	/* VNON, VBLK, VCHR, VBAD */
   1056 		if (!puffs_cred_isjuggernaut(pcn->pcn_cred))
   1057 			return EACCES;
   1058 		break;
   1059 	}
   1060 
   1061 
   1062 	ps = puffs_getspecific(pu);
   1063 	path = basename_r((char *)PCNPATH(pcn));
   1064 	len = sizeof(*fmi) + strlen(path) + 1;
   1065 
   1066 	pm = ps->ps_new_msg(pu, opc, FUSE_MKNOD, len, pcn->pcn_cred);
   1067 	fmi = GET_INPAYLOAD(ps, pm, fuse_mknod_in);
   1068 	fmi->mode = vap->va_mode | VTTOIF(vap->va_type);
   1069 	fmi->rdev = vap->va_rdev;
   1070 	fmi->umask = 0; 	/* Seems unused bu libfuse */
   1071 	(void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi));
   1072 
   1073 	return node_mk_common(pu, opc, pni, pcn, pm);
   1074 }
   1075 
   1076 
   1077 int
   1078 perfuse_node_open(pu, opc, mode, pcr)
   1079 	struct puffs_usermount *pu;
   1080 	puffs_cookie_t opc;
   1081 	int mode;
   1082 	const struct puffs_cred *pcr;
   1083 {
   1084 	struct perfuse_state *ps;
   1085 	struct perfuse_node_data *pnd;
   1086 	perfuse_msg_t *pm;
   1087 	mode_t pmode;
   1088 	mode_t fmode;
   1089 	int op;
   1090 	struct fuse_open_in *foi;
   1091 	struct fuse_open_out *foo;
   1092 	struct puffs_node *pn;
   1093 	int error;
   1094 
   1095 	ps = puffs_getspecific(pu);
   1096 	pnd = PERFUSE_NODE_DATA(opc);
   1097 
   1098 	pn = (struct puffs_node *)opc;
   1099 	if (puffs_pn_getvap(pn)->va_type == VDIR) {
   1100 		op = FUSE_OPENDIR;
   1101 		pmode = PUFFS_VREAD|PUFFS_VEXEC;
   1102 	} else {
   1103 		op = FUSE_OPEN;
   1104 		if (mode & FWRITE)
   1105 			pmode = PUFFS_VWRITE|PUFFS_VREAD;
   1106 		else
   1107 			pmode = PUFFS_VREAD;
   1108 	}
   1109 
   1110 	/*
   1111 	 * Opening a directory require R-X on the directory
   1112 	 * Opening a file requires R-- for reading, -W- for writing
   1113 	 * In both cases, --X is required on the parent.
   1114 	 */
   1115 	if (no_access((puffs_cookie_t)pnd->pnd_parent, pcr, PUFFS_VEXEC))
   1116 		return EACCES;
   1117 
   1118 	if (no_access(opc, pcr, pmode))
   1119 		return EACCES;
   1120 
   1121 	/*
   1122 	 * libfuse docs say O_CREAT should not be set.
   1123 	 */
   1124 	mode &= ~O_CREAT;
   1125 
   1126 	/*
   1127 	 * Do not open twice, and do not reopen for reading
   1128 	 * if we already have write handle.
   1129 	 * Directories are always open with read access only,
   1130 	 * whatever flags we get.
   1131 	 */
   1132 	if (op == FUSE_OPENDIR)
   1133 		mode = (mode & ~(FREAD|FWRITE)) | FREAD;
   1134 	if ((mode & FREAD) && (pnd->pnd_flags & PND_RFH))
   1135 		return 0;
   1136 	if ((mode & FWRITE) && (pnd->pnd_flags & PND_WFH))
   1137 		return 0;
   1138 
   1139 	/*
   1140 	 * Convert PUFFS mode to FUSE mode: convert FREAD/FWRITE
   1141 	 * to O_RDONLY/O_WRONLY while perserving the other options.
   1142 	 */
   1143 	fmode = mode & ~(FREAD|FWRITE);
   1144 	fmode |= (mode & FWRITE) ? O_RDWR : O_RDONLY;
   1145 
   1146 	pm = ps->ps_new_msg(pu, opc, op, sizeof(*foi), pcr);
   1147 	foi = GET_INPAYLOAD(ps, pm, fuse_open_in);
   1148 	foi->flags = fmode;
   1149 	foi->unused = 0;
   1150 
   1151 	if ((error = XCHG_MSG(ps, pu, pm, sizeof(*foo))) != 0)
   1152 		goto out;
   1153 
   1154 	foo = GET_OUTPAYLOAD(ps, pm, fuse_open_out);
   1155 
   1156 	/*
   1157 	 * Save the file handle in node private data
   1158 	 * so that we can reuse it later
   1159 	 */
   1160 	perfuse_new_fh((puffs_cookie_t)pn, foo->fh, mode);
   1161 
   1162 #ifdef PERFUSE_DEBUG
   1163 	if (perfuse_diagflags & PDF_FH)
   1164 		DPRINTF("%s: opc = %p, file = \"%s\", "
   1165 			"ino = %"PRId64", %s%sfh = 0x%"PRIx64"\n",
   1166 			__func__, (void *)opc,
   1167 			(char *)PNPATH((struct puffs_node *)opc),
   1168 			pnd->pnd_ino, mode & FREAD ? "r" : "",
   1169 			mode & FWRITE ? "w" : "", foo->fh);
   1170 #endif
   1171 out:
   1172 	ps->ps_destroy_msg(pm);
   1173 
   1174 	return error;
   1175 }
   1176 
   1177 /* ARGSUSED0 */
   1178 int
   1179 perfuse_node_close(pu, opc, flags, pcr)
   1180 	struct puffs_usermount *pu;
   1181 	puffs_cookie_t opc;
   1182 	int flags;
   1183 	const struct puffs_cred *pcr;
   1184 {
   1185 	struct puffs_node *pn;
   1186 	struct perfuse_node_data *pnd;
   1187 
   1188 	pn = (struct puffs_node *)opc;
   1189 	pnd = PERFUSE_NODE_DATA(opc);
   1190 
   1191 	if (!(pnd->pnd_flags & PND_OPEN))
   1192 		return EBADF;
   1193 
   1194 	/*
   1195 	 * Make sure all operation are finished
   1196 	 * There can be an ongoing write, or queued operations
   1197 	 * XXX perhaps deadlock. Use requeue_request
   1198 	 */
   1199 	while ((pnd->pnd_flags & PND_BUSY) ||
   1200 	       !TAILQ_EMPTY(&pnd->pnd_pcq))
   1201 		puffs_cc_yield(puffs_cc_getcc(pu));
   1202 
   1203 	/*
   1204 	 * The NetBSD kernel will send sync and setattr(mtime, ctime)
   1205 	 * afer a close on a regular file. Some FUSE filesystem will
   1206 	 * assume theses operations are performed on open files. We
   1207 	 * therefore postpone the close operation at reclaim time.
   1208 	 */
   1209 	if (puffs_pn_getvap(pn)->va_type != VREG)
   1210 		return node_close_common(pu, opc, flags);
   1211 
   1212 	return 0;
   1213 }
   1214 
   1215 int
   1216 perfuse_node_access(pu, opc, mode, pcr)
   1217 	struct puffs_usermount *pu;
   1218 	puffs_cookie_t opc;
   1219 	int mode;
   1220 	const struct puffs_cred *pcr;
   1221 {
   1222 	perfuse_msg_t *pm;
   1223 	struct perfuse_state *ps;
   1224 	struct fuse_access_in *fai;
   1225 	int error;
   1226 
   1227 	/*
   1228 	 * If we previously detected the filesystem does not
   1229 	 * implement access(), short-circuit the call and skip
   1230 	 * to libpffs access() emulation.
   1231 	 */
   1232 	ps = puffs_getspecific(pu);
   1233 	if (ps->ps_flags & PS_NO_ACCESS) {
   1234 		error = ENOSYS;
   1235 	} else {
   1236 		pm = ps->ps_new_msg(pu, opc, FUSE_ACCESS, sizeof(*fai), pcr);
   1237 		fai = GET_INPAYLOAD(ps, pm, fuse_access_in);
   1238 		fai->mask = mode;
   1239 
   1240 		error = XCHG_MSG(ps, pu, pm, NO_PAYLOAD_REPLY_LEN);
   1241 		ps->ps_destroy_msg(pm);
   1242 	}
   1243 
   1244 	if (error == ENOSYS) {
   1245 		struct fuse_getattr_in *fgi;
   1246 		struct fuse_attr_out *fao;
   1247 
   1248 		ps->ps_flags |= PS_NO_ACCESS;
   1249 
   1250 		pm = ps->ps_new_msg(pu, opc, FUSE_GETATTR,
   1251 			      sizeof(*fgi), NULL);
   1252 		fgi = GET_INPAYLOAD(ps, pm, fuse_getattr_in);
   1253 		fgi->getattr_flags = 0;
   1254 		fgi->dummy = 0;
   1255 		fgi->fh = perfuse_get_fh(opc, FREAD);
   1256 
   1257 #ifdef PERFUSE_DEBUG
   1258 		if (perfuse_diagflags & PDF_FH)
   1259 			DPRINTF("%s: opc = %p, ino = %"PRId64", "
   1260 				"fh = 0x%"PRIx64"\n", __func__, (void *)opc,
   1261 				PERFUSE_NODE_DATA(opc)->pnd_ino, fgi->fh);
   1262 #endif
   1263 		if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fao))) != 0) {
   1264 			ps->ps_destroy_msg(pm);
   1265 			goto out;
   1266 		}
   1267 
   1268 		fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out);
   1269 
   1270 		error = puffs_access(VREG, fao->attr.mode, fao->attr.uid,
   1271 				     fao->attr.gid, (mode_t)mode, pcr);
   1272 
   1273 		ps->ps_destroy_msg(pm);
   1274 	}
   1275 
   1276 out:
   1277 	return error;
   1278 }
   1279 
   1280 int
   1281 perfuse_node_getattr(pu, opc, vap, pcr)
   1282 	struct puffs_usermount *pu;
   1283 	puffs_cookie_t opc;
   1284 	struct vattr *vap;
   1285 	const struct puffs_cred *pcr;
   1286 {
   1287 	perfuse_msg_t *pm;
   1288 	struct perfuse_state *ps;
   1289 	struct fuse_getattr_in *fgi;
   1290 	struct fuse_attr_out *fao;
   1291 	int error;
   1292 
   1293 	/*
   1294 	 * getattr requires --X on the parent directory
   1295 	 */
   1296 	if (no_access((puffs_cookie_t)PERFUSE_NODE_DATA(opc)->pnd_parent,
   1297 	    pcr, PUFFS_VEXEC))
   1298 		return EACCES;
   1299 
   1300 	ps = puffs_getspecific(pu);
   1301 
   1302 	/*
   1303 	 * FUSE_GETATTR_FH must be set in fgi->flags
   1304 	 * if we use for fgi->fh, but we do not.
   1305 	 */
   1306 	pm = ps->ps_new_msg(pu, opc, FUSE_GETATTR, sizeof(*fgi), pcr);
   1307 	fgi = GET_INPAYLOAD(ps, pm, fuse_getattr_in);
   1308 	fgi->getattr_flags = 0;
   1309 	fgi->dummy = 0;
   1310 	fgi->fh = 0;
   1311 
   1312 	if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fao))) != 0)
   1313 		goto out;
   1314 
   1315 	fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out);
   1316 
   1317 	/*
   1318 	 * The message from filesystem has a cache timeout
   1319 	 * XXX this is ignored yet, is that right?
   1320 	 *
   1321 	 * We also set birthtime, flags, filerev,vaflags to 0.
   1322 	 * This seems the best bet, since the information is
   1323 	 * not available from filesystem.
   1324 	 */
   1325 	fuse_attr_to_vap(ps, vap, &fao->attr);
   1326 
   1327 out:
   1328 	ps->ps_destroy_msg(pm);
   1329 
   1330 	return error;
   1331 }
   1332 
   1333 int
   1334 perfuse_node_setattr(pu, opc, vap, pcr)
   1335 	struct puffs_usermount *pu;
   1336 	puffs_cookie_t opc;
   1337 	const struct vattr *vap;
   1338 	const struct puffs_cred *pcr;
   1339 {
   1340 	perfuse_msg_t *pm;
   1341 	uint64_t fh;
   1342 	struct perfuse_state *ps;
   1343 	struct perfuse_node_data *pnd;
   1344 	struct fuse_setattr_in *fsi;
   1345 	int error;
   1346 	int open_self;
   1347 	struct vattr *old_va;
   1348 
   1349 	open_self = 0;
   1350 	ps = puffs_getspecific(pu);
   1351 	pnd = PERFUSE_NODE_DATA(opc);
   1352 
   1353 	/*
   1354 	 * setattr requires --X on the parent directory
   1355 	 */
   1356 	if (no_access((puffs_cookie_t)pnd->pnd_parent, pcr, PUFFS_VEXEC))
   1357 		return EACCES;
   1358 
   1359 	old_va = puffs_pn_getvap((struct puffs_node *)opc);
   1360 
   1361 	/*
   1362 	 * Check for permission to change size
   1363 	 */
   1364 	if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) &&
   1365 	    no_access(opc, pcr, PUFFS_VWRITE))
   1366 		return EACCES;
   1367 
   1368 	/*
   1369 	 * Check for permission to change dates
   1370 	 */
   1371 	if (((vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) ||
   1372 	     (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL)) &&
   1373 	    (puffs_access_times(old_va->va_uid, old_va->va_gid,
   1374 				old_va->va_mode, 0, pcr) != 0))
   1375 		return EACCES;
   1376 
   1377 	/*
   1378 	 * Check for permission to change owner and group
   1379 	 */
   1380 	if (((vap->va_uid != (uid_t)PUFFS_VNOVAL) ||
   1381 	     (vap->va_gid != (gid_t)PUFFS_VNOVAL)) &&
   1382 	    (puffs_access_chown(old_va->va_uid, old_va->va_gid,
   1383 				vap->va_uid, vap->va_gid, pcr)) != 0)
   1384 		return EACCES;
   1385 
   1386 	/*
   1387 	 * Check for permission to change permissions
   1388 	 */
   1389 	if ((vap->va_mode != (mode_t)PUFFS_VNOVAL) &&
   1390 	    (puffs_access_chmod(old_va->va_uid, old_va->va_gid,
   1391 				old_va->va_type, vap->va_mode, pcr)) != 0)
   1392 		return EACCES;
   1393 
   1394 	/*
   1395 	 * setattr(mtime, ctime) require an open file,
   1396 	 * at least for glusterfs.
   1397 	 */
   1398 	if (((vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) ||
   1399 	     (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL)) &&
   1400 	    !(pnd->pnd_flags & PND_WFH)) {
   1401 		if ((error = perfuse_node_open(pu, opc, FWRITE, pcr)) != 0)
   1402 			return error;
   1403 		open_self = 1;
   1404 	}
   1405 	/*
   1406 	 * It seems troublesome to resize a file while
   1407 	 * a write is just beeing done. Wait for
   1408 	 * it to finish.
   1409 	 */
   1410 	if (vap->va_size != (u_quad_t)PUFFS_VNOVAL)
   1411 		while (pnd->pnd_flags & PND_INWRITE)
   1412 			requeue_request(pu, opc, PCQ_AFTERWRITE);
   1413 
   1414 
   1415 	pm = ps->ps_new_msg(pu, opc, FUSE_SETATTR, sizeof(*fsi), pcr);
   1416 	fsi = GET_INPAYLOAD(ps, pm, fuse_setattr_in);
   1417 	fsi->valid = 0;
   1418 
   1419 	if (pnd->pnd_flags & PND_WFH) {
   1420 		fh = perfuse_get_fh(opc, FWRITE);
   1421 		fsi->fh = fh;
   1422 		fsi->valid |= FUSE_FATTR_FH;
   1423 	}
   1424 
   1425 	if (vap->va_size != (u_quad_t)PUFFS_VNOVAL) {
   1426 		fsi->size = vap->va_size;
   1427 		fsi->valid |= FUSE_FATTR_SIZE;
   1428 	}
   1429 
   1430 	if (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) {
   1431 		fsi->atime = vap->va_atime.tv_sec;;
   1432 		fsi->atimensec = (uint32_t)vap->va_atime.tv_nsec;;
   1433 		fsi->valid |= (FUSE_FATTR_ATIME|FUSE_FATTR_ATIME_NOW);
   1434 	}
   1435 
   1436 	if (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) {
   1437 		fsi->mtime = vap->va_mtime.tv_sec;;
   1438 		fsi->mtimensec = (uint32_t)vap->va_mtime.tv_nsec;;
   1439 		fsi->valid |= (FUSE_FATTR_MTIME|FUSE_FATTR_MTIME_NOW);
   1440 	}
   1441 
   1442 	if (vap->va_mode != (mode_t)PUFFS_VNOVAL) {
   1443 		fsi->mode = vap->va_mode;
   1444 		fsi->valid |= FUSE_FATTR_MODE;
   1445 	}
   1446 
   1447 	if (vap->va_uid != (uid_t)PUFFS_VNOVAL) {
   1448 		fsi->uid = vap->va_uid;
   1449 		fsi->valid |= FUSE_FATTR_UID;
   1450 	}
   1451 
   1452 	if (vap->va_gid != (gid_t)PUFFS_VNOVAL) {
   1453 		fsi->gid = vap->va_gid;
   1454 		fsi->valid |= FUSE_FATTR_GID;
   1455 	}
   1456 
   1457 	if (pnd->pnd_lock_owner != 0) {
   1458 		fsi->lock_owner = pnd->pnd_lock_owner;
   1459 		fsi->valid |= FUSE_FATTR_LOCKOWNER;
   1460 	}
   1461 
   1462 	/*
   1463 	 * A fuse_attr_out is returned, but we ignore it.
   1464 	 */
   1465 	error = XCHG_MSG(ps, pu, pm, sizeof(struct fuse_attr_out));
   1466 
   1467 	ps->ps_destroy_msg(pm);
   1468 
   1469 	if (open_self)
   1470 		(void)perfuse_node_close(pu, opc, FWRITE, pcr);
   1471 
   1472 	return error;
   1473 }
   1474 
   1475 int
   1476 perfuse_node_poll(pu, opc, events)
   1477 	struct puffs_usermount *pu;
   1478 	puffs_cookie_t opc;
   1479 	int *events;
   1480 {
   1481 	struct perfuse_state *ps;
   1482 	perfuse_msg_t *pm;
   1483 	struct fuse_poll_in *fpi;
   1484 	struct fuse_poll_out *fpo;
   1485 	int error;
   1486 
   1487 	ps = puffs_getspecific(pu);
   1488 	/*
   1489 	 * kh is set if FUSE_POLL_SCHEDULE_NOTIFY is set.
   1490  	 */
   1491 	pm = ps->ps_new_msg(pu, opc, FUSE_POLL, sizeof(*fpi), NULL);
   1492 	fpi = GET_INPAYLOAD(ps, pm, fuse_poll_in);
   1493 	fpi->fh = perfuse_get_fh(opc, FREAD);
   1494 	fpi->kh = 0;
   1495 	fpi->flags = 0;
   1496 
   1497 #ifdef PERFUSE_DEBUG
   1498 	if (perfuse_diagflags & PDF_FH)
   1499 		DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
   1500 			__func__, (void *)opc,
   1501 			PERFUSE_NODE_DATA(opc)->pnd_ino, fpi->fh);
   1502 #endif
   1503 	if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fpo))) != 0)
   1504 		goto out;
   1505 
   1506 	fpo = GET_OUTPAYLOAD(ps, pm, fuse_poll_out);
   1507 	*events = fpo->revents;
   1508 out:
   1509 	ps->ps_destroy_msg(pm);
   1510 
   1511 	return error;
   1512 }
   1513 
   1514 /* ARGSUSED0 */
   1515 int
   1516 perfuse_node_mmap(pu, opc, flags, pcr)
   1517 	struct puffs_usermount *pu;
   1518 	puffs_cookie_t opc;
   1519 	int flags;
   1520 	const struct puffs_cred *pcr;
   1521 {
   1522 	/*
   1523 	 * Not implemented anymore in libfuse
   1524 	 */
   1525 	return ENOSYS;
   1526 }
   1527 
   1528 /* ARGSUSED2 */
   1529 int
   1530 perfuse_node_fsync(pu, opc, pcr, flags, offlo, offhi)
   1531 	struct puffs_usermount *pu;
   1532 	puffs_cookie_t opc;
   1533 	const struct puffs_cred *pcr;
   1534 	int flags;
   1535 	off_t offlo;
   1536 	off_t offhi;
   1537 {
   1538 	perfuse_msg_t *pm;
   1539 	struct perfuse_state *ps;
   1540 	struct perfuse_node_data *pnd;
   1541 	struct fuse_fsync_in *ffi;
   1542 	uint64_t fh;
   1543 	int open_self;
   1544 	int error;
   1545 
   1546 	pm = NULL;
   1547 	open_self = 0;
   1548 
   1549 	/*
   1550 	 * If we previously detected it as unimplemented,
   1551 	 * skip the call to the filesystem.
   1552 	 */
   1553 	ps = puffs_getspecific(pu);
   1554 	if (ps->ps_flags == PS_NO_FSYNC)
   1555 		return ENOSYS;
   1556 
   1557 	/*
   1558 	 * Do not sync if there are no change to sync
   1559 	 * XXX remove that test if we implement mmap
   1560 	 */
   1561 	pnd = PERFUSE_NODE_DATA(opc);
   1562 #ifdef PERFUSE_DEBUG
   1563 	if (perfuse_diagflags & PDF_SYNC)
   1564 		DPRINTF("%s: TEST opc = %p, file = \"%s\" is %sdirty\n",
   1565 			__func__, (void*)opc,
   1566 			(char *)PNPATH((struct puffs_node *)opc),
   1567 			pnd->pnd_flags & PND_DIRTY ? "" : "not ");
   1568 #endif
   1569 	if (!(pnd->pnd_flags & PND_DIRTY))
   1570 		return 0;
   1571 
   1572 	/*
   1573 	 * It seems NetBSD can call fsync without open first
   1574 	 * glusterfs complain in such a situation:
   1575 	 * "FSYNC() ERR => -1 (Invalid argument)"
   1576 	 */
   1577 	if (!(pnd->pnd_flags & PND_OPEN)) {
   1578 		if ((error = perfuse_node_open(pu, opc, FWRITE, pcr)) != 0)
   1579 			goto out;
   1580 		open_self = 1;
   1581 	}
   1582 
   1583 	fh = perfuse_get_fh(opc, FWRITE);
   1584 
   1585 	/*
   1586 	 * If fsync_flags  is set, meta data should not be flushed.
   1587 	 */
   1588 	pm = ps->ps_new_msg(pu, opc, FUSE_FSYNC, sizeof(*ffi), NULL);
   1589 	ffi = GET_INPAYLOAD(ps, pm, fuse_fsync_in);
   1590 	ffi->fh = fh;
   1591 	ffi->fsync_flags = (flags & FFILESYNC) ? 0 : 1;
   1592 
   1593 #ifdef PERFUSE_DEBUG
   1594 	if (perfuse_diagflags & PDF_FH)
   1595 		DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
   1596 			__func__, (void *)opc,
   1597 			PERFUSE_NODE_DATA(opc)->pnd_ino, ffi->fh);
   1598 #endif
   1599 
   1600 	if ((error = XCHG_MSG(ps, pu, pm, NO_PAYLOAD_REPLY_LEN)) != 0)
   1601 		goto out;
   1602 
   1603 	/*
   1604 	 * No reply beyond fuse_out_header: nothing to do on success
   1605 	 * just clear the dirty flag
   1606 	 */
   1607 	pnd->pnd_flags &= ~PND_DIRTY;
   1608 
   1609 #ifdef PERFUSE_DEBUG
   1610 	if (perfuse_diagflags & PDF_SYNC)
   1611 		DPRINTF("%s: CLEAR opc = %p, file = \"%s\"\n",
   1612 			__func__, (void*)opc,
   1613 			(char *)PNPATH((struct puffs_node *)opc));
   1614 #endif
   1615 
   1616 out:
   1617 	if (error == ENOSYS)
   1618 		ps->ps_flags |= PS_NO_FSYNC;
   1619 
   1620 	if (pm != NULL)
   1621 		ps->ps_destroy_msg(pm);
   1622 
   1623 	if (open_self)
   1624 		(void)node_close_common(pu, opc, FWRITE);
   1625 
   1626 	return error;
   1627 }
   1628 
   1629 /* ARGSUSED0 */
   1630 int
   1631 perfuse_node_seek(pu, opc, oldoff, newoff,  pcr)
   1632 	struct puffs_usermount *pu;
   1633 	puffs_cookie_t opc;
   1634 	off_t oldoff;
   1635 	off_t newoff;
   1636 	const struct puffs_cred *pcr;
   1637 {
   1638 	/*
   1639 	 * XXX what should I do with oldoff?
   1640 	 * XXX where is the newoffset returned?
   1641 	 * XXX the held seek pointer seems just unused
   1642 	 */
   1643 	PERFUSE_NODE_DATA(opc)->pnd_offset = newoff;
   1644 
   1645 	return 0;
   1646 }
   1647 
   1648 int
   1649 perfuse_node_remove(pu, opc, targ, pcn)
   1650 	struct puffs_usermount *pu;
   1651 	puffs_cookie_t opc;
   1652 	puffs_cookie_t targ;
   1653 	const struct puffs_cn *pcn;
   1654 {
   1655 	struct perfuse_state *ps;
   1656 	struct puffs_node *pn;
   1657 	struct perfuse_node_data *pnd;
   1658 	perfuse_msg_t *pm;
   1659 	char *path;
   1660 	const char *name;
   1661 	size_t len;
   1662 	int error;
   1663 
   1664 	pnd = PERFUSE_NODE_DATA(opc);
   1665 
   1666 	/*
   1667 	 * remove requires -WX on the parent directory
   1668 	 * no right required on the object.
   1669 	 */
   1670 	if (no_access((puffs_cookie_t)pnd->pnd_parent,
   1671 	    pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
   1672 		return EACCES;
   1673 
   1674 	if (targ == NULL)
   1675 		DERRX(EX_SOFTWARE, "%s: targ is NULL", __func__);
   1676 
   1677 	ps = puffs_getspecific(pu);
   1678 	pn = (struct puffs_node *)targ;
   1679 	name = basename_r((char *)PNPATH(pn));
   1680 	len = strlen(name) + 1;
   1681 
   1682 	pm = ps->ps_new_msg(pu, opc, FUSE_UNLINK, len, pcn->pcn_cred);
   1683 	path = _GET_INPAYLOAD(ps, pm, char *);
   1684 	(void)strlcpy(path, name, len);
   1685 
   1686 	if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
   1687 		goto out;
   1688 
   1689 	if (puffs_inval_namecache_dir(pu, opc) != 0)
   1690 		DERR(EX_OSERR, "puffs_inval_namecache_dir failed");
   1691 
   1692 	puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
   1693 
   1694 	/*
   1695 	 * Reclaim should take care of decreasing pnd_childcount
   1696 	 */
   1697 out:
   1698 	ps->ps_destroy_msg(pm);
   1699 
   1700 	return error;
   1701 }
   1702 
   1703 int
   1704 perfuse_node_link(pu, opc, targ, pcn)
   1705 	struct puffs_usermount *pu;
   1706 	puffs_cookie_t opc;
   1707 	puffs_cookie_t targ;
   1708 	const struct puffs_cn *pcn;
   1709 {
   1710 	struct perfuse_state *ps;
   1711 	perfuse_msg_t *pm;
   1712 	const char *name;
   1713 	size_t len;
   1714 	struct puffs_node *pn;
   1715 	struct fuse_link_in *fli;
   1716 	int error;
   1717 
   1718 	/*
   1719 	 * Create an object require -WX permission in the parent directory
   1720 	 */
   1721 	if (no_access(opc, pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
   1722 		return EACCES;
   1723 
   1724 
   1725 	ps = puffs_getspecific(pu);
   1726 	pn = (struct puffs_node *)targ;
   1727 	name = basename_r((char *)PCNPATH(pcn));
   1728 	len =  sizeof(*fli) + strlen(name) + 1;
   1729 
   1730 	pm = ps->ps_new_msg(pu, opc, FUSE_LINK, len, pcn->pcn_cred);
   1731 	fli = GET_INPAYLOAD(ps, pm, fuse_link_in);
   1732 	fli->oldnodeid = PERFUSE_NODE_DATA(pn)->pnd_ino;
   1733 	(void)strlcpy((char *)(void *)(fli + 1), name, len - sizeof(*fli));
   1734 
   1735 	error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN);
   1736 
   1737 	ps->ps_destroy_msg(pm);
   1738 
   1739 	return error;
   1740 }
   1741 
   1742 /* targ is unused since the name is in pcn_targ */
   1743 /* ARGSUSED5 */
   1744 int
   1745 perfuse_node_rename(pu, opc, src, pcn_src, targ_dir, targ, pcn_targ)
   1746 	struct puffs_usermount *pu;
   1747 	puffs_cookie_t opc;
   1748 	puffs_cookie_t src;
   1749 	const struct puffs_cn *pcn_src;
   1750 	puffs_cookie_t targ_dir;
   1751 	puffs_cookie_t targ;
   1752 	const struct puffs_cn *pcn_targ;
   1753 {
   1754 	struct perfuse_state *ps;
   1755 	perfuse_msg_t *pm;
   1756 	struct fuse_rename_in *fri;
   1757 	const char *newname;
   1758 	const char *oldname;
   1759 	char *np;
   1760 	int error;
   1761 	size_t len;
   1762 	size_t newname_len;
   1763 	size_t oldname_len;
   1764 
   1765 	/*
   1766 	 * move requires -WX on source and destination directory
   1767 	 */
   1768 	if (no_access(src, pcn_src->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC) ||
   1769 	    no_access(targ,  pcn_targ->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
   1770 		return EACCES;
   1771 
   1772 	ps = puffs_getspecific(pu);
   1773 	newname =  basename_r((char *)PCNPATH(pcn_targ));
   1774 	newname_len = strlen(newname) + 1;
   1775 	oldname =  basename_r((char *)PCNPATH(pcn_src));
   1776 	oldname_len = strlen(oldname) + 1;
   1777 
   1778 	len = sizeof(*fri) + oldname_len + newname_len;
   1779 	pm = ps->ps_new_msg(pu, opc, FUSE_RENAME, len, pcn_src->pcn_cred);
   1780 	fri = GET_INPAYLOAD(ps, pm, fuse_rename_in);
   1781 	fri->newdir = PERFUSE_NODE_DATA(targ_dir)->pnd_ino;
   1782 	np = (char *)(void *)(fri + 1);
   1783 	(void)strlcpy(np, oldname, oldname_len);
   1784 	np += oldname_len;
   1785 	(void)strlcpy(np, newname, newname_len);
   1786 
   1787 	if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
   1788 		goto out;
   1789 
   1790 	/*
   1791 	 * Update source and destination directories child count
   1792 	 * Update moved object parent directory
   1793 	 */
   1794 	PERFUSE_NODE_DATA(opc)->pnd_childcount--;
   1795 	PERFUSE_NODE_DATA(targ_dir)->pnd_childcount++;
   1796 	PERFUSE_NODE_DATA(src)->pnd_parent = targ_dir;
   1797 
   1798 out:
   1799 	ps->ps_destroy_msg(pm);
   1800 
   1801 	return error;
   1802 }
   1803 
   1804 int
   1805 perfuse_node_mkdir(pu, opc, pni, pcn, vap)
   1806 	struct puffs_usermount *pu;
   1807 	puffs_cookie_t opc;
   1808 	struct puffs_newinfo *pni;
   1809 	const struct puffs_cn *pcn;
   1810 	const struct vattr *vap;
   1811 {
   1812 	struct perfuse_state *ps;
   1813 	perfuse_msg_t *pm;
   1814 	struct fuse_mkdir_in *fmi;
   1815 	const char *path;
   1816 	size_t len;
   1817 
   1818 	/*
   1819 	 * Create an object require -WX permission in the parent directory
   1820 	 */
   1821 	if (no_access(opc, pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
   1822 		return EACCES;
   1823 
   1824 	ps = puffs_getspecific(pu);
   1825 	path = basename_r((char *)PCNPATH(pcn));
   1826 	len = sizeof(*fmi) + strlen(path) + 1;
   1827 
   1828 	pm = ps->ps_new_msg(pu, opc, FUSE_MKDIR, len, pcn->pcn_cred);
   1829 	fmi = GET_INPAYLOAD(ps, pm, fuse_mkdir_in);
   1830 	fmi->mode = vap->va_mode | VTTOIF(vap->va_type);
   1831 	fmi->umask = 0; 	/* Seems unused bu libfuse? */
   1832 	(void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi));
   1833 
   1834 	return node_mk_common(pu, opc, pni, pcn, pm);
   1835 }
   1836 
   1837 
   1838 int
   1839 perfuse_node_rmdir(pu, opc, targ, pcn)
   1840 	struct puffs_usermount *pu;
   1841 	puffs_cookie_t opc;
   1842 	puffs_cookie_t targ;
   1843 	const struct puffs_cn *pcn;
   1844 {
   1845 	struct perfuse_state *ps;
   1846 	struct perfuse_node_data *pnd;
   1847 	perfuse_msg_t *pm;
   1848 	struct puffs_node *pn;
   1849 	char *path;
   1850 	const char *name;
   1851 	size_t len;
   1852 	int error;
   1853 
   1854 	pnd = PERFUSE_NODE_DATA(opc);
   1855 
   1856 	/*
   1857 	 * remove requires -WX on the parent directory
   1858 	 * no right required on the object.
   1859 	 */
   1860 	if (no_access((puffs_cookie_t)pnd->pnd_parent,
   1861 	    pcn->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
   1862 		return EACCES;
   1863 
   1864 	ps = puffs_getspecific(pu);
   1865 	pn = (struct puffs_node *)targ;
   1866 	name = basename_r((char *)PNPATH(pn));
   1867 	len = strlen(name) + 1;
   1868 
   1869 	pm = ps->ps_new_msg(pu, opc, FUSE_RMDIR, len, pcn->pcn_cred);
   1870 	path = _GET_INPAYLOAD(ps, pm, char *);
   1871 	(void)strlcpy(path, name, len);
   1872 
   1873 	if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
   1874 		goto out;
   1875 
   1876 	if (puffs_inval_namecache_dir(pu, opc) != 0)
   1877 		DERR(EX_OSERR, "puffs_inval_namecache_dir failed");
   1878 
   1879 	puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2);
   1880 
   1881 out:
   1882 	ps->ps_destroy_msg(pm);
   1883 
   1884 	return error;
   1885 }
   1886 
   1887 /* vap is unused */
   1888 /* ARGSUSED4 */
   1889 int
   1890 perfuse_node_symlink(pu, opc, pni, pcn_src, vap, link_target)
   1891 	struct puffs_usermount *pu;
   1892 	puffs_cookie_t opc;
   1893 	struct puffs_newinfo *pni;
   1894 	const struct puffs_cn *pcn_src;
   1895 	const struct vattr *vap;
   1896 	const char *link_target;
   1897 {
   1898 	struct perfuse_state *ps;
   1899 	perfuse_msg_t *pm;
   1900 	char *np;
   1901 	const char *path;
   1902 	size_t path_len;
   1903 	size_t linkname_len;
   1904 	size_t len;
   1905 
   1906 	/*
   1907 	 * Create an object require -WX permission in the parent directory
   1908 	 */
   1909 	if (no_access(opc, pcn_src->pcn_cred, PUFFS_VWRITE|PUFFS_VEXEC))
   1910 		return EACCES;
   1911 
   1912 	ps = puffs_getspecific(pu);
   1913 	path = basename_r((char *)PCNPATH(pcn_src));
   1914 	path_len = strlen(path) + 1;
   1915 	linkname_len = strlen(link_target) + 1;
   1916 	len = path_len + linkname_len;
   1917 
   1918 	pm = ps->ps_new_msg(pu, opc, FUSE_SYMLINK, len, pcn_src->pcn_cred);
   1919 	np = _GET_INPAYLOAD(ps, pm, char *);
   1920 	(void)strlcpy(np, path, path_len);
   1921 	np += path_len;
   1922 	(void)strlcpy(np, link_target, linkname_len);
   1923 
   1924 	return node_mk_common(pu, opc, pni, pcn_src, pm);
   1925 }
   1926 
   1927 int
   1928 perfuse_node_readdir(pu, opc, dent, readoff,
   1929 		     reslen, pcr, eofflag, cookies, ncookies)
   1930 	struct puffs_usermount *pu;
   1931 	puffs_cookie_t opc;
   1932 	struct dirent *dent;
   1933 	off_t *readoff;
   1934 	size_t *reslen;
   1935 	const struct puffs_cred *pcr;
   1936 	int *eofflag;
   1937 	off_t *cookies;
   1938 	size_t *ncookies;
   1939 {
   1940 	perfuse_msg_t *pm;
   1941 	uint64_t fh;
   1942 	struct perfuse_state *ps;
   1943 	struct perfuse_node_data *pnd;
   1944 	struct fuse_read_in *fri;
   1945 	struct fuse_out_header *foh;
   1946 	struct fuse_dirent *fd;
   1947 	size_t foh_len;
   1948 	int error;
   1949 	int open_self;
   1950 	uint64_t fd_offset;
   1951 
   1952 	pm = NULL;
   1953 	error = 0;
   1954 	open_self = 0;
   1955 	ps = puffs_getspecific(pu);
   1956 
   1957 	/*
   1958 	 * readdir state is kept at node level, and several readdir
   1959 	 * requests can be issued at the same time on the same node.
   1960 	 * We need to queue requests so that only one is in readdir
   1961 	 * code at the same time.
   1962 	 */
   1963 	pnd = PERFUSE_NODE_DATA(opc);
   1964 	while (pnd->pnd_flags & PND_INREADDIR)
   1965 		requeue_request(pu, opc, PCQ_READDIR);
   1966 	pnd->pnd_flags |= PND_INREADDIR;
   1967 
   1968 #ifdef PERFUSE_DEBUG
   1969 	if (perfuse_diagflags & PDF_READDIR)
   1970 		DPRINTF("%s: READDIR opc = %p enter critical section\n",
   1971 			__func__, (void *)opc);
   1972 #endif
   1973 	/*
   1974 	 * Do we already have the data bufered?
   1975 	 */
   1976 	if (pnd->pnd_dirent != NULL)
   1977 		goto out;
   1978 	pnd->pnd_dirent_len = 0;
   1979 
   1980 	/*
   1981 	 * It seems NetBSD can call readdir without open first
   1982 	 * libfuse will crash if it is done that way, hence open first.
   1983 	 */
   1984 	if (!(pnd->pnd_flags & PND_OPEN)) {
   1985 		if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0)
   1986 			goto out;
   1987 		open_self = 1;
   1988 	}
   1989 
   1990 	fh = perfuse_get_fh(opc, FREAD);
   1991 
   1992 #ifdef PERFUSE_DEBUG
   1993 	if (perfuse_diagflags & PDF_FH)
   1994 		DPRINTF("%s: opc = %p, ino = %"PRId64", rfh = 0x%"PRIx64"\n",
   1995 			__func__, (void *)opc,
   1996 			PERFUSE_NODE_DATA(opc)->pnd_ino, fh);
   1997 #endif
   1998 
   1999 	pnd->pnd_all_fd = NULL;
   2000 	pnd->pnd_all_fd_len = 0;
   2001 	fd_offset = 0;
   2002 
   2003 	do {
   2004 		size_t fd_len;
   2005 		char *afdp;
   2006 
   2007 		pm = ps->ps_new_msg(pu, opc, FUSE_READDIR, sizeof(*fri), pcr);
   2008 
   2009 		/*
   2010 		 * read_flags, lock_owner and flags are unused in libfuse
   2011 		 *
   2012 		 * XXX if fri->size is too big (bigger than PAGE_SIZE?), 			 * we get strange bugs. ktrace shows 16 bytes or garbage
   2013 		 * at the end of sent frames, but perfused does not receive
   2014 		 * that data. The data length is hoverver the same, which
   2015 		 * cause perfused to use the last 16 bytes of the frame
   2016 		 * as the frame header of the next frame.
   2017 		 *
   2018 		 * This may be a kernel bug.
   2019 		 */
   2020 		fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
   2021 		fri->fh = fh;
   2022 		fri->offset = fd_offset;
   2023 		fri->size = PAGE_SIZE - sizeof(struct fuse_out_header);
   2024 		fri->read_flags = 0;
   2025 		fri->lock_owner = 0;
   2026 		fri->flags = 0;
   2027 
   2028 		if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
   2029 			goto out;
   2030 
   2031 		/*
   2032 		 * There are many puffs_framebufs calls later,
   2033 		 * therefore foh will not be valid for a long time.
   2034 		 * Just get the length and forget it.
   2035 		 */
   2036 		foh = GET_OUTHDR(ps, pm);
   2037 		foh_len = foh->len;
   2038 
   2039 		/*
   2040 		 * It seems that the only way to discover the end
   2041 		 * of the buffer is to get an empty read
   2042 		 */
   2043 		if (foh_len == sizeof(*foh))
   2044 			break;
   2045 
   2046 		/*
   2047 		 * Corrupted message.
   2048 		 */
   2049 		if (foh_len < sizeof(*foh) + sizeof(*fd)) {
   2050 			DWARNX("readdir reply too short");
   2051 			error = EIO;
   2052 			goto out;
   2053 		}
   2054 
   2055 
   2056 		fd = GET_OUTPAYLOAD(ps, pm, fuse_dirent);
   2057 		fd_len = foh_len - sizeof(*foh);
   2058 
   2059 		pnd->pnd_all_fd = realloc(pnd->pnd_all_fd,
   2060 					  pnd->pnd_all_fd_len + fd_len);
   2061 		if (pnd->pnd_all_fd  == NULL)
   2062 			DERR(EX_OSERR, "malloc failed");
   2063 
   2064 		afdp = (char *)(void *)pnd->pnd_all_fd + pnd->pnd_all_fd_len;
   2065 		(void)memcpy(afdp, fd, fd_len);
   2066 
   2067 		pnd->pnd_all_fd_len += fd_len;
   2068 		fd_offset += fd_len;
   2069 
   2070 		ps->ps_destroy_msg(pm);
   2071 		pm = NULL;
   2072 	} while (1 /* CONSTCOND */);
   2073 
   2074 	if (fuse_to_dirent(pu, opc, pnd->pnd_all_fd, pnd->pnd_all_fd_len) == -1)
   2075 		error = EIO;
   2076 
   2077 out:
   2078 	if (pnd->pnd_all_fd != NULL) {
   2079 		free(pnd->pnd_all_fd);
   2080 		pnd->pnd_all_fd = NULL;
   2081 		pnd->pnd_all_fd_len = 0;
   2082 	}
   2083 
   2084 	if (pm != NULL)
   2085 		ps->ps_destroy_msg(pm);
   2086 
   2087 	/*
   2088 	 * If we opened the directory ourselves, close now
   2089 	 * errors are ignored.
   2090 	 */
   2091 	if (open_self)
   2092 		(void)perfuse_node_close(pu, opc, FWRITE, pcr);
   2093 
   2094 	if (error == 0)
   2095 		error = readdir_buffered(ps, opc, dent, readoff,
   2096 			reslen, pcr, eofflag, cookies, ncookies);
   2097 
   2098 	/*
   2099 	 * Schedule queued readdir requests
   2100 	 */
   2101 	dequeue_requests(ps, opc, PCQ_READDIR, DEQUEUE_ALL);
   2102 	pnd->pnd_flags &= ~PND_INREADDIR;
   2103 
   2104 #ifdef PERFUSE_DEBUG
   2105 	if (perfuse_diagflags & PDF_READDIR)
   2106 		DPRINTF("%s: READDIR opc = %p exit critical section\n",
   2107 			__func__, (void *)opc);
   2108 #endif
   2109 
   2110 	return error;
   2111 }
   2112 
   2113 int
   2114 perfuse_node_readlink(pu, opc, pcr, linkname, linklen)
   2115 	struct puffs_usermount *pu;
   2116 	puffs_cookie_t opc;
   2117 	const struct puffs_cred *pcr;
   2118 	char *linkname;
   2119 	size_t *linklen;
   2120 {
   2121 	struct perfuse_state *ps;
   2122 	perfuse_msg_t *pm;
   2123 	int error;
   2124 	size_t len;
   2125 	struct fuse_out_header *foh;
   2126 
   2127 	/*
   2128 	 * --X required on parent, R-- required on link
   2129 	 */
   2130 	if (no_access((puffs_cookie_t)PERFUSE_NODE_DATA(opc)->pnd_parent,
   2131 	    pcr, PUFFS_VEXEC) ||
   2132 	   no_access(opc, pcr, PUFFS_VREAD))
   2133 		return EACCES;
   2134 
   2135 	ps = puffs_getspecific(pu);
   2136 
   2137 	pm = ps->ps_new_msg(pu, opc, FUSE_READLINK, 0, pcr);
   2138 
   2139 	if ((error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN)) != 0)
   2140 		goto out;
   2141 
   2142 	foh = GET_OUTHDR(ps, pm);
   2143 	len = foh->len - sizeof(*foh) + 1;
   2144 	if (len > *linklen)
   2145 		DERRX(EX_PROTOCOL, "path len = %zd too long", len);
   2146 
   2147 	*linklen = len;
   2148 	(void)strlcpy(linkname, _GET_OUTPAYLOAD(ps, pm, char *), len);
   2149 out:
   2150 	ps->ps_destroy_msg(pm);
   2151 
   2152 	return error;
   2153 }
   2154 
   2155 int
   2156 perfuse_node_reclaim(pu, opc)
   2157 	struct puffs_usermount *pu;
   2158 	puffs_cookie_t opc;
   2159 {
   2160 	struct perfuse_state *ps;
   2161 	perfuse_msg_t *pm;
   2162 	struct perfuse_node_data *pnd;
   2163 	struct fuse_forget_in *ffi;
   2164 	struct puffs_node *pn;
   2165 	struct puffs_node *pn_root;
   2166 
   2167 	ps = puffs_getspecific(pu);
   2168 	pnd = PERFUSE_NODE_DATA(opc);
   2169 
   2170 	/*
   2171 	 * Never forget the root.
   2172 	 */
   2173 	if (pnd->pnd_ino == FUSE_ROOT_ID)
   2174 		return 0;
   2175 
   2176 	pnd->pnd_flags |= PND_RECLAIMED;
   2177 
   2178 #ifdef PERFUSE_DEBUG
   2179 	if (perfuse_diagflags & PDF_RECLAIM)
   2180 		DPRINTF("%s (nodeid %"PRId64") reclaimed\n",
   2181 		       (char *)PNPATH((struct puffs_node *)opc), pnd->pnd_ino);
   2182 #endif
   2183 
   2184 	pn_root = puffs_getroot(pu);
   2185 	pn = (struct puffs_node *)opc;
   2186 	while (pn != pn_root) {
   2187 		struct puffs_node *parent_pn;
   2188 
   2189 		pnd = PERFUSE_NODE_DATA(pn);
   2190 
   2191 #ifdef PERFUSE_DEBUG
   2192 	if (perfuse_diagflags & PDF_RECLAIM)
   2193 		DPRINTF("%s (nodeid %"PRId64") is %sreclaimed, "
   2194 			"has childcount %d %s%s%s, pending ops:%s%s%s%s\n",
   2195 		        (char *)PNPATH(pn), pnd->pnd_ino,
   2196 		        pnd->pnd_flags & PND_RECLAIMED ? "" : "not ",
   2197 		        pnd->pnd_childcount,
   2198 			pnd->pnd_flags & PND_OPEN ? "open " : "not open",
   2199 			pnd->pnd_flags & PND_RFH ? "r" : "",
   2200 			pnd->pnd_flags & PND_WFH ? "w" : "",
   2201 			pnd->pnd_flags & PND_INREADDIR ? " readdir" : "",
   2202 			pnd->pnd_flags & PND_INREAD ? " read" : "",
   2203 			pnd->pnd_flags & PND_INWRITE ? " write" : "",
   2204 			pnd->pnd_flags & PND_BUSY ? "" : " none");
   2205 #endif
   2206 
   2207 		if (!(pnd->pnd_flags & PND_RECLAIMED) ||
   2208 		    (pnd->pnd_childcount != 0))
   2209 			return 0;
   2210 
   2211 		/*
   2212 		 * Make sure all operation are finished
   2213 		 * There can be an ongoing write, or queued operations
   2214 		 */
   2215 		while (pnd->pnd_flags & PND_INWRITE) {
   2216 			requeue_request(pu, opc, PCQ_AFTERWRITE);
   2217 
   2218 			/*
   2219 			 * It may have been cancelled in the meantime
   2220 			 */
   2221 			if (!(pnd->pnd_flags & PND_RECLAIMED))
   2222 				return 0;
   2223 		}
   2224 
   2225 #ifdef PERFUSE_DEBUG
   2226 		if ((pnd->pnd_flags & PND_BUSY) ||
   2227 		       !TAILQ_EMPTY(&pnd->pnd_pcq))
   2228 			DERRX(EX_SOFTWARE, "%s: opc = %p: ongoing operations",
   2229 			      __func__, (void *)opc);
   2230 #endif
   2231 
   2232 		/*
   2233 		 * Close open files
   2234 		 */
   2235 		if (pnd->pnd_flags & PND_WFH)
   2236 			(void)node_close_common(pu, opc, FREAD);
   2237 
   2238 		if (pnd->pnd_flags & PND_RFH)
   2239 			(void)node_close_common(pu, opc, FWRITE);
   2240 
   2241 		/*
   2242 		 * And send the FORGET message
   2243 		 */
   2244 		pm = ps->ps_new_msg(pu, (puffs_cookie_t)pn, FUSE_FORGET,
   2245 			      sizeof(*ffi), NULL);
   2246 		ffi = GET_INPAYLOAD(ps, pm, fuse_forget_in);
   2247 		ffi->nlookup = pnd->pnd_nlookup;
   2248 
   2249 		/*
   2250 		 * No reply is expected, pm is freed in XCHG_MSG
   2251 		 */
   2252 		(void)XCHG_MSG_NOREPLY(ps, pu, pm, UNSPEC_REPLY_LEN);
   2253 
   2254 		parent_pn = pnd->pnd_parent;
   2255 
   2256 		perfuse_destroy_pn(pn);
   2257 		puffs_pn_put(pn);
   2258 
   2259 		pn = parent_pn;
   2260 	}
   2261 
   2262 	return 0;
   2263 }
   2264 
   2265 /* ARGSUSED0 */
   2266 int
   2267 perfuse_node_inactive(pu, opc)
   2268 	struct puffs_usermount *pu;
   2269 	puffs_cookie_t opc;
   2270 {
   2271 	return 0;
   2272 }
   2273 
   2274 
   2275 /* ARGSUSED0 */
   2276 int
   2277 perfuse_node_print(pu, opc)
   2278 	struct puffs_usermount *pu;
   2279 	puffs_cookie_t opc;
   2280 {
   2281 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
   2282 	return 0;
   2283 }
   2284 
   2285 /* ARGSUSED0 */
   2286 int
   2287 perfuse_node_pathconf(pu, opc, name, retval)
   2288 	struct puffs_usermount *pu;
   2289 	puffs_cookie_t opc;
   2290 	int name;
   2291 	int *retval;
   2292 {
   2293 	DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__);
   2294 	return 0;
   2295 }
   2296 
   2297 /* id is unused */
   2298 /* ARGSUSED2 */
   2299 int
   2300 perfuse_node_advlock(pu, opc, id, op, fl, flags)
   2301 	struct puffs_usermount *pu;
   2302 	puffs_cookie_t opc;
   2303 	void *id;
   2304 	int op;
   2305 	struct flock *fl;
   2306 	int flags;
   2307 {
   2308 	struct perfuse_state *ps;
   2309 	int fop;
   2310 	perfuse_msg_t *pm;
   2311 	struct fuse_lk_in *fli;
   2312 	struct fuse_lk_out *flo;
   2313 	int error;
   2314 
   2315 	ps = puffs_getspecific(pu);
   2316 
   2317 	if (op == F_GETLK)
   2318 		fop = FUSE_GETLK;
   2319 	else
   2320 		fop = (flags & F_WAIT) ? FUSE_SETLKW : FUSE_SETLK;
   2321 
   2322 	pm = ps->ps_new_msg(pu, opc, fop, sizeof(*fli), NULL);
   2323 	fli = GET_INPAYLOAD(ps, pm, fuse_lk_in);
   2324 	fli->fh = perfuse_get_fh(opc, FWRITE);
   2325 	fli->owner = fl->l_pid;
   2326 	fli->lk.start = fl->l_start;
   2327 	fli->lk.end = fl->l_start + fl->l_len;
   2328 	fli->lk.type = fl->l_type;
   2329 	fli->lk.pid = fl->l_pid;
   2330 	fli->lk_flags = (flags & F_FLOCK) ? FUSE_LK_FLOCK : 0;
   2331 
   2332 #ifdef PERFUSE_DEBUG
   2333 	if (perfuse_diagflags & PDF_FH)
   2334 		DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
   2335 			__func__, (void *)opc,
   2336 			PERFUSE_NODE_DATA(opc)->pnd_ino, fli->fh);
   2337 #endif
   2338 
   2339 	if ((error = XCHG_MSG(ps, pu, pm, sizeof(*flo))) != 0)
   2340 		goto out;
   2341 
   2342 	flo = GET_OUTPAYLOAD(ps, pm, fuse_lk_out);
   2343 	fl->l_start = flo->lk.start;
   2344 	fl->l_len = flo->lk.end - flo->lk.start;
   2345 	fl->l_pid = flo->lk.pid;
   2346 	fl->l_type = flo->lk.type;
   2347 	fl->l_whence = SEEK_SET;	/* libfuse hardcodes it */
   2348 
   2349 	/*
   2350 	 * Save or clear the lock
   2351 	 */
   2352 	switch (op) {
   2353 	case F_SETLK:
   2354 		PERFUSE_NODE_DATA(opc)->pnd_lock_owner = flo->lk.pid;
   2355 		break;
   2356 	case F_UNLCK:
   2357 		PERFUSE_NODE_DATA(opc)->pnd_lock_owner = 0;
   2358 		break;
   2359 	default:
   2360 		break;
   2361 	}
   2362 
   2363 out:
   2364 	ps->ps_destroy_msg(pm);
   2365 
   2366 	return error;
   2367 }
   2368 
   2369 int
   2370 perfuse_node_read(pu, opc, buf, offset, resid, pcr, ioflag)
   2371 	struct puffs_usermount *pu;
   2372 	puffs_cookie_t opc;
   2373 	uint8_t *buf;
   2374 	off_t offset;
   2375 	size_t *resid;
   2376 	const struct puffs_cred *pcr;
   2377 	int ioflag;
   2378 {
   2379 	struct perfuse_state *ps;
   2380 	struct perfuse_node_data *pnd;
   2381 	perfuse_msg_t *pm;
   2382 	struct fuse_read_in *fri;
   2383 	struct fuse_out_header *foh;
   2384 	size_t readen;
   2385 	size_t requested;
   2386 	int error;
   2387 
   2388 	ps = puffs_getspecific(pu);
   2389 	pnd = PERFUSE_NODE_DATA(opc);
   2390 	pm = NULL;
   2391 
   2392 	if (puffs_pn_getvap((struct puffs_node *)opc)->va_type == VDIR)
   2393 		return EBADF;
   2394 
   2395 	pnd->pnd_flags |= PND_INREAD;
   2396 
   2397 	requested = *resid;
   2398 	if ((ps->ps_readahead + requested) > ps->ps_max_readahead) {
   2399 		if (perfuse_diagflags & PDF_REQUEUE)
   2400 			DPRINTF("readahead = %zd\n", ps->ps_readahead);
   2401 		requeue_request(pu, opc, PCQ_READ);
   2402 	}
   2403 	ps->ps_readahead += requested;
   2404 
   2405 	do {
   2406 		/*
   2407 		 * flags may be set to FUSE_READ_LOCKOWNER
   2408 		 * if lock_owner is provided.
   2409 		 *
   2410 		 * XXX See comment about fri->size in perfuse_node_readdir
   2411 		 * We encounter the same bug here.
   2412 		 */
   2413 		pm = ps->ps_new_msg(pu, opc, FUSE_READ, sizeof(*fri), pcr);
   2414 		fri = GET_INPAYLOAD(ps, pm, fuse_read_in);
   2415 		fri->fh = perfuse_get_fh(opc, FREAD);
   2416 		fri->offset = offset;
   2417 		fri->size = (uint32_t)MIN(*resid, PAGE_SIZE - sizeof(*foh));
   2418 		fri->read_flags = 0; /* XXX Unused by libfuse? */
   2419 		fri->lock_owner = pnd->pnd_lock_owner;
   2420 		fri->flags = 0;
   2421 		fri->flags |= (fri->lock_owner != 0) ? FUSE_READ_LOCKOWNER : 0;
   2422 
   2423 #ifdef PERFUSE_DEBUG
   2424 	if (perfuse_diagflags & PDF_FH)
   2425 		DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
   2426 			__func__, (void *)opc, pnd->pnd_ino, fri->fh);
   2427 #endif
   2428 		error = XCHG_MSG(ps, pu, pm, UNSPEC_REPLY_LEN);
   2429 
   2430 		if (error  != 0)
   2431 			goto out;
   2432 
   2433 		foh = GET_OUTHDR(ps, pm);
   2434 		readen = foh->len - sizeof(*foh);
   2435 
   2436 		(void)memcpy(buf,  _GET_OUTPAYLOAD(ps, pm, char *), readen);
   2437 
   2438 		buf += readen;
   2439 		offset += readen;
   2440 		*resid -= readen;
   2441 
   2442 		ps->ps_destroy_msg(pm);
   2443 		pm = NULL;
   2444 	} while ((*resid != 0) && (readen != 0));
   2445 
   2446 	if (ioflag & (IO_SYNC|IO_DSYNC))
   2447 		ps->ps_syncreads++;
   2448 	else
   2449 		ps->ps_asyncreads++;
   2450 
   2451 out:
   2452 	if (pm != NULL)
   2453 		ps->ps_destroy_msg(pm);
   2454 
   2455 	ps->ps_readahead -= requested;
   2456 	dequeue_requests(ps, opc, PCQ_READ, 1);
   2457 
   2458 	pnd->pnd_flags &= ~PND_INREAD;
   2459 
   2460 	return error;
   2461 }
   2462 
   2463 int
   2464 perfuse_node_write(pu, opc, buf, offset, resid, pcr, ioflag)
   2465 	struct puffs_usermount *pu;
   2466 	puffs_cookie_t opc;
   2467 	uint8_t *buf;
   2468 	off_t offset;
   2469 	size_t *resid;
   2470 	const struct puffs_cred *pcr;
   2471 	int ioflag;
   2472 {
   2473 	struct perfuse_state *ps;
   2474 	struct perfuse_node_data *pnd;
   2475 	perfuse_msg_t *pm;
   2476 	struct fuse_write_in *fwi;
   2477 	struct fuse_write_out *fwo;
   2478 	size_t data_len;
   2479 	size_t payload_len;
   2480 	size_t written;
   2481 	size_t requested;
   2482 	int error;
   2483 
   2484 	ps = puffs_getspecific(pu);
   2485 	pnd = PERFUSE_NODE_DATA(opc);
   2486 	pm = NULL;
   2487 	written = 0;
   2488 
   2489 	if (puffs_pn_getvap((struct puffs_node *)opc)->va_type == VDIR)
   2490 		return EBADF;
   2491 
   2492 DPRINTF("%s ENTER\n", __func__);
   2493 	pnd->pnd_flags |= PND_INWRITE;
   2494 
   2495 	requested = *resid;
   2496 	if ((ps->ps_write + requested) > ps->ps_max_write) {
   2497 		if (perfuse_diagflags & PDF_REQUEUE)
   2498 			DPRINTF("write = %zd\n", ps->ps_write);
   2499 		requeue_request(pu, opc, PCQ_WRITE);
   2500 	}
   2501 	ps->ps_write += requested;
   2502 
   2503 	do {
   2504 		/*
   2505 		 * It seems libfuse does not expects big chunks, so
   2506 		 * send it page per page. The writepage feature is
   2507 		 * probably there to minmize data movement.
   2508 		 * XXX use ps->ps_maxwrite?
   2509 		 */
   2510 		data_len = MIN(*resid, PAGE_SIZE);
   2511 		payload_len = data_len + sizeof(*fwi);
   2512 
   2513 		/*
   2514 		 * flags may be set to FUSE_WRITE_CACHE (XXX usage?)
   2515 		 * or FUSE_WRITE_LOCKOWNER, if lock_owner is provided.
   2516 		 * write_flags is set to 1 for writepage.
   2517 		 */
   2518 		pm = ps->ps_new_msg(pu, opc, FUSE_WRITE, payload_len, pcr);
   2519 		fwi = GET_INPAYLOAD(ps, pm, fuse_write_in);
   2520 		fwi->fh = perfuse_get_fh(opc, FWRITE);
   2521 		fwi->offset = offset;
   2522 		fwi->size = (uint32_t)data_len;
   2523 		fwi->write_flags = (fwi->size % PAGE_SIZE) ? 0 : 1;
   2524 		fwi->lock_owner = pnd->pnd_lock_owner;
   2525 		fwi->flags = 0;
   2526 		fwi->flags |= (fwi->lock_owner != 0) ? FUSE_WRITE_LOCKOWNER : 0;
   2527 		fwi->flags |= (ioflag & IO_DIRECT) ? 0 : FUSE_WRITE_CACHE;
   2528 		(void)memcpy((fwi + 1), buf + written, data_len);
   2529 
   2530 #ifdef PERFUSE_DEBUG
   2531 	if (perfuse_diagflags & PDF_FH)
   2532 		DPRINTF("%s: opc = %p, ino = %"PRId64", fh = 0x%"PRIx64"\n",
   2533 			__func__, (void *)opc, pnd->pnd_ino, fwi->fh);
   2534 #endif
   2535 		if ((error = XCHG_MSG(ps, pu, pm, sizeof(*fwo))) != 0)
   2536 			goto out;
   2537 
   2538 		fwo = GET_OUTPAYLOAD(ps, pm, fuse_write_out);
   2539 		written = fwo->size;
   2540 		*resid -= written;
   2541 		offset += written;
   2542 		buf += written;
   2543 
   2544 		ps->ps_destroy_msg(pm);
   2545 		pm = NULL;
   2546 	} while (*resid != 0);
   2547 
   2548 	/*
   2549 	 * puffs_ops(3) says
   2550 	 *  "everything must be written or an error will be generated"
   2551 	 */
   2552 	if (*resid != 0)
   2553 		error = EFBIG;
   2554 
   2555 	if (ioflag & (IO_SYNC|IO_DSYNC))
   2556 		ps->ps_syncwrites++;
   2557 	else
   2558 		ps->ps_asyncwrites++;
   2559 
   2560 	/*
   2561 	 * Remember to sync the file
   2562 	 */
   2563 	pnd->pnd_flags |= PND_DIRTY;
   2564 
   2565 #ifdef PERFUSE_DEBUG
   2566 	if (perfuse_diagflags & PDF_SYNC)
   2567 		DPRINTF("%s: DIRTY opc = %p, file = \"%s\"\n",
   2568 			__func__, (void*)opc,
   2569 			(char *)PNPATH((struct puffs_node *)opc));
   2570 #endif
   2571 out:
   2572 	if (pm != NULL)
   2573 		ps->ps_destroy_msg(pm);
   2574 
   2575 	ps->ps_write -= requested;
   2576 	dequeue_requests(ps, opc, PCQ_WRITE, 1);
   2577 
   2578 	pnd->pnd_flags &= ~PND_INWRITE;
   2579 
   2580 	/*
   2581 	 * Dequeue operation that were waiting for write to complete
   2582 	 */
   2583 	dequeue_requests(ps, opc, PCQ_AFTERWRITE, DEQUEUE_ALL);
   2584 
   2585 	return error;
   2586 }
   2587 
   2588 /* ARGSUSED0 */
   2589 void
   2590 perfuse_cache_write(pu, opc, size, runs)
   2591 	struct puffs_usermount *pu;
   2592 	puffs_cookie_t opc;
   2593 	size_t size;
   2594 	struct puffs_cacherun *runs;
   2595 {
   2596 	return;
   2597 }
   2598 
   2599