blk-zoned.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Zoned block device handling
  3. *
  4. * Copyright (c) 2015, Hannes Reinecke
  5. * Copyright (c) 2015, SUSE Linux GmbH
  6. *
  7. * Copyright (c) 2016, Damien Le Moal
  8. * Copyright (c) 2016, Western Digital
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/rbtree.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/blk-mq.h>
  15. #include "blk.h"
  16. static inline sector_t blk_zone_start(struct request_queue *q,
  17. sector_t sector)
  18. {
  19. sector_t zone_mask = blk_queue_zone_sectors(q) - 1;
  20. return sector & ~zone_mask;
  21. }
  22. /*
  23. * Return true if a request is a write requests that needs zone write locking.
  24. */
  25. bool blk_req_needs_zone_write_lock(struct request *rq)
  26. {
  27. if (!rq->q->seq_zones_wlock)
  28. return false;
  29. if (blk_rq_is_passthrough(rq))
  30. return false;
  31. switch (req_op(rq)) {
  32. case REQ_OP_WRITE_ZEROES:
  33. case REQ_OP_WRITE_SAME:
  34. case REQ_OP_WRITE:
  35. return blk_rq_zone_is_seq(rq);
  36. default:
  37. return false;
  38. }
  39. }
  40. EXPORT_SYMBOL_GPL(blk_req_needs_zone_write_lock);
  41. void __blk_req_zone_write_lock(struct request *rq)
  42. {
  43. if (WARN_ON_ONCE(test_and_set_bit(blk_rq_zone_no(rq),
  44. rq->q->seq_zones_wlock)))
  45. return;
  46. WARN_ON_ONCE(rq->rq_flags & RQF_ZONE_WRITE_LOCKED);
  47. rq->rq_flags |= RQF_ZONE_WRITE_LOCKED;
  48. }
  49. EXPORT_SYMBOL_GPL(__blk_req_zone_write_lock);
  50. void __blk_req_zone_write_unlock(struct request *rq)
  51. {
  52. rq->rq_flags &= ~RQF_ZONE_WRITE_LOCKED;
  53. if (rq->q->seq_zones_wlock)
  54. WARN_ON_ONCE(!test_and_clear_bit(blk_rq_zone_no(rq),
  55. rq->q->seq_zones_wlock));
  56. }
  57. EXPORT_SYMBOL_GPL(__blk_req_zone_write_unlock);
  58. static inline unsigned int __blkdev_nr_zones(struct request_queue *q,
  59. sector_t nr_sectors)
  60. {
  61. unsigned long zone_sectors = blk_queue_zone_sectors(q);
  62. return (nr_sectors + zone_sectors - 1) >> ilog2(zone_sectors);
  63. }
  64. /**
  65. * blkdev_nr_zones - Get number of zones
  66. * @bdev: Target block device
  67. *
  68. * Description:
  69. * Return the total number of zones of a zoned block device.
  70. * For a regular block device, the number of zones is always 0.
  71. */
  72. unsigned int blkdev_nr_zones(struct block_device *bdev)
  73. {
  74. struct request_queue *q = bdev_get_queue(bdev);
  75. if (!blk_queue_is_zoned(q))
  76. return 0;
  77. return __blkdev_nr_zones(q, bdev->bd_part->nr_sects);
  78. }
  79. EXPORT_SYMBOL_GPL(blkdev_nr_zones);
  80. /*
  81. * Check that a zone report belongs to this partition, and if yes, fix its start
  82. * sector and write pointer and return true. Return false otherwise.
  83. */
  84. static bool blkdev_report_zone(struct block_device *bdev, struct blk_zone *rep)
  85. {
  86. sector_t offset = get_start_sect(bdev);
  87. if (rep->start < offset)
  88. return false;
  89. rep->start -= offset;
  90. if (rep->start + rep->len > bdev->bd_part->nr_sects)
  91. return false;
  92. if (rep->type == BLK_ZONE_TYPE_CONVENTIONAL)
  93. rep->wp = rep->start + rep->len;
  94. else
  95. rep->wp -= offset;
  96. return true;
  97. }
  98. static int blk_report_zones(struct gendisk *disk, sector_t sector,
  99. struct blk_zone *zones, unsigned int *nr_zones,
  100. gfp_t gfp_mask)
  101. {
  102. struct request_queue *q = disk->queue;
  103. unsigned int z = 0, n, nrz = *nr_zones;
  104. sector_t capacity = get_capacity(disk);
  105. int ret;
  106. while (z < nrz && sector < capacity) {
  107. n = nrz - z;
  108. ret = disk->fops->report_zones(disk, sector, &zones[z], &n,
  109. gfp_mask);
  110. if (ret)
  111. return ret;
  112. if (!n)
  113. break;
  114. sector += blk_queue_zone_sectors(q) * n;
  115. z += n;
  116. }
  117. WARN_ON(z > *nr_zones);
  118. *nr_zones = z;
  119. return 0;
  120. }
  121. /**
  122. * blkdev_report_zones - Get zones information
  123. * @bdev: Target block device
  124. * @sector: Sector from which to report zones
  125. * @zones: Array of zone structures where to return the zones information
  126. * @nr_zones: Number of zone structures in the zone array
  127. * @gfp_mask: Memory allocation flags (for bio_alloc)
  128. *
  129. * Description:
  130. * Get zone information starting from the zone containing @sector.
  131. * The number of zone information reported may be less than the number
  132. * requested by @nr_zones. The number of zones actually reported is
  133. * returned in @nr_zones.
  134. */
  135. int blkdev_report_zones(struct block_device *bdev, sector_t sector,
  136. struct blk_zone *zones, unsigned int *nr_zones,
  137. gfp_t gfp_mask)
  138. {
  139. struct request_queue *q = bdev_get_queue(bdev);
  140. unsigned int i, nrz;
  141. int ret;
  142. if (!blk_queue_is_zoned(q))
  143. return -EOPNOTSUPP;
  144. /*
  145. * A block device that advertized itself as zoned must have a
  146. * report_zones method. If it does not have one defined, the device
  147. * driver has a bug. So warn about that.
  148. */
  149. if (WARN_ON_ONCE(!bdev->bd_disk->fops->report_zones))
  150. return -EOPNOTSUPP;
  151. if (!*nr_zones || sector >= bdev->bd_part->nr_sects) {
  152. *nr_zones = 0;
  153. return 0;
  154. }
  155. nrz = min(*nr_zones,
  156. __blkdev_nr_zones(q, bdev->bd_part->nr_sects - sector));
  157. ret = blk_report_zones(bdev->bd_disk, get_start_sect(bdev) + sector,
  158. zones, &nrz, gfp_mask);
  159. if (ret)
  160. return ret;
  161. for (i = 0; i < nrz; i++) {
  162. if (!blkdev_report_zone(bdev, zones))
  163. break;
  164. zones++;
  165. }
  166. *nr_zones = i;
  167. return 0;
  168. }
  169. EXPORT_SYMBOL_GPL(blkdev_report_zones);
  170. /**
  171. * blkdev_reset_zones - Reset zones write pointer
  172. * @bdev: Target block device
  173. * @sector: Start sector of the first zone to reset
  174. * @nr_sectors: Number of sectors, at least the length of one zone
  175. * @gfp_mask: Memory allocation flags (for bio_alloc)
  176. *
  177. * Description:
  178. * Reset the write pointer of the zones contained in the range
  179. * @sector..@sector+@nr_sectors. Specifying the entire disk sector range
  180. * is valid, but the specified range should not contain conventional zones.
  181. */
  182. int blkdev_reset_zones(struct block_device *bdev,
  183. sector_t sector, sector_t nr_sectors,
  184. gfp_t gfp_mask)
  185. {
  186. struct request_queue *q = bdev_get_queue(bdev);
  187. sector_t zone_sectors;
  188. sector_t end_sector = sector + nr_sectors;
  189. struct bio *bio = NULL;
  190. struct blk_plug plug;
  191. int ret;
  192. if (!blk_queue_is_zoned(q))
  193. return -EOPNOTSUPP;
  194. if (bdev_read_only(bdev))
  195. return -EPERM;
  196. if (!nr_sectors || end_sector > bdev->bd_part->nr_sects)
  197. /* Out of range */
  198. return -EINVAL;
  199. /* Check alignment (handle eventual smaller last zone) */
  200. zone_sectors = blk_queue_zone_sectors(q);
  201. if (sector & (zone_sectors - 1))
  202. return -EINVAL;
  203. if ((nr_sectors & (zone_sectors - 1)) &&
  204. end_sector != bdev->bd_part->nr_sects)
  205. return -EINVAL;
  206. blk_start_plug(&plug);
  207. while (sector < end_sector) {
  208. bio = blk_next_bio(bio, 0, gfp_mask);
  209. bio->bi_iter.bi_sector = sector;
  210. bio_set_dev(bio, bdev);
  211. bio_set_op_attrs(bio, REQ_OP_ZONE_RESET, 0);
  212. sector += zone_sectors;
  213. /* This may take a while, so be nice to others */
  214. cond_resched();
  215. }
  216. ret = submit_bio_wait(bio);
  217. bio_put(bio);
  218. blk_finish_plug(&plug);
  219. return ret;
  220. }
  221. EXPORT_SYMBOL_GPL(blkdev_reset_zones);
  222. /*
  223. * BLKREPORTZONE ioctl processing.
  224. * Called from blkdev_ioctl.
  225. */
  226. int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
  227. unsigned int cmd, unsigned long arg)
  228. {
  229. void __user *argp = (void __user *)arg;
  230. struct request_queue *q;
  231. struct blk_zone_report rep;
  232. struct blk_zone *zones;
  233. int ret;
  234. if (!argp)
  235. return -EINVAL;
  236. q = bdev_get_queue(bdev);
  237. if (!q)
  238. return -ENXIO;
  239. if (!blk_queue_is_zoned(q))
  240. return -ENOTTY;
  241. if (!capable(CAP_SYS_ADMIN))
  242. return -EACCES;
  243. if (copy_from_user(&rep, argp, sizeof(struct blk_zone_report)))
  244. return -EFAULT;
  245. if (!rep.nr_zones)
  246. return -EINVAL;
  247. rep.nr_zones = min(blkdev_nr_zones(bdev), rep.nr_zones);
  248. zones = kvmalloc_array(rep.nr_zones, sizeof(struct blk_zone),
  249. GFP_KERNEL | __GFP_ZERO);
  250. if (!zones)
  251. return -ENOMEM;
  252. ret = blkdev_report_zones(bdev, rep.sector,
  253. zones, &rep.nr_zones,
  254. GFP_KERNEL);
  255. if (ret)
  256. goto out;
  257. if (copy_to_user(argp, &rep, sizeof(struct blk_zone_report))) {
  258. ret = -EFAULT;
  259. goto out;
  260. }
  261. if (rep.nr_zones) {
  262. if (copy_to_user(argp + sizeof(struct blk_zone_report), zones,
  263. sizeof(struct blk_zone) * rep.nr_zones))
  264. ret = -EFAULT;
  265. }
  266. out:
  267. kvfree(zones);
  268. return ret;
  269. }
  270. /*
  271. * BLKRESETZONE ioctl processing.
  272. * Called from blkdev_ioctl.
  273. */
  274. int blkdev_reset_zones_ioctl(struct block_device *bdev, fmode_t mode,
  275. unsigned int cmd, unsigned long arg)
  276. {
  277. void __user *argp = (void __user *)arg;
  278. struct request_queue *q;
  279. struct blk_zone_range zrange;
  280. if (!argp)
  281. return -EINVAL;
  282. q = bdev_get_queue(bdev);
  283. if (!q)
  284. return -ENXIO;
  285. if (!blk_queue_is_zoned(q))
  286. return -ENOTTY;
  287. if (!capable(CAP_SYS_ADMIN))
  288. return -EACCES;
  289. if (!(mode & FMODE_WRITE))
  290. return -EBADF;
  291. if (copy_from_user(&zrange, argp, sizeof(struct blk_zone_range)))
  292. return -EFAULT;
  293. return blkdev_reset_zones(bdev, zrange.sector, zrange.nr_sectors,
  294. GFP_KERNEL);
  295. }
  296. static inline unsigned long *blk_alloc_zone_bitmap(int node,
  297. unsigned int nr_zones)
  298. {
  299. return kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(unsigned long),
  300. GFP_NOIO, node);
  301. }
  302. /*
  303. * Allocate an array of struct blk_zone to get nr_zones zone information.
  304. * The allocated array may be smaller than nr_zones.
  305. */
  306. static struct blk_zone *blk_alloc_zones(int node, unsigned int *nr_zones)
  307. {
  308. size_t size = *nr_zones * sizeof(struct blk_zone);
  309. struct page *page;
  310. int order;
  311. for (order = get_order(size); order >= 0; order--) {
  312. page = alloc_pages_node(node, GFP_NOIO | __GFP_ZERO, order);
  313. if (page) {
  314. *nr_zones = min_t(unsigned int, *nr_zones,
  315. (PAGE_SIZE << order) / sizeof(struct blk_zone));
  316. return page_address(page);
  317. }
  318. }
  319. return NULL;
  320. }
  321. void blk_queue_free_zone_bitmaps(struct request_queue *q)
  322. {
  323. kfree(q->seq_zones_bitmap);
  324. q->seq_zones_bitmap = NULL;
  325. kfree(q->seq_zones_wlock);
  326. q->seq_zones_wlock = NULL;
  327. }
  328. /**
  329. * blk_revalidate_disk_zones - (re)allocate and initialize zone bitmaps
  330. * @disk: Target disk
  331. *
  332. * Helper function for low-level device drivers to (re) allocate and initialize
  333. * a disk request queue zone bitmaps. This functions should normally be called
  334. * within the disk ->revalidate method. For BIO based queues, no zone bitmap
  335. * is allocated.
  336. */
  337. int blk_revalidate_disk_zones(struct gendisk *disk)
  338. {
  339. struct request_queue *q = disk->queue;
  340. unsigned int nr_zones = __blkdev_nr_zones(q, get_capacity(disk));
  341. unsigned long *seq_zones_wlock = NULL, *seq_zones_bitmap = NULL;
  342. unsigned int i, rep_nr_zones = 0, z = 0, nrz;
  343. struct blk_zone *zones = NULL;
  344. sector_t sector = 0;
  345. int ret = 0;
  346. /*
  347. * BIO based queues do not use a scheduler so only q->nr_zones
  348. * needs to be updated so that the sysfs exposed value is correct.
  349. */
  350. if (!queue_is_rq_based(q)) {
  351. q->nr_zones = nr_zones;
  352. return 0;
  353. }
  354. if (!blk_queue_is_zoned(q) || !nr_zones) {
  355. nr_zones = 0;
  356. goto update;
  357. }
  358. /* Allocate bitmaps */
  359. ret = -ENOMEM;
  360. seq_zones_wlock = blk_alloc_zone_bitmap(q->node, nr_zones);
  361. if (!seq_zones_wlock)
  362. goto out;
  363. seq_zones_bitmap = blk_alloc_zone_bitmap(q->node, nr_zones);
  364. if (!seq_zones_bitmap)
  365. goto out;
  366. /* Get zone information and initialize seq_zones_bitmap */
  367. rep_nr_zones = nr_zones;
  368. zones = blk_alloc_zones(q->node, &rep_nr_zones);
  369. if (!zones)
  370. goto out;
  371. while (z < nr_zones) {
  372. nrz = min(nr_zones - z, rep_nr_zones);
  373. ret = blk_report_zones(disk, sector, zones, &nrz, GFP_NOIO);
  374. if (ret)
  375. goto out;
  376. if (!nrz)
  377. break;
  378. for (i = 0; i < nrz; i++) {
  379. if (zones[i].type != BLK_ZONE_TYPE_CONVENTIONAL)
  380. set_bit(z, seq_zones_bitmap);
  381. z++;
  382. }
  383. sector += nrz * blk_queue_zone_sectors(q);
  384. }
  385. if (WARN_ON(z != nr_zones)) {
  386. ret = -EIO;
  387. goto out;
  388. }
  389. update:
  390. /*
  391. * Install the new bitmaps, making sure the queue is stopped and
  392. * all I/Os are completed (i.e. a scheduler is not referencing the
  393. * bitmaps).
  394. */
  395. blk_mq_freeze_queue(q);
  396. q->nr_zones = nr_zones;
  397. swap(q->seq_zones_wlock, seq_zones_wlock);
  398. swap(q->seq_zones_bitmap, seq_zones_bitmap);
  399. blk_mq_unfreeze_queue(q);
  400. out:
  401. free_pages((unsigned long)zones,
  402. get_order(rep_nr_zones * sizeof(struct blk_zone)));
  403. kfree(seq_zones_wlock);
  404. kfree(seq_zones_bitmap);
  405. if (ret) {
  406. pr_warn("%s: failed to revalidate zones\n", disk->disk_name);
  407. blk_mq_freeze_queue(q);
  408. blk_queue_free_zone_bitmaps(q);
  409. blk_mq_unfreeze_queue(q);
  410. }
  411. return ret;
  412. }
  413. EXPORT_SYMBOL_GPL(blk_revalidate_disk_zones);