null_blk.c 48.1 KB
Newer Older
Shaohua Li's avatar
Shaohua Li committed
1 2 3 4
/*
 * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
 * Shaohua Li <shli@fb.com>
 */
5
#include <linux/module.h>
6

7 8 9 10 11 12 13 14
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/blk-mq.h>
#include <linux/hrtimer.h>
15
#include <linux/lightnvm.h>
Shaohua Li's avatar
Shaohua Li committed
16
#include <linux/configfs.h>
Shaohua Li's avatar
Shaohua Li committed
17
#include <linux/badblocks.h>
18

19 20 21 22 23 24 25 26
#define SECTOR_SHIFT		9
#define PAGE_SECTORS_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
#define PAGE_SECTORS		(1 << PAGE_SECTORS_SHIFT)
#define SECTOR_SIZE		(1 << SECTOR_SHIFT)
#define SECTOR_MASK		(PAGE_SECTORS - 1)

#define FREE_BATCH		16

Shaohua Li's avatar
Shaohua Li committed
27 28 29 30 31 32 33
#define TICKS_PER_SEC		50ULL
#define TIMER_INTERVAL		(NSEC_PER_SEC / TICKS_PER_SEC)

static inline u64 mb_per_tick(int mbps)
{
	return (1 << 20) / TICKS_PER_SEC * ((u64) mbps);
}
34 35 36 37

struct nullb_cmd {
	struct list_head list;
	struct llist_node ll_list;
38
	call_single_data_t csd;
39 40 41 42
	struct request *rq;
	struct bio *bio;
	unsigned int tag;
	struct nullb_queue *nq;
43
	struct hrtimer timer;
44
	blk_status_t error;
45 46 47 48 49 50
};

struct nullb_queue {
	unsigned long *tag_map;
	wait_queue_head_t wait;
	unsigned int queue_depth;
Shaohua Li's avatar
Shaohua Li committed
51
	struct nullb_device *dev;
52 53 54 55

	struct nullb_cmd *cmds;
};

Shaohua Li's avatar
Shaohua Li committed
56 57 58 59 60
/*
 * Status flags for nullb_device.
 *
 * CONFIGURED:	Device has been configured and turned on. Cannot reconfigure.
 * UP:		Device is currently on and visible in userspace.
Shaohua Li's avatar
Shaohua Li committed
61
 * THROTTLED:	Device is being throttled.
Shaohua Li's avatar
Shaohua Li committed
62
 * CACHE:	Device is using a write-back cache.
Shaohua Li's avatar
Shaohua Li committed
63 64 65 66
 */
enum nullb_device_flags {
	NULLB_DEV_FL_CONFIGURED	= 0,
	NULLB_DEV_FL_UP		= 1,
Shaohua Li's avatar
Shaohua Li committed
67
	NULLB_DEV_FL_THROTTLED	= 2,
Shaohua Li's avatar
Shaohua Li committed
68
	NULLB_DEV_FL_CACHE	= 3,
Shaohua Li's avatar
Shaohua Li committed
69 70
};

71
#define MAP_SZ		((PAGE_SIZE >> SECTOR_SHIFT) + 2)
72 73 74 75 76 77 78
/*
 * 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
Shaohua Li's avatar
Shaohua Li committed
79 80 81 82
 * 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
83 84 85
 */
struct nullb_page {
	struct page *page;
86
	DECLARE_BITMAP(bitmap, MAP_SZ);
87
};
88 89
#define NULLB_PAGE_LOCK (MAP_SZ - 1)
#define NULLB_PAGE_FREE (MAP_SZ - 2)
90

Shaohua Li's avatar
Shaohua Li committed
91 92
struct nullb_device {
	struct nullb *nullb;
Shaohua Li's avatar
Shaohua Li committed
93
	struct config_item item;
94
	struct radix_tree_root data; /* data stored in the disk */
Shaohua Li's avatar
Shaohua Li committed
95
	struct radix_tree_root cache; /* disk cache data */
Shaohua Li's avatar
Shaohua Li committed
96
	unsigned long flags; /* device flags */
Shaohua Li's avatar
Shaohua Li committed
97
	unsigned int curr_cache;
Shaohua Li's avatar
Shaohua Li committed
98
	struct badblocks badblocks;
Shaohua Li's avatar
Shaohua Li committed
99 100 101

