[PATCH 1/8] fs: derive file descriptor number by pointer arithmetic

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx>
---
 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





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux