Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __BLK_NULL_BLK_H
0003 #define __BLK_NULL_BLK_H
0004 
0005 #undef pr_fmt
0006 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0007 
0008 #include <linux/blkdev.h>
0009 #include <linux/slab.h>
0010 #include <linux/blk-mq.h>
0011 #include <linux/hrtimer.h>
0012 #include <linux/configfs.h>
0013 #include <linux/badblocks.h>
0014 #include <linux/fault-inject.h>
0015 #include <linux/spinlock.h>
0016 #include <linux/mutex.h>
0017 
0018 struct nullb_cmd {
0019     union {
0020         struct request *rq;
0021         struct bio *bio;
0022     };
0023     unsigned int tag;
0024     blk_status_t error;
0025     bool fake_timeout;
0026     struct nullb_queue *nq;
0027     struct hrtimer timer;
0028 };
0029 
0030 struct nullb_queue {
0031     unsigned long *tag_map;
0032     wait_queue_head_t wait;
0033     unsigned int queue_depth;
0034     struct nullb_device *dev;
0035     unsigned int requeue_selection;
0036 
0037     struct list_head poll_list;
0038     spinlock_t poll_lock;
0039 
0040     struct nullb_cmd *cmds;
0041 };
0042 
0043 struct nullb_zone {
0044     /*
0045      * Zone lock to prevent concurrent modification of a zone write
0046      * pointer position and condition: with memory backing, a write
0047      * command execution may sleep on memory allocation. For this case,
0048      * use mutex as the zone lock. Otherwise, use the spinlock for
0049      * locking the zone.
0050      */
0051     union {
0052         spinlock_t spinlock;
0053         struct mutex mutex;
0054     };
0055     enum blk_zone_type type;
0056     enum blk_zone_cond cond;
0057     sector_t start;
0058     sector_t wp;
0059     unsigned int len;
0060     unsigned int capacity;
0061 };
0062 
0063 /* Queue modes */
0064 enum {
0065     NULL_Q_BIO  = 0,
0066     NULL_Q_RQ   = 1,
0067     NULL_Q_MQ   = 2,
0068 };
0069 
0070 struct nullb_device {
0071     struct nullb *nullb;
0072     struct config_item item;
0073     struct radix_tree_root data; /* data stored in the disk */
0074     struct radix_tree_root cache; /* disk cache data */
0075     unsigned long flags; /* device flags */
0076     unsigned int curr_cache;
0077     struct badblocks badblocks;
0078 
0079     unsigned int nr_zones;
0080     unsigned int nr_zones_imp_open;
0081     unsigned int nr_zones_exp_open;
0082     unsigned int nr_zones_closed;
0083     unsigned int imp_close_zone_no;
0084     struct nullb_zone *zones;
0085     sector_t zone_size_sects;
0086     bool need_zone_res_mgmt;
0087     spinlock_t zone_res_lock;
0088 
0089     unsigned long size; /* device size in MB */
0090     unsigned long completion_nsec; /* time in ns to complete a request */
0091     unsigned long cache_size; /* disk cache size in MB */
0092     unsigned long zone_size; /* zone size in MB if device is zoned */
0093     unsigned long zone_capacity; /* zone capacity in MB if device is zoned */
0094     unsigned int zone_nr_conv; /* number of conventional zones */
0095     unsigned int zone_max_open; /* max number of open zones */
0096     unsigned int zone_max_active; /* max number of active zones */
0097     unsigned int submit_queues; /* number of submission queues */
0098     unsigned int prev_submit_queues; /* number of submission queues before change */
0099     unsigned int poll_queues; /* number of IOPOLL submission queues */
0100     unsigned int prev_poll_queues; /* number of IOPOLL submission queues before change */
0101     unsigned int home_node; /* home node for the device */
0102     unsigned int queue_mode; /* block interface */
0103     unsigned int blocksize; /* block size */
0104     unsigned int max_sectors; /* Max sectors per command */
0105     unsigned int irqmode; /* IRQ completion handler */
0106     unsigned int hw_queue_depth; /* queue depth */
0107     unsigned int index; /* index of the disk, only valid with a disk */
0108     unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */
0109     bool blocking; /* blocking blk-mq device */
0110     bool use_per_node_hctx; /* use per-node allocation for hardware context */
0111     bool power; /* power on/off the device */
0112     bool memory_backed; /* if data is stored in memory */
0113     bool discard; /* if support discard */
0114     bool zoned; /* if device is zoned */
0115     bool virt_boundary; /* virtual boundary on/off for the device */
0116     bool no_sched; /* no IO scheduler for the device */
0117     bool shared_tag_bitmap; /* use hostwide shared tags */
0118 };
0119 
0120 struct nullb {
0121     struct nullb_device *dev;
0122     struct list_head list;
0123     unsigned int index;
0124     struct request_queue *q;
0125     struct gendisk *disk;
0126     struct blk_mq_tag_set *tag_set;
0127     struct blk_mq_tag_set __tag_set;
0128     unsigned int queue_depth;
0129     atomic_long_t cur_bytes;
0130     struct hrtimer bw_timer;
0131     unsigned long cache_flush_pos;
0132     spinlock_t lock;
0133 
0134     struct nullb_queue *queues;
0135     unsigned int nr_queues;
0136     char disk_name[DISK_NAME_LEN];
0137 };
0138 
0139 blk_status_t null_handle_discard(struct nullb_device *dev, sector_t sector,
0140                  sector_t nr_sectors);
0141 blk_status_t null_process_cmd(struct nullb_cmd *cmd, enum req_op op,
0142                   sector_t sector, unsigned int nr_sectors);
0143 
0144 #ifdef CONFIG_BLK_DEV_ZONED
0145 int null_init_zoned_dev(struct nullb_device *dev, struct request_queue *q);
0146 int null_register_zoned_dev(struct nullb *nullb);
0147 void null_free_zoned_dev(struct nullb_device *dev);
0148 int null_report_zones(struct gendisk *disk, sector_t sector,
0149               unsigned int nr_zones, report_zones_cb cb, void *data);
0150 blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd, enum req_op op,
0151                     sector_t sector, sector_t nr_sectors);
0152 size_t null_zone_valid_read_len(struct nullb *nullb,
0153                 sector_t sector, unsigned int len);
0154 #else
0155 static inline int null_init_zoned_dev(struct nullb_device *dev,
0156                       struct request_queue *q)
0157 {
0158     pr_err("CONFIG_BLK_DEV_ZONED not enabled\n");
0159     return -EINVAL;
0160 }
0161 static inline int null_register_zoned_dev(struct nullb *nullb)
0162 {
0163     return -ENODEV;
0164 }
0165 static inline void null_free_zoned_dev(struct nullb_device *dev) {}
0166 static inline blk_status_t null_process_zoned_cmd(struct nullb_cmd *cmd,
0167             enum req_op op, sector_t sector, sector_t nr_sectors)
0168 {
0169     return BLK_STS_NOTSUPP;
0170 }
0171 static inline size_t null_zone_valid_read_len(struct nullb *nullb,
0172                           sector_t sector,
0173                           unsigned int len)
0174 {
0175     return len;
0176 }
0177 #define null_report_zones   NULL
0178 #endif /* CONFIG_BLK_DEV_ZONED */
0179 #endif /* __NULL_BLK_H */