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