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 <hch@lst.de>
This commit is contained in:
Christoph Hellwig 2020-07-21 11:12:08 +02:00
parent 09cbcec07b
commit c60166f042
9 changed files with 46 additions and 11 deletions

View File

@ -25,6 +25,7 @@
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/kthread.h> #include <linux/kthread.h>
#include <linux/init_syscalls.h>
#include <uapi/linux/mount.h> #include <uapi/linux/mount.h>
#include "base.h" #include "base.h"
@ -359,7 +360,7 @@ int __init devtmpfs_mount(void)
if (!thread) if (!thread)
return 0; return 0;
err = do_mount("devtmpfs", "dev", "devtmpfs", MS_SILENT, NULL); err = init_mount("devtmpfs", "dev", "devtmpfs", MS_SILENT, NULL);
if (err) if (err)
printk(KERN_INFO "devtmpfs: error mounting %i\n", err); printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
else else
@ -408,7 +409,7 @@ static int __init devtmpfs_setup(void *p)
err = ksys_unshare(CLONE_NEWNS); err = ksys_unshare(CLONE_NEWNS);
if (err) if (err)
goto out; goto out;
err = do_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL); err = init_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
if (err) if (err)
goto out; goto out;
ksys_chdir("/.."); /* will traverse into overmounted root */ ksys_chdir("/.."); /* will traverse into overmounted root */

View File

@ -13,7 +13,7 @@ obj-y := open.o read_write.o file_table.o super.o \
seq_file.o xattr.o libfs.o fs-writeback.o \ seq_file.o xattr.o libfs.o fs-writeback.o \
pnode.o splice.o sync.o utimes.o d_path.o \ pnode.o splice.o sync.o utimes.o d_path.o \
stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \ stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \
fs_types.o fs_context.o fs_parser.o fsopen.o fs_types.o fs_context.o fs_parser.o fsopen.o init.o
ifeq ($(CONFIG_BLOCK),y) ifeq ($(CONFIG_BLOCK),y)
obj-y += buffer.o block_dev.o direct-io.o mpage.o obj-y += buffer.o block_dev.o direct-io.o mpage.o

25
fs/init.c Normal file
View File

@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Routines that mimic syscalls, but don't use the user address space or file
* descriptors. Only for init/ and related early init code.
*/
#include <linux/init.h>
#include <linux/mount.h>
#include <linux/namei.h>
#include <linux/fs.h>
#include <linux/init_syscalls.h>
#include "internal.h"
int __init init_mount(const char *dev_name, const char *dir_name,
const char *type_page, unsigned long flags, void *data_page)
{
struct path path;
int ret;
ret = kern_path(dir_name, LOOKUP_FOLLOW, &path);
if (ret)
return ret;
ret = path_mount(dev_name, &path, type_page, flags, data_page);
path_put(&path);
return ret;
}

View File

@ -89,6 +89,10 @@ extern int __mnt_want_write_file(struct file *);
extern void __mnt_drop_write_file(struct file *); extern void __mnt_drop_write_file(struct file *);
extern void dissolve_on_fput(struct vfsmount *); extern void dissolve_on_fput(struct vfsmount *);
int path_mount(const char *dev_name, struct path *path,
const char *type_page, unsigned long flags, void *data_page);
/* /*
* fs_struct.c * fs_struct.c
*/ */

View File

@ -3111,7 +3111,7 @@ char *copy_mount_string(const void __user *data)
* Therefore, if this magic number is present, it carries no information * Therefore, if this magic number is present, it carries no information
* and must be discarded. * and must be discarded.
*/ */
static int path_mount(const char *dev_name, struct path *path, int path_mount(const char *dev_name, struct path *path,
const char *type_page, unsigned long flags, void *data_page) const char *type_page, unsigned long flags, void *data_page)
{ {
unsigned int mnt_flags = 0, sb_flags; unsigned int mnt_flags = 0, sb_flags;

View File

@ -0,0 +1,4 @@
/* SPDX-License-Identifier: GPL-2.0 */
int __init init_mount(const char *dev_name, const char *dir_name,
const char *type_page, unsigned long flags, void *data_page);

View File

@ -395,16 +395,16 @@ static int __init do_mount_root(const char *name, const char *fs,
int ret; int ret;
if (data) { if (data) {
/* do_mount() requires a full page as fifth argument */ /* init_mount() requires a full page as fifth argument */
p = alloc_page(GFP_KERNEL); p = alloc_page(GFP_KERNEL);
if (!p) if (!p)
return -ENOMEM; return -ENOMEM;
data_page = page_address(p); data_page = page_address(p);
/* zero-pad. do_mount() will make sure it's terminated */ /* zero-pad. init_mount() will make sure it's terminated */
strncpy(data_page, data, PAGE_SIZE); strncpy(data_page, data, PAGE_SIZE);
} }
ret = do_mount(name, "/root", fs, flags, data_page); ret = init_mount(name, "/root", fs, flags, data_page);
if (ret) if (ret)
goto out; goto out;
@ -628,7 +628,7 @@ void __init prepare_namespace(void)
mount_root(); mount_root();
out: out:
devtmpfs_mount(); devtmpfs_mount();
do_mount(".", "/", NULL, MS_MOVE, NULL); init_mount(".", "/", NULL, MS_MOVE, NULL);
ksys_chroot("."); ksys_chroot(".");
} }

View File

@ -8,6 +8,7 @@
#include <linux/mount.h> #include <linux/mount.h>
#include <linux/major.h> #include <linux/major.h>
#include <linux/root_dev.h> #include <linux/root_dev.h>
#include <linux/init_syscalls.h>
void mount_block_root(char *name, int flags); void mount_block_root(char *name, int flags);
void mount_root(void); void mount_root(void);

View File

@ -62,7 +62,7 @@ static int __init init_linuxrc(struct subprocess_info *info, struct cred *new)
console_on_rootfs(); console_on_rootfs();
/* move initrd over / and chdir/chroot in initrd root */ /* move initrd over / and chdir/chroot in initrd root */
ksys_chdir("/root"); ksys_chdir("/root");
do_mount(".", "/", NULL, MS_MOVE, NULL); init_mount(".", "/", NULL, MS_MOVE, NULL);
ksys_chroot("."); ksys_chroot(".");
ksys_setsid(); ksys_setsid();
return 0; return 0;
@ -99,7 +99,7 @@ static void __init handle_initrd(void)
current->flags &= ~PF_FREEZER_SKIP; current->flags &= ~PF_FREEZER_SKIP;
/* move initrd to rootfs' /old */ /* move initrd to rootfs' /old */
do_mount("..", ".", NULL, MS_MOVE, NULL); init_mount("..", ".", NULL, MS_MOVE, NULL);
/* switch root and cwd back to / of rootfs */ /* switch root and cwd back to / of rootfs */
ksys_chroot(".."); ksys_chroot("..");
@ -113,7 +113,7 @@ static void __init handle_initrd(void)
mount_root(); mount_root();
printk(KERN_NOTICE "Trying to move old root to /initrd ... "); printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
error = do_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL); error = init_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
if (!error) if (!error)
printk("okay\n"); printk("okay\n");
else { else {