null_blk_main.c 45.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
S
Shaohua Li 已提交
2 3 4 5
/*
 * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
 * Shaohua Li <shli@fb.com>
 */
6
#include <linux/module.h>
7

8 9 10 11
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/init.h>
12
#include "null_blk.h"
13

S
Shaohua Li 已提交
14 15 16 17 18 19
#define PAGE_SECTORS_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
#define PAGE_SECTORS		(1 << PAGE_SECTORS_SHIFT)
#define SECTOR_MASK		(PAGE_SECTORS - 1)

#define FREE_BATCH		16

S
Shaohua Li 已提交
20 21 22
#define TICKS_PER_SEC		50ULL
#define TIMER_INTERVAL		(NSEC_PER_SEC / TICKS_PER_SEC)

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
static DECLARE_FAULT_ATTR(null_init_hctx_attr);
27
#endif
28

S
Shaohua Li 已提交
29 30 31 32
static inline u64 mb_per_tick(int mbps)
{
	return (1 << 20) / TICKS_PER_SEC * ((u64) mbps);
}
33

S
Shaohua Li 已提交
34 35 36 37 38
/*
 * Status flags for nullb_device.
 *
 * CONFIGURED:	Device has been configured and turned on. Cannot reconfigure.
 * UP:		Device is currently on and visible in userspace.
S
Shaohua Li 已提交
39
 * THROTTLED:	Device is being throttled.
S
Shaohua Li 已提交
40
 * CACHE:	Device is using a write-back cache.
S
Shaohua Li 已提交
41 42 43 44
 */
enum nullb_device_flags {
	NULLB_DEV_FL_CONFIGURED	= 0,
	NULLB_DEV_FL_UP		= 1,
S
Shaohua Li 已提交
45
	NULLB_DEV_FL_THROTTLED	= 2,
S
Shaohua Li 已提交
46
	NULLB_DEV_FL_CACHE	= 3,
S
Shaohua Li 已提交
47 48
};

49
#define MAP_SZ		((PAGE_SIZE >> SECTOR_SHIFT) + 2)
S
Shaohua Li 已提交
50 51 52 53 54 55 56
/*
 * nullb_page is a page in memory for nullb devices.
 *
 * @page:	The page holding the data.
 * @bitmap:	The bitmap represents which sector in the page has data.
 *		Each bit represents one block size. For example, sector 8
 *		will use the 7th bit
S
Shaohua Li 已提交
57 58 59 60
 * The highest 2 bits of bitmap are for special purpose. LOCK means the cache
 * page is being flushing to storage. FREE means the cache page is freed and
 * should be skipped from flushing to storage. Please see
 * null_make_cache_space
S
Shaohua Li 已提交
61 62 63
 */
struct nullb_page {
	struct page *page;
64
	DECLARE_BITMAP(bitmap, MAP_SZ);
S
Shaohua Li 已提交
65
};
66 67
#define NULLB_PAGE_LOCK (MAP_SZ - 1)
#define NULLB_PAGE_FREE (MAP_SZ - 2)
S
Shaohua Li 已提交
68

69 70 71
static LIST_HEAD(nullb_list);
static struct mutex lock;
static int null_major;
S
Shaohua Li 已提交
72
static DEFINE_IDA(nullb_indexes);
73
static struct blk_mq_tag_set tag_set;
74 75 76 77 78

enum {
	NULL_IRQ_NONE		= 0,
	NULL_IRQ_SOFTIRQ	= 1,
	NULL_IRQ_TIMER		= 2,
79
};
80

81
enum {
82 83 84 85 86
	NULL_Q_BIO		= 0,
	NULL_Q_RQ		= 1,
	NULL_Q_MQ		= 2,
};

87
static int g_no_sched;
88
module_param_named(no_sched, g_no_sched, int, 0444);
89 90
MODULE_PARM_DESC(no_sched, "No io scheduler");

S
Shaohua Li 已提交
91
static int g_submit_queues = 1;
92
module_param_named(submit_queues, g_submit_queues, int, 0444);
93 94
MODULE_PARM_DESC(submit_queues, "Number of submission queues");

S
Shaohua Li 已提交
95
static int g_home_node = NUMA_NO_NODE;
96
module_param_named(home_node, g_home_node, int, 0444);
97 98
MODULE_PARM_DESC(home_node, "Home node for the device");

99
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
100
static char g_timeout_str[80];
101
module_param_string(timeout, g_timeout_str, sizeof(g_timeout_str), 0444);
102 103

static char g_requeue_str[80];
104
module_param_string(requeue, g_requeue_str, sizeof(g_requeue_str), 0444);
105 106 107

static char g_init_hctx_str[80];
module_param_string(init_hctx, g_init_hctx_str, sizeof(g_init_hctx_str), 0444);
108
#endif
109

S
Shaohua Li 已提交
110
static int g_queue_mode = NULL_Q_MQ;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

static int null_param_store_val(const char *str, int *val, int min, int max)
{
	int ret, new_val;

	ret = kstrtoint(str, 10, &new_val);
	if (ret)
		return -EINVAL;

	if (new_val < min || new_val > max)
		return -EINVAL;

	*val = new_val;
	return 0;
}

static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
{
S
Shaohua Li 已提交
129
	return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
130 131
}

132
static const struct kernel_param_ops null_queue_mode_param_ops = {
133 134 135 136
	.set	= null_set_queue_mode,
	.get	= param_get_int,
};

137
device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, 0444);
138
MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
139

S
Shaohua Li 已提交
140
static int g_gb = 250;
141
module_param_named(gb, g_gb, int, 0444);
142 143
MODULE_PARM_DESC(gb, "Size in GB");

S
Shaohua Li 已提交
144
static int g_bs = 512;
145
module_param_named(bs, g_bs, int, 0444);
146 147
MODULE_PARM_DESC(bs, "Block size (in bytes)");

148
static unsigned int nr_devices = 1;
149
module_param(nr_devices, uint, 0444);
150 151
MODULE_PARM_DESC(nr_devices, "Number of devices to register");

S
Shaohua Li 已提交
152
static bool g_blocking;
153
module_param_named(blocking, g_blocking, bool, 0444);
J
Jens Axboe 已提交
154 155
MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");

156
static bool shared_tags;
157
module_param(shared_tags, bool, 0444);
158 159
MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");

S
Shaohua Li 已提交
160
static int g_irqmode = NULL_IRQ_SOFTIRQ;
161 162 163

static int null_set_irqmode(const char *str, const struct kernel_param *kp)
{
S
Shaohua Li 已提交
164
	return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
165 166 167
					NULL_IRQ_TIMER);
}

168
static const struct kernel_param_ops null_irqmode_param_ops = {
169 170 171 172
	.set	= null_set_irqmode,
	.get	= param_get_int,
};

173
device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, 0444);
174 175
MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");

S
Shaohua Li 已提交
176
static unsigned long g_completion_nsec = 10000;
177
module_param_named(completion_nsec, g_completion_nsec, ulong, 0444);
178 179
MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");

S
Shaohua Li 已提交
180
static int g_hw_queue_depth = 64;
181
module_param_named(hw_queue_depth, g_hw_queue_depth, int, 0444);
182 183
MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");

S
Shaohua Li 已提交
184
static bool g_use_per_node_hctx;
185
module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, 0444);
186
MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
187

M
Matias Bjørling 已提交
188 189 190 191 192 193 194 195
static bool g_zoned;
module_param_named(zoned, g_zoned, bool, S_IRUGO);
MODULE_PARM_DESC(zoned, "Make device as a host-managed zoned block device. Default: false");

static unsigned long g_zone_size = 256;
module_param_named(zone_size, g_zone_size, ulong, S_IRUGO);
MODULE_PARM_DESC(zone_size, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");

196 197 198 199
static unsigned int g_zone_nr_conv;
module_param_named(zone_nr_conv, g_zone_nr_conv, uint, 0444);
MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones when block device is zoned. Default: 0");

S
Shaohua Li 已提交
200 201
static struct nullb_device *null_alloc_dev(void);
static void null_free_dev(struct nullb_device *dev);
202 203
static void null_del_dev(struct nullb *nullb);
static int null_add_dev(struct nullb_device *dev);
S
Shaohua Li 已提交
204
static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
S
Shaohua Li 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

static inline struct nullb_device *to_nullb_device(struct config_item *item)
{
	return item ? container_of(item, struct nullb_device, item) : NULL;
}

static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page)
{
	return snprintf(page, PAGE_SIZE, "%u\n", val);
}

static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
	char *page)
{
	return snprintf(page, PAGE_SIZE, "%lu\n", val);
}

