Skip to content

Commit

Permalink
init: add an init_link helper
Browse files Browse the repository at this point in the history
Add a simple helper to link with a kernel space file name and switch
the early init code over to it.  Remove the now unused ksys_link.

Signed-off-by: Christoph Hellwig <hch@lst.de>
  • Loading branch information
Christoph Hellwig committed Jul 31, 2020
1 parent eb9d7d3 commit 812931d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
33 changes: 33 additions & 0 deletions fs/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,39 @@ int __init init_eaccess(const char *filename)
return error;
}

int __init init_link(const char *oldname, const char *newname)
{
struct dentry *new_dentry;
struct path old_path, new_path;
int error;

error = kern_path(oldname, 0, &old_path);
if (error)
return error;

new_dentry = kern_path_create(AT_FDCWD, newname, &new_path, 0);
error = PTR_ERR(new_dentry);
if (IS_ERR(new_dentry))
goto out;

error = -EXDEV;
if (old_path.mnt != new_path.mnt)
goto out_dput;
error = may_linkat(&old_path);
if (unlikely(error))
goto out_dput;
error = security_path_link(old_path.dentry, &new_path, new_dentry);
if (error)
goto out_dput;
error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry,
NULL);
out_dput:
done_path_create(&new_path, new_dentry);
out:
path_put(&old_path);
return error;
}

int __init init_unlink(const char *pathname)
{
return do_unlinkat(AT_FDCWD, getname_kernel(pathname));
Expand Down
3 changes: 1 addition & 2 deletions fs/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ long do_rmdir(int dfd, struct filename *name);
long do_unlinkat(int dfd, struct filename *name);
long do_symlinkat(const char __user *oldname, int newdfd,
const char __user *newname);
int do_linkat(int olddfd, const char __user *oldname, int newdfd,
const char __user *newname, int flags);
int may_linkat(struct path *link);

/*
* namespace.c
Expand Down
4 changes: 2 additions & 2 deletions fs/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ static bool safe_hardlink_source(struct inode *inode)
*
* Returns 0 if successful, -ve on error.
*/
static int may_linkat(struct path *link)
int may_linkat(struct path *link)
{
struct inode *inode = link->dentry->d_inode;

Expand Down Expand Up @@ -4086,7 +4086,7 @@ EXPORT_SYMBOL(vfs_link);
* with linux 2.0, and to avoid hard-linking to directories
* and other special files. --ADM
*/
int do_linkat(int olddfd, const char __user *oldname, int newdfd,
static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
const char __user *newname, int flags)
{
struct dentry *new_dentry;
Expand Down
1 change: 1 addition & 0 deletions include/linux/init_syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ int __init init_chroot(const char *filename);
int __init init_chown(const char *filename, uid_t user, gid_t group, int flags);
int __init init_chmod(const char *filename, umode_t mode);
int __init init_eaccess(const char *filename);
int __init init_link(const char *oldname, const char *newname);
int __init init_unlink(const char *pathname);
int __init init_rmdir(const char *pathname);
9 changes: 0 additions & 9 deletions include/linux/syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -1295,15 +1295,6 @@ static inline long ksys_mknod(const char __user *filename, umode_t mode,
return do_mknodat(AT_FDCWD, filename, mode, dev);
}

extern int do_linkat(int olddfd, const char __user *oldname, int newdfd,
const char __user *newname, int flags);

static inline long ksys_link(const char __user *oldname,
const char __user *newname)
{
return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
}

extern int do_fchownat(int dfd, const char __user *filename, uid_t user,
gid_t group, int flag);

Expand Down
2 changes: 1 addition & 1 deletion init/initramfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ static int __init maybe_link(void)
char *old = find_link(major, minor, ino, mode, collected);
if (old) {
clean_path(collected, 0);
return (ksys_link(old, collected) < 0) ? -1 : 1;
return (init_link(old, collected) < 0) ? -1 : 1;
}
}
return 0;
Expand Down

0 comments on commit 812931d

Please sign in to comment.