blk-ia-ranges.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Block device concurrent positioning ranges.
  4. *
  5. * Copyright (C) 2021 Western Digital Corporation or its Affiliates.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include "blk.h"
  12. static ssize_t
  13. blk_ia_range_sector_show(struct blk_independent_access_range *iar,
  14. char *buf)
  15. {
  16. return sprintf(buf, "%llu\n", iar->sector);
  17. }
  18. static ssize_t
  19. blk_ia_range_nr_sectors_show(struct blk_independent_access_range *iar,
  20. char *buf)
  21. {
  22. return sprintf(buf, "%llu\n", iar->nr_sectors);
  23. }
  24. struct blk_ia_range_sysfs_entry {
  25. struct attribute attr;
  26. ssize_t (*show)(struct blk_independent_access_range *iar, char *buf);
  27. };
  28. static struct blk_ia_range_sysfs_entry blk_ia_range_sector_entry = {
  29. .attr = { .name = "sector", .mode = 0444 },
  30. .show = blk_ia_range_sector_show,
  31. };
  32. static struct blk_ia_range_sysfs_entry blk_ia_range_nr_sectors_entry = {
  33. .attr = { .name = "nr_sectors", .mode = 0444 },
  34. .show = blk_ia_range_nr_sectors_show,
  35. };
  36. static struct attribute *blk_ia_range_attrs[] = {
  37. &blk_ia_range_sector_entry.attr,
  38. &blk_ia_range_nr_sectors_entry.attr,
  39. NULL,
  40. };
  41. ATTRIBUTE_GROUPS(blk_ia_range);
  42. static ssize_t blk_ia_range_sysfs_show(struct kobject *kobj,
  43. struct attribute *attr, char *buf)
  44. {
  45. struct blk_ia_range_sysfs_entry *entry =
  46. container_of(attr, struct blk_ia_range_sysfs_entry, attr);
  47. struct blk_independent_access_range *iar =
  48. container_of(kobj, struct blk_independent_access_range, kobj);
  49. return entry->show(iar, buf);
  50. }
  51. static const struct sysfs_ops blk_ia_range_sysfs_ops = {
  52. .show = blk_ia_range_sysfs_show,
  53. };
  54. /*
  55. * Independent access range entries are not freed individually, but alltogether
  56. * with struct blk_independent_access_ranges and its array of ranges. Since
  57. * kobject_add() takes a reference on the parent kobject contained in
  58. * struct blk_independent_access_ranges, the array of independent access range
  59. * entries cannot be freed until kobject_del() is called for all entries.
  60. * So we do not need to do anything here, but still need this no-op release
  61. * operation to avoid complaints from the kobject code.
  62. */
  63. static void blk_ia_range_sysfs_nop_release(struct kobject *kobj)
  64. {
  65. }
  66. static const struct kobj_type blk_ia_range_ktype = {
  67. .sysfs_ops = &blk_ia_range_sysfs_ops,
  68. .default_groups = blk_ia_range_groups,
  69. .release = blk_ia_range_sysfs_nop_release,
  70. };
  71. /*
  72. * This will be executed only after all independent access range entries are
  73. * removed with kobject_del(), at which point, it is safe to free everything,
  74. * including the array of ranges.
  75. */
  76. static void blk_ia_ranges_sysfs_release(struct kobject *kobj)
  77. {
  78. struct blk_independent_access_ranges *iars =
  79. container_of(kobj, struct blk_independent_access_ranges, kobj);
  80. kfree(iars);
  81. }
  82. static const struct kobj_type blk_ia_ranges_ktype = {
  83. .release = blk_ia_ranges_sysfs_release,
  84. };
  85. /**
  86. * disk_register_independent_access_ranges - register with sysfs a set of
  87. * independent access ranges
  88. * @disk: Target disk
  89. *
  90. * Register with sysfs a set of independent access ranges for @disk.
  91. */
  92. int disk_register_independent_access_ranges(struct gendisk *disk)
  93. {
  94. struct blk_independent_access_ranges *iars = disk->ia_ranges;
  95. struct request_queue *q = disk->queue;
  96. int i, ret;
  97. lockdep_assert_held(&q->sysfs_dir_lock);
  98. lockdep_assert_held(&q->sysfs_lock);
  99. if (!iars)
  100. return 0;
  101. /*
  102. * At this point, iars is the new set of sector access ranges that needs
  103. * to be registered with sysfs.
  104. */
  105. WARN_ON(iars->sysfs_registered);
  106. ret = kobject_init_and_add(&iars->kobj, &blk_ia_ranges_ktype,
  107. &disk->queue_kobj, "%s",
  108. "independent_access_ranges");
  109. if (ret) {
  110. disk->ia_ranges = NULL;
  111. kobject_put(&iars->kobj);
  112. return ret;
  113. }
  114. for (i = 0; i < iars->nr_ia_ranges; i++) {
  115. ret = kobject_init_and_add(&iars->ia_range[i].kobj,
  116. &blk_ia_range_ktype, &iars->kobj,
  117. "%d", i);
  118. if (ret) {
  119. while (--i >= 0)
  120. kobject_del(&iars->ia_range[i].kobj);
  121. kobject_del(&iars->kobj);
  122. kobject_put(&iars->kobj);
  123. return ret;
  124. }
  125. }
  126. iars->sysfs_registered = true;
  127. return 0;
  128. }
  129. void disk_unregister_independent_access_ranges(struct gendisk *disk)
  130. {
  131. struct request_queue *q = disk->queue;
  132. struct blk_independent_access_ranges *iars = disk->ia_ranges;
  133. int i;
  134. lockdep_assert_held(&q->sysfs_dir_lock);
  135. lockdep_assert_held(&q->sysfs_lock);
  136. if (!iars)
  137. return;
  138. if (iars->sysfs_registered) {
  139. for (i = 0; i < iars->nr_ia_ranges; i++)
  140. kobject_del(&iars->ia_range[i].kobj);
  141. kobject_del(&iars->kobj);
  142. kobject_put(&iars->kobj);
  143. } else {
  144. kfree(iars);
  145. }
  146. disk->ia_ranges = NULL;
  147. }
  148. static struct blk_independent_access_range *
  149. disk_find_ia_range(struct blk_independent_access_ranges *iars,
  150. sector_t sector)
  151. {
  152. struct blk_independent_access_range *iar;
  153. int i;
  154. for (i = 0; i < iars->nr_ia_ranges; i++) {
  155. iar = &iars->ia_range[i];
  156. if (sector >= iar->sector &&
  157. sector < iar->sector + iar->nr_sectors)
  158. return iar;
  159. }
  160. return NULL;
  161. }
  162. static bool disk_check_ia_ranges(struct gendisk *disk,
  163. struct blk_independent_access_ranges *iars)
  164. {
  165. struct blk_independent_access_range *iar, *tmp;
  166. sector_t capacity = get_capacity(disk);
  167. sector_t sector = 0;
  168. int i;
  169. if (WARN_ON_ONCE(!iars->nr_ia_ranges))
  170. return false;
  171. /*
  172. * While sorting the ranges in increasing LBA order, check that the
  173. * ranges do not overlap, that there are no sector holes and that all
  174. * sectors belong to one range.
  175. */
  176. for (i = 0; i < iars->nr_ia_ranges; i++) {
  177. tmp = disk_find_ia_range(iars, sector);
  178. if (!tmp || tmp->sector != sector) {
  179. pr_warn("Invalid non-contiguous independent access ranges\n");
  180. return false;
  181. }
  182. iar = &iars->ia_range[i];
  183. if (tmp != iar) {
  184. swap(iar->sector, tmp->sector);
  185. swap(iar->nr_sectors, tmp->nr_sectors);
  186. }
  187. sector += iar->nr_sectors;
  188. }
  189. if (sector != capacity) {
  190. pr_warn("Independent access ranges do not match disk capacity\n");
  191. return false;
  192. }
  193. return true;
  194. }
  195. static bool disk_ia_ranges_changed(struct gendisk *disk,
  196. struct blk_independent_access_ranges *new)
  197. {
  198. struct blk_independent_access_ranges *old = disk->ia_ranges;
  199. int i;
  200. if (!old)
  201. return true;
  202. if (old->nr_ia_ranges != new->nr_ia_ranges)
  203. return true;
  204. for (i = 0; i < old->nr_ia_ranges; i++) {
  205. if (new->ia_range[i].sector != old->ia_range[i].sector ||
  206. new->ia_range[i].nr_sectors != old->ia_range[i].nr_sectors)
  207. return true;
  208. }
  209. return false;
  210. }
  211. /**
  212. * disk_alloc_independent_access_ranges - Allocate an independent access ranges
  213. * data structure
  214. * @disk: target disk
  215. * @nr_ia_ranges: Number of independent access ranges
  216. *
  217. * Allocate a struct blk_independent_access_ranges structure with @nr_ia_ranges
  218. * access range descriptors.
  219. */
  220. struct blk_independent_access_ranges *
  221. disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges)
  222. {
  223. struct blk_independent_access_ranges *iars;
  224. iars = kzalloc_node(struct_size(iars, ia_range, nr_ia_ranges),
  225. GFP_KERNEL, disk->queue->node);
  226. if (iars)
  227. iars->nr_ia_ranges = nr_ia_ranges;
  228. return iars;
  229. }
  230. EXPORT_SYMBOL_GPL(disk_alloc_independent_access_ranges);
  231. /**
  232. * disk_set_independent_access_ranges - Set a disk independent access ranges
  233. * @disk: target disk
  234. * @iars: independent access ranges structure
  235. *
  236. * Set the independent access ranges information of the request queue
  237. * of @disk to @iars. If @iars is NULL and the independent access ranges
  238. * structure already set is cleared. If there are no differences between
  239. * @iars and the independent access ranges structure already set, @iars
  240. * is freed.
  241. */
  242. void disk_set_independent_access_ranges(struct gendisk *disk,
  243. struct blk_independent_access_ranges *iars)
  244. {
  245. struct request_queue *q = disk->queue;
  246. mutex_lock(&q->sysfs_dir_lock);
  247. mutex_lock(&q->sysfs_lock);
  248. if (iars && !disk_check_ia_ranges(disk, iars)) {
  249. kfree(iars);
  250. iars = NULL;
  251. }
  252. if (iars && !disk_ia_ranges_changed(disk, iars)) {
  253. kfree(iars);
  254. goto unlock;
  255. }
  256. /*
  257. * This may be called for a registered queue. E.g. during a device
  258. * revalidation. If that is the case, we need to unregister the old
  259. * set of independent access ranges and register the new set. If the
  260. * queue is not registered, registration of the device request queue
  261. * will register the independent access ranges.
  262. */
  263. disk_unregister_independent_access_ranges(disk);
  264. disk->ia_ranges = iars;
  265. if (blk_queue_registered(q))
  266. disk_register_independent_access_ranges(disk);
  267. unlock:
  268. mutex_unlock(&q->sysfs_lock);
  269. mutex_unlock(&q->sysfs_dir_lock);
  270. }
  271. EXPORT_SYMBOL_GPL(disk_set_independent_access_ranges);