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