	unsigned long size; /* device size in MB */
	unsigned long completion_nsec; /* time in ns to complete a request */
Shaohua Li's avatar
Shaohua Li committed
102
	unsigned long cache_size; /* disk cache size in MB */
Shaohua Li's avatar
Shaohua Li committed
103 104 105 106 107 108
	unsigned int submit_queues; /* number of submission queues */
	unsigned int home_node; /* home node for the device */
	unsigned int queue_mode; /* block interface */
	unsigned int blocksize; /* block size */
	unsigned int irqmode; /* IRQ completion handler */
	unsigned int hw_queue_depth; /* queue depth */
109
	unsigned int index; /* index of the disk, only valid with a disk */
Shaohua Li's avatar
Shaohua Li committed
110
	unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */
Shaohua Li's avatar
Shaohua Li committed
111 112 113
	bool use_lightnvm; /* register as a LightNVM device */
	bool blocking; /* blocking blk-mq device */
	bool use_per_node_hctx; /* use per-node allocation for hardware context */
114
	bool power; /* power on/off the device */
115
	bool memory_backed; /* if data is stored in memory */
Shaohua Li's avatar
Shaohua Li committed
116
	bool discard; /* if support discard */
Shaohua Li's avatar
Shaohua Li committed
117 118
};

119
struct nullb {
Shaohua Li's avatar
Shaohua Li committed
120
	struct nullb_device *dev;
121 122 123 124
	struct list_head list;
	unsigned int index;
	struct request_queue *q;
	struct gendisk *disk;
125
	struct nvm_dev *ndev;
126 127
	struct blk_mq_tag_set *tag_set;
	struct blk_mq_tag_set __tag_set;
128
	unsigned int queue_depth;
Shaohua Li's avatar
Shaohua Li committed
129 130
	atomic_long_t cur_bytes;
	struct hrtimer bw_timer;
Shaohua Li's avatar
Shaohua Li committed
131
	unsigned long cache_flush_pos;
132 133 134 135
	spinlock_t lock;

	struct nullb_queue *queues;
	unsigned int nr_queues;
136
	char disk_name[DISK_NAME_LEN];
137 138 139 140 141
};

static LIST_HEAD(nullb_list);
static struct mutex lock;
static int null_major;
Shaohua Li's avatar
Shaohua Li committed
142
static DEFINE_IDA(nullb_indexes);
143
static struct kmem_cache *ppa_cache;
144
static struct blk_mq_tag_set tag_set;
145 146 147 148 149

enum {
	NULL_IRQ_NONE		= 0,
	NULL_IRQ_SOFTIRQ	= 1,
	NULL_IRQ_TIMER		= 2,
150
};
151

152
enum {
153 154 155 156 157
	NULL_Q_BIO		= 0,
	NULL_Q_RQ		= 1,
	NULL_Q_MQ		= 2,
};

Shaohua Li's avatar
Shaohua Li committed
158 159
static int g_submit_queues = 1;
module_param_named(submit_queues, g_submit_queues, int, S_IRUGO);
160 161
MODULE_PARM_DESC(submit_queues, "Number of submission queues");

Shaohua Li's avatar
Shaohua Li committed
162 163
static int g_home_node = NUMA_NO_NODE;
module_param_named(home_node, g_home_node, int, S_IRUGO);
164 165
MODULE_PARM_DESC(home_node, "Home node for the device");

Shaohua Li's avatar
Shaohua Li committed
166
static int g_queue_mode = NULL_Q_MQ;
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

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)
{
Shaohua Li's avatar
Shaohua Li committed
185
	return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
186 187
}

188
static const struct kernel_param_ops null_queue_mode_param_ops = {
189 190 191 192
	.set	= null_set_queue_mode,
	.get	= param_get_int,
};

Shaohua Li's avatar
Shaohua Li committed
193
device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, S_IRUGO);
194
MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
195

Shaohua Li's avatar
Shaohua Li committed
196 197
static int g_gb = 250;
module_param_named(gb, g_gb, int, S_IRUGO);
198 199
MODULE_PARM_DESC(gb, "Size in GB");

Shaohua Li's avatar
Shaohua Li committed
200 201
static int g_bs = 512;
module_param_named(bs, g_bs, int, S_IRUGO);
202 203
MODULE_PARM_DESC(bs, "Block size (in bytes)");

204
static int nr_devices = 1;
205 206 207
module_param(nr_devices, int, S_IRUGO);
MODULE_PARM_DESC(nr_devices, "Number of devices to register");

Shaohua Li's avatar
Shaohua Li committed
208 209
static bool g_use_lightnvm;
module_param_named(use_lightnvm, g_use_lightnvm, bool, S_IRUGO);
210 211
MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device");

Shaohua Li's avatar
Shaohua Li committed
212 213
static bool g_blocking;
module_param_named(blocking, g_blocking, bool, S_IRUGO);
Jens Axboe's avatar
Jens Axboe committed
214 215
MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");

