Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and |
| 3 | * Shaohua Li <shli@fb.com> |
| 4 | */ |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 5 | #include <linux/module.h> |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 6 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 7 | #include <linux/moduleparam.h> |
| 8 | #include <linux/sched.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/blk-mq.h> |
| 14 | #include <linux/hrtimer.h> |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 15 | #include <linux/lightnvm.h> |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 16 | #include <linux/configfs.h> |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 17 | #include <linux/badblocks.h> |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 18 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 19 | #define SECTOR_SHIFT 9 |
| 20 | #define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
| 21 | #define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT) |
| 22 | #define SECTOR_SIZE (1 << SECTOR_SHIFT) |
| 23 | #define SECTOR_MASK (PAGE_SECTORS - 1) |
| 24 | |
| 25 | #define FREE_BATCH 16 |
| 26 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 27 | #define TICKS_PER_SEC 50ULL |
| 28 | #define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC) |
| 29 | |
| 30 | static inline u64 mb_per_tick(int mbps) |
| 31 | { |
| 32 | return (1 << 20) / TICKS_PER_SEC * ((u64) mbps); |
| 33 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 34 | |
| 35 | struct nullb_cmd { |
| 36 | struct list_head list; |
| 37 | struct llist_node ll_list; |
Ying Huang | 966a967 | 2017-08-08 12:30:00 +0800 | [diff] [blame] | 38 | call_single_data_t csd; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 39 | struct request *rq; |
| 40 | struct bio *bio; |
| 41 | unsigned int tag; |
| 42 | struct nullb_queue *nq; |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 43 | struct hrtimer timer; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 44 | blk_status_t error; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct nullb_queue { |
| 48 | unsigned long *tag_map; |
| 49 | wait_queue_head_t wait; |
| 50 | unsigned int queue_depth; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 51 | struct nullb_device *dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 52 | |
| 53 | struct nullb_cmd *cmds; |
| 54 | }; |
| 55 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 56 | /* |
| 57 | * Status flags for nullb_device. |
| 58 | * |
| 59 | * CONFIGURED: Device has been configured and turned on. Cannot reconfigure. |
| 60 | * UP: Device is currently on and visible in userspace. |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 61 | * THROTTLED: Device is being throttled. |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 62 | * CACHE: Device is using a write-back cache. |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 63 | */ |
| 64 | enum nullb_device_flags { |
| 65 | NULLB_DEV_FL_CONFIGURED = 0, |
| 66 | NULLB_DEV_FL_UP = 1, |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 67 | NULLB_DEV_FL_THROTTLED = 2, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 68 | NULLB_DEV_FL_CACHE = 3, |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 71 | /* |
| 72 | * nullb_page is a page in memory for nullb devices. |
| 73 | * |
| 74 | * @page: The page holding the data. |
| 75 | * @bitmap: The bitmap represents which sector in the page has data. |
| 76 | * Each bit represents one block size. For example, sector 8 |
| 77 | * will use the 7th bit |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 78 | * The highest 2 bits of bitmap are for special purpose. LOCK means the cache |
| 79 | * page is being flushing to storage. FREE means the cache page is freed and |
| 80 | * should be skipped from flushing to storage. Please see |
| 81 | * null_make_cache_space |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 82 | */ |
| 83 | struct nullb_page { |
| 84 | struct page *page; |
| 85 | unsigned long bitmap; |
| 86 | }; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 87 | #define NULLB_PAGE_LOCK (sizeof(unsigned long) * 8 - 1) |
| 88 | #define NULLB_PAGE_FREE (sizeof(unsigned long) * 8 - 2) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 89 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 90 | struct nullb_device { |
| 91 | struct nullb *nullb; |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 92 | struct config_item item; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 93 | struct radix_tree_root data; /* data stored in the disk */ |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 94 | struct radix_tree_root cache; /* disk cache data */ |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 95 | unsigned long flags; /* device flags */ |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 96 | unsigned int curr_cache; |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 97 | struct badblocks badblocks; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 98 | |
| 99 | unsigned long size; /* device size in MB */ |
| 100 | unsigned long completion_nsec; /* time in ns to complete a request */ |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 101 | unsigned long cache_size; /* disk cache size in MB */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 102 | unsigned int submit_queues; /* number of submission queues */ |
| 103 | unsigned int home_node; /* home node for the device */ |
| 104 | unsigned int queue_mode; /* block interface */ |
| 105 | unsigned int blocksize; /* block size */ |
| 106 | unsigned int irqmode; /* IRQ completion handler */ |
| 107 | unsigned int hw_queue_depth; /* queue depth */ |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 108 | unsigned int index; /* index of the disk, only valid with a disk */ |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 109 | unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 110 | bool use_lightnvm; /* register as a LightNVM device */ |
| 111 | bool blocking; /* blocking blk-mq device */ |
| 112 | bool use_per_node_hctx; /* use per-node allocation for hardware context */ |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 113 | bool power; /* power on/off the device */ |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 114 | bool memory_backed; /* if data is stored in memory */ |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 115 | bool discard; /* if support discard */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 118 | struct nullb { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 119 | struct nullb_device *dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 120 | struct list_head list; |
| 121 | unsigned int index; |
| 122 | struct request_queue *q; |
| 123 | struct gendisk *disk; |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 124 | struct nvm_dev *ndev; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 125 | struct blk_mq_tag_set *tag_set; |
| 126 | struct blk_mq_tag_set __tag_set; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 127 | unsigned int queue_depth; |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 128 | atomic_long_t cur_bytes; |
| 129 | struct hrtimer bw_timer; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 130 | unsigned long cache_flush_pos; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 131 | spinlock_t lock; |
| 132 | |
| 133 | struct nullb_queue *queues; |
| 134 | unsigned int nr_queues; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 135 | char disk_name[DISK_NAME_LEN]; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static LIST_HEAD(nullb_list); |
| 139 | static struct mutex lock; |
| 140 | static int null_major; |
Shaohua Li | 94bc02e | 2017-08-14 15:04:55 -0700 | [diff] [blame] | 141 | static DEFINE_IDA(nullb_indexes); |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 142 | static struct kmem_cache *ppa_cache; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 143 | static struct blk_mq_tag_set tag_set; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 144 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 145 | enum { |
| 146 | NULL_IRQ_NONE = 0, |
| 147 | NULL_IRQ_SOFTIRQ = 1, |
| 148 | NULL_IRQ_TIMER = 2, |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 149 | }; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 150 | |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 151 | enum { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 152 | NULL_Q_BIO = 0, |
| 153 | NULL_Q_RQ = 1, |
| 154 | NULL_Q_MQ = 2, |
| 155 | }; |
| 156 | |
weiping zhang | b3cffc3 | 2017-09-30 09:49:21 +0800 | [diff] [blame^] | 157 | static int g_no_sched; |
| 158 | module_param_named(no_sched, g_no_sched, int, S_IRUGO); |
| 159 | MODULE_PARM_DESC(no_sched, "No io scheduler"); |
| 160 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 161 | static int g_submit_queues = 1; |
| 162 | module_param_named(submit_queues, g_submit_queues, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 163 | MODULE_PARM_DESC(submit_queues, "Number of submission queues"); |
| 164 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 165 | static int g_home_node = NUMA_NO_NODE; |
| 166 | module_param_named(home_node, g_home_node, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 167 | MODULE_PARM_DESC(home_node, "Home node for the device"); |
| 168 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 169 | static int g_queue_mode = NULL_Q_MQ; |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 170 | |
| 171 | static int null_param_store_val(const char *str, int *val, int min, int max) |
| 172 | { |
| 173 | int ret, new_val; |
| 174 | |
| 175 | ret = kstrtoint(str, 10, &new_val); |
| 176 | if (ret) |
| 177 | return -EINVAL; |
| 178 | |
| 179 | if (new_val < min || new_val > max) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | *val = new_val; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int null_set_queue_mode(const char *str, const struct kernel_param *kp) |
| 187 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 188 | return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ); |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 191 | static const struct kernel_param_ops null_queue_mode_param_ops = { |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 192 | .set = null_set_queue_mode, |
| 193 | .get = param_get_int, |
| 194 | }; |
| 195 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 196 | device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, S_IRUGO); |
Mike Snitzer | 54ae81c | 2014-06-11 17:13:50 -0400 | [diff] [blame] | 197 | MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 198 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 199 | static int g_gb = 250; |
| 200 | module_param_named(gb, g_gb, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 201 | MODULE_PARM_DESC(gb, "Size in GB"); |
| 202 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 203 | static int g_bs = 512; |
| 204 | module_param_named(bs, g_bs, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 205 | MODULE_PARM_DESC(bs, "Block size (in bytes)"); |
| 206 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 207 | static int nr_devices = 1; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 208 | module_param(nr_devices, int, S_IRUGO); |
| 209 | MODULE_PARM_DESC(nr_devices, "Number of devices to register"); |
| 210 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 211 | static bool g_use_lightnvm; |
| 212 | module_param_named(use_lightnvm, g_use_lightnvm, bool, S_IRUGO); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 213 | MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device"); |
| 214 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 215 | static bool g_blocking; |
| 216 | module_param_named(blocking, g_blocking, bool, S_IRUGO); |
Jens Axboe | db5bcf8 | 2017-03-30 13:44:26 -0600 | [diff] [blame] | 217 | MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device"); |
| 218 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 219 | static bool shared_tags; |
| 220 | module_param(shared_tags, bool, S_IRUGO); |
| 221 | MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq"); |
| 222 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 223 | static int g_irqmode = NULL_IRQ_SOFTIRQ; |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 224 | |
| 225 | static int null_set_irqmode(const char *str, const struct kernel_param *kp) |
| 226 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 227 | return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE, |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 228 | NULL_IRQ_TIMER); |
| 229 | } |
| 230 | |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 231 | static const struct kernel_param_ops null_irqmode_param_ops = { |
Matias Bjorling | 709c866 | 2014-11-26 14:45:48 -0700 | [diff] [blame] | 232 | .set = null_set_irqmode, |
| 233 | .get = param_get_int, |
| 234 | }; |
| 235 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 236 | device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 237 | MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer"); |
| 238 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 239 | static unsigned long g_completion_nsec = 10000; |
| 240 | module_param_named(completion_nsec, g_completion_nsec, ulong, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 241 | MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns"); |
| 242 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 243 | static int g_hw_queue_depth = 64; |
| 244 | module_param_named(hw_queue_depth, g_hw_queue_depth, int, S_IRUGO); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 245 | MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64"); |
| 246 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 247 | static bool g_use_per_node_hctx; |
| 248 | module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, S_IRUGO); |
Matias Bjørling | 2000524 | 2013-12-21 00:11:00 +0100 | [diff] [blame] | 249 | MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 250 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 251 | static struct nullb_device *null_alloc_dev(void); |
| 252 | static void null_free_dev(struct nullb_device *dev); |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 253 | static void null_del_dev(struct nullb *nullb); |
| 254 | static int null_add_dev(struct nullb_device *dev); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 255 | static void null_free_device_storage(struct nullb_device *dev, bool is_cache); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 256 | |
| 257 | static inline struct nullb_device *to_nullb_device(struct config_item *item) |
| 258 | { |
| 259 | return item ? container_of(item, struct nullb_device, item) : NULL; |
| 260 | } |
| 261 | |
| 262 | static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page) |
| 263 | { |
| 264 | return snprintf(page, PAGE_SIZE, "%u\n", val); |
| 265 | } |
| 266 | |
| 267 | static inline ssize_t nullb_device_ulong_attr_show(unsigned long val, |
| 268 | char *page) |
| 269 | { |
| 270 | return snprintf(page, PAGE_SIZE, "%lu\n", val); |
| 271 | } |
| 272 | |
| 273 | static inline ssize_t nullb_device_bool_attr_show(bool val, char *page) |
| 274 | { |
| 275 | return snprintf(page, PAGE_SIZE, "%u\n", val); |
| 276 | } |
| 277 | |
| 278 | static ssize_t nullb_device_uint_attr_store(unsigned int *val, |
| 279 | const char *page, size_t count) |
| 280 | { |
| 281 | unsigned int tmp; |
| 282 | int result; |
| 283 | |
| 284 | result = kstrtouint(page, 0, &tmp); |
| 285 | if (result) |
| 286 | return result; |
| 287 | |
| 288 | *val = tmp; |
| 289 | return count; |
| 290 | } |
| 291 | |
| 292 | static ssize_t nullb_device_ulong_attr_store(unsigned long *val, |
| 293 | const char *page, size_t count) |
| 294 | { |
| 295 | int result; |
| 296 | unsigned long tmp; |
| 297 | |
| 298 | result = kstrtoul(page, 0, &tmp); |
| 299 | if (result) |
| 300 | return result; |
| 301 | |
| 302 | *val = tmp; |
| 303 | return count; |
| 304 | } |
| 305 | |
| 306 | static ssize_t nullb_device_bool_attr_store(bool *val, const char *page, |
| 307 | size_t count) |
| 308 | { |
| 309 | bool tmp; |
| 310 | int result; |
| 311 | |
| 312 | result = kstrtobool(page, &tmp); |
| 313 | if (result) |
| 314 | return result; |
| 315 | |
| 316 | *val = tmp; |
| 317 | return count; |
| 318 | } |
| 319 | |
| 320 | /* The following macro should only be used with TYPE = {uint, ulong, bool}. */ |
| 321 | #define NULLB_DEVICE_ATTR(NAME, TYPE) \ |
| 322 | static ssize_t \ |
| 323 | nullb_device_##NAME##_show(struct config_item *item, char *page) \ |
| 324 | { \ |
| 325 | return nullb_device_##TYPE##_attr_show( \ |
| 326 | to_nullb_device(item)->NAME, page); \ |
| 327 | } \ |
| 328 | static ssize_t \ |
| 329 | nullb_device_##NAME##_store(struct config_item *item, const char *page, \ |
| 330 | size_t count) \ |
| 331 | { \ |
| 332 | if (test_bit(NULLB_DEV_FL_CONFIGURED, &to_nullb_device(item)->flags)) \ |
| 333 | return -EBUSY; \ |
| 334 | return nullb_device_##TYPE##_attr_store( \ |
| 335 | &to_nullb_device(item)->NAME, page, count); \ |
| 336 | } \ |
| 337 | CONFIGFS_ATTR(nullb_device_, NAME); |
| 338 | |
| 339 | NULLB_DEVICE_ATTR(size, ulong); |
| 340 | NULLB_DEVICE_ATTR(completion_nsec, ulong); |
| 341 | NULLB_DEVICE_ATTR(submit_queues, uint); |
| 342 | NULLB_DEVICE_ATTR(home_node, uint); |
| 343 | NULLB_DEVICE_ATTR(queue_mode, uint); |
| 344 | NULLB_DEVICE_ATTR(blocksize, uint); |
| 345 | NULLB_DEVICE_ATTR(irqmode, uint); |
| 346 | NULLB_DEVICE_ATTR(hw_queue_depth, uint); |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 347 | NULLB_DEVICE_ATTR(index, uint); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 348 | NULLB_DEVICE_ATTR(use_lightnvm, bool); |
| 349 | NULLB_DEVICE_ATTR(blocking, bool); |
| 350 | NULLB_DEVICE_ATTR(use_per_node_hctx, bool); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 351 | NULLB_DEVICE_ATTR(memory_backed, bool); |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 352 | NULLB_DEVICE_ATTR(discard, bool); |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 353 | NULLB_DEVICE_ATTR(mbps, uint); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 354 | NULLB_DEVICE_ATTR(cache_size, ulong); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 355 | |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 356 | static ssize_t nullb_device_power_show(struct config_item *item, char *page) |
| 357 | { |
| 358 | return nullb_device_bool_attr_show(to_nullb_device(item)->power, page); |
| 359 | } |
| 360 | |
| 361 | static ssize_t nullb_device_power_store(struct config_item *item, |
| 362 | const char *page, size_t count) |
| 363 | { |
| 364 | struct nullb_device *dev = to_nullb_device(item); |
| 365 | bool newp = false; |
| 366 | ssize_t ret; |
| 367 | |
| 368 | ret = nullb_device_bool_attr_store(&newp, page, count); |
| 369 | if (ret < 0) |
| 370 | return ret; |
| 371 | |
| 372 | if (!dev->power && newp) { |
| 373 | if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags)) |
| 374 | return count; |
| 375 | if (null_add_dev(dev)) { |
| 376 | clear_bit(NULLB_DEV_FL_UP, &dev->flags); |
| 377 | return -ENOMEM; |
| 378 | } |
| 379 | |
| 380 | set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags); |
| 381 | dev->power = newp; |
Jens Axboe | b3c3051 | 2017-08-28 15:06:31 -0600 | [diff] [blame] | 382 | } else if (dev->power && !newp) { |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 383 | mutex_lock(&lock); |
| 384 | dev->power = newp; |
| 385 | null_del_dev(dev->nullb); |
| 386 | mutex_unlock(&lock); |
| 387 | clear_bit(NULLB_DEV_FL_UP, &dev->flags); |
| 388 | } |
| 389 | |
| 390 | return count; |
| 391 | } |
| 392 | |
| 393 | CONFIGFS_ATTR(nullb_device_, power); |
| 394 | |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 395 | static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page) |
| 396 | { |
| 397 | struct nullb_device *t_dev = to_nullb_device(item); |
| 398 | |
| 399 | return badblocks_show(&t_dev->badblocks, page, 0); |
| 400 | } |
| 401 | |
| 402 | static ssize_t nullb_device_badblocks_store(struct config_item *item, |
| 403 | const char *page, size_t count) |
| 404 | { |
| 405 | struct nullb_device *t_dev = to_nullb_device(item); |
| 406 | char *orig, *buf, *tmp; |
| 407 | u64 start, end; |
| 408 | int ret; |
| 409 | |
| 410 | orig = kstrndup(page, count, GFP_KERNEL); |
| 411 | if (!orig) |
| 412 | return -ENOMEM; |
| 413 | |
| 414 | buf = strstrip(orig); |
| 415 | |
| 416 | ret = -EINVAL; |
| 417 | if (buf[0] != '+' && buf[0] != '-') |
| 418 | goto out; |
| 419 | tmp = strchr(&buf[1], '-'); |
| 420 | if (!tmp) |
| 421 | goto out; |
| 422 | *tmp = '\0'; |
| 423 | ret = kstrtoull(buf + 1, 0, &start); |
| 424 | if (ret) |
| 425 | goto out; |
| 426 | ret = kstrtoull(tmp + 1, 0, &end); |
| 427 | if (ret) |
| 428 | goto out; |
| 429 | ret = -EINVAL; |
| 430 | if (start > end) |
| 431 | goto out; |
| 432 | /* enable badblocks */ |
| 433 | cmpxchg(&t_dev->badblocks.shift, -1, 0); |
| 434 | if (buf[0] == '+') |
| 435 | ret = badblocks_set(&t_dev->badblocks, start, |
| 436 | end - start + 1, 1); |
| 437 | else |
| 438 | ret = badblocks_clear(&t_dev->badblocks, start, |
| 439 | end - start + 1); |
| 440 | if (ret == 0) |
| 441 | ret = count; |
| 442 | out: |
| 443 | kfree(orig); |
| 444 | return ret; |
| 445 | } |
| 446 | CONFIGFS_ATTR(nullb_device_, badblocks); |
| 447 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 448 | static struct configfs_attribute *nullb_device_attrs[] = { |
| 449 | &nullb_device_attr_size, |
| 450 | &nullb_device_attr_completion_nsec, |
| 451 | &nullb_device_attr_submit_queues, |
| 452 | &nullb_device_attr_home_node, |
| 453 | &nullb_device_attr_queue_mode, |
| 454 | &nullb_device_attr_blocksize, |
| 455 | &nullb_device_attr_irqmode, |
| 456 | &nullb_device_attr_hw_queue_depth, |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 457 | &nullb_device_attr_index, |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 458 | &nullb_device_attr_use_lightnvm, |
| 459 | &nullb_device_attr_blocking, |
| 460 | &nullb_device_attr_use_per_node_hctx, |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 461 | &nullb_device_attr_power, |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 462 | &nullb_device_attr_memory_backed, |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 463 | &nullb_device_attr_discard, |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 464 | &nullb_device_attr_mbps, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 465 | &nullb_device_attr_cache_size, |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 466 | &nullb_device_attr_badblocks, |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 467 | NULL, |
| 468 | }; |
| 469 | |
| 470 | static void nullb_device_release(struct config_item *item) |
| 471 | { |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 472 | struct nullb_device *dev = to_nullb_device(item); |
| 473 | |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 474 | badblocks_exit(&dev->badblocks); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 475 | null_free_device_storage(dev, false); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 476 | null_free_dev(dev); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static struct configfs_item_operations nullb_device_ops = { |
| 480 | .release = nullb_device_release, |
| 481 | }; |
| 482 | |
| 483 | static struct config_item_type nullb_device_type = { |
| 484 | .ct_item_ops = &nullb_device_ops, |
| 485 | .ct_attrs = nullb_device_attrs, |
| 486 | .ct_owner = THIS_MODULE, |
| 487 | }; |
| 488 | |
| 489 | static struct |
| 490 | config_item *nullb_group_make_item(struct config_group *group, const char *name) |
| 491 | { |
| 492 | struct nullb_device *dev; |
| 493 | |
| 494 | dev = null_alloc_dev(); |
| 495 | if (!dev) |
| 496 | return ERR_PTR(-ENOMEM); |
| 497 | |
| 498 | config_item_init_type_name(&dev->item, name, &nullb_device_type); |
| 499 | |
| 500 | return &dev->item; |
| 501 | } |
| 502 | |
| 503 | static void |
| 504 | nullb_group_drop_item(struct config_group *group, struct config_item *item) |
| 505 | { |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 506 | struct nullb_device *dev = to_nullb_device(item); |
| 507 | |
| 508 | if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) { |
| 509 | mutex_lock(&lock); |
| 510 | dev->power = false; |
| 511 | null_del_dev(dev->nullb); |
| 512 | mutex_unlock(&lock); |
| 513 | } |
| 514 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 515 | config_item_put(item); |
| 516 | } |
| 517 | |
| 518 | static ssize_t memb_group_features_show(struct config_item *item, char *page) |
| 519 | { |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 520 | return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache,badblocks\n"); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | CONFIGFS_ATTR_RO(memb_group_, features); |
| 524 | |
| 525 | static struct configfs_attribute *nullb_group_attrs[] = { |
| 526 | &memb_group_attr_features, |
| 527 | NULL, |
| 528 | }; |
| 529 | |
| 530 | static struct configfs_group_operations nullb_group_ops = { |
| 531 | .make_item = nullb_group_make_item, |
| 532 | .drop_item = nullb_group_drop_item, |
| 533 | }; |
| 534 | |
| 535 | static struct config_item_type nullb_group_type = { |
| 536 | .ct_group_ops = &nullb_group_ops, |
| 537 | .ct_attrs = nullb_group_attrs, |
| 538 | .ct_owner = THIS_MODULE, |
| 539 | }; |
| 540 | |
| 541 | static struct configfs_subsystem nullb_subsys = { |
| 542 | .su_group = { |
| 543 | .cg_item = { |
| 544 | .ci_namebuf = "nullb", |
| 545 | .ci_type = &nullb_group_type, |
| 546 | }, |
| 547 | }, |
| 548 | }; |
| 549 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 550 | static inline int null_cache_active(struct nullb *nullb) |
| 551 | { |
| 552 | return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags); |
| 553 | } |
| 554 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 555 | static struct nullb_device *null_alloc_dev(void) |
| 556 | { |
| 557 | struct nullb_device *dev; |
| 558 | |
| 559 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 560 | if (!dev) |
| 561 | return NULL; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 562 | INIT_RADIX_TREE(&dev->data, GFP_ATOMIC); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 563 | INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC); |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 564 | if (badblocks_init(&dev->badblocks, 0)) { |
| 565 | kfree(dev); |
| 566 | return NULL; |
| 567 | } |
| 568 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 569 | dev->size = g_gb * 1024; |
| 570 | dev->completion_nsec = g_completion_nsec; |
| 571 | dev->submit_queues = g_submit_queues; |
| 572 | dev->home_node = g_home_node; |
| 573 | dev->queue_mode = g_queue_mode; |
| 574 | dev->blocksize = g_bs; |
| 575 | dev->irqmode = g_irqmode; |
| 576 | dev->hw_queue_depth = g_hw_queue_depth; |
| 577 | dev->use_lightnvm = g_use_lightnvm; |
| 578 | dev->blocking = g_blocking; |
| 579 | dev->use_per_node_hctx = g_use_per_node_hctx; |
| 580 | return dev; |
| 581 | } |
| 582 | |
| 583 | static void null_free_dev(struct nullb_device *dev) |
| 584 | { |
| 585 | kfree(dev); |
| 586 | } |
| 587 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 588 | static void put_tag(struct nullb_queue *nq, unsigned int tag) |
| 589 | { |
| 590 | clear_bit_unlock(tag, nq->tag_map); |
| 591 | |
| 592 | if (waitqueue_active(&nq->wait)) |
| 593 | wake_up(&nq->wait); |
| 594 | } |
| 595 | |
| 596 | static unsigned int get_tag(struct nullb_queue *nq) |
| 597 | { |
| 598 | unsigned int tag; |
| 599 | |
| 600 | do { |
| 601 | tag = find_first_zero_bit(nq->tag_map, nq->queue_depth); |
| 602 | if (tag >= nq->queue_depth) |
| 603 | return -1U; |
| 604 | } while (test_and_set_bit_lock(tag, nq->tag_map)); |
| 605 | |
| 606 | return tag; |
| 607 | } |
| 608 | |
| 609 | static void free_cmd(struct nullb_cmd *cmd) |
| 610 | { |
| 611 | put_tag(cmd->nq, cmd->tag); |
| 612 | } |
| 613 | |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 614 | static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer); |
| 615 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 616 | static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq) |
| 617 | { |
| 618 | struct nullb_cmd *cmd; |
| 619 | unsigned int tag; |
| 620 | |
| 621 | tag = get_tag(nq); |
| 622 | if (tag != -1U) { |
| 623 | cmd = &nq->cmds[tag]; |
| 624 | cmd->tag = tag; |
| 625 | cmd->nq = nq; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 626 | if (nq->dev->irqmode == NULL_IRQ_TIMER) { |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 627 | hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, |
| 628 | HRTIMER_MODE_REL); |
| 629 | cmd->timer.function = null_cmd_timer_expired; |
| 630 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 631 | return cmd; |
| 632 | } |
| 633 | |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait) |
| 638 | { |
| 639 | struct nullb_cmd *cmd; |
| 640 | DEFINE_WAIT(wait); |
| 641 | |
| 642 | cmd = __alloc_cmd(nq); |
| 643 | if (cmd || !can_wait) |
| 644 | return cmd; |
| 645 | |
| 646 | do { |
| 647 | prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 648 | cmd = __alloc_cmd(nq); |
| 649 | if (cmd) |
| 650 | break; |
| 651 | |
| 652 | io_schedule(); |
| 653 | } while (1); |
| 654 | |
| 655 | finish_wait(&nq->wait, &wait); |
| 656 | return cmd; |
| 657 | } |
| 658 | |
| 659 | static void end_cmd(struct nullb_cmd *cmd) |
| 660 | { |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 661 | struct request_queue *q = NULL; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 662 | int queue_mode = cmd->nq->dev->queue_mode; |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 663 | |
Mike Krinkin | e827120 | 2015-12-15 12:56:40 +0300 | [diff] [blame] | 664 | if (cmd->rq) |
| 665 | q = cmd->rq->q; |
| 666 | |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 667 | switch (queue_mode) { |
| 668 | case NULL_Q_MQ: |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 669 | blk_mq_end_request(cmd->rq, cmd->error); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 670 | return; |
| 671 | case NULL_Q_RQ: |
| 672 | INIT_LIST_HEAD(&cmd->rq->queuelist); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 673 | blk_end_request_all(cmd->rq, cmd->error); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 674 | break; |
| 675 | case NULL_Q_BIO: |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 676 | cmd->bio->bi_status = cmd->error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 677 | bio_endio(cmd->bio); |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 678 | break; |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 679 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 680 | |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 681 | free_cmd(cmd); |
| 682 | |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 683 | /* Restart queue if needed, as we are freeing a tag */ |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 684 | if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) { |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 685 | unsigned long flags; |
| 686 | |
| 687 | spin_lock_irqsave(q->queue_lock, flags); |
Jens Axboe | 48cc661 | 2015-12-28 13:02:47 -0700 | [diff] [blame] | 688 | blk_start_queue_async(q); |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 689 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 690 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer) |
| 694 | { |
Arianna Avanzini | cf8ecc5 | 2015-12-01 11:48:18 +0100 | [diff] [blame] | 695 | end_cmd(container_of(timer, struct nullb_cmd, timer)); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 696 | |
| 697 | return HRTIMER_NORESTART; |
| 698 | } |
| 699 | |
| 700 | static void null_cmd_end_timer(struct nullb_cmd *cmd) |
| 701 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 702 | ktime_t kt = cmd->nq->dev->completion_nsec; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 703 | |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 704 | hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static void null_softirq_done_fn(struct request *rq) |
| 708 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 709 | struct nullb *nullb = rq->q->queuedata; |
| 710 | |
| 711 | if (nullb->dev->queue_mode == NULL_Q_MQ) |
Jens Axboe | d891fa7 | 2014-06-16 11:40:25 -0600 | [diff] [blame] | 712 | end_cmd(blk_mq_rq_to_pdu(rq)); |
| 713 | else |
| 714 | end_cmd(rq->special); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 717 | static struct nullb_page *null_alloc_page(gfp_t gfp_flags) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 718 | { |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 719 | struct nullb_page *t_page; |
| 720 | |
| 721 | t_page = kmalloc(sizeof(struct nullb_page), gfp_flags); |
| 722 | if (!t_page) |
| 723 | goto out; |
| 724 | |
| 725 | t_page->page = alloc_pages(gfp_flags, 0); |
| 726 | if (!t_page->page) |
| 727 | goto out_freepage; |
| 728 | |
| 729 | t_page->bitmap = 0; |
| 730 | return t_page; |
| 731 | out_freepage: |
| 732 | kfree(t_page); |
| 733 | out: |
| 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | static void null_free_page(struct nullb_page *t_page) |
| 738 | { |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 739 | __set_bit(NULLB_PAGE_FREE, &t_page->bitmap); |
| 740 | if (test_bit(NULLB_PAGE_LOCK, &t_page->bitmap)) |
| 741 | return; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 742 | __free_page(t_page->page); |
| 743 | kfree(t_page); |
| 744 | } |
| 745 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 746 | static void null_free_sector(struct nullb *nullb, sector_t sector, |
| 747 | bool is_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 748 | { |
| 749 | unsigned int sector_bit; |
| 750 | u64 idx; |
| 751 | struct nullb_page *t_page, *ret; |
| 752 | struct radix_tree_root *root; |
| 753 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 754 | root = is_cache ? &nullb->dev->cache : &nullb->dev->data; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 755 | idx = sector >> PAGE_SECTORS_SHIFT; |
| 756 | sector_bit = (sector & SECTOR_MASK); |
| 757 | |
| 758 | t_page = radix_tree_lookup(root, idx); |
| 759 | if (t_page) { |
| 760 | __clear_bit(sector_bit, &t_page->bitmap); |
| 761 | |
| 762 | if (!t_page->bitmap) { |
| 763 | ret = radix_tree_delete_item(root, idx, t_page); |
| 764 | WARN_ON(ret != t_page); |
| 765 | null_free_page(ret); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 766 | if (is_cache) |
| 767 | nullb->dev->curr_cache -= PAGE_SIZE; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 773 | struct nullb_page *t_page, bool is_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 774 | { |
| 775 | struct radix_tree_root *root; |
| 776 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 777 | root = is_cache ? &nullb->dev->cache : &nullb->dev->data; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 778 | |
| 779 | if (radix_tree_insert(root, idx, t_page)) { |
| 780 | null_free_page(t_page); |
| 781 | t_page = radix_tree_lookup(root, idx); |
| 782 | WARN_ON(!t_page || t_page->page->index != idx); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 783 | } else if (is_cache) |
| 784 | nullb->dev->curr_cache += PAGE_SIZE; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 785 | |
| 786 | return t_page; |
| 787 | } |
| 788 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 789 | static void null_free_device_storage(struct nullb_device *dev, bool is_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 790 | { |
| 791 | unsigned long pos = 0; |
| 792 | int nr_pages; |
| 793 | struct nullb_page *ret, *t_pages[FREE_BATCH]; |
| 794 | struct radix_tree_root *root; |
| 795 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 796 | root = is_cache ? &dev->cache : &dev->data; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 797 | |
| 798 | do { |
| 799 | int i; |
| 800 | |
| 801 | nr_pages = radix_tree_gang_lookup(root, |
| 802 | (void **)t_pages, pos, FREE_BATCH); |
| 803 | |
| 804 | for (i = 0; i < nr_pages; i++) { |
| 805 | pos = t_pages[i]->page->index; |
| 806 | ret = radix_tree_delete_item(root, pos, t_pages[i]); |
| 807 | WARN_ON(ret != t_pages[i]); |
| 808 | null_free_page(ret); |
| 809 | } |
| 810 | |
| 811 | pos++; |
| 812 | } while (nr_pages == FREE_BATCH); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 813 | |
| 814 | if (is_cache) |
| 815 | dev->curr_cache = 0; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 818 | static struct nullb_page *__null_lookup_page(struct nullb *nullb, |
| 819 | sector_t sector, bool for_write, bool is_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 820 | { |
| 821 | unsigned int sector_bit; |
| 822 | u64 idx; |
| 823 | struct nullb_page *t_page; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 824 | struct radix_tree_root *root; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 825 | |
| 826 | idx = sector >> PAGE_SECTORS_SHIFT; |
| 827 | sector_bit = (sector & SECTOR_MASK); |
| 828 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 829 | root = is_cache ? &nullb->dev->cache : &nullb->dev->data; |
| 830 | t_page = radix_tree_lookup(root, idx); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 831 | WARN_ON(t_page && t_page->page->index != idx); |
| 832 | |
| 833 | if (t_page && (for_write || test_bit(sector_bit, &t_page->bitmap))) |
| 834 | return t_page; |
| 835 | |
| 836 | return NULL; |
| 837 | } |
| 838 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 839 | static struct nullb_page *null_lookup_page(struct nullb *nullb, |
| 840 | sector_t sector, bool for_write, bool ignore_cache) |
| 841 | { |
| 842 | struct nullb_page *page = NULL; |
| 843 | |
| 844 | if (!ignore_cache) |
| 845 | page = __null_lookup_page(nullb, sector, for_write, true); |
| 846 | if (page) |
| 847 | return page; |
| 848 | return __null_lookup_page(nullb, sector, for_write, false); |
| 849 | } |
| 850 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 851 | static struct nullb_page *null_insert_page(struct nullb *nullb, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 852 | sector_t sector, bool ignore_cache) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 853 | { |
| 854 | u64 idx; |
| 855 | struct nullb_page *t_page; |
| 856 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 857 | t_page = null_lookup_page(nullb, sector, true, ignore_cache); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 858 | if (t_page) |
| 859 | return t_page; |
| 860 | |
| 861 | spin_unlock_irq(&nullb->lock); |
| 862 | |
| 863 | t_page = null_alloc_page(GFP_NOIO); |
| 864 | if (!t_page) |
| 865 | goto out_lock; |
| 866 | |
| 867 | if (radix_tree_preload(GFP_NOIO)) |
| 868 | goto out_freepage; |
| 869 | |
| 870 | spin_lock_irq(&nullb->lock); |
| 871 | idx = sector >> PAGE_SECTORS_SHIFT; |
| 872 | t_page->page->index = idx; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 873 | t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 874 | radix_tree_preload_end(); |
| 875 | |
| 876 | return t_page; |
| 877 | out_freepage: |
| 878 | null_free_page(t_page); |
| 879 | out_lock: |
| 880 | spin_lock_irq(&nullb->lock); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 881 | return null_lookup_page(nullb, sector, true, ignore_cache); |
| 882 | } |
| 883 | |
| 884 | static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page) |
| 885 | { |
| 886 | int i; |
| 887 | unsigned int offset; |
| 888 | u64 idx; |
| 889 | struct nullb_page *t_page, *ret; |
| 890 | void *dst, *src; |
| 891 | |
| 892 | idx = c_page->page->index; |
| 893 | |
| 894 | t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true); |
| 895 | |
| 896 | __clear_bit(NULLB_PAGE_LOCK, &c_page->bitmap); |
| 897 | if (test_bit(NULLB_PAGE_FREE, &c_page->bitmap)) { |
| 898 | null_free_page(c_page); |
| 899 | if (t_page && t_page->bitmap == 0) { |
| 900 | ret = radix_tree_delete_item(&nullb->dev->data, |
| 901 | idx, t_page); |
| 902 | null_free_page(t_page); |
| 903 | } |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | if (!t_page) |
| 908 | return -ENOMEM; |
| 909 | |
| 910 | src = kmap_atomic(c_page->page); |
| 911 | dst = kmap_atomic(t_page->page); |
| 912 | |
| 913 | for (i = 0; i < PAGE_SECTORS; |
| 914 | i += (nullb->dev->blocksize >> SECTOR_SHIFT)) { |
| 915 | if (test_bit(i, &c_page->bitmap)) { |
| 916 | offset = (i << SECTOR_SHIFT); |
| 917 | memcpy(dst + offset, src + offset, |
| 918 | nullb->dev->blocksize); |
| 919 | __set_bit(i, &t_page->bitmap); |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | kunmap_atomic(dst); |
| 924 | kunmap_atomic(src); |
| 925 | |
| 926 | ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page); |
| 927 | null_free_page(ret); |
| 928 | nullb->dev->curr_cache -= PAGE_SIZE; |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static int null_make_cache_space(struct nullb *nullb, unsigned long n) |
| 934 | { |
| 935 | int i, err, nr_pages; |
| 936 | struct nullb_page *c_pages[FREE_BATCH]; |
| 937 | unsigned long flushed = 0, one_round; |
| 938 | |
| 939 | again: |
| 940 | if ((nullb->dev->cache_size * 1024 * 1024) > |
| 941 | nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0) |
| 942 | return 0; |
| 943 | |
| 944 | nr_pages = radix_tree_gang_lookup(&nullb->dev->cache, |
| 945 | (void **)c_pages, nullb->cache_flush_pos, FREE_BATCH); |
| 946 | /* |
| 947 | * nullb_flush_cache_page could unlock before using the c_pages. To |
| 948 | * avoid race, we don't allow page free |
| 949 | */ |
| 950 | for (i = 0; i < nr_pages; i++) { |
| 951 | nullb->cache_flush_pos = c_pages[i]->page->index; |
| 952 | /* |
| 953 | * We found the page which is being flushed to disk by other |
| 954 | * threads |
| 955 | */ |
| 956 | if (test_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap)) |
| 957 | c_pages[i] = NULL; |
| 958 | else |
| 959 | __set_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap); |
| 960 | } |
| 961 | |
| 962 | one_round = 0; |
| 963 | for (i = 0; i < nr_pages; i++) { |
| 964 | if (c_pages[i] == NULL) |
| 965 | continue; |
| 966 | err = null_flush_cache_page(nullb, c_pages[i]); |
| 967 | if (err) |
| 968 | return err; |
| 969 | one_round++; |
| 970 | } |
| 971 | flushed += one_round << PAGE_SHIFT; |
| 972 | |
| 973 | if (n > flushed) { |
| 974 | if (nr_pages == 0) |
| 975 | nullb->cache_flush_pos = 0; |
| 976 | if (one_round == 0) { |
| 977 | /* give other threads a chance */ |
| 978 | spin_unlock_irq(&nullb->lock); |
| 979 | spin_lock_irq(&nullb->lock); |
| 980 | } |
| 981 | goto again; |
| 982 | } |
| 983 | return 0; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | static int copy_to_nullb(struct nullb *nullb, struct page *source, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 987 | unsigned int off, sector_t sector, size_t n, bool is_fua) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 988 | { |
| 989 | size_t temp, count = 0; |
| 990 | unsigned int offset; |
| 991 | struct nullb_page *t_page; |
| 992 | void *dst, *src; |
| 993 | |
| 994 | while (count < n) { |
| 995 | temp = min_t(size_t, nullb->dev->blocksize, n - count); |
| 996 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 997 | if (null_cache_active(nullb) && !is_fua) |
| 998 | null_make_cache_space(nullb, PAGE_SIZE); |
| 999 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1000 | offset = (sector & SECTOR_MASK) << SECTOR_SHIFT; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1001 | t_page = null_insert_page(nullb, sector, |
| 1002 | !null_cache_active(nullb) || is_fua); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1003 | if (!t_page) |
| 1004 | return -ENOSPC; |
| 1005 | |
| 1006 | src = kmap_atomic(source); |
| 1007 | dst = kmap_atomic(t_page->page); |
| 1008 | memcpy(dst + offset, src + off + count, temp); |
| 1009 | kunmap_atomic(dst); |
| 1010 | kunmap_atomic(src); |
| 1011 | |
| 1012 | __set_bit(sector & SECTOR_MASK, &t_page->bitmap); |
| 1013 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1014 | if (is_fua) |
| 1015 | null_free_sector(nullb, sector, true); |
| 1016 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1017 | count += temp; |
| 1018 | sector += temp >> SECTOR_SHIFT; |
| 1019 | } |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | static int copy_from_nullb(struct nullb *nullb, struct page *dest, |
| 1024 | unsigned int off, sector_t sector, size_t n) |
| 1025 | { |
| 1026 | size_t temp, count = 0; |
| 1027 | unsigned int offset; |
| 1028 | struct nullb_page *t_page; |
| 1029 | void *dst, *src; |
| 1030 | |
| 1031 | while (count < n) { |
| 1032 | temp = min_t(size_t, nullb->dev->blocksize, n - count); |
| 1033 | |
| 1034 | offset = (sector & SECTOR_MASK) << SECTOR_SHIFT; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1035 | t_page = null_lookup_page(nullb, sector, false, |
| 1036 | !null_cache_active(nullb)); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1037 | |
| 1038 | dst = kmap_atomic(dest); |
| 1039 | if (!t_page) { |
| 1040 | memset(dst + off + count, 0, temp); |
| 1041 | goto next; |
| 1042 | } |
| 1043 | src = kmap_atomic(t_page->page); |
| 1044 | memcpy(dst + off + count, src + offset, temp); |
| 1045 | kunmap_atomic(src); |
| 1046 | next: |
| 1047 | kunmap_atomic(dst); |
| 1048 | |
| 1049 | count += temp; |
| 1050 | sector += temp >> SECTOR_SHIFT; |
| 1051 | } |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1055 | static void null_handle_discard(struct nullb *nullb, sector_t sector, size_t n) |
| 1056 | { |
| 1057 | size_t temp; |
| 1058 | |
| 1059 | spin_lock_irq(&nullb->lock); |
| 1060 | while (n > 0) { |
| 1061 | temp = min_t(size_t, n, nullb->dev->blocksize); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1062 | null_free_sector(nullb, sector, false); |
| 1063 | if (null_cache_active(nullb)) |
| 1064 | null_free_sector(nullb, sector, true); |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1065 | sector += temp >> SECTOR_SHIFT; |
| 1066 | n -= temp; |
| 1067 | } |
| 1068 | spin_unlock_irq(&nullb->lock); |
| 1069 | } |
| 1070 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1071 | static int null_handle_flush(struct nullb *nullb) |
| 1072 | { |
| 1073 | int err; |
| 1074 | |
| 1075 | if (!null_cache_active(nullb)) |
| 1076 | return 0; |
| 1077 | |
| 1078 | spin_lock_irq(&nullb->lock); |
| 1079 | while (true) { |
| 1080 | err = null_make_cache_space(nullb, |
| 1081 | nullb->dev->cache_size * 1024 * 1024); |
| 1082 | if (err || nullb->dev->curr_cache == 0) |
| 1083 | break; |
| 1084 | } |
| 1085 | |
| 1086 | WARN_ON(!radix_tree_empty(&nullb->dev->cache)); |
| 1087 | spin_unlock_irq(&nullb->lock); |
| 1088 | return err; |
| 1089 | } |
| 1090 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1091 | static int null_transfer(struct nullb *nullb, struct page *page, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1092 | unsigned int len, unsigned int off, bool is_write, sector_t sector, |
| 1093 | bool is_fua) |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1094 | { |
| 1095 | int err = 0; |
| 1096 | |
| 1097 | if (!is_write) { |
| 1098 | err = copy_from_nullb(nullb, page, off, sector, len); |
| 1099 | flush_dcache_page(page); |
| 1100 | } else { |
| 1101 | flush_dcache_page(page); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1102 | err = copy_to_nullb(nullb, page, off, sector, len, is_fua); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | return err; |
| 1106 | } |
| 1107 | |
| 1108 | static int null_handle_rq(struct nullb_cmd *cmd) |
| 1109 | { |
| 1110 | struct request *rq = cmd->rq; |
| 1111 | struct nullb *nullb = cmd->nq->dev->nullb; |
| 1112 | int err; |
| 1113 | unsigned int len; |
| 1114 | sector_t sector; |
| 1115 | struct req_iterator iter; |
| 1116 | struct bio_vec bvec; |
| 1117 | |
| 1118 | sector = blk_rq_pos(rq); |
| 1119 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1120 | if (req_op(rq) == REQ_OP_DISCARD) { |
| 1121 | null_handle_discard(nullb, sector, blk_rq_bytes(rq)); |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1125 | spin_lock_irq(&nullb->lock); |
| 1126 | rq_for_each_segment(bvec, rq, iter) { |
| 1127 | len = bvec.bv_len; |
| 1128 | err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1129 | op_is_write(req_op(rq)), sector, |
| 1130 | req_op(rq) & REQ_FUA); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1131 | if (err) { |
| 1132 | spin_unlock_irq(&nullb->lock); |
| 1133 | return err; |
| 1134 | } |
| 1135 | sector += len >> SECTOR_SHIFT; |
| 1136 | } |
| 1137 | spin_unlock_irq(&nullb->lock); |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | static int null_handle_bio(struct nullb_cmd *cmd) |
| 1143 | { |
| 1144 | struct bio *bio = cmd->bio; |
| 1145 | struct nullb *nullb = cmd->nq->dev->nullb; |
| 1146 | int err; |
| 1147 | unsigned int len; |
| 1148 | sector_t sector; |
| 1149 | struct bio_vec bvec; |
| 1150 | struct bvec_iter iter; |
| 1151 | |
| 1152 | sector = bio->bi_iter.bi_sector; |
| 1153 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1154 | if (bio_op(bio) == REQ_OP_DISCARD) { |
| 1155 | null_handle_discard(nullb, sector, |
| 1156 | bio_sectors(bio) << SECTOR_SHIFT); |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1160 | spin_lock_irq(&nullb->lock); |
| 1161 | bio_for_each_segment(bvec, bio, iter) { |
| 1162 | len = bvec.bv_len; |
| 1163 | err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset, |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1164 | op_is_write(bio_op(bio)), sector, |
| 1165 | bio_op(bio) & REQ_FUA); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1166 | if (err) { |
| 1167 | spin_unlock_irq(&nullb->lock); |
| 1168 | return err; |
| 1169 | } |
| 1170 | sector += len >> SECTOR_SHIFT; |
| 1171 | } |
| 1172 | spin_unlock_irq(&nullb->lock); |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1176 | static void null_stop_queue(struct nullb *nullb) |
| 1177 | { |
| 1178 | struct request_queue *q = nullb->q; |
| 1179 | |
| 1180 | if (nullb->dev->queue_mode == NULL_Q_MQ) |
| 1181 | blk_mq_stop_hw_queues(q); |
| 1182 | else { |
| 1183 | spin_lock_irq(q->queue_lock); |
| 1184 | blk_stop_queue(q); |
| 1185 | spin_unlock_irq(q->queue_lock); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | static void null_restart_queue_async(struct nullb *nullb) |
| 1190 | { |
| 1191 | struct request_queue *q = nullb->q; |
| 1192 | unsigned long flags; |
| 1193 | |
| 1194 | if (nullb->dev->queue_mode == NULL_Q_MQ) |
| 1195 | blk_mq_start_stopped_hw_queues(q, true); |
| 1196 | else { |
| 1197 | spin_lock_irqsave(q->queue_lock, flags); |
| 1198 | blk_start_queue_async(q); |
| 1199 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 1200 | } |
| 1201 | } |
| 1202 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1203 | static blk_status_t null_handle_cmd(struct nullb_cmd *cmd) |
| 1204 | { |
| 1205 | struct nullb_device *dev = cmd->nq->dev; |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1206 | struct nullb *nullb = dev->nullb; |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1207 | int err = 0; |
| 1208 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1209 | if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) { |
| 1210 | struct request *rq = cmd->rq; |
| 1211 | |
| 1212 | if (!hrtimer_active(&nullb->bw_timer)) |
| 1213 | hrtimer_restart(&nullb->bw_timer); |
| 1214 | |
| 1215 | if (atomic_long_sub_return(blk_rq_bytes(rq), |
| 1216 | &nullb->cur_bytes) < 0) { |
| 1217 | null_stop_queue(nullb); |
| 1218 | /* race with timer */ |
| 1219 | if (atomic_long_read(&nullb->cur_bytes) > 0) |
| 1220 | null_restart_queue_async(nullb); |
| 1221 | if (dev->queue_mode == NULL_Q_RQ) { |
| 1222 | struct request_queue *q = nullb->q; |
| 1223 | |
| 1224 | spin_lock_irq(q->queue_lock); |
| 1225 | rq->rq_flags |= RQF_DONTPREP; |
| 1226 | blk_requeue_request(q, rq); |
| 1227 | spin_unlock_irq(q->queue_lock); |
| 1228 | return BLK_STS_OK; |
| 1229 | } else |
| 1230 | /* requeue request */ |
| 1231 | return BLK_STS_RESOURCE; |
| 1232 | } |
| 1233 | } |
| 1234 | |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 1235 | if (nullb->dev->badblocks.shift != -1) { |
| 1236 | int bad_sectors; |
| 1237 | sector_t sector, size, first_bad; |
| 1238 | bool is_flush = true; |
| 1239 | |
| 1240 | if (dev->queue_mode == NULL_Q_BIO && |
| 1241 | bio_op(cmd->bio) != REQ_OP_FLUSH) { |
| 1242 | is_flush = false; |
| 1243 | sector = cmd->bio->bi_iter.bi_sector; |
| 1244 | size = bio_sectors(cmd->bio); |
| 1245 | } |
| 1246 | if (dev->queue_mode != NULL_Q_BIO && |
| 1247 | req_op(cmd->rq) != REQ_OP_FLUSH) { |
| 1248 | is_flush = false; |
| 1249 | sector = blk_rq_pos(cmd->rq); |
| 1250 | size = blk_rq_sectors(cmd->rq); |
| 1251 | } |
| 1252 | if (!is_flush && badblocks_check(&nullb->dev->badblocks, sector, |
| 1253 | size, &first_bad, &bad_sectors)) { |
| 1254 | cmd->error = BLK_STS_IOERR; |
| 1255 | goto out; |
| 1256 | } |
| 1257 | } |
| 1258 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1259 | if (dev->memory_backed) { |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1260 | if (dev->queue_mode == NULL_Q_BIO) { |
| 1261 | if (bio_op(cmd->bio) == REQ_OP_FLUSH) |
| 1262 | err = null_handle_flush(nullb); |
| 1263 | else |
| 1264 | err = null_handle_bio(cmd); |
| 1265 | } else { |
| 1266 | if (req_op(cmd->rq) == REQ_OP_FLUSH) |
| 1267 | err = null_handle_flush(nullb); |
| 1268 | else |
| 1269 | err = null_handle_rq(cmd); |
| 1270 | } |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1271 | } |
| 1272 | cmd->error = errno_to_blk_status(err); |
Shaohua Li | 2f54a61 | 2017-08-14 15:05:00 -0700 | [diff] [blame] | 1273 | out: |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1274 | /* Complete IO by inline, softirq or timer */ |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1275 | switch (dev->irqmode) { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 1276 | case NULL_IRQ_SOFTIRQ: |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1277 | switch (dev->queue_mode) { |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 1278 | case NULL_Q_MQ: |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 1279 | blk_mq_complete_request(cmd->rq); |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 1280 | break; |
| 1281 | case NULL_Q_RQ: |
| 1282 | blk_complete_request(cmd->rq); |
| 1283 | break; |
| 1284 | case NULL_Q_BIO: |
| 1285 | /* |
| 1286 | * XXX: no proper submitting cpu information available. |
| 1287 | */ |
| 1288 | end_cmd(cmd); |
| 1289 | break; |
| 1290 | } |
| 1291 | break; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1292 | case NULL_IRQ_NONE: |
| 1293 | end_cmd(cmd); |
| 1294 | break; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1295 | case NULL_IRQ_TIMER: |
| 1296 | null_cmd_end_timer(cmd); |
| 1297 | break; |
| 1298 | } |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1299 | return BLK_STS_OK; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1302 | static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer) |
| 1303 | { |
| 1304 | struct nullb *nullb = container_of(timer, struct nullb, bw_timer); |
| 1305 | ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL); |
| 1306 | unsigned int mbps = nullb->dev->mbps; |
| 1307 | |
| 1308 | if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps)) |
| 1309 | return HRTIMER_NORESTART; |
| 1310 | |
| 1311 | atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps)); |
| 1312 | null_restart_queue_async(nullb); |
| 1313 | |
| 1314 | hrtimer_forward_now(&nullb->bw_timer, timer_interval); |
| 1315 | |
| 1316 | return HRTIMER_RESTART; |
| 1317 | } |
| 1318 | |
| 1319 | static void nullb_setup_bwtimer(struct nullb *nullb) |
| 1320 | { |
| 1321 | ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL); |
| 1322 | |
| 1323 | hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1324 | nullb->bw_timer.function = nullb_bwtimer_fn; |
| 1325 | atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps)); |
| 1326 | hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | static struct nullb_queue *nullb_to_queue(struct nullb *nullb) |
| 1330 | { |
| 1331 | int index = 0; |
| 1332 | |
| 1333 | if (nullb->nr_queues != 1) |
| 1334 | index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues); |
| 1335 | |
| 1336 | return &nullb->queues[index]; |
| 1337 | } |
| 1338 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1339 | static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1340 | { |
| 1341 | struct nullb *nullb = q->queuedata; |
| 1342 | struct nullb_queue *nq = nullb_to_queue(nullb); |
| 1343 | struct nullb_cmd *cmd; |
| 1344 | |
| 1345 | cmd = alloc_cmd(nq, 1); |
| 1346 | cmd->bio = bio; |
| 1347 | |
| 1348 | null_handle_cmd(cmd); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 1349 | return BLK_QC_T_NONE; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | static int null_rq_prep_fn(struct request_queue *q, struct request *req) |
| 1353 | { |
| 1354 | struct nullb *nullb = q->queuedata; |
| 1355 | struct nullb_queue *nq = nullb_to_queue(nullb); |
| 1356 | struct nullb_cmd *cmd; |
| 1357 | |
| 1358 | cmd = alloc_cmd(nq, 0); |
| 1359 | if (cmd) { |
| 1360 | cmd->rq = req; |
| 1361 | req->special = cmd; |
| 1362 | return BLKPREP_OK; |
| 1363 | } |
Akinobu Mita | 8b70f45 | 2015-06-02 08:35:10 +0900 | [diff] [blame] | 1364 | blk_stop_queue(q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1365 | |
| 1366 | return BLKPREP_DEFER; |
| 1367 | } |
| 1368 | |
| 1369 | static void null_request_fn(struct request_queue *q) |
| 1370 | { |
| 1371 | struct request *rq; |
| 1372 | |
| 1373 | while ((rq = blk_fetch_request(q)) != NULL) { |
| 1374 | struct nullb_cmd *cmd = rq->special; |
| 1375 | |
| 1376 | spin_unlock_irq(q->queue_lock); |
| 1377 | null_handle_cmd(cmd); |
| 1378 | spin_lock_irq(q->queue_lock); |
| 1379 | } |
| 1380 | } |
| 1381 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 1382 | static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx, |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1383 | const struct blk_mq_queue_data *bd) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1384 | { |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1385 | struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1386 | struct nullb_queue *nq = hctx->driver_data; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1387 | |
Jens Axboe | db5bcf8 | 2017-03-30 13:44:26 -0600 | [diff] [blame] | 1388 | might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING); |
| 1389 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1390 | if (nq->dev->irqmode == NULL_IRQ_TIMER) { |
Paolo Valente | 3c395a9 | 2015-12-01 11:48:17 +0100 | [diff] [blame] | 1391 | hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1392 | cmd->timer.function = null_cmd_timer_expired; |
| 1393 | } |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1394 | cmd->rq = bd->rq; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1395 | cmd->nq = nq; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1396 | |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 1397 | blk_mq_start_request(bd->rq); |
Christoph Hellwig | e249007 | 2014-09-13 16:40:09 -0700 | [diff] [blame] | 1398 | |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1399 | return null_handle_cmd(cmd); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1400 | } |
| 1401 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 1402 | static const struct blk_mq_ops null_mq_ops = { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1403 | .queue_rq = null_queue_rq, |
Christoph Hellwig | ce2c350 | 2014-02-10 03:24:40 -0800 | [diff] [blame] | 1404 | .complete = null_softirq_done_fn, |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1405 | }; |
| 1406 | |
Matias Bjørling | de65d2d | 2015-08-31 14:17:18 +0200 | [diff] [blame] | 1407 | static void cleanup_queue(struct nullb_queue *nq) |
| 1408 | { |
| 1409 | kfree(nq->tag_map); |
| 1410 | kfree(nq->cmds); |
| 1411 | } |
| 1412 | |
| 1413 | static void cleanup_queues(struct nullb *nullb) |
| 1414 | { |
| 1415 | int i; |
| 1416 | |
| 1417 | for (i = 0; i < nullb->nr_queues; i++) |
| 1418 | cleanup_queue(&nullb->queues[i]); |
| 1419 | |
| 1420 | kfree(nullb->queues); |
| 1421 | } |
| 1422 | |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1423 | #ifdef CONFIG_NVM |
| 1424 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1425 | static void null_lnvm_end_io(struct request *rq, blk_status_t status) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1426 | { |
| 1427 | struct nvm_rq *rqd = rq->end_io_data; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1428 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1429 | /* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */ |
| 1430 | rqd->error = status ? -EIO : 0; |
Matias Bjørling | 06894ef | 2017-01-31 13:17:17 +0100 | [diff] [blame] | 1431 | nvm_end_io(rqd); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1432 | |
| 1433 | blk_put_request(rq); |
| 1434 | } |
| 1435 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1436 | static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1437 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1438 | struct request_queue *q = dev->q; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1439 | struct request *rq; |
| 1440 | struct bio *bio = rqd->bio; |
| 1441 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 1442 | rq = blk_mq_alloc_request(q, |
| 1443 | op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1444 | if (IS_ERR(rq)) |
| 1445 | return -ENOMEM; |
| 1446 | |
Bart Van Assche | 2644a3c | 2017-04-19 14:01:25 -0700 | [diff] [blame] | 1447 | blk_init_request_from_bio(rq, bio); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1448 | |
| 1449 | rq->end_io_data = rqd; |
| 1450 | |
| 1451 | blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io); |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1456 | static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1457 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1458 | struct nullb *nullb = dev->q->queuedata; |
| 1459 | sector_t size = (sector_t)nullb->dev->size * 1024 * 1024ULL; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1460 | sector_t blksize; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1461 | struct nvm_id_group *grp; |
| 1462 | |
| 1463 | id->ver_id = 0x1; |
| 1464 | id->vmnt = 0; |
Matias Bjørling | bf64318 | 2016-02-04 15:13:27 +0100 | [diff] [blame] | 1465 | id->cap = 0x2; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1466 | id->dom = 0x1; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1467 | |
| 1468 | id->ppaf.blk_offset = 0; |
| 1469 | id->ppaf.blk_len = 16; |
| 1470 | id->ppaf.pg_offset = 16; |
| 1471 | id->ppaf.pg_len = 16; |
| 1472 | id->ppaf.sect_offset = 32; |
| 1473 | id->ppaf.sect_len = 8; |
| 1474 | id->ppaf.pln_offset = 40; |
| 1475 | id->ppaf.pln_len = 8; |
| 1476 | id->ppaf.lun_offset = 48; |
| 1477 | id->ppaf.lun_len = 8; |
| 1478 | id->ppaf.ch_offset = 56; |
| 1479 | id->ppaf.ch_len = 8; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1480 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1481 | sector_div(size, nullb->dev->blocksize); /* convert size to pages */ |
Arnd Bergmann | e93d12a | 2016-01-13 23:04:08 +0100 | [diff] [blame] | 1482 | size >>= 8; /* concert size to pgs pr blk */ |
Matias Bjørling | 19bd6fe | 2017-01-31 13:17:15 +0100 | [diff] [blame] | 1483 | grp = &id->grp; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1484 | grp->mtype = 0; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1485 | grp->fmtype = 0; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1486 | grp->num_ch = 1; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1487 | grp->num_pg = 256; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1488 | blksize = size; |
Arnd Bergmann | e93d12a | 2016-01-13 23:04:08 +0100 | [diff] [blame] | 1489 | size >>= 16; |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1490 | grp->num_lun = size + 1; |
Arnd Bergmann | e93d12a | 2016-01-13 23:04:08 +0100 | [diff] [blame] | 1491 | sector_div(blksize, grp->num_lun); |
Matias Bjørling | 5b40db9 | 2015-11-19 12:50:09 +0100 | [diff] [blame] | 1492 | grp->num_blk = blksize; |
| 1493 | grp->num_pln = 1; |
| 1494 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1495 | grp->fpg_sz = nullb->dev->blocksize; |
| 1496 | grp->csecs = nullb->dev->blocksize; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1497 | grp->trdt = 25000; |
| 1498 | grp->trdm = 25000; |
| 1499 | grp->tprt = 500000; |
| 1500 | grp->tprm = 500000; |
| 1501 | grp->tbet = 1500000; |
| 1502 | grp->tbem = 1500000; |
| 1503 | grp->mpos = 0x010101; /* single plane rwe */ |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1504 | grp->cpar = nullb->dev->hw_queue_depth; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1505 | |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1509 | static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name) |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1510 | { |
| 1511 | mempool_t *virtmem_pool; |
| 1512 | |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1513 | virtmem_pool = mempool_create_slab_pool(64, ppa_cache); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1514 | if (!virtmem_pool) { |
| 1515 | pr_err("null_blk: Unable to create virtual memory pool\n"); |
| 1516 | return NULL; |
| 1517 | } |
| 1518 | |
| 1519 | return virtmem_pool; |
| 1520 | } |
| 1521 | |
| 1522 | static void null_lnvm_destroy_dma_pool(void *pool) |
| 1523 | { |
| 1524 | mempool_destroy(pool); |
| 1525 | } |
| 1526 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 1527 | static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool, |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1528 | gfp_t mem_flags, dma_addr_t *dma_handler) |
| 1529 | { |
| 1530 | return mempool_alloc(pool, mem_flags); |
| 1531 | } |
| 1532 | |
| 1533 | static void null_lnvm_dev_dma_free(void *pool, void *entry, |
| 1534 | dma_addr_t dma_handler) |
| 1535 | { |
| 1536 | mempool_free(entry, pool); |
| 1537 | } |
| 1538 | |
| 1539 | static struct nvm_dev_ops null_lnvm_dev_ops = { |
| 1540 | .identity = null_lnvm_id, |
| 1541 | .submit_io = null_lnvm_submit_io, |
| 1542 | |
| 1543 | .create_dma_pool = null_lnvm_create_dma_pool, |
| 1544 | .destroy_dma_pool = null_lnvm_destroy_dma_pool, |
| 1545 | .dev_dma_alloc = null_lnvm_dev_dma_alloc, |
| 1546 | .dev_dma_free = null_lnvm_dev_dma_free, |
| 1547 | |
| 1548 | /* Simulate nvme protocol restriction */ |
| 1549 | .max_phys_sect = 64, |
| 1550 | }; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1551 | |
| 1552 | static int null_nvm_register(struct nullb *nullb) |
| 1553 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1554 | struct nvm_dev *dev; |
| 1555 | int rv; |
| 1556 | |
| 1557 | dev = nvm_alloc_dev(0); |
| 1558 | if (!dev) |
| 1559 | return -ENOMEM; |
| 1560 | |
| 1561 | dev->q = nullb->q; |
| 1562 | memcpy(dev->name, nullb->disk_name, DISK_NAME_LEN); |
| 1563 | dev->ops = &null_lnvm_dev_ops; |
| 1564 | |
| 1565 | rv = nvm_register(dev); |
| 1566 | if (rv) { |
| 1567 | kfree(dev); |
| 1568 | return rv; |
| 1569 | } |
| 1570 | nullb->ndev = dev; |
| 1571 | return 0; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | static void null_nvm_unregister(struct nullb *nullb) |
| 1575 | { |
Matias Bjørling | b0b4e09 | 2016-09-16 14:25:07 +0200 | [diff] [blame] | 1576 | nvm_unregister(nullb->ndev); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1577 | } |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1578 | #else |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1579 | static int null_nvm_register(struct nullb *nullb) |
| 1580 | { |
Yasuaki Ishimatsu | 92153d3 | 2016-11-16 08:26:11 -0700 | [diff] [blame] | 1581 | pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n"); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1582 | return -EINVAL; |
| 1583 | } |
| 1584 | static void null_nvm_unregister(struct nullb *nullb) {} |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1585 | #endif /* CONFIG_NVM */ |
| 1586 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1587 | static void null_del_dev(struct nullb *nullb) |
| 1588 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1589 | struct nullb_device *dev = nullb->dev; |
| 1590 | |
Shaohua Li | 94bc02e | 2017-08-14 15:04:55 -0700 | [diff] [blame] | 1591 | ida_simple_remove(&nullb_indexes, nullb->index); |
| 1592 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1593 | list_del_init(&nullb->list); |
| 1594 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1595 | if (dev->use_lightnvm) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1596 | null_nvm_unregister(nullb); |
| 1597 | else |
| 1598 | del_gendisk(nullb->disk); |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1599 | |
| 1600 | if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) { |
| 1601 | hrtimer_cancel(&nullb->bw_timer); |
| 1602 | atomic_long_set(&nullb->cur_bytes, LONG_MAX); |
| 1603 | null_restart_queue_async(nullb); |
| 1604 | } |
| 1605 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1606 | blk_cleanup_queue(nullb->q); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1607 | if (dev->queue_mode == NULL_Q_MQ && |
| 1608 | nullb->tag_set == &nullb->__tag_set) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1609 | blk_mq_free_tag_set(nullb->tag_set); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1610 | if (!dev->use_lightnvm) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1611 | put_disk(nullb->disk); |
| 1612 | cleanup_queues(nullb); |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1613 | if (null_cache_active(nullb)) |
| 1614 | null_free_device_storage(nullb->dev, true); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1615 | kfree(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1616 | dev->nullb = NULL; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1617 | } |
| 1618 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1619 | static void null_config_discard(struct nullb *nullb) |
| 1620 | { |
| 1621 | if (nullb->dev->discard == false) |
| 1622 | return; |
| 1623 | nullb->q->limits.discard_granularity = nullb->dev->blocksize; |
| 1624 | nullb->q->limits.discard_alignment = nullb->dev->blocksize; |
| 1625 | blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9); |
| 1626 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nullb->q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | static int null_open(struct block_device *bdev, fmode_t mode) |
| 1630 | { |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
| 1634 | static void null_release(struct gendisk *disk, fmode_t mode) |
| 1635 | { |
| 1636 | } |
| 1637 | |
| 1638 | static const struct block_device_operations null_fops = { |
| 1639 | .owner = THIS_MODULE, |
| 1640 | .open = null_open, |
| 1641 | .release = null_release, |
| 1642 | }; |
| 1643 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1644 | static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq) |
| 1645 | { |
| 1646 | BUG_ON(!nullb); |
| 1647 | BUG_ON(!nq); |
| 1648 | |
| 1649 | init_waitqueue_head(&nq->wait); |
| 1650 | nq->queue_depth = nullb->queue_depth; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1651 | nq->dev = nullb->dev; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | static void null_init_queues(struct nullb *nullb) |
| 1655 | { |
| 1656 | struct request_queue *q = nullb->q; |
| 1657 | struct blk_mq_hw_ctx *hctx; |
| 1658 | struct nullb_queue *nq; |
| 1659 | int i; |
| 1660 | |
| 1661 | queue_for_each_hw_ctx(q, hctx, i) { |
| 1662 | if (!hctx->nr_ctx || !hctx->tags) |
| 1663 | continue; |
| 1664 | nq = &nullb->queues[i]; |
| 1665 | hctx->driver_data = nq; |
| 1666 | null_init_queue(nullb, nq); |
| 1667 | nullb->nr_queues++; |
| 1668 | } |
| 1669 | } |
| 1670 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1671 | static int setup_commands(struct nullb_queue *nq) |
| 1672 | { |
| 1673 | struct nullb_cmd *cmd; |
| 1674 | int i, tag_size; |
| 1675 | |
| 1676 | nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL); |
| 1677 | if (!nq->cmds) |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1678 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1679 | |
| 1680 | tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG; |
| 1681 | nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL); |
| 1682 | if (!nq->tag_map) { |
| 1683 | kfree(nq->cmds); |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1684 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | for (i = 0; i < nq->queue_depth; i++) { |
| 1688 | cmd = &nq->cmds[i]; |
| 1689 | INIT_LIST_HEAD(&cmd->list); |
| 1690 | cmd->ll_list.next = NULL; |
| 1691 | cmd->tag = -1U; |
| 1692 | } |
| 1693 | |
| 1694 | return 0; |
| 1695 | } |
| 1696 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1697 | static int setup_queues(struct nullb *nullb) |
| 1698 | { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1699 | nullb->queues = kzalloc(nullb->dev->submit_queues * |
| 1700 | sizeof(struct nullb_queue), GFP_KERNEL); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1701 | if (!nullb->queues) |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1702 | return -ENOMEM; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1703 | |
| 1704 | nullb->nr_queues = 0; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1705 | nullb->queue_depth = nullb->dev->hw_queue_depth; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1706 | |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | static int init_driver_queues(struct nullb *nullb) |
| 1711 | { |
| 1712 | struct nullb_queue *nq; |
| 1713 | int i, ret = 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1714 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1715 | for (i = 0; i < nullb->dev->submit_queues; i++) { |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1716 | nq = &nullb->queues[i]; |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1717 | |
| 1718 | null_init_queue(nullb, nq); |
| 1719 | |
| 1720 | ret = setup_commands(nq); |
| 1721 | if (ret) |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 1722 | return ret; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1723 | nullb->nr_queues++; |
| 1724 | } |
Matias Bjorling | 2d263a78 | 2013-12-18 13:41:43 +0100 | [diff] [blame] | 1725 | return 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1726 | } |
| 1727 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1728 | static int null_gendisk_register(struct nullb *nullb) |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1729 | { |
| 1730 | struct gendisk *disk; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1731 | sector_t size; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1732 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1733 | disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node); |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1734 | if (!disk) |
| 1735 | return -ENOMEM; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1736 | size = (sector_t)nullb->dev->size * 1024 * 1024ULL; |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1737 | set_capacity(disk, size >> 9); |
| 1738 | |
| 1739 | disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO; |
| 1740 | disk->major = null_major; |
| 1741 | disk->first_minor = nullb->index; |
| 1742 | disk->fops = &null_fops; |
| 1743 | disk->private_data = nullb; |
| 1744 | disk->queue = nullb->q; |
| 1745 | strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN); |
| 1746 | |
| 1747 | add_disk(disk); |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1751 | static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1752 | { |
| 1753 | set->ops = &null_mq_ops; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1754 | set->nr_hw_queues = nullb ? nullb->dev->submit_queues : |
| 1755 | g_submit_queues; |
| 1756 | set->queue_depth = nullb ? nullb->dev->hw_queue_depth : |
| 1757 | g_hw_queue_depth; |
| 1758 | set->numa_node = nullb ? nullb->dev->home_node : g_home_node; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1759 | set->cmd_size = sizeof(struct nullb_cmd); |
| 1760 | set->flags = BLK_MQ_F_SHOULD_MERGE; |
weiping zhang | b3cffc3 | 2017-09-30 09:49:21 +0800 | [diff] [blame^] | 1761 | if (g_no_sched) |
| 1762 | set->flags |= BLK_MQ_F_NO_SCHED; |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1763 | set->driver_data = NULL; |
| 1764 | |
Shaohua Li | 0d06a42 | 2017-08-25 13:46:25 -0700 | [diff] [blame] | 1765 | if ((nullb && nullb->dev->blocking) || g_blocking) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1766 | set->flags |= BLK_MQ_F_BLOCKING; |
| 1767 | |
| 1768 | return blk_mq_alloc_tag_set(set); |
| 1769 | } |
| 1770 | |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1771 | static void null_validate_conf(struct nullb_device *dev) |
| 1772 | { |
| 1773 | dev->blocksize = round_down(dev->blocksize, 512); |
| 1774 | dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096); |
| 1775 | if (dev->use_lightnvm && dev->blocksize != 4096) |
| 1776 | dev->blocksize = 4096; |
| 1777 | |
| 1778 | if (dev->use_lightnvm && dev->queue_mode != NULL_Q_MQ) |
| 1779 | dev->queue_mode = NULL_Q_MQ; |
| 1780 | |
| 1781 | if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) { |
| 1782 | if (dev->submit_queues != nr_online_nodes) |
| 1783 | dev->submit_queues = nr_online_nodes; |
| 1784 | } else if (dev->submit_queues > nr_cpu_ids) |
| 1785 | dev->submit_queues = nr_cpu_ids; |
| 1786 | else if (dev->submit_queues == 0) |
| 1787 | dev->submit_queues = 1; |
| 1788 | |
| 1789 | dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ); |
| 1790 | dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER); |
Shaohua Li | 5bcd0e0 | 2017-08-14 15:04:56 -0700 | [diff] [blame] | 1791 | |
| 1792 | /* Do memory allocation, so set blocking */ |
| 1793 | if (dev->memory_backed) |
| 1794 | dev->blocking = true; |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1795 | else /* cache is meaningless */ |
| 1796 | dev->cache_size = 0; |
| 1797 | dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024, |
| 1798 | dev->cache_size); |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1799 | dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps); |
| 1800 | /* can not stop a queue */ |
| 1801 | if (dev->queue_mode == NULL_Q_BIO) |
| 1802 | dev->mbps = 0; |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1805 | static int null_add_dev(struct nullb_device *dev) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1806 | { |
| 1807 | struct nullb *nullb; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1808 | int rv; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1809 | |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1810 | null_validate_conf(dev); |
| 1811 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1812 | nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1813 | if (!nullb) { |
| 1814 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1815 | goto out; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1816 | } |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1817 | nullb->dev = dev; |
| 1818 | dev->nullb = nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1819 | |
| 1820 | spin_lock_init(&nullb->lock); |
| 1821 | |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1822 | rv = setup_queues(nullb); |
| 1823 | if (rv) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1824 | goto out_free_nullb; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1825 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1826 | if (dev->queue_mode == NULL_Q_MQ) { |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1827 | if (shared_tags) { |
| 1828 | nullb->tag_set = &tag_set; |
| 1829 | rv = 0; |
| 1830 | } else { |
| 1831 | nullb->tag_set = &nullb->__tag_set; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1832 | rv = null_init_tag_set(nullb, nullb->tag_set); |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1833 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1834 | |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1835 | if (rv) |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1836 | goto out_cleanup_queues; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1837 | |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1838 | nullb->q = blk_mq_init_queue(nullb->tag_set); |
Ming Lei | 35b489d | 2015-01-02 14:25:27 +0000 | [diff] [blame] | 1839 | if (IS_ERR(nullb->q)) { |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1840 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1841 | goto out_cleanup_tags; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1842 | } |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1843 | null_init_queues(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1844 | } else if (dev->queue_mode == NULL_Q_BIO) { |
| 1845 | nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1846 | if (!nullb->q) { |
| 1847 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1848 | goto out_cleanup_queues; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1849 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1850 | blk_queue_make_request(nullb->q, null_queue_bio); |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 1851 | rv = init_driver_queues(nullb); |
| 1852 | if (rv) |
| 1853 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1854 | } else { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1855 | nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, |
| 1856 | dev->home_node); |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1857 | if (!nullb->q) { |
| 1858 | rv = -ENOMEM; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1859 | goto out_cleanup_queues; |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1860 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1861 | blk_queue_prep_rq(nullb->q, null_rq_prep_fn); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1862 | blk_queue_softirq_done(nullb->q, null_softirq_done_fn); |
Jan Kara | 31f9690 | 2014-10-22 15:34:21 +0200 | [diff] [blame] | 1863 | rv = init_driver_queues(nullb); |
| 1864 | if (rv) |
| 1865 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1866 | } |
| 1867 | |
Shaohua Li | eff2c4f | 2017-08-14 15:04:58 -0700 | [diff] [blame] | 1868 | if (dev->mbps) { |
| 1869 | set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags); |
| 1870 | nullb_setup_bwtimer(nullb); |
| 1871 | } |
| 1872 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1873 | if (dev->cache_size > 0) { |
| 1874 | set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags); |
| 1875 | blk_queue_write_cache(nullb->q, true, true); |
| 1876 | blk_queue_flush_queueable(nullb->q, true); |
| 1877 | } |
| 1878 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1879 | nullb->q->queuedata = nullb; |
| 1880 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 1881 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1882 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1883 | mutex_lock(&lock); |
Shaohua Li | 94bc02e | 2017-08-14 15:04:55 -0700 | [diff] [blame] | 1884 | nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL); |
Shaohua Li | cedcafa | 2017-08-14 15:04:54 -0700 | [diff] [blame] | 1885 | dev->index = nullb->index; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1886 | mutex_unlock(&lock); |
| 1887 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1888 | blk_queue_logical_block_size(nullb->q, dev->blocksize); |
| 1889 | blk_queue_physical_block_size(nullb->q, dev->blocksize); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1890 | |
Shaohua Li | 306eb6b | 2017-08-14 15:04:57 -0700 | [diff] [blame] | 1891 | null_config_discard(nullb); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1892 | |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1893 | sprintf(nullb->disk_name, "nullb%d", nullb->index); |
| 1894 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1895 | if (dev->use_lightnvm) |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1896 | rv = null_nvm_register(nullb); |
| 1897 | else |
| 1898 | rv = null_gendisk_register(nullb); |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1899 | |
Matias Bjørling | 9ae2d0a | 2016-09-16 14:25:05 +0200 | [diff] [blame] | 1900 | if (rv) |
| 1901 | goto out_cleanup_blk_queue; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1902 | |
Matias Bjørling | a514379 | 2016-02-11 14:49:13 +0100 | [diff] [blame] | 1903 | mutex_lock(&lock); |
| 1904 | list_add_tail(&nullb->list, &nullb_list); |
| 1905 | mutex_unlock(&lock); |
Wenwei Tao | 3681c85d | 2016-03-05 00:27:04 +0800 | [diff] [blame] | 1906 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1907 | return 0; |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1908 | out_cleanup_blk_queue: |
| 1909 | blk_cleanup_queue(nullb->q); |
| 1910 | out_cleanup_tags: |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1911 | if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 1912 | blk_mq_free_tag_set(nullb->tag_set); |
Christoph Hellwig | 24d2f90 | 2014-04-15 14:14:00 -0600 | [diff] [blame] | 1913 | out_cleanup_queues: |
| 1914 | cleanup_queues(nullb); |
| 1915 | out_free_nullb: |
| 1916 | kfree(nullb); |
| 1917 | out: |
Robert Elliott | dc501dc | 2014-09-02 11:38:49 -0500 | [diff] [blame] | 1918 | return rv; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | static int __init null_init(void) |
| 1922 | { |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1923 | int ret = 0; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1924 | unsigned int i; |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1925 | struct nullb *nullb; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1926 | struct nullb_device *dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1927 | |
Shaohua Li | deb78b4 | 2017-08-14 15:04:59 -0700 | [diff] [blame] | 1928 | /* check for nullb_page.bitmap */ |
| 1929 | if (sizeof(unsigned long) * 8 - 2 < (PAGE_SIZE >> SECTOR_SHIFT)) |
| 1930 | return -EINVAL; |
| 1931 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1932 | if (g_bs > PAGE_SIZE) { |
Raghavendra K T | 9967d8a | 2014-01-21 16:59:59 +0530 | [diff] [blame] | 1933 | pr_warn("null_blk: invalid block size\n"); |
| 1934 | pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1935 | g_bs = PAGE_SIZE; |
Raghavendra K T | 9967d8a | 2014-01-21 16:59:59 +0530 | [diff] [blame] | 1936 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1937 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1938 | if (g_use_lightnvm && g_bs != 4096) { |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1939 | pr_warn("null_blk: LightNVM only supports 4k block size\n"); |
| 1940 | pr_warn("null_blk: defaults block size to 4k\n"); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1941 | g_bs = 4096; |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1942 | } |
| 1943 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1944 | if (g_use_lightnvm && g_queue_mode != NULL_Q_MQ) { |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1945 | pr_warn("null_blk: LightNVM only supported for blk-mq\n"); |
| 1946 | pr_warn("null_blk: defaults queue mode to blk-mq\n"); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1947 | g_queue_mode = NULL_Q_MQ; |
Matias Bjørling | b2b7e00 | 2015-11-12 20:25:10 +0100 | [diff] [blame] | 1948 | } |
| 1949 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1950 | if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) { |
| 1951 | if (g_submit_queues != nr_online_nodes) { |
weiping zhang | 558ab300 | 2017-08-03 00:26:39 +0800 | [diff] [blame] | 1952 | pr_warn("null_blk: submit_queues param is set to %u.\n", |
Matias Bjorling | d15ee6b | 2013-12-18 13:41:44 +0100 | [diff] [blame] | 1953 | nr_online_nodes); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1954 | g_submit_queues = nr_online_nodes; |
Matias Bjørling | fc1bc35 | 2013-12-21 00:11:01 +0100 | [diff] [blame] | 1955 | } |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1956 | } else if (g_submit_queues > nr_cpu_ids) |
| 1957 | g_submit_queues = nr_cpu_ids; |
| 1958 | else if (g_submit_queues <= 0) |
| 1959 | g_submit_queues = 1; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1960 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1961 | if (g_queue_mode == NULL_Q_MQ && shared_tags) { |
| 1962 | ret = null_init_tag_set(NULL, &tag_set); |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1963 | if (ret) |
| 1964 | return ret; |
| 1965 | } |
| 1966 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1967 | config_group_init(&nullb_subsys.su_group); |
| 1968 | mutex_init(&nullb_subsys.su_mutex); |
| 1969 | |
| 1970 | ret = configfs_register_subsystem(&nullb_subsys); |
| 1971 | if (ret) |
| 1972 | goto err_tagset; |
| 1973 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1974 | mutex_init(&lock); |
| 1975 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1976 | null_major = register_blkdev(0, "nullb"); |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1977 | if (null_major < 0) { |
| 1978 | ret = null_major; |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 1979 | goto err_conf; |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 1980 | } |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1981 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1982 | if (g_use_lightnvm) { |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1983 | ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64), |
| 1984 | 0, 0, NULL); |
| 1985 | if (!ppa_cache) { |
| 1986 | pr_err("null_blk: unable to create ppa cache\n"); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1987 | ret = -ENOMEM; |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 1988 | goto err_ppa; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 1989 | } |
| 1990 | } |
| 1991 | |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1992 | for (i = 0; i < nr_devices; i++) { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1993 | dev = null_alloc_dev(); |
| 1994 | if (!dev) |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 1995 | goto err_dev; |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 1996 | ret = null_add_dev(dev); |
| 1997 | if (ret) { |
| 1998 | null_free_dev(dev); |
| 1999 | goto err_dev; |
| 2000 | } |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2003 | pr_info("null: module loaded\n"); |
| 2004 | return 0; |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2005 | |
| 2006 | err_dev: |
| 2007 | while (!list_empty(&nullb_list)) { |
| 2008 | nullb = list_entry(nullb_list.next, struct nullb, list); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2009 | dev = nullb->dev; |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2010 | null_del_dev(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2011 | null_free_dev(dev); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2012 | } |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 2013 | kmem_cache_destroy(ppa_cache); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2014 | err_ppa: |
| 2015 | unregister_blkdev(null_major, "nullb"); |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 2016 | err_conf: |
| 2017 | configfs_unregister_subsystem(&nullb_subsys); |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 2018 | err_tagset: |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2019 | if (g_queue_mode == NULL_Q_MQ && shared_tags) |
Max Gurtovoy | db2d153 | 2017-07-06 18:00:07 +0300 | [diff] [blame] | 2020 | blk_mq_free_tag_set(&tag_set); |
Minfei Huang | af096e2 | 2015-12-08 13:47:34 -0700 | [diff] [blame] | 2021 | return ret; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | static void __exit null_exit(void) |
| 2025 | { |
| 2026 | struct nullb *nullb; |
| 2027 | |
Shaohua Li | 3bf2bd2 | 2017-08-14 15:04:53 -0700 | [diff] [blame] | 2028 | configfs_unregister_subsystem(&nullb_subsys); |
| 2029 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2030 | unregister_blkdev(null_major, "nullb"); |
| 2031 | |
| 2032 | mutex_lock(&lock); |
| 2033 | while (!list_empty(&nullb_list)) { |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2034 | struct nullb_device *dev; |
| 2035 | |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2036 | nullb = list_entry(nullb_list.next, struct nullb, list); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2037 | dev = nullb->dev; |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2038 | null_del_dev(nullb); |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2039 | null_free_dev(dev); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2040 | } |
| 2041 | mutex_unlock(&lock); |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 2042 | |
Shaohua Li | 2984c86 | 2017-08-14 15:04:52 -0700 | [diff] [blame] | 2043 | if (g_queue_mode == NULL_Q_MQ && shared_tags) |
Jens Axboe | 82f402f | 2017-06-20 14:22:01 -0600 | [diff] [blame] | 2044 | blk_mq_free_tag_set(&tag_set); |
| 2045 | |
Matias Bjørling | 6bb9535 | 2015-11-19 12:50:08 +0100 | [diff] [blame] | 2046 | kmem_cache_destroy(ppa_cache); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | module_init(null_init); |
| 2050 | module_exit(null_exit); |
| 2051 | |
Jens Axboe | 231b3db | 2017-08-25 12:53:15 -0600 | [diff] [blame] | 2052 | MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>"); |
Jens Axboe | f2298c0 | 2013-10-25 11:52:25 +0100 | [diff] [blame] | 2053 | MODULE_LICENSE("GPL"); |