init: add an init_link helper
authorChristoph Hellwig <hch@lst.de>
Wed, 22 Jul 2020 09:14:19 +0000 (11:14 +0200)
committerChristoph Hellwig <hch@lst.de>
Fri, 31 Jul 2020 06:17:53 +0000 (08:17 +0200)
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>
fs/init.c
fs/internal.h
fs/namei.c
include/linux/init_syscalls.h
include/linux/syscalls.h
init/initramfs.c

index 6d9af40..5db9d9f 100644 (file)
--- a/fs/init.c
+++ b/fs/init.c
@@ -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));
index 6d82681..58451b0 100644 (file)
@@ -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
index d75a603..13de64c 100644 (file)
@@ -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;
 
@@ -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;
index 7031c09..5ca15a5 100644 (file)
@@ -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);
index a277963..4b18b91 100644 (file)
@@ -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);
 
index efa9fb7..516a669 100644 (file)
@@ -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;