static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
{
	return snprintf(page, PAGE_SIZE, "%u\n", val);
}

static ssize_t nullb_device_uint_attr_store(unsigned int *val,
	const char *page, size_t count)
{
	unsigned int tmp;
	int result;

	result = kstrtouint(page, 0, &tmp);
234
	if (result < 0)
S
Shaohua Li 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247
		return result;

	*val = tmp;
	return count;
}

static ssize_t nullb_device_ulong_attr_store(unsigned long *val,
	const char *page, size_t count)
{
	int result;
	unsigned long tmp;

	result = kstrtoul(page, 0, &tmp);
248
	if (result < 0)
S
Shaohua Li 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261
		return result;

	*val = tmp;
	return count;
}

static ssize_t nullb_device_bool_attr_store(bool *val, const char *page,
	size_t count)
{
	bool tmp;
	int result;

	result = kstrtobool(page,  &tmp);
262
	if (result < 0)
S
Shaohua Li 已提交
263 264 265 266 267 268 269
		return result;

	*val = tmp;
	return count;
}

/* The following macro should only be used with TYPE = {uint, ulong, bool}. */
270 271 272 273 274 275 276 277 278 279 280 281 282
#define NULLB_DEVICE_ATTR(NAME, TYPE, APPLY)				\
static ssize_t								\
nullb_device_##NAME##_show(struct config_item *item, char *page)	\
{									\
	return nullb_device_##TYPE##_attr_show(				\
				to_nullb_device(item)->NAME, page);	\
}									\
static ssize_t								\
nullb_device_##NAME##_store(struct config_item *item, const char *page,	\
			    size_t count)				\
{									\
	int (*apply_fn)(struct nullb_device *dev, TYPE new_value) = APPLY;\
	struct nullb_device *dev = to_nullb_device(item);		\
283
	TYPE new_value = 0;						\
284 285 286 287 288 289 290 291 292 293 294 295 296 297
	int ret;							\
									\
	ret = nullb_device_##TYPE##_attr_store(&new_value, page, count);\
	if (ret < 0)							\
		return ret;						\
	if (apply_fn)							\
		ret = apply_fn(dev, new_value);				\
	else if (test_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags)) 	\
		ret = -EBUSY;						\
	if (ret < 0)							\
		return ret;						\
	dev->NAME = new_value;						\
	return count;							\
}									\
S
Shaohua Li 已提交
298 299
CONFIGFS_ATTR(nullb_device_, NAME);

300 301 302 303 304 305 306 307 308
static int nullb_apply_submit_queues(struct nullb_device *dev,
				     unsigned int submit_queues)
{
	struct nullb *nullb = dev->nullb;
	struct blk_mq_tag_set *set;

	if (!nullb)
		return 0;

309 310 311 312 313 314
	/*
	 * Make sure that null_init_hctx() does not access nullb->queues[] past
	 * the end of that array.
	 */
	if (submit_queues > nr_cpu_ids)
		return -EINVAL;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	set = nullb->tag_set;
	blk_mq_update_nr_hw_queues(set, submit_queues);
	return set->nr_hw_queues == submit_queues ? 0 : -ENOMEM;
}

NULLB_DEVICE_ATTR(size, ulong, NULL);
NULLB_DEVICE_ATTR(completion_nsec, ulong, NULL);
NULLB_DEVICE_ATTR(submit_queues, uint, nullb_apply_submit_queues);
NULLB_DEVICE_ATTR(home_node, uint, NULL);
NULLB_DEVICE_ATTR(queue_mode, uint, NULL);
NULLB_DEVICE_ATTR(blocksize, uint, NULL);
NULLB_DEVICE_ATTR(irqmode, uint, NULL);
NULLB_DEVICE_ATTR(hw_queue_depth, uint, NULL);
NULLB_DEVICE_ATTR(index, uint, NULL);
NULLB_DEVICE_ATTR(blocking, bool, NULL);
NULLB_DEVICE_ATTR(use_per_node_hctx, bool, NULL);
NULLB_DEVICE_ATTR(memory_backed, bool, NULL);
NULLB_DEVICE_ATTR(discard, bool, NULL);
NULLB_DEVICE_ATTR(mbps, uint, NULL);
NULLB_DEVICE_ATTR(cache_size, ulong, NULL);
NULLB_DEVICE_ATTR(zoned, bool, NULL);
NULLB_DEVICE_ATTR(zone_size, ulong, NULL);
NULLB_DEVICE_ATTR(zone_nr_conv, uint, NULL);
S
Shaohua Li 已提交
338

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
static ssize_t nullb_device_power_show(struct config_item *item, char *page)
{
	return nullb_device_bool_attr_show(to_nullb_device(item)->power, page);
}

static ssize_t nullb_device_power_store(struct config_item *item,
				     const char *page, size_t count)
{
	struct nullb_device *dev = to_nullb_device(item);
	bool newp = false;
	ssize_t ret;

	ret = nullb_device_bool_attr_store(&newp, page, count);
	if (ret < 0)
		return ret;

	if (!dev->power && newp) {
		if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
			return count;
		if (null_add_dev(dev)) {
			clear_bit(NULLB_DEV_FL_UP, &dev->flags);
			return -ENOMEM;
		}

		set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
		dev->power = newp;
365
	} else if (dev->power && !newp) {
366 367 368 369 370 371
		if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
			mutex_lock(&lock);
			dev->power = newp;
			null_del_dev(dev->nullb);
			mutex_unlock(&lock);
		}
372
		clear_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
373 374 375 376 377 378 379
	}

	return count;
}

CONFIGFS_ATTR(nullb_device_, power);

S
Shaohua Li 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page)
{
	struct nullb_device *t_dev = to_nullb_device(item);

	return badblocks_show(&t_dev->badblocks, page, 0);
}

static ssize_t nullb_device_badblocks_store(struct config_item *item,
				     const char *page, size_t count)
{
	struct nullb_device *t_dev = to_nullb_device(item);
	char *orig, *buf, *tmp;
	u64 start, end;
	int ret;

	orig = kstrndup(page, count, GFP_KERNEL);
	if (!orig)
		return -ENOMEM;

	buf = strstrip(orig);

	ret = -EINVAL;
	if (buf[0] != '+' && buf[0] != '-')
		goto out;
	tmp = strchr(&buf[1], '-');
	if (!tmp)
		goto out;
	*tmp = '\0';
	ret = kstrtoull(buf + 1, 0, &start);
	if (ret)
		goto out;
	ret = kstrtoull(tmp + 1, 0, &end);
	if (ret)
		goto out;
	ret = -EINVAL;
	if (start > end)
		goto out;
	/* enable badblocks */
	cmpxchg(&t_dev->badblocks.shift, -1, 0);
	if (buf[0] == '+')
		ret = badblocks_set(&t_dev->badblocks, start,
			end - start + 1, 1);
	else
		ret = badblocks_clear(&t_dev->badblocks, start,
			end - start + 1);
	if (ret == 0)
		ret = count;
out:
	kfree(orig);
	return ret;
}
CONFIGFS_ATTR(nullb_device_, badblocks);

S
Shaohua Li 已提交
433 434 435 436 437 438 439 440 441
static struct configfs_attribute *nullb_device_attrs[] = {
	&nullb_device_attr_size,
	&nullb_device_attr_completion_nsec,
	&nullb_device_attr_submit_queues,
	&nullb_device_attr_home_node,
	&nullb_device_attr_queue_mode,
	&nullb_device_attr_blocksize,
	&nullb_device_attr_irqmode,
	&nullb_device_attr_hw_queue_depth,
442
	&nullb_device_attr_index,
S
Shaohua Li 已提交
443 444
	&nullb_device_attr_blocking,
	&nullb_device_attr_use_per_node_hctx,
445
	&nullb_device_attr_power,
S
Shaohua Li 已提交
446
	&nullb_device_attr_memory_backed,
S
Shaohua Li 已提交
447
	&nullb_device_attr_discard,
S
Shaohua Li 已提交
448
	&nullb_device_attr_mbps,
S
Shaohua Li 已提交
449
	&nullb_device_attr_cache_size,
S
Shaohua Li 已提交
450
	&nullb_device_attr_badblocks,
M
Matias Bjørling 已提交
451 452
	&nullb_device_attr_zoned,
	&nullb_device_attr_zone_size,
453
	&nullb_device_attr_zone_nr_conv,
S
Shaohua Li 已提交
454 455 456 457 458
	NULL,
};

