1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __BLK_NULL_BLK_H
3 #define __BLK_NULL_BLK_H
4 
5 #undef pr_fmt
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/blkdev.h>
9 #include <linux/slab.h>
10 #include <linux/blk-mq.h>
11 #include <linux/hrtimer.h>
12 #include <linux/configfs.h>
13 #include <linux/badblocks.h>
14 #include <linux/fault-inject.h>
15 
16 struct nullb_cmd {
17 	struct list_head list;
18 	struct llist_node ll_list;
19 	struct __call_single_data csd;
20 	struct request *rq;
21 	struct bio *bio;
22 	unsigned int tag;
23 	blk_status_t error;
24 	struct nullb_queue *nq;
25 	struct hrtimer timer;
26 };
27 
28 struct nullb_queue {
29 	unsigned long *tag_map;
30 	wait_queue_head_t wait;
31 	unsigned int queue_depth;
32 	struct nullb_device *dev;
33 	unsigned int requeue_selection;
34 
35 	struct nullb_cmd *cmds;
36 };
37 
38 struct nullb_device {
39 	struct nullb *nullb;
40 	struct config_item item;
41 	struct radix_tree_root data; /* data stored in the disk */
42 	struct radix_tree_root cache; /* disk cache data */
43 	unsigned long flags; /* device flags */
44 	unsigned int curr_cache;
45 	struct badblocks badblocks;
46 
47 	unsigned int nr_zones;
48 	struct blk_zone *zones;
49 	sector_t zone_size_sects;
50 
51 	unsigned long size; /* device size in MB */
52 	unsigned long completion_nsec; /* time in ns to complete a request */
53 	unsigned long cache_size; /* disk cache size in MB */
54 	unsigned long zone_size; /* zone size in MB if device is zoned */
55 	unsigned int zone_nr_conv; /* number of conventional zones */
56 	unsigned int submit_queues; /* number of submission queues */
57 	unsigned int home_node; /* home node for the device */
58 	unsigned int queue_mode; /* block interface */
59 	unsigned int blocksize; /* block size */
60 	unsigned int irqmode; /* IRQ completion handler */
61 	unsigned int hw_queue_depth; /* queue depth */
62 	unsigned int index; /* index of the disk, only valid with a disk */
63 	unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */
64 	bool blocking; /* blocking blk-mq device */
65 	bool use_per_node_hctx; /* use per-node allocation for hardware context */
66 	bool power; /* power on/off the device */
67 	bool memory_backed; /* if data is stored in memory */
68 	bool discard; /* if support discard */
69 	bool zoned; /* if device is zoned */
70 };
71 
72 struct nullb {
73 	struct nullb_device *dev;
74 	struct list_head list;
75 	unsigned int index;
76 	struct request_queue *q;
77 	struct gendisk *disk;
78 	struct blk_mq_tag_set *tag_set;
79 	struct blk_mq_tag_set __tag_set;
80 	unsigned int queue_depth;
81 	atomic_long_t cur_bytes;
82 	struct hrtimer bw_timer;
83 	unsigned long cache_flush_pos;
84 	spinlock_t lock;
85 
86 	struct nullb_queue *queues;
87 	unsigned int nr_queues;
88 	char disk_name[DISK_NAME_LEN];
89 };
90 
91 #ifdef CONFIG_BLK_DEV_ZONED
92 int null_zone_init(struct nullb_device *dev);
93 void null_zone_exit(struct nullb_device *dev);
94 int null_zone_report(struct gendisk *disk, sector_t sector,
95 		     struct blk_zone *zones, unsigned int *nr_zones);
96 blk_status_t null_handle_zoned(struct nullb_cmd *cmd,
97 				enum req_opf op, sector_t sector,
98 				sector_t nr_sectors);
99 #else
null_zone_init(struct nullb_device * dev)100 static inline int null_zone_init(struct nullb_device *dev)
101 {
102 	pr_err("CONFIG_BLK_DEV_ZONED not enabled\n");
103 	return -EINVAL;
104 }
null_zone_exit(struct nullb_device * dev)105 static inline void null_zone_exit(struct nullb_device *dev) {}
null_zone_report(struct gendisk * disk,sector_t sector,struct blk_zone * zones,unsigned int * nr_zones)106 static inline int null_zone_report(struct gendisk *disk, sector_t sector,
107 				   struct blk_zone *zones,
108 				   unsigned int *nr_zones)
109 {
110 	return -EOPNOTSUPP;
111 }
null_handle_zoned(struct nullb_cmd * cmd,enum req_opf op,sector_t sector,sector_t nr_sectors)112 static inline blk_status_t null_handle_zoned(struct nullb_cmd *cmd,
113 					     enum req_opf op, sector_t sector,
114 					     sector_t nr_sectors)
115 {
116 	return BLK_STS_NOTSUPP;
117 }
118 #endif /* CONFIG_BLK_DEV_ZONED */
119 #endif /* __NULL_BLK_H */
120