1/*  $NetBSD$ */
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 <fcntl.h>
32#include <string.h>
33#include <errno.h>
34#include <puffs.h>
35#include <sys/types.h>
36#include <sys/mman.h>
37#include <sys/resource.h>
38#include <sys/socket.h>
39#include <sys/extattr.h>
40#include <sys/hash.h>
41#include <sys/un.h>
42#include <machine/vmparam.h>
43
44#define LIBPERFUSE
45#include "perfuse.h"
46#include "perfuse_if.h"
47#include "perfuse_priv.h"
48
49int perfuse_diagflags = 0; /* global used only in DPRINTF/DERR/DWARN */
50extern char **environ;
51
52static struct perfuse_state *init_state(void);
53static int get_fd(const char *);
54
55
56static struct perfuse_state *
57init_state(void)
58{
59	struct perfuse_state *ps;
60	size_t len;
61	char opts[1024];
62	int i;
63
64	if ((ps = malloc(sizeof(*ps))) == NULL)
65		DERR(EX_OSERR, "%s:%d malloc failed", __func__, __LINE__);
66
67	(void)memset(ps, 0, sizeof(*ps));
68	ps->ps_max_write = UINT_MAX;
69	ps->ps_max_readahead = UINT_MAX;
70	TAILQ_INIT(&ps->ps_trace);
71
72	ps->ps_nnidhash = PUFFS_PNODEBUCKETS;
73	len = sizeof(*ps->ps_nidhash) * ps->ps_nnidhash;
74	if ((ps->ps_nidhash = malloc(len)) == NULL)
75		DERR(EX_OSERR, "%s:%d malloc failed", __func__, __LINE__);
76	for (i = 0; i < ps->ps_nnidhash; i++)
77		LIST_INIT(&ps->ps_nidhash[i]);
78
79	/*
80	 * Most of the time, access() is broken because the filesystem
81	 * performs the check with root privileges. glusterfs will do that
82	 * if the Linux-specific setfsuid() is missing, for instance.
83	 */
84	ps->ps_flags |= PS_NO_ACCESS;
85
86	/*
87	 * This is a temporary way to toggle access and creat usage.
88	 * It would be nice if that could be provided as mount options,
89	 * but that will not be obvious to do.
90 	 */
91	if (getenv_r("PERFUSE_OPTIONS", opts, sizeof(opts)) != -1) {
92		char *optname;
93		char *last;
94
95		 for ((optname = strtok_r(opts, ",", &last));
96		      optname != NULL;
97		      (optname = strtok_r(NULL, ",", &last))) {
98			if (strcmp(optname, "enable_access") == 0)
99				ps->ps_flags &= ~PS_NO_ACCESS;
100
101			if (strcmp(optname, "disable_access") == 0)
102				ps->ps_flags |= PS_NO_ACCESS;
103
104			if (strcmp(optname, "enable_creat") == 0)
105				ps->ps_flags &= ~PS_NO_CREAT;
106
107			if (strcmp(optname, "disable_creat") == 0)
108				ps->ps_flags |= PS_NO_CREAT;
109		}
110	}
111
112
113	return ps;
114}
115
116
117static int
118get_fd(const char *data)
119{
120	char *string;
121	const char fdopt[] = "fd=";
122	char *lastp;
123	char *opt;
124	int fd = -1;
125
126	if ((string = strdup(data)) == NULL)
127		return -1;
128
129	for (opt = strtok_r(string, ",", &lastp);
130	     opt != NULL;
131	     opt = strtok_r(NULL, ",", &lastp)) {
132		if (strncmp(opt, fdopt, strlen(fdopt)) == 0) {
133			fd =  atoi(opt + strlen(fdopt));
134			break;
135		}
136	}
137
138	/*
139	 * No file descriptor found
140	 */
141	if (fd == -1)
142		errno = EINVAL;
143
144	free(string);
145	return fd;
146
147}
148
149int
150perfuse_open(const char *path, int flags, mode_t mode)
151{
152	int sv[2];
153	struct sockaddr_un sun;
154	struct sockaddr *sa;
155	char progname[] = _PATH_PERFUSED;
156	char minus_i[] = "-i";
157	char fdstr[16];
158	char *const argv[] = { progname, minus_i, fdstr, NULL};
159	uint32_t opt;
160	uint32_t optlen;
161	int sock_type = SOCK_SEQPACKET;
162
163	if (strcmp(path, _PATH_FUSE) != 0)
164		return open(path, flags, mode);
165
166	/*
167	 * Try SOCK_SEQPACKET then SOCK_DGRAM if unavailable
168	 */
169	if ((sv[0] = socket(PF_LOCAL, SOCK_SEQPACKET, 0)) == -1) {
170		sock_type = SOCK_DGRAM;
171                DWARNX("SEQPACKET local sockets unavailable, using less "
172		       "reliable DGRAM sockets. Expect file operation hangs.");
173
174		if ((sv[0] = socket(PF_LOCAL, SOCK_DGRAM, 0)) == -1) {
175#ifdef PERFUSE_DEBUG
176			DWARN("%s: %d socket failed", __func__, __LINE__);
177#endif
178			return -1;
179		}
180	}
181
182	/*
183	 * Set a buffer lentgh large enough so that any FUSE packet
184	 * will fit.
185	 */
186	opt = (uint32_t)FUSE_BUFSIZE;
187	optlen = sizeof(opt);
188	if (setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &opt, optlen) != 0)
189		DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt);
190
191	if (setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &opt, optlen) != 0)
192		DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt);
193
194	sa = (struct sockaddr *)(void *)&sun;
195	sun.sun_len = sizeof(sun);
196	sun.sun_family = AF_LOCAL;
197	(void)strcpy(sun.sun_path, path);
198
199	if (connect(sv[0], sa, (socklen_t)sun.sun_len) == 0)
200		return sv[0];
201
202	/*
203	 * Attempt to run perfused on our own
204	 * if it does not run yet; In that case
205	 * we will talk using a socketpair
206	 * instead of /dev/fuse.
207	 */
208	if (socketpair(PF_LOCAL, sock_type, 0, sv) != 0) {
209		DWARN("%s:%d: socketpair failed", __func__, __LINE__);
210		return -1;
211	}
212
213	/*
214	 * Set a buffer lentgh large enough so that any FUSE packet
215	 * will fit.
216	 */
217	opt = (uint32_t)(4 * FUSE_BUFSIZE);
218	optlen = sizeof(opt);
219	if (setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &opt, optlen) != 0)
220		DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt);
221
222	if (setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &opt, optlen) != 0)
223		DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt);
224
225	if (setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &opt, optlen) != 0)
226		DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt);
227
228	if (setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &opt, optlen) != 0)
229		DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt);
230
231	/*
232	 * Request peer credentials. This musr be done before first
233	 * frame is sent.
234	 */
235	opt = 1;
236	optlen = sizeof(opt);
237	if (setsockopt(sv[1], 0, LOCAL_CREDS, &opt, optlen) != 0)
238		DWARN("%s: setsockopt LOCAL_CREDS failed", __func__);
239
240	(void)sprintf(fdstr, "%d", sv[1]);
241
242	switch(fork()) {
243	case -1:
244#ifdef PERFUSE_DEBUG
245		DWARN("%s:%d: fork failed", __func__, __LINE__);
246#endif
247		return -1;
248		/* NOTREACHED */
249		break;
250	case 0:
251		(void)close(sv[0]);
252		(void)execve(argv[0], argv, environ);
253#ifdef PERFUSE_DEBUG
254		DWARN("%s:%d: execve failed", __func__, __LINE__);
255#endif
256		return -1;
257		/* NOTREACHED */
258		break;
259	default:
260		break;
261	}
262
263	(void)close(sv[1]);
264	return sv[0];
265}
266
267int
268perfuse_mount(const char *source, const char *target,
269	const char *filesystemtype, long mountflags, const void *data)
270{
271	int s;
272	size_t len;
273	struct perfuse_mount_out *pmo;
274	struct sockaddr_storage ss;
275	struct sockaddr_un *sun;
276	struct sockaddr *sa;
277	socklen_t sa_len;
278	size_t sock_len;
279	char *frame;
280	char *cp;
281
282#ifdef PERFUSE_DEBUG
283	if (perfuse_diagflags & PDF_MISC)
284		DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\")\n",
285			__func__, source, target, filesystemtype,
286			mountflags, (const char *)data);
287#endif
288
289	if ((s = get_fd(data)) == -1)
290		return -1;
291
292	/*
293	 * If we are connected to /dev/fuse, we need a second
294	 * socket to get replies from perfused.
295	 * XXX This socket is not removed at exit time yet
296	 */
297	sock_len = 0;
298	sa = (struct sockaddr *)(void *)&ss;
299	sun = (struct sockaddr_un *)(void *)&ss;
300	sa_len = sizeof(ss);
301	if ((getpeername(s, sa, &sa_len) == 0) &&
302	    (sa->sa_family = AF_LOCAL) &&
303	    (strcmp(sun->sun_path, _PATH_FUSE) == 0)) {
304
305		sun->sun_len = sizeof(*sun);
306		sun->sun_family = AF_LOCAL;
307		(void)sprintf(sun->sun_path, "%s/%s-%d",
308			      _PATH_TMP, getprogname(), getpid());
309
310		if (bind(s, sa, (socklen_t)sa->sa_len) != 0)
311			DERR(EX_OSERR, "%s:%d bind to \"%s\" failed",
312			     __func__, __LINE__, sun->sun_path);
313
314		sock_len = strlen(sun->sun_path) + 1;
315	}
316
317	len = sizeof(*pmo);
318	len += source ? (uint32_t)strlen(source) + 1 : 0;
319	len += target ? (uint32_t)strlen(target) + 1 : 0;
320	len += filesystemtype ? (uint32_t)strlen(filesystemtype) + 1 : 0;
321	len += data ? (uint32_t)strlen(data) + 1 : 0;
322	len += sock_len;
323
324	if ((frame = malloc(len)) == NULL) {
325#ifdef PERFUSE_DEBUG
326		if (perfuse_diagflags & PDF_MISC)
327			DWARN("%s:%d malloc failed", __func__, __LINE__);
328#endif
329		return -1;
330	}
331
332	pmo = (struct perfuse_mount_out *)(void *)frame;
333	pmo->pmo_len = (uint32_t)len;
334	pmo->pmo_error = 0;
335	pmo->pmo_unique = (uint64_t)-1;
336	(void)strcpy(pmo->pmo_magic, PERFUSE_MOUNT_MAGIC);
337
338	pmo->pmo_source_len = source ? (uint32_t)strlen(source) + 1 : 0;
339	pmo->pmo_target_len = target ? (uint32_t)strlen(target) + 1: 0;
340	pmo->pmo_filesystemtype_len =
341	    filesystemtype ? (uint32_t)strlen(filesystemtype) + 1 : 0;
342	pmo->pmo_mountflags = (uint32_t)mountflags;
343	pmo->pmo_data_len = data ? (uint32_t)strlen(data) + 1 : 0;
344	pmo->pmo_sock_len = (uint32_t)sock_len;
345
346	cp = (char *)(void *)(pmo + 1);
347
348	if (source) {
349		(void)strcpy(cp, source);
350		cp += pmo->pmo_source_len;
351	}
352
353	if (target) {
354		(void)strcpy(cp, target);
355		cp += pmo->pmo_target_len;
356	}
357
358	if (filesystemtype) {
359		(void)strcpy(cp, filesystemtype);
360		cp += pmo->pmo_filesystemtype_len;
361	}
362
363	if (data) {
364		(void)strcpy(cp, data);
365		cp += pmo->pmo_data_len;
366	}
367
368	if (sock_len != 0) {
369		(void)strcpy(cp, sun->sun_path);
370		cp += pmo->pmo_sock_len;
371	}
372
373	if (send(s, frame, len, MSG_NOSIGNAL) != (ssize_t)len) {
374#ifdef PERFUSE_DEBUG
375		DWARN("%s:%d sendto failed", __func__, __LINE__);
376#endif
377		return -1;
378	}
379
380	return 0;
381}
382
383
384uint64_t
385perfuse_next_unique(struct puffs_usermount *pu)
386{
387	struct perfuse_state *ps;
388
389	ps = puffs_getspecific(pu);
390
391	return ps->ps_unique++;
392}
393
394struct puffs_usermount *
395perfuse_init(struct perfuse_callbacks *pc, struct perfuse_mount_info *pmi)
396{
397	struct perfuse_state *ps;
398	struct puffs_usermount *pu;
399	struct puffs_ops *pops;
400	const char *source = _PATH_PUFFS;
401	char *fstype;
402	unsigned int puffs_flags;
403	struct puffs_node *pn_root;
404	struct puffs_pathobj *po_root;
405	struct rlimit rl;
406
407	/*
408	 * perfused can grow quite large, let assume there's enough ram ...
409	 */
410	rl.rlim_cur = RLIM_INFINITY;
411	rl.rlim_max = RLIM_INFINITY;
412
413	if (setrlimit(RLIMIT_DATA, &rl) < 0) {
414		DERR(EX_OSERR, "%s: setrlimit failed: %s", __func__,
415		    strerror(errno));
416	}
417
418	if (setrlimit(RLIMIT_AS, &rl) < 0) {
419		DERR(EX_OSERR, "%s: setrlimit failed: %s", __func__,
420		    strerror(errno));
421	}
422
423	ps = init_state();
424	ps->ps_owner_uid = pmi->pmi_uid;
425
426	if (pmi->pmi_source) {
427		if ((ps->ps_source = strdup(pmi->pmi_source)) == NULL)
428			DERR(EX_OSERR, "%s: strdup failed", __func__);
429
430		source = ps->ps_source;
431	}
432
433	if (pmi->pmi_filesystemtype) {
434		size_t len;
435
436		ps->ps_filesystemtype = strdup(pmi->pmi_filesystemtype);
437		if (ps->ps_filesystemtype == NULL)
438			DERR(EX_OSERR, "%s: strdup failed", __func__);
439
440		len = sizeof("perfuse|") + strlen(ps->ps_filesystemtype) + 1;
441		if ((fstype = malloc(len)) == NULL)
442			DERR(EX_OSERR, "%s: malloc failed", __func__);
443
444		(void)sprintf(fstype, "perfuse|%s", ps->ps_filesystemtype);
445	} else {
446		if ((fstype = strdup("perfuse")) == NULL)
447			DERR(EX_OSERR, "%s: strdup failed", __func__);
448	}
449
450	if ((ps->ps_target = strdup(pmi->pmi_target)) == NULL)
451		DERR(EX_OSERR, "%s: strdup failed", __func__);
452
453	ps->ps_mountflags = pmi->pmi_mountflags;
454
455	/*
456	 * Some options are forbidden for non root users
457	 */
458	if (ps->ps_owner_uid != 0)
459	    ps->ps_mountflags |= MNT_NOSUID|MNT_NODEV;
460
461	PUFFSOP_INIT(pops);
462	PUFFSOP_SET(pops, perfuse, fs, unmount);
463	PUFFSOP_SET(pops, perfuse, fs, statvfs);
464	PUFFSOP_SET(pops, perfuse, fs, sync);
465	PUFFSOP_SET(pops, perfuse, node, lookup);
466	PUFFSOP_SET(pops, perfuse, node, create);
467	PUFFSOP_SET(pops, perfuse, node, mknod);
468	PUFFSOP_SET(pops, perfuse, node, open);
469	PUFFSOP_SET(pops, perfuse, node, close);
470	PUFFSOP_SET(pops, perfuse, node, access);
471	PUFFSOP_SET(pops, perfuse, node, getattr);
472	PUFFSOP_SET(pops, perfuse, node, setattr);
473	PUFFSOP_SET(pops, perfuse, node, poll);
474#if 0
475	PUFFSOP_SET(pops, perfuse, node, mmap);
476#endif
477	PUFFSOP_SET(pops, perfuse, node, fsync);
478	PUFFSOP_SET(pops, perfuse, node, seek);
479	PUFFSOP_SET(pops, perfuse, node, remove);
480	PUFFSOP_SET(pops, perfuse, node, link);
481	PUFFSOP_SET(pops, perfuse, node, rename);
482	PUFFSOP_SET(pops, perfuse, node, mkdir);
483	PUFFSOP_SET(pops, perfuse, node, rmdir);
484	PUFFSOP_SET(pops, perfuse, node, symlink);
485	PUFFSOP_SET(pops, perfuse, node, readdir);
486	PUFFSOP_SET(pops, perfuse, node, readlink);
487	PUFFSOP_SET(pops, perfuse, node, reclaim);
488	PUFFSOP_SET(pops, perfuse, node, inactive);
489	PUFFSOP_SET(pops, perfuse, node, print);
490	PUFFSOP_SET(pops, perfuse, node, advlock);
491	PUFFSOP_SET(pops, perfuse, node, read);
492	PUFFSOP_SET(pops, perfuse, node, write);
493#ifdef PUFFS_EXTNAMELEN
494	PUFFSOP_SET(pops, perfuse, node, getextattr);
495	PUFFSOP_SET(pops, perfuse, node, setextattr);
496	PUFFSOP_SET(pops, perfuse, node, listextattr);
497	PUFFSOP_SET(pops, perfuse, node, deleteextattr);
498#endif /* PUFFS_EXTNAMELEN */
499#ifdef PUFFS_KFLAG_CACHE_FS_TTL
500	PUFFSOP_SET(pops, perfuse, node, getattr_ttl);
501	PUFFSOP_SET(pops, perfuse, node, setattr_ttl);
502#endif /* PUFFS_KFLAG_CACHE_FS_TTL */
503#ifdef PUFFS_SETATTR_FAF
504	PUFFSOP_SET(pops, perfuse, node, write2);
505#endif /* PUFFS_SETATTR_FAF */
506
507	/*
508	 * PUFFS_KFLAG_NOCACHE_NAME is required so that we can see changes
509	 * done by other machines in networked filesystems. In later
510	 * NetBSD releases we use the alternative PUFFS_KFLAG_CACHE_FS_TTL,
511	 * which implement name cache with a filesystem-provided TTL.
512	 */
513#ifdef PUFFS_KFLAG_CACHE_FS_TTL
514	puffs_flags = PUFFS_KFLAG_CACHE_FS_TTL;
515#else
516	puffs_flags = PUFFS_KFLAG_NOCACHE_NAME;
517#endif
518
519	/*
520	 * Do not lookuo ..
521	 * That means we keep all parent vnode active
522	 */
523#ifdef PUFFS_KFLAG_CACHE_DOTDOT
524	puffs_flags |= PUFFS_KFLAG_CACHE_DOTDOT;
525#endif
526
527	/*
528	 * It would be nice to avoid useless inactive, and only
529	 * get them on file open for writing (PUFFS does
530	 * CLOSE/WRITE/INACTIVE, therefore actual close must be
531	 * done at INACTIVE time). Unfortunatley, puffs_setback
532	 * crashes when called on OPEN, therefore leave it for
533	 * another day.
534	 */
535#ifdef notyet
536	puffs_flags |= PUFFS_FLAG_IAONDEMAND;
537#endif
538
539	if (perfuse_diagflags & PDF_PUFFS)
540		puffs_flags |= PUFFS_FLAG_OPDUMP;
541
542	if ((pu = puffs_init(pops, source, fstype, ps, puffs_flags)) == NULL)
543		DERR(EX_OSERR, "%s: puffs_init failed", __func__);
544
545	puffs_setncookiehash(pu, PUFFS_PNODEBUCKETS);
546
547	ps->ps_pu = pu;
548
549	/*
550	 * Setup filesystem root
551	 */
552	pn_root = perfuse_new_pn(pu, "", NULL);
553	PERFUSE_NODE_DATA(pn_root)->pnd_nodeid = FUSE_ROOT_ID;
554	PERFUSE_NODE_DATA(pn_root)->pnd_parent_nodeid = FUSE_ROOT_ID;
555	perfuse_node_cache(ps, pn_root);
556	puffs_setroot(pu, pn_root);
557	ps->ps_fsid = pn_root->pn_va.va_fsid;
558
559	po_root = puffs_getrootpathobj(pu);
560	if ((po_root->po_path = strdup("/")) == NULL)
561		DERRX(EX_OSERR, "perfuse_mount_start() failed");
562
563	po_root->po_len = 1;
564	puffs_path_buildhash(pu, po_root);
565
566	puffs_vattr_null(&pn_root->pn_va);
567	pn_root->pn_va.va_type = VDIR;
568	pn_root->pn_va.va_mode = 0755;
569	pn_root->pn_va.va_fileid = FUSE_ROOT_ID;
570
571	ps->ps_root = pn_root;
572
573	/*
574	 *  Callbacks
575	 */
576	ps->ps_new_msg = pc->pc_new_msg;
577	ps->ps_xchg_msg = pc->pc_xchg_msg;
578	ps->ps_destroy_msg = pc->pc_destroy_msg;
579	ps->ps_get_inhdr = pc->pc_get_inhdr;
580	ps->ps_get_inpayload = pc->pc_get_inpayload;
581	ps->ps_get_outhdr = pc->pc_get_outhdr;
582	ps->ps_get_outpayload = pc->pc_get_outpayload;
583	ps->ps_umount = pc->pc_umount;
584
585	pc->pc_fsreq = *perfuse_fsreq;
586
587	return pu;
588}
589
590void
591perfuse_setspecific(struct puffs_usermount *pu, void *priv)
592{
593	struct perfuse_state *ps;
594
595	ps = puffs_getspecific(pu);
596	ps->ps_private = priv;
597
598	return;
599}
600
601void *
602perfuse_getspecific(struct puffs_usermount *pu)
603{
604	struct perfuse_state *ps;
605
606	ps = puffs_getspecific(pu);
607
608	return ps->ps_private;
609}
610
611int
612perfuse_inloop(struct puffs_usermount *pu)
613{
614	struct perfuse_state *ps;
615
616	ps = puffs_getspecific(pu);
617
618	return ps->ps_flags & PS_INLOOP;
619}
620
621int
622perfuse_mainloop(struct puffs_usermount *pu)
623{
624	struct perfuse_state *ps;
625
626	ps = puffs_getspecific(pu);
627
628	ps->ps_flags |= PS_INLOOP;
629	if (puffs_mainloop(ps->ps_pu) != 0) {
630		DERR(EX_OSERR, "%s: failed", __func__);
631		return -1;
632	}
633
634	/*
635	 * Normal exit after unmount
636	 */
637	return 0;
638}
639
640/* ARGSUSED0 */
641uint64_t
642perfuse_get_nodeid(struct puffs_usermount *pu, puffs_cookie_t opc)
643{
644	return PERFUSE_NODE_DATA(opc)->pnd_nodeid;
645}
646
647int
648perfuse_unmount(struct puffs_usermount *pu)
649{
650	struct perfuse_state *ps;
651
652	ps = puffs_getspecific(pu);
653
654	return unmount(ps->ps_target, MNT_FORCE);
655}
656
657void
658perfuse_fsreq(struct puffs_usermount *pu, perfuse_msg_t *pm)
659{
660	struct perfuse_state *ps;
661	struct fuse_out_header *foh;
662
663	ps = puffs_getspecific(pu);
664	foh = GET_OUTHDR(ps, pm);
665
666	/*
667	 * There are some operations we may use in a  Fire and Forget wey,
668	 * because the kernel does not await a reply, but FUSE still
669	 * sends a reply. This happens for fsyc, setattr (for metadata
670	 * associated with a fsync) and write (for VOP_PUTPAGES). Ignore
671	 * if it was fine, warn or abort otherwise.
672	 */
673	switch (foh->error) {
674	case 0:
675		break;
676	case -ENOENT:
677		/* File disapeared during a FAF operation */
678		break;
679	case -ENOTCONN: /* FALLTHROUGH */
680	case -EAGAIN: /* FALLTHROUGH */
681	case -EMSGSIZE:
682		DWARN("operation unique = %"PRId64" failed", foh->unique);
683		break;
684	default:
685		DWARNX("Unexpected frame: unique = %"PRId64", error = %d",
686		        foh->unique, foh->error);
687		/* NOTREACHED */
688		break;
689	}
690
691	ps->ps_destroy_msg(pm);
692
693	return;
694}
695