static void nullb_device_release(struct config_item *item)
{
S
Shaohua Li 已提交
459 460
	struct nullb_device *dev = to_nullb_device(item);

S
Shaohua Li 已提交
461
	null_free_device_storage(dev, false);
S
Shaohua Li 已提交
462
	null_free_dev(dev);
S
Shaohua Li 已提交
463 464 465 466 467 468
}

static struct configfs_item_operations nullb_device_ops = {
	.release	= nullb_device_release,
};

B
Bhumika Goyal 已提交
469
static const struct config_item_type nullb_device_type = {
S
Shaohua Li 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	.ct_item_ops	= &nullb_device_ops,
	.ct_attrs	= nullb_device_attrs,
	.ct_owner	= THIS_MODULE,
};

static struct
config_item *nullb_group_make_item(struct config_group *group, const char *name)
{
	struct nullb_device *dev;

	dev = null_alloc_dev();
	if (!dev)
		return ERR_PTR(-ENOMEM);

	config_item_init_type_name(&dev->item, name, &nullb_device_type);

	return &dev->item;
}

static void
nullb_group_drop_item(struct config_group *group, struct config_item *item)
{
492 493 494 495 496 497 498 499 500
	struct nullb_device *dev = to_nullb_device(item);

	if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
		mutex_lock(&lock);
		dev->power = false;
		null_del_dev(dev->nullb);
		mutex_unlock(&lock);
	}

S
Shaohua Li 已提交
501 502 503 504 505
	config_item_put(item);
}

static ssize_t memb_group_features_show(struct config_item *item, char *page)
{
506
	return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache,badblocks,zoned,zone_size,zone_nr_conv\n");
S
Shaohua Li 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520
}

CONFIGFS_ATTR_RO(memb_group_, features);

static struct configfs_attribute *nullb_group_attrs[] = {
	&memb_group_attr_features,
	NULL,
};

static struct configfs_group_operations nullb_group_ops = {
	.make_item	= nullb_group_make_item,
	.drop_item	= nullb_group_drop_item,
};

B
Bhumika Goyal 已提交
521
static const struct config_item_type nullb_group_type = {
S
Shaohua Li 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535
	.ct_group_ops	= &nullb_group_ops,
	.ct_attrs	= nullb_group_attrs,
	.ct_owner	= THIS_MODULE,
};

static struct configfs_subsystem nullb_subsys = {
	.su_group = {
		.cg_item = {
			.ci_namebuf = "nullb",
			.ci_type = &nullb_group_type,
		},
	},
};

S
Shaohua Li 已提交
536 537 538 539 540
static inline int null_cache_active(struct nullb *nullb)
{
	return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
}

S
Shaohua Li 已提交
541 542 543 544 545 546 547
static struct nullb_device *null_alloc_dev(void)
{
	struct nullb_device *dev;

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return NULL;
S
Shaohua Li 已提交
548
	INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
S
Shaohua Li 已提交
549
	INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
S
Shaohua Li 已提交
550 551 552 553 554
	if (badblocks_init(&dev->badblocks, 0)) {
		kfree(dev);
		return NULL;
	}

S
Shaohua Li 已提交
555 556 557 558 559 560 561 562 563 564
	dev->size = g_gb * 1024;
	dev->completion_nsec = g_completion_nsec;
	dev->submit_queues = g_submit_queues;
	dev->home_node = g_home_node;
	dev->queue_mode = g_queue_mode;
	dev->blocksize = g_bs;
	dev->irqmode = g_irqmode;
	dev->hw_queue_depth = g_hw_queue_depth;
	dev->blocking = g_blocking;
	dev->use_per_node_hctx = g_use_per_node_hctx;
M
Matias Bjørling 已提交
565 566
	dev->zoned = g_zoned;
	dev->zone_size = g_zone_size;
567
	dev->zone_nr_conv = g_zone_nr_conv;
S
Shaohua Li 已提交
568 569 570 571 572
	return dev;
}

static void null_free_dev(struct nullb_device *dev)
{
573 574 575
	if (!dev)
		return;

M
Matias Bjørling 已提交
576
	null_zone_exit(dev);
577
	badblocks_exit(&dev->badblocks);
S
Shaohua Li 已提交
578 579 580
	kfree(dev);
}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
static void put_tag(struct nullb_queue *nq, unsigned int tag)
{
	clear_bit_unlock(tag, nq->tag_map);

	if (waitqueue_active(&nq->wait))
		wake_up(&nq->wait);
}

static unsigned int get_tag(struct nullb_queue *nq)
{
	unsigned int tag;

	do {
		tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
		if (tag >= nq->queue_depth)
			return -1U;
	} while (test_and_set_bit_lock(tag, nq->tag_map));

	return tag;
}

static void free_cmd(struct nullb_cmd *cmd)
{
	put_tag(cmd->nq, cmd->tag);
}

607 608
static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);

609 610 611 612 613 614 615 616 617 618
static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
{
	struct nullb_cmd *cmd;
	unsigned int tag;

	tag = get_tag(nq);
	if (tag != -1U) {
		cmd = &nq->cmds[tag];
		cmd->tag = tag;
		cmd->nq = nq;
S
Shaohua Li 已提交
619
		if (nq->dev->irqmode == NULL_IRQ_TIMER) {
620 621 622 623
			hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
				     HRTIMER_MODE_REL);
			cmd->timer.function = null_cmd_timer_expired;
		}
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
		return cmd;
	}

	return NULL;
}

static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
{
	struct nullb_cmd *cmd;
	DEFINE_WAIT(wait);

	cmd = __alloc_cmd(nq);
	if (cmd || !can_wait)
		return cmd;

	do {
		prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
		cmd = __alloc_cmd(nq);
		if (cmd)
			break;

		io_schedule();
	} while (1);

	finish_wait(&nq->wait, &wait);
	return cmd;
}

static void end_cmd(struct nullb_cmd *cmd)
{
S
Shaohua Li 已提交
654
	int queue_mode = cmd->nq->dev->queue_mode;
655

656 657
	switch (queue_mode)  {
	case NULL_Q_MQ:
S
Shaohua Li 已提交
658
		blk_mq_end_request(cmd->rq, cmd->error);
659 660
		return;
	case NULL_Q_BIO:
S
Shaohua Li 已提交
661
		cmd->bio->bi_status = cmd->error;
662
		bio_endio(cmd->bio);
663
		break;
664
	}
665

666
	free_cmd(cmd);
667 668 669 670 671
}

static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
{
	end_cmd(container_of(timer, struct nullb_cmd, timer));
672 673 674 675 676 677

	return HRTIMER_NORESTART;
}

static void null_cmd_end_timer(struct nullb_cmd *cmd)
{
S
Shaohua Li 已提交
678
	ktime_t kt = cmd->nq->dev->completion_nsec;
679

680
	hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
681 682
}

683
static void null_complete_rq(struct request *rq)
684
{
685
	end_cmd(blk_mq_rq_to_pdu(rq));
686 687
}

S
Shaohua Li 已提交
688 689 690 691 692 693 694 695 696 697 698 699
static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
{
	struct nullb_page *t_page;

	t_page = kmalloc(sizeof(struct nullb_page), gfp_flags);
	if (!t_page)
		goto out;

	t_page->page = alloc_pages(gfp_flags, 0);
	if (!t_page->page)
		goto out_freepage;

700
	memset(t_page->bitmap, 0, sizeof(t_page->bitmap));
S
Shaohua Li 已提交
701 702 703 704 705 706 707 708 709
	return t_page;
out_freepage:
	kfree(t_page);
out:
	return NULL;
}

static void null_free_page(struct nullb_page *t_page)
{
710 711
	__set_bit(NULLB_PAGE_FREE, t_page->bitmap);
	if (test_bit(NULLB_PAGE_LOCK, t_page->bitmap))
S
Shaohua Li 已提交
712
		return;
S
Shaohua Li 已提交
713 714 715 716
	__free_page(t_page->page);
	kfree(t_page);
}

717 718 719 720 721 722 723
static bool null_page_empty(struct nullb_page *page)
{
	int size = MAP_SZ - 2;

	return find_first_bit(page->bitmap, size) == size;
}

S
Shaohua Li 已提交
724 725
static void null_free_sector(struct nullb *nullb, sector_t sector,
	bool is_cache)
