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.

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