Add the support to mount a device at runtime. The number of dynamic
mounts is limited by a #define.
For devices supporting multiple files struct file is modified to hold
a pointer to file specific data in contrast to device specific data.
Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
---
include/lib.h | 5 +++++
lib/sys.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 54 insertions(+), 1 deletion(-)
diff --git a/include/lib.h b/include/lib.h
index 36d94ec4..fd8c36de 100644
--- a/include/lib.h
+++ b/include/lib.h
@@ -172,6 +172,7 @@ struct file {
union {
int fd; /* Any fd from an upper layer. */
void *dev;
+ void *filedata;
};
};
@@ -194,6 +195,10 @@ struct mount_point {
void *dev;
};
+int mount(const char *path, void *dev,
+ int (*open)(struct mount_point *, const char *, int, mode_t));
+void umount(const char *path);
+
unsigned int alloc_file_type(const struct file_ops *ops);
off_t lseek_default(struct file *file, off_t offset, int whence);
diff --git a/lib/sys.c b/lib/sys.c
index 2f33c937..dc8a8c69 100644
--- a/lib/sys.c
+++ b/lib/sys.c
@@ -339,7 +339,14 @@ static int open_mem(struct mount_point *mnt, const char *pathname, int flags,
return fd;
}
-static struct mount_point mount_points[] = {
+#ifdef CONFIG_CONSFRONT
+#define STATIC_MNTS 4
+#else
+#define STATIC_MNTS 2
+#endif
+#define DYNAMIC_MNTS 8
+
+static struct mount_point mount_points[STATIC_MNTS + DYNAMIC_MNTS] = {
{ .path = "/var/log", .open = open_log, .dev = NULL },
{ .path = "/dev/mem", .open = open_mem, .dev = NULL },
#ifdef CONFIG_CONSFRONT
@@ -365,6 +372,8 @@ int open(const char *pathname, int flags, ...)
for ( m = 0; m < ARRAY_SIZE(mount_points); m++ )
{
mnt = mount_points + m;
+ if ( !mnt->path )
+ continue;
mlen = strlen(mnt->path);
if ( !strncmp(pathname, mnt->path, mlen) &&
(pathname[mlen] == '/' || pathname[mlen] == 0) )
@@ -375,6 +384,45 @@ int open(const char *pathname, int flags, ...)
return -1;
}
+int mount(const char *path, void *dev,
+ int (*open)(struct mount_point *, const char *, int, mode_t))
+{
+ unsigned int m;
+ struct mount_point *mnt;
+
+ for ( m = 0; m < ARRAY_SIZE(mount_points); m++ )
+ {
+ mnt = mount_points + m;
+ if ( !mnt->path )
+ {
+ mnt->path = strdup(path);
+ mnt->open = open;
+ mnt->dev = dev;
+ return 0;
+ }
+ }
+
+ errno = ENOSPC;
+ return -1;
+}
+
+void umount(const char *path)
+{
+ unsigned int m;
+ struct mount_point *mnt;
+
+ for ( m = 0; m < ARRAY_SIZE(mount_points); m++ )
+ {
+ mnt = mount_points + m;
+ if ( mnt->path && !strcmp(mnt->path, path) )
+ {
+ free((char *)mnt->path);
+ mnt->path = NULL;
+ return;
+ }
+ }
+}
+
int isatty(int fd)
{
return files[fd].type == FTYPE_CONSOLE;
--
2.35.3
On 10/02/2023 10:46 am, Juergen Gross wrote:
> diff --git a/lib/sys.c b/lib/sys.c
> index 2f33c937..dc8a8c69 100644
> --- a/lib/sys.c
> +++ b/lib/sys.c
> @@ -339,7 +339,14 @@ static int open_mem(struct mount_point *mnt, const char *pathname, int flags,
> return fd;
> }
>
> -static struct mount_point mount_points[] = {
> +#ifdef CONFIG_CONSFRONT
> +#define STATIC_MNTS 4
> +#else
> +#define STATIC_MNTS 2
> +#endif
This ought to be
#define STATIC_MNTS (2 + (IS_ENABLED(CONFIG_CONSFRONT) * 2))
because it shows where the parts come from, and is much cleaner to add a
3rd one to in due course.
That said, it would be simpler to just have a total mounts set at 16 or
so? Does a difference of two dynamic mounts depending on CONSFRONT
actually matter?
~Andrew
On 10.02.23 12:43, Andrew Cooper wrote:
> On 10/02/2023 10:46 am, Juergen Gross wrote:
>> diff --git a/lib/sys.c b/lib/sys.c
>> index 2f33c937..dc8a8c69 100644
>> --- a/lib/sys.c
>> +++ b/lib/sys.c
>> @@ -339,7 +339,14 @@ static int open_mem(struct mount_point *mnt, const char *pathname, int flags,
>> return fd;
>> }
>>
>> -static struct mount_point mount_points[] = {
>> +#ifdef CONFIG_CONSFRONT
>> +#define STATIC_MNTS 4
>> +#else
>> +#define STATIC_MNTS 2
>> +#endif
>
> This ought to be
>
> #define STATIC_MNTS (2 + (IS_ENABLED(CONFIG_CONSFRONT) * 2))
>
> because it shows where the parts come from, and is much cleaner to add a
> 3rd one to in due course.
In principle fine, but I'm not sure it is worth the effort to add
IS_ENABLED() support to Mini-OS.
> That said, it would be simpler to just have a total mounts set at 16 or
> so? Does a difference of two dynamic mounts depending on CONSFRONT
> actually matter?
Probably not. Samuel, any thoughts on that?
Juergen
Juergen Gross, le ven. 10 févr. 2023 13:14:20 +0100, a ecrit:
> On 10.02.23 12:43, Andrew Cooper wrote:
> > On 10/02/2023 10:46 am, Juergen Gross wrote:
> > > diff --git a/lib/sys.c b/lib/sys.c
> > > index 2f33c937..dc8a8c69 100644
> > > --- a/lib/sys.c
> > > +++ b/lib/sys.c
> > > @@ -339,7 +339,14 @@ static int open_mem(struct mount_point *mnt, const char *pathname, int flags,
> > > return fd;
> > > }
> > > -static struct mount_point mount_points[] = {
> > > +#ifdef CONFIG_CONSFRONT
> > > +#define STATIC_MNTS 4
> > > +#else
> > > +#define STATIC_MNTS 2
> > > +#endif
> >
> > This ought to be
> >
> > #define STATIC_MNTS (2 + (IS_ENABLED(CONFIG_CONSFRONT) * 2))
> >
> > because it shows where the parts come from, and is much cleaner to add a
> > 3rd one to in due course.
>
> In principle fine, but I'm not sure it is worth the effort to add
> IS_ENABLED() support to Mini-OS.
>
> > That said, it would be simpler to just have a total mounts set at 16 or
> > so? Does a difference of two dynamic mounts depending on CONSFRONT
> > actually matter?
>
> Probably not. Samuel, any thoughts on that?
We can probably as well just have 16 mount entries indeed.
Samuel
On 10/02/2023 12:44 pm, Samuel Thibault wrote:
> Juergen Gross, le ven. 10 févr. 2023 13:14:20 +0100, a ecrit:
>> On 10.02.23 12:43, Andrew Cooper wrote:
>>> On 10/02/2023 10:46 am, Juergen Gross wrote:
>>>> diff --git a/lib/sys.c b/lib/sys.c
>>>> index 2f33c937..dc8a8c69 100644
>>>> --- a/lib/sys.c
>>>> +++ b/lib/sys.c
>>>> @@ -339,7 +339,14 @@ static int open_mem(struct mount_point *mnt, const char *pathname, int flags,
>>>> return fd;
>>>> }
>>>> -static struct mount_point mount_points[] = {
>>>> +#ifdef CONFIG_CONSFRONT
>>>> +#define STATIC_MNTS 4
>>>> +#else
>>>> +#define STATIC_MNTS 2
>>>> +#endif
>>> This ought to be
>>>
>>> #define STATIC_MNTS (2 + (IS_ENABLED(CONFIG_CONSFRONT) * 2))
>>>
>>> because it shows where the parts come from, and is much cleaner to add a
>>> 3rd one to in due course.
>> In principle fine, but I'm not sure it is worth the effort to add
>> IS_ENABLED() support to Mini-OS.
>>
>>> That said, it would be simpler to just have a total mounts set at 16 or
>>> so? Does a difference of two dynamic mounts depending on CONSFRONT
>>> actually matter?
>> Probably not. Samuel, any thoughts on that?
> We can probably as well just have 16 mount entries indeed.
I'm happy to fix that up on commit if it's the only other issue in this
patch.
~Andrew
© 2016 - 2026 Red Hat, Inc.