S
Shaohua Li 已提交
726 727 728 729 730 731
{
	unsigned int sector_bit;
	u64 idx;
	struct nullb_page *t_page, *ret;
	struct radix_tree_root *root;

S
Shaohua Li 已提交
732
	root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
S
Shaohua Li 已提交
733 734 735 736 737
	idx = sector >> PAGE_SECTORS_SHIFT;
	sector_bit = (sector & SECTOR_MASK);

	t_page = radix_tree_lookup(root, idx);
	if (t_page) {
738
		__clear_bit(sector_bit, t_page->bitmap);
S
Shaohua Li 已提交
739

740
		if (null_page_empty(t_page)) {
S
Shaohua Li 已提交
741 742 743
			ret = radix_tree_delete_item(root, idx, t_page);
			WARN_ON(ret != t_page);
			null_free_page(ret);
S
Shaohua Li 已提交
744 745
			if (is_cache)
				nullb->dev->curr_cache -= PAGE_SIZE;
S
Shaohua Li 已提交
746 747 748 749 750
		}
	}
}

static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
S
Shaohua Li 已提交
751
	struct nullb_page *t_page, bool is_cache)
S
Shaohua Li 已提交
752 753 754
{
	struct radix_tree_root *root;

S
Shaohua Li 已提交
755
	root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
S
Shaohua Li 已提交
756 757 758 759 760

	if (radix_tree_insert(root, idx, t_page)) {
		null_free_page(t_page);
		t_page = radix_tree_lookup(root, idx);
		WARN_ON(!t_page || t_page->page->index != idx);
S
Shaohua Li 已提交
761 762
	} else if (is_cache)
		nullb->dev->curr_cache += PAGE_SIZE;
S
Shaohua Li 已提交
763 764 765 766

	return t_page;
}

S
Shaohua Li 已提交
767
static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
S
Shaohua Li 已提交
768 769 770 771 772 773
{
	unsigned long pos = 0;
	int nr_pages;
	struct nullb_page *ret, *t_pages[FREE_BATCH];
	struct radix_tree_root *root;

S
Shaohua Li 已提交
774
	root = is_cache ? &dev->cache : &dev->data;
S
Shaohua Li 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

	do {
		int i;

		nr_pages = radix_tree_gang_lookup(root,
				(void **)t_pages, pos, FREE_BATCH);

		for (i = 0; i < nr_pages; i++) {
			pos = t_pages[i]->page->index;
			ret = radix_tree_delete_item(root, pos, t_pages[i]);
			WARN_ON(ret != t_pages[i]);
			null_free_page(ret);
		}

		pos++;
	} while (nr_pages == FREE_BATCH);
S
Shaohua Li 已提交
791 792 793

	if (is_cache)
		dev->curr_cache = 0;
S
Shaohua Li 已提交
794 795
}

S
Shaohua Li 已提交
796 797
static struct nullb_page *__null_lookup_page(struct nullb *nullb,
	sector_t sector, bool for_write, bool is_cache)
S
Shaohua Li 已提交
798 799 800 801
{
	unsigned int sector_bit;
	u64 idx;
	struct nullb_page *t_page;
S
Shaohua Li 已提交
802
	struct radix_tree_root *root;
S
Shaohua Li 已提交
803 804 805 806

	idx = sector >> PAGE_SECTORS_SHIFT;
	sector_bit = (sector & SECTOR_MASK);

S
Shaohua Li 已提交
807 808
	root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
	t_page = radix_tree_lookup(root, idx);
S
Shaohua Li 已提交
809 810
	WARN_ON(t_page && t_page->page->index != idx);

811
	if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
S
Shaohua Li 已提交
812 813 814 815 816
		return t_page;

	return NULL;
}

S
Shaohua Li 已提交
817 818 819 820 821 822 823 824 825 826 827 828
static struct nullb_page *null_lookup_page(struct nullb *nullb,
	sector_t sector, bool for_write, bool ignore_cache)
{
	struct nullb_page *page = NULL;

	if (!ignore_cache)
		page = __null_lookup_page(nullb, sector, for_write, true);
	if (page)
		return page;
	return __null_lookup_page(nullb, sector, for_write, false);
}

S
Shaohua Li 已提交
829
static struct nullb_page *null_insert_page(struct nullb *nullb,
830 831 832
					   sector_t sector, bool ignore_cache)
	__releases(&nullb->lock)
	__acquires(&nullb->lock)
S
Shaohua Li 已提交
833 834 835 836
{
	u64 idx;
	struct nullb_page *t_page;

S
Shaohua Li 已提交
837
	t_page = null_lookup_page(nullb, sector, true, ignore_cache);
S
Shaohua Li 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
	if (t_page)
		return t_page;

	spin_unlock_irq(&nullb->lock);

	t_page = null_alloc_page(GFP_NOIO);
	if (!t_page)
		goto out_lock;

	if (radix_tree_preload(GFP_NOIO))
		goto out_freepage;

	spin_lock_irq(&nullb->lock);
	idx = sector >> PAGE_SECTORS_SHIFT;
	t_page->page->index = idx;
S
Shaohua Li 已提交
853
	t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
S
Shaohua Li 已提交
854 855 856 857 858 859 860
	radix_tree_preload_end();

	return t_page;
out_freepage:
	null_free_page(t_page);
out_lock:
	spin_lock_irq(&nullb->lock);
S
Shaohua Li 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
	return null_lookup_page(nullb, sector, true, ignore_cache);
}

static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
{
	int i;
	unsigned int offset;
	u64 idx;
	struct nullb_page *t_page, *ret;
	void *dst, *src;

	idx = c_page->page->index;

	t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);

876 877
	__clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
	if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
S
Shaohua Li 已提交
878
		null_free_page(c_page);
879
		if (t_page && null_page_empty(t_page)) {
S
Shaohua Li 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
			ret = radix_tree_delete_item(&nullb->dev->data,
				idx, t_page);
			null_free_page(t_page);
		}
		return 0;
	}

	if (!t_page)
		return -ENOMEM;

	src = kmap_atomic(c_page->page);
	dst = kmap_atomic(t_page->page);

	for (i = 0; i < PAGE_SECTORS;
			i += (nullb->dev->blocksize >> SECTOR_SHIFT)) {
895
		if (test_bit(i, c_page->bitmap)) {
S
Shaohua Li 已提交
896 897 898
			offset = (i << SECTOR_SHIFT);
			memcpy(dst + offset, src + offset,
				nullb->dev->blocksize);
899
			__set_bit(i, t_page->bitmap);
S
Shaohua Li 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913
		}
	}

	kunmap_atomic(dst);
	kunmap_atomic(src);

	ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page);
	null_free_page(ret);
	nullb->dev->curr_cache -= PAGE_SIZE;

	return 0;
}

static int null_make_cache_space(struct nullb *nullb, unsigned long n)
914
{
S
Shaohua Li 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
	int i, err, nr_pages;
	struct nullb_page *c_pages[FREE_BATCH];
	unsigned long flushed = 0, one_round;

again:
	if ((nullb->dev->cache_size * 1024 * 1024) >
	     nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0)
		return 0;

	nr_pages = radix_tree_gang_lookup(&nullb->dev->cache,
			(void **)c_pages, nullb->cache_flush_pos, FREE_BATCH);
	/*
	 * nullb_flush_cache_page could unlock before using the c_pages. To
	 * avoid race, we don't allow page free
	 */
	for (i = 0; i < nr_pages; i++) {
		nullb->cache_flush_pos = c_pages[i]->page->index;
		/*
		 * We found the page which is being flushed to disk by other
		 * threads
		 */
936
		if (test_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap))
S
Shaohua Li 已提交
937 938
			c_pages[i] = NULL;
		else
939
			__set_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap);
S
Shaohua Li 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
	}

	one_round = 0;
	for (i = 0; i < nr_pages; i++) {
		if (c_pages[i] == NULL)
			continue;
		err = null_flush_cache_page(nullb, c_pages[i]);
		if (err)
			return err;
		one_round++;
	}
	flushed += one_round << PAGE_SHIFT;

	if (n > flushed) {
		if (nr_pages == 0)
			nullb->cache_flush_pos = 0;
		if (one_round == 0) {
			/* give other threads a chance */
			spin_unlock_irq(&nullb->lock);
			spin_lock_irq(&nullb->lock);
		}
		goto again;
	}
	return 0;
S
Shaohua Li 已提交
964 965 966
}

static int copy_to_nullb(struct nullb *nullb, struct page *source,
S
Shaohua Li 已提交
967
	unsigned int off, sector_t sector, size_t n, bool is_fua)
