mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/6] fs: use filename_create/filename_lookup instead of open-coding
Date: Thu, 20 Mar 2025 06:20:14 +0100	[thread overview]
Message-ID: <20250320052019.1726331-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250320052019.1726331-1-a.fatoum@pengutronix.de>

By reusing the existing file lookup code, open can open all directories,
e.g. / and /tmp/., which so far wasn't possible.

We need to be able to open these paths for directory FDs to work as one
would expect.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/fs.c           | 55 ++++++++---------------------------------------
 test/self/dirfd.c |  7 +-----
 2 files changed, 10 insertions(+), 52 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index 96ca60341ea4..1766719c567a 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -2543,9 +2543,7 @@ int openat(int dirfd, const char *pathname, int flags)
 	int error = 0;
 	struct inode *inode = NULL;
 	struct dentry *dentry = NULL;
-	struct nameidata nd;
-	const char *s;
-	struct filename *filename;
+	struct path path;
 
 	if (flags & O_TMPFILE) {
 		fsdev = get_fsdevice_by_path(dirfd, pathname);
@@ -2575,57 +2573,22 @@ int openat(int dirfd, const char *pathname, int flags)
 		return file_to_fd(f);
 	}
 
-	filename = getname(pathname);
-	if (IS_ERR(filename))
-		return PTR_ERR(filename);
-
-	set_nameidata(&nd, filename);
-
-	s = path_init(dirfd, &nd, LOOKUP_FOLLOW);
-	if (IS_ERR(s))
-		return PTR_ERR(s);
-
-	while (1) {
-		error = link_path_walk(s, &nd);
-		if (error)
-			break;
-
-		if (!d_is_dir(nd.path.dentry)) {
-			error = -ENOTDIR;
-			break;
-		}
-
-		dentry = __lookup_hash(&nd.last, nd.path.dentry, 0);
-		if (IS_ERR(dentry)) {
-			error = PTR_ERR(dentry);
-			break;
-		}
-
-		if (!d_is_symlink(dentry))
-			break;
-
-		dput(dentry);
-
-		error = lookup_last(&nd);
-		if (error <= 0)
-			break;
-
-		s = trailing_symlink(&nd);
-		if (IS_ERR(s)) {
-			error = PTR_ERR(s);
-			break;
-		}
+	if (flags & O_CREAT) {
+		dentry = filename_create(dirfd, getname(pathname), &path, 0);
+		error = PTR_ERR_OR_ZERO(dentry);
 	}
 
-	terminate_walk(&nd);
-	putname(nd.name);
+	if (!(flags & O_CREAT) || error == -EEXIST) {
+		error = filename_lookup(dirfd, getname(pathname), LOOKUP_FOLLOW, &path);
+		dentry = path.dentry;
+	}
 
 	if (error)
 		goto out1;
 
 	if (d_is_negative(dentry)) {
 		if (flags & O_CREAT) {
-			error = create(nd.path.dentry, dentry);
+			error = create(path.dentry, dentry);
 			if (error)
 				goto out1;
 		} else {
diff --git a/test/self/dirfd.c b/test/self/dirfd.c
index 644ff214fb37..20b54258715a 100644
--- a/test/self/dirfd.c
+++ b/test/self/dirfd.c
@@ -100,13 +100,8 @@ static void test_dirfd(void)
 	int fd;
 
 	fd = open("/", O_PATH);
-	if (expect(fd < 0, false, "open(/, O_PATH) = %d", fd)) {
+	if (expect(fd < 0, false, "open(/, O_PATH) = %d", fd))
 		close(fd);
-	} else {
-		pr_info("\tIgnoring expected failure\n");
-		failed_tests--;
-		skipped_tests++;
-	}
 
 #define B(dot, dotdot, zero, dev) 0b##dev##zero##dotdot##dot
 	/* We do fiften tests for every configuration
-- 
2.39.5




  reply	other threads:[~2025-03-20  5:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-20  5:20 [PATCH 0/6] fs: implement tree and truncate Ahmad Fatoum
2025-03-20  5:20 ` Ahmad Fatoum [this message]
2025-03-20  5:20 ` [PATCH 2/6] fs: implement O_DIRECTORY Ahmad Fatoum
2025-03-20  5:20 ` [PATCH 3/6] fs: implement opendir in terms of fdopendir Ahmad Fatoum
2025-03-20  5:20 ` [PATCH 4/6] commands: implement tree command Ahmad Fatoum
2025-03-20  5:20 ` [PATCH 5/6] commands: add new truncate command Ahmad Fatoum
2025-03-20  5:20 ` [PATCH 6/6] Documentation: devel: add short section on file systems Ahmad Fatoum

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=20250320052019.1726331-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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