1 /* $NetBSD: perfuse.c,v 1.15 2011/05/30 14:50:08 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 int sock_type = SOCK_SEQPACKET; 152 153 if (strcmp(path, _PATH_FUSE) != 0) 154 return open(path, flags, mode); 155 156 /* 157 * Try SOCK_SEQPACKET then SOCK_DGRAM if unavailable 158 */ 159 if ((sv[0] = socket(PF_LOCAL, SOCK_SEQPACKET, 0)) == -1) { 160 sock_type = SOCK_DGRAM; 161 DWARNX("SEQPACKET local sockets unavailable, using less " 162 "reliable DGRAM sockets. Expect file operation hangs."); 163 164 if ((sv[0] = socket(PF_LOCAL, SOCK_DGRAM, 0)) == -1) { 165 #ifdef PERFUSE_DEBUG 166 DWARN("%s:%d socket failed: %s", __func__, __LINE__); 167 #endif 168 return -1; 169 } 170 } 171 172 /* 173 * Set a buffer lentgh large enough so that any FUSE packet 174 * will fit. 175 */ 176 opt = FUSE_BUFSIZE; 177 optlen = sizeof(opt); 178 if (setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &opt, optlen) != 0) 179 DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt); 180 181 opt = FUSE_BUFSIZE; 182 optlen = sizeof(opt); 183 if (setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &opt, optlen) != 0) 184 DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt); 185 186 sa = (struct sockaddr *)(void *)&sun; 187 sun.sun_len = sizeof(sun); 188 sun.sun_family = AF_LOCAL; 189 (void)strcpy(sun.sun_path, path); 190 191 if (connect(sv[0], sa, (socklen_t)sun.sun_len) == 0) 192 return sv[0]; 193 194 /* 195 * Attempt to run perfused on our own 196 * if it does not run yet; In that case 197 * we will talk using a socketpair 198 * instead of /dev/fuse. 199 */ 200 if (socketpair(PF_LOCAL, sock_type, 0, sv) != 0) { 201 DWARN("%s:%d: socketpair failed", __func__, __LINE__); 202 return -1; 203 } 204 205 /* 206 * Set a buffer lentgh large enough so that any FUSE packet 207 * will fit. 208 */ 209 opt = 4 * FUSE_BUFSIZE; 210 optlen = sizeof(opt); 211 if (setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &opt, optlen) != 0) 212 DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt); 213 214 opt = 4 * FUSE_BUFSIZE; 215 optlen = sizeof(opt); 216 if (setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &opt, optlen) != 0) 217 DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt); 218 219 opt = 4 * FUSE_BUFSIZE; 220 optlen = sizeof(opt); 221 if (setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &opt, optlen) != 0) 222 DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt); 223 224 opt = 4 * FUSE_BUFSIZE; 225 optlen = sizeof(opt); 226 if (setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &opt, optlen) != 0) 227 DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt); 228 229 /* 230 * Request peer credentials. This musr be done before first 231 * frame is sent. 232 */ 233 opt = 1; 234 optlen = sizeof(opt); 235 if (setsockopt(sv[1], 0, LOCAL_CREDS, &opt, optlen) != 0) 236 DWARN("%s: setsockopt LOCAL_CREDS failed", __func__); 237 238 (void)sprintf(fdstr, "%d", sv[1]); 239 240 switch(fork()) { 241 case -1: 242 #ifdef PERFUSE_DEBUG 243 DWARN("%s:%d: fork failed", __func__, __LINE__); 244 #endif 245 return -1; 246 /* NOTREACHED */ 247 break; 248 case 0: 249 (void)execve(argv[0], argv, environ); 250 #ifdef PERFUSE_DEBUG 251 DWARN("%s:%d: execve failed", __func__, __LINE__); 252 #endif 253 return -1; 254 /* NOTREACHED */ 255 break; 256 default: 257 break; 258 } 259 260 return sv[0]; 261 } 262 263 int 264 perfuse_mount(source, target, filesystemtype, mountflags, data) 265 const char *source; 266 const char *target; 267 const char *filesystemtype; 268 long mountflags; 269 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 384 uint64_t 385 perfuse_next_unique(pu) 386 struct puffs_usermount *pu; 387 { 388 struct perfuse_state *ps; 389 390 ps = puffs_getspecific(pu); 391 392 return ps->ps_unique++; 393 } 394 395 struct puffs_usermount * 396 perfuse_init(pc, pmi) 397 struct perfuse_callbacks *pc; 398 struct perfuse_mount_info *pmi; 399 { 400 struct perfuse_state *ps; 401 struct puffs_usermount *pu; 402 struct puffs_ops *pops; 403 const char *source = _PATH_PUFFS; 404 char *fstype; 405 unsigned int puffs_flags; 406 struct puffs_node *pn_root; 407 struct puffs_pathobj *po_root; 408 409 ps = init_state(); 410 ps->ps_owner_uid = pmi->pmi_uid; 411 412 if (pmi->pmi_source) { 413 if ((ps->ps_source = strdup(pmi->pmi_source)) == NULL) 414 DERR(EX_OSERR, "strdup failed"); 415 416 source = ps->ps_source; 417 } 418 419 if (pmi->pmi_filesystemtype) { 420 size_t len; 421 422 ps->ps_filesystemtype = strdup(pmi->pmi_filesystemtype); 423 if (ps->ps_filesystemtype == NULL) 424 DERR(EX_OSERR, "strdup failed"); 425 426 len = sizeof("perfuse|") + strlen(ps->ps_filesystemtype) + 1; 427 if ((fstype = malloc(len)) == NULL) 428 DERR(EX_OSERR, "malloc failed"); 429 430 (void)sprintf(fstype, "perfuse|%s", ps->ps_filesystemtype); 431 } else { 432 if ((fstype = strdup("perfuse")) == NULL) 433 DERR(EX_OSERR, "strdup failed"); 434 } 435 436 if ((ps->ps_target = strdup(pmi->pmi_target)) == NULL) 437 DERR(EX_OSERR, "strdup failed"); 438 439 ps->ps_mountflags = pmi->pmi_mountflags; 440 441 /* 442 * Some options are forbidden for non root users 443 */ 444 if (ps->ps_owner_uid != 0) 445 ps->ps_mountflags |= MNT_NOSUID|MNT_NODEV; 446 447 PUFFSOP_INIT(pops); 448 PUFFSOP_SET(pops, perfuse, fs, unmount); 449 PUFFSOP_SET(pops, perfuse, fs, statvfs); 450 PUFFSOP_SET(pops, perfuse, fs, sync); 451 PUFFSOP_SET(pops, perfuse, node, lookup); 452 PUFFSOP_SET(pops, perfuse, node, create); 453 PUFFSOP_SET(pops, perfuse, node, mknod); 454 PUFFSOP_SET(pops, perfuse, node, open); 455 PUFFSOP_SET(pops, perfuse, node, close); 456 PUFFSOP_SET(pops, perfuse, node, access); 457 PUFFSOP_SET(pops, perfuse, node, getattr); 458 PUFFSOP_SET(pops, perfuse, node, setattr); 459 PUFFSOP_SET(pops, perfuse, node, poll); 460 #if 0 461 PUFFSOP_SET(pops, perfuse, node, mmap); 462 #endif 463 PUFFSOP_SET(pops, perfuse, node, fsync); 464 PUFFSOP_SET(pops, perfuse, node, seek); 465 PUFFSOP_SET(pops, perfuse, node, remove); 466 PUFFSOP_SET(pops, perfuse, node, link); 467 PUFFSOP_SET(pops, perfuse, node, rename); 468 PUFFSOP_SET(pops, perfuse, node, mkdir); 469 PUFFSOP_SET(pops, perfuse, node, rmdir); 470 PUFFSOP_SET(pops, perfuse, node, symlink); 471 PUFFSOP_SET(pops, perfuse, node, readdir); 472 PUFFSOP_SET(pops, perfuse, node, readlink); 473 PUFFSOP_SET(pops, perfuse, node, reclaim); 474 PUFFSOP_SET(pops, perfuse, node, inactive); 475 PUFFSOP_SET(pops, perfuse, node, print); 476 PUFFSOP_SET(pops, perfuse, node, advlock); 477 PUFFSOP_SET(pops, perfuse, node, read); 478 PUFFSOP_SET(pops, perfuse, node, write); 479 480 puffs_flags = PUFFS_KFLAG_WTCACHE; 481 482 if (perfuse_diagflags & PDF_PUFFS) 483 puffs_flags |= PUFFS_FLAG_OPDUMP; 484 485 if ((pu = puffs_init(pops, source, fstype, ps, puffs_flags)) == NULL) 486 DERR(EX_OSERR, "puffs_init failed"); 487 488 ps->ps_pu = pu; 489 490 /* 491 * Setup filesystem root 492 */ 493 pn_root = perfuse_new_pn(pu, "", NULL); 494 PERFUSE_NODE_DATA(pn_root)->pnd_ino = FUSE_ROOT_ID; 495 PERFUSE_NODE_DATA(pn_root)->pnd_parent = pn_root; 496 puffs_setroot(pu, pn_root); 497 ps->ps_fsid = pn_root->pn_va.va_fsid; 498 499 po_root = puffs_getrootpathobj(pu); 500 if ((po_root->po_path = strdup("/")) == NULL) 501 DERRX(EX_OSERR, "perfuse_mount_start() failed"); 502 503 po_root->po_len = 1; 504 puffs_path_buildhash(pu, po_root); 505 506 puffs_vattr_null(&pn_root->pn_va); 507 pn_root->pn_va.va_type = VDIR; 508 pn_root->pn_va.va_mode = 0755; 509 510 ps->ps_root = pn_root; 511 512 /* 513 * Callbacks 514 */ 515 ps->ps_new_msg = pc->pc_new_msg; 516 ps->ps_xchg_msg = pc->pc_xchg_msg; 517 ps->ps_destroy_msg = pc->pc_destroy_msg; 518 ps->ps_get_inhdr = pc->pc_get_inhdr; 519 ps->ps_get_inpayload = pc->pc_get_inpayload; 520 ps->ps_get_outhdr = pc->pc_get_outhdr; 521 ps->ps_get_outpayload = pc->pc_get_outpayload; 522 ps->ps_umount = pc->pc_umount; 523 524 return pu; 525 } 526 527 void 528 perfuse_setspecific(pu, priv) 529 struct puffs_usermount *pu; 530 void *priv; 531 { 532 struct perfuse_state *ps; 533 534 ps = puffs_getspecific(pu); 535 ps->ps_private = priv; 536 537 return; 538 } 539 540 void * 541 perfuse_getspecific(pu) 542 struct puffs_usermount *pu; 543 { 544 struct perfuse_state *ps; 545 546 ps = puffs_getspecific(pu); 547 548 return ps->ps_private; 549 } 550 551 int 552 perfuse_inloop(pu) 553 struct puffs_usermount *pu; 554 { 555 struct perfuse_state *ps; 556 557 ps = puffs_getspecific(pu); 558 559 return ps->ps_flags & PS_INLOOP; 560 } 561 562 int 563 perfuse_mainloop(pu) 564 struct puffs_usermount *pu; 565 { 566 struct perfuse_state *ps; 567 568 ps = puffs_getspecific(pu); 569 570 ps->ps_flags |= PS_INLOOP; 571 if (puffs_mainloop(ps->ps_pu) != 0) { 572 DERR(EX_OSERR, "puffs_mainloop failed"); 573 return -1; 574 } 575 576 /* 577 * Normal exit after unmount 578 */ 579 return 0; 580 } 581 582 /* ARGSUSED0 */ 583 uint64_t 584 perfuse_get_ino(pu, opc) 585 struct puffs_usermount *pu; 586 puffs_cookie_t opc; 587 { 588 return PERFUSE_NODE_DATA(opc)->pnd_ino; 589 } 590 591 int 592 perfuse_unmount(pu) 593 struct puffs_usermount *pu; 594 { 595 struct perfuse_state *ps; 596 597 ps = puffs_getspecific(pu); 598 599 return unmount(ps->ps_target, MNT_FORCE); 600 } 601