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