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.

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