You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

600 lines
14 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU LGPLv2.
  5. See the file COPYING.LIB.
  6. */
  7. #include "config.h"
  8. #include "fuse_i.h"
  9. #include "fuse_misc.h"
  10. #include "fuse_opt.h"
  11. #include "mount_util.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <stddef.h>
  16. #include <string.h>
  17. #include <fcntl.h>
  18. #include <errno.h>
  19. #include <sys/poll.h>
  20. #include <sys/socket.h>
  21. #include <sys/un.h>
  22. #include <sys/wait.h>
  23. #include <sys/mount.h>
  24. #ifdef __NetBSD__
  25. #include <perfuse.h>
  26. #define MS_RDONLY MNT_RDONLY
  27. #define MS_NOSUID MNT_NOSUID
  28. #define MS_NODEV MNT_NODEV
  29. #define MS_NOEXEC MNT_NOEXEC
  30. #define MS_SYNCHRONOUS MNT_SYNCHRONOUS
  31. #define MS_NOATIME MNT_NOATIME
  32. #define umount2(mnt, flags) unmount(mnt, (flags == 2) ? MNT_FORCE : 0)
  33. #endif
  34. #define FUSERMOUNT_PROG "fusermount"
  35. #define FUSE_COMMFD_ENV "_FUSE_COMMFD"
  36. #ifndef HAVE_FORK
  37. #define fork() vfork()
  38. #endif
  39. #ifndef MS_DIRSYNC
  40. #define MS_DIRSYNC 128
  41. #endif
  42. enum {
  43. KEY_KERN_FLAG,
  44. KEY_KERN_OPT,
  45. KEY_FUSERMOUNT_OPT,
  46. KEY_SUBTYPE_OPT,
  47. KEY_MTAB_OPT,
  48. KEY_RO,
  49. KEY_HELP,
  50. KEY_VERSION,
  51. };
  52. struct mount_opts {
  53. int ishelp;
  54. int flags;
  55. int auto_unmount;
  56. int blkdev;
  57. char *fsname;
  58. char *subtype;
  59. char *subtype_opt;
  60. char *mtab_opts;
  61. char *fusermount_opts;
  62. char *kernel_opts;
  63. };
  64. #define FUSE_MOUNT_OPT(t, p) { t, offsetof(struct mount_opts, p), 1 }
  65. static const struct fuse_opt fuse_mount_opts[] = {
  66. FUSE_MOUNT_OPT("blkdev", blkdev),
  67. FUSE_MOUNT_OPT("auto_unmount", auto_unmount),
  68. FUSE_MOUNT_OPT("fsname=%s", fsname),
  69. FUSE_MOUNT_OPT("subtype=%s", subtype),
  70. FUSE_OPT_KEY("auto_unmount", KEY_FUSERMOUNT_OPT),
  71. FUSE_OPT_KEY("blkdev", KEY_FUSERMOUNT_OPT),
  72. FUSE_OPT_KEY("fsname=", KEY_FUSERMOUNT_OPT),
  73. FUSE_OPT_KEY("subtype=", KEY_SUBTYPE_OPT),
  74. FUSE_OPT_KEY("large_read", KEY_KERN_OPT),
  75. FUSE_OPT_KEY("blksize=", KEY_KERN_OPT),
  76. FUSE_OPT_KEY("default_permissions", KEY_KERN_OPT),
  77. FUSE_OPT_KEY("context=", KEY_KERN_OPT),
  78. FUSE_OPT_KEY("fscontext=", KEY_KERN_OPT),
  79. FUSE_OPT_KEY("defcontext=", KEY_KERN_OPT),
  80. FUSE_OPT_KEY("rootcontext=", KEY_KERN_OPT),
  81. FUSE_OPT_KEY("max_read=", KEY_KERN_OPT),
  82. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP),
  83. FUSE_OPT_KEY("user=", KEY_MTAB_OPT),
  84. FUSE_OPT_KEY("-r", KEY_RO),
  85. FUSE_OPT_KEY("ro", KEY_KERN_FLAG),
  86. FUSE_OPT_KEY("rw", KEY_KERN_FLAG),
  87. FUSE_OPT_KEY("suid", KEY_KERN_FLAG),
  88. FUSE_OPT_KEY("nosuid", KEY_KERN_FLAG),
  89. FUSE_OPT_KEY("dev", KEY_KERN_FLAG),
  90. FUSE_OPT_KEY("nodev", KEY_KERN_FLAG),
  91. FUSE_OPT_KEY("exec", KEY_KERN_FLAG),
  92. FUSE_OPT_KEY("noexec", KEY_KERN_FLAG),
  93. FUSE_OPT_KEY("async", KEY_KERN_FLAG),
  94. FUSE_OPT_KEY("sync", KEY_KERN_FLAG),
  95. FUSE_OPT_KEY("dirsync", KEY_KERN_FLAG),
  96. FUSE_OPT_KEY("atime", KEY_KERN_FLAG),
  97. FUSE_OPT_KEY("noatime", KEY_KERN_FLAG),
  98. FUSE_OPT_KEY("-h", KEY_HELP),
  99. FUSE_OPT_KEY("--help", KEY_HELP),
  100. FUSE_OPT_KEY("-V", KEY_VERSION),
  101. FUSE_OPT_KEY("--version", KEY_VERSION),
  102. FUSE_OPT_END
  103. };
  104. static void mount_help(void)
  105. {
  106. fprintf(stderr,
  107. " -o auto_unmount auto unmount on process termination\n"
  108. " -o default_permissions enable permission checking by kernel\n"
  109. " -o fsname=NAME set filesystem name\n"
  110. " -o subtype=NAME set filesystem type\n"
  111. " -o large_read issue large read requests (2.4 only)\n"
  112. " -o max_read=N set maximum size of read requests\n"
  113. "\n");
  114. }
  115. static void exec_fusermount(const char *argv[])
  116. {
  117. execv(FUSERMOUNT_DIR "/mergerfs-" FUSERMOUNT_PROG, (char **) argv);
  118. execvp("mergerfs-" FUSERMOUNT_PROG, (char **) argv);
  119. execv(FUSERMOUNT_DIR "/" FUSERMOUNT_PROG, (char **) argv);
  120. execvp(FUSERMOUNT_PROG, (char **) argv);
  121. }
  122. static void mount_version(void)
  123. {
  124. int pid = fork();
  125. if (!pid) {
  126. const char *argv[] = { FUSERMOUNT_PROG, "--version", NULL };
  127. exec_fusermount(argv);
  128. _exit(1);
  129. } else if (pid != -1)
  130. waitpid(pid, NULL, 0);
  131. }
  132. struct mount_flags {
  133. const char *opt;
  134. unsigned long flag;
  135. int on;
  136. };
  137. static const struct mount_flags mount_flags[] = {
  138. {"rw", MS_RDONLY, 0},
  139. {"ro", MS_RDONLY, 1},
  140. {"suid", MS_NOSUID, 0},
  141. {"nosuid", MS_NOSUID, 1},
  142. {"dev", MS_NODEV, 0},
  143. {"nodev", MS_NODEV, 1},
  144. {"exec", MS_NOEXEC, 0},
  145. {"noexec", MS_NOEXEC, 1},
  146. {"async", MS_SYNCHRONOUS, 0},
  147. {"sync", MS_SYNCHRONOUS, 1},
  148. {"atime", MS_NOATIME, 0},
  149. {"noatime", MS_NOATIME, 1},
  150. #ifndef __NetBSD__
  151. {"dirsync", MS_DIRSYNC, 1},
  152. #endif
  153. {NULL, 0, 0}
  154. };
  155. static void set_mount_flag(const char *s, int *flags)
  156. {
  157. int i;
  158. for (i = 0; mount_flags[i].opt != NULL; i++) {
  159. const char *opt = mount_flags[i].opt;
  160. if (strcmp(opt, s) == 0) {
  161. if (mount_flags[i].on)
  162. *flags |= mount_flags[i].flag;
  163. else
  164. *flags &= ~mount_flags[i].flag;
  165. return;
  166. }
  167. }
  168. fprintf(stderr, "fuse: internal error, can't find mount flag\n");
  169. abort();
  170. }
  171. static int fuse_mount_opt_proc(void *data, const char *arg, int key,
  172. struct fuse_args *outargs)
  173. {
  174. struct mount_opts *mo = data;
  175. switch (key) {
  176. case KEY_RO:
  177. arg = "ro";
  178. /* fall through */
  179. case KEY_KERN_FLAG:
  180. set_mount_flag(arg, &mo->flags);
  181. return 0;
  182. case KEY_KERN_OPT:
  183. return fuse_opt_add_opt(&mo->kernel_opts, arg);
  184. case KEY_FUSERMOUNT_OPT:
  185. return fuse_opt_add_opt_escaped(&mo->fusermount_opts, arg);
  186. case KEY_SUBTYPE_OPT:
  187. return fuse_opt_add_opt(&mo->subtype_opt, arg);
  188. case KEY_MTAB_OPT:
  189. return fuse_opt_add_opt(&mo->mtab_opts, arg);
  190. case KEY_HELP:
  191. mount_help();
  192. mo->ishelp = 1;
  193. break;
  194. case KEY_VERSION:
  195. mount_version();
  196. mo->ishelp = 1;
  197. break;
  198. }
  199. return 1;
  200. }
  201. /* return value:
  202. * >= 0 => fd
  203. * -1 => error
  204. */
  205. static int receive_fd(int fd)
  206. {
  207. struct msghdr msg;
  208. struct iovec iov;
  209. char buf[1];
  210. int rv;
  211. size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)];
  212. struct cmsghdr *cmsg;
  213. iov.iov_base = buf;
  214. iov.iov_len = 1;
  215. memset(&msg, 0, sizeof(msg));
  216. msg.msg_name = 0;
  217. msg.msg_namelen = 0;
  218. msg.msg_iov = &iov;
  219. msg.msg_iovlen = 1;
  220. /* old BSD implementations should use msg_accrights instead of
  221. * msg_control; the interface is different. */
  222. msg.msg_control = ccmsg;
  223. msg.msg_controllen = sizeof(ccmsg);
  224. while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR);
  225. if (rv == -1) {
  226. perror("recvmsg");
  227. return -1;
  228. }
  229. if(!rv) {
  230. /* EOF */
  231. return -1;
  232. }
  233. cmsg = CMSG_FIRSTHDR(&msg);
  234. if (cmsg->cmsg_type != SCM_RIGHTS) {
  235. fprintf(stderr, "got control message of unknown type %d\n",
  236. cmsg->cmsg_type);
  237. return -1;
  238. }
  239. return *(int*)CMSG_DATA(cmsg);
  240. }
  241. void fuse_kern_unmount(const char *mountpoint, int fd)
  242. {
  243. int res;
  244. int pid;
  245. if (!mountpoint)
  246. return;
  247. if (fd != -1) {
  248. struct pollfd pfd;
  249. pfd.fd = fd;
  250. pfd.events = 0;
  251. res = poll(&pfd, 1, 0);
  252. /* Need to close file descriptor, otherwise synchronous umount
  253. would recurse into filesystem, and deadlock.
  254. Caller expects fuse_kern_unmount to close the fd, so close it
  255. anyway. */
  256. close(fd);
  257. /* If file poll returns POLLERR on the device file descriptor,
  258. then the filesystem is already unmounted */
  259. if (res == 1 && (pfd.revents & POLLERR))
  260. return;
  261. }
  262. if (geteuid() == 0) {
  263. fuse_mnt_umount("fuse", mountpoint, mountpoint, 1);
  264. return;
  265. }
  266. res = umount2(mountpoint, 2);
  267. if (res == 0)
  268. return;
  269. pid = fork();
  270. if(pid == -1)
  271. return;
  272. if(pid == 0) {
  273. const char *argv[] = { FUSERMOUNT_PROG, "-u", "-q", "-z",
  274. "--", mountpoint, NULL };
  275. exec_fusermount(argv);
  276. _exit(1);
  277. }
  278. waitpid(pid, NULL, 0);
  279. }
  280. static int fuse_mount_fusermount(const char *mountpoint, struct mount_opts *mo,
  281. const char *opts, int quiet)
  282. {
  283. int fds[2], pid;
  284. int res;
  285. int rv;
  286. if (!mountpoint) {
  287. fprintf(stderr, "fuse: missing mountpoint parameter\n");
  288. return -1;
  289. }
  290. res = socketpair(PF_UNIX, SOCK_STREAM, 0, fds);
  291. if(res == -1) {
  292. perror("fuse: socketpair() failed");
  293. return -1;
  294. }
  295. pid = fork();
  296. if(pid == -1) {
  297. perror("fuse: fork() failed");
  298. close(fds[0]);
  299. close(fds[1]);
  300. return -1;
  301. }
  302. if(pid == 0) {
  303. char env[10];
  304. const char *argv[32];
  305. int a = 0;
  306. if (quiet) {
  307. int fd = open("/dev/null", O_RDONLY);
  308. if (fd != -1) {
  309. dup2(fd, 1);
  310. dup2(fd, 2);
  311. }
  312. }
  313. argv[a++] = FUSERMOUNT_PROG;
  314. if (opts) {
  315. argv[a++] = "-o";
  316. argv[a++] = opts;
  317. }
  318. argv[a++] = "--";
  319. argv[a++] = mountpoint;
  320. argv[a++] = NULL;
  321. close(fds[1]);
  322. fcntl(fds[0], F_SETFD, 0);
  323. snprintf(env, sizeof(env), "%i", fds[0]);
  324. setenv(FUSE_COMMFD_ENV, env, 1);
  325. exec_fusermount(argv);
  326. perror("fuse: failed to exec fusermount");
  327. _exit(1);
  328. }
  329. close(fds[0]);
  330. rv = receive_fd(fds[1]);
  331. if (!mo->auto_unmount) {
  332. /* with auto_unmount option fusermount will not exit until
  333. this socket is closed */
  334. close(fds[1]);
  335. waitpid(pid, NULL, 0); /* bury zombie */
  336. }
  337. return rv;
  338. }
  339. static int fuse_mount_sys(const char *mnt, struct mount_opts *mo,
  340. const char *mnt_opts)
  341. {
  342. char tmp[128];
  343. const char *devname = "/dev/fuse";
  344. char *source = NULL;
  345. char *type = NULL;
  346. struct stat stbuf;
  347. int fd;
  348. int res;
  349. if (!mnt) {
  350. fprintf(stderr, "fuse: missing mountpoint parameter\n");
  351. return -1;
  352. }
  353. res = stat(mnt, &stbuf);
  354. if (res == -1) {
  355. fprintf(stderr ,"fuse: failed to access mountpoint %s: %s\n",
  356. mnt, strerror(errno));
  357. return -1;
  358. }
  359. if (mo->auto_unmount) {
  360. /* Tell the caller to fallback to fusermount because
  361. auto-unmount does not work otherwise. */
  362. return -2;
  363. }
  364. fd = open(devname, O_RDWR);
  365. if (fd == -1) {
  366. if (errno == ENODEV || errno == ENOENT)
  367. fprintf(stderr, "fuse: device not found, try 'modprobe fuse' first\n");
  368. else
  369. fprintf(stderr, "fuse: failed to open %s: %s\n",
  370. devname, strerror(errno));
  371. return -1;
  372. }
  373. snprintf(tmp, sizeof(tmp), "fd=%i,rootmode=%o,user_id=%u,group_id=%u",
  374. fd, stbuf.st_mode & S_IFMT, getuid(), getgid());
  375. res = fuse_opt_add_opt(&mo->kernel_opts, tmp);
  376. if (res == -1)
  377. goto out_close;
  378. source = malloc((mo->fsname ? strlen(mo->fsname) : 0) +
  379. (mo->subtype ? strlen(mo->subtype) : 0) +
  380. strlen(devname) + 32);
  381. type = malloc((mo->subtype ? strlen(mo->subtype) : 0) + 32);
  382. if (!type || !source) {
  383. fprintf(stderr, "fuse: failed to allocate memory\n");
  384. goto out_close;
  385. }
  386. strcpy(type, mo->blkdev ? "fuseblk" : "fuse");
  387. if (mo->subtype) {
  388. strcat(type, ".");
  389. strcat(type, mo->subtype);
  390. }
  391. strcpy(source,
  392. mo->fsname ? mo->fsname : (mo->subtype ? mo->subtype : devname));
  393. res = mount(source, mnt, type, mo->flags, mo->kernel_opts);
  394. if (res == -1 && errno == ENODEV && mo->subtype) {
  395. /* Probably missing subtype support */
  396. strcpy(type, mo->blkdev ? "fuseblk" : "fuse");
  397. if (mo->fsname) {
  398. if (!mo->blkdev)
  399. sprintf(source, "%s#%s", mo->subtype,
  400. mo->fsname);
  401. } else {
  402. strcpy(source, type);
  403. }
  404. res = mount(source, mnt, type, mo->flags, mo->kernel_opts);
  405. }
  406. if (res == -1) {
  407. /*
  408. * Maybe kernel doesn't support unprivileged mounts, in this
  409. * case try falling back to fusermount
  410. */
  411. if (errno == EPERM) {
  412. res = -2;
  413. } else {
  414. int errno_save = errno;
  415. if (mo->blkdev && errno == ENODEV &&
  416. !fuse_mnt_check_fuseblk())
  417. fprintf(stderr,
  418. "fuse: 'fuseblk' support missing\n");
  419. else
  420. fprintf(stderr, "fuse: mount failed: %s\n",
  421. strerror(errno_save));
  422. }
  423. goto out_close;
  424. }
  425. #ifndef __NetBSD__
  426. #ifndef IGNORE_MTAB
  427. if (geteuid() == 0) {
  428. char *newmnt = fuse_mnt_resolve_path("fuse", mnt);
  429. res = -1;
  430. if (!newmnt)
  431. goto out_umount;
  432. res = fuse_mnt_add_mount("fuse", source, newmnt, type,
  433. mnt_opts);
  434. free(newmnt);
  435. if (res == -1)
  436. goto out_umount;
  437. }
  438. #endif /* IGNORE_MTAB */
  439. #endif /* __NetBSD__ */
  440. free(type);
  441. free(source);
  442. return fd;
  443. out_umount:
  444. umount2(mnt, 2); /* lazy umount */
  445. out_close:
  446. free(type);
  447. free(source);
  448. close(fd);
  449. return res;
  450. }
  451. static int get_mnt_flag_opts(char **mnt_optsp, int flags)
  452. {
  453. int i;
  454. if (!(flags & MS_RDONLY) && fuse_opt_add_opt(mnt_optsp, "rw") == -1)
  455. return -1;
  456. for (i = 0; mount_flags[i].opt != NULL; i++) {
  457. if (mount_flags[i].on && (flags & mount_flags[i].flag) &&
  458. fuse_opt_add_opt(mnt_optsp, mount_flags[i].opt) == -1)
  459. return -1;
  460. }
  461. return 0;
  462. }
  463. int fuse_kern_mount(const char *mountpoint, struct fuse_args *args)
  464. {
  465. struct mount_opts mo;
  466. int res = -1;
  467. char *mnt_opts = NULL;
  468. memset(&mo, 0, sizeof(mo));
  469. mo.flags = MS_NOSUID | MS_NODEV;
  470. if (args &&
  471. fuse_opt_parse(args, &mo, fuse_mount_opts, fuse_mount_opt_proc) == -1)
  472. return -1;
  473. fuse_opt_add_opt(&mo.kernel_opts,"allow_other");
  474. res = 0;
  475. if (mo.ishelp)
  476. goto out;
  477. res = -1;
  478. if (get_mnt_flag_opts(&mnt_opts, mo.flags) == -1)
  479. goto out;
  480. if (mo.kernel_opts && fuse_opt_add_opt(&mnt_opts, mo.kernel_opts) == -1)
  481. goto out;
  482. if (mo.mtab_opts && fuse_opt_add_opt(&mnt_opts, mo.mtab_opts) == -1)
  483. goto out;
  484. res = fuse_mount_sys(mountpoint, &mo, mnt_opts);
  485. if (res == -2) {
  486. if (mo.fusermount_opts &&
  487. fuse_opt_add_opt(&mnt_opts, mo.fusermount_opts) == -1)
  488. goto out;
  489. if (mo.subtype) {
  490. char *tmp_opts = NULL;
  491. res = -1;
  492. if (fuse_opt_add_opt(&tmp_opts, mnt_opts) == -1 ||
  493. fuse_opt_add_opt(&tmp_opts, mo.subtype_opt) == -1) {
  494. free(tmp_opts);
  495. goto out;
  496. }
  497. res = fuse_mount_fusermount(mountpoint, &mo, tmp_opts, 1);
  498. free(tmp_opts);
  499. if (res == -1)
  500. res = fuse_mount_fusermount(mountpoint, &mo,
  501. mnt_opts, 0);
  502. } else {
  503. res = fuse_mount_fusermount(mountpoint, &mo, mnt_opts, 0);
  504. }
  505. }
  506. out:
  507. free(mnt_opts);
  508. free(mo.fsname);
  509. free(mo.subtype);
  510. free(mo.fusermount_opts);
  511. free(mo.subtype_opt);
  512. free(mo.kernel_opts);
  513. free(mo.mtab_opts);
  514. return res;
  515. }