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