216 217 218 219
static bool shared_tags;
module_param(shared_tags, bool, S_IRUGO);
MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");

Shaohua Li's avatar
Shaohua Li committed
220
static int g_irqmode = NULL_IRQ_SOFTIRQ;
221 222 223

static int null_set_irqmode(const char *str, const struct kernel_param *kp)
{
Shaohua Li's avatar
Shaohua Li committed
224
	return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
225 226 227
					NULL_IRQ_TIMER);
}

228
static const struct kernel_param_ops null_irqmode_param_ops = {
229 230 231 232
	.set	= null_set_irqmode,
	.get	= param_get_int,
};

Shaohua Li's avatar
Shaohua Li committed
233
device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, S_IRUGO);
234 235
MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");

Shaohua Li's avatar
Shaohua Li committed
236 237
static unsigned long g_completion_nsec = 10000;
module_param_named(completion_nsec, g_completion_nsec, ulong, S_IRUGO);
238 239
MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");

Shaohua Li's avatar
Shaohua Li committed
240 241
static int g_hw_queue_depth = 64;
module_param_named(hw_queue_depth, g_hw_queue_depth, int, S_IRUGO);
242 243
MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");

Shaohua Li's avatar
Shaohua Li committed
244 245
static bool g_use_per_node_hctx;
module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, S_IRUGO);
246
MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
247

Shaohua Li's avatar
Shaohua Li committed
248 249
static struct nullb_device *null_alloc_dev(void);
static void null_free_dev(struct nullb_device *dev);
250 251
static void null_del_dev(struct nullb *nullb);
static int null_add_dev(struct nullb_device *dev);
Shaohua Li's avatar
Shaohua Li committed
252
static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
Shaohua Li's avatar
Shaohua Li committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

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);
	if (result)
		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);
	if (result)
		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);
	if (result)
		return result;

	*val = tmp;
	return count;
}

/* The following macro should only be used with TYPE = {uint, ulong, bool}. */
#define NULLB_DEVICE_ATTR(NAME, TYPE)						\
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)					\
{										\
	if (test_bit(NULLB_DEV_FL_CONFIGURED, &to_nullb_device(item)->flags))	\
		return -EBUSY;							\
	return nullb_device_##TYPE##_attr_store(				\
			&to_nullb_device(item)->NAME, page, count);		\
}										\
CONFIGFS_ATTR(nullb_device_, NAME);

NULLB_DEVICE_ATTR(size, ulong);
NULLB_DEVICE_ATTR(completion_nsec, ulong);
NULLB_DEVICE_ATTR(submit_queues, uint);
NULLB_DEVICE_ATTR(home_node, uint);
NULLB_DEVICE_ATTR(queue_mode, uint);
NULLB_DEVICE_ATTR(blocksize, uint);
NULLB_DEVICE_ATTR(irqmode, uint);
NULLB_DEVICE_ATTR(hw_queue_depth, uint);
344
NULLB_DEVICE_ATTR(index, uint);
Shaohua Li's avatar
Shaohua Li committed
345 346 347
NULLB_DEVICE_ATTR(use_lightnvm, bool);
NULLB_DEVICE_ATTR(blocking, bool);
NULLB_DEVICE_ATTR(use_per_node_hctx, bool);
348
NULLB_DEVICE_ATTR(memory_backed, bool);
Shaohua Li's avatar
Shaohua Li committed
349
NULLB_DEVICE_ATTR(discard, bool);
Shaohua Li's avatar
Shaohua Li committed
350
NULLB_DEVICE_ATTR(mbps, uint);
Shaohua Li's avatar
Shaohua Li committed
351
NULLB_DEVICE_ATTR(cache_size, ulong);
Shaohua Li's avatar
Shaohua Li committed
352

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
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;
379
	} else if (dev->power && !newp) {
380 381 382 383 384 385 386 387 388 389 390 391
		mutex_lock(&lock);
		dev->power = newp;
		null_del_dev(dev->nullb);
		mutex_unlock(&lock);
		clear_bit(NULLB_DEV_FL_UP, &dev->flags);
	}

	return count;
}

CONFIGFS_ATTR(nullb_device_, power);

Shaohua Li's avatar
Shaohua Li committed
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 433 434 435 436 437 438 439 440 441 442 443 444
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);

Shaohua Li's avatar
Shaohua Li committed
445 446 447 448 449 450 451 452 453
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,
454
	&nullb_device_attr_index,
Shaohua Li's avatar
Shaohua Li committed
455 456 457
	&nullb_device_attr_use_lightnvm,
	&nullb_device_attr_blocking,
	&nullb_device_attr_use_per_node_hctx,
458
	&nullb_device_attr_power,
459
	&nullb_device_attr_memory_backed,
Shaohua Li's avatar
Shaohua Li committed
460
	&nullb_device_attr_discard,
Shaohua Li's avatar
Shaohua Li committed
461
	&nullb_device_attr_mbps,
Shaohua Li's avatar
Shaohua Li committed
462
	&nullb_device_attr_cache_size,
Shaohua Li's avatar
Shaohua Li committed
463
	&nullb_device_attr_badblocks,
Shaohua Li's avatar
Shaohua Li committed
464 465 466 467 468
	NULL,
};

static void nullb_device_release(struct config_item *item)
{
469 470
	struct nullb_device *dev = to_nullb_device(item);

Shaohua Li's avatar
Shaohua Li committed
471
	null_free_device_storage(dev, false);
472
	null_free_dev(dev);
Shaohua Li's avatar
Shaohua Li committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
}

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

static struct config_item_type nullb_device_type = {
	.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)
{
502 503 504 505 506 507 508 509 510
	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);
	}

Shaohua Li's avatar
Shaohua Li committed
511 512 513 514 515
	config_item_put(item);
}

static ssize_t memb_group_features_show(struct config_item *item, char *page)
{
Shaohua Li's avatar
Shaohua Li committed
516
	return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache,badblocks\n");
Shaohua Li's avatar
Shaohua Li committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
}

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,
};

static struct config_item_type nullb_group_type = {
	.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,
		},
	},
};

Shaohua Li's avatar
Shaohua Li committed
546 547 548 549 550
static inline int null_cache_active(struct nullb *nullb)
{
	return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
}

Shaohua Li's avatar
Shaohua Li committed
551 552 553 554 555 556 557
static struct nullb_device *null_alloc_dev(void)
{
	struct nullb_device *dev;

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return NULL;
558
	INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
Shaohua Li's avatar
Shaohua Li committed
559
	INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
Shaohua Li's avatar
Shaohua Li committed
560 561 562 563 564
	if (badblocks_init(&dev->badblocks, 0)) {
		kfree(dev);
		return NULL;
	}

Shaohua Li's avatar
Shaohua Li committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	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->use_lightnvm = g_use_lightnvm;
	dev->blocking = g_blocking;
	dev->use_per_node_hctx = g_use_per_node_hctx;
	return dev;
}

static void null_free_dev(struct nullb_device *dev)
{
581 582 583 584
	if (!dev)
		return;

	badblocks_exit(&dev->badblocks);
Shaohua Li's avatar
Shaohua Li committed
585 586 587
	kfree(dev);
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
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);
}

614 615
static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);

616 617 618 619 620 621 622 623 624 625
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;
Shaohua Li's avatar
Shaohua Li committed
626
		if (nq->dev->irqmode == NULL_IRQ_TIMER) {
627 628 629 630
			hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
				     HRTIMER_MODE_REL);
			cmd->timer.function = null_cmd_timer_expired;
		}
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
		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)
{
661
	struct request_queue *q = NULL;
Shaohua Li's avatar
Shaohua Li committed
662
	int queue_mode = cmd->nq->dev->queue_mode;
663

664 665 666
	if (cmd->rq)
		q = cmd->rq->q;

667 668
	switch (queue_mode)  {
	case NULL_Q_MQ:
669
		blk_mq_end_request(cmd->rq, cmd->error);
670 671 672
		return;
	case NULL_Q_RQ:
		INIT_LIST_HEAD(&cmd->rq->queuelist);
673
		blk_end_request_all(cmd->rq, cmd->error);
674 675
		break;
	case NULL_Q_BIO:
676
		cmd->bio->bi_status = cmd->error;
677
		bio_endio(cmd->bio);
678
		break;
679
	}
680

681 682
	free_cmd(cmd);

683
	/* Restart queue if needed, as we are freeing a tag */
684
	if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
685 686 687
		unsigned long flags;

		spin_lock_irqsave(q->queue_lock, flags);
688
		blk_start_queue_async(q);
689
		spin_unlock_irqrestore(q->queue_lock, flags);
690
	}
691 692 693 694 695
}

static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
{
	end_cmd(container_of(timer, struct nullb_cmd, timer));
696 697 698 699 700 701

	return HRTIMER_NORESTART;
}

static void null_cmd_end_timer(struct nullb_cmd *cmd)
{
Shaohua Li's avatar
Shaohua Li committed
702
	ktime_t kt = cmd->nq->dev->completion_nsec;
703

704
	hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
705 706 707 708
}

