public inbox for linux-fsdevel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <djwong@kernel.org>
To: Bernd Schubert <bernd@bsbernd.com>
Cc: linux-fsdevel@vger.kernel.org, Miklos Szeredi <miklos@szeredi.hu>,
	Joanne Koong <joannelkoong@gmail.com>,
	Bernd Schubert <bschubert@ddn.com>
Subject: Re: [PATCH 08/19] Refactor mount code / move common functions to mount_util.c
Date: Mon, 23 Mar 2026 15:40:52 -0700	[thread overview]
Message-ID: <20260323224052.GJ6202@frogsfrogsfrogs> (raw)
In-Reply-To: <20260323-fuse-init-before-mount-v1-8-a52d3040af69@bsbernd.com>

On Mon, Mar 23, 2026 at 06:45:03PM +0100, Bernd Schubert wrote:
> From: Bernd Schubert <bschubert@ddn.com>
> 
> Also create the new "mount_i.h", which is independent of the
> the rest of libfuse.
> 
> This is preparation for the new mount API, which goes into a new file.
> 
> Signed-off-by: Bernd Schubert <bschubert@ddn.com>

Hey, I want to use some of these new helpers too! :)

Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>

Can we use asprintf for fuse_mnt_build_{source,type} though?

--D

> ---
>  lib/fuse_i.h         | 10 ++----
>  lib/mount.c          | 96 ++++++++++++++++++++++++++++------------------------
>  lib/mount_bsd.c      |  1 +
>  lib/mount_common_i.h | 31 +++++++++++++++++
>  lib/mount_i_linux.h  | 32 ++++++++++++++++++
>  lib/mount_util.c     | 24 +++++++++++++
>  lib/mount_util.h     |  6 ++++
>  7 files changed, 148 insertions(+), 52 deletions(-)
> 
> diff --git a/lib/fuse_i.h b/lib/fuse_i.h
> index b4c1d3eef41010287f6c9555ec0b2442d904d192..6d63c9fd2149eb4ae3b0e0170640a4ce2eed4705 100644
> --- a/lib/fuse_i.h
> +++ b/lib/fuse_i.h
> @@ -19,14 +19,15 @@
>  #include <stdbool.h>
>  #include <stdatomic.h>
>  
> +#ifndef MIN
>  #define MIN(a, b) \
>  ({									\
>  	typeof(a) _a = (a);						\
>  	typeof(b) _b = (b);						\
>  	_a < _b ? _a : _b;						\
>  })
> +#endif
>  
> -struct mount_opts;
>  struct fuse_ring_pool;
>  
>  struct fuse_req {
> @@ -217,13 +218,8 @@ struct fuse_chan *fuse_chan_get(struct fuse_chan *ch);
>   */
>  void fuse_chan_put(struct fuse_chan *ch);
>  
> -/* Special return value for mount functions to indicate fallback to fusermount3 is needed */
> -#define FUSE_MOUNT_FALLBACK_NEEDED -2
> -
> -struct mount_opts *parse_mount_opts(struct fuse_args *args);
> -void destroy_mount_opts(struct mount_opts *mo);
> +/* Mount-related functions */
>  void fuse_mount_version(void);
> -unsigned get_max_read(struct mount_opts *o);
>  void fuse_kern_unmount(const char *mountpoint, int fd);
>  int fuse_kern_mount(const char *mountpoint, struct mount_opts *mo);
>  
> diff --git a/lib/mount.c b/lib/mount.c
> index dec9d52274c13536648cacef959789f472c5682c..fe353e2cc4579adb47473cac5db7d1bae2defb2c 100644
> --- a/lib/mount.c
> +++ b/lib/mount.c
> @@ -16,6 +16,7 @@
>  #include "fuse_misc.h"
>  #include "fuse_opt.h"
>  #include "mount_util.h"
> +#include "mount_i_linux.h"
>  
>  #include <stdio.h>
>  #include <stdlib.h>
> @@ -49,7 +50,6 @@
>  #define FUSERMOUNT_PROG		"fusermount3"
>  #define FUSE_COMMFD_ENV		"_FUSE_COMMFD"
>  #define FUSE_COMMFD2_ENV	"_FUSE_COMMFD2"
> -#define FUSE_KERN_DEVICE_ENV	"FUSE_KERN_DEVICE"
>  
>  #ifndef MS_DIRSYNC
>  #define MS_DIRSYNC 128
> @@ -65,20 +65,6 @@ enum {
>  	KEY_RO,
>  };
>  
> -struct mount_opts {
> -	int allow_other;
> -	int flags;
> -	int auto_unmount;
> -	int blkdev;
> -	char *fsname;
> -	char *subtype;
> -	char *subtype_opt;
> -	char *mtab_opts;
> -	char *fusermount_opts;
> -	char *kernel_opts;
> -	unsigned max_read;
> -};
> -
>  #define FUSE_MOUNT_OPT(t, p) { t, offsetof(struct mount_opts, p), 1 }
>  
>  static const struct fuse_opt fuse_mount_opts[] = {
> @@ -197,7 +183,7 @@ static const struct mount_flags mount_flags[] = {
>  	{NULL,	    0,		    0}
>  };
>  
> -unsigned get_max_read(struct mount_opts *o)
> +unsigned int get_max_read(struct mount_opts *o)
>  {
>  	return o->max_read;
>  }
> @@ -510,7 +496,7 @@ static int fuse_kern_mount_prepare(const char *mnt,
>  				   struct mount_opts *mo)
>  {
>  	char tmp[128];
> -	const char *devname = getenv(FUSE_KERN_DEVICE_ENV) ?: "/dev/fuse";
> +	const char *devname = fuse_mnt_get_devname();
>  	struct stat stbuf;
>  	int fd;
>  	int res;
> @@ -563,35 +549,24 @@ out_close:
>   * @mo: mount options
>   * @mnt_opts: mount options to pass to the kernel
>   *
> - * Returns: 0 on success, -1 on failure, FUSE_MOUNT_FALLBACK_NEEDED if fusermount should be used
> + * Returns: 0 on success, -1 on failure,
> + *          FUSE_MOUNT_FALLBACK_NEEDED if fusermount should be used
>   */
>  static int fuse_kern_do_mount(const char *mnt, struct mount_opts *mo,
>  				  const char *mnt_opts)
>  {
> -	const char *devname = getenv(FUSE_KERN_DEVICE_ENV) ?: "/dev/fuse";
>  	char *source = NULL;
>  	char *type = NULL;
>  	int res;
>  
>  	res = -ENOMEM;
> -	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);
> +	source = fuse_mnt_build_source(mo);
> +	type = fuse_mnt_build_type(mo);
>  	if (!type || !source) {
>  		fuse_log(FUSE_LOG_ERR, "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 */
> @@ -627,20 +602,10 @@ static int fuse_kern_do_mount(const char *mnt, struct mount_opts *mo,
>  		goto out_close;
>  	}
>  
> -#ifndef IGNORE_MTAB
> -	if (geteuid() == 0) {
> -		char *newmnt = fuse_mnt_resolve_path("fuse", mnt);
> -		res = -1;
> -		if (!newmnt)
> -			goto out_umount;
> +	res = fuse_mnt_add_mount_helper(mnt, source, type, mnt_opts);
> +	if (res == -1)
> +		goto out_umount;
>  
> -		res = fuse_mnt_add_mount("fuse", source, newmnt, type,
> -					 mnt_opts);
> -		free(newmnt);
> -		if (res == -1)
> -			goto out_umount;
> -	}
> -#endif /* IGNORE_MTAB */
>  	free(type);
>  	free(source);
>  
> @@ -777,3 +742,44 @@ out:
>  	free(mnt_opts);
>  	return res;
>  }
> +
> +const char *fuse_mnt_get_devname(void)
> +{
> +	const char *devname = getenv(FUSE_KERN_DEVICE_ENV);
> +
> +	return devname ? devname : "/dev/fuse";
> +}
> +
> +char *fuse_mnt_build_source(const struct mount_opts *mo)
> +{
> +	const char *devname = fuse_mnt_get_devname();
> +	char *source;
> +
> +	source = malloc((mo->fsname ? strlen(mo->fsname) : 0) +
> +			(mo->subtype ? strlen(mo->subtype) : 0) +
> +			strlen(devname) + 32);
> +	if (!source)
> +		return NULL;
> +
> +	strcpy(source,
> +	       mo->fsname ? mo->fsname : (mo->subtype ? mo->subtype : devname));
> +
> +	return source;
> +}
> +
> +char *fuse_mnt_build_type(const struct mount_opts *mo)
> +{
> +	char *type;
> +
> +	type = malloc((mo->subtype ? strlen(mo->subtype) : 0) + 32);
> +	if (!type)
> +		return NULL;
> +
> +	strcpy(type, mo->blkdev ? "fuseblk" : "fuse");
> +	if (mo->subtype) {
> +		strcat(type, ".");
> +		strcat(type, mo->subtype);
> +	}
> +
> +	return type;
> +}
> diff --git a/lib/mount_bsd.c b/lib/mount_bsd.c
> index c12ab322e7dc86d5c8d2f2abc6b71488e82523cf..c5b831160f826c0e4620626a72c4b93427d18bab 100644
> --- a/lib/mount_bsd.c
> +++ b/lib/mount_bsd.c
> @@ -10,6 +10,7 @@
>  
>  #include "fuse_config.h"
>  #include "fuse_i.h"
> +#include "mount_common_i.h"
>  #include "fuse_misc.h"
>  #include "fuse_opt.h"
>  #include "util.h"
> diff --git a/lib/mount_common_i.h b/lib/mount_common_i.h
> new file mode 100644
> index 0000000000000000000000000000000000000000..d27d2aa624ae3806c61a0fe382c2d024080c9bb3
> --- /dev/null
> +++ b/lib/mount_common_i.h
> @@ -0,0 +1,31 @@
> +/*
> + *  FUSE: Filesystem in Userspace
> + *  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
> + *                2026 Bernd Schubert <bernd@bsbernd.com>
> + *
> + *  This program can be distributed under the terms of the GNU LGPLv2.
> + *  See the file LGPL2.txt
> + */
> +
> +#ifndef FUSE_MOUNT_COMMON_I_H_
> +#define FUSE_MOUNT_COMMON_I_H_
> +
> +/* Forward declaration for fuse_args */
> +struct fuse_args;
> +struct mount_opts;
> +
> +/* Special return value for mount functions to indicate fallback to fusermount3 is needed */
> +#define FUSE_MOUNT_FALLBACK_NEEDED -2
> +
> +/* Environment variable for FUSE kernel device */
> +#define FUSE_KERN_DEVICE_ENV "FUSE_KERN_DEVICE"
> +
> +/* Mount options management functions */
> +struct mount_opts *parse_mount_opts(struct fuse_args *args);
> +void destroy_mount_opts(struct mount_opts *mo);
> +unsigned int get_max_read(struct mount_opts *o);
> +char *fuse_mnt_build_source(const struct mount_opts *mo);
> +char *fuse_mnt_build_type(const struct mount_opts *mo);
> +
> +
> +#endif /* FUSE_MOUNT_COMMON_I_H_ */
> diff --git a/lib/mount_i_linux.h b/lib/mount_i_linux.h
> new file mode 100644
> index 0000000000000000000000000000000000000000..abcd1b08012feedef6b4c8961b55ac847a27496a
> --- /dev/null
> +++ b/lib/mount_i_linux.h
> @@ -0,0 +1,32 @@
> +/*
> + *  FUSE: Filesystem in Userspace
> + *  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
> + *                2026 Bernd Schubert <bernd@bsbernd.com>
> + *
> + *  This program can be distributed under the terms of the GNU LGPLv2.
> + *  See the file LGPL2.txt
> + */
> +
> +#ifndef FUSE_MOUNT_I_H_
> +#define FUSE_MOUNT_I_H_
> +
> +/* Forward declaration for fuse_args */
> +struct fuse_args;
> +
> +/* Mount options structure */
> +struct mount_opts {
> +	int allow_other;
> +	int flags;
> +	int auto_unmount;
> +	int blkdev;
> +	char *fsname;
> +	char *subtype;
> +	char *subtype_opt;
> +	char *mtab_opts;
> +	char *fusermount_opts;
> +	char *kernel_opts;
> +	unsigned int max_read;
> +};
> +
> +
> +#endif /* FUSE_MOUNT_I_H_ */
> diff --git a/lib/mount_util.c b/lib/mount_util.c
> index 8c0cdf72d978da68c95125964416b92668104924..a42a02a0b92f98778abb2c491cdc7a01260f56ee 100644
> --- a/lib/mount_util.c
> +++ b/lib/mount_util.c
> @@ -375,3 +375,27 @@ int fuse_mnt_parse_fuse_fd(const char *mountpoint)
>  
>  	return -1;
>  }
> +
> +int fuse_mnt_add_mount_helper(const char *mnt, const char *source,
> +			       const char *type, const char *mnt_opts)
> +{
> +#ifndef IGNORE_MTAB
> +	if (geteuid() == 0) {
> +		char *newmnt = fuse_mnt_resolve_path("fuse", mnt);
> +		int res;
> +
> +		if (!newmnt)
> +			return -1;
> +
> +		res = fuse_mnt_add_mount("fuse", source, newmnt, type,
> +					 mnt_opts);
> +		free(newmnt);
> +		return res;
> +	}
> +#endif
> +	(void)mnt;
> +	(void)source;
> +	(void)type;
> +	(void)mnt_opts;
> +	return 0;
> +}
> diff --git a/lib/mount_util.h b/lib/mount_util.h
> index 9cb9077dd177381d712e34e7118bf73572142c6a..4688d00091f001b3ccd36b1ef3b7e799031ed773 100644
> --- a/lib/mount_util.h
> +++ b/lib/mount_util.h
> @@ -7,6 +7,7 @@
>  */
>  
>  #include <sys/types.h>
> +#include "mount_common_i.h" // IWYU pragma: keep
>  
>  int fuse_mnt_add_mount(const char *progname, const char *fsname,
>  		       const char *mnt, const char *type, const char *opts);
> @@ -16,3 +17,8 @@ int fuse_mnt_umount(const char *progname, const char *abs_mnt,
>  char *fuse_mnt_resolve_path(const char *progname, const char *orig);
>  int fuse_mnt_check_fuseblk(void);
>  int fuse_mnt_parse_fuse_fd(const char *mountpoint);
> +
> +/* Helper functions for mount operations */
> +const char *fuse_mnt_get_devname(void);
> +int fuse_mnt_add_mount_helper(const char *mnt, const char *source,
> +			       const char *type, const char *mnt_opts);
> 
> -- 
> 2.43.0
> 
> 

  reply	other threads:[~2026-03-23 22:40 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-23 17:44 [PATCH 00/19] libfuse: Add support for synchronous init Bernd Schubert
2026-03-23 17:44 ` [PATCH 01/19] ci-build: Add environment logging Bernd Schubert
2026-03-23 17:44 ` [PATCH 02/19] Add 'STRCPY' to the checkpatch ignore option Bernd Schubert
2026-03-23 21:03   ` Darrick J. Wong
2026-03-23 17:44 ` [PATCH 03/19] checkpatch.pl: Add _Atomic to $Attribute patttern Bernd Schubert
2026-03-23 21:09   ` Darrick J. Wong
2026-03-23 17:44 ` [PATCH 04/19] Add a new daemonize API Bernd Schubert
2026-03-23 22:28   ` Darrick J. Wong
2026-03-24 17:36     ` Bernd Schubert
2026-03-24 22:20       ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 05/19] Sync fuse_kernel.h with linux-6.18 Bernd Schubert
2026-03-23 21:16   ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 06/19] mount.c: Split fuse_mount_sys to prepare privileged sync FUSE_INIT Bernd Schubert
2026-03-23 22:34   ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 07/19] Add FUSE_MOUNT_FALLBACK_NEEDED define for -2 mount errors Bernd Schubert
2026-03-23 22:36   ` Darrick J. Wong
2026-03-24 18:03     ` Bernd Schubert
2026-03-23 17:45 ` [PATCH 08/19] Refactor mount code / move common functions to mount_util.c Bernd Schubert
2026-03-23 22:40   ` Darrick J. Wong [this message]
2026-03-23 17:45 ` [PATCH 09/19] Move mount flags to mount_i.h Bernd Schubert
2026-03-23 22:45   ` Darrick J. Wong
2026-03-24 18:40     ` Bernd Schubert
2026-03-23 17:45 ` [PATCH 10/19] conftest.py: Add more valgrind filter patterns Bernd Schubert
2026-03-23 17:45 ` [PATCH 11/19] Add support for the new linux mount API Bernd Schubert
2026-03-23 23:42   ` Darrick J. Wong
2026-03-24 20:16     ` Bernd Schubert
2026-03-24 22:46       ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 12/19] fuse mount: Support synchronous FUSE_INIT (privileged daemon) Bernd Schubert
2026-03-24  0:03   ` Darrick J. Wong
2026-03-24 20:42     ` Bernd Schubert
2026-03-24 22:50       ` Darrick J. Wong
2026-03-25  7:52         ` Bernd Schubert
2026-03-25 16:42           ` Darrick J. Wong
2026-03-26 19:32         ` Bernd Schubert
2026-03-26 22:33           ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 13/19] Add fuse_session_set_debug() to enable debug output without foreground Bernd Schubert
2026-03-24  0:04   ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 14/19] Move more generic mount code to mount_util.{c,h} Bernd Schubert
2026-03-24  0:06   ` Darrick J. Wong
2026-03-24 20:57     ` Bernd Schubert
2026-03-23 17:45 ` [PATCH 15/19] Split the fusermount do_mount function Bernd Schubert
2026-03-24  0:14   ` Darrick J. Wong
2026-03-24 21:05     ` Bernd Schubert
2026-03-24 22:53       ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 16/19] fusermount: Refactor extract_x_options Bernd Schubert
2026-03-24  0:18   ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 17/19] Make fusermount work bidirectional for sync init Bernd Schubert
2026-03-24 19:35   ` Darrick J. Wong
2026-03-24 21:24     ` Bernd Schubert
2026-03-24 22:59       ` Darrick J. Wong
2026-03-25 19:48         ` Bernd Schubert
2026-03-25 22:03           ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 18/19] New mount API: Filter out "user=" Bernd Schubert
2026-03-24 19:51   ` Darrick J. Wong
2026-03-24 20:01     ` Bernd Schubert
2026-03-24 23:02       ` Darrick J. Wong
2026-03-23 17:45 ` [PATCH 19/19] Add support for sync-init of unprivileged daemons Bernd Schubert
2026-03-24 20:21   ` Darrick J. Wong
2026-03-24 21:53     ` Bernd Schubert
2026-03-24 23:13       ` Darrick J. Wong
2026-03-24  0:19 ` [PATCH 00/19] libfuse: Add support for synchronous init Darrick J. Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260323224052.GJ6202@frogsfrogsfrogs \
    --to=djwong@kernel.org \
    --cc=bernd@bsbernd.com \
    --cc=bschubert@ddn.com \
    --cc=joannelkoong@gmail.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=miklos@szeredi.hu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox