1 // SPDX-License-Identifier: GPL-2.0
3 * Block device concurrent positioning ranges.
5 * Copyright (C) 2021 Western Digital Corporation or its Affiliates.
7 #include <linux/kernel.h>
8 #include <linux/blkdev.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
15 blk_ia_range_sector_show(struct blk_independent_access_range *iar,
18 return sprintf(buf, "%llu\n", iar->sector);
22 blk_ia_range_nr_sectors_show(struct blk_independent_access_range *iar,
25 return sprintf(buf, "%llu\n", iar->nr_sectors);
28 struct blk_ia_range_sysfs_entry {
29 struct attribute attr;
30 ssize_t (*show)(struct blk_independent_access_range *iar, char *buf);
33 static struct blk_ia_range_sysfs_entry blk_ia_range_sector_entry = {
34 .attr = { .name = "sector", .mode = 0444 },
35 .show = blk_ia_range_sector_show,
38 static struct blk_ia_range_sysfs_entry blk_ia_range_nr_sectors_entry = {
39 .attr = { .name = "nr_sectors", .mode = 0444 },
40 .show = blk_ia_range_nr_sectors_show,
43 static struct attribute *blk_ia_range_attrs[] = {
44 &blk_ia_range_sector_entry.attr,
45 &blk_ia_range_nr_sectors_entry.attr,
48 ATTRIBUTE_GROUPS(blk_ia_range);
50 static ssize_t blk_ia_range_sysfs_show(struct kobject *kobj,
51 struct attribute *attr, char *buf)
53 struct blk_ia_range_sysfs_entry *entry =
54 container_of(attr, struct blk_ia_range_sysfs_entry, attr);
55 struct blk_independent_access_range *iar =
56 container_of(kobj, struct blk_independent_access_range, kobj);
58 return entry->show(iar, buf);
61 static const struct sysfs_ops blk_ia_range_sysfs_ops = {
62 .show = blk_ia_range_sysfs_show,
66 * Independent access range entries are not freed individually, but alltogether
67 * with struct blk_independent_access_ranges and its array of ranges. Since
68 * kobject_add() takes a reference on the parent kobject contained in
69 * struct blk_independent_access_ranges, the array of independent access range
70 * entries cannot be freed until kobject_del() is called for all entries.
71 * So we do not need to do anything here, but still need this no-op release
72 * operation to avoid complaints from the kobject code.
74 static void blk_ia_range_sysfs_nop_release(struct kobject *kobj)
78 static struct kobj_type blk_ia_range_ktype = {
79 .sysfs_ops = &blk_ia_range_sysfs_ops,
80 .default_groups = blk_ia_range_groups,
81 .release = blk_ia_range_sysfs_nop_release,
85 * This will be executed only after all independent access range entries are
86 * removed with kobject_del(), at which point, it is safe to free everything,
87 * including the array of ranges.
89 static void blk_ia_ranges_sysfs_release(struct kobject *kobj)
91 struct blk_independent_access_ranges *iars =
92 container_of(kobj, struct blk_independent_access_ranges, kobj);
97 static struct kobj_type blk_ia_ranges_ktype = {
98 .release = blk_ia_ranges_sysfs_release,
102 * disk_register_independent_access_ranges - register with sysfs a set of
103 * independent access ranges
105 * @new_iars: New set of independent access ranges
107 * Register with sysfs a set of independent access ranges for @disk.
108 * If @new_iars is not NULL, this set of ranges is registered and the old set
109 * specified by q->ia_ranges is unregistered. Otherwise, q->ia_ranges is
110 * registered if it is not already.
112 int disk_register_independent_access_ranges(struct gendisk *disk,
113 struct blk_independent_access_ranges *new_iars)
115 struct request_queue *q = disk->queue;
116 struct blk_independent_access_ranges *iars;
119 lockdep_assert_held(&q->sysfs_dir_lock);
120 lockdep_assert_held(&q->sysfs_lock);
122 /* If a new range set is specified, unregister the old one */
125 disk_unregister_independent_access_ranges(disk);
126 q->ia_ranges = new_iars;
134 * At this point, iars is the new set of sector access ranges that needs
135 * to be registered with sysfs.
137 WARN_ON(iars->sysfs_registered);
138 ret = kobject_init_and_add(&iars->kobj, &blk_ia_ranges_ktype,
139 &q->kobj, "%s", "independent_access_ranges");
142 kobject_put(&iars->kobj);
146 for (i = 0; i < iars->nr_ia_ranges; i++) {
147 iars->ia_range[i].queue = q;
148 ret = kobject_init_and_add(&iars->ia_range[i].kobj,
149 &blk_ia_range_ktype, &iars->kobj,
153 kobject_del(&iars->ia_range[i].kobj);
154 kobject_del(&iars->kobj);
155 kobject_put(&iars->kobj);
160 iars->sysfs_registered = true;
165 void disk_unregister_independent_access_ranges(struct gendisk *disk)
167 struct request_queue *q = disk->queue;
168 struct blk_independent_access_ranges *iars = q->ia_ranges;
171 lockdep_assert_held(&q->sysfs_dir_lock);
172 lockdep_assert_held(&q->sysfs_lock);
177 if (iars->sysfs_registered) {
178 for (i = 0; i < iars->nr_ia_ranges; i++)
179 kobject_del(&iars->ia_range[i].kobj);
180 kobject_del(&iars->kobj);
181 kobject_put(&iars->kobj);
189 static struct blk_independent_access_range *
190 disk_find_ia_range(struct blk_independent_access_ranges *iars,
193 struct blk_independent_access_range *iar;
196 for (i = 0; i < iars->nr_ia_ranges; i++) {
197 iar = &iars->ia_range[i];
198 if (sector >= iar->sector &&
199 sector < iar->sector + iar->nr_sectors)
206 static bool disk_check_ia_ranges(struct gendisk *disk,
207 struct blk_independent_access_ranges *iars)
209 struct blk_independent_access_range *iar, *tmp;
210 sector_t capacity = get_capacity(disk);
215 * While sorting the ranges in increasing LBA order, check that the
216 * ranges do not overlap, that there are no sector holes and that all
217 * sectors belong to one range.
219 for (i = 0; i < iars->nr_ia_ranges; i++) {
220 tmp = disk_find_ia_range(iars, sector);
221 if (!tmp || tmp->sector != sector) {
222 pr_warn("Invalid non-contiguous independent access ranges\n");
226 iar = &iars->ia_range[i];
228 swap(iar->sector, tmp->sector);
229 swap(iar->nr_sectors, tmp->nr_sectors);
232 sector += iar->nr_sectors;
235 if (sector != capacity) {
236 pr_warn("Independent access ranges do not match disk capacity\n");
243 static bool disk_ia_ranges_changed(struct gendisk *disk,
244 struct blk_independent_access_ranges *new)
246 struct blk_independent_access_ranges *old = disk->queue->ia_ranges;
252 if (old->nr_ia_ranges != new->nr_ia_ranges)
255 for (i = 0; i < old->nr_ia_ranges; i++) {
256 if (new->ia_range[i].sector != old->ia_range[i].sector ||
257 new->ia_range[i].nr_sectors != old->ia_range[i].nr_sectors)
265 * disk_alloc_independent_access_ranges - Allocate an independent access ranges
268 * @nr_ia_ranges: Number of independent access ranges
270 * Allocate a struct blk_independent_access_ranges structure with @nr_ia_ranges
271 * access range descriptors.
273 struct blk_independent_access_ranges *
274 disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges)
276 struct blk_independent_access_ranges *iars;
278 iars = kzalloc_node(struct_size(iars, ia_range, nr_ia_ranges),
279 GFP_KERNEL, disk->queue->node);
281 iars->nr_ia_ranges = nr_ia_ranges;
284 EXPORT_SYMBOL_GPL(disk_alloc_independent_access_ranges);
287 * disk_set_independent_access_ranges - Set a disk independent access ranges
289 * @iars: independent access ranges structure
291 * Set the independent access ranges information of the request queue
292 * of @disk to @iars. If @iars is NULL and the independent access ranges
293 * structure already set is cleared. If there are no differences between
294 * @iars and the independent access ranges structure already set, @iars
297 void disk_set_independent_access_ranges(struct gendisk *disk,
298 struct blk_independent_access_ranges *iars)
300 struct request_queue *q = disk->queue;
302 if (WARN_ON_ONCE(iars && !iars->nr_ia_ranges)) {
307 mutex_lock(&q->sysfs_dir_lock);
308 mutex_lock(&q->sysfs_lock);
311 if (!disk_check_ia_ranges(disk, iars)) {
317 if (!disk_ia_ranges_changed(disk, iars)) {
324 * This may be called for a registered queue. E.g. during a device
325 * revalidation. If that is the case, we need to unregister the old
326 * set of independent access ranges and register the new set. If the
327 * queue is not registered, registration of the device request queue
328 * will register the independent access ranges, so only swap in the
329 * new set and free the old one.
332 if (blk_queue_registered(q)) {
333 disk_register_independent_access_ranges(disk, iars);
335 swap(q->ia_ranges, iars);
340 mutex_unlock(&q->sysfs_lock);
341 mutex_unlock(&q->sysfs_dir_lock);
343 EXPORT_SYMBOL_GPL(disk_set_independent_access_ranges);