static void null_softirq_done_fn(struct request *rq)
{
Shaohua Li's avatar
Shaohua Li committed
709 710 711
	struct nullb *nullb = rq->q->queuedata;

	if (nullb->dev->queue_mode == NULL_Q_MQ)
712 713 714
		end_cmd(blk_mq_rq_to_pdu(rq));
	else
		end_cmd(rq->special);
715 716
}

717 718 719 720 721 722 723 724 725 726 727 728
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;

729
	memset(t_page->bitmap, 0, sizeof(t_page->bitmap));
730 731 732 733 734 735 736 737 738
	return t_page;
out_freepage:
	kfree(t_page);
out:
	return NULL;
}

static void null_free_page(struct nullb_page *t_page)
{
739 740
	__set_bit(NULLB_PAGE_FREE, t_page->bitmap);
	if (test_bit(NULLB_PAGE_LOCK, t_page->bitmap))
Shaohua Li's avatar
Shaohua Li committed
741
		return;
742 743 744 745
	__free_page(t_page->page);
	kfree(t_page);
}

746 747 748 749 750 751 752
static bool null_page_empty(struct nullb_page *page)
{
	int size = MAP_SZ - 2;

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

Shaohua Li's avatar
Shaohua Li committed
753 754
static void null_free_sector(struct nullb *nullb, sector_t sector,
	bool is_cache)
755 756 757 758 759 760
{
	unsigned int sector_bit;
	u64 idx;
	struct nullb_page *t_page, *ret;
	struct radix_tree_root *root;

Shaohua Li's avatar
Shaohua Li committed
761
	root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
762 763 764 765 766
	idx = sector >> PAGE_SECTORS_SHIFT;
	sector_bit = (sector & SECTOR_MASK);

	t_page = radix_tree_lookup(root, idx);
	if (t_page) {
767
		__clear_bit(sector_bit, t_page->bitmap);
768

769
		if (null_page_empty(t_page)) {
770 771 772
			ret = radix_tree_delete_item(root, idx, t_page);
			WARN_ON(ret != t_page);
			null_free_page(ret);
Shaohua Li's avatar
Shaohua Li committed
773 774
			if (is_cache)
				nullb->dev->curr_cache -= PAGE_SIZE;
775 776 777 778 779
		}
	}
}

static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
Shaohua Li's avatar
Shaohua Li committed
780
	struct nullb_page *t_page, bool is_cache)
781 782 783
{
	struct radix_tree_root *root;

Shaohua Li's avatar
Shaohua Li committed
784
	root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
785 786 787 788 789

	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);
Shaohua Li's avatar
Shaohua Li committed
790 791
	} else if (is_cache)
		nullb->dev->curr_cache += PAGE_SIZE;
792 793 794 795

	return t_page;
}

Shaohua Li's avatar
Shaohua Li committed
796
static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
797 798 799 800 801 802
{
	unsigned long pos = 0;
	int nr_pages;
	struct nullb_page *ret, *t_pages[FREE_BATCH];
	struct radix_tree_root *root;

Shaohua Li's avatar
Shaohua Li committed
803
	root = is_cache ? &dev->cache : &dev->data;
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

	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);
Shaohua Li's avatar
Shaohua Li committed
820 821 822

	if (is_cache)
		dev->curr_cache = 0;
823 824
}

Shaohua Li's avatar
Shaohua Li committed
825 826
static struct nullb_page *__null_lookup_page(struct nullb *nullb,
	sector_t sector, bool for_write, bool is_cache)
827 828 829 830
{
	unsigned int sector_bit;
	u64 idx;
	struct nullb_page *t_page;
Shaohua Li's avatar
Shaohua Li committed
831
	struct radix_tree_root *root;
832 833 834 835

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

Shaohua Li's avatar
Shaohua Li committed
836 837
	root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
	t_page = radix_tree_lookup(root, idx);
838 839
	WARN_ON(t_page && t_page->page->index != idx);

840
	if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
841 842 843 844 845
		return t_page;

	return NULL;
}

Shaohua Li's avatar
Shaohua Li committed
846 847 848 849 850 851 852 853 854 855 856 857
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);
}

858
static struct nullb_page *null_insert_page(struct nullb *nullb,
Shaohua Li's avatar
Shaohua Li committed
859
	sector_t sector, bool ignore_cache)
860 861 862 863
{
	u64 idx;
	struct nullb_page *t_page;

Shaohua Li's avatar
Shaohua Li committed
864
	t_page = null_lookup_page(nullb, sector, true, ignore_cache);
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
	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;
Shaohua Li's avatar
Shaohua Li committed
880
	t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
881 882 883 884 885 886 887
	radix_tree_preload_end();

	return t_page;
out_freepage:
	null_free_page(t_page);
out_lock:
	spin_lock_irq(&nullb->lock);
Shaohua Li's avatar
Shaohua Li committed
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
	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);