S
Shaohua Li 已提交
968 969 970 971 972 973 974 975 976
{
	size_t temp, count = 0;
	unsigned int offset;
	struct nullb_page *t_page;
	void *dst, *src;

	while (count < n) {
		temp = min_t(size_t, nullb->dev->blocksize, n - count);

S
Shaohua Li 已提交
977 978 979
		if (null_cache_active(nullb) && !is_fua)
			null_make_cache_space(nullb, PAGE_SIZE);

S
Shaohua Li 已提交
980
		offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
S
Shaohua Li 已提交
981 982
		t_page = null_insert_page(nullb, sector,
			!null_cache_active(nullb) || is_fua);
S
Shaohua Li 已提交
983 984 985 986 987 988 989 990 991
		if (!t_page)
			return -ENOSPC;

		src = kmap_atomic(source);
		dst = kmap_atomic(t_page->page);
		memcpy(dst + offset, src + off + count, temp);
		kunmap_atomic(dst);
		kunmap_atomic(src);

992
		__set_bit(sector & SECTOR_MASK, t_page->bitmap);
S
Shaohua Li 已提交
993

S
Shaohua Li 已提交
994 995 996
		if (is_fua)
			null_free_sector(nullb, sector, true);

S
Shaohua Li 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
		count += temp;
		sector += temp >> SECTOR_SHIFT;
	}
	return 0;
}

static int copy_from_nullb(struct nullb *nullb, struct page *dest,
	unsigned int off, sector_t sector, size_t n)
{
	size_t temp, count = 0;
	unsigned int offset;
	struct nullb_page *t_page;
	void *dst, *src;

	while (count < n) {
		temp = min_t(size_t, nullb->dev->blocksize, n - count);

		offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
S
Shaohua Li 已提交
1015 1016
		t_page = null_lookup_page(nullb, sector, false,
			!null_cache_active(nullb));
S
Shaohua Li 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034

		dst = kmap_atomic(dest);
		if (!t_page) {
			memset(dst + off + count, 0, temp);
			goto next;
		}
		src = kmap_atomic(t_page->page);
		memcpy(dst + off + count, src + offset, temp);
		kunmap_atomic(src);
next:
		kunmap_atomic(dst);

		count += temp;
		sector += temp >> SECTOR_SHIFT;
	}
	return 0;
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
static void nullb_fill_pattern(struct nullb *nullb, struct page *page,
			       unsigned int len, unsigned int off)
{
	void *dst;

	dst = kmap_atomic(page);
	memset(dst + off, 0xFF, len);
	kunmap_atomic(dst);
}

S
Shaohua Li 已提交
1045 1046 1047 1048 1049 1050 1051
static void null_handle_discard(struct nullb *nullb, sector_t sector, size_t n)
{
	size_t temp;

	spin_lock_irq(&nullb->lock);
	while (n > 0) {
		temp = min_t(size_t, n, nullb->dev->blocksize);
S
Shaohua Li 已提交
1052 1053 1054
		null_free_sector(nullb, sector, false);
		if (null_cache_active(nullb))
			null_free_sector(nullb, sector, true);
S
Shaohua Li 已提交
1055 1056 1057 1058 1059 1060
		sector += temp >> SECTOR_SHIFT;
		n -= temp;
	}
	spin_unlock_irq(&nullb->lock);
}

S
Shaohua Li 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
static int null_handle_flush(struct nullb *nullb)
{
	int err;

	if (!null_cache_active(nullb))
		return 0;

	spin_lock_irq(&nullb->lock);
	while (true) {
		err = null_make_cache_space(nullb,
			nullb->dev->cache_size * 1024 * 1024);
		if (err || nullb->dev->curr_cache == 0)
			break;
	}

	WARN_ON(!radix_tree_empty(&nullb->dev->cache));
	spin_unlock_irq(&nullb->lock);
	return err;
}

S
Shaohua Li 已提交
1081
static int null_transfer(struct nullb *nullb, struct page *page,
S
Shaohua Li 已提交
1082 1083
	unsigned int len, unsigned int off, bool is_write, sector_t sector,
	bool is_fua)
1084
{
1085 1086
	struct nullb_device *dev = nullb->dev;
	unsigned int valid_len = len;
S
Shaohua Li 已提交
1087 1088 1089
	int err = 0;

	if (!is_write) {
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
		if (dev->zoned)
			valid_len = null_zone_valid_read_len(nullb,
				sector, len);

		if (valid_len) {
			err = copy_from_nullb(nullb, page, off,
				sector, valid_len);
			off += valid_len;
			len -= valid_len;
		}

		if (len)
			nullb_fill_pattern(nullb, page, len, off);
S
Shaohua Li 已提交
1103 1104 1105
		flush_dcache_page(page);
	} else {
		flush_dcache_page(page);
S
Shaohua Li 已提交
1106
		err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
S
Shaohua Li 已提交
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	}

	return err;
}

static int null_handle_rq(struct nullb_cmd *cmd)
{
	struct request *rq = cmd->rq;
	struct nullb *nullb = cmd->nq->dev->nullb;
	int err;
	unsigned int len;
	sector_t sector;
	struct req_iterator iter;
	struct bio_vec bvec;

	sector = blk_rq_pos(rq);

S
Shaohua Li 已提交
1124 1125 1126 1127 1128
	if (req_op(rq) == REQ_OP_DISCARD) {
		null_handle_discard(nullb, sector, blk_rq_bytes(rq));
		return 0;
	}

S
Shaohua Li 已提交
1129 1130 1131 1132
	spin_lock_irq(&nullb->lock);
	rq_for_each_segment(bvec, rq, iter) {
		len = bvec.bv_len;
		err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
S
Shaohua Li 已提交
1133 1134
				     op_is_write(req_op(rq)), sector,
				     req_op(rq) & REQ_FUA);
S
Shaohua Li 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
		if (err) {
			spin_unlock_irq(&nullb->lock);
			return err;
		}
		sector += len >> SECTOR_SHIFT;
	}
	spin_unlock_irq(&nullb->lock);

	return 0;
}

static int null_handle_bio(struct nullb_cmd *cmd)
{
	struct bio *bio = cmd->bio;
	struct nullb *nullb = cmd->nq->dev->nullb;
	int err;
	unsigned int len;
	sector_t sector;
	struct bio_vec bvec;
	struct bvec_iter iter;

	sector = bio->bi_iter.bi_sector;

S
Shaohua Li 已提交
1158 1159 1160 1161 1162 1163
	if (bio_op(bio) == REQ_OP_DISCARD) {
		null_handle_discard(nullb, sector,
			bio_sectors(bio) << SECTOR_SHIFT);
		return 0;
	}

S
Shaohua Li 已提交
1164 1165 1166 1167
	spin_lock_irq(&nullb->lock);
	bio_for_each_segment(bvec, bio, iter) {
		len = bvec.bv_len;
		err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
S
Shaohua Li 已提交
1168
				     op_is_write(bio_op(bio)), sector,
1169
				     bio->bi_opf & REQ_FUA);
S
Shaohua Li 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
		if (err) {
			spin_unlock_irq(&nullb->lock);
			return err;
		}
		sector += len >> SECTOR_SHIFT;
	}
	spin_unlock_irq(&nullb->lock);
	return 0;
}

S
Shaohua Li 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
static void null_stop_queue(struct nullb *nullb)
{
	struct request_queue *q = nullb->q;

	if (nullb->dev->queue_mode == NULL_Q_MQ)
		blk_mq_stop_hw_queues(q);
}

static void null_restart_queue_async(struct nullb *nullb)
{
	struct request_queue *q = nullb->q;

	if (nullb->dev->queue_mode == NULL_Q_MQ)
		blk_mq_start_stopped_hw_queues(q, true);
}

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
static inline blk_status_t null_handle_throttled(struct nullb_cmd *cmd)
{
	struct nullb_device *dev = cmd->nq->dev;
	struct nullb *nullb = dev->nullb;
	blk_status_t sts = BLK_STS_OK;
	struct request *rq = cmd->rq;

	if (!hrtimer_active(&nullb->bw_timer))
		hrtimer_restart(&nullb->bw_timer);

	if (atomic_long_sub_return(blk_rq_bytes(rq), &nullb->cur_bytes) < 0) {
		null_stop_queue(nullb);
		/* race with timer */
		if (atomic_long_read(&nullb->cur_bytes) > 0)
			null_restart_queue_async(nullb);
		/* requeue request */
		sts = BLK_STS_DEV_RESOURCE;
	}
	return sts;
}

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
static inline blk_status_t null_handle_badblocks(struct nullb_cmd *cmd,
						 sector_t sector,
						 sector_t nr_sectors)
{
	struct badblocks *bb = &cmd->nq->dev->badblocks;
	sector_t first_bad;
	int bad_sectors;

	if (badblocks_check(bb, sector, nr_sectors, &first_bad, &bad_sectors))
		return BLK_STS_IOERR;

	return BLK_STS_OK;
}

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
static inline blk_status_t null_handle_memory_backed(struct nullb_cmd *cmd,
						     enum req_opf op)
{
	struct nullb_device *dev = cmd->nq->dev;
	int err;

