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