903 904
	__clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
	if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
Shaohua Li's avatar
Shaohua Li committed
905
		null_free_page(c_page);
906
		if (t_page && null_page_empty(t_page)) {
Shaohua Li's avatar
Shaohua Li committed
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
			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)) {
922
		if (test_bit(i, c_page->bitmap)) {
Shaohua Li's avatar
Shaohua Li committed
923 924 925
			offset = (i << SECTOR_SHIFT);
			memcpy(dst + offset, src + offset,
				nullb->dev->blocksize);
926
			__set_bit(i, t_page->bitmap);
Shaohua Li's avatar
Shaohua Li committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940
		}
	}

	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)
941
{
Shaohua Li's avatar
Shaohua Li committed
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
	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
		 */
963
		if (test_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap))
Shaohua Li's avatar
Shaohua Li committed
964 965
			c_pages[i] = NULL;
		else
966
			__set_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap);
Shaohua Li's avatar
Shaohua Li committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
	}

	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;
991 992 993
}

static int copy_to_nullb(struct nullb *nullb, struct page *source,
Shaohua Li's avatar
Shaohua Li committed
994
	unsigned int off, sector_t sector, size_t n, bool is_fua)
995 996 997 998 999 1000 1001 1002 1003
{
	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);

Shaohua Li's avatar
Shaohua Li committed
1004 1005 1006
		if (null_cache_active(nullb) && !is_fua)
			null_make_cache_space(nullb, PAGE_SIZE);

1007
		offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
Shaohua Li's avatar
Shaohua Li committed
1008 1009
		t_page = null_insert_page(nullb, sector,
			!null_cache_active(nullb) || is_fua);
1010 1011 1012 1013 1014 1015 1016 1017 1018
		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);

1019
		__set_bit(sector & SECTOR_MASK, t_page->bitmap);
1020

Shaohua Li's avatar
Shaohua Li committed
1021 1022 1023
		if (is_fua)
			null_free_sector(nullb, sector, true);

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
		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;
Shaohua Li's avatar
Shaohua Li committed
1042 1043
		t_page = null_lookup_page(nullb, sector, false,
			!null_cache_active(nullb));
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

		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;
}

Shaohua Li's avatar
Shaohua Li committed
1062 1063 1064 1065 1066 1067 1068
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);
Shaohua Li's avatar
Shaohua Li committed
1069 1070 1071
		null_free_sector(nullb, sector, false);
		if (null_cache_active(nullb))
			null_free_sector(nullb, sector, true);
Shaohua Li's avatar
Shaohua Li committed
1072 1073 1074 1075 1076 1077
		sector += temp >> SECTOR_SHIFT;
		n -= temp;
	}
	spin_unlock_irq(&nullb->lock);
}

Shaohua Li's avatar
Shaohua Li committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
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;
}

1098
static int null_transfer(struct nullb *nullb, struct page *page,
Shaohua Li's avatar
Shaohua Li committed
1099 1100
	unsigned int len, unsigned int off, bool is_write, sector_t sector,
	bool is_fua)
1101
{
1102 1103 1104 1105 1106 1107 1108
	int err = 0;

	if (!is_write) {
		err = copy_from_nullb(nullb, page, off, sector, len);
		flush_dcache_page(page);
	} else {
		flush_dcache_page(page);
Shaohua Li's avatar
Shaohua Li committed
1109
		err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	}

	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);

Shaohua Li's avatar
Shaohua Li committed
1127 1128 1129 1130 1131
	if (req_op(rq) == REQ_OP_DISCARD) {
		null_handle_discard(nullb, sector, blk_rq_bytes(rq));
		return 0;
	}

1132 1133 1134 1135
	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,
Shaohua Li's avatar
Shaohua Li committed
1136 1137
				     op_is_write(req_op(rq)), sector,
				     req_op(rq) & REQ_FUA);
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
		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;

Shaohua Li's avatar
Shaohua Li committed
1161 1162 1163 1164 1165 1166
	if (bio_op(bio) == REQ_OP_DISCARD) {
		null_handle_discard(nullb, sector,
			bio_sectors(bio) << SECTOR_SHIFT);
		return 0;
	}

1167 1168 1169 1170
	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,
Shaohua Li's avatar
Shaohua Li committed
1171 1172
				     op_is_write(bio_op(bio)), sector,
				     bio_op(bio) & REQ_FUA);
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
		if (err) {
			spin_unlock_irq(&nullb->lock);
			return err;
		}
		sector += len >> SECTOR_SHIFT;
	}
	spin_unlock_irq(&nullb->lock);
	return 0;
}

