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