	if (dev->queue_mode == NULL_Q_BIO)
		err = null_handle_bio(cmd);
	else
		err = null_handle_rq(cmd);

	return errno_to_blk_status(err);
}

1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
static inline void nullb_complete_cmd(struct nullb_cmd *cmd)
{
	/* Complete IO by inline, softirq or timer */
	switch (cmd->nq->dev->irqmode) {
	case NULL_IRQ_SOFTIRQ:
		switch (cmd->nq->dev->queue_mode) {
		case NULL_Q_MQ:
			blk_mq_complete_request(cmd->rq);
			break;
		case NULL_Q_BIO:
			/*
			 * XXX: no proper submitting cpu information available.
			 */
			end_cmd(cmd);
			break;
		}
		break;
	case NULL_IRQ_NONE:
		end_cmd(cmd);
		break;
	case NULL_IRQ_TIMER:
		null_cmd_end_timer(cmd);
		break;
	}
}

1271 1272
static blk_status_t null_handle_cmd(struct nullb_cmd *cmd, sector_t sector,
				    sector_t nr_sectors, enum req_opf op)
S
Shaohua Li 已提交
1273 1274
{
	struct nullb_device *dev = cmd->nq->dev;
S
Shaohua Li 已提交
1275
	struct nullb *nullb = dev->nullb;
1276
	blk_status_t sts;
S
Shaohua Li 已提交
1277

S
Shaohua Li 已提交
1278
	if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) {
1279 1280 1281
		sts = null_handle_throttled(cmd);
		if (sts != BLK_STS_OK)
			return sts;
S
Shaohua Li 已提交
1282 1283
	}

1284 1285 1286 1287 1288
	if (op == REQ_OP_FLUSH) {
		cmd->error = errno_to_blk_status(null_handle_flush(nullb));
		goto out;
	}

1289 1290 1291
	if (nullb->dev->badblocks.shift != -1) {
		cmd->error = null_handle_badblocks(cmd, sector, nr_sectors);
		if (cmd->error != BLK_STS_OK)
S
Shaohua Li 已提交
1292 1293 1294
			goto out;
	}

1295 1296
	if (dev->memory_backed)
		cmd->error = null_handle_memory_backed(cmd, op);
M
Matias Bjørling 已提交
1297

1298 1299 1300
	if (!cmd->error && dev->zoned)
		cmd->error = null_handle_zoned(cmd, op, sector, nr_sectors);

S
Shaohua Li 已提交
1301
out:
1302
	nullb_complete_cmd(cmd);
S
Shaohua Li 已提交
1303
	return BLK_STS_OK;
1304 1305
}

S
Shaohua Li 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
{
	struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
	ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
	unsigned int mbps = nullb->dev->mbps;

	if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
		return HRTIMER_NORESTART;

	atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
	null_restart_queue_async(nullb);

	hrtimer_forward_now(&nullb->bw_timer, timer_interval);

	return HRTIMER_RESTART;
}

static void nullb_setup_bwtimer(struct nullb *nullb)
{
	ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);

	hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	nullb->bw_timer.function = nullb_bwtimer_fn;
	atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps));
	hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL);
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
}

static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
{
	int index = 0;

	if (nullb->nr_queues != 1)
		index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);

	return &nullb->queues[index];
}

1343
static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
1344
{
1345 1346
	sector_t sector = bio->bi_iter.bi_sector;
	sector_t nr_sectors = bio_sectors(bio);
1347 1348 1349 1350 1351 1352 1353
	struct nullb *nullb = q->queuedata;
	struct nullb_queue *nq = nullb_to_queue(nullb);
	struct nullb_cmd *cmd;

	cmd = alloc_cmd(nq, 1);
	cmd->bio = bio;

1354
	null_handle_cmd(cmd, sector, nr_sectors, bio_op(bio));
1355
	return BLK_QC_T_NONE;
1356 1357
}

1358 1359
static bool should_timeout_request(struct request *rq)
{
1360
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1361 1362
	if (g_timeout_str[0])
		return should_fail(&null_timeout_attr, 1);
1363
#endif
1364 1365
	return false;
}
1366

1367 1368 1369 1370 1371 1372
static bool should_requeue_request(struct request *rq)
{
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
	if (g_requeue_str[0])
		return should_fail(&null_requeue_attr, 1);
#endif
1373 1374 1375
	return false;
}

J
Jens Axboe 已提交
1376 1377
static enum blk_eh_timer_return null_timeout_rq(struct request *rq, bool res)
{
1378
	pr_info("rq %p timed out\n", rq);
1379 1380
	blk_mq_complete_request(rq);
	return BLK_EH_DONE;
J
Jens Axboe 已提交
1381 1382
}

1383
static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
1384
			 const struct blk_mq_queue_data *bd)
1385
{
1386
	struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
S
Shaohua Li 已提交
1387
	struct nullb_queue *nq = hctx->driver_data;
1388 1389
	sector_t nr_sectors = blk_rq_sectors(bd->rq);
	sector_t sector = blk_rq_pos(bd->rq);
1390

J
Jens Axboe 已提交
1391 1392
	might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);

S
Shaohua Li 已提交
1393
	if (nq->dev->irqmode == NULL_IRQ_TIMER) {
1394 1395 1396
		hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
		cmd->timer.function = null_cmd_timer_expired;
	}
1397
	cmd->rq = bd->rq;
S
Shaohua Li 已提交
1398
	cmd->nq = nq;
1399

1400
	blk_mq_start_request(bd->rq);
1401

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
	if (should_requeue_request(bd->rq)) {
		/*
		 * Alternate between hitting the core BUSY path, and the
		 * driver driven requeue path
		 */
		nq->requeue_selection++;
		if (nq->requeue_selection & 1)
			return BLK_STS_RESOURCE;
		else {
			blk_mq_requeue_request(bd->rq, true);
			return BLK_STS_OK;
		}
	}
	if (should_timeout_request(bd->rq))
		return BLK_STS_OK;
1417

1418
	return null_handle_cmd(cmd, sector, nr_sectors, req_op(bd->rq));
1419 1420
}

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
static void cleanup_queue(struct nullb_queue *nq)
{
	kfree(nq->tag_map);
	kfree(nq->cmds);
}

static void cleanup_queues(struct nullb *nullb)
{
	int i;

	for (i = 0; i < nullb->nr_queues; i++)
		cleanup_queue(&nullb->queues[i]);

	kfree(nullb->queues);
}

1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
static void null_exit_hctx(struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
{
	struct nullb_queue *nq = hctx->driver_data;
	struct nullb *nullb = nq->dev->nullb;

	nullb->nr_queues--;
}

static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
{
	init_waitqueue_head(&nq->wait);
	nq->queue_depth = nullb->queue_depth;
	nq->dev = nullb->dev;
}

static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
			  unsigned int hctx_idx)
{
	struct nullb *nullb = hctx->queue->queuedata;
	struct nullb_queue *nq;

1458 1459 1460 1461 1462
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
	if (g_init_hctx_str[0] && should_fail(&null_init_hctx_attr, 1))
		return -EFAULT;
#endif

1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
	nq = &nullb->queues[hctx_idx];
	hctx->driver_data = nq;
	null_init_queue(nullb, nq);
	nullb->nr_queues++;

	return 0;
}

static const struct blk_mq_ops null_mq_ops = {
	.queue_rq       = null_queue_rq,
	.complete	= null_complete_rq,
	.timeout	= null_timeout_rq,
	.init_hctx	= null_init_hctx,
	.exit_hctx	= null_exit_hctx,
};

1479 1480
static void null_del_dev(struct nullb *nullb)
{
1481 1482 1483 1484 1485 1486
	struct nullb_device *dev;

	if (!nullb)
		return;

	dev = nullb->dev;
S
Shaohua Li 已提交
1487

S
Shaohua Li 已提交
1488 1489
	ida_simple_remove(&nullb_indexes, nullb->index);

1490 1491
	list_del_init(&nullb->list);

1492
	del_gendisk(nullb->disk);
S
Shaohua Li 已提交
1493 1494 1495 1496 1497 1498 1499

	if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
		hrtimer_cancel(&nullb->bw_timer);
		atomic_long_set(&nullb->cur_bytes, LONG_MAX);
		null_restart_queue_async(nullb);
	}

1500
	blk_cleanup_queue(nullb->q);