Shaohua Li's avatar
Shaohua Li committed
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
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);
	else {
		spin_lock_irq(q->queue_lock);
		blk_stop_queue(q);
		spin_unlock_irq(q->queue_lock);
	}
}

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

	if (nullb->dev->queue_mode == NULL_Q_MQ)
		blk_mq_start_stopped_hw_queues(q, true);
	else {
		spin_lock_irqsave(q->queue_lock, flags);
		blk_start_queue_async(q);
		spin_unlock_irqrestore(q->queue_lock, flags);
	}
}

1210 1211 1212
static blk_status_t null_handle_cmd(struct nullb_cmd *cmd)
{
	struct nullb_device *dev = cmd->nq->dev;
Shaohua Li's avatar
Shaohua Li committed
1213
	struct nullb *nullb = dev->nullb;
1214 1215
	int err = 0;

Shaohua Li's avatar
Shaohua Li committed
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) {
		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);
			if (dev->queue_mode == NULL_Q_RQ) {
				struct request_queue *q = nullb->q;

				spin_lock_irq(q->queue_lock);
				rq->rq_flags |= RQF_DONTPREP;
				blk_requeue_request(q, rq);
				spin_unlock_irq(q->queue_lock);
				return BLK_STS_OK;
			} else
				/* requeue request */
				return BLK_STS_RESOURCE;
		}
	}

Shaohua Li's avatar
Shaohua Li committed
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	if (nullb->dev->badblocks.shift != -1) {
		int bad_sectors;
		sector_t sector, size, first_bad;
		bool is_flush = true;

		if (dev->queue_mode == NULL_Q_BIO &&
				bio_op(cmd->bio) != REQ_OP_FLUSH) {
			is_flush = false;
			sector = cmd->bio->bi_iter.bi_sector;
			size = bio_sectors(cmd->bio);
		}
		if (dev->queue_mode != NULL_Q_BIO &&
				req_op(cmd->rq) != REQ_OP_FLUSH) {
			is_flush = false;
			sector = blk_rq_pos(cmd->rq);
			size = blk_rq_sectors(cmd->rq);
		}
		if (!is_flush && badblocks_check(&nullb->dev->badblocks, sector,
				size, &first_bad, &bad_sectors)) {
			cmd->error = BLK_STS_IOERR;
			goto out;
		}
	}

1266
	if (dev->memory_backed) {
Shaohua Li's avatar
Shaohua Li committed
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
		if (dev->queue_mode == NULL_Q_BIO) {
			if (bio_op(cmd->bio) == REQ_OP_FLUSH)
				err = null_handle_flush(nullb);
			else
				err = null_handle_bio(cmd);
		} else {
			if (req_op(cmd->rq) == REQ_OP_FLUSH)
				err = null_handle_flush(nullb);
			else
				err = null_handle_rq(cmd);
		}
1278 1279
	}
	cmd->error = errno_to_blk_status(err);
Shaohua Li's avatar
Shaohua Li committed
1280
out:
1281
	/* Complete IO by inline, softirq or timer */
1282
	switch (dev->irqmode) {
1283
	case NULL_IRQ_SOFTIRQ:
1284
		switch (dev->queue_mode)  {
1285
		case NULL_Q_MQ:
1286
			blk_mq_complete_request(cmd->rq);
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
			break;
		case NULL_Q_RQ:
			blk_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:
1300 1301 1302 1303 1304 1305
		end_cmd(cmd);
		break;
	case NULL_IRQ_TIMER:
		null_cmd_end_timer(cmd);
		break;
	}
1306
	return BLK_STS_OK;
1307 1308
}

Shaohua Li's avatar
Shaohua Li committed
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
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);
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
}

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];
}

1346
static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
1347 1348 1349 1350 1351 1352 1353 1354 1355
{
	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;

	null_handle_cmd(cmd);
1356
	return BLK_QC_T_NONE;
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
}

static int null_rq_prep_fn(struct request_queue *q, struct request *req)
{
	struct nullb *nullb = q->queuedata;
	struct nullb_queue *nq = nullb_to_queue(nullb);
	struct nullb_cmd *cmd;

	cmd = alloc_cmd(nq, 0);
	if (cmd) {
		cmd->rq = req;
		req->special = cmd;
		return BLKPREP_OK;
	}
1371
	blk_stop_queue(q);
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

	return BLKPREP_DEFER;
}

