linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Al Viro <viro@zeniv.linux.org.uk>,
	Linus Torvalds <torvalds@linux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org
Subject: [PATCH 24/24] fs: add a kern_stat helper
Date: Mon, 20 Jul 2020 17:59:02 +0200	[thread overview]
Message-ID: <20200720155902.181712-25-hch@lst.de> (raw)
In-Reply-To: <20200720155902.181712-1-hch@lst.de>

Add a simple helper to stat/lstat with a kernel space file name and
switch the early init code over to it.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/md/md-autodetect.c |  2 +-
 fs/stat.c                  | 32 ++++++++++++++++++++++----------
 include/linux/fs.h         |  1 +
 init/initramfs.c           |  3 ++-
 4 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/drivers/md/md-autodetect.c b/drivers/md/md-autodetect.c
index 14b6e86814c061..5bd52ec05ed821 100644
--- a/drivers/md/md-autodetect.c
+++ b/drivers/md/md-autodetect.c
@@ -151,7 +151,7 @@ static void __init md_setup_drive(struct md_setup_args *args)
 		if (strncmp(devname, "/dev/", 5) == 0)
 			devname += 5;
 		snprintf(comp_name, 63, "/dev/%s", devname);
-		if (vfs_stat(comp_name, &stat) == 0 && S_ISBLK(stat.mode))
+		if (kern_stat(comp_name, &stat, 0) == 0 && S_ISBLK(stat.mode))
 			dev = new_decode_dev(stat.rdev);
 		if (!dev) {
 			pr_warn("md: Unknown device name: %s\n", devname);
diff --git a/fs/stat.c b/fs/stat.c
index dacecdda2e7967..3c976b92db00ca 100644
--- a/fs/stat.c
+++ b/fs/stat.c
@@ -151,7 +151,7 @@ int vfs_fstat(int fd, struct kstat *stat)
 /**
  * vfs_statx - Get basic and extra attributes by filename
  * @dfd: A file descriptor representing the base dir for a relative filename
- * @filename: The name of the file of interest
+ * @name: The name of the file of interest
  * @flags: Flags to control the query
  * @stat: The result structure to fill in.
  * @request_mask: STATX_xxx flags indicating what the caller wants
@@ -163,16 +163,16 @@ int vfs_fstat(int fd, struct kstat *stat)
  *
  * 0 will be returned on success, and a -ve error code if unsuccessful.
  */
-static int vfs_statx(int dfd, const char __user *filename, int flags,
+static int vfs_statx(int dfd, struct filename *name, int flags,
 	      struct kstat *stat, u32 request_mask)
 {
 	struct path path;
 	unsigned lookup_flags = 0;
-	int error;
+	int error = -EINVAL;
 
 	if (flags & ~(AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT | AT_EMPTY_PATH |
 		      AT_STATX_SYNC_TYPE))
-		return -EINVAL;
+		goto out_putname;
 
 	if (!(flags & AT_SYMLINK_NOFOLLOW))
 		lookup_flags |= LOOKUP_FOLLOW;
@@ -182,9 +182,9 @@ static int vfs_statx(int dfd, const char __user *filename, int flags,
 		lookup_flags |= LOOKUP_EMPTY;
 
 retry:
-	error = user_path_at(dfd, filename, lookup_flags, &path);
+	error = filename_lookup(dfd, name, lookup_flags, &path, NULL);
 	if (error)
-		goto out;
+		return error;
 
 	error = vfs_getattr(&path, stat, request_mask, flags);
 	stat->mnt_id = real_mount(path.mnt)->mnt_id;
@@ -197,15 +197,25 @@ static int vfs_statx(int dfd, const char __user *filename, int flags,
 		lookup_flags |= LOOKUP_REVAL;
 		goto retry;
 	}
-out:
+out_putname:
+	if (!IS_ERR(name))
+		putname(name);
 	return error;
 }
 
+int __init kern_stat(const char *filename, struct kstat *stat, int flags)
+{
+	return vfs_statx(AT_FDCWD, getname_kernel(filename),
+			 flags | AT_NO_AUTOMOUNT, stat, STATX_BASIC_STATS);
+}
+
 int vfs_fstatat(int dfd, const char __user *filename,
 			      struct kstat *stat, int flags)
 {
-	return vfs_statx(dfd, filename, flags | AT_NO_AUTOMOUNT,
-			 stat, STATX_BASIC_STATS);
+	int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
+
+	return vfs_statx(dfd, getname_flags(filename, lookup_flags, NULL),
+			 flags | AT_NO_AUTOMOUNT, stat, STATX_BASIC_STATS);
 }
 
 #ifdef __ARCH_WANT_OLD_STAT
@@ -569,6 +579,7 @@ cp_statx(const struct kstat *stat, struct statx __user *buffer)
 int do_statx(int dfd, const char __user *filename, unsigned flags,
 	     unsigned int mask, struct statx __user *buffer)
 {
+	int lookup_flags = (flags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0;
 	struct kstat stat;
 	int error;
 
@@ -577,7 +588,8 @@ int do_statx(int dfd, const char __user *filename, unsigned flags,
 	if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_SYNC_TYPE)
 		return -EINVAL;
 
-	error = vfs_statx(dfd, filename, flags, &stat, mask);
+	error = vfs_statx(dfd, getname_flags(filename, lookup_flags, NULL),
+			  flags, &stat, mask);
 	if (error)
 		return error;
 
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 0e0cd6a988bb38..d1f8edb39cf969 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3671,5 +3671,6 @@ int __init kern_link(const char *oldname, const char *newname);
 int __init kern_symlink(const char *oldname, const char *newname);
 int kern_unlink(const char *pathname);
 int __init kern_rmdir(const char *pathname);
+int __init kern_stat(const char *filename, struct kstat *stat, int flags);
 
 #endif /* _LINUX_FS_H */
diff --git a/init/initramfs.c b/init/initramfs.c
index d72594298133a7..6c605f23900fa1 100644
--- a/init/initramfs.c
+++ b/init/initramfs.c
@@ -296,7 +296,8 @@ static void __init clean_path(char *path, umode_t fmode)
 {
 	struct kstat st;
 
-	if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
+	if (kern_stat(path, &st, AT_SYMLINK_NOFOLLOW) &&
+	    (st.mode ^ fmode) & S_IFMT) {
 		if (S_ISDIR(st.mode))
 			kern_rmdir(path);
 		else
-- 
2.27.0


      parent reply	other threads:[~2020-07-20 16:26 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-20 15:58 add file system helpers that take kernel pointers for the init code Christoph Hellwig
2020-07-20 15:58 ` [PATCH 01/24] init: initialize ramdisk_execute_command at compile time Christoph Hellwig
2020-07-20 15:58 ` [PATCH 02/24] fs: add a do_kern_mount helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 03/24] fs: add a kern_umount helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 04/24] fs: move the putname from filename_create to the callers Christoph Hellwig
2020-07-20 18:05   ` Linus Torvalds
2020-07-20 15:58 ` [PATCH 05/24] fs: move the putname from filename_lookup " Christoph Hellwig
2020-07-20 18:11   ` Al Viro
2020-07-20 15:58 ` [PATCH 06/24] fs: add a kern_chdir helper Christoph Hellwig
2020-07-20 18:17   ` Al Viro
2020-07-20 15:58 ` [PATCH 07/24] fs: add a kern_chroot helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 08/24] fs: add a kern_access helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 09/24] fs: add a kern_chown helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 10/24] fs: move the uid16 (f)chown syscalls to fs/open.c Christoph Hellwig
2020-07-20 15:58 ` [PATCH 11/24] fs: add a kern_chmod helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 12/24] fs: add a kern_utimes helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 13/24] fs: add a kern_mkdir helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 14/24] fs: add a kern_mknod helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 15/24] fs: add a kern_link helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 16/24] fs: add a kern_symlink helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 17/24] fs: add a kern_unlink helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 18/24] fs: add a kern_rmdir helper Christoph Hellwig
2020-07-20 15:58 ` [PATCH 19/24] fs: remove vfs_statx_fd Christoph Hellwig
2020-07-20 15:58 ` [PATCH 20/24] fs: implement vfs_stat and vfs_lstat in terms of vfs_fstatat Christoph Hellwig
2020-07-20 15:58 ` [PATCH 21/24] fs: move vfs_fstatat out of line Christoph Hellwig
2020-07-20 15:59 ` [PATCH 22/24] fs: remove vfs_stat_set_lookup_flags Christoph Hellwig
2020-07-20 15:59 ` [PATCH 23/24] fs: remove KSTAT_QUERY_FLAGS Christoph Hellwig
2020-07-20 15:59 ` Christoph Hellwig [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200720155902.181712-25-hch@lst.de \
    --to=hch@lst.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).