blob: 0b302d707095fb1384c9ac848ed775cfb4d10313 [file] [log] [blame]
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001/*
2 * Add configfs and memory store: Kyungchan Koh <kkc6196@fb.com> and
3 * Shaohua Li <shli@fb.com>
4 */
Jens Axboef2298c02013-10-25 11:52:25 +01005#include <linux/module.h>
Matias Bjørlingfc1bc352013-12-21 00:11:01 +01006
Jens Axboef2298c02013-10-25 11:52:25 +01007#include <linux/moduleparam.h>
8#include <linux/sched.h>
9#include <linux/fs.h>
Jens Axboef2298c02013-10-25 11:52:25 +010010#include <linux/init.h>
Matias Bjørling6dad38d2018-07-06 19:38:38 +020011#include "null_blk.h"
Jens Axboef2298c02013-10-25 11:52:25 +010012
Shaohua Li5bcd0e02017-08-14 15:04:56 -070013#define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
14#define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
Shaohua Li5bcd0e02017-08-14 15:04:56 -070015#define SECTOR_MASK (PAGE_SECTORS - 1)
16
17#define FREE_BATCH 16
18
Shaohua Lieff2c4f2017-08-14 15:04:58 -070019#define TICKS_PER_SEC 50ULL
20#define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC)
21
Arnd Bergmann33f782c2018-01-11 11:31:25 +010022#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
Jens Axboe93b57042018-01-10 09:06:23 -070023static DECLARE_FAULT_ATTR(null_timeout_attr);
Jens Axboe24941b92018-02-28 09:18:57 -070024static DECLARE_FAULT_ATTR(null_requeue_attr);
Arnd Bergmann33f782c2018-01-11 11:31:25 +010025#endif
Jens Axboe93b57042018-01-10 09:06:23 -070026
Shaohua Lieff2c4f2017-08-14 15:04:58 -070027static inline u64 mb_per_tick(int mbps)
28{
29 return (1 << 20) / TICKS_PER_SEC * ((u64) mbps);
30}
Jens Axboef2298c02013-10-25 11:52:25 +010031
Shaohua Li3bf2bd22017-08-14 15:04:53 -070032/*
33 * Status flags for nullb_device.
34 *
35 * CONFIGURED: Device has been configured and turned on. Cannot reconfigure.
36 * UP: Device is currently on and visible in userspace.
Shaohua Lieff2c4f2017-08-14 15:04:58 -070037 * THROTTLED: Device is being throttled.
Shaohua Lideb78b42017-08-14 15:04:59 -070038 * CACHE: Device is using a write-back cache.
Shaohua Li3bf2bd22017-08-14 15:04:53 -070039 */
40enum nullb_device_flags {
41 NULLB_DEV_FL_CONFIGURED = 0,
42 NULLB_DEV_FL_UP = 1,
Shaohua Lieff2c4f2017-08-14 15:04:58 -070043 NULLB_DEV_FL_THROTTLED = 2,
Shaohua Lideb78b42017-08-14 15:04:59 -070044 NULLB_DEV_FL_CACHE = 3,
Shaohua Li3bf2bd22017-08-14 15:04:53 -070045};
46
Ming Lei66231ad2018-03-06 12:07:13 +080047#define MAP_SZ ((PAGE_SIZE >> SECTOR_SHIFT) + 2)
Shaohua Li5bcd0e02017-08-14 15:04:56 -070048/*
49 * nullb_page is a page in memory for nullb devices.
50 *
51 * @page: The page holding the data.
52 * @bitmap: The bitmap represents which sector in the page has data.
53 * Each bit represents one block size. For example, sector 8
54 * will use the 7th bit
Shaohua Lideb78b42017-08-14 15:04:59 -070055 * The highest 2 bits of bitmap are for special purpose. LOCK means the cache
56 * page is being flushing to storage. FREE means the cache page is freed and
57 * should be skipped from flushing to storage. Please see
58 * null_make_cache_space
Shaohua Li5bcd0e02017-08-14 15:04:56 -070059 */
60struct nullb_page {
61 struct page *page;
Ming Lei66231ad2018-03-06 12:07:13 +080062 DECLARE_BITMAP(bitmap, MAP_SZ);
Shaohua Li5bcd0e02017-08-14 15:04:56 -070063};
Ming Lei66231ad2018-03-06 12:07:13 +080064#define NULLB_PAGE_LOCK (MAP_SZ - 1)
65#define NULLB_PAGE_FREE (MAP_SZ - 2)
Shaohua Li5bcd0e02017-08-14 15:04:56 -070066
Jens Axboef2298c02013-10-25 11:52:25 +010067static LIST_HEAD(nullb_list);
68static struct mutex lock;
69static int null_major;
Shaohua Li94bc02e2017-08-14 15:04:55 -070070static DEFINE_IDA(nullb_indexes);
Jens Axboe82f402f2017-06-20 14:22:01 -060071static struct blk_mq_tag_set tag_set;
Jens Axboef2298c02013-10-25 11:52:25 +010072
Jens Axboef2298c02013-10-25 11:52:25 +010073enum {
74 NULL_IRQ_NONE = 0,
75 NULL_IRQ_SOFTIRQ = 1,
76 NULL_IRQ_TIMER = 2,
Christoph Hellwigce2c3502014-02-10 03:24:40 -080077};
Jens Axboef2298c02013-10-25 11:52:25 +010078
Christoph Hellwigce2c3502014-02-10 03:24:40 -080079enum {
Jens Axboef2298c02013-10-25 11:52:25 +010080 NULL_Q_BIO = 0,
81 NULL_Q_RQ = 1,
82 NULL_Q_MQ = 2,
83};
84
weiping zhangb3cffc32017-09-30 09:49:21 +080085static int g_no_sched;
Joe Perches5657a812018-05-24 13:38:59 -060086module_param_named(no_sched, g_no_sched, int, 0444);
weiping zhangb3cffc32017-09-30 09:49:21 +080087MODULE_PARM_DESC(no_sched, "No io scheduler");
88
Shaohua Li2984c862017-08-14 15:04:52 -070089static int g_submit_queues = 1;
Joe Perches5657a812018-05-24 13:38:59 -060090module_param_named(submit_queues, g_submit_queues, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +010091MODULE_PARM_DESC(submit_queues, "Number of submission queues");
92
Shaohua Li2984c862017-08-14 15:04:52 -070093static int g_home_node = NUMA_NO_NODE;
Joe Perches5657a812018-05-24 13:38:59 -060094module_param_named(home_node, g_home_node, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +010095MODULE_PARM_DESC(home_node, "Home node for the device");
96
Arnd Bergmann33f782c2018-01-11 11:31:25 +010097#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
Jens Axboe93b57042018-01-10 09:06:23 -070098static char g_timeout_str[80];
Joe Perches5657a812018-05-24 13:38:59 -060099module_param_string(timeout, g_timeout_str, sizeof(g_timeout_str), 0444);
Jens Axboe24941b92018-02-28 09:18:57 -0700100
101static char g_requeue_str[80];
Joe Perches5657a812018-05-24 13:38:59 -0600102module_param_string(requeue, g_requeue_str, sizeof(g_requeue_str), 0444);
Arnd Bergmann33f782c2018-01-11 11:31:25 +0100103#endif
Jens Axboe93b57042018-01-10 09:06:23 -0700104
Shaohua Li2984c862017-08-14 15:04:52 -0700105static int g_queue_mode = NULL_Q_MQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700106
107static int null_param_store_val(const char *str, int *val, int min, int max)
108{
109 int ret, new_val;
110
111 ret = kstrtoint(str, 10, &new_val);
112 if (ret)
113 return -EINVAL;
114
115 if (new_val < min || new_val > max)
116 return -EINVAL;
117
118 *val = new_val;
119 return 0;
120}
121
122static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
123{
Shaohua Li2984c862017-08-14 15:04:52 -0700124 return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
Matias Bjorling709c8662014-11-26 14:45:48 -0700125}
126
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930127static const struct kernel_param_ops null_queue_mode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700128 .set = null_set_queue_mode,
129 .get = param_get_int,
130};
131
Joe Perches5657a812018-05-24 13:38:59 -0600132device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, 0444);
Mike Snitzer54ae81c2014-06-11 17:13:50 -0400133MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
Jens Axboef2298c02013-10-25 11:52:25 +0100134
Shaohua Li2984c862017-08-14 15:04:52 -0700135static int g_gb = 250;
Joe Perches5657a812018-05-24 13:38:59 -0600136module_param_named(gb, g_gb, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100137MODULE_PARM_DESC(gb, "Size in GB");
138
Shaohua Li2984c862017-08-14 15:04:52 -0700139static int g_bs = 512;
Joe Perches5657a812018-05-24 13:38:59 -0600140module_param_named(bs, g_bs, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100141MODULE_PARM_DESC(bs, "Block size (in bytes)");
142
Jens Axboe82f402f2017-06-20 14:22:01 -0600143static int nr_devices = 1;
Joe Perches5657a812018-05-24 13:38:59 -0600144module_param(nr_devices, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100145MODULE_PARM_DESC(nr_devices, "Number of devices to register");
146
Shaohua Li2984c862017-08-14 15:04:52 -0700147static bool g_blocking;
Joe Perches5657a812018-05-24 13:38:59 -0600148module_param_named(blocking, g_blocking, bool, 0444);
Jens Axboedb5bcf82017-03-30 13:44:26 -0600149MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
150
Jens Axboe82f402f2017-06-20 14:22:01 -0600151static bool shared_tags;
Joe Perches5657a812018-05-24 13:38:59 -0600152module_param(shared_tags, bool, 0444);
Jens Axboe82f402f2017-06-20 14:22:01 -0600153MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");
154
Shaohua Li2984c862017-08-14 15:04:52 -0700155static int g_irqmode = NULL_IRQ_SOFTIRQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700156
157static int null_set_irqmode(const char *str, const struct kernel_param *kp)
158{
Shaohua Li2984c862017-08-14 15:04:52 -0700159 return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
Matias Bjorling709c8662014-11-26 14:45:48 -0700160 NULL_IRQ_TIMER);
161}
162
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930163static const struct kernel_param_ops null_irqmode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700164 .set = null_set_irqmode,
165 .get = param_get_int,
166};
167
Joe Perches5657a812018-05-24 13:38:59 -0600168device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100169MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
170
Shaohua Li2984c862017-08-14 15:04:52 -0700171static unsigned long g_completion_nsec = 10000;
Joe Perches5657a812018-05-24 13:38:59 -0600172module_param_named(completion_nsec, g_completion_nsec, ulong, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100173MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
174
Shaohua Li2984c862017-08-14 15:04:52 -0700175static int g_hw_queue_depth = 64;
Joe Perches5657a812018-05-24 13:38:59 -0600176module_param_named(hw_queue_depth, g_hw_queue_depth, int, 0444);
Jens Axboef2298c02013-10-25 11:52:25 +0100177MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
178
Shaohua Li2984c862017-08-14 15:04:52 -0700179static bool g_use_per_node_hctx;
Joe Perches5657a812018-05-24 13:38:59 -0600180module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, 0444);
Matias Bjørling20005242013-12-21 00:11:00 +0100181MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
Jens Axboef2298c02013-10-25 11:52:25 +0100182
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200183static bool g_zoned;
184module_param_named(zoned, g_zoned, bool, S_IRUGO);
185MODULE_PARM_DESC(zoned, "Make device as a host-managed zoned block device. Default: false");
186
187static unsigned long g_zone_size = 256;
188module_param_named(zone_size, g_zone_size, ulong, S_IRUGO);
189MODULE_PARM_DESC(zone_size, "Zone size in MB when block device is zoned. Must be power-of-two: Default: 256");
190
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700191static struct nullb_device *null_alloc_dev(void);
192static void null_free_dev(struct nullb_device *dev);
Shaohua Licedcafa2017-08-14 15:04:54 -0700193static void null_del_dev(struct nullb *nullb);
194static int null_add_dev(struct nullb_device *dev);
Shaohua Lideb78b42017-08-14 15:04:59 -0700195static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700196
197static inline struct nullb_device *to_nullb_device(struct config_item *item)
198{
199 return item ? container_of(item, struct nullb_device, item) : NULL;
200}
201
202static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page)
203{
204 return snprintf(page, PAGE_SIZE, "%u\n", val);
205}
206
207static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
208 char *page)
209{
210 return snprintf(page, PAGE_SIZE, "%lu\n", val);
211}
212
213static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
214{
215 return snprintf(page, PAGE_SIZE, "%u\n", val);
216}
217
218static ssize_t nullb_device_uint_attr_store(unsigned int *val,
219 const char *page, size_t count)
220{
221 unsigned int tmp;
222 int result;
223
224 result = kstrtouint(page, 0, &tmp);
225 if (result)
226 return result;
227
228 *val = tmp;
229 return count;
230}
231
232static ssize_t nullb_device_ulong_attr_store(unsigned long *val,
233 const char *page, size_t count)
234{
235 int result;
236 unsigned long tmp;
237
238 result = kstrtoul(page, 0, &tmp);
239 if (result)
240 return result;
241
242 *val = tmp;
243 return count;
244}
245
246static ssize_t nullb_device_bool_attr_store(bool *val, const char *page,
247 size_t count)
248{
249 bool tmp;
250 int result;
251
252 result = kstrtobool(page, &tmp);
253 if (result)
254 return result;
255
256 *val = tmp;
257 return count;
258}
259
260/* The following macro should only be used with TYPE = {uint, ulong, bool}. */
261#define NULLB_DEVICE_ATTR(NAME, TYPE) \
262static ssize_t \
263nullb_device_##NAME##_show(struct config_item *item, char *page) \
264{ \
265 return nullb_device_##TYPE##_attr_show( \
266 to_nullb_device(item)->NAME, page); \
267} \
268static ssize_t \
269nullb_device_##NAME##_store(struct config_item *item, const char *page, \
270 size_t count) \
271{ \
272 if (test_bit(NULLB_DEV_FL_CONFIGURED, &to_nullb_device(item)->flags)) \
273 return -EBUSY; \
274 return nullb_device_##TYPE##_attr_store( \
275 &to_nullb_device(item)->NAME, page, count); \
276} \
277CONFIGFS_ATTR(nullb_device_, NAME);
278
279NULLB_DEVICE_ATTR(size, ulong);
280NULLB_DEVICE_ATTR(completion_nsec, ulong);
281NULLB_DEVICE_ATTR(submit_queues, uint);
282NULLB_DEVICE_ATTR(home_node, uint);
283NULLB_DEVICE_ATTR(queue_mode, uint);
284NULLB_DEVICE_ATTR(blocksize, uint);
285NULLB_DEVICE_ATTR(irqmode, uint);
286NULLB_DEVICE_ATTR(hw_queue_depth, uint);
Shaohua Licedcafa2017-08-14 15:04:54 -0700287NULLB_DEVICE_ATTR(index, uint);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700288NULLB_DEVICE_ATTR(blocking, bool);
289NULLB_DEVICE_ATTR(use_per_node_hctx, bool);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700290NULLB_DEVICE_ATTR(memory_backed, bool);
Shaohua Li306eb6b2017-08-14 15:04:57 -0700291NULLB_DEVICE_ATTR(discard, bool);
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700292NULLB_DEVICE_ATTR(mbps, uint);
Shaohua Lideb78b42017-08-14 15:04:59 -0700293NULLB_DEVICE_ATTR(cache_size, ulong);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200294NULLB_DEVICE_ATTR(zoned, bool);
295NULLB_DEVICE_ATTR(zone_size, ulong);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700296
Shaohua Licedcafa2017-08-14 15:04:54 -0700297static ssize_t nullb_device_power_show(struct config_item *item, char *page)
298{
299 return nullb_device_bool_attr_show(to_nullb_device(item)->power, page);
300}
301
302static ssize_t nullb_device_power_store(struct config_item *item,
303 const char *page, size_t count)
304{
305 struct nullb_device *dev = to_nullb_device(item);
306 bool newp = false;
307 ssize_t ret;
308
309 ret = nullb_device_bool_attr_store(&newp, page, count);
310 if (ret < 0)
311 return ret;
312
313 if (!dev->power && newp) {
314 if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
315 return count;
316 if (null_add_dev(dev)) {
317 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
318 return -ENOMEM;
319 }
320
321 set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
322 dev->power = newp;
Jens Axboeb3c30512017-08-28 15:06:31 -0600323 } else if (dev->power && !newp) {
Shaohua Licedcafa2017-08-14 15:04:54 -0700324 mutex_lock(&lock);
325 dev->power = newp;
326 null_del_dev(dev->nullb);
327 mutex_unlock(&lock);
328 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
Liu Bo00a8cdb2018-07-06 03:07:13 +0800329 clear_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
Shaohua Licedcafa2017-08-14 15:04:54 -0700330 }
331
332 return count;
333}
334
335CONFIGFS_ATTR(nullb_device_, power);
336
Shaohua Li2f54a612017-08-14 15:05:00 -0700337static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page)
338{
339 struct nullb_device *t_dev = to_nullb_device(item);
340
341 return badblocks_show(&t_dev->badblocks, page, 0);
342}
343
344static ssize_t nullb_device_badblocks_store(struct config_item *item,
345 const char *page, size_t count)
346{
347 struct nullb_device *t_dev = to_nullb_device(item);
348 char *orig, *buf, *tmp;
349 u64 start, end;
350 int ret;
351
352 orig = kstrndup(page, count, GFP_KERNEL);
353 if (!orig)
354 return -ENOMEM;
355
356 buf = strstrip(orig);
357
358 ret = -EINVAL;
359 if (buf[0] != '+' && buf[0] != '-')
360 goto out;
361 tmp = strchr(&buf[1], '-');
362 if (!tmp)
363 goto out;
364 *tmp = '\0';
365 ret = kstrtoull(buf + 1, 0, &start);
366 if (ret)
367 goto out;
368 ret = kstrtoull(tmp + 1, 0, &end);
369 if (ret)
370 goto out;
371 ret = -EINVAL;
372 if (start > end)
373 goto out;
374 /* enable badblocks */
375 cmpxchg(&t_dev->badblocks.shift, -1, 0);
376 if (buf[0] == '+')
377 ret = badblocks_set(&t_dev->badblocks, start,
378 end - start + 1, 1);
379 else
380 ret = badblocks_clear(&t_dev->badblocks, start,
381 end - start + 1);
382 if (ret == 0)
383 ret = count;
384out:
385 kfree(orig);
386 return ret;
387}
388CONFIGFS_ATTR(nullb_device_, badblocks);
389
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700390static struct configfs_attribute *nullb_device_attrs[] = {
391 &nullb_device_attr_size,
392 &nullb_device_attr_completion_nsec,
393 &nullb_device_attr_submit_queues,
394 &nullb_device_attr_home_node,
395 &nullb_device_attr_queue_mode,
396 &nullb_device_attr_blocksize,
397 &nullb_device_attr_irqmode,
398 &nullb_device_attr_hw_queue_depth,
Shaohua Licedcafa2017-08-14 15:04:54 -0700399 &nullb_device_attr_index,
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700400 &nullb_device_attr_blocking,
401 &nullb_device_attr_use_per_node_hctx,
Shaohua Licedcafa2017-08-14 15:04:54 -0700402 &nullb_device_attr_power,
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700403 &nullb_device_attr_memory_backed,
Shaohua Li306eb6b2017-08-14 15:04:57 -0700404 &nullb_device_attr_discard,
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700405 &nullb_device_attr_mbps,
Shaohua Lideb78b42017-08-14 15:04:59 -0700406 &nullb_device_attr_cache_size,
Shaohua Li2f54a612017-08-14 15:05:00 -0700407 &nullb_device_attr_badblocks,
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200408 &nullb_device_attr_zoned,
409 &nullb_device_attr_zone_size,
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700410 NULL,
411};
412
413static void nullb_device_release(struct config_item *item)
414{
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700415 struct nullb_device *dev = to_nullb_device(item);
416
Shaohua Lideb78b42017-08-14 15:04:59 -0700417 null_free_device_storage(dev, false);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700418 null_free_dev(dev);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700419}
420
421static struct configfs_item_operations nullb_device_ops = {
422 .release = nullb_device_release,
423};
424
Bhumika Goyale1919df2017-10-16 17:18:49 +0200425static const struct config_item_type nullb_device_type = {
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700426 .ct_item_ops = &nullb_device_ops,
427 .ct_attrs = nullb_device_attrs,
428 .ct_owner = THIS_MODULE,
429};
430
431static struct
432config_item *nullb_group_make_item(struct config_group *group, const char *name)
433{
434 struct nullb_device *dev;
435
436 dev = null_alloc_dev();
437 if (!dev)
438 return ERR_PTR(-ENOMEM);
439
440 config_item_init_type_name(&dev->item, name, &nullb_device_type);
441
442 return &dev->item;
443}
444
445static void
446nullb_group_drop_item(struct config_group *group, struct config_item *item)
447{
Shaohua Licedcafa2017-08-14 15:04:54 -0700448 struct nullb_device *dev = to_nullb_device(item);
449
450 if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
451 mutex_lock(&lock);
452 dev->power = false;
453 null_del_dev(dev->nullb);
454 mutex_unlock(&lock);
455 }
456
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700457 config_item_put(item);
458}
459
460static ssize_t memb_group_features_show(struct config_item *item, char *page)
461{
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200462 return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache,badblocks,zoned,zone_size\n");
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700463}
464
465CONFIGFS_ATTR_RO(memb_group_, features);
466
467static struct configfs_attribute *nullb_group_attrs[] = {
468 &memb_group_attr_features,
469 NULL,
470};
471
472static struct configfs_group_operations nullb_group_ops = {
473 .make_item = nullb_group_make_item,
474 .drop_item = nullb_group_drop_item,
475};
476
Bhumika Goyale1919df2017-10-16 17:18:49 +0200477static const struct config_item_type nullb_group_type = {
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700478 .ct_group_ops = &nullb_group_ops,
479 .ct_attrs = nullb_group_attrs,
480 .ct_owner = THIS_MODULE,
481};
482
483static struct configfs_subsystem nullb_subsys = {
484 .su_group = {
485 .cg_item = {
486 .ci_namebuf = "nullb",
487 .ci_type = &nullb_group_type,
488 },
489 },
490};
491
Shaohua Lideb78b42017-08-14 15:04:59 -0700492static inline int null_cache_active(struct nullb *nullb)
493{
494 return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
495}
496
Shaohua Li2984c862017-08-14 15:04:52 -0700497static struct nullb_device *null_alloc_dev(void)
498{
499 struct nullb_device *dev;
500
501 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
502 if (!dev)
503 return NULL;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700504 INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
Shaohua Lideb78b42017-08-14 15:04:59 -0700505 INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
Shaohua Li2f54a612017-08-14 15:05:00 -0700506 if (badblocks_init(&dev->badblocks, 0)) {
507 kfree(dev);
508 return NULL;
509 }
510
Shaohua Li2984c862017-08-14 15:04:52 -0700511 dev->size = g_gb * 1024;
512 dev->completion_nsec = g_completion_nsec;
513 dev->submit_queues = g_submit_queues;
514 dev->home_node = g_home_node;
515 dev->queue_mode = g_queue_mode;
516 dev->blocksize = g_bs;
517 dev->irqmode = g_irqmode;
518 dev->hw_queue_depth = g_hw_queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -0700519 dev->blocking = g_blocking;
520 dev->use_per_node_hctx = g_use_per_node_hctx;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200521 dev->zoned = g_zoned;
522 dev->zone_size = g_zone_size;
Shaohua Li2984c862017-08-14 15:04:52 -0700523 return dev;
524}
525
526static void null_free_dev(struct nullb_device *dev)
527{
David Disseldorp1addb792017-11-08 17:29:44 +0100528 if (!dev)
529 return;
530
Matias Bjørlingca4b2a02018-07-06 19:38:39 +0200531 null_zone_exit(dev);
David Disseldorp1addb792017-11-08 17:29:44 +0100532 badblocks_exit(&dev->badblocks);
Shaohua Li2984c862017-08-14 15:04:52 -0700533 kfree(dev);
534}
535
Jens Axboef2298c02013-10-25 11:52:25 +0100536static void put_tag(struct nullb_queue *nq, unsigned int tag)
537{
538 clear_bit_unlock(tag, nq->tag_map);
539
540 if (waitqueue_active(&nq->wait))
541 wake_up(&nq->wait);
542}
543
544static unsigned int get_tag(struct nullb_queue *nq)
545{
546 unsigned int tag;
547
548 do {
549 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
550 if (tag >= nq->queue_depth)
551 return -1U;
552 } while (test_and_set_bit_lock(tag, nq->tag_map));
553
554 return tag;
555}
556
557static void free_cmd(struct nullb_cmd *cmd)
558{
559 put_tag(cmd->nq, cmd->tag);
560}
561
Paolo Valente3c395a92015-12-01 11:48:17 +0100562static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
563
Jens Axboef2298c02013-10-25 11:52:25 +0100564static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
565{
566 struct nullb_cmd *cmd;
567 unsigned int tag;
568
569 tag = get_tag(nq);
570 if (tag != -1U) {
571 cmd = &nq->cmds[tag];
572 cmd->tag = tag;
573 cmd->nq = nq;
Shaohua Li2984c862017-08-14 15:04:52 -0700574 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +0100575 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
576 HRTIMER_MODE_REL);
577 cmd->timer.function = null_cmd_timer_expired;
578 }
Jens Axboef2298c02013-10-25 11:52:25 +0100579 return cmd;
580 }
581
582 return NULL;
583}
584
585static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
586{
587 struct nullb_cmd *cmd;
588 DEFINE_WAIT(wait);
589
590 cmd = __alloc_cmd(nq);
591 if (cmd || !can_wait)
592 return cmd;
593
594 do {
595 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
596 cmd = __alloc_cmd(nq);
597 if (cmd)
598 break;
599
600 io_schedule();
601 } while (1);
602
603 finish_wait(&nq->wait, &wait);
604 return cmd;
605}
606
607static void end_cmd(struct nullb_cmd *cmd)
608{
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100609 struct request_queue *q = NULL;
Shaohua Li2984c862017-08-14 15:04:52 -0700610 int queue_mode = cmd->nq->dev->queue_mode;
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100611
Mike Krinkine8271202015-12-15 12:56:40 +0300612 if (cmd->rq)
613 q = cmd->rq->q;
614
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800615 switch (queue_mode) {
616 case NULL_Q_MQ:
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700617 blk_mq_end_request(cmd->rq, cmd->error);
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800618 return;
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800619 case NULL_Q_BIO:
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700620 cmd->bio->bi_status = cmd->error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200621 bio_endio(cmd->bio);
Jens Axboe48cc6612015-12-28 13:02:47 -0700622 break;
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800623 }
Jens Axboef2298c02013-10-25 11:52:25 +0100624
Jens Axboe48cc6612015-12-28 13:02:47 -0700625 free_cmd(cmd);
Jens Axboef2298c02013-10-25 11:52:25 +0100626}
627
628static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
629{
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100630 end_cmd(container_of(timer, struct nullb_cmd, timer));
Jens Axboef2298c02013-10-25 11:52:25 +0100631
632 return HRTIMER_NORESTART;
633}
634
635static void null_cmd_end_timer(struct nullb_cmd *cmd)
636{
Shaohua Li2984c862017-08-14 15:04:52 -0700637 ktime_t kt = cmd->nq->dev->completion_nsec;
Jens Axboef2298c02013-10-25 11:52:25 +0100638
Paolo Valente3c395a92015-12-01 11:48:17 +0100639 hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
Jens Axboef2298c02013-10-25 11:52:25 +0100640}
641
642static void null_softirq_done_fn(struct request *rq)
643{
Shaohua Li2984c862017-08-14 15:04:52 -0700644 struct nullb *nullb = rq->q->queuedata;
645
646 if (nullb->dev->queue_mode == NULL_Q_MQ)
Jens Axboed891fa72014-06-16 11:40:25 -0600647 end_cmd(blk_mq_rq_to_pdu(rq));
648 else
649 end_cmd(rq->special);
Jens Axboef2298c02013-10-25 11:52:25 +0100650}
651
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700652static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
Jens Axboef2298c02013-10-25 11:52:25 +0100653{
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700654 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 Lei66231ad2018-03-06 12:07:13 +0800664 memset(t_page->bitmap, 0, sizeof(t_page->bitmap));
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700665 return t_page;
666out_freepage:
667 kfree(t_page);
668out:
669 return NULL;
670}
671
672static void null_free_page(struct nullb_page *t_page)
673{
Ming Lei66231ad2018-03-06 12:07:13 +0800674 __set_bit(NULLB_PAGE_FREE, t_page->bitmap);
675 if (test_bit(NULLB_PAGE_LOCK, t_page->bitmap))
Shaohua Lideb78b42017-08-14 15:04:59 -0700676 return;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700677 __free_page(t_page->page);
678 kfree(t_page);
679}
680
Ming Lei66231ad2018-03-06 12:07:13 +0800681static 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 Lideb78b42017-08-14 15:04:59 -0700688static void null_free_sector(struct nullb *nullb, sector_t sector,
689 bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700690{
691 unsigned int sector_bit;
692 u64 idx;
693 struct nullb_page *t_page, *ret;
694 struct radix_tree_root *root;
695
Shaohua Lideb78b42017-08-14 15:04:59 -0700696 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700697 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 Lei66231ad2018-03-06 12:07:13 +0800702 __clear_bit(sector_bit, t_page->bitmap);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700703
Ming Lei66231ad2018-03-06 12:07:13 +0800704 if (null_page_empty(t_page)) {
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700705 ret = radix_tree_delete_item(root, idx, t_page);
706 WARN_ON(ret != t_page);
707 null_free_page(ret);
Shaohua Lideb78b42017-08-14 15:04:59 -0700708 if (is_cache)
709 nullb->dev->curr_cache -= PAGE_SIZE;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700710 }
711 }
712}
713
714static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
Shaohua Lideb78b42017-08-14 15:04:59 -0700715 struct nullb_page *t_page, bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700716{
717 struct radix_tree_root *root;
718
Shaohua Lideb78b42017-08-14 15:04:59 -0700719 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700720
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 Lideb78b42017-08-14 15:04:59 -0700725 } else if (is_cache)
726 nullb->dev->curr_cache += PAGE_SIZE;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700727
728 return t_page;
729}
730
Shaohua Lideb78b42017-08-14 15:04:59 -0700731static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700732{
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 Lideb78b42017-08-14 15:04:59 -0700738 root = is_cache ? &dev->cache : &dev->data;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700739
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 Lideb78b42017-08-14 15:04:59 -0700755
756 if (is_cache)
757 dev->curr_cache = 0;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700758}
759
Shaohua Lideb78b42017-08-14 15:04:59 -0700760static struct nullb_page *__null_lookup_page(struct nullb *nullb,
761 sector_t sector, bool for_write, bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700762{
763 unsigned int sector_bit;
764 u64 idx;
765 struct nullb_page *t_page;
Shaohua Lideb78b42017-08-14 15:04:59 -0700766 struct radix_tree_root *root;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700767
768 idx = sector >> PAGE_SECTORS_SHIFT;
769 sector_bit = (sector & SECTOR_MASK);
770
Shaohua Lideb78b42017-08-14 15:04:59 -0700771 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
772 t_page = radix_tree_lookup(root, idx);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700773 WARN_ON(t_page && t_page->page->index != idx);
774
Ming Lei66231ad2018-03-06 12:07:13 +0800775 if (t_page && (for_write || test_bit(sector_bit, t_page->bitmap)))
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700776 return t_page;
777
778 return NULL;
779}
780
Shaohua Lideb78b42017-08-14 15:04:59 -0700781static 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 Li5bcd0e02017-08-14 15:04:56 -0700793static struct nullb_page *null_insert_page(struct nullb *nullb,
Jens Axboe61884de2018-08-09 14:22:41 -0600794 sector_t sector, bool ignore_cache)
795 __releases(&nullb->lock)
796 __acquires(&nullb->lock)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700797{
798 u64 idx;
799 struct nullb_page *t_page;
800
Shaohua Lideb78b42017-08-14 15:04:59 -0700801 t_page = null_lookup_page(nullb, sector, true, ignore_cache);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700802 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 Lideb78b42017-08-14 15:04:59 -0700817 t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700818 radix_tree_preload_end();
819
820 return t_page;
821out_freepage:
822 null_free_page(t_page);
823out_lock:
824 spin_lock_irq(&nullb->lock);
Shaohua Lideb78b42017-08-14 15:04:59 -0700825 return null_lookup_page(nullb, sector, true, ignore_cache);
826}
827
828static 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 Lei66231ad2018-03-06 12:07:13 +0800840 __clear_bit(NULLB_PAGE_LOCK, c_page->bitmap);
841 if (test_bit(NULLB_PAGE_FREE, c_page->bitmap)) {
Shaohua Lideb78b42017-08-14 15:04:59 -0700842 null_free_page(c_page);
Ming Lei66231ad2018-03-06 12:07:13 +0800843 if (t_page && null_page_empty(t_page)) {
Shaohua Lideb78b42017-08-14 15:04:59 -0700844 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 Lei66231ad2018-03-06 12:07:13 +0800859 if (test_bit(i, c_page->bitmap)) {
Shaohua Lideb78b42017-08-14 15:04:59 -0700860 offset = (i << SECTOR_SHIFT);
861 memcpy(dst + offset, src + offset,
862 nullb->dev->blocksize);
Ming Lei66231ad2018-03-06 12:07:13 +0800863 __set_bit(i, t_page->bitmap);
Shaohua Lideb78b42017-08-14 15:04:59 -0700864 }
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
877static 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
883again:
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 Lei66231ad2018-03-06 12:07:13 +0800900 if (test_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap))
Shaohua Lideb78b42017-08-14 15:04:59 -0700901 c_pages[i] = NULL;
902 else
Ming Lei66231ad2018-03-06 12:07:13 +0800903 __set_bit(NULLB_PAGE_LOCK, c_pages[i]->bitmap);
Shaohua Lideb78b42017-08-14 15:04:59 -0700904 }
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 Li5bcd0e02017-08-14 15:04:56 -0700928}
929
930static int copy_to_nullb(struct nullb *nullb, struct page *source,
Shaohua Lideb78b42017-08-14 15:04:59 -0700931 unsigned int off, sector_t sector, size_t n, bool is_fua)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700932{
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 Lideb78b42017-08-14 15:04:59 -0700941 if (null_cache_active(nullb) && !is_fua)
942 null_make_cache_space(nullb, PAGE_SIZE);
943
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700944 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
Shaohua Lideb78b42017-08-14 15:04:59 -0700945 t_page = null_insert_page(nullb, sector,
946 !null_cache_active(nullb) || is_fua);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700947 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 Lei66231ad2018-03-06 12:07:13 +0800956 __set_bit(sector & SECTOR_MASK, t_page->bitmap);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700957
Shaohua Lideb78b42017-08-14 15:04:59 -0700958 if (is_fua)
959 null_free_sector(nullb, sector, true);
960
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700961 count += temp;
962 sector += temp >> SECTOR_SHIFT;
963 }
964 return 0;
965}
966
967static 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 Lideb78b42017-08-14 15:04:59 -0700979 t_page = null_lookup_page(nullb, sector, false,
980 !null_cache_active(nullb));
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700981
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);
990next:
991 kunmap_atomic(dst);
992
993 count += temp;
994 sector += temp >> SECTOR_SHIFT;
995 }
996 return 0;
997}
998
Shaohua Li306eb6b2017-08-14 15:04:57 -0700999static 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 Lideb78b42017-08-14 15:04:59 -07001006 null_free_sector(nullb, sector, false);
1007 if (null_cache_active(nullb))
1008 null_free_sector(nullb, sector, true);
Shaohua Li306eb6b2017-08-14 15:04:57 -07001009 sector += temp >> SECTOR_SHIFT;
1010 n -= temp;
1011 }
1012 spin_unlock_irq(&nullb->lock);
1013}
1014
Shaohua Lideb78b42017-08-14 15:04:59 -07001015static 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 Li5bcd0e02017-08-14 15:04:56 -07001035static int null_transfer(struct nullb *nullb, struct page *page,
Shaohua Lideb78b42017-08-14 15:04:59 -07001036 unsigned int len, unsigned int off, bool is_write, sector_t sector,
1037 bool is_fua)
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001038{
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 Lideb78b42017-08-14 15:04:59 -07001046 err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001047 }
1048
1049 return err;
1050}
1051
1052static 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 Li306eb6b2017-08-14 15:04:57 -07001064 if (req_op(rq) == REQ_OP_DISCARD) {
1065 null_handle_discard(nullb, sector, blk_rq_bytes(rq));
1066 return 0;
1067 }
1068
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001069 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 Lideb78b42017-08-14 15:04:59 -07001073 op_is_write(req_op(rq)), sector,
1074 req_op(rq) & REQ_FUA);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001075 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
1086static 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 Li306eb6b2017-08-14 15:04:57 -07001098 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 Li5bcd0e02017-08-14 15:04:56 -07001104 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 Lideb78b42017-08-14 15:04:59 -07001108 op_is_write(bio_op(bio)), sector,
1109 bio_op(bio) & REQ_FUA);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001110 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 Lieff2c4f2017-08-14 15:04:58 -07001120static 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 Lieff2c4f2017-08-14 15:04:58 -07001126}
1127
1128static void null_restart_queue_async(struct nullb *nullb)
1129{
1130 struct request_queue *q = nullb->q;
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001131
1132 if (nullb->dev->queue_mode == NULL_Q_MQ)
1133 blk_mq_start_stopped_hw_queues(q, true);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001134}
1135
Jens Axboeb228ba12018-09-12 18:21:11 -06001136static bool cmd_report_zone(struct nullb *nullb, struct nullb_cmd *cmd)
1137{
1138 struct nullb_device *dev = cmd->nq->dev;
1139
1140 if (dev->queue_mode == NULL_Q_BIO) {
1141 if (bio_op(cmd->bio) == REQ_OP_ZONE_REPORT) {
1142 cmd->error = null_zone_report(nullb, cmd->bio);
1143 return true;
1144 }
1145 } else {
1146 if (req_op(cmd->rq) == REQ_OP_ZONE_REPORT) {
1147 cmd->error = null_zone_report(nullb, cmd->rq->bio);
1148 return true;
1149 }
1150 }
1151
1152 return false;
1153}
1154
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001155static blk_status_t null_handle_cmd(struct nullb_cmd *cmd)
1156{
1157 struct nullb_device *dev = cmd->nq->dev;
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001158 struct nullb *nullb = dev->nullb;
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001159 int err = 0;
1160
Jens Axboeb228ba12018-09-12 18:21:11 -06001161 if (cmd_report_zone(nullb, cmd))
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001162 goto out;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001163
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001164 if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) {
1165 struct request *rq = cmd->rq;
1166
1167 if (!hrtimer_active(&nullb->bw_timer))
1168 hrtimer_restart(&nullb->bw_timer);
1169
1170 if (atomic_long_sub_return(blk_rq_bytes(rq),
1171 &nullb->cur_bytes) < 0) {
1172 null_stop_queue(nullb);
1173 /* race with timer */
1174 if (atomic_long_read(&nullb->cur_bytes) > 0)
1175 null_restart_queue_async(nullb);
Jens Axboee50b1e32018-10-11 17:58:17 -06001176 /* requeue request */
1177 return BLK_STS_DEV_RESOURCE;
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001178 }
1179 }
1180
Shaohua Li2f54a612017-08-14 15:05:00 -07001181 if (nullb->dev->badblocks.shift != -1) {
1182 int bad_sectors;
1183 sector_t sector, size, first_bad;
1184 bool is_flush = true;
1185
1186 if (dev->queue_mode == NULL_Q_BIO &&
1187 bio_op(cmd->bio) != REQ_OP_FLUSH) {
1188 is_flush = false;
1189 sector = cmd->bio->bi_iter.bi_sector;
1190 size = bio_sectors(cmd->bio);
1191 }
1192 if (dev->queue_mode != NULL_Q_BIO &&
1193 req_op(cmd->rq) != REQ_OP_FLUSH) {
1194 is_flush = false;
1195 sector = blk_rq_pos(cmd->rq);
1196 size = blk_rq_sectors(cmd->rq);
1197 }
1198 if (!is_flush && badblocks_check(&nullb->dev->badblocks, sector,
1199 size, &first_bad, &bad_sectors)) {
1200 cmd->error = BLK_STS_IOERR;
1201 goto out;
1202 }
1203 }
1204
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001205 if (dev->memory_backed) {
Shaohua Lideb78b42017-08-14 15:04:59 -07001206 if (dev->queue_mode == NULL_Q_BIO) {
1207 if (bio_op(cmd->bio) == REQ_OP_FLUSH)
1208 err = null_handle_flush(nullb);
1209 else
1210 err = null_handle_bio(cmd);
1211 } else {
1212 if (req_op(cmd->rq) == REQ_OP_FLUSH)
1213 err = null_handle_flush(nullb);
1214 else
1215 err = null_handle_rq(cmd);
1216 }
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001217 }
1218 cmd->error = errno_to_blk_status(err);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001219
1220 if (!cmd->error && dev->zoned) {
Jens Axboeb228ba12018-09-12 18:21:11 -06001221 sector_t sector;
1222 unsigned int nr_sectors;
1223 int op;
1224
1225 if (dev->queue_mode == NULL_Q_BIO) {
1226 op = bio_op(cmd->bio);
1227 sector = cmd->bio->bi_iter.bi_sector;
1228 nr_sectors = cmd->bio->bi_iter.bi_size >> 9;
1229 } else {
1230 op = req_op(cmd->rq);
1231 sector = blk_rq_pos(cmd->rq);
1232 nr_sectors = blk_rq_sectors(cmd->rq);
1233 }
1234
1235 if (op == REQ_OP_WRITE)
1236 null_zone_write(cmd, sector, nr_sectors);
1237 else if (op == REQ_OP_ZONE_RESET)
1238 null_zone_reset(cmd, sector);
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001239 }
Shaohua Li2f54a612017-08-14 15:05:00 -07001240out:
Jens Axboef2298c02013-10-25 11:52:25 +01001241 /* Complete IO by inline, softirq or timer */
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001242 switch (dev->irqmode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001243 case NULL_IRQ_SOFTIRQ:
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001244 switch (dev->queue_mode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001245 case NULL_Q_MQ:
Christoph Hellwig08e00292017-04-20 16:03:09 +02001246 blk_mq_complete_request(cmd->rq);
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001247 break;
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001248 case NULL_Q_BIO:
1249 /*
1250 * XXX: no proper submitting cpu information available.
1251 */
1252 end_cmd(cmd);
1253 break;
1254 }
1255 break;
Jens Axboef2298c02013-10-25 11:52:25 +01001256 case NULL_IRQ_NONE:
1257 end_cmd(cmd);
1258 break;
Jens Axboef2298c02013-10-25 11:52:25 +01001259 case NULL_IRQ_TIMER:
1260 null_cmd_end_timer(cmd);
1261 break;
1262 }
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001263 return BLK_STS_OK;
Jens Axboef2298c02013-10-25 11:52:25 +01001264}
1265
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001266static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
1267{
1268 struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
1269 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1270 unsigned int mbps = nullb->dev->mbps;
1271
1272 if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
1273 return HRTIMER_NORESTART;
1274
1275 atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
1276 null_restart_queue_async(nullb);
1277
1278 hrtimer_forward_now(&nullb->bw_timer, timer_interval);
1279
1280 return HRTIMER_RESTART;
1281}
1282
1283static void nullb_setup_bwtimer(struct nullb *nullb)
1284{
1285 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1286
1287 hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1288 nullb->bw_timer.function = nullb_bwtimer_fn;
1289 atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps));
1290 hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL);
Jens Axboef2298c02013-10-25 11:52:25 +01001291}
1292
1293static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
1294{
1295 int index = 0;
1296
1297 if (nullb->nr_queues != 1)
1298 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
1299
1300 return &nullb->queues[index];
1301}
1302
Jens Axboedece1632015-11-05 10:41:16 -07001303static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
Jens Axboef2298c02013-10-25 11:52:25 +01001304{
1305 struct nullb *nullb = q->queuedata;
1306 struct nullb_queue *nq = nullb_to_queue(nullb);
1307 struct nullb_cmd *cmd;
1308
1309 cmd = alloc_cmd(nq, 1);
1310 cmd->bio = bio;
1311
1312 null_handle_cmd(cmd);
Jens Axboedece1632015-11-05 10:41:16 -07001313 return BLK_QC_T_NONE;
Jens Axboef2298c02013-10-25 11:52:25 +01001314}
1315
Jens Axboe93b57042018-01-10 09:06:23 -07001316static bool should_timeout_request(struct request *rq)
1317{
Arnd Bergmann33f782c2018-01-11 11:31:25 +01001318#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
Jens Axboe93b57042018-01-10 09:06:23 -07001319 if (g_timeout_str[0])
1320 return should_fail(&null_timeout_attr, 1);
Arnd Bergmann33f782c2018-01-11 11:31:25 +01001321#endif
Jens Axboe24941b92018-02-28 09:18:57 -07001322 return false;
1323}
Jens Axboe93b57042018-01-10 09:06:23 -07001324
Jens Axboe24941b92018-02-28 09:18:57 -07001325static bool should_requeue_request(struct request *rq)
1326{
1327#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1328 if (g_requeue_str[0])
1329 return should_fail(&null_requeue_attr, 1);
1330#endif
Jens Axboe93b57042018-01-10 09:06:23 -07001331 return false;
1332}
1333
Jens Axboe5448aca2018-01-09 12:47:24 -07001334static enum blk_eh_timer_return null_timeout_rq(struct request *rq, bool res)
1335{
1336 pr_info("null: rq %p timed out\n", rq);
Christoph Hellwig0df0bb02018-05-29 15:52:33 +02001337 blk_mq_complete_request(rq);
1338 return BLK_EH_DONE;
Jens Axboe5448aca2018-01-09 12:47:24 -07001339}
1340
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001341static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -06001342 const struct blk_mq_queue_data *bd)
Jens Axboef2298c02013-10-25 11:52:25 +01001343{
Jens Axboe74c45052014-10-29 11:14:52 -06001344 struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Shaohua Li2984c862017-08-14 15:04:52 -07001345 struct nullb_queue *nq = hctx->driver_data;
Jens Axboef2298c02013-10-25 11:52:25 +01001346
Jens Axboedb5bcf82017-03-30 13:44:26 -06001347 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1348
Shaohua Li2984c862017-08-14 15:04:52 -07001349 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +01001350 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1351 cmd->timer.function = null_cmd_timer_expired;
1352 }
Jens Axboe74c45052014-10-29 11:14:52 -06001353 cmd->rq = bd->rq;
Shaohua Li2984c862017-08-14 15:04:52 -07001354 cmd->nq = nq;
Jens Axboef2298c02013-10-25 11:52:25 +01001355
Jens Axboe74c45052014-10-29 11:14:52 -06001356 blk_mq_start_request(bd->rq);
Christoph Hellwige2490072014-09-13 16:40:09 -07001357
Jens Axboe24941b92018-02-28 09:18:57 -07001358 if (should_requeue_request(bd->rq)) {
1359 /*
1360 * Alternate between hitting the core BUSY path, and the
1361 * driver driven requeue path
1362 */
1363 nq->requeue_selection++;
1364 if (nq->requeue_selection & 1)
1365 return BLK_STS_RESOURCE;
1366 else {
1367 blk_mq_requeue_request(bd->rq, true);
1368 return BLK_STS_OK;
1369 }
1370 }
1371 if (should_timeout_request(bd->rq))
1372 return BLK_STS_OK;
Jens Axboe93b57042018-01-10 09:06:23 -07001373
Jens Axboe24941b92018-02-28 09:18:57 -07001374 return null_handle_cmd(cmd);
Jens Axboef2298c02013-10-25 11:52:25 +01001375}
1376
Eric Biggersf363b082017-03-30 13:39:16 -07001377static const struct blk_mq_ops null_mq_ops = {
Jens Axboef2298c02013-10-25 11:52:25 +01001378 .queue_rq = null_queue_rq,
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001379 .complete = null_softirq_done_fn,
Jens Axboe5448aca2018-01-09 12:47:24 -07001380 .timeout = null_timeout_rq,
Jens Axboef2298c02013-10-25 11:52:25 +01001381};
1382
Matias Bjørlingde65d2d2015-08-31 14:17:18 +02001383static void cleanup_queue(struct nullb_queue *nq)
1384{
1385 kfree(nq->tag_map);
1386 kfree(nq->cmds);
1387}
1388
1389static void cleanup_queues(struct nullb *nullb)
1390{
1391 int i;
1392
1393 for (i = 0; i < nullb->nr_queues; i++)
1394 cleanup_queue(&nullb->queues[i]);
1395
1396 kfree(nullb->queues);
1397}
1398
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001399static void null_del_dev(struct nullb *nullb)
1400{
Shaohua Li2984c862017-08-14 15:04:52 -07001401 struct nullb_device *dev = nullb->dev;
1402
Shaohua Li94bc02e2017-08-14 15:04:55 -07001403 ida_simple_remove(&nullb_indexes, nullb->index);
1404
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001405 list_del_init(&nullb->list);
1406
Matias Bjørling74ede5a2018-01-05 14:15:57 +01001407 del_gendisk(nullb->disk);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001408
1409 if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
1410 hrtimer_cancel(&nullb->bw_timer);
1411 atomic_long_set(&nullb->cur_bytes, LONG_MAX);
1412 null_restart_queue_async(nullb);
1413 }
1414
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001415 blk_cleanup_queue(nullb->q);
Shaohua Li2984c862017-08-14 15:04:52 -07001416 if (dev->queue_mode == NULL_Q_MQ &&
1417 nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001418 blk_mq_free_tag_set(nullb->tag_set);
Matias Bjørling74ede5a2018-01-05 14:15:57 +01001419 put_disk(nullb->disk);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001420 cleanup_queues(nullb);
Shaohua Lideb78b42017-08-14 15:04:59 -07001421 if (null_cache_active(nullb))
1422 null_free_device_storage(nullb->dev, true);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001423 kfree(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001424 dev->nullb = NULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001425}
1426
Shaohua Li306eb6b2017-08-14 15:04:57 -07001427static void null_config_discard(struct nullb *nullb)
1428{
1429 if (nullb->dev->discard == false)
1430 return;
1431 nullb->q->limits.discard_granularity = nullb->dev->blocksize;
1432 nullb->q->limits.discard_alignment = nullb->dev->blocksize;
1433 blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001434 blk_queue_flag_set(QUEUE_FLAG_DISCARD, nullb->q);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001435}
1436
Jens Axboef2298c02013-10-25 11:52:25 +01001437static int null_open(struct block_device *bdev, fmode_t mode)
1438{
1439 return 0;
1440}
1441
1442static void null_release(struct gendisk *disk, fmode_t mode)
1443{
1444}
1445
1446static const struct block_device_operations null_fops = {
1447 .owner = THIS_MODULE,
1448 .open = null_open,
1449 .release = null_release,
1450};
1451
Jens Axboe82f402f2017-06-20 14:22:01 -06001452static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
1453{
1454 BUG_ON(!nullb);
1455 BUG_ON(!nq);
1456
1457 init_waitqueue_head(&nq->wait);
1458 nq->queue_depth = nullb->queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -07001459 nq->dev = nullb->dev;
Jens Axboe82f402f2017-06-20 14:22:01 -06001460}
1461
1462static void null_init_queues(struct nullb *nullb)
1463{
1464 struct request_queue *q = nullb->q;
1465 struct blk_mq_hw_ctx *hctx;
1466 struct nullb_queue *nq;
1467 int i;
1468
1469 queue_for_each_hw_ctx(q, hctx, i) {
1470 if (!hctx->nr_ctx || !hctx->tags)
1471 continue;
1472 nq = &nullb->queues[i];
1473 hctx->driver_data = nq;
1474 null_init_queue(nullb, nq);
1475 nullb->nr_queues++;
1476 }
1477}
1478
Jens Axboef2298c02013-10-25 11:52:25 +01001479static int setup_commands(struct nullb_queue *nq)
1480{
1481 struct nullb_cmd *cmd;
1482 int i, tag_size;
1483
Kees Cook6396bb22018-06-12 14:03:40 -07001484 nq->cmds = kcalloc(nq->queue_depth, sizeof(*cmd), GFP_KERNEL);
Jens Axboef2298c02013-10-25 11:52:25 +01001485 if (!nq->cmds)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001486 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001487
1488 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
Kees Cook6396bb22018-06-12 14:03:40 -07001489 nq->tag_map = kcalloc(tag_size, sizeof(unsigned long), GFP_KERNEL);
Jens Axboef2298c02013-10-25 11:52:25 +01001490 if (!nq->tag_map) {
1491 kfree(nq->cmds);
Matias Bjorling2d263a782013-12-18 13:41:43 +01001492 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001493 }
1494
1495 for (i = 0; i < nq->queue_depth; i++) {
1496 cmd = &nq->cmds[i];
1497 INIT_LIST_HEAD(&cmd->list);
1498 cmd->ll_list.next = NULL;
1499 cmd->tag = -1U;
1500 }
1501
1502 return 0;
1503}
1504
Jens Axboef2298c02013-10-25 11:52:25 +01001505static int setup_queues(struct nullb *nullb)
1506{
Kees Cook6396bb22018-06-12 14:03:40 -07001507 nullb->queues = kcalloc(nullb->dev->submit_queues,
1508 sizeof(struct nullb_queue),
1509 GFP_KERNEL);
Jens Axboef2298c02013-10-25 11:52:25 +01001510 if (!nullb->queues)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001511 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001512
1513 nullb->nr_queues = 0;
Shaohua Li2984c862017-08-14 15:04:52 -07001514 nullb->queue_depth = nullb->dev->hw_queue_depth;
Jens Axboef2298c02013-10-25 11:52:25 +01001515
Matias Bjorling2d263a782013-12-18 13:41:43 +01001516 return 0;
1517}
1518
1519static int init_driver_queues(struct nullb *nullb)
1520{
1521 struct nullb_queue *nq;
1522 int i, ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001523
Shaohua Li2984c862017-08-14 15:04:52 -07001524 for (i = 0; i < nullb->dev->submit_queues; i++) {
Jens Axboef2298c02013-10-25 11:52:25 +01001525 nq = &nullb->queues[i];
Matias Bjorling2d263a782013-12-18 13:41:43 +01001526
1527 null_init_queue(nullb, nq);
1528
1529 ret = setup_commands(nq);
1530 if (ret)
Jan Kara31f96902014-10-22 15:34:21 +02001531 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01001532 nullb->nr_queues++;
1533 }
Matias Bjorling2d263a782013-12-18 13:41:43 +01001534 return 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001535}
1536
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001537static int null_gendisk_register(struct nullb *nullb)
Jens Axboef2298c02013-10-25 11:52:25 +01001538{
1539 struct gendisk *disk;
Jens Axboef2298c02013-10-25 11:52:25 +01001540 sector_t size;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001541
Shaohua Li2984c862017-08-14 15:04:52 -07001542 disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001543 if (!disk)
1544 return -ENOMEM;
Shaohua Li2984c862017-08-14 15:04:52 -07001545 size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001546 set_capacity(disk, size >> 9);
1547
1548 disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
1549 disk->major = null_major;
1550 disk->first_minor = nullb->index;
1551 disk->fops = &null_fops;
1552 disk->private_data = nullb;
1553 disk->queue = nullb->q;
1554 strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
1555
1556 add_disk(disk);
1557 return 0;
1558}
1559
Shaohua Li2984c862017-08-14 15:04:52 -07001560static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001561{
1562 set->ops = &null_mq_ops;
Shaohua Li2984c862017-08-14 15:04:52 -07001563 set->nr_hw_queues = nullb ? nullb->dev->submit_queues :
1564 g_submit_queues;
1565 set->queue_depth = nullb ? nullb->dev->hw_queue_depth :
1566 g_hw_queue_depth;
1567 set->numa_node = nullb ? nullb->dev->home_node : g_home_node;
Jens Axboe82f402f2017-06-20 14:22:01 -06001568 set->cmd_size = sizeof(struct nullb_cmd);
1569 set->flags = BLK_MQ_F_SHOULD_MERGE;
weiping zhangb3cffc32017-09-30 09:49:21 +08001570 if (g_no_sched)
1571 set->flags |= BLK_MQ_F_NO_SCHED;
Jens Axboe82f402f2017-06-20 14:22:01 -06001572 set->driver_data = NULL;
1573
Shaohua Li0d06a422017-08-25 13:46:25 -07001574 if ((nullb && nullb->dev->blocking) || g_blocking)
Jens Axboe82f402f2017-06-20 14:22:01 -06001575 set->flags |= BLK_MQ_F_BLOCKING;
1576
1577 return blk_mq_alloc_tag_set(set);
1578}
1579
Shaohua Licedcafa2017-08-14 15:04:54 -07001580static void null_validate_conf(struct nullb_device *dev)
1581{
1582 dev->blocksize = round_down(dev->blocksize, 512);
1583 dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096);
Shaohua Licedcafa2017-08-14 15:04:54 -07001584
1585 if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) {
1586 if (dev->submit_queues != nr_online_nodes)
1587 dev->submit_queues = nr_online_nodes;
1588 } else if (dev->submit_queues > nr_cpu_ids)
1589 dev->submit_queues = nr_cpu_ids;
1590 else if (dev->submit_queues == 0)
1591 dev->submit_queues = 1;
1592
1593 dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ);
1594 dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001595
1596 /* Do memory allocation, so set blocking */
1597 if (dev->memory_backed)
1598 dev->blocking = true;
Shaohua Lideb78b42017-08-14 15:04:59 -07001599 else /* cache is meaningless */
1600 dev->cache_size = 0;
1601 dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
1602 dev->cache_size);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001603 dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
1604 /* can not stop a queue */
1605 if (dev->queue_mode == NULL_Q_BIO)
1606 dev->mbps = 0;
Shaohua Licedcafa2017-08-14 15:04:54 -07001607}
1608
Jens Axboe24941b92018-02-28 09:18:57 -07001609#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
1610static bool __null_setup_fault(struct fault_attr *attr, char *str)
1611{
1612 if (!str[0])
1613 return true;
1614
1615 if (!setup_fault_attr(attr, str))
1616 return false;
1617
1618 attr->verbose = 0;
1619 return true;
1620}
1621#endif
1622
Jens Axboe93b57042018-01-10 09:06:23 -07001623static bool null_setup_fault(void)
1624{
Arnd Bergmann33f782c2018-01-11 11:31:25 +01001625#ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION
Jens Axboe24941b92018-02-28 09:18:57 -07001626 if (!__null_setup_fault(&null_timeout_attr, g_timeout_str))
Jens Axboe93b57042018-01-10 09:06:23 -07001627 return false;
Jens Axboe24941b92018-02-28 09:18:57 -07001628 if (!__null_setup_fault(&null_requeue_attr, g_requeue_str))
1629 return false;
Arnd Bergmann33f782c2018-01-11 11:31:25 +01001630#endif
Jens Axboe93b57042018-01-10 09:06:23 -07001631 return true;
1632}
1633
Shaohua Li2984c862017-08-14 15:04:52 -07001634static int null_add_dev(struct nullb_device *dev)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001635{
1636 struct nullb *nullb;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001637 int rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001638
Shaohua Licedcafa2017-08-14 15:04:54 -07001639 null_validate_conf(dev);
1640
Shaohua Li2984c862017-08-14 15:04:52 -07001641 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001642 if (!nullb) {
1643 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001644 goto out;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001645 }
Shaohua Li2984c862017-08-14 15:04:52 -07001646 nullb->dev = dev;
1647 dev->nullb = nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001648
1649 spin_lock_init(&nullb->lock);
1650
Robert Elliottdc501dc2014-09-02 11:38:49 -05001651 rv = setup_queues(nullb);
1652 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001653 goto out_free_nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001654
Shaohua Li2984c862017-08-14 15:04:52 -07001655 if (dev->queue_mode == NULL_Q_MQ) {
Jens Axboe82f402f2017-06-20 14:22:01 -06001656 if (shared_tags) {
1657 nullb->tag_set = &tag_set;
1658 rv = 0;
1659 } else {
1660 nullb->tag_set = &nullb->__tag_set;
Shaohua Li2984c862017-08-14 15:04:52 -07001661 rv = null_init_tag_set(nullb, nullb->tag_set);
Jens Axboe82f402f2017-06-20 14:22:01 -06001662 }
Jens Axboef2298c02013-10-25 11:52:25 +01001663
Robert Elliottdc501dc2014-09-02 11:38:49 -05001664 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001665 goto out_cleanup_queues;
Jens Axboef2298c02013-10-25 11:52:25 +01001666
Jens Axboe93b57042018-01-10 09:06:23 -07001667 if (!null_setup_fault())
1668 goto out_cleanup_queues;
1669
Jens Axboe5448aca2018-01-09 12:47:24 -07001670 nullb->tag_set->timeout = 5 * HZ;
Jens Axboe82f402f2017-06-20 14:22:01 -06001671 nullb->q = blk_mq_init_queue(nullb->tag_set);
Ming Lei35b489d2015-01-02 14:25:27 +00001672 if (IS_ERR(nullb->q)) {
Robert Elliottdc501dc2014-09-02 11:38:49 -05001673 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001674 goto out_cleanup_tags;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001675 }
Jens Axboe82f402f2017-06-20 14:22:01 -06001676 null_init_queues(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001677 } else if (dev->queue_mode == NULL_Q_BIO) {
Bart Van Assche5ee05242018-02-28 10:15:31 -08001678 nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node,
1679 NULL);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001680 if (!nullb->q) {
1681 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001682 goto out_cleanup_queues;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001683 }
Jens Axboef2298c02013-10-25 11:52:25 +01001684 blk_queue_make_request(nullb->q, null_queue_bio);
Jan Kara31f96902014-10-22 15:34:21 +02001685 rv = init_driver_queues(nullb);
1686 if (rv)
1687 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001688 }
1689
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001690 if (dev->mbps) {
1691 set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags);
1692 nullb_setup_bwtimer(nullb);
1693 }
1694
Shaohua Lideb78b42017-08-14 15:04:59 -07001695 if (dev->cache_size > 0) {
1696 set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
1697 blk_queue_write_cache(nullb->q, true, true);
1698 blk_queue_flush_queueable(nullb->q, true);
1699 }
1700
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001701 if (dev->zoned) {
1702 rv = null_zone_init(dev);
1703 if (rv)
1704 goto out_cleanup_blk_queue;
1705
1706 blk_queue_chunk_sectors(nullb->q, dev->zone_size_sects);
1707 nullb->q->limits.zoned = BLK_ZONED_HM;
1708 }
1709
Jens Axboef2298c02013-10-25 11:52:25 +01001710 nullb->q->queuedata = nullb;
Bart Van Assche8b904b52018-03-07 17:10:10 -08001711 blk_queue_flag_set(QUEUE_FLAG_NONROT, nullb->q);
1712 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, nullb->q);
Jens Axboef2298c02013-10-25 11:52:25 +01001713
Jens Axboef2298c02013-10-25 11:52:25 +01001714 mutex_lock(&lock);
Shaohua Li94bc02e2017-08-14 15:04:55 -07001715 nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
Shaohua Licedcafa2017-08-14 15:04:54 -07001716 dev->index = nullb->index;
Jens Axboef2298c02013-10-25 11:52:25 +01001717 mutex_unlock(&lock);
1718
Shaohua Li2984c862017-08-14 15:04:52 -07001719 blk_queue_logical_block_size(nullb->q, dev->blocksize);
1720 blk_queue_physical_block_size(nullb->q, dev->blocksize);
Jens Axboef2298c02013-10-25 11:52:25 +01001721
Shaohua Li306eb6b2017-08-14 15:04:57 -07001722 null_config_discard(nullb);
Jens Axboef2298c02013-10-25 11:52:25 +01001723
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001724 sprintf(nullb->disk_name, "nullb%d", nullb->index);
1725
Matias Bjørling74ede5a2018-01-05 14:15:57 +01001726 rv = null_gendisk_register(nullb);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001727 if (rv)
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001728 goto out_cleanup_zone;
Jens Axboef2298c02013-10-25 11:52:25 +01001729
Matias Bjørlinga5143792016-02-11 14:49:13 +01001730 mutex_lock(&lock);
1731 list_add_tail(&nullb->list, &nullb_list);
1732 mutex_unlock(&lock);
Wenwei Tao3681c852016-03-05 00:27:04 +08001733
Jens Axboef2298c02013-10-25 11:52:25 +01001734 return 0;
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001735out_cleanup_zone:
1736 if (dev->zoned)
1737 null_zone_exit(dev);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001738out_cleanup_blk_queue:
1739 blk_cleanup_queue(nullb->q);
1740out_cleanup_tags:
Shaohua Li2984c862017-08-14 15:04:52 -07001741 if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001742 blk_mq_free_tag_set(nullb->tag_set);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001743out_cleanup_queues:
1744 cleanup_queues(nullb);
1745out_free_nullb:
1746 kfree(nullb);
1747out:
Robert Elliottdc501dc2014-09-02 11:38:49 -05001748 return rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001749}
1750
1751static int __init null_init(void)
1752{
Minfei Huangaf096e22015-12-08 13:47:34 -07001753 int ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001754 unsigned int i;
Minfei Huangaf096e22015-12-08 13:47:34 -07001755 struct nullb *nullb;
Shaohua Li2984c862017-08-14 15:04:52 -07001756 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +01001757
Shaohua Li2984c862017-08-14 15:04:52 -07001758 if (g_bs > PAGE_SIZE) {
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301759 pr_warn("null_blk: invalid block size\n");
1760 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
Shaohua Li2984c862017-08-14 15:04:52 -07001761 g_bs = PAGE_SIZE;
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301762 }
Jens Axboef2298c02013-10-25 11:52:25 +01001763
Matias Bjørlingca4b2a02018-07-06 19:38:39 +02001764 if (!is_power_of_2(g_zone_size)) {
1765 pr_err("null_blk: zone_size must be power-of-two\n");
1766 return -EINVAL;
1767 }
1768
Jens Axboee50b1e32018-10-11 17:58:17 -06001769 if (g_queue_mode == NULL_Q_RQ) {
1770 pr_err("null_blk: legacy IO path no longer available\n");
1771 return -EINVAL;
1772 }
Shaohua Li2984c862017-08-14 15:04:52 -07001773 if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) {
1774 if (g_submit_queues != nr_online_nodes) {
weiping zhang558ab3002017-08-03 00:26:39 +08001775 pr_warn("null_blk: submit_queues param is set to %u.\n",
Matias Bjorlingd15ee6b2013-12-18 13:41:44 +01001776 nr_online_nodes);
Shaohua Li2984c862017-08-14 15:04:52 -07001777 g_submit_queues = nr_online_nodes;
Matias Bjørlingfc1bc352013-12-21 00:11:01 +01001778 }
Shaohua Li2984c862017-08-14 15:04:52 -07001779 } else if (g_submit_queues > nr_cpu_ids)
1780 g_submit_queues = nr_cpu_ids;
1781 else if (g_submit_queues <= 0)
1782 g_submit_queues = 1;
Jens Axboef2298c02013-10-25 11:52:25 +01001783
Shaohua Li2984c862017-08-14 15:04:52 -07001784 if (g_queue_mode == NULL_Q_MQ && shared_tags) {
1785 ret = null_init_tag_set(NULL, &tag_set);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001786 if (ret)
1787 return ret;
1788 }
1789
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001790 config_group_init(&nullb_subsys.su_group);
1791 mutex_init(&nullb_subsys.su_mutex);
1792
1793 ret = configfs_register_subsystem(&nullb_subsys);
1794 if (ret)
1795 goto err_tagset;
1796
Jens Axboef2298c02013-10-25 11:52:25 +01001797 mutex_init(&lock);
1798
Jens Axboef2298c02013-10-25 11:52:25 +01001799 null_major = register_blkdev(0, "nullb");
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001800 if (null_major < 0) {
1801 ret = null_major;
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001802 goto err_conf;
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001803 }
Jens Axboef2298c02013-10-25 11:52:25 +01001804
Minfei Huangaf096e22015-12-08 13:47:34 -07001805 for (i = 0; i < nr_devices; i++) {
Shaohua Li2984c862017-08-14 15:04:52 -07001806 dev = null_alloc_dev();
Wei Yongjun30c516d2017-10-17 12:11:46 +00001807 if (!dev) {
1808 ret = -ENOMEM;
Minfei Huangaf096e22015-12-08 13:47:34 -07001809 goto err_dev;
Wei Yongjun30c516d2017-10-17 12:11:46 +00001810 }
Shaohua Li2984c862017-08-14 15:04:52 -07001811 ret = null_add_dev(dev);
1812 if (ret) {
1813 null_free_dev(dev);
1814 goto err_dev;
1815 }
Minfei Huangaf096e22015-12-08 13:47:34 -07001816 }
1817
Jens Axboef2298c02013-10-25 11:52:25 +01001818 pr_info("null: module loaded\n");
1819 return 0;
Minfei Huangaf096e22015-12-08 13:47:34 -07001820
1821err_dev:
1822 while (!list_empty(&nullb_list)) {
1823 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07001824 dev = nullb->dev;
Minfei Huangaf096e22015-12-08 13:47:34 -07001825 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001826 null_free_dev(dev);
Minfei Huangaf096e22015-12-08 13:47:34 -07001827 }
Minfei Huangaf096e22015-12-08 13:47:34 -07001828 unregister_blkdev(null_major, "nullb");
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001829err_conf:
1830 configfs_unregister_subsystem(&nullb_subsys);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001831err_tagset:
Shaohua Li2984c862017-08-14 15:04:52 -07001832 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001833 blk_mq_free_tag_set(&tag_set);
Minfei Huangaf096e22015-12-08 13:47:34 -07001834 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01001835}
1836
1837static void __exit null_exit(void)
1838{
1839 struct nullb *nullb;
1840
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001841 configfs_unregister_subsystem(&nullb_subsys);
1842
Jens Axboef2298c02013-10-25 11:52:25 +01001843 unregister_blkdev(null_major, "nullb");
1844
1845 mutex_lock(&lock);
1846 while (!list_empty(&nullb_list)) {
Shaohua Li2984c862017-08-14 15:04:52 -07001847 struct nullb_device *dev;
1848
Jens Axboef2298c02013-10-25 11:52:25 +01001849 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07001850 dev = nullb->dev;
Jens Axboef2298c02013-10-25 11:52:25 +01001851 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001852 null_free_dev(dev);
Jens Axboef2298c02013-10-25 11:52:25 +01001853 }
1854 mutex_unlock(&lock);
Matias Bjørling6bb95352015-11-19 12:50:08 +01001855
Shaohua Li2984c862017-08-14 15:04:52 -07001856 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Jens Axboe82f402f2017-06-20 14:22:01 -06001857 blk_mq_free_tag_set(&tag_set);
Jens Axboef2298c02013-10-25 11:52:25 +01001858}
1859
1860module_init(null_init);
1861module_exit(null_exit);
1862
Jens Axboe231b3db2017-08-25 12:53:15 -06001863MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
Jens Axboef2298c02013-10-25 11:52:25 +01001864MODULE_LICENSE("GPL");