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