null_blk.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. bool badblocks_once;
  60. bool badblocks_partial_io;
  61. unsigned int nr_zones;
  62. unsigned int nr_zones_imp_open;
  63. unsigned int nr_zones_exp_open;
  64. unsigned int nr_zones_closed;
  65. unsigned int imp_close_zone_no;
  66. struct nullb_zone *zones;
  67. sector_t zone_size_sects;
  68. bool need_zone_res_mgmt;
  69. spinlock_t zone_res_lock;
  70. unsigned long size; /* device size in MB */
  71. unsigned long completion_nsec; /* time in ns to complete a request */
  72. unsigned long cache_size; /* disk cache size in MB */
  73. unsigned long zone_size; /* zone size in MB if device is zoned */
  74. unsigned long zone_capacity; /* zone capacity in MB if device is zoned */
  75. unsigned int zone_nr_conv; /* number of conventional zones */
  76. unsigned int zone_max_open; /* max number of open zones */
  77. unsigned int zone_max_active; /* max number of active zones */
  78. unsigned int zone_append_max_sectors; /* Max sectors per zone append command */
  79. unsigned int submit_queues; /* number of submission queues */
  80. unsigned int prev_submit_queues; /* number of submission queues before change */
  81. unsigned int poll_queues; /* number of IOPOLL submission queues */
  82. unsigned int prev_poll_queues; /* number of IOPOLL submission queues before change */
  83. unsigned int home_node; /* home node for the device */
  84. unsigned int queue_mode; /* block interface */
  85. unsigned int blocksize; /* block size */
  86. unsigned int max_sectors; /* Max sectors per command */
  87. unsigned int irqmode; /* IRQ completion handler */
  88. unsigned int hw_queue_depth; /* queue depth */
  89. unsigned int index; /* index of the disk, only valid with a disk */
  90. unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */
  91. bool blocking; /* blocking blk-mq device */
  92. bool use_per_node_hctx; /* use per-node allocation for hardware context */
  93. bool power; /* power on/off the device */
  94. bool memory_backed; /* if data is stored in memory */
  95. bool discard; /* if support discard */
  96. bool zoned; /* if device is zoned */
  97. bool zone_full; /* Initialize zones to be full */
  98. bool virt_boundary; /* virtual boundary on/off for the device */
  99. bool no_sched; /* no IO scheduler for the device */
  100. bool shared_tags; /* share tag set between devices for blk-mq */
  101. bool shared_tag_bitmap; /* use hostwide shared tags */
  102. bool fua; /* Support FUA */
  103. bool rotational; /* Fake rotational device */
  104. };
  105. struct nullb {
  106. struct nullb_device *dev;
  107. struct list_head list;
  108. unsigned int index;
  109. struct request_queue *q;
  110. struct gendisk *disk;
  111. struct blk_mq_tag_set *tag_set;
  112. struct blk_mq_tag_set __tag_set;
  113. atomic_long_t cur_bytes;
  114. struct hrtimer bw_timer;
  115. unsigned long cache_flush_pos;
  116. spinlock_t lock;
  117. struct nullb_queue *queues;
  118. char disk_name[DISK_NAME_LEN];
  119. };
  120. blk_status_t null_handle_discard(struct nullb_device *dev, sector_t sector,
  121. sector_t nr_sectors);
  122. blk_status_t null_process_cmd(struct nullb_cmd *cmd, enum req_op op,
  123. sector_t sector, unsigned int nr_sectors);
  124. blk_status_t null_handle_badblocks(struct nullb_cmd *cmd, sector_t sector,
  125. unsigned int *nr_sectors);
  126. blk_status_t null_handle_memory_backed(struct nullb_cmd *cmd, enum req_op op,
  127. sector_t sector, sector_t nr_sectors);
  128. #ifdef CONFIG_BLK_DEV_ZONED
  129. int null_init_zoned_dev(struct nullb_device *dev, struct queue_limits *lim);
  130. int null_register_zoned_dev(struct nullb *nullb);
  131. void null_free_zoned_dev(struct nullb_device *dev);
  132. int null_report_zones(struct gendisk *disk, sector_t sector,
  133. unsigned int nr_zones,
  134. struct blk_report_zones_args *args);
  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. ssize_t zone_cond_store(struct nullb_device *dev, const char *page,
  140. size_t count, enum blk_zone_cond cond);
  141. #else
  142. static inline int null_init_zoned_dev(struct nullb_device *dev,
  143. struct queue_limits *lim)
  144. {
  145. pr_err("CONFIG_BLK_DEV_ZONED not enabled\n");
  146. return -EINVAL;
  147. }
  148. static inline int null_register_zoned_dev(struct nullb *nullb)
  149. {
  150. return -ENODEV;
  151. }
  152. static inline void null_free_zoned_dev(struct nullb_device *dev) {}
  153. static inline blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd,
  154. enum req_op op, sector_t sector, sector_t nr_sectors)
  155. {
  156. return BLK_STS_NOTSUPP;
  157. }
  158. static inline size_t null_zone_valid_read_len(struct nullb *nullb,
  159. sector_t sector,
  160. unsigned int len)
  161. {
  162. return len;
  163. }
  164. static inline ssize_t zone_cond_store(struct nullb_device *dev,
  165. const char *page, size_t count,
  166. enum blk_zone_cond cond)
  167. {
  168. return -EOPNOTSUPP;
  169. }
  170. #define null_report_zones NULL
  171. #endif /* CONFIG_BLK_DEV_ZONED */
  172. #endif /* __NULL_BLK_H */