S
Shaohua Li 已提交
1501 1502
	if (dev->queue_mode == NULL_Q_MQ &&
	    nullb->tag_set == &nullb->__tag_set)
1503
		blk_mq_free_tag_set(nullb->tag_set);
1504
	put_disk(nullb->disk);
1505
	cleanup_queues(nullb);
S
Shaohua Li 已提交
1506 1507
	if (null_cache_active(nullb))
		null_free_device_storage(nullb->dev, true);
1508
	kfree(nullb);
S
Shaohua Li 已提交
1509
	dev->nullb = NULL;
1510 1511
}

S
Shaohua Li 已提交
1512 1513 1514 1515 1516 1517 1518
static void null_config_discard(struct nullb *nullb)
{
	if (nullb->dev->discard == false)
		return;
	nullb->q->limits.discard_granularity = nullb->dev->blocksize;
	nullb->q->limits.discard_alignment = nullb->dev->blocksize;
	blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9);
1519
	blk_queue_flag_set(QUEUE_FLAG_DISCARD, nullb->q);
1520 1521
}

1522 1523
static const struct block_device_operations null_ops = {
	.owner		= THIS_MODULE,
1524
	.report_zones	= null_report_zones,
1525 1526 1527 1528 1529 1530 1531
};

static int setup_commands(struct nullb_queue *nq)
{
	struct nullb_cmd *cmd;
	int i, tag_size;

K
Kees Cook 已提交
1532
	nq->cmds = kcalloc(nq->queue_depth, sizeof(*cmd), GFP_KERNEL);
1533
	if (!nq->cmds)
1534
		return -ENOMEM;
1535 1536

	tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
K
Kees Cook 已提交
1537
	nq->tag_map = kcalloc(tag_size, sizeof(unsigned long), GFP_KERNEL);
1538 1539
	if (!nq->tag_map) {
		kfree(nq->cmds);
1540
		return -ENOMEM;
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
	}

	for (i = 0; i < nq->queue_depth; i++) {
		cmd = &nq->cmds[i];
		cmd->tag = -1U;
	}

	return 0;
}

static int setup_queues(struct nullb *nullb)
{
1553
	nullb->queues = kcalloc(nr_cpu_ids, sizeof(struct nullb_queue),
K
Kees Cook 已提交
1554
				GFP_KERNEL);
1555
	if (!nullb->queues)
1556
		return -ENOMEM;
1557

S
Shaohua Li 已提交
1558
	nullb->queue_depth = nullb->dev->hw_queue_depth;
1559

1560 1561 1562 1563 1564 1565 1566
	return 0;
}

static int init_driver_queues(struct nullb *nullb)
{
	struct nullb_queue *nq;
	int i, ret = 0;
1567

S
Shaohua Li 已提交
1568
	for (i = 0; i < nullb->dev->submit_queues; i++) {
1569
		nq = &nullb->queues[i];
1570 1571 1572 1573 1574

		null_init_queue(nullb, nq);

		ret = setup_commands(nq);
		if (ret)
1575
			return ret;
1576 1577
		nullb->nr_queues++;
	}
1578
	return 0;
1579 1580
}

1581
static int null_gendisk_register(struct nullb *nullb)
1582
{
1583
	sector_t size = ((sector_t)nullb->dev->size * SZ_1M) >> SECTOR_SHIFT;
1584
	struct gendisk *disk;
1585

S
Shaohua Li 已提交
1586
	disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node);
1587 1588
	if (!disk)
		return -ENOMEM;
1589
	set_capacity(disk, size);
1590 1591 1592 1593

	disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
	disk->major		= null_major;
	disk->first_minor	= nullb->index;
1594
	disk->fops		= &null_ops;
1595 1596 1597 1598
	disk->private_data	= nullb;
	disk->queue		= nullb->q;
	strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);

1599
#ifdef CONFIG_BLK_DEV_ZONED
1600
	if (nullb->dev->zoned) {
1601
		if (queue_is_mq(nullb->q)) {
1602
			int ret = blk_revalidate_disk_zones(disk);
1603 1604 1605
			if (ret)
				return ret;
		} else {
1606 1607
			blk_queue_chunk_sectors(nullb->q,
					nullb->dev->zone_size_sects);
1608 1609
			nullb->q->nr_zones = blkdev_nr_zones(disk);
		}
1610
	}
1611
#endif
1612

1613 1614 1615 1616
	add_disk(disk);
	return 0;
}

S
Shaohua Li 已提交
1617
static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set)
1618 1619
{
	set->ops = &null_mq_ops;
S
Shaohua Li 已提交
1620 1621 1622 1623 1624
	set->nr_hw_queues = nullb ? nullb->dev->submit_queues :
						g_submit_queues;
	set->queue_depth = nullb ? nullb->dev->hw_queue_depth :
						g_hw_queue_depth;
	set->numa_node = nullb ? nullb->dev->home_node : g_home_node;
1625 1626
	set->cmd_size	= sizeof(struct nullb_cmd);
	set->flags = BLK_MQ_F_SHOULD_MERGE;
1627 1628
	if (g_no_sched)
		set->flags |= BLK_MQ_F_NO_SCHED;
1629 1630
	set->driver_data = NULL;

S
Shaohua Li 已提交
1631
	if ((nullb && nullb->dev->blocking) || g_blocking)
1632 1633 1634 1635 1636
		set->flags |= BLK_MQ_F_BLOCKING;

	return blk_mq_alloc_tag_set(set);
}

1637
static int null_validate_conf(struct nullb_device *dev)
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
{
	dev->blocksize = round_down(dev->blocksize, 512);
	dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096);

	if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) {
		if (dev->submit_queues != nr_online_nodes)
			dev->submit_queues = nr_online_nodes;
	} else if (dev->submit_queues > nr_cpu_ids)
		dev->submit_queues = nr_cpu_ids;
	else if (dev->submit_queues == 0)
		dev->submit_queues = 1;

	dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ);
	dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
S
Shaohua Li 已提交
1652 1653 1654 1655

	/* Do memory allocation, so set blocking */
	if (dev->memory_backed)
		dev->blocking = true;
S
Shaohua Li 已提交
1656 1657 1658 1659
	else /* cache is meaningless */
		dev->cache_size = 0;
	dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
						dev->cache_size);
S
Shaohua Li 已提交
1660 1661 1662 1663
	dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
	/* can not stop a queue */
	if (dev->queue_mode == NULL_Q_BIO)
		dev->mbps = 0;
1664 1665 1666 1667 1668 1669 1670 1671

	if (dev->zoned &&
	    (!dev->zone_size || !is_power_of_2(dev->zone_size))) {
		pr_err("zone_size must be power-of-two\n");
		return -EINVAL;
	}

	return 0;
1672 1673
}

1674
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1675 1676 1677
static bool __null_setup_fault(struct fault_attr *attr, char *str)
{
	if (!str[0])
1678 1679
		return true;

1680
	if (!setup_fault_attr(attr, str))
1681 1682
		return false;

1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
	attr->verbose = 0;
	return true;
}
#endif

static bool null_setup_fault(void)
{
#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
	if (!__null_setup_fault(&null_timeout_attr, g_timeout_str))
		return false;
	if (!__null_setup_fault(&null_requeue_attr, g_requeue_str))
		return false;
1695 1696
	if (!__null_setup_fault(&null_init_hctx_attr, g_init_hctx_str))
		return false;
1697
#endif
1698 1699 1700
	return true;
}

S
Shaohua Li 已提交
1701
static int null_add_dev(struct nullb_device *dev)
1702 1703
{
	struct nullb *nullb;
1704
	int rv;
1705

1706 1707 1708
	rv = null_validate_conf(dev);
	if (rv)
		return rv;
1709

S
Shaohua Li 已提交
1710
	nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
1711 1712
	if (!nullb) {
		rv = -ENOMEM;
1713
		goto out;
1714
	}
S
Shaohua Li 已提交
1715 1716
	nullb->dev = dev;
	dev->nullb = nullb;
1717 1718 1719

	spin_lock_init(&nullb->lock);

1720 1721
	rv = setup_queues(nullb);
	if (rv)
1722
		goto out_free_nullb;
1723

S
Shaohua Li 已提交
1724
	if (dev->queue_mode == NULL_Q_MQ) {
1725 1726 1727 1728 1729
		if (shared_tags) {
			nullb->tag_set = &tag_set;
			rv = 0;
		} else {
			nullb->tag_set = &nullb->__tag_set;
S
Shaohua Li 已提交
1730
			rv = null_init_tag_set(nullb, nullb->tag_set);
1731 1732
		}

1733
		if (rv)
1734 1735
			goto out_cleanup_queues;

1736 1737 1738
		if (!null_setup_fault())
			goto out_cleanup_queues;

J
Jens Axboe 已提交
1739
		nullb->tag_set->timeout = 5 * HZ;
1740
		nullb->q = blk_mq_init_queue_data(nullb->tag_set, nullb);
1741
		if (IS_ERR(nullb->q)) {
1742
			rv = -ENOMEM;
1743
			goto out_cleanup_tags;
1744
		}
S
Shaohua Li 已提交
1745
	} else if (dev->queue_mode == NULL_Q_BIO) {
1746
		nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node);
1747 1748
		if (!nullb->q) {
			rv = -ENOMEM;
1749
			goto out_cleanup_queues;
1750
		}
1751
		blk_queue_make_request(nullb->q, null_queue_bio);
1752 1753 1754
		rv = init_driver_queues(nullb);
		if (rv)
			goto out_cleanup_blk_queue;
1755 1756
	}

