Skip to content

Commit c60166f

Browse files
author
Christoph Hellwig
committed
init: add an init_mount helper
Like do_mount, but takes a kernel pointer for the destination path. Switch over the mounts in the init code and devtmpfs to it, which just happen to work due to the implicit set_fs(KERNEL_DS) during early init right now. Signed-off-by: Christoph Hellwig <[email protected]>
1 parent 09cbcec commit c60166f

File tree

9 files changed

+46
-11
lines changed

9 files changed

+46
-11
lines changed

drivers/base/devtmpfs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/sched.h>
2626
#include <linux/slab.h>
2727
#include <linux/kthread.h>
28+
#include <linux/init_syscalls.h>
2829
#include <uapi/linux/mount.h>
2930
#include "base.h"
3031

@@ -359,7 +360,7 @@ int __init devtmpfs_mount(void)
359360
if (!thread)
360361
return 0;
361362

362-
err = do_mount("devtmpfs", "dev", "devtmpfs", MS_SILENT, NULL);
363+
err = init_mount("devtmpfs", "dev", "devtmpfs", MS_SILENT, NULL);
363364
if (err)
364365
printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
365366
else
@@ -408,7 +409,7 @@ static int __init devtmpfs_setup(void *p)
408409
err = ksys_unshare(CLONE_NEWNS);
409410
if (err)
410411
goto out;
411-
err = do_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
412+
err = init_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
412413
if (err)
413414
goto out;
414415
ksys_chdir("/.."); /* will traverse into overmounted root */

fs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ obj-y := open.o read_write.o file_table.o super.o \
1313
seq_file.o xattr.o libfs.o fs-writeback.o \
1414
pnode.o splice.o sync.o utimes.o d_path.o \
1515
stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \
16-
fs_types.o fs_context.o fs_parser.o fsopen.o
16+
fs_types.o fs_context.o fs_parser.o fsopen.o init.o
1717

1818
ifeq ($(CONFIG_BLOCK),y)
1919
obj-y += buffer.o block_dev.o direct-io.o mpage.o

fs/init.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Routines that mimic syscalls, but don't use the user address space or file
4+
* descriptors. Only for init/ and related early init code.
5+
*/
6+
#include <linux/init.h>
7+
#include <linux/mount.h>
8+
#include <linux/namei.h>
9+
#include <linux/fs.h>
10+
#include <linux/init_syscalls.h>
11+
#include "internal.h"
12+
13+
int __init init_mount(const char *dev_name, const char *dir_name,
14+
const char *type_page, unsigned long flags, void *data_page)
15+
{
16+
struct path path;
17+
int ret;
18+
19+
ret = kern_path(dir_name, LOOKUP_FOLLOW, &path);
20+
if (ret)
21+
return ret;
22+
ret = path_mount(dev_name, &path, type_page, flags, data_page);
23+
path_put(&path);
24+
return ret;
25+
}

fs/internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ extern int __mnt_want_write_file(struct file *);
8989
extern void __mnt_drop_write_file(struct file *);
9090

9191
extern void dissolve_on_fput(struct vfsmount *);
92+
93+
int path_mount(const char *dev_name, struct path *path,
94+
const char *type_page, unsigned long flags, void *data_page);
95+
9296
/*
9397
* fs_struct.c
9498
*/

fs/namespace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3111,7 +3111,7 @@ char *copy_mount_string(const void __user *data)
31113111
* Therefore, if this magic number is present, it carries no information
31123112
* and must be discarded.
31133113
*/
3114-
static int path_mount(const char *dev_name, struct path *path,
3114+
int path_mount(const char *dev_name, struct path *path,
31153115
const char *type_page, unsigned long flags, void *data_page)
31163116
{
31173117
unsigned int mnt_flags = 0, sb_flags;

include/linux/init_syscalls.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
int __init init_mount(const char *dev_name, const char *dir_name,
4+
const char *type_page, unsigned long flags, void *data_page);

init/do_mounts.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,16 @@ static int __init do_mount_root(const char *name, const char *fs,
395395
int ret;
396396

397397
if (data) {
398-
/* do_mount() requires a full page as fifth argument */
398+
/* init_mount() requires a full page as fifth argument */
399399
p = alloc_page(GFP_KERNEL);
400400
if (!p)
401401
return -ENOMEM;
402402
data_page = page_address(p);
403-
/* zero-pad. do_mount() will make sure it's terminated */
403+
/* zero-pad. init_mount() will make sure it's terminated */
404404
strncpy(data_page, data, PAGE_SIZE);
405405
}
406406

407-
ret = do_mount(name, "/root", fs, flags, data_page);
407+
ret = init_mount(name, "/root", fs, flags, data_page);
408408
if (ret)
409409
goto out;
410410

@@ -628,7 +628,7 @@ void __init prepare_namespace(void)
628628
mount_root();
629629
out:
630630
devtmpfs_mount();
631-
do_mount(".", "/", NULL, MS_MOVE, NULL);
631+
init_mount(".", "/", NULL, MS_MOVE, NULL);
632632
ksys_chroot(".");
633633
}
634634

init/do_mounts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/mount.h>
99
#include <linux/major.h>
1010
#include <linux/root_dev.h>
11+
#include <linux/init_syscalls.h>
1112

1213
void mount_block_root(char *name, int flags);
1314
void mount_root(void);

init/do_mounts_initrd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static int __init init_linuxrc(struct subprocess_info *info, struct cred *new)
6262
console_on_rootfs();
6363
/* move initrd over / and chdir/chroot in initrd root */
6464
ksys_chdir("/root");
65-
do_mount(".", "/", NULL, MS_MOVE, NULL);
65+
init_mount(".", "/", NULL, MS_MOVE, NULL);
6666
ksys_chroot(".");
6767
ksys_setsid();
6868
return 0;
@@ -99,7 +99,7 @@ static void __init handle_initrd(void)
9999
current->flags &= ~PF_FREEZER_SKIP;
100100

101101
/* move initrd to rootfs' /old */
102-
do_mount("..", ".", NULL, MS_MOVE, NULL);
102+
init_mount("..", ".", NULL, MS_MOVE, NULL);
103103
/* switch root and cwd back to / of rootfs */
104104
ksys_chroot("..");
105105

@@ -113,7 +113,7 @@ static void __init handle_initrd(void)
113113
mount_root();
114114

115115
printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
116-
error = do_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
116+
error = init_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
117117
if (!error)
118118
printk("okay\n");
119119
else {

0 commit comments

Comments
 (0)