xref: /linux/drivers/block/null_blk/main.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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 #undef pr_fmt
15 #define pr_fmt(fmt)	"null_blk: " fmt
16 
17 #define FREE_BATCH		16
18 
19 #define TICKS_PER_SEC		50ULL
20 #define TIMER_INTERVAL		(NSEC_PER_SEC / TICKS_PER_SEC)
21 
22 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
23 static DECLARE_FAULT_ATTR(null_timeout_attr);
24 static DECLARE_FAULT_ATTR(null_requeue_attr);
25 static DECLARE_FAULT_ATTR(null_init_hctx_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 static bool g_virt_boundary;
81 module_param_named(virt_boundary, g_virt_boundary, bool, 0444);
82 MODULE_PARM_DESC(virt_boundary, "Require a virtual boundary for the device. Default: False");
83 
84 static int g_no_sched;
85 module_param_named(no_sched, g_no_sched, int, 0444);
86 MODULE_PARM_DESC(no_sched, "No io scheduler");
87 
88 static int g_submit_queues = 1;
89 module_param_named(submit_queues, g_submit_queues, int, 0444);
90 MODULE_PARM_DESC(submit_queues, "Number of submission queues");
91 
92 static int g_poll_queues = 1;
93 module_param_named(poll_queues, g_poll_queues, int, 0444);
94 MODULE_PARM_DESC(poll_queues, "Number of IOPOLL submission queues");
95 
96 static int g_home_node = NUMA_NO_NODE;
97 module_param_named(home_node, g_home_node, int, 0444);
98 MODULE_PARM_DESC(home_node, "Home node for the device");
99 
100 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
101 /*
102  * For more details about fault injection, please refer to
103  * Documentation/fault-injection/fault-injection.rst.
104  */
105 static char g_timeout_str[80];
106 module_param_string(timeout, g_timeout_str, sizeof(g_timeout_str), 0444);
107 MODULE_PARM_DESC(timeout, "Fault injection. timeout=<interval>,<probability>,<space>,<times>");
108 
109 static char g_requeue_str[80];
110 module_param_string(requeue, g_requeue_str, sizeof(g_requeue_str), 0444);
111 MODULE_PARM_DESC(requeue, "Fault injection. requeue=<interval>,<probability>,<space>,<times>");
112 
113 static char g_init_hctx_str[80];
114 module_param_string(init_hctx, g_init_hctx_str, sizeof(g_init_hctx_str), 0444);
115 MODULE_PARM_DESC(init_hctx, "Fault injection to fail hctx init. init_hctx=<interval>,<probability>,<space>,<times>");
116 #endif
117 
118 /*
119  * Historic queue modes.
120  *
121  * These days nothing but NULL_Q_MQ is actually supported, but we keep it the
122  * enum for error reporting.
123  */
124 enum {
125 	NULL_Q_BIO	= 0,
126 	NULL_Q_RQ	= 1,
127 	NULL_Q_MQ	= 2,
128 };
129 
130 static int g_queue_mode = NULL_Q_MQ;
131 
null_param_store_val(const char * str,int * val,int min,int max)132 static int null_param_store_val(const char *str, int *val, int min, int max)
133 {
134 	int ret, new_val;
135 
136 	ret = kstrtoint(str, 10, &new_val);
137 	if (ret)
138 		return -EINVAL;
139 
140 	if (new_val < min || new_val > max)
141 		return -EINVAL;
142 
143 	*val = new_val;
144 	return 0;
145 }
146 
null_set_queue_mode(const char * str,const struct kernel_param * kp)147 static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
148 {
149 	return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
150 }
151 
152 static const struct kernel_param_ops null_queue_mode_param_ops = {
153 	.set	= null_set_queue_mode,
154 	.get	= param_get_int,
155 };
156 
157 device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, 0444);
158 MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
159 
160 static int g_gb = 250;
161 module_param_named(gb, g_gb, int, 0444);
162 MODULE_PARM_DESC(gb, "Size in GB");
163 
164 static int g_bs = 512;
165 module_param_named(bs, g_bs, int, 0444);
166 MODULE_PARM_DESC(bs, "Block size (in bytes)");
167 
168 static int g_max_sectors;
169 module_param_named(max_sectors, g_max_sectors, int, 0444);
170 MODULE_PARM_DESC(max_sectors, "Maximum size of a command (in 512B sectors)");
171 
172 static unsigned int nr_devices = 1;
173 module_param(nr_devices, uint, 0444);
174 MODULE_PARM_DESC(nr_devices, "Number of devices to register");
175 
176 static bool g_blocking;
177 module_param_named(blocking, g_blocking, bool, 0444);
178 MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
179 
180 static bool g_shared_tags;
181 module_param_named(shared_tags, g_shared_tags, bool, 0444);
182 MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");
183 
184 static bool g_shared_tag_bitmap;
185 module_param_named(shared_tag_bitmap, g_shared_tag_bitmap, bool, 0444);
186 MODULE_PARM_DESC(shared_tag_bitmap, "Use shared tag bitmap for all submission queues for blk-mq");
187 
188 static int g_irqmode = NULL_IRQ_SOFTIRQ;
189 
null_set_irqmode(const char * str,const struct kernel_param * kp)190 static int null_set_irqmode(const char *str, const struct kernel_param *kp)
191 {
192 	return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
193 					NULL_IRQ_TIMER);
194 }
195 
196 static const struct kernel_param_ops null_irqmode_param_ops = {
197 	.set	= null_set_irqmode,
198 	.get	= param_get_int,
199 };
200 
201 device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, 0444);
202 MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
203 
204 static unsigned long g_completion_nsec = 10000;
205 module_param_named(completion_nsec, g_completion_nsec, ulong, 0444);
206 MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
207 
208 static int g_hw_queue_depth = 64;
209 module_param_named(hw_queue_depth, g_hw_queue_depth, int, 0444);
210 MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
211 
212 static bool g_use_per_node_hctx;
213 module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, 0444);
214 MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
215 
216 static bool g_memory_backed;
217 module_param_named(memory_backed, g_memory_backed, bool, 0444);
218 MODULE_PARM_DESC(memory_backed, "Create a memory-backed block device. Default: false");
219 
220 static bool g_discard;
221 module_param_named(discard, g_discard, bool, 0444);
222 MODULE_PARM_DESC(discard, "Support discard operations (requires memory-backed null_blk device). Default: false");
223 
224 static unsigned long g_cache_size;
225 module_param_named(cache_size, g_cache_size, ulong, 0444);
226 MODULE_PARM_DESC(mbps, "Cache size in MiB for memory-backed device. Default: 0 (none)");
227 
228 static bool g_fua = true;
229 module_param_named(fua, g_fua, bool, 0444);
230 MODULE_PARM_DESC(fua, "Enable/disable FUA support when cache_size is used. Default: true");
231 
232 static unsigned int g_mbps;
233 module_param_named(mbps, g_mbps, uint, 0444);
234 MODULE_PARM_DESC(mbps, "Limit maximum bandwidth (in MiB/s). Default: 0 (no limit)");
235 
236 static bool g_zoned;
237 module_param_named(zoned, g_zoned, bool, S_IRUGO);
238 MODULE_PARM_DESC(zoned, "Make device as a host-managed zoned block device. Default: false");
239 
240 static unsigned long g_zone_size = 256;
241 module_param_named(zone_size, g_zone_size, ulong, S_IRUGO);
242 MODULE_PARM_DESC(zone_size, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");
243 
244 static unsigned long g_zone_capacity;
245 module_param_named(zone_capacity, g_zone_capacity, ulong, 0444);
246 MODULE_PARM_DESC(zone_capacity, "Zone capacity in MB when block device is zoned. Can be less than or equal to zone size. Default: Zone size");
247 
248 static unsigned int g_zone_nr_conv;
249 module_param_named(zone_nr_conv, g_zone_nr_conv, uint, 0444);
250 MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones when block device is zoned. Default: 0");
251 
252 static unsigned int g_zone_max_open;
253 module_param_named(zone_max_open, g_zone_max_open, uint, 0444);
254 MODULE_PARM_DESC(zone_max_open, "Maximum number of open zones when block device is zoned. Default: 0 (no limit)");
255 
256 static unsigned int g_zone_max_active;
257 module_param_named(zone_max_active, g_zone_max_active, uint, 0444);
258 MODULE_PARM_DESC(zone_max_active, "Maximum number of active zones when block device is zoned. Default: 0 (no limit)");
259 
260 static int g_zone_append_max_sectors = INT_MAX;
261 module_param_named(zone_append_max_sectors, g_zone_append_max_sectors, int, 0444);
262 MODULE_PARM_DESC(zone_append_max_sectors,
263 		 "Maximum size of a zone append command (in 512B sectors). Specify 0 for zone append emulation");
264 
265 static bool g_zone_full;
266 module_param_named(zone_full, g_zone_full, bool, S_IRUGO);
267 MODULE_PARM_DESC(zone_full, "Initialize the sequential write required zones of a zoned device to be full. Default: false");
268 
269 static struct nullb_device *null_alloc_dev(void);
270 static void null_free_dev(struct nullb_device *dev);
271 static void null_del_dev(struct nullb *nullb);
272 static int null_add_dev(struct nullb_device *dev);
273 static struct nullb *null_find_dev_by_name(const char *name);
274 static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
275 
to_nullb_device(struct config_item * item)276 static inline struct nullb_device *to_nullb_device(struct config_item *item)
277 {
278 	return item ? container_of(to_config_group(item), struct nullb_device, group) : NULL;
279 }
280 
nullb_device_uint_attr_show(unsigned int val,char * page)281 static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page)
282 {
283 	return snprintf(page, PAGE_SIZE, "%u\n", val);
284 }
285 
nullb_device_ulong_attr_show(unsigned long val,char * page)286 static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
287 	char *page)
288 {
289 	return snprintf(page, PAGE_SIZE, "%lu\n", val);
290 }
291 
nullb_device_bool_attr_show(bool val,char * page)292 static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
293 {
294 	return snprintf(page, PAGE_SIZE, "%u\n", val);
295 }
296 
nullb_device_uint_attr_store(unsigned int * val,const char * page,size_t count)297 static ssize_t nullb_device_uint_attr_store(unsigned int *val,
298 	const char *page, size_t count)
299 {
300 	unsigned int tmp;
301 	int result;
302 
303 	result = kstrtouint(page, 0, &tmp);
304 	if (result < 0)
305 		return result;
306 
307 	*val = tmp;
308 	return count;
309 }
310 
nullb_device_ulong_attr_store(unsigned long * val,const char * page,size_t count)311 static ssize_t nullb_device_ulong_attr_store(unsigned long *val,
312 	const char *page, size_t count)
313 {
314 	int result;
315 	unsigned long tmp;
316 
317 	result = kstrtoul(page, 0, &tmp);
318 	if (result < 0)
319 		return result;
320 
321 	*val = tmp;
322 	return count;
323 }
324 
nullb_device_bool_attr_store(bool * val,const char * page,size_t count)325 static ssize_t nullb_device_bool_attr_store(bool *val, const char *page,
326 	size_t count)
327 {
328 	bool tmp;
329 	int result;
330 
331 	result = kstrtobool(page,  &tmp);
332 	if (result < 0)
333 		return result;
334 
335 	*val = tmp;
336 	return count;
337 }
338 
339 /* The following macro should only be used with TYPE = {uint, ulong, bool}. */
340 #define NULLB_DEVICE_ATTR(NAME, TYPE, APPLY)				\
341 static ssize_t								\
342 nullb_device_##NAME##_show(struct config_item *item, char *page)	\
343 {									\
344 	return nullb_device_##TYPE##_attr_show(				\
345 				to_nullb_device(item)->NAME, page);	\
346 }									\
347 static ssize_t								\
348 nullb_device_##NAME##_store(struct config_item *item, const char *page,	\
349 			    size_t count)				\
350 {									\
351 	int (*apply_fn)(struct nullb_device *dev, TYPE new_value) = APPLY;\
352 	struct nullb_device *dev = to_nullb_device(item);		\
353 	TYPE new_value = 0;						\
354 	int ret;							\
355 									\
356 	ret = nullb_device_##TYPE##_attr_store(&new_value, page, count);\
357 	if (ret < 0)							\
358 		return ret;						\
359 	if (apply_fn)							\
360 		ret = apply_fn(dev, new_value);				\
361 	else if (test_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags)) 	\
362 		ret = -EBUSY;						\
363 	if (ret < 0)							\
364 		return ret;						\
365 	dev->NAME = new_value;						\
366 	return count;							\
367 }									\
368 CONFIGFS_ATTR(nullb_device_, NAME);
369 
nullb_update_nr_hw_queues(struct nullb_device * dev,unsigned int submit_queues,unsigned int poll_queues)370 static int nullb_update_nr_hw_queues(struct nullb_device *dev,
371 				     unsigned int submit_queues,
372 				     unsigned int poll_queues)
373 
374 {
375 	struct blk_mq_tag_set *set;
376 	int ret, nr_hw_queues;
377 
378 	if (!dev->nullb)
379 		return 0;
380 
381 	/*
382 	 * Make sure at least one submit queue exists.
383 	 */
384 	if (!submit_queues)
385 		return -EINVAL;
386 
387 	/*
388 	 * Make sure that null_init_hctx() does not access nullb->queues[] past
389 	 * the end of that array.
390 	 */
391 	if (submit_queues > nr_cpu_ids || poll_queues > g_poll_queues)
392 		return -EINVAL;
393 
394 	/*
395 	 * Keep previous and new queue numbers in nullb_device for reference in
396 	 * the call back function null_map_queues().
397 	 */
398 	dev->prev_submit_queues = dev->submit_queues;
399 	dev->prev_poll_queues = dev->poll_queues;
400 	dev->submit_queues = submit_queues;
401 	dev->poll_queues = poll_queues;
402 
403 	set = dev->nullb->tag_set;
404 	nr_hw_queues = submit_queues + poll_queues;
405 	blk_mq_update_nr_hw_queues(set, nr_hw_queues);
406 	ret = set->nr_hw_queues == nr_hw_queues ? 0 : -ENOMEM;
407 
408 	if (ret) {
409 		/* on error, revert the queue numbers */
410 		dev->submit_queues = dev->prev_submit_queues;
411 		dev->poll_queues = dev->prev_poll_queues;
412 	}
413 
414 	return ret;
415 }
416 
nullb_apply_submit_queues(struct nullb_device * dev,unsigned int submit_queues)417 static int nullb_apply_submit_queues(struct nullb_device *dev,
418 				     unsigned int submit_queues)
419 {
420 	int ret;
421 
422 	mutex_lock(&lock);
423 	ret = nullb_update_nr_hw_queues(dev, submit_queues, dev->poll_queues);
424 	mutex_unlock(&lock);
425 
426 	return ret;
427 }
428 
nullb_apply_poll_queues(struct nullb_device * dev,unsigned int poll_queues)429 static int nullb_apply_poll_queues(struct nullb_device *dev,
430 				   unsigned int poll_queues)
431 {
432 	int ret;
433 
434 	mutex_lock(&lock);
435 	ret = nullb_update_nr_hw_queues(dev, dev->submit_queues, poll_queues);
436 	mutex_unlock(&lock);
437 
438 	return ret;
439 }
440 
441 NULLB_DEVICE_ATTR(size, ulong, NULL);
442 NULLB_DEVICE_ATTR(completion_nsec, ulong, NULL);
443 NULLB_DEVICE_ATTR(submit_queues, uint, nullb_apply_submit_queues);
444 NULLB_DEVICE_ATTR(poll_queues, uint, nullb_apply_poll_queues);
445 NULLB_DEVICE_ATTR(home_node, uint, NULL);
446 NULLB_DEVICE_ATTR(queue_mode, uint, NULL);
447 NULLB_DEVICE_ATTR(blocksize, uint, NULL);
448 NULLB_DEVICE_ATTR(max_sectors, uint, NULL);
449 NULLB_DEVICE_ATTR(irqmode, uint, NULL);
450 NULLB_DEVICE_ATTR(hw_queue_depth, uint, NULL);
451 NULLB_DEVICE_ATTR(index, uint, NULL);
452 NULLB_DEVICE_ATTR(blocking, bool, NULL);
453 NULLB_DEVICE_ATTR(use_per_node_hctx, bool, NULL);
454 NULLB_DEVICE_ATTR(memory_backed, bool, NULL);
455 NULLB_DEVICE_ATTR(discard, bool, NULL);
456 NULLB_DEVICE_ATTR(mbps, uint, NULL);
457 NULLB_DEVICE_ATTR(cache_size, ulong, NULL);
458 NULLB_DEVICE_ATTR(zoned, bool, NULL);
459 NULLB_DEVICE_ATTR(zone_size, ulong, NULL);
460 NULLB_DEVICE_ATTR(zone_capacity, ulong, NULL);
461 NULLB_DEVICE_ATTR(zone_nr_conv, uint, NULL);
462 NULLB_DEVICE_ATTR(zone_max_open, uint, NULL);
463 NULLB_DEVICE_ATTR(zone_max_active, uint, NULL);
464 NULLB_DEVICE_ATTR(zone_append_max_sectors, uint, NULL);
465 NULLB_DEVICE_ATTR(zone_full, bool, NULL);
466 NULLB_DEVICE_ATTR(virt_boundary, bool, NULL);
467 NULLB_DEVICE_ATTR(no_sched, bool, NULL);
468 NULLB_DEVICE_ATTR(shared_tags, bool, NULL);
469 NULLB_DEVICE_ATTR(shared_tag_bitmap, bool, NULL);
470 NULLB_DEVICE_ATTR(fua, bool, NULL);
471 
nullb_device_power_show(struct config_item * item,char * page)472 static ssize_t nullb_device_power_show(struct config_item *item, char *page)
473 {
474 	return nullb_device_bool_attr_show(to_nullb_device(item)->power, page);
475 }
476 
nullb_device_power_store(struct config_item * item,const char * page,size_t count)477 static ssize_t nullb_device_power_store(struct config_item *item,
478 				     const char *page, size_t count)
479 {
480 	struct nullb_device *dev = to_nullb_device(item);
481 	bool newp = false;
482 	ssize_t ret;
483 
484 	ret = nullb_device_bool_attr_store(&newp, page, count);
485 	if (ret < 0)
486 		return ret;
487 
488 	ret = count;
489 	mutex_lock(&lock);
490 	if (!dev->power && newp) {
491 		if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
492 			goto out;
493 
494 		ret = null_add_dev(dev);
495 		if (ret) {
496 			clear_bit(NULLB_DEV_FL_UP, &dev->flags);
497 			goto out;
498 		}
499 
500 		set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
501 		dev->power = newp;
502 		ret = count;
503 	} else if (dev->power && !newp) {
504 		if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
505 			dev->power = newp;
506 			null_del_dev(dev->nullb);
507 		}
508 		clear_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
509 	}
510 
511 out:
512 	mutex_unlock(&lock);
513 	return ret;
514 }
515 
516 CONFIGFS_ATTR(nullb_device_, power);
517 
nullb_device_badblocks_show(struct config_item * item,char * page)518 static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page)
519 {
520 	struct nullb_device *t_dev = to_nullb_device(item);
521 
522 	return badblocks_show(&t_dev->badblocks, page, 0);
523 }
524 
nullb_device_badblocks_store(struct config_item * item,const char * page,size_t count)525 static ssize_t nullb_device_badblocks_store(struct config_item *item,
526 				     const char *page, size_t count)
527 {
528 	struct nullb_device *t_dev = to_nullb_device(item);
529 	char *orig, *buf, *tmp;
530 	u64 start, end;
531 	int ret;
532 
533 	orig = kstrndup(page, count, GFP_KERNEL);
534 	if (!orig)
535 		return -ENOMEM;
536 
537 	buf = strstrip(orig);
538 
539 	ret = -EINVAL;
540 	if (buf[0] != '+' && buf[0] != '-')
541 		goto out;
542 	tmp = strchr(&buf[1], '-');
543 	if (!tmp)
544 		goto out;
545 	*tmp = '\0';
546 	ret = kstrtoull(buf + 1, 0, &start);
547 	if (ret)
548 		goto out;
549 	ret = kstrtoull(tmp + 1, 0, &end);
550 	if (ret)
551 		goto out;
552 	ret = -EINVAL;
553 	if (start > end)
554 		goto out;
555 	/* enable badblocks */
556 	cmpxchg(&t_dev->badblocks.shift, -1, 0);
557 	if (buf[0] == '+')
558 		ret = badblocks_set(&t_dev->badblocks, start,
559 			end - start + 1, 1);
560 	else
561 		ret = badblocks_clear(&t_dev->badblocks, start,
562 			end - start + 1);
563 	if (ret == 0)
564 		ret = count;
565 out:
566 	kfree(orig);
567 	return ret;
568 }
569 CONFIGFS_ATTR(nullb_device_, badblocks);
570 
nullb_device_zone_readonly_store(struct config_item * item,const char * page,size_t count)571 static ssize_t nullb_device_zone_readonly_store(struct config_item *item,
572 						const char *page, size_t count)
573 {
574 	struct nullb_device *dev = to_nullb_device(item);
575 
576 	return zone_cond_store(dev, page, count, BLK_ZONE_COND_READONLY);
577 }
578 CONFIGFS_ATTR_WO(nullb_device_, zone_readonly);
579 
nullb_device_zone_offline_store(struct config_item * item,const char * page,size_t count)580 static ssize_t nullb_device_zone_offline_store(struct config_item *item,
581 					       const char *page, size_t count)
582 {
583 	struct nullb_device *dev = to_nullb_device(item);
584 
585 	return zone_cond_store(dev, page, count, BLK_ZONE_COND_OFFLINE);
586 }
587 CONFIGFS_ATTR_WO(nullb_device_, zone_offline);
588 
589 static struct configfs_attribute *nullb_device_attrs[] = {
590 	&nullb_device_attr_size,
591 	&nullb_device_attr_completion_nsec,
592 	&nullb_device_attr_submit_queues,
593 	&nullb_device_attr_poll_queues,
594 	&nullb_device_attr_home_node,
595 	&nullb_device_attr_queue_mode,
596 	&nullb_device_attr_blocksize,
597 	&nullb_device_attr_max_sectors,
598 	&nullb_device_attr_irqmode,
599 	&nullb_device_attr_hw_queue_depth,
600 	&nullb_device_attr_index,
601 	&nullb_device_attr_blocking,
602 	&nullb_device_attr_use_per_node_hctx,
603 	&nullb_device_attr_power,
604 	&nullb_device_attr_memory_backed,
605 	&nullb_device_attr_discard,
606 	&nullb_device_attr_mbps,
607 	&nullb_device_attr_cache_size,
608 	&nullb_device_attr_badblocks,
609 	&nullb_device_attr_zoned,
610 	&nullb_device_attr_zone_size,
611 	&nullb_device_attr_zone_capacity,
612 	&nullb_device_attr_zone_nr_conv,
613 	&nullb_device_attr_zone_max_open,
614 	&nullb_device_attr_zone_max_active,
615 	&nullb_device_attr_zone_append_max_sectors,
616 	&nullb_device_attr_zone_readonly,
617 	&nullb_device_attr_zone_offline,
618 	&nullb_device_attr_zone_full,
619 	&nullb_device_attr_virt_boundary,
620 	&nullb_device_attr_no_sched,
621 	&nullb_device_attr_shared_tags,
622 	&nullb_device_attr_shared_tag_bitmap,
623 	&nullb_device_attr_fua,
624 	NULL,
625 };
626 
nullb_device_release(struct config_item * item)627 static void nullb_device_release(struct config_item *item)
628 {
629 	struct nullb_device *dev = to_nullb_device(item);
630 
631 	null_free_device_storage(dev, false);
632 	null_free_dev(dev);
633 }
634 
635 static struct configfs_item_operations nullb_device_ops = {
636 	.release	= nullb_device_release,
637 };
638 
639 static const struct config_item_type nullb_device_type = {
640 	.ct_item_ops	= &nullb_device_ops,
641 	.ct_attrs	= nullb_device_attrs,
642 	.ct_owner	= THIS_MODULE,
643 };
644 
645 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
646 
nullb_add_fault_config(struct nullb_device * dev)647 static void nullb_add_fault_config(struct nullb_device *dev)
648 {
649 	fault_config_init(&dev->timeout_config, "timeout_inject");
650 	fault_config_init(&dev->requeue_config, "requeue_inject");
651 	fault_config_init(&dev->init_hctx_fault_config, "init_hctx_fault_inject");
652 
653 	configfs_add_default_group(&dev->timeout_config.group, &dev->group);
654 	configfs_add_default_group(&dev->requeue_config.group, &dev->group);
655 	configfs_add_default_group(&dev->init_hctx_fault_config.group, &dev->group);
656 }
657 
658 #else
659 
nullb_add_fault_config(struct nullb_device * dev)660 static void nullb_add_fault_config(struct nullb_device *dev)
661 {
662 }
663 
664 #endif
665 
666 static struct
nullb_group_make_group(struct config_group * group,const char * name)667 config_group *nullb_group_make_group(struct config_group *group, const char *name)
668 {
669 	struct nullb_device *dev;
670 
671 	if (null_find_dev_by_name(name))
672 		return ERR_PTR(-EEXIST);
673 
674 	dev = null_alloc_dev();
675 	if (!dev)
676 		return ERR_PTR(-ENOMEM);
677 
678 	config_group_init_type_name(&dev->group, name, &nullb_device_type);
679 	nullb_add_fault_config(dev);
680 
681 	return &dev->group;
682 }
683 
684 static void
nullb_group_drop_item(struct config_group * group,struct config_item * item)685 nullb_group_drop_item(struct config_group *group, struct config_item *item)
686 {
687 	struct nullb_device *dev = to_nullb_device(item);
688 
689 	if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
690 		mutex_lock(&lock);
691 		dev->power = false;
692 		null_del_dev(dev->nullb);
693 		mutex_unlock(&lock);
694 	}
695 
696 	config_item_put(item);
697 }
698 
memb_group_features_show(struct config_item * item,char * page)699 static ssize_t memb_group_features_show(struct config_item *item, char *page)
700 {
701 	return snprintf(page, PAGE_SIZE,
702 			"badblocks,blocking,blocksize,cache_size,fua,"
703 			"completion_nsec,discard,home_node,hw_queue_depth,"
704 			"irqmode,max_sectors,mbps,memory_backed,no_sched,"
705 			"poll_queues,power,queue_mode,shared_tag_bitmap,"
706 			"shared_tags,size,submit_queues,use_per_node_hctx,"
707 			"virt_boundary,zoned,zone_capacity,zone_max_active,"
708 			"zone_max_open,zone_nr_conv,zone_offline,zone_readonly,"
709 			"zone_size,zone_append_max_sectors,zone_full\n");
710 }
711 
712 CONFIGFS_ATTR_RO(memb_group_, features);
713 
714 static struct configfs_attribute *nullb_group_attrs[] = {
715 	&memb_group_attr_features,
716 	NULL,
717 };
718 
719 static struct configfs_group_operations nullb_group_ops = {
720 	.make_group	= nullb_group_make_group,
721 	.drop_item	= nullb_group_drop_item,
722 };
723 
724 static const struct config_item_type nullb_group_type = {
725 	.ct_group_ops	= &nullb_group_ops,
726 	.ct_attrs	= nullb_group_attrs,
727 	.ct_owner	= THIS_MODULE,
728 };
729 
730 static struct configfs_subsystem nullb_subsys = {
731 	.su_group = {
732 		.cg_item = {
733 			.ci_namebuf = "nullb",
734 			.ci_type = &nullb_group_type,
735 		},
736 	},
737 };
738 
null_cache_active(struct nullb * nullb)739 static inline int null_cache_active(struct nullb *nullb)
740 {
741 	return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
742 }
743 
null_alloc_dev(void)744 static struct nullb_device *null_alloc_dev(void)
745 {
746 	struct nullb_device *dev;
747 
748 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
749 	if (!dev)
750 		return NULL;
751 
752 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
753 	dev->timeout_config.attr = null_timeout_attr;
754 	dev->requeue_config.attr = null_requeue_attr;
755 	dev->init_hctx_fault_config.attr = null_init_hctx_attr;
756 #endif
757 
758 	INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
759 	INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
760 	if (badblocks_init(&dev->badblocks, 0)) {
761 		kfree(dev);
762 		return NULL;
763 	}
764 
765 	dev->size = g_gb * 1024;
766 	dev->completion_nsec = g_completion_nsec;
767 	dev->submit_queues = g_submit_queues;
768 	dev->prev_submit_queues = g_submit_queues;
769 	dev->poll_queues = g_poll_queues;
770 	dev->prev_poll_queues = g_poll_queues;
771 	dev->home_node = g_home_node;
772 	dev->queue_mode = g_queue_mode;
773 	dev->blocksize = g_bs;
774 	dev->max_sectors = g_max_sectors;
775 	dev->irqmode = g_irqmode;
776 	dev->hw_queue_depth = g_hw_queue_depth;
777 	dev->blocking = g_blocking;
778 	dev->memory_backed = g_memory_backed;
779 	dev->discard = g_discard;
780 	dev->cache_size = g_cache_size;
781 	dev->mbps = g_mbps;
782 	dev->use_per_node_hctx = g_use_per_node_hctx;
783 	dev->zoned = g_zoned;
784 	dev->zone_size = g_zone_size;
785 	dev->zone_capacity = g_zone_capacity;
786 	dev->zone_nr_conv = g_zone_nr_conv;
787 	dev->zone_max_open = g_zone_max_open;
788 	dev->zone_max_active = g_zone_max_active;
789 	dev->zone_append_max_sectors = g_zone_append_max_sectors;
790 	dev->zone_full = g_zone_full;
791 	dev->virt_boundary = g_virt_boundary;
792 	dev->no_sched = g_no_sched;
793 	dev->shared_tags = g_shared_tags;
794 	dev->shared_tag_bitmap = g_shared_tag_bitmap;
795 	dev->fua = g_fua;
796 
797 	return dev;
798 }
799 
null_free_dev(struct nullb_device * dev)800 static void null_free_dev(struct nullb_device *dev)
801 {
802 	if (!dev)
803 		return;
804 
805 	null_free_zoned_dev(dev);
806 	badblocks_exit(&dev->badblocks);
807 	kfree(dev);
808 }
809 
null_cmd_timer_expired(struct hrtimer * timer)810 static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
811 {
812 	struct nullb_cmd *cmd = container_of(timer, struct nullb_cmd, timer);
813 
814 	blk_mq_end_request(blk_mq_rq_from_pdu(cmd), cmd->error);
815 	return HRTIMER_NORESTART;
816 }
817 
null_cmd_end_timer(struct nullb_cmd * cmd)818 static void null_cmd_end_timer(struct nullb_cmd *cmd)
819 {
820 	ktime_t kt = cmd->nq->dev->completion_nsec;
821 
822 	hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
823 }
824 
null_complete_rq(struct request * rq)825 static void null_complete_rq(struct request *rq)
826 {
827 	struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
828 
829 	blk_mq_end_request(rq, cmd->error);
830 }
831 
null_alloc_page(void)832 static struct nullb_page *null_alloc_page(void)
833 {
834 	struct nullb_page *t_page;
835 
836 	t_page = kmalloc(sizeof(struct nullb_page), GFP_NOIO);
837 	if (!t_page)
838 		return NULL;
839 
840 	t_page->page = alloc_pages(GFP_NOIO, 0);
841 	if (!t_page->page) {
842 		kfree(t_page);
843 		return NULL;
844 	}
845 
846 	memset(t_page->bitmap, 0, sizeof(t_page->bitmap));
847 	return t_page;
848 }
849 
null_free_page(struct nullb_page * t_page)850 static void null_free_page(struct nullb_page *t_page)
851 {
852 	__set_bit(NULLB_PAGE_FREE, t_page->bitmap);
853 	if (test_bit(NULLB_PAGE_LOCK, t_page->bitmap))
854 		return;
855 	__free_page(t_page->page);
856 	kfree(t_page);
857 }
858 
null_page_empty(struct nullb_page * page)859 static bool null_page_empty(struct nullb_page *page)
860 {
861 	int size = MAP_SZ - 2;
862 
863 	return find_first_bit(page->bitmap, size) == size;
864 }
865 
null_free_sector(struct nullb * nullb,sector_t sector,bool is_cache)866 static void null_free_sector(struct nullb *nullb, sector_t sector,
867 	bool is_cache)
868 {
869 	unsigned int sector_bit;
870 	u64 idx;
871 	struct nullb_page *t_page, *ret;
872 	struct radix_tree_root *root;
873 
874 	root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
875 	idx = sector >> PAGE_SECTORS_SHIFT;
876 	sector_bit = (sector & SECTOR_MASK);
877 
878 	t_page = radix_tree_lookup(root, idx);
879 	if (t_page) {
880 		__clear_bit(sector_bit, t_page->bitmap);
881 
882 		if (null_page_empty(t_page)) {
883 			ret = radix_tree_delete_item(root, idx, t_page);
884 			WARN_ON(ret != t_page);
885 			null_free_page(ret);
886 			if (is_cache)
887 				nullb->dev->curr_cache -= PAGE_SIZE;
888 		}
889 	}
890 }
891 
null_radix_tree_insert(struct nullb * nullb,u64 idx,struct nullb_page * t_page,bool is_cache)892 static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
893 	struct nullb_page *t_page, bool is_cache)
894 {
895 	struct radix_tree_root *root;
896 
897 	root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
898 
899 	if (radix_tree_insert(root, idx, t_page)) {
900 		null_free_page(t_page);
901 		t_page = radix_tree_lookup(root, idx);
902 		WARN_ON(!t_page || t_page->page->index != idx);
903 	} else if (is_cache)
904 		nullb->dev->curr_cache += PAGE_SIZE;
905 
906 	return t_page;
907 }
908 
null_free_device_storage(struct nullb_device * dev,bool is_cache)909 static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
910 {
911 	unsigned long pos = 0;
912 	int nr_pages;
913 	struct nullb_page *ret, *t_pages[FREE_BATCH];
914 	struct radix_tree_root *root;
915 
916 	root = is_cache ? &dev->cache : &dev->data;
917 
918 	do {
919 		int i;
920 
921 		nr_pages = radix_tree_gang_lookup(root,
922 				(void **)t_pages, pos, FREE_BATCH);
923 
924 		for (i = 0; i < nr_pages; i++) {
925 			pos = t_pages[i]->page->index;
926 			ret = radix_tree_delete_item(root, pos, t_pages[i]);
927 			WARN_ON(ret != t_pages[i]);
928 			null_free_page(ret);
929 		}
930 
931 		pos++;
932 	} while (nr_pages == FREE_BATCH);
933 
934 	if (is_cache)
935 		dev->curr_cache = 0;
936 }
937 
__null_lookup_page(struct nullb * nullb,sector_t sector,bool for_write,bool is_cache)938 static struct nullb_page *__null_lookup_page(struct nullb *nullb,
939 	sector_t sector, bool for_write, bool is_cache)
940 {
941 	unsigned int sector_bit;
942 	u64 idx;
943 	struct nullb_page *t_page;
944 	struct radix_tree_root *root;
945 
946 	idx = sector >> PAGE_SECTORS_SHIFT;
947 	sector_bit = (sector & SECTOR_MASK);
948 
949 	root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
950 	t_page = radix_tree_lookup(root, idx);
951 	WARN_ON(t_page && t_page->page->index != idx);
952 
953 	if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
954 		return t_page;
955 
956 	return NULL;
957 }
958 
null_lookup_page(struct nullb * nullb,sector_t sector,bool for_write,bool ignore_cache)959 static struct nullb_page *null_lookup_page(struct nullb *nullb,
960 	sector_t sector, bool for_write, bool ignore_cache)
961 {
962 	struct nullb_page *page = NULL;
963 
964 	if (!ignore_cache)
965 		page = __null_lookup_page(nullb, sector, for_write, true);
966 	if (page)
967 		return page;
968 	return __null_lookup_page(nullb, sector, for_write, false);
969 }
970 
null_insert_page(struct nullb * nullb,sector_t sector,bool ignore_cache)971 static struct nullb_page *null_insert_page(struct nullb *nullb,
972 					   sector_t sector, bool ignore_cache)
973 	__releases(&nullb->lock)
974 	__acquires(&nullb->lock)
975 {
976 	u64 idx;
977 	struct nullb_page *t_page;
978 
979 	t_page = null_lookup_page(nullb, sector, true, ignore_cache);
980 	if (t_page)
981 		return t_page;
982 
983 	spin_unlock_irq(&nullb->lock);
984 
985 	t_page = null_alloc_page();
986 	if (!t_page)
987 		goto out_lock;
988 
989 	if (radix_tree_preload(GFP_NOIO))
990 		goto out_freepage;
991 
992 	spin_lock_irq(&nullb->lock);
993 	idx = sector >> PAGE_SECTORS_SHIFT;
994 	t_page->page->index = idx;
995 	t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
996 	radix_tree_preload_end();
997 
998 	return t_page;
999 out_freepage:
1000 	null_free_page(t_page);
1001 out_lock:
1002 	spin_lock_irq(&nullb->lock);
1003 	return null_lookup_page(nullb, sector, true, ignore_cache);
1004 }
1005 
null_flush_cache_page(struct nullb * nullb,struct nullb_page * c_page)1006 static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
1007 {
1008 	int i;
1009 	unsigned int offset;
1010 	u64 idx;
1011 	struct nullb_page *t_page, *ret;
1012 	void *dst, *src;
1013 
1014 	idx = c_page->page->index;
1015 
1016 	t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);
1017 
1018 	__clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
1019 	if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
1020 		null_free_page(c_page);
1021 		if (t_page && null_page_empty(t_page)) {
1022 			ret = radix_tree_delete_item(&nullb->dev->data,
1023 				idx, t_page);
1024 			null_free_page(t_page);
1025 		}
1026 		return 0;
1027 	}
1028 
1029 	if (!t_page)
1030 		return -ENOMEM;
1031 
1032 	src = kmap_local_page(c_page->page);
1033 	dst = kmap_local_page(t_page->page);
1034 
1035 	for (i = 0; i < PAGE_SECTORS;
1036 			i += (nullb->dev->blocksize >> SECTOR_SHIFT)) {
1037 		if (test_bit(i, c_page->bitmap)) {
1038 			offset = (i << SECTOR_SHIFT);
1039 			memcpy(dst + offset, src + offset,
1040 				nullb->dev->blocksize);
1041 			__set_bit(i, t_page->bitmap);
1042 		}
1043 	}
1044 
1045 	kunmap_local(dst);
1046 	kunmap_local(src);
1047 
1048 	ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page);
1049 	null_free_page(ret);
1050 	nullb->dev->curr_cache -= PAGE_SIZE;
1051 
1052 	return 0;
1053 }
1054 
null_make_cache_space(struct nullb * nullb,unsigned long n)1055 static int null_make_cache_space(struct nullb *nullb, unsigned long n)
1056 {
1057 	int i, err, nr_pages;
1058 	struct nullb_page *c_pages[FREE_BATCH];
1059 	unsigned long flushed = 0, one_round;
1060 
1061 again:
1062 	if ((nullb->dev->cache_size * 1024 * 1024) >
1063 	     nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0)
1064 		return 0;
1065 
1066 	nr_pages = radix_tree_gang_lookup(&nullb->dev->cache,
1067 			(void **)c_pages, nullb->cache_flush_pos, FREE_BATCH);
1068 	/*
1069 	 * nullb_flush_cache_page could unlock before using the c_pages. To
1070 	 * avoid race, we don't allow page free
1071 	 */
1072 	for (i = 0; i < nr_pages; i++) {
1073 		nullb->cache_flush_pos = c_pages[i]->page->index;
1074 		/*
1075 		 * We found the page which is being flushed to disk by other
1076 		 * threads
1077 		 */
1078 		if (test_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap))
1079 			c_pages[i] = NULL;
1080 		else
1081 			__set_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap);
1082 	}
1083 
1084 	one_round = 0;
1085 	for (i = 0; i < nr_pages; i++) {
1086 		if (c_pages[i] == NULL)
1087 			continue;
1088 		err = null_flush_cache_page(nullb, c_pages[i]);
1089 		if (err)
1090 			return err;
1091 		one_round++;
1092 	}
1093 	flushed += one_round << PAGE_SHIFT;
1094 
1095 	if (n > flushed) {
1096 		if (nr_pages == 0)
1097 			nullb->cache_flush_pos = 0;
1098 		if (one_round == 0) {
1099 			/* give other threads a chance */
1100 			spin_unlock_irq(&nullb->lock);
1101 			spin_lock_irq(&nullb->lock);
1102 		}
1103 		goto again;
1104 	}
1105 	return 0;
1106 }
1107 
copy_to_nullb(struct nullb * nullb,struct page * source,unsigned int off,sector_t sector,size_t n,bool is_fua)1108 static int copy_to_nullb(struct nullb *nullb, struct page *source,
1109 	unsigned int off, sector_t sector, size_t n, bool is_fua)
1110 {
1111 	size_t temp, count = 0;
1112 	unsigned int offset;
1113 	struct nullb_page *t_page;
1114 
1115 	while (count < n) {
1116 		temp = min_t(size_t, nullb->dev->blocksize, n - count);
1117 
1118 		if (null_cache_active(nullb) && !is_fua)
1119 			null_make_cache_space(nullb, PAGE_SIZE);
1120 
1121 		offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
1122 		t_page = null_insert_page(nullb, sector,
1123 			!null_cache_active(nullb) || is_fua);
1124 		if (!t_page)
1125 			return -ENOSPC;
1126 
1127 		memcpy_page(t_page->page, offset, source, off + count, temp);
1128 
1129 		__set_bit(sector & SECTOR_MASK, t_page->bitmap);
1130 
1131 		if (is_fua)
1132 			null_free_sector(nullb, sector, true);
1133 
1134 		count += temp;
1135 		sector += temp >> SECTOR_SHIFT;
1136 	}
1137 	return 0;
1138 }
1139 
copy_from_nullb(struct nullb * nullb,struct page * dest,unsigned int off,sector_t sector,size_t n)1140 static int copy_from_nullb(struct nullb *nullb, struct page *dest,
1141 	unsigned int off, sector_t sector, size_t n)
1142 {
1143 	size_t temp, count = 0;
1144 	unsigned int offset;
1145 	struct nullb_page *t_page;
1146 
1147 	while (count < n) {
1148 		temp = min_t(size_t, nullb->dev->blocksize, n - count);
1149 
1150 		offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
1151 		t_page = null_lookup_page(nullb, sector, false,
1152 			!null_cache_active(nullb));
1153 
1154 		if (t_page)
1155 			memcpy_page(dest, off + count, t_page->page, offset,
1156 				    temp);
1157 		else
1158 			zero_user(dest, off + count, temp);
1159 
1160 		count += temp;
1161 		sector += temp >> SECTOR_SHIFT;
1162 	}
1163 	return 0;
1164 }
1165 
nullb_fill_pattern(struct nullb * nullb,struct page * page,unsigned int len,unsigned int off)1166 static void nullb_fill_pattern(struct nullb *nullb, struct page *page,
1167 			       unsigned int len, unsigned int off)
1168 {
1169 	memset_page(page, off, 0xff, len);
1170 }
1171 
null_handle_discard(struct nullb_device * dev,sector_t sector,sector_t nr_sectors)1172 blk_status_t null_handle_discard(struct nullb_device *dev,
1173 				 sector_t sector, sector_t nr_sectors)
1174 {
1175 	struct nullb *nullb = dev->nullb;
1176 	size_t n = nr_sectors << SECTOR_SHIFT;
1177 	size_t temp;
1178 
1179 	spin_lock_irq(&nullb->lock);
1180 	while (n > 0) {
1181 		temp = min_t(size_t, n, dev->blocksize);
1182 		null_free_sector(nullb, sector, false);
1183 		if (null_cache_active(nullb))
1184 			null_free_sector(nullb, sector, true);
1185 		sector += temp >> SECTOR_SHIFT;
1186 		n -= temp;
1187 	}
1188 	spin_unlock_irq(&nullb->lock);
1189 
1190 	return BLK_STS_OK;
1191 }
1192 
null_handle_flush(struct nullb * nullb)1193 static blk_status_t null_handle_flush(struct nullb *nullb)
1194 {
1195 	int err;
1196 
1197 	if (!null_cache_active(nullb))
1198 		return 0;
1199 
1200 	spin_lock_irq(&nullb->lock);
1201 	while (true) {
1202 		err = null_make_cache_space(nullb,
1203 			nullb->dev->cache_size * 1024 * 1024);
1204 		if (err || nullb->dev->curr_cache == 0)
1205 			break;
1206 	}
1207 
1208 	WARN_ON(!radix_tree_empty(&nullb->dev->cache));
1209 	spin_unlock_irq(&nullb->lock);
1210 	return errno_to_blk_status(err);
1211 }
1212 
null_transfer(struct nullb * nullb,struct page * page,unsigned int len,unsigned int off,bool is_write,sector_t sector,bool is_fua)1213 static int null_transfer(struct nullb *nullb, struct page *page,
1214 	unsigned int len, unsigned int off, bool is_write, sector_t sector,
1215 	bool is_fua)
1216 {
1217 	struct nullb_device *dev = nullb->dev;
1218 	unsigned int valid_len = len;
1219 	int err = 0;
1220 
1221 	if (!is_write) {
1222 		if (dev->zoned)
1223 			valid_len = null_zone_valid_read_len(nullb,
1224 				sector, len);
1225 
1226 		if (valid_len) {
1227 			err = copy_from_nullb(nullb, page, off,
1228 				sector, valid_len);
1229 			off += valid_len;
1230 			len -= valid_len;
1231 		}
1232 
1233 		if (len)
1234 			nullb_fill_pattern(nullb, page, len, off);
1235 		flush_dcache_page(page);
1236 	} else {
1237 		flush_dcache_page(page);
1238 		err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
1239 	}
1240 
1241 	return err;
1242 }
1243 
null_handle_rq(struct nullb_cmd * cmd)1244 static blk_status_t null_handle_rq(struct nullb_cmd *cmd)
1245 {
1246 	struct request *rq = blk_mq_rq_from_pdu(cmd);
1247 	struct nullb *nullb = cmd->nq->dev->nullb;
1248 	int err = 0;
1249 	unsigned int len;
1250 	sector_t sector = blk_rq_pos(rq);
1251 	struct req_iterator iter;
1252 	struct bio_vec bvec;
1253 
1254 	spin_lock_irq(&nullb->lock);
1255 	rq_for_each_segment(bvec, rq, iter) {
1256 		len = bvec.bv_len;
1257 		err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
1258 				     op_is_write(req_op(rq)), sector,
1259 				     rq->cmd_flags & REQ_FUA);
1260 		if (err)
1261 			break;
1262 		sector += len >> SECTOR_SHIFT;
1263 	}
1264 	spin_unlock_irq(&nullb->lock);
1265 
1266 	return errno_to_blk_status(err);
1267 }
1268 
null_handle_throttled(struct nullb_cmd * cmd)1269 static inline blk_status_t null_handle_throttled(struct nullb_cmd *cmd)
1270 {
1271 	struct nullb_device *dev = cmd->nq->dev;
1272 	struct nullb *nullb = dev->nullb;
1273 	blk_status_t sts = BLK_STS_OK;
1274 	struct request *rq = blk_mq_rq_from_pdu(cmd);
1275 
1276 	if (!hrtimer_active(&nullb->bw_timer))
1277 		hrtimer_restart(&nullb->bw_timer);
1278 
1279 	if (atomic_long_sub_return(blk_rq_bytes(rq), &nullb->cur_bytes) < 0) {
1280 		blk_mq_stop_hw_queues(nullb->q);
1281 		/* race with timer */
1282 		if (atomic_long_read(&nullb->cur_bytes) > 0)
1283 			blk_mq_start_stopped_hw_queues(nullb->q, true);
1284 		/* requeue request */
1285 		sts = BLK_STS_DEV_RESOURCE;
1286 	}
1287 	return sts;
1288 }
1289 
null_handle_badblocks(struct nullb_cmd * cmd,sector_t sector,sector_t nr_sectors)1290 static inline blk_status_t null_handle_badblocks(struct nullb_cmd *cmd,
1291 						 sector_t sector,
1292 						 sector_t nr_sectors)
1293 {
1294 	struct badblocks *bb = &cmd->nq->dev->badblocks;
1295 	sector_t first_bad;
1296 	int bad_sectors;
1297 
1298 	if (badblocks_check(bb, sector, nr_sectors, &first_bad, &bad_sectors))
1299 		return BLK_STS_IOERR;
1300 
1301 	return BLK_STS_OK;
1302 }
1303 
null_handle_memory_backed(struct nullb_cmd * cmd,enum req_op op,sector_t sector,sector_t nr_sectors)1304 static inline blk_status_t null_handle_memory_backed(struct nullb_cmd *cmd,
1305 						     enum req_op op,
1306 						     sector_t sector,
1307 						     sector_t nr_sectors)
1308 {
1309 	struct nullb_device *dev = cmd->nq->dev;
1310 
1311 	if (op == REQ_OP_DISCARD)
1312 		return null_handle_discard(dev, sector, nr_sectors);
1313 
1314 	return null_handle_rq(cmd);
1315 }
1316 
nullb_zero_read_cmd_buffer(struct nullb_cmd * cmd)1317 static void nullb_zero_read_cmd_buffer(struct nullb_cmd *cmd)
1318 {
1319 	struct request *rq = blk_mq_rq_from_pdu(cmd);
1320 	struct nullb_device *dev = cmd->nq->dev;
1321 	struct bio *bio;
1322 
1323 	if (!dev->memory_backed && req_op(rq) == REQ_OP_READ) {
1324 		__rq_for_each_bio(bio, rq)
1325 			zero_fill_bio(bio);
1326 	}
1327 }
1328 
nullb_complete_cmd(struct nullb_cmd * cmd)1329 static inline void nullb_complete_cmd(struct nullb_cmd *cmd)
1330 {
1331 	struct request *rq = blk_mq_rq_from_pdu(cmd);
1332 
1333 	/*
1334 	 * Since root privileges are required to configure the null_blk
1335 	 * driver, it is fine that this driver does not initialize the
1336 	 * data buffers of read commands. Zero-initialize these buffers
1337 	 * anyway if KMSAN is enabled to prevent that KMSAN complains
1338 	 * about null_blk not initializing read data buffers.
1339 	 */
1340 	if (IS_ENABLED(CONFIG_KMSAN))
1341 		nullb_zero_read_cmd_buffer(cmd);
1342 
1343 	/* Complete IO by inline, softirq or timer */
1344 	switch (cmd->nq->dev->irqmode) {
1345 	case NULL_IRQ_SOFTIRQ:
1346 		blk_mq_complete_request(rq);
1347 		break;
1348 	case NULL_IRQ_NONE:
1349 		blk_mq_end_request(rq, cmd->error);
1350 		break;
1351 	case NULL_IRQ_TIMER:
1352 		null_cmd_end_timer(cmd);
1353 		break;
1354 	}
1355 }
1356 
null_process_cmd(struct nullb_cmd * cmd,enum req_op op,sector_t sector,unsigned int nr_sectors)1357 blk_status_t null_process_cmd(struct nullb_cmd *cmd, enum req_op op,
1358 			      sector_t sector, unsigned int nr_sectors)
1359 {
1360 	struct nullb_device *dev = cmd->nq->dev;
1361 	blk_status_t ret;
1362 
1363 	if (dev->badblocks.shift != -1) {
1364 		ret = null_handle_badblocks(cmd, sector, nr_sectors);
1365 		if (ret != BLK_STS_OK)
1366 			return ret;
1367 	}
1368 
1369 	if (dev->memory_backed)
1370 		return null_handle_memory_backed(cmd, op, sector, nr_sectors);
1371 
1372 	return BLK_STS_OK;
1373 }
1374 
null_handle_cmd(struct nullb_cmd * cmd,sector_t sector,sector_t nr_sectors,enum req_op op)1375 static void null_handle_cmd(struct nullb_cmd *cmd, sector_t sector,
1376 			    sector_t nr_sectors, enum req_op op)
1377 {
1378 	struct nullb_device *dev = cmd->nq->dev;
1379 	struct nullb *nullb = dev->nullb;
1380 	blk_status_t sts;
1381 
1382 	if (op == REQ_OP_FLUSH) {
1383 		cmd->error = null_handle_flush(nullb);
1384 		goto out;
1385 	}
1386 
1387 	if (dev->zoned)
1388 		sts = null_process_zoned_cmd(cmd, op, sector, nr_sectors);
1389 	else
1390 		sts = null_process_cmd(cmd, op, sector, nr_sectors);
1391 
1392 	/* Do not overwrite errors (e.g. timeout errors) */
1393 	if (cmd->error == BLK_STS_OK)
1394 		cmd->error = sts;
1395 
1396 out:
1397 	nullb_complete_cmd(cmd);
1398 }
1399 
nullb_bwtimer_fn(struct hrtimer * timer)1400 static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
1401 {
1402 	struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
1403 	ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1404 	unsigned int mbps = nullb->dev->mbps;
1405 
1406 	if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
1407 		return HRTIMER_NORESTART;
1408 
1409 	atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
1410 	blk_mq_start_stopped_hw_queues(nullb->q, true);
1411 
1412 	hrtimer_forward_now(&nullb->bw_timer, timer_interval);
1413 
1414 	return HRTIMER_RESTART;
1415 }
1416 
nullb_setup_bwtimer(struct nullb * nullb)1417 static void nullb_setup_bwtimer(struct nullb *nullb)
1418 {
1419 	ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1420 
1421 	hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1422 	nullb->bw_timer.function = nullb_bwtimer_fn;
1423 	atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps));
1424 	hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL);
1425 }
1426 
1427 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1428 
should_timeout_request(struct request * rq)1429 static bool should_timeout_request(struct request *rq)
1430 {
1431 	struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
1432 	struct nullb_device *dev = cmd->nq->dev;
1433 
1434 	return should_fail(&dev->timeout_config.attr, 1);
1435 }
1436 
should_requeue_request(struct request * rq)1437 static bool should_requeue_request(struct request *rq)
1438 {
1439 	struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
1440 	struct nullb_device *dev = cmd->nq->dev;
1441 
1442 	return should_fail(&dev->requeue_config.attr, 1);
1443 }
1444 
should_init_hctx_fail(struct nullb_device * dev)1445 static bool should_init_hctx_fail(struct nullb_device *dev)
1446 {
1447 	return should_fail(&dev->init_hctx_fault_config.attr, 1);
1448 }
1449 
1450 #else
1451 
should_timeout_request(struct request * rq)1452 static bool should_timeout_request(struct request *rq)
1453 {
1454 	return false;
1455 }
1456 
should_requeue_request(struct request * rq)1457 static bool should_requeue_request(struct request *rq)
1458 {
1459 	return false;
1460 }
1461 
should_init_hctx_fail(struct nullb_device * dev)1462 static bool should_init_hctx_fail(struct nullb_device *dev)
1463 {
1464 	return false;
1465 }
1466 
1467 #endif
1468 
null_map_queues(struct blk_mq_tag_set * set)1469 static void null_map_queues(struct blk_mq_tag_set *set)
1470 {
1471 	struct nullb *nullb = set->driver_data;
1472 	int i, qoff;
1473 	unsigned int submit_queues = g_submit_queues;
1474 	unsigned int poll_queues = g_poll_queues;
1475 
1476 	if (nullb) {
1477 		struct nullb_device *dev = nullb->dev;
1478 
1479 		/*
1480 		 * Refer nr_hw_queues of the tag set to check if the expected
1481 		 * number of hardware queues are prepared. If block layer failed
1482 		 * to prepare them, use previous numbers of submit queues and
1483 		 * poll queues to map queues.
1484 		 */
1485 		if (set->nr_hw_queues ==
1486 		    dev->submit_queues + dev->poll_queues) {
1487 			submit_queues = dev->submit_queues;
1488 			poll_queues = dev->poll_queues;
1489 		} else if (set->nr_hw_queues ==
1490 			   dev->prev_submit_queues + dev->prev_poll_queues) {
1491 			submit_queues = dev->prev_submit_queues;
1492 			poll_queues = dev->prev_poll_queues;
1493 		} else {
1494 			pr_warn("tag set has unexpected nr_hw_queues: %d\n",
1495 				set->nr_hw_queues);
1496 			WARN_ON_ONCE(true);
1497 			submit_queues = 1;
1498 			poll_queues = 0;
1499 		}
1500 	}
1501 
1502 	for (i = 0, qoff = 0; i < set->nr_maps; i++) {
1503 		struct blk_mq_queue_map *map = &set->map[i];
1504 
1505 		switch (i) {
1506 		case HCTX_TYPE_DEFAULT:
1507 			map->nr_queues = submit_queues;
1508 			break;
1509 		case HCTX_TYPE_READ:
1510 			map->nr_queues = 0;
1511 			continue;
1512 		case HCTX_TYPE_POLL:
1513 			map->nr_queues = poll_queues;
1514 			break;
1515 		}
1516 		map->queue_offset = qoff;
1517 		qoff += map->nr_queues;
1518 		blk_mq_map_queues(map);
1519 	}
1520 }
1521 
null_poll(struct blk_mq_hw_ctx * hctx,struct io_comp_batch * iob)1522 static int null_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
1523 {
1524 	struct nullb_queue *nq = hctx->driver_data;
1525 	LIST_HEAD(list);
1526 	int nr = 0;
1527 	struct request *rq;
1528 
1529 	spin_lock(&nq->poll_lock);
1530 	list_splice_init(&nq->poll_list, &list);
1531 	list_for_each_entry(rq, &list, queuelist)
1532 		blk_mq_set_request_complete(rq);
1533 	spin_unlock(&nq->poll_lock);
1534 
1535 	while (!list_empty(&list)) {
1536 		struct nullb_cmd *cmd;
1537 		struct request *req;
1538 
1539 		req = list_first_entry(&list, struct request, queuelist);
1540 		list_del_init(&req->queuelist);
1541 		cmd = blk_mq_rq_to_pdu(req);
1542 		cmd->error = null_process_cmd(cmd, req_op(req), blk_rq_pos(req),
1543 						blk_rq_sectors(req));
1544 		if (!blk_mq_add_to_batch(req, iob, (__force int) cmd->error,
1545 					blk_mq_end_request_batch))
1546 			blk_mq_end_request(req, cmd->error);
1547 		nr++;
1548 	}
1549 
1550 	return nr;
1551 }
1552 
null_timeout_rq(struct request * rq)1553 static enum blk_eh_timer_return null_timeout_rq(struct request *rq)
1554 {
1555 	struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1556 	struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
1557 
1558 	if (hctx->type == HCTX_TYPE_POLL) {
1559 		struct nullb_queue *nq = hctx->driver_data;
1560 
1561 		spin_lock(&nq->poll_lock);
1562 		/* The request may have completed meanwhile. */
1563 		if (blk_mq_request_completed(rq)) {
1564 			spin_unlock(&nq->poll_lock);
1565 			return BLK_EH_DONE;
1566 		}
1567 		list_del_init(&rq->queuelist);
1568 		spin_unlock(&nq->poll_lock);
1569 	}
1570 
1571 	pr_info("rq %p timed out\n", rq);
1572 
1573 	/*
1574 	 * If the device is marked as blocking (i.e. memory backed or zoned
1575 	 * device), the submission path may be blocked waiting for resources
1576 	 * and cause real timeouts. For these real timeouts, the submission
1577 	 * path will complete the request using blk_mq_complete_request().
1578 	 * Only fake timeouts need to execute blk_mq_complete_request() here.
1579 	 */
1580 	cmd->error = BLK_STS_TIMEOUT;
1581 	if (cmd->fake_timeout || hctx->type == HCTX_TYPE_POLL)
1582 		blk_mq_complete_request(rq);
1583 	return BLK_EH_DONE;
1584 }
1585 
null_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)1586 static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
1587 				  const struct blk_mq_queue_data *bd)
1588 {
1589 	struct request *rq = bd->rq;
1590 	struct nullb_cmd *cmd = blk_mq_rq_to_pdu(rq);
1591 	struct nullb_queue *nq = hctx->driver_data;
1592 	sector_t nr_sectors = blk_rq_sectors(rq);
1593 	sector_t sector = blk_rq_pos(rq);
1594 	const bool is_poll = hctx->type == HCTX_TYPE_POLL;
1595 
1596 	might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1597 
1598 	if (!is_poll && nq->dev->irqmode == NULL_IRQ_TIMER) {
1599 		hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1600 		cmd->timer.function = null_cmd_timer_expired;
1601 	}
1602 	cmd->error = BLK_STS_OK;
1603 	cmd->nq = nq;
1604 	cmd->fake_timeout = should_timeout_request(rq) ||
1605 		blk_should_fake_timeout(rq->q);
1606 
1607 	if (should_requeue_request(rq)) {
1608 		/*
1609 		 * Alternate between hitting the core BUSY path, and the
1610 		 * driver driven requeue path
1611 		 */
1612 		nq->requeue_selection++;
1613 		if (nq->requeue_selection & 1)
1614 			return BLK_STS_RESOURCE;
1615 		blk_mq_requeue_request(rq, true);
1616 		return BLK_STS_OK;
1617 	}
1618 
1619 	if (test_bit(NULLB_DEV_FL_THROTTLED, &nq->dev->flags)) {
1620 		blk_status_t sts = null_handle_throttled(cmd);
1621 
1622 		if (sts != BLK_STS_OK)
1623 			return sts;
1624 	}
1625 
1626 	blk_mq_start_request(rq);
1627 
1628 	if (is_poll) {
1629 		spin_lock(&nq->poll_lock);
1630 		list_add_tail(&rq->queuelist, &nq->poll_list);
1631 		spin_unlock(&nq->poll_lock);
1632 		return BLK_STS_OK;
1633 	}
1634 	if (cmd->fake_timeout)
1635 		return BLK_STS_OK;
1636 
1637 	null_handle_cmd(cmd, sector, nr_sectors, req_op(rq));
1638 	return BLK_STS_OK;
1639 }
1640 
null_queue_rqs(struct request ** rqlist)1641 static void null_queue_rqs(struct request **rqlist)
1642 {
1643 	struct request *requeue_list = NULL;
1644 	struct request **requeue_lastp = &requeue_list;
1645 	struct blk_mq_queue_data bd = { };
1646 	blk_status_t ret;
1647 
1648 	do {
1649 		struct request *rq = rq_list_pop(rqlist);
1650 
1651 		bd.rq = rq;
1652 		ret = null_queue_rq(rq->mq_hctx, &bd);
1653 		if (ret != BLK_STS_OK)
1654 			rq_list_add_tail(&requeue_lastp, rq);
1655 	} while (!rq_list_empty(*rqlist));
1656 
1657 	*rqlist = requeue_list;
1658 }
1659 
null_init_queue(struct nullb * nullb,struct nullb_queue * nq)1660 static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
1661 {
1662 	nq->dev = nullb->dev;
1663 	INIT_LIST_HEAD(&nq->poll_list);
1664 	spin_lock_init(&nq->poll_lock);
1665 }
1666 
null_init_hctx(struct blk_mq_hw_ctx * hctx,void * driver_data,unsigned int hctx_idx)1667 static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
1668 			  unsigned int hctx_idx)
1669 {
1670 	struct nullb *nullb = hctx->queue->queuedata;
1671 	struct nullb_queue *nq;
1672 
1673 	if (should_init_hctx_fail(nullb->dev))
1674 		return -EFAULT;
1675 
1676 	nq = &nullb->queues[hctx_idx];
1677 	hctx->driver_data = nq;
1678 	null_init_queue(nullb, nq);
1679 
1680 	return 0;
1681 }
1682 
1683 static const struct blk_mq_ops null_mq_ops = {
1684 	.queue_rq       = null_queue_rq,
1685 	.queue_rqs	= null_queue_rqs,
1686 	.complete	= null_complete_rq,
1687 	.timeout	= null_timeout_rq,
1688 	.poll		= null_poll,
1689 	.map_queues	= null_map_queues,
1690 	.init_hctx	= null_init_hctx,
1691 };
1692 
null_del_dev(struct nullb * nullb)1693 static void null_del_dev(struct nullb *nullb)
1694 {
1695 	struct nullb_device *dev;
1696 
1697 	if (!nullb)
1698 		return;
1699 
1700 	dev = nullb->dev;
1701 
1702 	ida_free(&nullb_indexes, nullb->index);
1703 
1704 	list_del_init(&nullb->list);
1705 
1706 	del_gendisk(nullb->disk);
1707 
1708 	if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
1709 		hrtimer_cancel(&nullb->bw_timer);
1710 		atomic_long_set(&nullb->cur_bytes, LONG_MAX);
1711 		blk_mq_start_stopped_hw_queues(nullb->q, true);
1712 	}
1713 
1714 	put_disk(nullb->disk);
1715 	if (nullb->tag_set == &nullb->__tag_set)
1716 		blk_mq_free_tag_set(nullb->tag_set);
1717 	kfree(nullb->queues);
1718 	if (null_cache_active(nullb))
1719 		null_free_device_storage(nullb->dev, true);
1720 	kfree(nullb);
1721 	dev->nullb = NULL;
1722 }
1723 
null_config_discard(struct nullb * nullb,struct queue_limits * lim)1724 static void null_config_discard(struct nullb *nullb, struct queue_limits *lim)
1725 {
1726 	if (nullb->dev->discard == false)
1727 		return;
1728 
1729 	if (!nullb->dev->memory_backed) {
1730 		nullb->dev->discard = false;
1731 		pr_info("discard option is ignored without memory backing\n");
1732 		return;
1733 	}
1734 
1735 	if (nullb->dev->zoned) {
1736 		nullb->dev->discard = false;
1737 		pr_info("discard option is ignored in zoned mode\n");
1738 		return;
1739 	}
1740 
1741 	lim->max_hw_discard_sectors = UINT_MAX >> 9;
1742 }
1743 
1744 static const struct block_device_operations null_ops = {
1745 	.owner		= THIS_MODULE,
1746 	.report_zones	= null_report_zones,
1747 };
1748 
setup_queues(struct nullb * nullb)1749 static int setup_queues(struct nullb *nullb)
1750 {
1751 	int nqueues = nr_cpu_ids;
1752 
1753 	if (g_poll_queues)
1754 		nqueues += g_poll_queues;
1755 
1756 	nullb->queues = kcalloc(nqueues, sizeof(struct nullb_queue),
1757 				GFP_KERNEL);
1758 	if (!nullb->queues)
1759 		return -ENOMEM;
1760 
1761 	return 0;
1762 }
1763 
null_init_tag_set(struct blk_mq_tag_set * set,int poll_queues)1764 static int null_init_tag_set(struct blk_mq_tag_set *set, int poll_queues)
1765 {
1766 	set->ops = &null_mq_ops;
1767 	set->cmd_size = sizeof(struct nullb_cmd);
1768 	set->timeout = 5 * HZ;
1769 	set->nr_maps = 1;
1770 	if (poll_queues) {
1771 		set->nr_hw_queues += poll_queues;
1772 		set->nr_maps += 2;
1773 	}
1774 	return blk_mq_alloc_tag_set(set);
1775 }
1776 
null_init_global_tag_set(void)1777 static int null_init_global_tag_set(void)
1778 {
1779 	int error;
1780 
1781 	if (tag_set.ops)
1782 		return 0;
1783 
1784 	tag_set.nr_hw_queues = g_submit_queues;
1785 	tag_set.queue_depth = g_hw_queue_depth;
1786 	tag_set.numa_node = g_home_node;
1787 	tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
1788 	if (g_no_sched)
1789 		tag_set.flags |= BLK_MQ_F_NO_SCHED;
1790 	if (g_shared_tag_bitmap)
1791 		tag_set.flags |= BLK_MQ_F_TAG_HCTX_SHARED;
1792 	if (g_blocking)
1793 		tag_set.flags |= BLK_MQ_F_BLOCKING;
1794 
1795 	error = null_init_tag_set(&tag_set, g_poll_queues);
1796 	if (error)
1797 		tag_set.ops = NULL;
1798 	return error;
1799 }
1800 
null_setup_tagset(struct nullb * nullb)1801 static int null_setup_tagset(struct nullb *nullb)
1802 {
1803 	if (nullb->dev->shared_tags) {
1804 		nullb->tag_set = &tag_set;
1805 		return null_init_global_tag_set();
1806 	}
1807 
1808 	nullb->tag_set = &nullb->__tag_set;
1809 	nullb->tag_set->driver_data = nullb;
1810 	nullb->tag_set->nr_hw_queues = nullb->dev->submit_queues;
1811 	nullb->tag_set->queue_depth = nullb->dev->hw_queue_depth;
1812 	nullb->tag_set->numa_node = nullb->dev->home_node;
1813 	nullb->tag_set->flags = BLK_MQ_F_SHOULD_MERGE;
1814 	if (nullb->dev->no_sched)
1815 		nullb->tag_set->flags |= BLK_MQ_F_NO_SCHED;
1816 	if (nullb->dev->shared_tag_bitmap)
1817 		nullb->tag_set->flags |= BLK_MQ_F_TAG_HCTX_SHARED;
1818 	if (nullb->dev->blocking)
1819 		nullb->tag_set->flags |= BLK_MQ_F_BLOCKING;
1820 	return null_init_tag_set(nullb->tag_set, nullb->dev->poll_queues);
1821 }
1822 
null_validate_conf(struct nullb_device * dev)1823 static int null_validate_conf(struct nullb_device *dev)
1824 {
1825 	if (dev->queue_mode == NULL_Q_RQ) {
1826 		pr_err("legacy IO path is no longer available\n");
1827 		return -EINVAL;
1828 	}
1829 	if (dev->queue_mode == NULL_Q_BIO) {
1830 		pr_err("BIO-based IO path is no longer available, using blk-mq instead.\n");
1831 		dev->queue_mode = NULL_Q_MQ;
1832 	}
1833 
1834 	if (dev->use_per_node_hctx) {
1835 		if (dev->submit_queues != nr_online_nodes)
1836 			dev->submit_queues = nr_online_nodes;
1837 	} else if (dev->submit_queues > nr_cpu_ids)
1838 		dev->submit_queues = nr_cpu_ids;
1839 	else if (dev->submit_queues == 0)
1840 		dev->submit_queues = 1;
1841 	dev->prev_submit_queues = dev->submit_queues;
1842 
1843 	if (dev->poll_queues > g_poll_queues)
1844 		dev->poll_queues = g_poll_queues;
1845 	dev->prev_poll_queues = dev->poll_queues;
1846 	dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
1847 
1848 	/* Do memory allocation, so set blocking */
1849 	if (dev->memory_backed)
1850 		dev->blocking = true;
1851 	else /* cache is meaningless */
1852 		dev->cache_size = 0;
1853 	dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
1854 						dev->cache_size);
1855 	dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
1856 
1857 	if (dev->zoned &&
1858 	    (!dev->zone_size || !is_power_of_2(dev->zone_size))) {
1859 		pr_err("zone_size must be power-of-two\n");
1860 		return -EINVAL;
1861 	}
1862 
1863 	return 0;
1864 }
1865 
1866 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
__null_setup_fault(struct fault_attr * attr,char * str)1867 static bool __null_setup_fault(struct fault_attr *attr, char *str)
1868 {
1869 	if (!str[0])
1870 		return true;
1871 
1872 	if (!setup_fault_attr(attr, str))
1873 		return false;
1874 
1875 	attr->verbose = 0;
1876 	return true;
1877 }
1878 #endif
1879 
null_setup_fault(void)1880 static bool null_setup_fault(void)
1881 {
1882 #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1883 	if (!__null_setup_fault(&null_timeout_attr, g_timeout_str))
1884 		return false;
1885 	if (!__null_setup_fault(&null_requeue_attr, g_requeue_str))
1886 		return false;
1887 	if (!__null_setup_fault(&null_init_hctx_attr, g_init_hctx_str))
1888 		return false;
1889 #endif
1890 	return true;
1891 }
1892 
null_add_dev(struct nullb_device * dev)1893 static int null_add_dev(struct nullb_device *dev)
1894 {
1895 	struct queue_limits lim = {
1896 		.logical_block_size	= dev->blocksize,
1897 		.physical_block_size	= dev->blocksize,
1898 		.max_hw_sectors		= dev->max_sectors,
1899 	};
1900 
1901 	struct nullb *nullb;
1902 	int rv;
1903 
1904 	rv = null_validate_conf(dev);
1905 	if (rv)
1906 		return rv;
1907 
1908 	nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
1909 	if (!nullb) {
1910 		rv = -ENOMEM;
1911 		goto out;
1912 	}
1913 	nullb->dev = dev;
1914 	dev->nullb = nullb;
1915 
1916 	spin_lock_init(&nullb->lock);
1917 
1918 	rv = setup_queues(nullb);
1919 	if (rv)
1920 		goto out_free_nullb;
1921 
1922 	rv = null_setup_tagset(nullb);
1923 	if (rv)
1924 		goto out_cleanup_queues;
1925 
1926 	if (dev->virt_boundary)
1927 		lim.virt_boundary_mask = PAGE_SIZE - 1;
1928 	null_config_discard(nullb, &lim);
1929 	if (dev->zoned) {
1930 		rv = null_init_zoned_dev(dev, &lim);
1931 		if (rv)
1932 			goto out_cleanup_tags;
1933 	}
1934 
1935 	if (dev->cache_size > 0) {
1936 		set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
1937 		lim.features |= BLK_FEAT_WRITE_CACHE;
1938 		if (dev->fua)
1939 			lim.features |= BLK_FEAT_FUA;
1940 	}
1941 
1942 	nullb->disk = blk_mq_alloc_disk(nullb->tag_set, &lim, nullb);
1943 	if (IS_ERR(nullb->disk)) {
1944 		rv = PTR_ERR(nullb->disk);
1945 		goto out_cleanup_zone;
1946 	}
1947 	nullb->q = nullb->disk->queue;
1948 
1949 	if (dev->mbps) {
1950 		set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags);
1951 		nullb_setup_bwtimer(nullb);
1952 	}
1953 
1954 	nullb->q->queuedata = nullb;
1955 
1956 	rv = ida_alloc(&nullb_indexes, GFP_KERNEL);
1957 	if (rv < 0)
1958 		goto out_cleanup_disk;
1959 
1960 	nullb->index = rv;
1961 	dev->index = rv;
1962 
1963 	if (config_item_name(&dev->group.cg_item)) {
1964 		/* Use configfs dir name as the device name */
1965 		snprintf(nullb->disk_name, sizeof(nullb->disk_name),
1966 			 "%s", config_item_name(&dev->group.cg_item));
1967 	} else {
1968 		sprintf(nullb->disk_name, "nullb%d", nullb->index);
1969 	}
1970 
1971 	set_capacity(nullb->disk,
1972 		((sector_t)nullb->dev->size * SZ_1M) >> SECTOR_SHIFT);
1973 	nullb->disk->major = null_major;
1974 	nullb->disk->first_minor = nullb->index;
1975 	nullb->disk->minors = 1;
1976 	nullb->disk->fops = &null_ops;
1977 	nullb->disk->private_data = nullb;
1978 	strscpy_pad(nullb->disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
1979 
1980 	if (nullb->dev->zoned) {
1981 		rv = null_register_zoned_dev(nullb);
1982 		if (rv)
1983 			goto out_ida_free;
1984 	}
1985 
1986 	rv = add_disk(nullb->disk);
1987 	if (rv)
1988 		goto out_ida_free;
1989 
1990 	list_add_tail(&nullb->list, &nullb_list);
1991 
1992 	pr_info("disk %s created\n", nullb->disk_name);
1993 
1994 	return 0;
1995 
1996 out_ida_free:
1997 	ida_free(&nullb_indexes, nullb->index);
1998 out_cleanup_disk:
1999 	put_disk(nullb->disk);
2000 out_cleanup_zone:
2001 	null_free_zoned_dev(dev);
2002 out_cleanup_tags:
2003 	if (nullb->tag_set == &nullb->__tag_set)
2004 		blk_mq_free_tag_set(nullb->tag_set);
2005 out_cleanup_queues:
2006 	kfree(nullb->queues);
2007 out_free_nullb:
2008 	kfree(nullb);
2009 	dev->nullb = NULL;
2010 out:
2011 	return rv;
2012 }
2013 
null_find_dev_by_name(const char * name)2014 static struct nullb *null_find_dev_by_name(const char *name)
2015 {
2016 	struct nullb *nullb = NULL, *nb;
2017 
2018 	mutex_lock(&lock);
2019 	list_for_each_entry(nb, &nullb_list, list) {
2020 		if (strcmp(nb->disk_name, name) == 0) {
2021 			nullb = nb;
2022 			break;
2023 		}
2024 	}
2025 	mutex_unlock(&lock);
2026 
2027 	return nullb;
2028 }
2029 
null_create_dev(void)2030 static int null_create_dev(void)
2031 {
2032 	struct nullb_device *dev;
2033 	int ret;
2034 
2035 	dev = null_alloc_dev();
2036 	if (!dev)
2037 		return -ENOMEM;
2038 
2039 	mutex_lock(&lock);
2040 	ret = null_add_dev(dev);
2041 	mutex_unlock(&lock);
2042 	if (ret) {
2043 		null_free_dev(dev);
2044 		return ret;
2045 	}
2046 
2047 	return 0;
2048 }
2049 
null_destroy_dev(struct nullb * nullb)2050 static void null_destroy_dev(struct nullb *nullb)
2051 {
2052 	struct nullb_device *dev = nullb->dev;
2053 
2054 	null_del_dev(nullb);
2055 	null_free_device_storage(dev, false);
2056 	null_free_dev(dev);
2057 }
2058 
null_init(void)2059 static int __init null_init(void)
2060 {
2061 	int ret = 0;
2062 	unsigned int i;
2063 	struct nullb *nullb;
2064 
2065 	if (g_bs > PAGE_SIZE) {
2066 		pr_warn("invalid block size\n");
2067 		pr_warn("defaults block size to %lu\n", PAGE_SIZE);
2068 		g_bs = PAGE_SIZE;
2069 	}
2070 
2071 	if (g_home_node != NUMA_NO_NODE && g_home_node >= nr_online_nodes) {
2072 		pr_err("invalid home_node value\n");
2073 		g_home_node = NUMA_NO_NODE;
2074 	}
2075 
2076 	if (!null_setup_fault())
2077 		return -EINVAL;
2078 
2079 	if (g_queue_mode == NULL_Q_RQ) {
2080 		pr_err("legacy IO path is no longer available\n");
2081 		return -EINVAL;
2082 	}
2083 
2084 	if (g_use_per_node_hctx) {
2085 		if (g_submit_queues != nr_online_nodes) {
2086 			pr_warn("submit_queues param is set to %u.\n",
2087 				nr_online_nodes);
2088 			g_submit_queues = nr_online_nodes;
2089 		}
2090 	} else if (g_submit_queues > nr_cpu_ids) {
2091 		g_submit_queues = nr_cpu_ids;
2092 	} else if (g_submit_queues <= 0) {
2093 		g_submit_queues = 1;
2094 	}
2095 
2096 	config_group_init(&nullb_subsys.su_group);
2097 	mutex_init(&nullb_subsys.su_mutex);
2098 
2099 	ret = configfs_register_subsystem(&nullb_subsys);
2100 	if (ret)
2101 		return ret;
2102 
2103 	mutex_init(&lock);
2104 
2105 	null_major = register_blkdev(0, "nullb");
2106 	if (null_major < 0) {
2107 		ret = null_major;
2108 		goto err_conf;
2109 	}
2110 
2111 	for (i = 0; i < nr_devices; i++) {
2112 		ret = null_create_dev();
2113 		if (ret)
2114 			goto err_dev;
2115 	}
2116 
2117 	pr_info("module loaded\n");
2118 	return 0;
2119 
2120 err_dev:
2121 	while (!list_empty(&nullb_list)) {
2122 		nullb = list_entry(nullb_list.next, struct nullb, list);
2123 		null_destroy_dev(nullb);
2124 	}
2125 	unregister_blkdev(null_major, "nullb");
2126 err_conf:
2127 	configfs_unregister_subsystem(&nullb_subsys);
2128 	return ret;
2129 }
2130 
null_exit(void)2131 static void __exit null_exit(void)
2132 {
2133 	struct nullb *nullb;
2134 
2135 	configfs_unregister_subsystem(&nullb_subsys);
2136 
2137 	unregister_blkdev(null_major, "nullb");
2138 
2139 	mutex_lock(&lock);
2140 	while (!list_empty(&nullb_list)) {
2141 		nullb = list_entry(nullb_list.next, struct nullb, list);
2142 		null_destroy_dev(nullb);
2143 	}
2144 	mutex_unlock(&lock);
2145 
2146 	if (tag_set.ops)
2147 		blk_mq_free_tag_set(&tag_set);
2148 
2149 	mutex_destroy(&lock);
2150 }
2151 
2152 module_init(null_init);
2153 module_exit(null_exit);
2154 
2155 MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
2156 MODULE_DESCRIPTION("multi queue aware block test driver");
2157 MODULE_LICENSE("GPL");
2158