S
Shaohua Li 已提交
1757 1758 1759 1760 1761
	if (dev->mbps) {
		set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags);
		nullb_setup_bwtimer(nullb);
	}

S
Shaohua Li 已提交
1762 1763 1764 1765 1766
	if (dev->cache_size > 0) {
		set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
		blk_queue_write_cache(nullb->q, true, true);
	}

M
Matias Bjørling 已提交
1767 1768 1769 1770 1771 1772
	if (dev->zoned) {
		rv = null_zone_init(dev);
		if (rv)
			goto out_cleanup_blk_queue;

		nullb->q->limits.zoned = BLK_ZONED_HM;
1773
		blk_queue_flag_set(QUEUE_FLAG_ZONE_RESETALL, nullb->q);
1774 1775
		blk_queue_required_elevator_features(nullb->q,
						ELEVATOR_F_ZBD_SEQ_WRITE);
M
Matias Bjørling 已提交
1776 1777
	}

1778
	nullb->q->queuedata = nullb;
1779 1780
	blk_queue_flag_set(QUEUE_FLAG_NONROT, nullb->q);
	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, nullb->q);
1781 1782

	mutex_lock(&lock);
S
Shaohua Li 已提交
1783
	nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
1784
	dev->index = nullb->index;
1785 1786
	mutex_unlock(&lock);

S
Shaohua Li 已提交
1787 1788
	blk_queue_logical_block_size(nullb->q, dev->blocksize);
	blk_queue_physical_block_size(nullb->q, dev->blocksize);
1789

S
Shaohua Li 已提交
1790
	null_config_discard(nullb);
1791

1792 1793
	sprintf(nullb->disk_name, "nullb%d", nullb->index);

1794
	rv = null_gendisk_register(nullb);
1795
	if (rv)
M
Matias Bjørling 已提交
1796
		goto out_cleanup_zone;
1797 1798 1799 1800

	mutex_lock(&lock);
	list_add_tail(&nullb->list, &nullb_list);
	mutex_unlock(&lock);
1801

1802
	return 0;
M
Matias Bjørling 已提交
1803 1804 1805
out_cleanup_zone:
	if (dev->zoned)
		null_zone_exit(dev);
1806 1807 1808
out_cleanup_blk_queue:
	blk_cleanup_queue(nullb->q);
out_cleanup_tags:
S
Shaohua Li 已提交
1809
	if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set)
1810
		blk_mq_free_tag_set(nullb->tag_set);
1811 1812 1813 1814
out_cleanup_queues:
	cleanup_queues(nullb);
out_free_nullb:
	kfree(nullb);
1815
	dev->nullb = NULL;
1816
out:
1817
	return rv;
1818 1819 1820 1821
}

static int __init null_init(void)
{
1822
	int ret = 0;
1823
	unsigned int i;
1824
	struct nullb *nullb;
S
Shaohua Li 已提交
1825
	struct nullb_device *dev;
1826

S
Shaohua Li 已提交
1827
	if (g_bs > PAGE_SIZE) {
1828 1829
		pr_warn("invalid block size\n");
		pr_warn("defaults block size to %lu\n", PAGE_SIZE);
S
Shaohua Li 已提交
1830
		g_bs = PAGE_SIZE;
1831
	}
1832

1833
	if (g_home_node != NUMA_NO_NODE && g_home_node >= nr_online_nodes) {
1834
		pr_err("invalid home_node value\n");
1835 1836 1837
		g_home_node = NUMA_NO_NODE;
	}

J
Jens Axboe 已提交
1838
	if (g_queue_mode == NULL_Q_RQ) {
1839
		pr_err("legacy IO path no longer available\n");
J
Jens Axboe 已提交
1840 1841
		return -EINVAL;
	}
S
Shaohua Li 已提交
1842 1843
	if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) {
		if (g_submit_queues != nr_online_nodes) {
1844
			pr_warn("submit_queues param is set to %u.\n",
1845
							nr_online_nodes);
S
Shaohua Li 已提交
1846
			g_submit_queues = nr_online_nodes;
1847
		}
S
Shaohua Li 已提交
1848 1849 1850 1851
	} else if (g_submit_queues > nr_cpu_ids)
		g_submit_queues = nr_cpu_ids;
	else if (g_submit_queues <= 0)
		g_submit_queues = 1;
1852

S
Shaohua Li 已提交
1853 1854
	if (g_queue_mode == NULL_Q_MQ && shared_tags) {
		ret = null_init_tag_set(NULL, &tag_set);
1855 1856 1857 1858
		if (ret)
			return ret;
	}

S
Shaohua Li 已提交
1859 1860 1861 1862 1863 1864 1865
	config_group_init(&nullb_subsys.su_group);
	mutex_init(&nullb_subsys.su_mutex);

	ret = configfs_register_subsystem(&nullb_subsys);
	if (ret)
		goto err_tagset;

1866 1867 1868
	mutex_init(&lock);

	null_major = register_blkdev(0, "nullb");
1869 1870
	if (null_major < 0) {
		ret = null_major;
S
Shaohua Li 已提交
1871
		goto err_conf;
1872
	}
1873 1874

	for (i = 0; i < nr_devices; i++) {
S
Shaohua Li 已提交
1875
		dev = null_alloc_dev();
1876 1877
		if (!dev) {
			ret = -ENOMEM;
S
Shaohua Li 已提交
1878
			goto err_dev;
1879
		}
S
Shaohua Li 已提交
1880 1881 1882
		ret = null_add_dev(dev);
		if (ret) {
			null_free_dev(dev);
1883
			goto err_dev;
S
Shaohua Li 已提交
1884
		}
1885 1886
	}

1887
	pr_info("module loaded\n");
1888
	return 0;
1889 1890 1891 1892

err_dev:
	while (!list_empty(&nullb_list)) {
		nullb = list_entry(nullb_list.next, struct nullb, list);
S
Shaohua Li 已提交
1893
		dev = nullb->dev;
1894
		null_del_dev(nullb);
S
Shaohua Li 已提交
1895
		null_free_dev(dev);
1896 1897
	}
	unregister_blkdev(null_major, "nullb");
S
Shaohua Li 已提交
1898 1899
err_conf:
	configfs_unregister_subsystem(&nullb_subsys);
1900
err_tagset:
S
Shaohua Li 已提交
1901
	if (g_queue_mode == NULL_Q_MQ && shared_tags)
1902
		blk_mq_free_tag_set(&tag_set);
1903
	return ret;
1904 1905 1906 1907 1908 1909
}

static void __exit null_exit(void)
{
	struct nullb *nullb;

S
Shaohua Li 已提交
1910 1911
	configfs_unregister_subsystem(&nullb_subsys);

1912 1913 1914 1915
	unregister_blkdev(null_major, "nullb");

	mutex_lock(&lock);
	while (!list_empty(&nullb_list)) {
S
Shaohua Li 已提交
1916 1917
		struct nullb_device *dev;

1918
		nullb = list_entry(nullb_list.next, struct nullb, list);
S
Shaohua Li 已提交
1919
		dev = nullb->dev;
1920
		null_del_dev(nullb);
S
Shaohua Li 已提交
1921
		null_free_dev(dev);
1922 1923
	}
	mutex_unlock(&lock);
M
Matias Bjørling 已提交
1924

S
Shaohua Li 已提交
1925
	if (g_queue_mode == NULL_Q_MQ && shared_tags)
1926
		blk_mq_free_tag_set(&tag_set);
1927 1928 1929 1930 1931
}

module_init(null_init);
module_exit(null_exit);

J
Jens Axboe 已提交
1932
MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
1933
MODULE_LICENSE("GPL");