[PATCH 6/8] cdev: Create missing cdev_* functions

Sascha Hauer s.hauer at pengutronix.de
Mon Feb 7 01:49:51 PST 2022


We have several functions like cdev_read(), cdev_write() and others. For
consistency create the remaining functions: cdev_lseek(),
cdev_protect(), cdev_discard_range(), cdev_memmap() and cdev_truncate()

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 fs/devfs-core.c  | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/devfs.c       | 46 +++++-------------------------------
 include/driver.h |  5 ++++
 3 files changed, 72 insertions(+), 40 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index dd6a9585bc..c9a8529737 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -239,6 +239,67 @@ int cdev_erase(struct cdev *cdev, loff_t count, loff_t offset)
 	return cdev->ops->erase(cdev, count, cdev->offset + offset);
 }
 
+int cdev_lseek(struct cdev *cdev, loff_t pos)
+{
+	int ret;
+
+	if (cdev->ops->lseek) {
+		ret = cdev->ops->lseek(cdev, pos + cdev->offset);
+		if (ret < 0)
+			return ret;
+	}
+
+	return 0;
+}
+
+int cdev_protect(struct cdev *cdev, size_t count, loff_t offset, int prot)
+{
+	if (!cdev->ops->protect)
+		return -ENOSYS;
+
+	return cdev->ops->protect(cdev, count, offset + cdev->offset, prot);
+}
+
+int cdev_discard_range(struct cdev *cdev, loff_t count, loff_t offset)
+{
+	if (!cdev->ops->discard_range)
+		return -ENOSYS;
+
+	if (cdev->flags & DEVFS_PARTITION_READONLY)
+		return -EPERM;
+
+	if (offset >= cdev->size)
+		return 0;
+
+	if (count + offset > cdev->size)
+		count = cdev->size - offset;
+
+	return cdev->ops->discard_range(cdev, count, offset + cdev->offset);
+}
+
+int cdev_memmap(struct cdev *cdev, void **map, int flags)
+{
+	int ret = -ENOSYS;
+
+	if (!cdev->ops->memmap)
+		return -EINVAL;
+
+	ret = cdev->ops->memmap(cdev, map, flags);
+
+	if (!ret)
+		*map = (void *)((unsigned long)*map + (unsigned long)cdev->offset);
+
+	return ret;
+}
+
+int cdev_truncate(struct cdev *cdev, size_t size)
+{
+	if (cdev->ops->truncate)
+		return cdev->ops->truncate(cdev, size);
+
+	return -EPERM;
+}
+
 int devfs_create(struct cdev *new)
 {
 	struct cdev *cdev;
diff --git a/fs/devfs.c b/fs/devfs.c
index df229cca48..d205881765 100644
--- a/fs/devfs.c
+++ b/fs/devfs.c
@@ -57,15 +57,8 @@ static int devfs_write(struct device_d *_dev, FILE *f, const void *buf, size_t s
 static int devfs_lseek(struct device_d *_dev, FILE *f, loff_t pos)
 {
 	struct cdev *cdev = f->priv;
-	int ret;
 
-	if (cdev->ops->lseek) {
-		ret = cdev->ops->lseek(cdev, pos + cdev->offset);
-		if (ret < 0)
-			return ret;
-	}
-
-	return 0;
+	return cdev_lseek(cdev, pos);
 }
 
 static int devfs_erase(struct device_d *_dev, FILE *f, loff_t count, loff_t offset)
@@ -81,14 +74,11 @@ static int devfs_erase(struct device_d *_dev, FILE *f, loff_t count, loff_t offs
 	return cdev_erase(cdev, count, offset);
 }
 
-static int devfs_protect(struct device_d *_dev, FILE *f, size_t count, loff_t offset, int prot)
+static int devfs_protect(struct device_d *dev, FILE *f, size_t count, loff_t offset, int prot)
 {
 	struct cdev *cdev = f->priv;
 
-	if (!cdev->ops->protect)
-		return -ENOSYS;
-
-	return cdev->ops->protect(cdev, count, offset + cdev->offset, prot);
+	return cdev_protect(cdev, count, offset, prot);
 }
 
 static int devfs_discard_range(struct device_d *dev, FILE *f, loff_t count,
@@ -96,35 +86,14 @@ static int devfs_discard_range(struct device_d *dev, FILE *f, loff_t count,
 {
 	struct cdev *cdev = f->priv;
 
-	if (!cdev->ops->discard_range)
-		return -ENOSYS;
-
-	if (cdev->flags & DEVFS_PARTITION_READONLY)
-		return -EPERM;
-
-	if (offset >= cdev->size)
-		return 0;
-
-	if (count + offset > cdev->size)
-		count = cdev->size - offset;
-
-	return cdev->ops->discard_range(cdev, count, offset + cdev->offset);
+	return cdev_discard_range(cdev, count, offset);
 }
 
 static int devfs_memmap(struct device_d *_dev, FILE *f, void **map, int flags)
 {
 	struct cdev *cdev = f->priv;
-	int ret = -ENOSYS;
 
-	if (!cdev->ops->memmap)
-		return -EINVAL;
-
-	ret = cdev->ops->memmap(cdev, map, flags);
-
-	if (!ret)
-		*map = (void *)((unsigned long)*map + (unsigned long)cdev->offset);
-
-	return ret;
+	return cdev_memmap(cdev, map, flags);
 }
 
 static int devfs_open(struct device_d *_dev, FILE *f, const char *filename)
@@ -183,10 +152,7 @@ static int devfs_truncate(struct device_d *dev, FILE *f, loff_t size)
 {
 	struct cdev *cdev = f->priv;
 
-	if (cdev->ops->truncate)
-		return cdev->ops->truncate(cdev, size);
-
-	return -EPERM;
+	return cdev_truncate(cdev, size);
 }
 
 static struct inode *devfs_alloc_inode(struct super_block *sb)
diff --git a/include/driver.h b/include/driver.h
index 93de4f676e..b9b0b8497d 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -503,6 +503,11 @@ ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulo
 ssize_t cdev_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset, ulong flags);
 int cdev_ioctl(struct cdev *cdev, int cmd, void *buf);
 int cdev_erase(struct cdev *cdev, loff_t count, loff_t offset);
+int cdev_lseek(struct cdev*, loff_t);
+int cdev_protect(struct cdev*, size_t count, loff_t offset, int prot);
+int cdev_discard_range(struct cdev*, loff_t count, loff_t offset);
+int cdev_memmap(struct cdev*, void **map, int flags);
+int cdev_truncate(struct cdev*, size_t size);
 loff_t cdev_unallocated_space(struct cdev *cdev);
 
 #define DEVFS_PARTITION_FIXED		(1U << 0)
-- 
2.30.2




More information about the barebox mailing list