blk-ia-ranges.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 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 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. &q->kobj, "%s", "independent_access_ranges");
  108. if (ret) {
  109. disk->ia_ranges = NULL;
  110. kobject_put(&iars->kobj);
  111. return ret;
  112. }
  113. for (i = 0; i < iars->nr_ia_ranges; i++) {
  114. ret = kobject_init_and_add(&iars->ia_range[i].kobj,
  115. &blk_ia_range_ktype, &iars->kobj,
  116. "%d", i);
  117. if (ret) {
  118. while (--i >= 0)
  119. kobject_del(&iars->ia_range[i].kobj);
  120. kobject_del(&iars->kobj);
  121. kobject_put(&iars->kobj);
  122. return ret;
  123. }
  124. }
  125. iars->sysfs_registered = true;
  126. return 0;
  127. }
  128. void disk_unregister_independent_access_ranges(struct gendisk *disk)
  129. {
  130. struct request_queue *q = disk->queue;
  131. struct blk_independent_access_ranges *iars = disk->ia_ranges;
  132. int i;
  133. lockdep_assert_held(&q->sysfs_dir_lock);
  134. lockdep_assert_held(&q->sysfs_lock);
  135. if (!iars)
  136. return;
  137. if (iars->sysfs_registered) {
  138. for (i = 0; i < iars->nr_ia_ranges; i++)
  139. kobject_del(&iars->ia_range[i].kobj);
  140. kobject_del(&iars->kobj);
  141. kobject_put(&iars->kobj);
  142. } else {
  143. kfree(iars);
  144. }
  145. disk->ia_ranges = NULL;
  146. }
  147. static struct blk_independent_access_range *
  148. disk_find_ia_range(struct blk_independent_access_ranges *iars,
  149. sector_t sector)
  150. {
  151. struct blk_independent_access_range *iar;
  152. int i;
  153. for (i = 0; i < iars->nr_ia_ranges; i++) {
  154. iar = &iars->ia_range[i];
  155. if (sector >= iar->sector &&
  156. sector < iar->sector + iar->nr_sectors)
  157. return iar;
  158. }
  159. return NULL;
  160. }
  161. static bool disk_check_ia_ranges(struct gendisk *disk,
  162. struct blk_independent_access_ranges *iars)
  163. {
  164. struct blk_independent_access_range *iar, *tmp;
  165. sector_t capacity = get_capacity(disk);
  166. sector_t sector = 0;
  167. int i;
  168. if (WARN_ON_ONCE(!iars->nr_ia_ranges))
  169. return false;
  170. /*
  171. * While sorting the ranges in increasing LBA order, check that the
  172. * ranges do not overlap, that there are no sector holes and that all
  173. * sectors belong to one range.
  174. */
  175. for (i = 0; i < iars->nr_ia_ranges; i++) {
  176. tmp = disk_find_ia_range(iars, sector);
  177. if (!tmp || tmp->sector != sector) {
  178. pr_warn("Invalid non-contiguous independent access ranges\n");
  179. return false;
  180. }
  181. iar = &iars->ia_range[i];
  182. if (tmp != iar) {
  183. swap(iar->sector, tmp->sector);
  184. swap(iar->nr_sectors, tmp->nr_sectors);
  185. }
  186. sector += iar->nr_sectors;
  187. }
  188. if (sector != capacity) {
  189. pr_warn("Independent access ranges do not match disk capacity\n");
  190. return false;
  191. }
  192. return true;
  193. }
  194. static bool disk_ia_ranges_changed(struct gendisk *disk,
  195. struct blk_independent_access_ranges *new)
  196. {
  197. struct blk_independent_access_ranges *old = disk->ia_ranges;
  198. int i;
  199. if (!old)
  200. return true;
  201. if (old->nr_ia_ranges != new->nr_ia_ranges)
  202. return true;
  203. for (i = 0; i < old->nr_ia_ranges; i++) {
  204. if (new->ia_range[i].sector != old->ia_range[i].sector ||
  205. new->ia_range[i].nr_sectors != old->ia_range[i].nr_sectors)
  206. return true;
  207. }
  208. return false;
  209. }
  210. /**
  211. * disk_alloc_independent_access_ranges - Allocate an independent access ranges
  212. * data structure
  213. * @disk: target disk
  214. * @nr_ia_ranges: Number of independent access ranges
  215. *
  216. * Allocate a struct blk_independent_access_ranges structure with @nr_ia_ranges
  217. * access range descriptors.
  218. */
  219. struct blk_independent_access_ranges *
  220. disk_alloc_independent_access_ranges(struct gendisk *disk, int nr_ia_ranges)
  221. {
  222. struct blk_independent_access_ranges *iars;
  223. iars = kzalloc_node(struct_size(iars, ia_range, nr_ia_ranges),
  224. GFP_KERNEL, disk->queue->node);
  225. if (iars)
  226. iars->nr_ia_ranges = nr_ia_ranges;
  227. return iars;
  228. }
  229. EXPORT_SYMBOL_GPL(disk_alloc_independent_access_ranges);
  230. /**
  231. * disk_set_independent_access_ranges - Set a disk independent access ranges
  232. * @disk: target disk
  233. * @iars: independent access ranges structure
  234. *
  235. * Set the independent access ranges information of the request queue
  236. * of @disk to @iars. If @iars is NULL and the independent access ranges
  237. * structure already set is cleared. If there are no differences between
  238. * @iars and the independent access ranges structure already set, @iars
  239. * is freed.
  240. */
  241. void disk_set_independent_access_ranges(struct gendisk *disk,
  242. struct blk_independent_access_ranges *iars)
  243. {
  244. struct request_queue *q = disk->queue;
  245. mutex_lock(&q->sysfs_dir_lock);
  246. mutex_lock(&q->sysfs_lock);
  247. if (iars && !disk_check_ia_ranges(disk, iars)) {
  248. kfree(iars);
  249. iars = NULL;
  250. }
  251. if (iars && !disk_ia_ranges_changed(disk, iars)) {
  252. kfree(iars);
  253. goto unlock;
  254. }
  255. /*
  256. * This may be called for a registered queue. E.g. during a device
  257. * revalidation. If that is the case, we need to unregister the old
  258. * set of independent access ranges and register the new set. If the
  259. * queue is not registered, registration of the device request queue
  260. * will register the independent access ranges.
  261. */
  262. disk_unregister_independent_access_ranges(disk);
  263. disk->ia_ranges = iars;
  264. if (blk_queue_registered(q))
  265. disk_register_independent_access_ranges(disk);
  266. unlock:
  267. mutex_unlock(&q->sysfs_lock);
  268. mutex_unlock(&q->sysfs_dir_lock);
  269. }
  270. EXPORT_SYMBOL_GPL(disk_set_independent_access_ranges);