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.

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