+2007-06-20 Miklos Szeredi <miklos@szeredi.hu>
+
+ * Add fs subtype support to libfuse and fusermount
+
2007-06-19 Miklos Szeredi <miklos@szeredi.hu>
* kernel: sync with mainline (2.6.22)
int fuse_parse_cmdline(struct fuse_args *args, char **mountpoint,
int *multithreaded, int *foreground);
-
+/**
+ * Go into the background
+ *
+ * @param foreground if true, stay in the foreground
+ * @return 0 on success, -1 on failure
+ */
int fuse_daemonize(int foreground);
+/**
+ * Get the version of the library
+ *
+ * @return the version
+ */
+int fuse_version(void);
+
/* ----------------------------------------------------------- *
* Signal handling *
* ----------------------------------------------------------- */
fuse_fs_utimens;
fuse_fs_write;
fuse_register_module;
+ fuse_version;
local:
*;
struct helper_opts {
int singlethread;
int foreground;
- int fsname;
+ int nodefault_subtype;
char *mountpoint;
};
FUSE_HELPER_OPT("debug", foreground),
FUSE_HELPER_OPT("-f", foreground),
FUSE_HELPER_OPT("-s", singlethread),
- FUSE_HELPER_OPT("fsname=", fsname),
+ FUSE_HELPER_OPT("fsname=", nodefault_subtype),
+ FUSE_HELPER_OPT("subtype=", nodefault_subtype),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
FUSE_OPT_KEY("fsname=", FUSE_OPT_KEY_KEEP),
+ FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP),
FUSE_OPT_END
};
}
}
-static int add_default_fsname(const char *progname, struct fuse_args *args)
+static int add_default_subtype(const char *progname, struct fuse_args *args)
{
int res;
- char *fsname_opt;
+ char *subtype_opt;
const char *basename = strrchr(progname, '/');
if (basename == NULL)
basename = progname;
else if (basename[1] != '\0')
basename++;
- fsname_opt = (char *) malloc(strlen(basename) + 64);
- if (fsname_opt == NULL) {
+ subtype_opt = (char *) malloc(strlen(basename) + 64);
+ if (subtype_opt == NULL) {
fprintf(stderr, "fuse: memory allocation failed\n");
return -1;
}
- sprintf(fsname_opt, "-ofsname=%s", basename);
- res = fuse_opt_add_arg(args, fsname_opt);
- free(fsname_opt);
+ sprintf(subtype_opt, "-osubtype=%s", basename);
+ res = fuse_opt_add_arg(args, subtype_opt);
+ free(subtype_opt);
return res;
}
if (res == -1)
return -1;
- if (!hopts.fsname) {
- res = add_default_fsname(args->argv[0], args);
+ if (!hopts.nodefault_subtype) {
+ res = add_default_subtype(args->argv[0], args);
if (res == -1)
goto err;
}
return -1;
}
+int fuse_version(void)
+{
+ return FUSE_VERSION;
+}
+
#include "fuse_compat.h"
#ifndef __FreeBSD__
KEY_KERN_FLAG,
KEY_KERN_OPT,
KEY_FUSERMOUNT_OPT,
+ KEY_SUBTYPE_OPT,
KEY_MTAB_OPT,
KEY_ALLOW_ROOT,
KEY_RO,
int nonempty;
int blkdev;
char *fsname;
+ char *subtype;
+ char *subtype_opt;
char *mtab_opts;
char *fusermount_opts;
char *kernel_opts;
FUSE_MOUNT_OPT("nonempty", nonempty),
FUSE_MOUNT_OPT("blkdev", blkdev),
FUSE_MOUNT_OPT("fsname=%s", fsname),
+ FUSE_MOUNT_OPT("subtype=%s", subtype),
FUSE_OPT_KEY("allow_other", KEY_KERN_OPT),
FUSE_OPT_KEY("allow_root", KEY_ALLOW_ROOT),
FUSE_OPT_KEY("nonempty", KEY_FUSERMOUNT_OPT),
FUSE_OPT_KEY("blkdev", KEY_FUSERMOUNT_OPT),
FUSE_OPT_KEY("fsname=", KEY_FUSERMOUNT_OPT),
+ FUSE_OPT_KEY("subtype=", KEY_SUBTYPE_OPT),
FUSE_OPT_KEY("large_read", KEY_KERN_OPT),
FUSE_OPT_KEY("blksize=", KEY_KERN_OPT),
FUSE_OPT_KEY("default_permissions", KEY_KERN_OPT),
" -o nonempty allow mounts over non-empty file/dir\n"
" -o default_permissions enable permission checking by kernel\n"
" -o fsname=NAME set filesystem name\n"
+ " -o subtype=NAME set filesystem type\n"
" -o large_read issue large read requests (2.4 only)\n"
" -o max_read=N set maximum size of read requests\n"
"\n"
case KEY_FUSERMOUNT_OPT:
return fuse_opt_add_opt(&mo->fusermount_opts, arg);
+ case KEY_SUBTYPE_OPT:
+ return fuse_opt_add_opt(&mo->subtype_opt, arg);
+
case KEY_MTAB_OPT:
return fuse_opt_add_opt(&mo->mtab_opts, arg);
fuse_kern_unmount(mountpoint, -1);
}
-int fuse_mount_compat22(const char *mountpoint, const char *opts)
+static int fuse_mount_fusermount(const char *mountpoint, const char *opts,
+ int quiet)
{
int fds[2], pid;
int res;
const char *argv[32];
int a = 0;
+ if (quiet) {
+ int fd = open("/dev/null", O_RDONLY);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ }
+
argv[a++] = FUSERMOUNT_PROG;
if (opts) {
argv[a++] = "-o";
return rv;
}
+int fuse_mount_compat22(const char *mountpoint, const char *opts)
+{
+ return fuse_mount_fusermount(mountpoint, opts, 0);
+}
static int fuse_mount_sys(const char *mnt, struct mount_opts *mo,
const char *mnt_opts)
{
- const char *type = mo->blkdev ? "fuseblk" : "fuse";
char tmp[128];
const char *devname = "/dev/fuse";
+ char *source = NULL;
+ char *type = NULL;
struct stat stbuf;
int fd;
int res;
return -1;
}
- if (mo->fsname)
- devname = mo->fsname;
-
snprintf(tmp, sizeof(tmp), "fd=%i,rootmode=%o,user_id=%i,group_id=%i", fd,
stbuf.st_mode & S_IFMT, getuid(), getgid());
if (res == -1)
goto out_close;
- res = mount(devname, mnt, type, mo->flags, mo->kernel_opts);
+ source = malloc((mo->fsname ? strlen(mo->fsname) : 0) +
+ (mo->subtype ? strlen(mo->subtype) : 0) +
+ strlen(devname) + 32);
+
+ type = malloc((mo->subtype ? strlen(mo->subtype) : 0) + 32);
+ if (!type || !source) {
+ fprintf(stderr, "fuse: failed to allocate memory\n");
+ goto out_close;
+ }
+
+ strcpy(type, mo->blkdev ? "fuseblk" : "fuse");
+ if (mo->subtype) {
+ strcat(type, ".");
+ strcat(type, mo->subtype);
+ }
+ strcpy(source,
+ mo->fsname ? mo->fsname : (mo->subtype ? mo->subtype : devname));
+
+ res = mount(source, mnt, type, mo->flags, mo->kernel_opts);
+ if (res == -1 && errno == ENODEV && mo->subtype) {
+ /* Probably missing subtype support */
+ strcpy(type, mo->blkdev ? "fuseblk" : "fuse");
+ if (mo->fsname) {
+ if (!mo->blkdev)
+ sprintf(source, "%s#%s", mo->subtype, mo->fsname);
+ } else {
+ strcpy(source, type);
+ }
+ res = mount(source, mnt, type, mo->flags, mo->kernel_opts);
+ }
if (res == -1) {
/*
* Maybe kernel doesn't support unprivileged mounts, in this
* case try falling back to fusermount
*/
- if (errno == EPERM)
+ if (errno == EPERM) {
res = -2;
- else
- perror("fuse: mount failed");
+ } else {
+ int errno_save = errno;
+ if (mo->blkdev && errno == ENODEV && !fuse_mnt_check_fuseblk())
+ fprintf(stderr, "fuse: 'fuseblk' support missing\n");
+ else
+ fprintf(stderr, "fuse: mount failed: %s\n",
+ strerror(errno_save));
+ }
+
goto out_close;
}
if (!newmnt)
goto out_umount;
- res = fuse_mnt_add_mount("fuse", devname, newmnt, type, mnt_opts);
+ res = fuse_mnt_add_mount("fuse", source, newmnt, type, mnt_opts);
free(newmnt);
if (res == -1)
goto out_umount;
out_umount:
umount2(mnt, 2); /* lazy umount */
out_close:
+ free(type);
+ free(source);
close(fd);
return res;
}
int res = -1;
char *mnt_opts = NULL;
+ if (!mountpoint) {
+ fprintf(stderr, "fuse: missing mountpoint\n");
+ return -1;
+ }
+
memset(&mo, 0, sizeof(mo));
mo.flags = MS_NOSUID | MS_NODEV;
fuse_opt_add_opt(&mnt_opts, mo.fusermount_opts) == -1)
goto out;
- res = fuse_mount_compat22(mountpoint, mnt_opts);
+ if (mo.subtype) {
+ char *tmp_opts = NULL;
+
+ res = -1;
+ if (fuse_opt_add_opt(&tmp_opts, mnt_opts) == -1 ||
+ fuse_opt_add_opt(&tmp_opts, mo.subtype_opt) == -1) {
+ free(tmp_opts);
+ goto out;
+ }
+
+ res = fuse_mount_fusermount(mountpoint, tmp_opts, 1);
+ free(tmp_opts);
+ if (res == -1)
+ res = fuse_mount_fusermount(mountpoint, mnt_opts, 0);
+ } else {
+ res = fuse_mount_fusermount(mountpoint, mnt_opts, 0);
+ }
}
out:
free(mnt_opts);
free(mo.fsname);
+ free(mo.subtype);
free(mo.fusermount_opts);
+ free(mo.subtype_opt);
free(mo.kernel_opts);
free(mo.mtab_opts);
return res;
}
return 0;
}
+
+int fuse_mnt_check_fuseblk(void)
+{
+ char buf[256];
+ FILE *f = fopen("/proc/filesystems", "r");
+ if (!f)
+ return 1;
+
+ while (fgets(buf, sizeof(buf), f))
+ if (strstr(buf, "fuseblk\n")) {
+ fclose(f);
+ return 1;
+ }
+
+ fclose(f);
+ return 0;
+}
char *fuse_mnt_resolve_path(const char *progname, const char *orig);
int fuse_mnt_check_empty(const char *progname, const char *mnt,
mode_t rootmode, off_t rootsize);
+int fuse_mnt_check_fuseblk(void);
}
#ifndef IGNORE_MTAB
-static int add_mount(const char *fsname, const char *mnt, const char *type,
+static int add_mount(const char *source, const char *mnt, const char *type,
const char *opts)
{
- return fuse_mnt_add_mount(progname, fsname, mnt, type, opts);
+ return fuse_mnt_add_mount(progname, source, mnt, type, opts);
}
static int unmount_fuse(const char *mnt, int quiet, int lazy)
while ((entp = getmntent(fp)) != NULL) {
if (!found && strcmp(entp->mnt_dir, mnt) == 0 &&
(strcmp(entp->mnt_type, "fuse") == 0 ||
- strcmp(entp->mnt_type, "fuseblk") == 0)) {
+ strcmp(entp->mnt_type, "fuseblk") == 0 ||
+ strncmp(entp->mnt_type, "fuse.", 5) == 0 ||
+ strncmp(entp->mnt_type, "fuseblk.", 8) == 0)) {
char *p = strstr(entp->mnt_opts, "user=");
if (p && (p == entp->mnt_opts || *(p-1) == ',') &&
strcmp(p + 5, user) == 0) {
return -1;
}
while ((entp = getmntent(fp)) != NULL) {
- if (strcmp(entp->mnt_type, "fuse") == 0)
+ if (strcmp(entp->mnt_type, "fuse") == 0 ||
+ strncmp(entp->mnt_type, "fuse.", 5) == 0)
count ++;
}
endmntent(fp);
return 0;
}
-static int add_mount(const char *fsname, const char *mnt, const char *type,
+static int add_mount(const char *source, const char *mnt, const char *type,
const char *opts)
{
- (void) fsname;
+ (void) source;
(void) mnt;
(void) type;
(void) opts;
return 0;
}
-static int has_fuseblk(void)
+static int get_string_opt(const char *s, unsigned len, const char *opt,
+ char **val)
{
- char buf[256];
- FILE *f = fopen("/proc/filesystems", "r");
- if (!f)
- return 1;
+ unsigned opt_len = strlen(opt);
- while (fgets(buf, sizeof(buf), f))
- if (strstr(buf, "fuseblk\n")) {
- fclose(f);
- return 1;
- }
+ if (*val)
+ free(*val);
+ *val = (char *) malloc(len - opt_len + 1);
+ if (!*val) {
+ fprintf(stderr, "%s: failed to allocate memory\n", progname);
+ return 0;
+ }
- fclose(f);
- return 0;
+ memcpy(*val, s + opt_len, len - opt_len);
+ (*val)[len - opt_len] = '\0';
+ return 1;
}
-static int do_mount(const char *mnt, const char **type, mode_t rootmode,
- int fd, const char *opts, const char *dev, char **fsnamep,
+static int do_mount(const char *mnt, char **typep, mode_t rootmode,
+ int fd, const char *opts, const char *dev, char **sourcep,
char **mnt_optsp, off_t rootsize)
{
int res;
const char *s;
char *d;
char *fsname = NULL;
+ char *subtype = NULL;
+ char *source = NULL;
+ char *type = NULL;
int check_empty = 1;
int blkdev = 0;
for (s = opts, d = optbuf; *s;) {
unsigned len;
const char *fsname_str = "fsname=";
+ const char *subtype_str = "subtype=";
for (len = 0; s[len] && s[len] != ','; len++);
if (begins_with(s, fsname_str)) {
- unsigned fsname_str_len = strlen(fsname_str);
- if (fsname)
- free(fsname);
- fsname = (char *) malloc(len - fsname_str_len + 1);
- if (!fsname) {
- fprintf(stderr, "%s: failed to allocate memory\n", progname);
+ if (!get_string_opt(s, len, fsname_str, &fsname))
+ goto err;
+ } else if (begins_with(s, subtype_str)) {
+ if (!get_string_opt(s, len, subtype_str, &subtype))
goto err;
- }
- memcpy(fsname, s + fsname_str_len, len - fsname_str_len);
- fsname[len - fsname_str_len] = '\0';
} else if (opt_eq(s, len, "blkdev")) {
if (getuid() != 0) {
fprintf(stderr, "%s: option blkdev is privileged\n", progname);
sprintf(d, "fd=%i,rootmode=%o,user_id=%i,group_id=%i",
fd, rootmode, getuid(), getgid());
- if (fsname == NULL) {
- fsname = strdup(dev);
- if (!fsname) {
- fprintf(stderr, "%s: failed to allocate memory\n", progname);
- goto err;
- }
- }
if (check_empty &&
fuse_mnt_check_empty(progname, mnt, rootmode, rootsize) == -1)
goto err;
- if (blkdev)
- *type = "fuseblk";
- res = mount(fsname, mnt, *type, flags, optbuf);
+ source = malloc((fsname ? strlen(fsname) : 0) +
+ (subtype ? strlen(subtype) : 0) + strlen(dev) + 32);
+
+ type = malloc((subtype ? strlen(subtype) : 0) + 32);
+ if (!type || !source) {
+ fprintf(stderr, "%s: failed to allocate memory\n", progname);
+ goto err;
+ }
+
+ if (subtype)
+ sprintf(type, "%s.%s", blkdev ? "fuseblk" : "fuse", subtype);
+ else
+ strcpy(type, blkdev ? "fuseblk" : "fuse");
+
+ if (fsname)
+ strcpy(source, fsname);
+ else
+ strcpy(source, subtype ? subtype : dev);
+
+ res = mount(source, mnt, type, flags, optbuf);
+ if (res == -1 && errno == ENODEV && subtype) {
+ /* Probably missing subtype support */
+ strcpy(type, blkdev ? "fuseblk" : "fuse");
+ if (fsname) {
+ if (!blkdev)
+ sprintf(source, "%s#%s", subtype, fsname);
+ } else {
+ strcpy(source, type);
+ }
+
+ res = mount(source, mnt, type, flags, optbuf);
+ }
if (res == -1 && errno == EINVAL) {
/* It could be an old version not supporting group_id */
sprintf(d, "fd=%i,rootmode=%o,user_id=%i", fd, rootmode, getuid());
- res = mount(fsname, mnt, *type, flags, optbuf);
+ res = mount(source, mnt, type, flags, optbuf);
}
if (res == -1) {
int errno_save = errno;
- if (blkdev && errno == ENODEV && !has_fuseblk())
- fprintf(stderr, "%s: 'fuseblk' support missing; try the kernel module from fuse-2.6.0 or later\n", progname);
+ if (blkdev && errno == ENODEV && !fuse_mnt_check_fuseblk())
+ fprintf(stderr, "%s: 'fuseblk' support missing\n", progname);
else
fprintf(stderr, "%s: mount failed: %s\n", progname, strerror(errno_save));
goto err;
} else {
- *fsnamep = fsname;
+ *sourcep = source;
+ *typep = type;
*mnt_optsp = mnt_opts;
}
free(optbuf);
err:
free(fsname);
+ free(subtype);
+ free(source);
+ free(type);
free(mnt_opts);
free(optbuf);
return -1;
int res;
int fd;
char *dev;
- const char *type = "fuse";
struct stat stbuf;
- char *fsname = NULL;
+ char *type = NULL;
+ char *source = NULL;
char *mnt_opts = NULL;
const char *real_mnt = mnt;
int currdir_fd = -1;
restore_privs();
if (res != -1)
res = do_mount(real_mnt, &type, stbuf.st_mode & S_IFMT, fd, opts,
- dev, &fsname, &mnt_opts, stbuf.st_size);
+ dev, &source, &mnt_opts, stbuf.st_size);
} else
restore_privs();
}
if (geteuid() == 0) {
- res = add_mount(fsname, mnt, type, mnt_opts);
+ res = add_mount(source, mnt, type, mnt_opts);
if (res == -1) {
umount2(mnt, 2); /* lazy umount */
close(fd);
}
}
- free(fsname);
+ free(source);
+ free(type);
free(mnt_opts);
free(dev);