null_blk_zoned.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/vmalloc.h>
  3. #include "null_blk.h"
  4. /* zone_size in MBs to sectors. */
  5. #define ZONE_SIZE_SHIFT 11
  6. static inline unsigned int null_zone_no(struct nullb_device *dev, sector_t sect)
  7. {
  8. return sect >> ilog2(dev->zone_size_sects);
  9. }
  10. int null_zone_init(struct nullb_device *dev)
  11. {
  12. sector_t dev_size = (sector_t)dev->size * 1024 * 1024;
  13. sector_t sector = 0;
  14. unsigned int i;
  15. if (!is_power_of_2(dev->zone_size)) {
  16. pr_err("null_blk: zone_size must be power-of-two\n");
  17. return -EINVAL;
  18. }
  19. dev->zone_size_sects = dev->zone_size << ZONE_SIZE_SHIFT;
  20. dev->nr_zones = dev_size >>
  21. (SECTOR_SHIFT + ilog2(dev->zone_size_sects));
  22. dev->zones = kvmalloc_array(dev->nr_zones, sizeof(struct blk_zone),
  23. GFP_KERNEL | __GFP_ZERO);
  24. if (!dev->zones)
  25. return -ENOMEM;
  26. for (i = 0; i < dev->nr_zones; i++) {
  27. struct blk_zone *zone = &dev->zones[i];
  28. zone->start = zone->wp = sector;
  29. zone->len = dev->zone_size_sects;
  30. zone->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
  31. zone->cond = BLK_ZONE_COND_EMPTY;
  32. sector += dev->zone_size_sects;
  33. }
  34. return 0;
  35. }
  36. void null_zone_exit(struct nullb_device *dev)
  37. {
  38. kvfree(dev->zones);
  39. }
  40. static void null_zone_fill_bio(struct nullb_device *dev, struct bio *bio,
  41. unsigned int zno, unsigned int nr_zones)
  42. {
  43. struct blk_zone_report_hdr *hdr = NULL;
  44. struct bio_vec bvec;
  45. struct bvec_iter iter;
  46. void *addr;
  47. unsigned int zones_to_cpy;
  48. bio_for_each_segment(bvec, bio, iter) {
  49. addr = kmap_atomic(bvec.bv_page);
  50. zones_to_cpy = bvec.bv_len / sizeof(struct blk_zone);
  51. if (!hdr) {
  52. hdr = (struct blk_zone_report_hdr *)addr;
  53. hdr->nr_zones = nr_zones;
  54. zones_to_cpy--;
  55. addr += sizeof(struct blk_zone_report_hdr);
  56. }
  57. zones_to_cpy = min_t(unsigned int, zones_to_cpy, nr_zones);
  58. memcpy(addr, &dev->zones[zno],
  59. zones_to_cpy * sizeof(struct blk_zone));
  60. kunmap_atomic(addr);
  61. nr_zones -= zones_to_cpy;
  62. zno += zones_to_cpy;
  63. if (!nr_zones)
  64. break;
  65. }
  66. }
  67. blk_status_t null_zone_report(struct nullb *nullb, struct bio *bio)
  68. {
  69. struct nullb_device *dev = nullb->dev;
  70. unsigned int zno = null_zone_no(dev, bio->bi_iter.bi_sector);
  71. unsigned int nr_zones = dev->nr_zones - zno;
  72. unsigned int max_zones;
  73. max_zones = (bio->bi_iter.bi_size / sizeof(struct blk_zone)) - 1;
  74. nr_zones = min_t(unsigned int, nr_zones, max_zones);
  75. null_zone_fill_bio(nullb->dev, bio, zno, nr_zones);
  76. return BLK_STS_OK;
  77. }
  78. void null_zone_write(struct nullb_cmd *cmd, sector_t sector,
  79. unsigned int nr_sectors)
  80. {
  81. struct nullb_device *dev = cmd->nq->dev;
  82. unsigned int zno = null_zone_no(dev, sector);
  83. struct blk_zone *zone = &dev->zones[zno];
  84. switch (zone->cond) {
  85. case BLK_ZONE_COND_FULL:
  86. /* Cannot write to a full zone */
  87. cmd->error = BLK_STS_IOERR;
  88. break;
  89. case BLK_ZONE_COND_EMPTY:
  90. case BLK_ZONE_COND_IMP_OPEN:
  91. /* Writes must be at the write pointer position */
  92. if (sector != zone->wp) {
  93. cmd->error = BLK_STS_IOERR;
  94. break;
  95. }
  96. if (zone->cond == BLK_ZONE_COND_EMPTY)
  97. zone->cond = BLK_ZONE_COND_IMP_OPEN;
  98. zone->wp += nr_sectors;
  99. if (zone->wp == zone->start + zone->len)
  100. zone->cond = BLK_ZONE_COND_FULL;
  101. break;
  102. default:
  103. /* Invalid zone condition */
  104. cmd->error = BLK_STS_IOERR;
  105. break;
  106. }
  107. }
  108. void null_zone_reset(struct nullb_cmd *cmd, sector_t sector)
  109. {
  110. struct nullb_device *dev = cmd->nq->dev;
  111. unsigned int zno = null_zone_no(dev, sector);
  112. struct blk_zone *zone = &dev->zones[zno];
  113. zone->cond = BLK_ZONE_COND_EMPTY;
  114. zone->wp = zone->start;
  115. }