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