null_blk.h 5.8 KB

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