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