From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Cc: Michael Olbrich <mol@pengutronix.de>
Subject: [PATCH 6/8] cdev: Create missing cdev_* functions
Date: Mon, 7 Feb 2022 10:49:51 +0100 [thread overview]
Message-ID: <20220207094953.949868-6-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20220207094953.949868-1-s.hauer@pengutronix.de>
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@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
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2022-02-07 9:52 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-07 9:49 [PATCH 1/8] cdev: rename partuuid to uuid Sascha Hauer
2022-02-07 9:49 ` [PATCH 2/8] cdev: add diskuuid support Sascha Hauer
2022-02-07 9:49 ` [PATCH 3/8] rename cdev_open() -> cdev_open_name() Sascha Hauer
2022-02-07 10:43 ` Ahmad Fatoum
2022-02-07 9:49 ` [PATCH 4/8] cdev: implement cdev_open() Sascha Hauer
2022-02-07 10:46 ` Ahmad Fatoum
2022-02-07 9:49 ` [PATCH 5/8] driver: Add functions to free devices Sascha Hauer
2022-02-07 9:49 ` Sascha Hauer [this message]
2022-02-07 9:49 ` [PATCH 7/8] cdev: create iterator for cdev list Sascha Hauer
2022-02-07 9:49 ` [PATCH 8/8] misc: Add storage-by-uuid driver Sascha Hauer
2022-02-07 9:52 ` Sascha Hauer
2022-02-07 10:59 ` [PATCH] efi: probe devices from the device-tree Michael Olbrich
2022-02-08 9:29 ` [PATCH 8/8] misc: Add storage-by-uuid driver Michael Olbrich
2022-02-08 12:27 ` 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=20220207094953.949868-6-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=mol@pengutronix.de \
/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