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