null_blk.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __BLK_NULL_BLK_H
  3. #define __BLK_NULL_BLK_H
  4. #undef pr_fmt
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/blkdev.h>
  7. #include <linux/slab.h>
  8. #include <linux/blk-mq.h>
  9. #include <linux/hrtimer.h>
  10. #include <linux/configfs.h>
  11. #include <linux/badblocks.h>
  12. #include <linux/fault-inject.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/mutex.h>
  15. struct nullb_cmd {
  16. union {
  17. struct request *rq;
  18. struct bio *bio;
  19. };
  20. unsigned int tag;
  21. blk_status_t error;
  22. bool fake_timeout;
  23. struct nullb_queue *nq;
  24. struct hrtimer timer;
  25. };
  26. struct nullb_queue {
  27. unsigned long *tag_map;
  28. wait_queue_head_t wait;
  29. unsigned int queue_depth;
  30. struct nullb_device *dev;
  31. unsigned int requeue_selection;
  32. struct list_head poll_list;
  33. spinlock_t poll_lock;
  34. struct nullb_cmd *cmds;
  35. };
  36. struct nullb_zone {
  37. /*
  38. * Zone lock to prevent concurrent modification of a zone write
  39. * pointer position and condition: with memory backing, a write
  40. * command execution may sleep on memory allocation. For this case,
  41. * use mutex as the zone lock. Otherwise, use the spinlock for
  42. * locking the zone.
  43. */
  44. union {
  45. spinlock_t spinlock;
  46. struct mutex mutex;
  47. };
  48. enum blk_zone_type type;
  49. enum blk_zone_cond cond;
  50. sector_t start;
  51. sector_t wp;
  52. unsigned int len;
  53. unsigned int capacity;
  54. };
  55. /* Queue modes */
  56. enum {
  57. NULL_Q_BIO = 0,
  58. NULL_Q_RQ = 1,
  59. NULL_Q_MQ = 2,
  60. };
  61. struct nullb_device {
  62. struct nullb *nullb;
  63. struct config_item item;
  64. struct radix_tree_root data; /* data stored in the disk */
  65. struct radix_tree_root cache; /* disk cache data */
  66. unsigned long flags; /* device flags */
  67. unsigned int curr_cache;
  68. struct badblocks badblocks;
  69. unsigned int nr_zones;
  70. unsigned int nr_zones_imp_open;
  71. unsigned int nr_zones_exp_open;
  72. unsigned int nr_zones_closed;
  73. unsigned int imp_close_zone_no;
  74. struct nullb_zone *zones;
  75. sector_t zone_size_sects;
  76. bool need_zone_res_mgmt;
  77. spinlock_t zone_res_lock;
  78. unsigned long size; /* device size in MB */
  79. unsigned long completion_nsec; /* time in ns to complete a request */
  80. unsigned long cache_size; /* disk cache size in MB */
  81. unsigned long zone_size; /* zone size in MB if device is zoned */
  82. unsigned long zone_capacity; /* zone capacity in MB if device is zoned */
  83. unsigned int zone_nr_conv; /* number of conventional zones */
  84. unsigned int zone_max_open; /* max number of open zones */
  85. unsigned int zone_max_active; /* max number of active zones */
  86. unsigned int submit_queues; /* number of submission queues */
  87. unsigned int prev_submit_queues; /* number of submission queues before change */
  88. unsigned int poll_queues; /* number of IOPOLL submission queues */
  89. unsigned int prev_poll_queues; /* number of IOPOLL submission queues before change */
  90. unsigned int home_node; /* home node for the device */
  91. unsigned int queue_mode; /* block interface */
  92. unsigned int blocksize; /* block size */
  93. unsigned int max_sectors; /* Max sectors per command */
  94. unsigned int irqmode; /* IRQ completion handler */
  95. unsigned int hw_queue_depth; /* queue depth */
  96. unsigned int index; /* index of the disk, only valid with a disk */
  97. unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */
  98. bool blocking; /* blocking blk-mq device */
  99. bool use_per_node_hctx; /* use per-node allocation for hardware context */
  100. bool power; /* power on/off the device */
  101. bool memory_backed; /* if data is stored in memory */
  102. bool discard; /* if support discard */
  103. bool zoned; /* if device is zoned */
  104. bool virt_boundary; /* virtual boundary on/off for the device */
  105. bool no_sched; /* no IO scheduler for the device */
  106. bool shared_tag_bitmap; /* use hostwide shared tags */
  107. };
  108. struct nullb {
  109. struct nullb_device *dev;
  110. struct list_head list;
  111. unsigned int index;
  112. struct request_queue *q;
  113. struct gendisk *disk;
  114. struct blk_mq_tag_set *tag_set;
  115. struct blk_mq_tag_set __tag_set;
  116. unsigned int queue_depth;
  117. atomic_long_t cur_bytes;
  118. struct hrtimer bw_timer;
  119. unsigned long cache_flush_pos;
  120. spinlock_t lock;
  121. struct nullb_queue *queues;
  122. unsigned int nr_queues;
  123. char disk_name[DISK_NAME_LEN];
  124. };
  125. blk_status_t null_handle_discard(struct nullb_device *dev, sector_t sector,
  126. sector_t nr_sectors);
  127. blk_status_t null_process_cmd(struct nullb_cmd *cmd, enum req_op op,
  128. sector_t sector, unsigned int nr_sectors);
  129. #ifdef CONFIG_BLK_DEV_ZONED
  130. int null_init_zoned_dev(struct nullb_device *dev, struct request_queue *q);
  131. int null_register_zoned_dev(struct nullb *nullb);
  132. void null_free_zoned_dev(struct nullb_device *dev);
  133. int null_report_zones(struct gendisk *disk, sector_t sector,
  134. unsigned int nr_zones, report_zones_cb cb, void *data);
  135. blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd, enum req_op op,
  136. sector_t sector, sector_t nr_sectors);
  137. size_t null_zone_valid_read_len(struct nullb *nullb,
  138. sector_t sector, unsigned int len);
  139. #else
  140. static inline int null_init_zoned_dev(struct nullb_device *dev,
  141. struct request_queue *q)
  142. {
  143. pr_err("CONFIG_BLK_DEV_ZONED not enabled\n");
  144. return -EINVAL;
  145. }
  146. static inline int null_register_zoned_dev(struct nullb *nullb)
  147. {
  148. return -ENODEV;
  149. }
  150. static inline void null_free_zoned_dev(struct nullb_device *dev) {}
  151. static inline blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd,
  152. enum req_op op, sector_t sector, sector_t nr_sectors)
  153. {
  154. return BLK_STS_NOTSUPP;
  155. }
  156. static inline size_t null_zone_valid_read_len(struct nullb *nullb,
  157. sector_t sector,
  158. unsigned int len)
  159. {
  160. return len;
  161. }
  162. #define null_report_zones NULL
  163. #endif /* CONFIG_BLK_DEV_ZONED */
  164. #endif /* __NULL_BLK_H */