1 /* $NetBSD: perfuse.c,v 1.3 2010/08/28 03:46:21 manu Exp $ */ 2 3 /*- 4 * Copyright (c) 2010 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 39 #define LIBPERFUSE 40 #include "perfuse.h" 41 #include "perfuse_if.h" 42 #include "perfuse_priv.h" 43 44 int perfuse_diagflags = 0; /* global used only in DPRINTF/DERR/DWARN */ 45 46 static struct perfuse_state *init_state(void); 47 static int get_fd(const char *); 48 49 static struct perfuse_state * 50 init_state(void) 51 { 52 struct perfuse_state *ps; 53 54 if ((ps = malloc(sizeof(*ps))) == NULL) 55 DERR(EX_OSERR, "malloc failed"); 56 57 (void)memset(ps, 0, sizeof(*ps)); 58 ps->ps_max_write = UINT_MAX; 59 ps->ps_max_readahead = UINT_MAX; 60 61 return ps; 62 } 63 64 65 static int 66 get_fd(data) 67 const char *data; 68 { 69 char *string; 70 const char fdopt[] = "fd="; 71 char *lastp; 72 char *opt; 73 int fd = -1; 74 75 if ((string = strdup(data)) == NULL) 76 return -1; 77 78 for (opt = strtok_r(string, ",", &lastp); 79 opt != NULL; 80 opt = strtok_r(NULL, ",", &lastp)) { 81 if (strncmp(opt, fdopt, strlen(fdopt)) == 0) { 82 fd = atoi(opt + strlen(fdopt)); 83 break; 84 } 85 } 86 87 /* 88 * No file descriptor found 89 */ 90 if (fd == -1) 91 errno = EINVAL; 92 93 free(string); 94 return fd; 95 96 } 97 98 int 99 perfuse_open(path, flags, mode) 100 const char *path; 101 int flags; 102 mode_t mode; 103 { 104 int sv[2]; 105 struct sockaddr_un sun; 106 struct sockaddr *sa; 107 char progname[] = _PATH_PERFUSED; 108 char minus_i[] = "-i"; 109 char fdstr[16]; 110 char *const argv[] = { progname, minus_i, fdstr, NULL}; 111 char *const envp[] = { NULL }; 112 113 if (strcmp(path, _PATH_FUSE) != 0) 114 return open(path, flags, mode); 115 116 if ((sv[0] = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1) { 117 #ifdef PERFUSE_DEBUG 118 printf("%s:%d socket failed: %s\n", 119 __func__, __LINE__, strerror(errno)); 120 #endif 121 return -1; 122 } 123 124 sa = (struct sockaddr *)(void *)&sun; 125 sun.sun_len = sizeof(sun); 126 sun.sun_family = AF_LOCAL; 127 (void)strcpy(sun.sun_path, path); 128 129 if (connect(sv[0], sa, (socklen_t)sun.sun_len) == 0) 130 return sv[0]; 131 132 133 /* 134 * Attempt to run perfused on our own 135 * if it does not run yet; In that case 136 * we will talk using a socketpair 137 * instead of /dev/fuse. 138 */ 139 if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) != 0) { 140 #ifdef PERFUSE_DEBUG 141 printf("%s:%d: socketpair failed: %s\n", 142 __func__, __LINE__, strerror(errno)); 143 #endif 144 return -1; 145 } 146 147 (void)sprintf(fdstr, "%d", sv[1]); 148 149 switch(fork()) { 150 case -1: 151 #ifdef PERFUSE_DEBUG 152 printf("%s:%d: fork failed: %s\n", 153 __func__, __LINE__, strerror(errno)); 154 #endif 155 return -1; 156 /* NOTREACHED */ 157 break; 158 case 0: 159 (void)execve(argv[0], argv, envp); 160 #ifdef PERFUSE_DEBUG 161 printf("%s:%d: execve failed: %s\n", 162 __func__, __LINE__, strerror(errno)); 163 #endif 164 return -1; 165 /* NOTREACHED */ 166 break; 167 default: 168 break; 169 } 170 171 return sv[0]; 172 } 173 174 175 int 176 perfuse_mount(source, target, filesystemtype, mountflags, data) 177 const char *source; 178 const char *target; 179 const char *filesystemtype; 180 long mountflags; 181 const void *data; 182 { 183 int s; 184 #if 0 185 struct sockaddr_un sun; 186 #endif 187 size_t len; 188 struct perfuse_mount_out pmo; 189 190 #ifdef PERFUSE_DEBUG 191 printf("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\")\n", __func__, 192 source, target, filesystemtype, mountflags, (const char *)data); 193 #endif 194 195 #if 0 196 if ((s = socket(PF_LOCAL, SOCK_STREAM, 0)) == -1) 197 err(EX_OSERR, "socket failed"); 198 199 sun.sun_len = sizeof(sun); 200 sun.sun_family = AF_LOCAL; 201 (void)strcpy(sun.sun_path, _PATH_FUSE); 202 203 if (connect(s, (struct sockaddr *)&sun, sun.sun_len) == -1) 204 err(EX_UNAVAILABLE, "cannot connect to \"%s\"", _PATH_FUSE); 205 #endif 206 if ((s = get_fd(data)) == -1) 207 return -1; 208 209 210 pmo.pmo_len = sizeof(pmo); 211 pmo.pmo_len += source ? strlen(source) : 0; 212 pmo.pmo_len += target ? strlen(target) : 0; 213 pmo.pmo_len += filesystemtype ? strlen(filesystemtype) : 0; 214 pmo.pmo_len += data ? strlen(data) : 0; 215 pmo.pmo_error = 0; 216 pmo.pmo_unique = (uint64_t)-1; 217 218 (void)strcpy(pmo.pmo_magic, PERFUSE_MOUNT_MAGIC); 219 pmo.pmo_source_len = source ? strlen(source) : 0; 220 pmo.pmo_target_len = target ? strlen(target) : 0; 221 pmo.pmo_filesystemtype_len = 222 filesystemtype ? strlen(filesystemtype) : 0; 223 pmo.pmo_mountflags = mountflags; 224 pmo.pmo_data_len = data ? strlen(data) : 0; 225 226 227 if (write(s, &pmo, sizeof(pmo)) != sizeof(pmo)) { 228 #ifdef PERFUSE_DEBUG 229 printf("%s:%d short write\n", __func__, __LINE__); 230 #endif 231 return -1; 232 } 233 234 if (source) { 235 len = pmo.pmo_source_len; 236 if (write(s, source, len) != (ssize_t)len) { 237 #ifdef PERFUSE_DEBUG 238 printf("%s:%d short write\n", __func__, __LINE__); 239 #endif 240 return -1; 241 } 242 } 243 244 if (target) { 245 len = pmo.pmo_target_len; 246 if (write(s, target, len) != (ssize_t)len) { 247 #ifdef PERFUSE_DEBUG 248 printf("%s:%d short write\n", __func__, __LINE__); 249 #endif 250 return -1; 251 } 252 } 253 254 if (filesystemtype) { 255 len = pmo.pmo_filesystemtype_len; 256 if (write(s, filesystemtype, len) != (ssize_t)len) { 257 #ifdef PERFUSE_DEBUG 258 printf("%s:%d short write\n", __func__, __LINE__); 259 #endif 260 return -1; 261 } 262 } 263 264 if (data) { 265 len = pmo.pmo_data_len; 266 if (write(s, data, len) != (ssize_t)len) { 267 #ifdef PERFUSE_DEBUG 268 printf("%s:%d short write\n", __func__, __LINE__); 269 #endif 270 return -1; 271 } 272 } 273 274 return 0; 275 } 276 277 278 uint64_t 279 perfuse_next_unique(pu) 280 struct puffs_usermount *pu; 281 { 282 struct perfuse_state *ps; 283 284 ps = puffs_getspecific(pu); 285 286 return ps->ps_unique++; 287 } 288 289 struct puffs_usermount * 290 perfuse_init(pc, pmi) 291 struct perfuse_callbacks *pc; 292 struct perfuse_mount_info *pmi; 293 { 294 struct perfuse_state *ps; 295 struct puffs_usermount *pu; 296 struct puffs_ops *pops; 297 char name[] = "perfuse"; 298 unsigned int puffs_flags; 299 struct puffs_node *pn_root; 300 struct puffs_pathobj *po_root; 301 302 ps = init_state(); 303 ps->ps_owner_uid = pmi->pmi_uid; 304 305 if (pmi->pmi_source) 306 ps->ps_source = strdup(pmi->pmi_source); 307 if (pmi->pmi_filesystemtype) 308 ps->ps_filesystemtype = strdup(pmi->pmi_filesystemtype); 309 ps->ps_target = strdup(pmi->pmi_target); 310 ps->ps_mountflags = pmi->pmi_mountflags; 311 312 /* 313 * Some options are forbidden for non root users 314 */ 315 if (ps->ps_owner_uid != 0) 316 ps->ps_mountflags |= MNT_NOSUID|MNT_NODEV; 317 318 PUFFSOP_INIT(pops); 319 PUFFSOP_SET(pops, perfuse, fs, unmount); 320 PUFFSOP_SET(pops, perfuse, fs, statvfs); 321 PUFFSOP_SET(pops, perfuse, fs, sync); 322 PUFFSOP_SET(pops, perfuse, node, lookup); 323 PUFFSOP_SET(pops, perfuse, node, create); 324 PUFFSOP_SET(pops, perfuse, node, mknod); 325 PUFFSOP_SET(pops, perfuse, node, open); 326 PUFFSOP_SET(pops, perfuse, node, close); 327 PUFFSOP_SET(pops, perfuse, node, access); 328 PUFFSOP_SET(pops, perfuse, node, getattr); 329 PUFFSOP_SET(pops, perfuse, node, setattr); 330 PUFFSOP_SET(pops, perfuse, node, poll); 331 #if 0 332 PUFFSOP_SET(pops, perfuse, node, mmap); 333 #endif 334 PUFFSOP_SET(pops, perfuse, node, fsync); 335 PUFFSOP_SET(pops, perfuse, node, seek); 336 PUFFSOP_SET(pops, perfuse, node, remove); 337 PUFFSOP_SET(pops, perfuse, node, link); 338 PUFFSOP_SET(pops, perfuse, node, rename); 339 PUFFSOP_SET(pops, perfuse, node, mkdir); 340 PUFFSOP_SET(pops, perfuse, node, rmdir); 341 PUFFSOP_SET(pops, perfuse, node, symlink); 342 PUFFSOP_SET(pops, perfuse, node, readdir); 343 PUFFSOP_SET(pops, perfuse, node, readlink); 344 PUFFSOP_SET(pops, perfuse, node, reclaim); 345 PUFFSOP_SET(pops, perfuse, node, inactive); 346 PUFFSOP_SET(pops, perfuse, node, print); 347 PUFFSOP_SET(pops, perfuse, node, advlock); 348 PUFFSOP_SET(pops, perfuse, node, read); 349 PUFFSOP_SET(pops, perfuse, node, write); 350 351 puffs_flags = PUFFS_FLAG_BUILDPATH | PUFFS_FLAG_HASHPATH; 352 if (perfuse_diagflags & PDF_PUFFS) 353 puffs_flags |= PUFFS_FLAG_OPDUMP; 354 355 if ((pu = puffs_init(pops, _PATH_PUFFS, name, ps, puffs_flags)) == NULL) 356 DERR(EX_OSERR, "puffs_init failed"); 357 358 ps->ps_pu = pu; 359 360 /* 361 * Setup filesystem root 362 */ 363 pn_root = perfuse_new_pn(pu, NULL); 364 PERFUSE_NODE_DATA(pn_root)->pnd_ino = FUSE_ROOT_ID; 365 PERFUSE_NODE_DATA(pn_root)->pnd_parent = pn_root; 366 puffs_setroot(pu, pn_root); 367 ps->ps_fsid = pn_root->pn_va.va_fsid; 368 369 po_root = puffs_getrootpathobj(pu); 370 if ((po_root->po_path = strdup("/")) == NULL) 371 DERRX(EX_OSERR, "perfuse_mount_start() failed"); 372 373 po_root->po_len = 1; 374 puffs_path_buildhash(pu, po_root); 375 376 puffs_vattr_null(&pn_root->pn_va); 377 pn_root->pn_va.va_type = VDIR; 378 pn_root->pn_va.va_mode = 0755; 379 380 ps->ps_root = pn_root; 381 382 /* 383 * Callbacks 384 */ 385 ps->ps_new_msg = pc->pc_new_msg; 386 ps->ps_xchg_msg = pc->pc_xchg_msg; 387 ps->ps_destroy_msg = pc->pc_destroy_msg; 388 ps->ps_get_inhdr = pc->pc_get_inhdr; 389 ps->ps_get_inpayload = pc->pc_get_inpayload; 390 ps->ps_get_outhdr = pc->pc_get_outhdr; 391 ps->ps_get_outpayload = pc->pc_get_outpayload; 392 393 return pu; 394 } 395 396 void 397 perfuse_setspecific(pu, priv) 398 struct puffs_usermount *pu; 399 void *priv; 400 { 401 struct perfuse_state *ps; 402 403 ps = puffs_getspecific(pu); 404 ps->ps_private = priv; 405 406 return; 407 } 408 409 void * 410 perfuse_getspecific(pu) 411 struct puffs_usermount *pu; 412 { 413 struct perfuse_state *ps; 414 415 ps = puffs_getspecific(pu); 416 417 return ps->ps_private; 418 } 419 420 int 421 perfuse_inloop(pu) 422 struct puffs_usermount *pu; 423 { 424 struct perfuse_state *ps; 425 426 ps = puffs_getspecific(pu); 427 428 return ps->ps_flags & PS_INLOOP; 429 } 430 431 int 432 perfuse_mainloop(pu) 433 struct puffs_usermount *pu; 434 { 435 struct perfuse_state *ps; 436 437 ps = puffs_getspecific(pu); 438 439 ps->ps_flags |= PS_INLOOP; 440 if (puffs_mainloop(ps->ps_pu) != 0) 441 DERR(EX_OSERR, "puffs_mainloop failed"); 442 DERR(EX_OSERR, "puffs_mainloop exit"); 443 444 /* NOTREACHED */ 445 return -1; 446 } 447 448 /* ARGSUSED0 */ 449 uint64_t 450 perfuse_get_ino(pu, opc) 451 struct puffs_usermount *pu; 452 puffs_cookie_t opc; 453 { 454 return PERFUSE_NODE_DATA(opc)->pnd_ino; 455 } 456 457 int 458 perfuse_unmount(pu) 459 struct puffs_usermount *pu; 460 { 461 struct perfuse_state *ps; 462 463 ps = puffs_getspecific(pu); 464 465 return unmount(ps->ps_target, MNT_FORCE); 466 } 467