static void null_request_fn(struct request_queue *q)
{
	struct request *rq;

	while ((rq = blk_fetch_request(q)) != NULL) {
		struct nullb_cmd *cmd = rq->special;

		spin_unlock_irq(q->queue_lock);
		null_handle_cmd(cmd);
		spin_lock_irq(q->queue_lock);
	}
}

1389
static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
1390
			 const struct blk_mq_queue_data *bd)
1391
{
1392
	struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Shaohua Li's avatar
Shaohua Li committed
1393
	struct nullb_queue *nq = hctx->driver_data;
1394

Jens Axboe's avatar
Jens Axboe committed
1395 1396
	might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);

Shaohua Li's avatar
Shaohua Li committed
1397
	if (nq->dev->irqmode == NULL_IRQ_TIMER) {
1398 1399 1400
		hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
		cmd->timer.function = null_cmd_timer_expired;
	}
1401
	cmd->rq = bd->rq;
Shaohua Li's avatar
Shaohua Li committed
1402
	cmd->nq = nq;
1403

1404
	blk_mq_start_request(bd->rq);
1405

1406
	return null_handle_cmd(cmd);
1407 1408
}

1409
static const struct blk_mq_ops null_mq_ops = {
1410
	.queue_rq       = null_queue_rq,
1411
	.complete	= null_softirq_done_fn,
1412 1413
};

1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
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);
}

1430 1431
#ifdef CONFIG_NVM

1432
static void null_lnvm_end_io(struct request *rq, blk_status_t status)
1433 1434 1435
{
	struct nvm_rq *rqd = rq->end_io_data;

1436 1437
	/* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */
	rqd->error = status ? -EIO : 0;
1438
	nvm_end_io(rqd);
1439 1440 1441 1442

	blk_put_request(rq);
}

1443
static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
1444
{
1445
	struct request_queue *q = dev->q;
1446 1447 1448
	struct request *rq;
	struct bio *bio = rqd->bio;

1449 1450
	rq = blk_mq_alloc_request(q,
		op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
1451 1452 1453
	if (IS_ERR(rq))
		return -ENOMEM;

1454
	blk_init_request_from_bio(rq, bio);
1455 1456 1457 1458 1459 1460 1461 1462

	rq->end_io_data = rqd;

	blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io);

	return 0;
}

1463
static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
1464
{
Shaohua Li's avatar
Shaohua Li committed
1465 1466
	struct nullb *nullb = dev->q->queuedata;
	sector_t size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
1467
	sector_t blksize;
1468 1469 1470 1471
	struct nvm_id_group *grp;

	id->ver_id = 0x1;
	id->vmnt = 0;
1472
	id->cap = 0x2;
1473
	id->dom = 0x1;
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486

	id->ppaf.blk_offset = 0;
	id->ppaf.blk_len = 16;
	id->ppaf.pg_offset = 16;
	id->ppaf.pg_len = 16;
	id->ppaf.sect_offset = 32;
	id->ppaf.sect_len = 8;
	id->ppaf.pln_offset = 40;
	id->ppaf.pln_len = 8;
	id->ppaf.lun_offset = 48;
	id->ppaf.lun_len = 8;
	id->ppaf.ch_offset = 56;
	id->ppaf.ch_len = 8;
1487

Shaohua Li's avatar
Shaohua Li committed
1488
	sector_div(size, nullb->dev->blocksize); /* convert size to pages */
1489
	size >>= 8; /* concert size to pgs pr blk */
1490
	grp = &id->grp;
1491
	grp->mtype = 0;
1492
	grp->fmtype = 0;
1493 1494
	grp->num_ch = 1;
	grp->num_pg = 256;
1495
	blksize = size;
1496
	size >>= 16;
1497
	grp->num_lun = size + 1;
1498
	sector_div(blksize, grp->num_lun);
1499 1500 1501
	grp->num_blk = blksize;
	grp->num_pln = 1;

Shaohua Li's avatar
Shaohua Li committed
1502 1503
	grp->fpg_sz = nullb->dev->blocksize;
	grp->csecs = nullb->dev->blocksize;
1504 1505 1506 1507 1508 1509 1510
	grp->trdt = 25000;
	grp->trdm = 25000;
	grp->tprt = 500000;
	grp->tprm = 500000;
	grp->tbet = 1500000;
	grp->tbem = 1500000;
	grp->mpos = 0x010101; /* single plane rwe */
Shaohua Li's avatar
Shaohua Li committed
1511
	grp->cpar = nullb->dev->hw_queue_depth;
1512 1513 1514 1515

	return 0;
}

1516
static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
1517 1518 1519
{
	mempool_t *virtmem_pool;

1520
	virtmem_pool = mempool_create_slab_pool(64, ppa_cache);