blob: 447d635c79a24eb1036a2b8037fdc4df7f3290b0 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Shaohua Li3bf2bd22017-08-14 15:04:53 -07002/*
3 * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
4 * Shaohua Li <shli@fb.com>
5 */
Jens Axboef2298c02013-10-25 11:52:25 +01006#include <linux/module.h>
Matias Bjørlingfc1bc352013-12-21 00:11:01 +01007
Jens Axboef2298c02013-10-25 11:52:25 +01008#include <linux/moduleparam.h>
9#include <linux/sched.h>
10#include <linux/fs.h>
Jens Axboef2298c02013-10-25 11:52:25 +010011#include <linux/init.h>
Matias Bjørling6dad38d2018-07-06 19:38:38 +020012#include "null_blk.h"
Jens Axboef2298c02013-10-25 11:52:25 +010013
Shaohua Li5bcd0e02017-08-14 15:04:56 -070014#define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
15#define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
Shaohua Li5bcd0e02017-08-14 15:04:56 -070016#define SECTOR_MASK (PAGE_SECTORS - 1)
17
18#define FREE_BATCH 16
19
Shaohua Lieff2c4f2017-08-14 15:04:58 -070020#define TICKS_PER_SEC 50ULL
21#define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC)
22
Arnd Bergmann33f782c2018-01-11 11:31:25 +010023#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
Jens Axboe93b57042018-01-10 09:06:23 -070024static DECLARE_FAULT_ATTR(null_timeout_attr);
Jens Axboe24941b92018-02-28 09:18:57 -070025static DECLARE_FAULT_ATTR(null_requeue_attr);
Arnd Bergmann33f782c2018-01-11 11:31:25 +010026#endif
Jens Axboe93b57042018-01-10 09:06:23 -070027
Shaohua Lieff2c4f2017-08-14 15:04:58 -070028static inline u64 mb_per_tick(int mbps)
29{
30 return (1 << 20) / TICKS_PER_SEC * ((u64) mbps);
31}
Jens Axboef2298c02013-10-25 11:52:25 +010032
Shaohua Li3bf2bd22017-08-14 15:04:53 -070033/*
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 Lieff2c4f2017-08-14 15:04:58 -070038 * THROTTLED: Device is being throttled.
Shaohua Lideb78b42017-08-14 15:04:59 -070039 * CACHE: Device is using a write-back cache.
Shaohua Li3bf2bd22017-08-14 15:04:53 -070040 */
41enum nullb_device_flags {
42 NULLB_DEV_FL_CONFIGURED = 0,
43 NULLB_DEV_FL_UP = 1,
Shaohua Lieff2c4f2017-08-14 15:04:58 -070044 NULLB_DEV_FL_THROTTLED = 2,
Shaohua Lideb78b42017-08-14 15:04:59 -070045 NULLB_DEV_FL_CACHE = 3,
Shaohua Li3bf2bd22017-08-14 15:04:53 -070046};
47
Ming Lei66231ad2018-03-06 12:07:13 +080048#define MAP_SZ ((PAGE_SIZE >> SECTOR_SHIFT) + 2)
Shaohua Li5bcd0e02017-08-14 15:04:56 -070049/*
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 Lideb78b42017-08-14 15:04:59 -070056 * 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 Li5bcd0e02017-08-14 15:04:56 -070060 */
61struct nullb_page {
62 struct page *page;
Ming Lei66231ad2018-03-06 12:07:13 +080063 DECLARE_BITMAP(bitmap, MAP_SZ);
Shaohua Li5bcd0e02017-08-14 15:04:56 -070064};
Ming Lei66231ad2018-03-06 12:07:13 +080065#define NULLB_PAGE_LOCK (MAP_SZ - 1)
66#define NULLB_PAGE_FREE (MAP_SZ - 2)
Shaohua Li5bcd0e02017-08-14 15:04:56 -070067
Jens Axboef2298c02013-10-25 11:52:25 +010068static LIST_HEAD(nullb_list);
69static struct mutex lock;
70static int null_major;
Shaohua Li94bc02e2017-08-14 15:04:55 -070071static DEFINE_IDA(nullb_indexes);
Jens Axboe82f402f2017-06-20 14:22:01 -060072static struct blk_mq_tag_set tag_set;
Jens Axboef2298c02013-10-25 11:52:25 +010073
Jens Axboef2298c02013-10-25 11:52:25 +010074enum {
75 NULL_IRQ_NONE = 0,
76 NULL_IRQ_SOFTIRQ = 1,
77 NULL_IRQ_TIMER = 2,
Christoph Hellwigce2c3502014-02-10 03:24:40 -080078};
Jens Axboef2298c02013-10-25 11:52:25 +010079
Christoph Hellwigce2c3502014-02-10 03:24:40 -080080enum {
Jens Axboef2298c02013-10-25 11:52:25 +010081 NULL_Q_BIO = 0,
82 NULL_Q_RQ = 1,
83 NULL_Q_MQ = 2,
84};
85
weiping zhangb3cffc32017-09-30 09:49:21 +080086static int g_no_sched;
Joe Perches5657a812018-05-24 13:38:59 -060087module_param_named(no_sched, g_no_sched, int, 0444);
weiping zhangb3cffc32017-09-30 09:49:21 +080088MODULE_PARM_DESC(no_sched, "No io scheduler");
89
Shaohua Li2984c862017-08-14 15:04:52 -070090static int g_submit_queues = 1;
Joe Perches5657a812018-05-24 13:38:59 -060091module_param_named(submit_queues, g_submit_queues, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +010092MODULE_PARM_DESC(submit_queues, "Number of submission queues");
93
Shaohua Li2984c862017-08-14 15:04:52 -070094static int g_home_node = NUMA_NO_NODE;
Joe Perches5657a812018-05-24 13:38:59 -060095module_param_named(home_node, g_home_node, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +010096MODULE_PARM_DESC(home_node, "Home node for the device");
97
Arnd Bergmann33f782c2018-01-11 11:31:25 +010098#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
Jens Axboe93b57042018-01-10 09:06:23 -070099static char g_timeout_str[80];
Joe Perches5657a812018-05-24 13:38:59 -0600100module_param_string(timeout, g_timeout_str, sizeof(g_timeout_str), 0444);
Jens Axboe24941b92018-02-28 09:18:57 -0700101
102static char g_requeue_str[80];
Joe Perches5657a812018-05-24 13:38:59 -0600103module_param_string(requeue, g_requeue_str, sizeof(g_requeue_str), 0444);
Arnd Bergmann33f782c2018-01-11 11:31:25 +0100104#endif
Jens Axboe93b57042018-01-10 09:06:23 -0700105
Shaohua Li2984c862017-08-14 15:04:52 -0700106static int g_queue_mode = NULL_Q_MQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700107
108static 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
123static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
124{
Shaohua Li2984c862017-08-14 15:04:52 -0700125 return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
Matias Bjorling709c8662014-11-26 14:45:48 -0700126}
127
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930128static const struct kernel_param_ops null_queue_mode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700129 .set = null_set_queue_mode,
130 .get = param_get_int,
131};
132
Joe Perches5657a812018-05-24 13:38:59 -0600133device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, 0444);
Mike Snitzer54ae81c2014-06-11 17:13:50 -0400134MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
Jens Axboef2298c02013-10-25 11:52:25 +0100135
Shaohua Li2984c862017-08-14 15:04:52 -0700136static int g_gb = 250;
Joe Perches5657a812018-05-24 13:38:59 -0600137module_param_named(gb, g_gb, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100138MODULE_PARM_DESC(gb, "Size in GB");
139
Shaohua Li2984c862017-08-14 15:04:52 -0700140static int g_bs = 512;
Joe Perches5657a812018-05-24 13:38:59 -0600141module_param_named(bs, g_bs, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100142MODULE_PARM_DESC(bs, "Block size (in bytes)");
143
Jens Axboe82f402f2017-06-20 14:22:01 -0600144static int nr_devices = 1;
Joe Perches5657a812018-05-24 13:38:59 -0600145module_param(nr_devices, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100146MODULE_PARM_DESC(nr_devices, "Number of devices to register");
147
Shaohua Li2984c862017-08-14 15:04:52 -0700148static bool g_blocking;
Joe Perches5657a812018-05-24 13:38:59 -0600149module_param_named(blocking, g_blocking, bool, 0444);
Jens Axboedb5bcf82017-03-30 13:44:26 -0600150MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
151
Jens Axboe82f402f2017-06-20 14:22:01 -0600152static bool shared_tags;
Joe Perches5657a812018-05-24 13:38:59 -0600153module_param(shared_tags, bool, 0444);
Jens Axboe82f402f2017-06-20 14:22:01 -0600154MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");
155
Shaohua Li2984c862017-08-14 15:04:52 -0700156static int g_irqmode = NULL_IRQ_SOFTIRQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700157
158static int null_set_irqmode(const char *str, const struct kernel_param *kp)
159{
Shaohua Li2984c862017-08-14 15:04:52 -0700160 return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
Matias Bjorling709c8662014-11-26 14:45:48 -0700161 NULL_IRQ_TIMER);
162}
163
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930164static const struct kernel_param_ops null_irqmode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700165 .set = null_set_irqmode,
166 .get = param_get_int,
167};
168
Joe Perches5657a812018-05-24 13:38:59 -0600169device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100170MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
171
Shaohua Li2984c862017-08-14 15:04:52 -0700172static unsigned long g_completion_nsec = 10000;
Joe Perches5657a812018-05-24 13:38:59 -0600173module_param_named(completion_nsec, g_completion_nsec, ulong, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100174MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
175
Shaohua Li2984c862017-08-14 15:04:52 -0700176static int g_hw_queue_depth = 64;
Joe Perches5657a812018-05-24 13:38:59 -0600177module_param_named(hw_queue_depth, g_hw_queue_depth, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100178MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
179
Shaohua Li2984c862017-08-14 15:04:52 -0700180static bool g_use_per_node_hctx;
Joe Perches5657a812018-05-24 13:38:59 -0600181module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, 0444);
Matias Bjørling20005242013-12-21 00:11:00 +0100182MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
Jens Axboef2298c02013-10-25 11:52:25 +0100183
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200184static bool g_zoned;
185module_param_named(zoned, g_zoned, bool, S_IRUGO);
186MODULE_PARM_DESC(zoned, "Make device as a host-managed zoned block device. Default: false");
187
188static unsigned long g_zone_size = 256;
189module_param_named(zone_size, g_zone_size, ulong, S_IRUGO);
190MODULE_PARM_DESC(zone_size, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");
191
Masato Suzukiea2c18e2018-10-30 16:14:05 +0900192static unsigned int g_zone_nr_conv;
193module_param_named(zone_nr_conv, g_zone_nr_conv, uint, 0444);
194MODULE_PARM_DESC(zone_nr_conv, "Number of conventional zones when block device is zoned. Default: 0");
195
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700196static struct nullb_device *null_alloc_dev(void);
197static void null_free_dev(struct nullb_device *dev);
Shaohua Licedcafa2017-08-14 15:04:54 -0700198static void null_del_dev(struct nullb *nullb);
199static int null_add_dev(struct nullb_device *dev);
Shaohua Lideb78b42017-08-14 15:04:59 -0700200static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700201
202static 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
207static 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
212static 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
218static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
219{
220 return snprintf(page, PAGE_SIZE, "%u\n", val);
221}
222
223static 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
237static 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
251static 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) \
267static ssize_t \
268nullb_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} \
273static ssize_t \
274nullb_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} \
282CONFIGFS_ATTR(nullb_device_, NAME);
283
284NULLB_DEVICE_ATTR(size, ulong);
285NULLB_DEVICE_ATTR(completion_nsec, ulong);
286NULLB_DEVICE_ATTR(submit_queues, uint);
287NULLB_DEVICE_ATTR(home_node, uint);
288NULLB_DEVICE_ATTR(queue_mode, uint);
289NULLB_DEVICE_ATTR(blocksize, uint);
290NULLB_DEVICE_ATTR(irqmode, uint);
291NULLB_DEVICE_ATTR(hw_queue_depth, uint);
Shaohua Licedcafa2017-08-14 15:04:54 -0700292NULLB_DEVICE_ATTR(index, uint);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700293NULLB_DEVICE_ATTR(blocking, bool);
294NULLB_DEVICE_ATTR(use_per_node_hctx, bool);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700295NULLB_DEVICE_ATTR(memory_backed, bool);
Shaohua Li306eb6b2017-08-14 15:04:57 -0700296NULLB_DEVICE_ATTR(discard, bool);
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700297NULLB_DEVICE_ATTR(mbps, uint);
Shaohua Lideb78b42017-08-14 15:04:59 -0700298NULLB_DEVICE_ATTR(cache_size, ulong);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200299NULLB_DEVICE_ATTR(zoned, bool);
300NULLB_DEVICE_ATTR(zone_size, ulong);
Masato Suzukiea2c18e2018-10-30 16:14:05 +0900301NULLB_DEVICE_ATTR(zone_nr_conv, uint);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700302
Shaohua Licedcafa2017-08-14 15:04:54 -0700303static 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
308static 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 Axboeb3c30512017-08-28 15:06:31 -0600329 } else if (dev->power && !newp) {
Shaohua Licedcafa2017-08-14 15:04:54 -0700330 mutex_lock(&lock);
331 dev->power = newp;
332 null_del_dev(dev->nullb);
333 mutex_unlock(&lock);
334 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
Liu Bo00a8cdb2018-07-06 03:07:13 +0800335 clear_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
Shaohua Licedcafa2017-08-14 15:04:54 -0700336 }
337
338 return count;
339}
340
341CONFIGFS_ATTR(nullb_device_, power);
342
Shaohua Li2f54a612017-08-14 15:05:00 -0700343static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page)
344{
345 struct nullb_device *t_dev = to_nullb_device(item);
346
347 return badblocks_show(&t_dev->badblocks, page, 0);
348}
349
350static ssize_t nullb_device_badblocks_store(struct config_item *item,
351 const char *page, size_t count)
352{
353 struct nullb_device *t_dev = to_nullb_device(item);
354 char *orig, *buf, *tmp;
355 u64 start, end;
356 int ret;
357
358 orig = kstrndup(page, count, GFP_KERNEL);
359 if (!orig)
360 return -ENOMEM;
361
362 buf = strstrip(orig);
363
364 ret = -EINVAL;
365 if (buf[0] != '+' && buf[0] != '-')
366 goto out;
367 tmp = strchr(&buf[1], '-');
368 if (!tmp)
369 goto out;
370 *tmp = '\0';
371 ret = kstrtoull(buf + 1, 0, &start);
372 if (ret)
373 goto out;
374 ret = kstrtoull(tmp + 1, 0, &end);
375 if (ret)
376 goto out;
377 ret = -EINVAL;
378 if (start > end)
379 goto out;
380 /* enable badblocks */
381 cmpxchg(&t_dev->badblocks.shift, -1, 0);
382 if (buf[0] == '+')
383 ret = badblocks_set(&t_dev->badblocks, start,
384 end - start + 1, 1);
385 else
386 ret = badblocks_clear(&t_dev->badblocks, start,
387 end - start + 1);
388 if (ret == 0)
389 ret = count;
390out:
391 kfree(orig);
392 return ret;
393}
394CONFIGFS_ATTR(nullb_device_, badblocks);
395
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700396static struct configfs_attribute *nullb_device_attrs[] = {
397 &nullb_device_attr_size,
398 &nullb_device_attr_completion_nsec,
399 &nullb_device_attr_submit_queues,
400 &nullb_device_attr_home_node,
401 &nullb_device_attr_queue_mode,
402 &nullb_device_attr_blocksize,
403 &nullb_device_attr_irqmode,
404 &nullb_device_attr_hw_queue_depth,
Shaohua Licedcafa2017-08-14 15:04:54 -0700405 &nullb_device_attr_index,
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700406 &nullb_device_attr_blocking,
407 &nullb_device_attr_use_per_node_hctx,
Shaohua Licedcafa2017-08-14 15:04:54 -0700408 &nullb_device_attr_power,
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700409 &nullb_device_attr_memory_backed,
Shaohua Li306eb6b2017-08-14 15:04:57 -0700410 &nullb_device_attr_discard,
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700411 &nullb_device_attr_mbps,
Shaohua Lideb78b42017-08-14 15:04:59 -0700412 &nullb_device_attr_cache_size,
Shaohua Li2f54a612017-08-14 15:05:00 -0700413 &nullb_device_attr_badblocks,
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200414 &nullb_device_attr_zoned,
415 &nullb_device_attr_zone_size,
Masato Suzukiea2c18e2018-10-30 16:14:05 +0900416 &nullb_device_attr_zone_nr_conv,
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700417 NULL,
418};
419
420static void nullb_device_release(struct config_item *item)
421{
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700422 struct nullb_device *dev = to_nullb_device(item);
423
Shaohua Lideb78b42017-08-14 15:04:59 -0700424 null_free_device_storage(dev, false);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700425 null_free_dev(dev);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700426}
427
428static struct configfs_item_operations nullb_device_ops = {
429 .release = nullb_device_release,
430};
431
Bhumika Goyale1919df2017-10-16 17:18:49 +0200432static const struct config_item_type nullb_device_type = {
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700433 .ct_item_ops = &nullb_device_ops,
434 .ct_attrs = nullb_device_attrs,
435 .ct_owner = THIS_MODULE,
436};
437
438static struct
439config_item *nullb_group_make_item(struct config_group *group, const char *name)
440{
441 struct nullb_device *dev;
442
443 dev = null_alloc_dev();
444 if (!dev)
445 return ERR_PTR(-ENOMEM);
446
447 config_item_init_type_name(&dev->item, name, &nullb_device_type);
448
449 return &dev->item;
450}
451
452static void
453nullb_group_drop_item(struct config_group *group, struct config_item *item)
454{
Shaohua Licedcafa2017-08-14 15:04:54 -0700455 struct nullb_device *dev = to_nullb_device(item);
456
457 if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
458 mutex_lock(&lock);
459 dev->power = false;
460 null_del_dev(dev->nullb);
461 mutex_unlock(&lock);
462 }
463
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700464 config_item_put(item);
465}
466
467static ssize_t memb_group_features_show(struct config_item *item, char *page)
468{
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200469 return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache,badblocks,zoned,zone_size\n");
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700470}
471
472CONFIGFS_ATTR_RO(memb_group_, features);
473
474static struct configfs_attribute *nullb_group_attrs[] = {
475 &memb_group_attr_features,
476 NULL,
477};
478
479static struct configfs_group_operations nullb_group_ops = {
480 .make_item = nullb_group_make_item,
481 .drop_item = nullb_group_drop_item,
482};
483
Bhumika Goyale1919df2017-10-16 17:18:49 +0200484static const struct config_item_type nullb_group_type = {
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700485 .ct_group_ops = &nullb_group_ops,
486 .ct_attrs = nullb_group_attrs,
487 .ct_owner = THIS_MODULE,
488};
489
490static struct configfs_subsystem nullb_subsys = {
491 .su_group = {
492 .cg_item = {
493 .ci_namebuf = "nullb",
494 .ci_type = &nullb_group_type,
495 },
496 },
497};
498
Shaohua Lideb78b42017-08-14 15:04:59 -0700499static inline int null_cache_active(struct nullb *nullb)
500{
501 return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
502}
503
Shaohua Li2984c862017-08-14 15:04:52 -0700504static struct nullb_device *null_alloc_dev(void)
505{
506 struct nullb_device *dev;
507
508 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
509 if (!dev)
510 return NULL;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700511 INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
Shaohua Lideb78b42017-08-14 15:04:59 -0700512 INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
Shaohua Li2f54a612017-08-14 15:05:00 -0700513 if (badblocks_init(&dev->badblocks, 0)) {
514 kfree(dev);
515 return NULL;
516 }
517
Shaohua Li2984c862017-08-14 15:04:52 -0700518 dev->size = g_gb * 1024;
519 dev->completion_nsec = g_completion_nsec;
520 dev->submit_queues = g_submit_queues;
521 dev->home_node = g_home_node;
522 dev->queue_mode = g_queue_mode;
523 dev->blocksize = g_bs;
524 dev->irqmode = g_irqmode;
525 dev->hw_queue_depth = g_hw_queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -0700526 dev->blocking = g_blocking;
527 dev->use_per_node_hctx = g_use_per_node_hctx;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200528 dev->zoned = g_zoned;
529 dev->zone_size = g_zone_size;
Masato Suzukiea2c18e2018-10-30 16:14:05 +0900530 dev->zone_nr_conv = g_zone_nr_conv;
Shaohua Li2984c862017-08-14 15:04:52 -0700531 return dev;
532}
533
534static void null_free_dev(struct nullb_device *dev)
535{
David Disseldorp1addb792017-11-08 17:29:44 +0100536 if (!dev)
537 return;
538
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200539 null_zone_exit(dev);
David Disseldorp1addb792017-11-08 17:29:44 +0100540 badblocks_exit(&dev->badblocks);
Shaohua Li2984c862017-08-14 15:04:52 -0700541 kfree(dev);
542}
543
Jens Axboef2298c02013-10-25 11:52:25 +0100544static void put_tag(struct nullb_queue *nq, unsigned int tag)
545{
546 clear_bit_unlock(tag, nq->tag_map);
547
548 if (waitqueue_active(&nq->wait))
549 wake_up(&nq->wait);
550}
551
552static unsigned int get_tag(struct nullb_queue *nq)
553{
554 unsigned int tag;
555
556 do {
557 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
558 if (tag >= nq->queue_depth)
559 return -1U;
560 } while (test_and_set_bit_lock(tag, nq->tag_map));
561
562 return tag;
563}
564
565static void free_cmd(struct nullb_cmd *cmd)
566{
567 put_tag(cmd->nq, cmd->tag);
568}
569
Paolo Valente3c395a92015-12-01 11:48:17 +0100570static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
571
Jens Axboef2298c02013-10-25 11:52:25 +0100572static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
573{
574 struct nullb_cmd *cmd;
575 unsigned int tag;
576
577 tag = get_tag(nq);
578 if (tag != -1U) {
579 cmd = &nq->cmds[tag];
580 cmd->tag = tag;
581 cmd->nq = nq;
Shaohua Li2984c862017-08-14 15:04:52 -0700582 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +0100583 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
584 HRTIMER_MODE_REL);
585 cmd->timer.function = null_cmd_timer_expired;
586 }
Jens Axboef2298c02013-10-25 11:52:25 +0100587 return cmd;
588 }
589
590 return NULL;
591}
592
593static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
594{
595 struct nullb_cmd *cmd;
596 DEFINE_WAIT(wait);
597
598 cmd = __alloc_cmd(nq);
599 if (cmd || !can_wait)
600 return cmd;
601
602 do {
603 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
604 cmd = __alloc_cmd(nq);
605 if (cmd)
606 break;
607
608 io_schedule();
609 } while (1);
610
611 finish_wait(&nq->wait, &wait);
612 return cmd;
613}
614
615static void end_cmd(struct nullb_cmd *cmd)
616{
Shaohua Li2984c862017-08-14 15:04:52 -0700617 int queue_mode = cmd->nq->dev->queue_mode;
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100618
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800619 switch (queue_mode) {
620 case NULL_Q_MQ:
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700621 blk_mq_end_request(cmd->rq, cmd->error);
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800622 return;
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800623 case NULL_Q_BIO:
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700624 cmd->bio->bi_status = cmd->error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200625 bio_endio(cmd->bio);
Jens Axboe48cc6612015-12-28 13:02:47 -0700626 break;
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800627 }
Jens Axboef2298c02013-10-25 11:52:25 +0100628
Jens Axboe48cc6612015-12-28 13:02:47 -0700629 free_cmd(cmd);
Jens Axboef2298c02013-10-25 11:52:25 +0100630}
631
632static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
633{
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100634 end_cmd(container_of(timer, struct nullb_cmd, timer));
Jens Axboef2298c02013-10-25 11:52:25 +0100635
636 return HRTIMER_NORESTART;
637}
638
639static void null_cmd_end_timer(struct nullb_cmd *cmd)
640{
Shaohua Li2984c862017-08-14 15:04:52 -0700641 ktime_t kt = cmd->nq->dev->completion_nsec;
Jens Axboef2298c02013-10-25 11:52:25 +0100642
Paolo Valente3c395a92015-12-01 11:48:17 +0100643 hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
Jens Axboef2298c02013-10-25 11:52:25 +0100644}
645
Christoph Hellwig49f66132018-11-10 09:30:45 +0100646static void null_complete_rq(struct request *rq)
Jens Axboef2298c02013-10-25 11:52:25 +0100647{
Christoph Hellwig49f66132018-11-10 09:30:45 +0100648 end_cmd(blk_mq_rq_to_pdu(rq));
Jens Axboef2298c02013-10-25 11:52:25 +0100649}
650
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700651static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
Jens Axboef2298c02013-10-25 11:52:25 +0100652{
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700653 struct nullb_page *t_page;
654
655 t_page = kmalloc(sizeof(struct nullb_page), gfp_flags);
656 if (!t_page)
657 goto out;
658
659 t_page->page = alloc_pages(gfp_flags, 0);
660 if (!t_page->page)
661 goto out_freepage;
662
Ming Lei66231ad2018-03-06 12:07:13 +0800663 memset(t_page->bitmap, 0, sizeof(t_page->bitmap));
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700664 return t_page;
665out_freepage:
666 kfree(t_page);
667out:
668 return NULL;
669}
670
671static void null_free_page(struct nullb_page *t_page)
672{
Ming Lei66231ad2018-03-06 12:07:13 +0800673 __set_bit(NULLB_PAGE_FREE, t_page->bitmap);
674 if (test_bit(NULLB_PAGE_LOCK, t_page->bitmap))
Shaohua Lideb78b42017-08-14 15:04:59 -0700675 return;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700676 __free_page(t_page->page);
677 kfree(t_page);
678}
679
Ming Lei66231ad2018-03-06 12:07:13 +0800680static bool null_page_empty(struct nullb_page *page)
681{
682 int size = MAP_SZ - 2;
683
684 return find_first_bit(page->bitmap, size) == size;
685}
686
Shaohua Lideb78b42017-08-14 15:04:59 -0700687static void null_free_sector(struct nullb *nullb, sector_t sector,
688 bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700689{
690 unsigned int sector_bit;
691 u64 idx;
692 struct nullb_page *t_page, *ret;
693 struct radix_tree_root *root;
694
Shaohua Lideb78b42017-08-14 15:04:59 -0700695 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700696 idx = sector >> PAGE_SECTORS_SHIFT;
697 sector_bit = (sector & SECTOR_MASK);
698
699 t_page = radix_tree_lookup(root, idx);
700 if (t_page) {
Ming Lei66231ad2018-03-06 12:07:13 +0800701 __clear_bit(sector_bit, t_page->bitmap);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700702
Ming Lei66231ad2018-03-06 12:07:13 +0800703 if (null_page_empty(t_page)) {
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700704 ret = radix_tree_delete_item(root, idx, t_page);
705 WARN_ON(ret != t_page);
706 null_free_page(ret);
Shaohua Lideb78b42017-08-14 15:04:59 -0700707 if (is_cache)
708 nullb->dev->curr_cache -= PAGE_SIZE;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700709 }
710 }
711}
712
713static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
Shaohua Lideb78b42017-08-14 15:04:59 -0700714 struct nullb_page *t_page, bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700715{
716 struct radix_tree_root *root;
717
Shaohua Lideb78b42017-08-14 15:04:59 -0700718 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700719
720 if (radix_tree_insert(root, idx, t_page)) {
721 null_free_page(t_page);
722 t_page = radix_tree_lookup(root, idx);
723 WARN_ON(!t_page || t_page->page->index != idx);
Shaohua Lideb78b42017-08-14 15:04:59 -0700724 } else if (is_cache)
725 nullb->dev->curr_cache += PAGE_SIZE;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700726
727 return t_page;
728}
729
Shaohua Lideb78b42017-08-14 15:04:59 -0700730static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700731{
732 unsigned long pos = 0;
733 int nr_pages;
734 struct nullb_page *ret, *t_pages[FREE_BATCH];
735 struct radix_tree_root *root;
736
Shaohua Lideb78b42017-08-14 15:04:59 -0700737 root = is_cache ? &dev->cache : &dev->data;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700738
739 do {
740 int i;
741
742 nr_pages = radix_tree_gang_lookup(root,
743 (void **)t_pages, pos, FREE_BATCH);
744
745 for (i = 0; i < nr_pages; i++) {
746 pos = t_pages[i]->page->index;
747 ret = radix_tree_delete_item(root, pos, t_pages[i]);
748 WARN_ON(ret != t_pages[i]);
749 null_free_page(ret);
750 }
751
752 pos++;
753 } while (nr_pages == FREE_BATCH);
Shaohua Lideb78b42017-08-14 15:04:59 -0700754
755 if (is_cache)
756 dev->curr_cache = 0;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700757}
758
Shaohua Lideb78b42017-08-14 15:04:59 -0700759static struct nullb_page *__null_lookup_page(struct nullb *nullb,
760 sector_t sector, bool for_write, bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700761{
762 unsigned int sector_bit;
763 u64 idx;
764 struct nullb_page *t_page;
Shaohua Lideb78b42017-08-14 15:04:59 -0700765 struct radix_tree_root *root;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700766
767 idx = sector >> PAGE_SECTORS_SHIFT;
768 sector_bit = (sector & SECTOR_MASK);
769
Shaohua Lideb78b42017-08-14 15:04:59 -0700770 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
771 t_page = radix_tree_lookup(root, idx);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700772 WARN_ON(t_page && t_page->page->index != idx);
773
Ming Lei66231ad2018-03-06 12:07:13 +0800774 if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700775 return t_page;
776
777 return NULL;
778}
779
Shaohua Lideb78b42017-08-14 15:04:59 -0700780static struct nullb_page *null_lookup_page(struct nullb *nullb,
781 sector_t sector, bool for_write, bool ignore_cache)
782{
783 struct nullb_page *page = NULL;
784
785 if (!ignore_cache)
786 page = __null_lookup_page(nullb, sector, for_write, true);
787 if (page)
788 return page;
789 return __null_lookup_page(nullb, sector, for_write, false);
790}
791
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700792static struct nullb_page *null_insert_page(struct nullb *nullb,
Jens Axboe61884de2018-08-09 14:22:41 -0600793 sector_t sector, bool ignore_cache)
794 __releases(&nullb->lock)
795 __acquires(&nullb->lock)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700796{
797 u64 idx;
798 struct nullb_page *t_page;
799
Shaohua Lideb78b42017-08-14 15:04:59 -0700800 t_page = null_lookup_page(nullb, sector, true, ignore_cache);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700801 if (t_page)
802 return t_page;
803
804 spin_unlock_irq(&nullb->lock);
805
806 t_page = null_alloc_page(GFP_NOIO);
807 if (!t_page)
808 goto out_lock;
809
810 if (radix_tree_preload(GFP_NOIO))
811 goto out_freepage;
812
813 spin_lock_irq(&nullb->lock);
814 idx = sector >> PAGE_SECTORS_SHIFT;
815 t_page->page->index = idx;
Shaohua Lideb78b42017-08-14 15:04:59 -0700816 t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700817 radix_tree_preload_end();
818
819 return t_page;
820out_freepage:
821 null_free_page(t_page);
822out_lock:
823 spin_lock_irq(&nullb->lock);
Shaohua Lideb78b42017-08-14 15:04:59 -0700824 return null_lookup_page(nullb, sector, true, ignore_cache);
825}
826
827static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
828{
829 int i;
830 unsigned int offset;
831 u64 idx;
832 struct nullb_page *t_page, *ret;
833 void *dst, *src;
834
835 idx = c_page->page->index;
836
837 t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);
838
Ming Lei66231ad2018-03-06 12:07:13 +0800839 __clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
840 if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
Shaohua Lideb78b42017-08-14 15:04:59 -0700841 null_free_page(c_page);
Ming Lei66231ad2018-03-06 12:07:13 +0800842 if (t_page && null_page_empty(t_page)) {
Shaohua Lideb78b42017-08-14 15:04:59 -0700843 ret = radix_tree_delete_item(&nullb->dev->data,
844 idx, t_page);
845 null_free_page(t_page);
846 }
847 return 0;
848 }
849
850 if (!t_page)
851 return -ENOMEM;
852
853 src = kmap_atomic(c_page->page);
854 dst = kmap_atomic(t_page->page);
855
856 for (i = 0; i < PAGE_SECTORS;
857 i += (nullb->dev->blocksize >> SECTOR_SHIFT)) {
Ming Lei66231ad2018-03-06 12:07:13 +0800858 if (test_bit(i, c_page->bitmap)) {
Shaohua Lideb78b42017-08-14 15:04:59 -0700859 offset = (i << SECTOR_SHIFT);
860 memcpy(dst + offset, src + offset,
861 nullb->dev->blocksize);
Ming Lei66231ad2018-03-06 12:07:13 +0800862 __set_bit(i, t_page->bitmap);
Shaohua Lideb78b42017-08-14 15:04:59 -0700863 }
864 }
865
866 kunmap_atomic(dst);
867 kunmap_atomic(src);
868
869 ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page);
870 null_free_page(ret);
871 nullb->dev->curr_cache -= PAGE_SIZE;
872
873 return 0;
874}
875
876static int null_make_cache_space(struct nullb *nullb, unsigned long n)
877{
878 int i, err, nr_pages;
879 struct nullb_page *c_pages[FREE_BATCH];
880 unsigned long flushed = 0, one_round;
881
882again:
883 if ((nullb->dev->cache_size * 1024 * 1024) >
884 nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0)
885 return 0;
886
887 nr_pages = radix_tree_gang_lookup(&nullb->dev->cache,
888 (void **)c_pages, nullb->cache_flush_pos, FREE_BATCH);
889 /*
890 * nullb_flush_cache_page could unlock before using the c_pages. To
891 * avoid race, we don't allow page free
892 */
893 for (i = 0; i < nr_pages; i++) {
894 nullb->cache_flush_pos = c_pages[i]->page->index;
895 /*
896 * We found the page which is being flushed to disk by other
897 * threads
898 */
Ming Lei66231ad2018-03-06 12:07:13 +0800899 if (test_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap))
Shaohua Lideb78b42017-08-14 15:04:59 -0700900 c_pages[i] = NULL;
901 else
Ming Lei66231ad2018-03-06 12:07:13 +0800902 __set_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap);
Shaohua Lideb78b42017-08-14 15:04:59 -0700903 }
904
905 one_round = 0;
906 for (i = 0; i < nr_pages; i++) {
907 if (c_pages[i] == NULL)
908 continue;
909 err = null_flush_cache_page(nullb, c_pages[i]);
910 if (err)
911 return err;
912 one_round++;
913 }
914 flushed += one_round << PAGE_SHIFT;
915
916 if (n > flushed) {
917 if (nr_pages == 0)
918 nullb->cache_flush_pos = 0;
919 if (one_round == 0) {
920 /* give other threads a chance */
921 spin_unlock_irq(&nullb->lock);
922 spin_lock_irq(&nullb->lock);
923 }
924 goto again;
925 }
926 return 0;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700927}
928
929static int copy_to_nullb(struct nullb *nullb, struct page *source,
Shaohua Lideb78b42017-08-14 15:04:59 -0700930 unsigned int off, sector_t sector, size_t n, bool is_fua)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700931{
932 size_t temp, count = 0;
933 unsigned int offset;
934 struct nullb_page *t_page;
935 void *dst, *src;
936
937 while (count < n) {
938 temp = min_t(size_t, nullb->dev->blocksize, n - count);
939
Shaohua Lideb78b42017-08-14 15:04:59 -0700940 if (null_cache_active(nullb) && !is_fua)
941 null_make_cache_space(nullb, PAGE_SIZE);
942
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700943 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
Shaohua Lideb78b42017-08-14 15:04:59 -0700944 t_page = null_insert_page(nullb, sector,
945 !null_cache_active(nullb) || is_fua);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700946 if (!t_page)
947 return -ENOSPC;
948
949 src = kmap_atomic(source);
950 dst = kmap_atomic(t_page->page);
951 memcpy(dst + offset, src + off + count, temp);
952 kunmap_atomic(dst);
953 kunmap_atomic(src);
954
Ming Lei66231ad2018-03-06 12:07:13 +0800955 __set_bit(sector & SECTOR_MASK, t_page->bitmap);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700956
Shaohua Lideb78b42017-08-14 15:04:59 -0700957 if (is_fua)
958 null_free_sector(nullb, sector, true);
959
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700960 count += temp;
961 sector += temp >> SECTOR_SHIFT;
962 }
963 return 0;
964}
965
966static int copy_from_nullb(struct nullb *nullb, struct page *dest,
967 unsigned int off, sector_t sector, size_t n)
968{
969 size_t temp, count = 0;
970 unsigned int offset;
971 struct nullb_page *t_page;
972 void *dst, *src;
973
974 while (count < n) {
975 temp = min_t(size_t, nullb->dev->blocksize, n - count);
976
977 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
Shaohua Lideb78b42017-08-14 15:04:59 -0700978 t_page = null_lookup_page(nullb, sector, false,
979 !null_cache_active(nullb));
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700980
981 dst = kmap_atomic(dest);
982 if (!t_page) {
983 memset(dst + off + count, 0, temp);
984 goto next;
985 }
986 src = kmap_atomic(t_page->page);
987 memcpy(dst + off + count, src + offset, temp);
988 kunmap_atomic(src);
989next:
990 kunmap_atomic(dst);
991
992 count += temp;
993 sector += temp >> SECTOR_SHIFT;
994 }
995 return 0;
996}
997
Shaohua Li306eb6b2017-08-14 15:04:57 -0700998static void null_handle_discard(struct nullb *nullb, sector_t sector, size_t n)
999{
1000 size_t temp;
1001
1002 spin_lock_irq(&nullb->lock);
1003 while (n > 0) {
1004 temp = min_t(size_t, n, nullb->dev->blocksize);
Shaohua Lideb78b42017-08-14 15:04:59 -07001005 null_free_sector(nullb, sector, false);
1006 if (null_cache_active(nullb))
1007 null_free_sector(nullb, sector, true);
Shaohua Li306eb6b2017-08-14 15:04:57 -07001008 sector += temp >> SECTOR_SHIFT;
1009 n -= temp;
1010 }
1011 spin_unlock_irq(&nullb->lock);
1012}
1013
Shaohua Lideb78b42017-08-14 15:04:59 -07001014static int null_handle_flush(struct nullb *nullb)
1015{
1016 int err;
1017
1018 if (!null_cache_active(nullb))
1019 return 0;
1020
1021 spin_lock_irq(&nullb->lock);
1022 while (true) {
1023 err = null_make_cache_space(nullb,
1024 nullb->dev->cache_size * 1024 * 1024);
1025 if (err || nullb->dev->curr_cache == 0)
1026 break;
1027 }
1028
1029 WARN_ON(!radix_tree_empty(&nullb->dev->cache));
1030 spin_unlock_irq(&nullb->lock);
1031 return err;
1032}
1033
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001034static int null_transfer(struct nullb *nullb, struct page *page,
Shaohua Lideb78b42017-08-14 15:04:59 -07001035 unsigned int len, unsigned int off, bool is_write, sector_t sector,
1036 bool is_fua)
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001037{
1038 int err = 0;
1039
1040 if (!is_write) {
1041 err = copy_from_nullb(nullb, page, off, sector, len);
1042 flush_dcache_page(page);
1043 } else {
1044 flush_dcache_page(page);
Shaohua Lideb78b42017-08-14 15:04:59 -07001045 err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001046 }
1047
1048 return err;
1049}
1050
1051static int null_handle_rq(struct nullb_cmd *cmd)
1052{
1053 struct request *rq = cmd->rq;
1054 struct nullb *nullb = cmd->nq->dev->nullb;
1055 int err;
1056 unsigned int len;
1057 sector_t sector;
1058 struct req_iterator iter;
1059 struct bio_vec bvec;
1060
1061 sector = blk_rq_pos(rq);
1062
Shaohua Li306eb6b2017-08-14 15:04:57 -07001063 if (req_op(rq) == REQ_OP_DISCARD) {
1064 null_handle_discard(nullb, sector, blk_rq_bytes(rq));
1065 return 0;
1066 }
1067
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001068 spin_lock_irq(&nullb->lock);
1069 rq_for_each_segment(bvec, rq, iter) {
1070 len = bvec.bv_len;
1071 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
Shaohua Lideb78b42017-08-14 15:04:59 -07001072 op_is_write(req_op(rq)), sector,
1073 req_op(rq) & REQ_FUA);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001074 if (err) {
1075 spin_unlock_irq(&nullb->lock);
1076 return err;
1077 }
1078 sector += len >> SECTOR_SHIFT;
1079 }
1080 spin_unlock_irq(&nullb->lock);
1081
1082 return 0;
1083}
1084
1085static int null_handle_bio(struct nullb_cmd *cmd)
1086{
1087 struct bio *bio = cmd->bio;
1088 struct nullb *nullb = cmd->nq->dev->nullb;
1089 int err;
1090 unsigned int len;
1091 sector_t sector;
1092 struct bio_vec bvec;
1093 struct bvec_iter iter;
1094
1095 sector = bio->bi_iter.bi_sector;
1096
Shaohua Li306eb6b2017-08-14 15:04:57 -07001097 if (bio_op(bio) == REQ_OP_DISCARD) {
1098 null_handle_discard(nullb, sector,
1099 bio_sectors(bio) << SECTOR_SHIFT);
1100 return 0;
1101 }
1102
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001103 spin_lock_irq(&nullb->lock);
1104 bio_for_each_segment(bvec, bio, iter) {
1105 len = bvec.bv_len;
1106 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
Shaohua Lideb78b42017-08-14 15:04:59 -07001107 op_is_write(bio_op(bio)), sector,
Heinz Mauelshagenbf7c7a02019-02-22 20:00:01 +01001108 bio->bi_opf & REQ_FUA);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001109 if (err) {
1110 spin_unlock_irq(&nullb->lock);
1111 return err;
1112 }
1113 sector += len >> SECTOR_SHIFT;
1114 }
1115 spin_unlock_irq(&nullb->lock);
1116 return 0;
1117}
1118
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001119static void null_stop_queue(struct nullb *nullb)
1120{
1121 struct request_queue *q = nullb->q;
1122
1123 if (nullb->dev->queue_mode == NULL_Q_MQ)
1124 blk_mq_stop_hw_queues(q);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001125}
1126
1127static void null_restart_queue_async(struct nullb *nullb)
1128{
1129 struct request_queue *q = nullb->q;
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001130
1131 if (nullb->dev->queue_mode == NULL_Q_MQ)
1132 blk_mq_start_stopped_hw_queues(q, true);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001133}
1134
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001135static blk_status_t null_handle_cmd(struct nullb_cmd *cmd)
1136{
1137 struct nullb_device *dev = cmd->nq->dev;
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001138 struct nullb *nullb = dev->nullb;
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001139 int err = 0;
1140
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001141 if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) {
1142 struct request *rq = cmd->rq;
1143
1144 if (!hrtimer_active(&nullb->bw_timer))
1145 hrtimer_restart(&nullb->bw_timer);
1146
1147 if (atomic_long_sub_return(blk_rq_bytes(rq),
1148 &nullb->cur_bytes) < 0) {
1149 null_stop_queue(nullb);
1150 /* race with timer */
1151 if (atomic_long_read(&nullb->cur_bytes) > 0)
1152 null_restart_queue_async(nullb);
Jens Axboee50b1e32018-10-11 17:58:17 -06001153 /* requeue request */
1154 return BLK_STS_DEV_RESOURCE;
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001155 }
1156 }
1157
Shaohua Li2f54a612017-08-14 15:05:00 -07001158 if (nullb->dev->badblocks.shift != -1) {
1159 int bad_sectors;
1160 sector_t sector, size, first_bad;
1161 bool is_flush = true;
1162
1163 if (dev->queue_mode == NULL_Q_BIO &&
1164 bio_op(cmd->bio) != REQ_OP_FLUSH) {
1165 is_flush = false;
1166 sector = cmd->bio->bi_iter.bi_sector;
1167 size = bio_sectors(cmd->bio);
1168 }
1169 if (dev->queue_mode != NULL_Q_BIO &&
1170 req_op(cmd->rq) != REQ_OP_FLUSH) {
1171 is_flush = false;
1172 sector = blk_rq_pos(cmd->rq);
1173 size = blk_rq_sectors(cmd->rq);
1174 }
1175 if (!is_flush && badblocks_check(&nullb->dev->badblocks, sector,
1176 size, &first_bad, &bad_sectors)) {
1177 cmd->error = BLK_STS_IOERR;
1178 goto out;
1179 }
1180 }
1181
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001182 if (dev->memory_backed) {
Shaohua Lideb78b42017-08-14 15:04:59 -07001183 if (dev->queue_mode == NULL_Q_BIO) {
1184 if (bio_op(cmd->bio) == REQ_OP_FLUSH)
1185 err = null_handle_flush(nullb);
1186 else
1187 err = null_handle_bio(cmd);
1188 } else {
1189 if (req_op(cmd->rq) == REQ_OP_FLUSH)
1190 err = null_handle_flush(nullb);
1191 else
1192 err = null_handle_rq(cmd);
1193 }
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001194 }
1195 cmd->error = errno_to_blk_status(err);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001196
1197 if (!cmd->error && dev->zoned) {
Jens Axboeb228ba12018-09-12 18:21:11 -06001198 sector_t sector;
1199 unsigned int nr_sectors;
1200 int op;
1201
1202 if (dev->queue_mode == NULL_Q_BIO) {
1203 op = bio_op(cmd->bio);
1204 sector = cmd->bio->bi_iter.bi_sector;
1205 nr_sectors = cmd->bio->bi_iter.bi_size >> 9;
1206 } else {
1207 op = req_op(cmd->rq);
1208 sector = blk_rq_pos(cmd->rq);
1209 nr_sectors = blk_rq_sectors(cmd->rq);
1210 }
1211
1212 if (op == REQ_OP_WRITE)
1213 null_zone_write(cmd, sector, nr_sectors);
1214 else if (op == REQ_OP_ZONE_RESET)
1215 null_zone_reset(cmd, sector);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001216 }
Shaohua Li2f54a612017-08-14 15:05:00 -07001217out:
Jens Axboef2298c02013-10-25 11:52:25 +01001218 /* Complete IO by inline, softirq or timer */
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001219 switch (dev->irqmode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001220 case NULL_IRQ_SOFTIRQ:
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001221 switch (dev->queue_mode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001222 case NULL_Q_MQ:
Christoph Hellwig08e00292017-04-20 16:03:09 +02001223 blk_mq_complete_request(cmd->rq);
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001224 break;
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001225 case NULL_Q_BIO:
1226 /*
1227 * XXX: no proper submitting cpu information available.
1228 */
1229 end_cmd(cmd);
1230 break;
1231 }
1232 break;
Jens Axboef2298c02013-10-25 11:52:25 +01001233 case NULL_IRQ_NONE:
1234 end_cmd(cmd);
1235 break;
Jens Axboef2298c02013-10-25 11:52:25 +01001236 case NULL_IRQ_TIMER:
1237 null_cmd_end_timer(cmd);
1238 break;
1239 }
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001240 return BLK_STS_OK;
Jens Axboef2298c02013-10-25 11:52:25 +01001241}
1242
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001243static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
1244{
1245 struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
1246 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1247 unsigned int mbps = nullb->dev->mbps;
1248
1249 if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
1250 return HRTIMER_NORESTART;
1251
1252 atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
1253 null_restart_queue_async(nullb);
1254
1255 hrtimer_forward_now(&nullb->bw_timer, timer_interval);
1256
1257 return HRTIMER_RESTART;
1258}
1259
1260static void nullb_setup_bwtimer(struct nullb *nullb)
1261{
1262 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1263
1264 hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1265 nullb->bw_timer.function = nullb_bwtimer_fn;
1266 atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps));
1267 hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL);
Jens Axboef2298c02013-10-25 11:52:25 +01001268}
1269
1270static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
1271{
1272 int index = 0;
1273
1274 if (nullb->nr_queues != 1)
1275 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
1276
1277 return &nullb->queues[index];
1278}
1279
Jens Axboedece1632015-11-05 10:41:16 -07001280static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
Jens Axboef2298c02013-10-25 11:52:25 +01001281{
1282 struct nullb *nullb = q->queuedata;
1283 struct nullb_queue *nq = nullb_to_queue(nullb);
1284 struct nullb_cmd *cmd;
1285
1286 cmd = alloc_cmd(nq, 1);
1287 cmd->bio = bio;
1288
1289 null_handle_cmd(cmd);
Jens Axboedece1632015-11-05 10:41:16 -07001290 return BLK_QC_T_NONE;
Jens Axboef2298c02013-10-25 11:52:25 +01001291}
1292
Jens Axboe93b57042018-01-10 09:06:23 -07001293static bool should_timeout_request(struct request *rq)
1294{
Arnd Bergmann33f782c2018-01-11 11:31:25 +01001295#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
Jens Axboe93b57042018-01-10 09:06:23 -07001296 if (g_timeout_str[0])
1297 return should_fail(&null_timeout_attr, 1);
Arnd Bergmann33f782c2018-01-11 11:31:25 +01001298#endif
Jens Axboe24941b92018-02-28 09:18:57 -07001299 return false;
1300}
Jens Axboe93b57042018-01-10 09:06:23 -07001301
Jens Axboe24941b92018-02-28 09:18:57 -07001302static bool should_requeue_request(struct request *rq)
1303{
1304#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1305 if (g_requeue_str[0])
1306 return should_fail(&null_requeue_attr, 1);
1307#endif
Jens Axboe93b57042018-01-10 09:06:23 -07001308 return false;
1309}
1310
Jens Axboe5448aca2018-01-09 12:47:24 -07001311static enum blk_eh_timer_return null_timeout_rq(struct request *rq, bool res)
1312{
1313 pr_info("null: rq %p timed out\n", rq);
Christoph Hellwig0df0bb02018-05-29 15:52:33 +02001314 blk_mq_complete_request(rq);
1315 return BLK_EH_DONE;
Jens Axboe5448aca2018-01-09 12:47:24 -07001316}
1317
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001318static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -06001319 const struct blk_mq_queue_data *bd)
Jens Axboef2298c02013-10-25 11:52:25 +01001320{
Jens Axboe74c45052014-10-29 11:14:52 -06001321 struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Shaohua Li2984c862017-08-14 15:04:52 -07001322 struct nullb_queue *nq = hctx->driver_data;
Jens Axboef2298c02013-10-25 11:52:25 +01001323
Jens Axboedb5bcf82017-03-30 13:44:26 -06001324 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1325
Shaohua Li2984c862017-08-14 15:04:52 -07001326 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +01001327 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1328 cmd->timer.function = null_cmd_timer_expired;
1329 }
Jens Axboe74c45052014-10-29 11:14:52 -06001330 cmd->rq = bd->rq;
Shaohua Li2984c862017-08-14 15:04:52 -07001331 cmd->nq = nq;
Jens Axboef2298c02013-10-25 11:52:25 +01001332
Jens Axboe74c45052014-10-29 11:14:52 -06001333 blk_mq_start_request(bd->rq);
Christoph Hellwige2490072014-09-13 16:40:09 -07001334
Jens Axboe24941b92018-02-28 09:18:57 -07001335 if (should_requeue_request(bd->rq)) {
1336 /*
1337 * Alternate between hitting the core BUSY path, and the
1338 * driver driven requeue path
1339 */
1340 nq->requeue_selection++;
1341 if (nq->requeue_selection & 1)
1342 return BLK_STS_RESOURCE;
1343 else {
1344 blk_mq_requeue_request(bd->rq, true);
1345 return BLK_STS_OK;
1346 }
1347 }
1348 if (should_timeout_request(bd->rq))
1349 return BLK_STS_OK;
Jens Axboe93b57042018-01-10 09:06:23 -07001350
Jens Axboe24941b92018-02-28 09:18:57 -07001351 return null_handle_cmd(cmd);
Jens Axboef2298c02013-10-25 11:52:25 +01001352}
1353
Eric Biggersf363b082017-03-30 13:39:16 -07001354static const struct blk_mq_ops null_mq_ops = {
Jens Axboef2298c02013-10-25 11:52:25 +01001355 .queue_rq = null_queue_rq,
Christoph Hellwig49f66132018-11-10 09:30:45 +01001356 .complete = null_complete_rq,
Jens Axboe5448aca2018-01-09 12:47:24 -07001357 .timeout = null_timeout_rq,
Jens Axboef2298c02013-10-25 11:52:25 +01001358};
1359
Matias Bjørlingde65d2d2015-08-31 14:17:18 +02001360static void cleanup_queue(struct nullb_queue *nq)
1361{
1362 kfree(nq->tag_map);
1363 kfree(nq->cmds);
1364}
1365
1366static void cleanup_queues(struct nullb *nullb)
1367{
1368 int i;
1369
1370 for (i = 0; i < nullb->nr_queues; i++)
1371 cleanup_queue(&nullb->queues[i]);
1372
1373 kfree(nullb->queues);
1374}
1375
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001376static void null_del_dev(struct nullb *nullb)
1377{
Shaohua Li2984c862017-08-14 15:04:52 -07001378 struct nullb_device *dev = nullb->dev;
1379
Shaohua Li94bc02e2017-08-14 15:04:55 -07001380 ida_simple_remove(&nullb_indexes, nullb->index);
1381
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001382 list_del_init(&nullb->list);
1383
Matias Bjørling74ede5a2018-01-05 14:15:57 +01001384 del_gendisk(nullb->disk);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001385
1386 if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
1387 hrtimer_cancel(&nullb->bw_timer);
1388 atomic_long_set(&nullb->cur_bytes, LONG_MAX);
1389 null_restart_queue_async(nullb);
1390 }
1391
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001392 blk_cleanup_queue(nullb->q);
Shaohua Li2984c862017-08-14 15:04:52 -07001393 if (dev->queue_mode == NULL_Q_MQ &&
1394 nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001395 blk_mq_free_tag_set(nullb->tag_set);
Matias Bjørling74ede5a2018-01-05 14:15:57 +01001396 put_disk(nullb->disk);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001397 cleanup_queues(nullb);
Shaohua Lideb78b42017-08-14 15:04:59 -07001398 if (null_cache_active(nullb))
1399 null_free_device_storage(nullb->dev, true);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001400 kfree(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001401 dev->nullb = NULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001402}
1403
Shaohua Li306eb6b2017-08-14 15:04:57 -07001404static void null_config_discard(struct nullb *nullb)
1405{
1406 if (nullb->dev->discard == false)
1407 return;
1408 nullb->q->limits.discard_granularity = nullb->dev->blocksize;
1409 nullb->q->limits.discard_alignment = nullb->dev->blocksize;
1410 blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001411 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nullb->q);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001412}
1413
Jens Axboef2298c02013-10-25 11:52:25 +01001414static int null_open(struct block_device *bdev, fmode_t mode)
1415{
1416 return 0;
1417}
1418
1419static void null_release(struct gendisk *disk, fmode_t mode)
1420{
1421}
1422
1423static const struct block_device_operations null_fops = {
1424 .owner = THIS_MODULE,
1425 .open = null_open,
1426 .release = null_release,
Christoph Hellwige76239a2018-10-12 19:08:49 +09001427 .report_zones = null_zone_report,
Jens Axboef2298c02013-10-25 11:52:25 +01001428};
1429
Jens Axboe82f402f2017-06-20 14:22:01 -06001430static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
1431{
1432 BUG_ON(!nullb);
1433 BUG_ON(!nq);
1434
1435 init_waitqueue_head(&nq->wait);
1436 nq->queue_depth = nullb->queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -07001437 nq->dev = nullb->dev;
Jens Axboe82f402f2017-06-20 14:22:01 -06001438}
1439
1440static void null_init_queues(struct nullb *nullb)
1441{
1442 struct request_queue *q = nullb->q;
1443 struct blk_mq_hw_ctx *hctx;
1444 struct nullb_queue *nq;
1445 int i;
1446
1447 queue_for_each_hw_ctx(q, hctx, i) {
1448 if (!hctx->nr_ctx || !hctx->tags)
1449 continue;
1450 nq = &nullb->queues[i];
1451 hctx->driver_data = nq;
1452 null_init_queue(nullb, nq);
1453 nullb->nr_queues++;
1454 }
1455}
1456
Jens Axboef2298c02013-10-25 11:52:25 +01001457static int setup_commands(struct nullb_queue *nq)
1458{
1459 struct nullb_cmd *cmd;
1460 int i, tag_size;
1461
Kees Cook6396bb22018-06-12 14:03:40 -07001462 nq->cmds = kcalloc(nq->queue_depth, sizeof(*cmd), GFP_KERNEL);
Jens Axboef2298c02013-10-25 11:52:25 +01001463 if (!nq->cmds)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001464 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001465
1466 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07001467 nq->tag_map = kcalloc(tag_size, sizeof(unsigned long), GFP_KERNEL);
Jens Axboef2298c02013-10-25 11:52:25 +01001468 if (!nq->tag_map) {
1469 kfree(nq->cmds);
Matias Bjorling2d263a782013-12-18 13:41:43 +01001470 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001471 }
1472
1473 for (i = 0; i < nq->queue_depth; i++) {
1474 cmd = &nq->cmds[i];
1475 INIT_LIST_HEAD(&cmd->list);
1476 cmd->ll_list.next = NULL;
1477 cmd->tag = -1U;
1478 }
1479
1480 return 0;
1481}
1482
Jens Axboef2298c02013-10-25 11:52:25 +01001483static int setup_queues(struct nullb *nullb)
1484{
Kees Cook6396bb22018-06-12 14:03:40 -07001485 nullb->queues = kcalloc(nullb->dev->submit_queues,
1486 sizeof(struct nullb_queue),
1487 GFP_KERNEL);
Jens Axboef2298c02013-10-25 11:52:25 +01001488 if (!nullb->queues)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001489 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001490
1491 nullb->nr_queues = 0;
Shaohua Li2984c862017-08-14 15:04:52 -07001492 nullb->queue_depth = nullb->dev->hw_queue_depth;
Jens Axboef2298c02013-10-25 11:52:25 +01001493
Matias Bjorling2d263a782013-12-18 13:41:43 +01001494 return 0;
1495}
1496
1497static int init_driver_queues(struct nullb *nullb)
1498{
1499 struct nullb_queue *nq;
1500 int i, ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001501
Shaohua Li2984c862017-08-14 15:04:52 -07001502 for (i = 0; i < nullb->dev->submit_queues; i++) {
Jens Axboef2298c02013-10-25 11:52:25 +01001503 nq = &nullb->queues[i];
Matias Bjorling2d263a782013-12-18 13:41:43 +01001504
1505 null_init_queue(nullb, nq);
1506
1507 ret = setup_commands(nq);
1508 if (ret)
Jan Kara31f96902014-10-22 15:34:21 +02001509 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01001510 nullb->nr_queues++;
1511 }
Matias Bjorling2d263a782013-12-18 13:41:43 +01001512 return 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001513}
1514
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001515static int null_gendisk_register(struct nullb *nullb)
Jens Axboef2298c02013-10-25 11:52:25 +01001516{
1517 struct gendisk *disk;
Jens Axboef2298c02013-10-25 11:52:25 +01001518 sector_t size;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001519
Shaohua Li2984c862017-08-14 15:04:52 -07001520 disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001521 if (!disk)
1522 return -ENOMEM;
Shaohua Li2984c862017-08-14 15:04:52 -07001523 size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001524 set_capacity(disk, size >> 9);
1525
1526 disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
1527 disk->major = null_major;
1528 disk->first_minor = nullb->index;
1529 disk->fops = &null_fops;
1530 disk->private_data = nullb;
1531 disk->queue = nullb->q;
1532 strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
1533
Damien Le Moalbf505452018-10-12 19:08:50 +09001534 if (nullb->dev->zoned) {
1535 int ret = blk_revalidate_disk_zones(disk);
1536
1537 if (ret != 0)
1538 return ret;
1539 }
1540
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001541 add_disk(disk);
1542 return 0;
1543}
1544
Shaohua Li2984c862017-08-14 15:04:52 -07001545static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001546{
1547 set->ops = &null_mq_ops;
Shaohua Li2984c862017-08-14 15:04:52 -07001548 set->nr_hw_queues = nullb ? nullb->dev->submit_queues :
1549 g_submit_queues;
1550 set->queue_depth = nullb ? nullb->dev->hw_queue_depth :
1551 g_hw_queue_depth;
1552 set->numa_node = nullb ? nullb->dev->home_node : g_home_node;
Jens Axboe82f402f2017-06-20 14:22:01 -06001553 set->cmd_size = sizeof(struct nullb_cmd);
1554 set->flags = BLK_MQ_F_SHOULD_MERGE;
weiping zhangb3cffc32017-09-30 09:49:21 +08001555 if (g_no_sched)
1556 set->flags |= BLK_MQ_F_NO_SCHED;
Jens Axboe82f402f2017-06-20 14:22:01 -06001557 set->driver_data = NULL;
1558
Shaohua Li0d06a422017-08-25 13:46:25 -07001559 if ((nullb && nullb->dev->blocking) || g_blocking)
Jens Axboe82f402f2017-06-20 14:22:01 -06001560 set->flags |= BLK_MQ_F_BLOCKING;
1561
1562 return blk_mq_alloc_tag_set(set);
1563}
1564
Shaohua Licedcafa2017-08-14 15:04:54 -07001565static void null_validate_conf(struct nullb_device *dev)
1566{
1567 dev->blocksize = round_down(dev->blocksize, 512);
1568 dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096);
Shaohua Licedcafa2017-08-14 15:04:54 -07001569
1570 if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) {
1571 if (dev->submit_queues != nr_online_nodes)
1572 dev->submit_queues = nr_online_nodes;
1573 } else if (dev->submit_queues > nr_cpu_ids)
1574 dev->submit_queues = nr_cpu_ids;
1575 else if (dev->submit_queues == 0)
1576 dev->submit_queues = 1;
1577
1578 dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ);
1579 dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001580
1581 /* Do memory allocation, so set blocking */
1582 if (dev->memory_backed)
1583 dev->blocking = true;
Shaohua Lideb78b42017-08-14 15:04:59 -07001584 else /* cache is meaningless */
1585 dev->cache_size = 0;
1586 dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
1587 dev->cache_size);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001588 dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
1589 /* can not stop a queue */
1590 if (dev->queue_mode == NULL_Q_BIO)
1591 dev->mbps = 0;
Shaohua Licedcafa2017-08-14 15:04:54 -07001592}
1593
Jens Axboe24941b92018-02-28 09:18:57 -07001594#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1595static bool __null_setup_fault(struct fault_attr *attr, char *str)
1596{
1597 if (!str[0])
1598 return true;
1599
1600 if (!setup_fault_attr(attr, str))
1601 return false;
1602
1603 attr->verbose = 0;
1604 return true;
1605}
1606#endif
1607
Jens Axboe93b57042018-01-10 09:06:23 -07001608static bool null_setup_fault(void)
1609{
Arnd Bergmann33f782c2018-01-11 11:31:25 +01001610#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
Jens Axboe24941b92018-02-28 09:18:57 -07001611 if (!__null_setup_fault(&null_timeout_attr, g_timeout_str))
Jens Axboe93b57042018-01-10 09:06:23 -07001612 return false;
Jens Axboe24941b92018-02-28 09:18:57 -07001613 if (!__null_setup_fault(&null_requeue_attr, g_requeue_str))
1614 return false;
Arnd Bergmann33f782c2018-01-11 11:31:25 +01001615#endif
Jens Axboe93b57042018-01-10 09:06:23 -07001616 return true;
1617}
1618
Shaohua Li2984c862017-08-14 15:04:52 -07001619static int null_add_dev(struct nullb_device *dev)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001620{
1621 struct nullb *nullb;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001622 int rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001623
Shaohua Licedcafa2017-08-14 15:04:54 -07001624 null_validate_conf(dev);
1625
Shaohua Li2984c862017-08-14 15:04:52 -07001626 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001627 if (!nullb) {
1628 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001629 goto out;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001630 }
Shaohua Li2984c862017-08-14 15:04:52 -07001631 nullb->dev = dev;
1632 dev->nullb = nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001633
1634 spin_lock_init(&nullb->lock);
1635
Robert Elliottdc501dc2014-09-02 11:38:49 -05001636 rv = setup_queues(nullb);
1637 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001638 goto out_free_nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001639
Shaohua Li2984c862017-08-14 15:04:52 -07001640 if (dev->queue_mode == NULL_Q_MQ) {
Jens Axboe82f402f2017-06-20 14:22:01 -06001641 if (shared_tags) {
1642 nullb->tag_set = &tag_set;
1643 rv = 0;
1644 } else {
1645 nullb->tag_set = &nullb->__tag_set;
Shaohua Li2984c862017-08-14 15:04:52 -07001646 rv = null_init_tag_set(nullb, nullb->tag_set);
Jens Axboe82f402f2017-06-20 14:22:01 -06001647 }
Jens Axboef2298c02013-10-25 11:52:25 +01001648
Robert Elliottdc501dc2014-09-02 11:38:49 -05001649 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001650 goto out_cleanup_queues;
Jens Axboef2298c02013-10-25 11:52:25 +01001651
Jens Axboe93b57042018-01-10 09:06:23 -07001652 if (!null_setup_fault())
1653 goto out_cleanup_queues;
1654
Jens Axboe5448aca2018-01-09 12:47:24 -07001655 nullb->tag_set->timeout = 5 * HZ;
Jens Axboe82f402f2017-06-20 14:22:01 -06001656 nullb->q = blk_mq_init_queue(nullb->tag_set);
Ming Lei35b489d2015-01-02 14:25:27 +00001657 if (IS_ERR(nullb->q)) {
Robert Elliottdc501dc2014-09-02 11:38:49 -05001658 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001659 goto out_cleanup_tags;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001660 }
Jens Axboe82f402f2017-06-20 14:22:01 -06001661 null_init_queues(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001662 } else if (dev->queue_mode == NULL_Q_BIO) {
Christoph Hellwig6d469642018-11-14 17:02:18 +01001663 nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001664 if (!nullb->q) {
1665 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001666 goto out_cleanup_queues;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001667 }
Jens Axboef2298c02013-10-25 11:52:25 +01001668 blk_queue_make_request(nullb->q, null_queue_bio);
Jan Kara31f96902014-10-22 15:34:21 +02001669 rv = init_driver_queues(nullb);
1670 if (rv)
1671 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001672 }
1673
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001674 if (dev->mbps) {
1675 set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags);
1676 nullb_setup_bwtimer(nullb);
1677 }
1678
Shaohua Lideb78b42017-08-14 15:04:59 -07001679 if (dev->cache_size > 0) {
1680 set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
1681 blk_queue_write_cache(nullb->q, true, true);
Shaohua Lideb78b42017-08-14 15:04:59 -07001682 }
1683
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001684 if (dev->zoned) {
1685 rv = null_zone_init(dev);
1686 if (rv)
1687 goto out_cleanup_blk_queue;
1688
1689 blk_queue_chunk_sectors(nullb->q, dev->zone_size_sects);
1690 nullb->q->limits.zoned = BLK_ZONED_HM;
1691 }
1692
Jens Axboef2298c02013-10-25 11:52:25 +01001693 nullb->q->queuedata = nullb;
Bart Van Assche8b904b52018-03-07 17:10:10 -08001694 blk_queue_flag_set(QUEUE_FLAG_NONROT, nullb->q);
1695 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, nullb->q);
Jens Axboef2298c02013-10-25 11:52:25 +01001696
Jens Axboef2298c02013-10-25 11:52:25 +01001697 mutex_lock(&lock);
Shaohua Li94bc02e2017-08-14 15:04:55 -07001698 nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
Shaohua Licedcafa2017-08-14 15:04:54 -07001699 dev->index = nullb->index;
Jens Axboef2298c02013-10-25 11:52:25 +01001700 mutex_unlock(&lock);
1701
Shaohua Li2984c862017-08-14 15:04:52 -07001702 blk_queue_logical_block_size(nullb->q, dev->blocksize);
1703 blk_queue_physical_block_size(nullb->q, dev->blocksize);
Jens Axboef2298c02013-10-25 11:52:25 +01001704
Shaohua Li306eb6b2017-08-14 15:04:57 -07001705 null_config_discard(nullb);
Jens Axboef2298c02013-10-25 11:52:25 +01001706
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001707 sprintf(nullb->disk_name, "nullb%d", nullb->index);
1708
Matias Bjørling74ede5a2018-01-05 14:15:57 +01001709 rv = null_gendisk_register(nullb);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001710 if (rv)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001711 goto out_cleanup_zone;
Jens Axboef2298c02013-10-25 11:52:25 +01001712
Matias Bjørlinga5143792016-02-11 14:49:13 +01001713 mutex_lock(&lock);
1714 list_add_tail(&nullb->list, &nullb_list);
1715 mutex_unlock(&lock);
Wenwei Tao3681c85d2016-03-05 00:27:04 +08001716
Jens Axboef2298c02013-10-25 11:52:25 +01001717 return 0;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001718out_cleanup_zone:
1719 if (dev->zoned)
1720 null_zone_exit(dev);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001721out_cleanup_blk_queue:
1722 blk_cleanup_queue(nullb->q);
1723out_cleanup_tags:
Shaohua Li2984c862017-08-14 15:04:52 -07001724 if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001725 blk_mq_free_tag_set(nullb->tag_set);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001726out_cleanup_queues:
1727 cleanup_queues(nullb);
1728out_free_nullb:
1729 kfree(nullb);
1730out:
Robert Elliottdc501dc2014-09-02 11:38:49 -05001731 return rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001732}
1733
1734static int __init null_init(void)
1735{
Minfei Huangaf096e22015-12-08 13:47:34 -07001736 int ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001737 unsigned int i;
Minfei Huangaf096e22015-12-08 13:47:34 -07001738 struct nullb *nullb;
Shaohua Li2984c862017-08-14 15:04:52 -07001739 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +01001740
Shaohua Li2984c862017-08-14 15:04:52 -07001741 if (g_bs > PAGE_SIZE) {
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301742 pr_warn("null_blk: invalid block size\n");
1743 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
Shaohua Li2984c862017-08-14 15:04:52 -07001744 g_bs = PAGE_SIZE;
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301745 }
Jens Axboef2298c02013-10-25 11:52:25 +01001746
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001747 if (!is_power_of_2(g_zone_size)) {
1748 pr_err("null_blk: zone_size must be power-of-two\n");
1749 return -EINVAL;
1750 }
1751
John Pittman7ff684a2019-04-05 17:42:45 -04001752 if (g_home_node != NUMA_NO_NODE && g_home_node >= nr_online_nodes) {
1753 pr_err("null_blk: invalid home_node value\n");
1754 g_home_node = NUMA_NO_NODE;
1755 }
1756
Jens Axboee50b1e32018-10-11 17:58:17 -06001757 if (g_queue_mode == NULL_Q_RQ) {
1758 pr_err("null_blk: legacy IO path no longer available\n");
1759 return -EINVAL;
1760 }
Shaohua Li2984c862017-08-14 15:04:52 -07001761 if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) {
1762 if (g_submit_queues != nr_online_nodes) {
weiping zhang558ab3002017-08-03 00:26:39 +08001763 pr_warn("null_blk: submit_queues param is set to %u.\n",
Matias Bjorlingd15ee6b2013-12-18 13:41:44 +01001764 nr_online_nodes);
Shaohua Li2984c862017-08-14 15:04:52 -07001765 g_submit_queues = nr_online_nodes;
Matias Bjørlingfc1bc352013-12-21 00:11:01 +01001766 }
Shaohua Li2984c862017-08-14 15:04:52 -07001767 } else if (g_submit_queues > nr_cpu_ids)
1768 g_submit_queues = nr_cpu_ids;
1769 else if (g_submit_queues <= 0)
1770 g_submit_queues = 1;
Jens Axboef2298c02013-10-25 11:52:25 +01001771
Shaohua Li2984c862017-08-14 15:04:52 -07001772 if (g_queue_mode == NULL_Q_MQ && shared_tags) {
1773 ret = null_init_tag_set(NULL, &tag_set);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001774 if (ret)
1775 return ret;
1776 }
1777
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001778 config_group_init(&nullb_subsys.su_group);
1779 mutex_init(&nullb_subsys.su_mutex);
1780
1781 ret = configfs_register_subsystem(&nullb_subsys);
1782 if (ret)
1783 goto err_tagset;
1784
Jens Axboef2298c02013-10-25 11:52:25 +01001785 mutex_init(&lock);
1786
Jens Axboef2298c02013-10-25 11:52:25 +01001787 null_major = register_blkdev(0, "nullb");
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001788 if (null_major < 0) {
1789 ret = null_major;
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001790 goto err_conf;
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001791 }
Jens Axboef2298c02013-10-25 11:52:25 +01001792
Minfei Huangaf096e22015-12-08 13:47:34 -07001793 for (i = 0; i < nr_devices; i++) {
Shaohua Li2984c862017-08-14 15:04:52 -07001794 dev = null_alloc_dev();
Wei Yongjun30c516d2017-10-17 12:11:46 +00001795 if (!dev) {
1796 ret = -ENOMEM;
Minfei Huangaf096e22015-12-08 13:47:34 -07001797 goto err_dev;
Wei Yongjun30c516d2017-10-17 12:11:46 +00001798 }
Shaohua Li2984c862017-08-14 15:04:52 -07001799 ret = null_add_dev(dev);
1800 if (ret) {
1801 null_free_dev(dev);
1802 goto err_dev;
1803 }
Minfei Huangaf096e22015-12-08 13:47:34 -07001804 }
1805
Jens Axboef2298c02013-10-25 11:52:25 +01001806 pr_info("null: module loaded\n");
1807 return 0;
Minfei Huangaf096e22015-12-08 13:47:34 -07001808
1809err_dev:
1810 while (!list_empty(&nullb_list)) {
1811 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07001812 dev = nullb->dev;
Minfei Huangaf096e22015-12-08 13:47:34 -07001813 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001814 null_free_dev(dev);
Minfei Huangaf096e22015-12-08 13:47:34 -07001815 }
Minfei Huangaf096e22015-12-08 13:47:34 -07001816 unregister_blkdev(null_major, "nullb");
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001817err_conf:
1818 configfs_unregister_subsystem(&nullb_subsys);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001819err_tagset:
Shaohua Li2984c862017-08-14 15:04:52 -07001820 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001821 blk_mq_free_tag_set(&tag_set);
Minfei Huangaf096e22015-12-08 13:47:34 -07001822 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01001823}
1824
1825static void __exit null_exit(void)
1826{
1827 struct nullb *nullb;
1828
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001829 configfs_unregister_subsystem(&nullb_subsys);
1830
Jens Axboef2298c02013-10-25 11:52:25 +01001831 unregister_blkdev(null_major, "nullb");
1832
1833 mutex_lock(&lock);
1834 while (!list_empty(&nullb_list)) {
Shaohua Li2984c862017-08-14 15:04:52 -07001835 struct nullb_device *dev;
1836
Jens Axboef2298c02013-10-25 11:52:25 +01001837 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07001838 dev = nullb->dev;
Jens Axboef2298c02013-10-25 11:52:25 +01001839 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001840 null_free_dev(dev);
Jens Axboef2298c02013-10-25 11:52:25 +01001841 }
1842 mutex_unlock(&lock);
Matias Bjørling6bb95352015-11-19 12:50:08 +01001843
Shaohua Li2984c862017-08-14 15:04:52 -07001844 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Jens Axboe82f402f2017-06-20 14:22:01 -06001845 blk_mq_free_tag_set(&tag_set);
Jens Axboef2298c02013-10-25 11:52:25 +01001846}
1847
1848module_init(null_init);
1849module_exit(null_exit);
1850
Jens Axboe231b3db2017-08-25 12:53:15 -06001851MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
Jens Axboef2298c02013-10-25 11:52:25 +01001852MODULE_LICENSE("GPL");