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/8] fs: derive file descriptor number by pointer arithmetic
Date: Tue,  7 Jan 2025 08:59:32 +0100	[thread overview]
Message-ID: <20250107075939.2841119-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250107075939.2841119-1-a.fatoum@pengutronix.de>

In preparation for merging struct file and struct filep, we are going to
remove struct filep specific members that are not in the upstream struct
file. The .no member is an easy one, because it's just the index
relative to the base of the file descriptor array and can thus be
simply replaced by a subtraction.

While at it, we move the array to static storage as there is no extra
initialization being done.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/fs.c      | 19 ++++++++++++-------
 include/fs.h |  2 --
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index 57bd781025f9..57d173beb737 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -221,7 +221,7 @@ static char *cwd;
 static struct dentry *cwd_dentry;
 static struct vfsmount *cwd_mnt;
 
-static FILE *files;
+static FILE files[MAX_FILES];
 static struct dentry *d_root;
 static struct vfsmount *mnt_root;
 
@@ -232,8 +232,6 @@ static int init_fs(void)
 	cwd = xzalloc(PATH_MAX);
 	*cwd = '/';
 
-	files = xzalloc(sizeof(FILE) * MAX_FILES);
-
 	return 0;
 }
 
@@ -314,13 +312,20 @@ static FILE *get_file(void)
 		if (!files[i].in_use) {
 			memset(&files[i], 0, sizeof(FILE));
 			files[i].in_use = 1;
-			files[i].no = i;
 			return &files[i];
 		}
 	}
 	return NULL;
 }
 
+static int file_to_fd(FILE *f)
+{
+	int fd = f - files;
+	if (fd < 0 || fd >= ARRAY_SIZE(files))
+		return -EBADFD;
+	return fd;
+}
+
 static void put_file(FILE *f)
 {
 	free(f->path);
@@ -2573,7 +2578,7 @@ int openat(int dirfd, const char *pathname, int flags)
 		f->size = 0;
 		f->fsdev = fsdev;
 
-		return f->no;
+		return file_to_fd(f);
 	}
 
 	filename = getname(pathname);
@@ -2662,7 +2667,7 @@ int openat(int dirfd, const char *pathname, int flags)
 	f->fsdev = fsdev;
 
 	if (flags & O_PATH)
-		return f->no;
+		return file_to_fd(f);
 
 	if (fsdrv->open) {
 		char *pathname = dpath(dentry, fsdev->vfsmount.mnt_root);
@@ -2684,7 +2689,7 @@ int openat(int dirfd, const char *pathname, int flags)
 	if (flags & O_APPEND)
 		f->pos = f->size;
 
-	return f->no;
+	return file_to_fd(f);
 
 out:
 	put_file(f);
diff --git a/include/fs.h b/include/fs.h
index 137eb2d2863e..5525dc1c9f17 100644
--- a/include/fs.h
+++ b/include/fs.h
@@ -29,8 +29,6 @@ typedef struct filep {
 
 	void *priv;         /* private to the filesystem driver              */
 
-	/* private fields. Mapping between FILE and filedescriptor number     */
-	int no;
 	char in_use;
 
 	struct inode *f_inode;
-- 
2.39.5




  reply	other threads:[~2025-01-07  8:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-07  7:59 [PATCH 0/8] fs: merge struct filep (FILE) and struct file Ahmad Fatoum
2025-01-07  7:59 ` Ahmad Fatoum [this message]
2025-01-07  7:59 ` [PATCH 2/8] fs: drop ifdefs in linux/fs.h Ahmad Fatoum
2025-01-07  7:59 ` [PATCH 3/8] fs: retire FILE.in_use member Ahmad Fatoum
2025-01-07  7:59 ` [PATCH 4/8] fs: align FILE struct member names with upstream struct file Ahmad Fatoum
2025-01-07  7:59 ` [PATCH 5/8] fs: fat: rename f_size to f_len Ahmad Fatoum
2025-01-07  7:59 ` [PATCH 6/8] fs: replace FILE.size by f_inode.i_size Ahmad Fatoum
2025-01-07  7:59 ` [PATCH 7/8] fs: merge struct file and struct filep Ahmad Fatoum
2025-01-07  7:59 ` [PATCH 8/8] fs: retire FILE typdef Ahmad Fatoum
2025-01-08 14:02 ` [PATCH 0/8] fs: merge struct filep (FILE) and struct file Sascha Hauer

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=20250107075939.2841119-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