[PATCH 1/7] fs: move cdev open count to cdev_open()/cdev_close()

Sascha Hauer s.hauer at pengutronix.de
Wed Feb 14 23:47:51 PST 2024


All code opening a cdev goes through cdev_open(), so open counting
must be done there and not in devfs_open() which itself only calls
cdev_open().

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 fs/devfs-core.c | 13 +++++++++++--
 fs/devfs.c      |  4 ----
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index c7187e3c01..87d4591d99 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -175,8 +175,15 @@ int cdev_find_free_index(const char *basename)
 
 int cdev_open(struct cdev *cdev, unsigned long flags)
 {
-	if (cdev->ops->open)
-		return cdev->ops->open(cdev, flags);
+	int ret;
+
+	if (cdev->ops->open) {
+		ret = cdev->ops->open(cdev, flags);
+		if (ret)
+			return ret;
+	}
+
+	cdev->open++;
 
 	return 0;
 }
@@ -221,6 +228,8 @@ void cdev_close(struct cdev *cdev)
 {
 	if (cdev->ops->close)
 		cdev->ops->close(cdev);
+
+	cdev->open--;
 }
 
 ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulong flags)
diff --git a/fs/devfs.c b/fs/devfs.c
index c30ae4f384..c8ddbbdab0 100644
--- a/fs/devfs.c
+++ b/fs/devfs.c
@@ -114,8 +114,6 @@ static int devfs_open(struct device *_dev, FILE *f, const char *filename)
 			return ret;
 	}
 
-	cdev->open++;
-
 	return 0;
 }
 
@@ -130,8 +128,6 @@ static int devfs_close(struct device *_dev, FILE *f)
 			return ret;
 	}
 
-	cdev->open--;
-
 	return 0;
 }
 
-- 
2.39.2




More information about the barebox mailing list