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.

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