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