[PATCH 1/3] block: Add invalidate_gendisk() helper to invalidate the gendisk
To hide internal implementation and simplify some driver code,
this adds a helper to invalidate the gendisk. It will clean the
gendisk's associated buffer/page caches and reset its internal
states.
Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
---
block/genhd.c | 21 +++++++++++++++++++++
include/linux/genhd.h | 1 +
2 files changed, 22 insertions(+)
diff --git a/block/genhd.c b/block/genhd.c
index 7b6e5e1cf956..7d97fb93069a 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -601,6 +601,27 @@ void del_gendisk(struct gendisk *disk)
}
EXPORT_SYMBOL(del_gendisk);
+/**
+ * invalidate_gendisk - invalidate the gendisk
+ * @disk: the struct gendisk to invalidate
+ *
+ * A helper to invalidates the gendisk. It will clean the gendisk's associated
+ * buffer/page caches and reset its internal states so that the gendisk
+ * can be reused by the drivers.
+ *
+ * Context: can sleep
+ */
+
+void invalidate_gendisk(struct gendisk *disk)
+{
+ struct block_device *bdev = disk->part0;
+
+ invalidate_bdev(bdev);
+ bdev->bd_inode->i_mapping->wb_err = 0;
+ set_capacity(disk, 0);
+}
+EXPORT_SYMBOL(invalidate_gendisk);
+
/* sysfs access to bad-blocks list. */
static ssize_t disk_badblocks_show(struct device *dev,
struct device_attribute *attr,
diff --git a/include/linux/genhd.h b/include/linux/genhd.h
index c68d83c87f83..a3b2f6b1b7e8 100644
--- a/include/linux/genhd.h
+++ b/include/linux/genhd.h
@@ -221,6 +221,7 @@ static inline int add_disk(struct gendisk *disk)
return device_add_disk(NULL, disk, NULL);
}
extern void del_gendisk(struct gendisk *gp);
+extern void invalidate_gendisk(struct gendisk *gp);
void set_disk_ro(struct gendisk *disk, bool read_only);
--
2.11.0
Reply to: