blob: bf2c8ca3242aa60749ea03483892526de02a0389 [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>
10#include <linux/blkdev.h>
11#include <linux/init.h>
12#include <linux/slab.h>
13#include <linux/blk-mq.h>
14#include <linux/hrtimer.h>
Matias Bjørlingb2b7e002015-11-12 20:25:10 +010015#include <linux/lightnvm.h>
Shaohua Li3bf2bd22017-08-14 15:04:53 -070016#include <linux/configfs.h>
Shaohua Li2f54a612017-08-14 15:05:00 -070017#include <linux/badblocks.h>
Jens Axboef2298c02013-10-25 11:52:25 +010018
Shaohua Li5bcd0e02017-08-14 15:04:56 -070019#define SECTOR_SHIFT 9
20#define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
21#define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
22#define SECTOR_SIZE (1 << SECTOR_SHIFT)
23#define SECTOR_MASK (PAGE_SECTORS - 1)
24
25#define FREE_BATCH 16
26
Shaohua Lieff2c4f2017-08-14 15:04:58 -070027#define TICKS_PER_SEC 50ULL
28#define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC)
29
30static inline u64 mb_per_tick(int mbps)
31{
32 return (1 << 20) / TICKS_PER_SEC * ((u64) mbps);
33}
Jens Axboef2298c02013-10-25 11:52:25 +010034
35struct nullb_cmd {
36 struct list_head list;
37 struct llist_node ll_list;
Ying Huang966a9672017-08-08 12:30:00 +080038 call_single_data_t csd;
Jens Axboef2298c02013-10-25 11:52:25 +010039 struct request *rq;
40 struct bio *bio;
41 unsigned int tag;
42 struct nullb_queue *nq;
Paolo Valente3c395a92015-12-01 11:48:17 +010043 struct hrtimer timer;
Shaohua Li5bcd0e02017-08-14 15:04:56 -070044 blk_status_t error;
Jens Axboef2298c02013-10-25 11:52:25 +010045};
46
47struct nullb_queue {
48 unsigned long *tag_map;
49 wait_queue_head_t wait;
50 unsigned int queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -070051 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +010052
53 struct nullb_cmd *cmds;
54};
55
Shaohua Li3bf2bd22017-08-14 15:04:53 -070056/*
57 * Status flags for nullb_device.
58 *
59 * CONFIGURED: Device has been configured and turned on. Cannot reconfigure.
60 * UP: Device is currently on and visible in userspace.
Shaohua Lieff2c4f2017-08-14 15:04:58 -070061 * THROTTLED: Device is being throttled.
Shaohua Lideb78b42017-08-14 15:04:59 -070062 * CACHE: Device is using a write-back cache.
Shaohua Li3bf2bd22017-08-14 15:04:53 -070063 */
64enum nullb_device_flags {
65 NULLB_DEV_FL_CONFIGURED = 0,
66 NULLB_DEV_FL_UP = 1,
Shaohua Lieff2c4f2017-08-14 15:04:58 -070067 NULLB_DEV_FL_THROTTLED = 2,
Shaohua Lideb78b42017-08-14 15:04:59 -070068 NULLB_DEV_FL_CACHE = 3,
Shaohua Li3bf2bd22017-08-14 15:04:53 -070069};
70
Shaohua Li5bcd0e02017-08-14 15:04:56 -070071/*
72 * nullb_page is a page in memory for nullb devices.
73 *
74 * @page: The page holding the data.
75 * @bitmap: The bitmap represents which sector in the page has data.
76 * Each bit represents one block size. For example, sector 8
77 * will use the 7th bit
Shaohua Lideb78b42017-08-14 15:04:59 -070078 * The highest 2 bits of bitmap are for special purpose. LOCK means the cache
79 * page is being flushing to storage. FREE means the cache page is freed and
80 * should be skipped from flushing to storage. Please see
81 * null_make_cache_space
Shaohua Li5bcd0e02017-08-14 15:04:56 -070082 */
83struct nullb_page {
84 struct page *page;
85 unsigned long bitmap;
86};
Shaohua Lideb78b42017-08-14 15:04:59 -070087#define NULLB_PAGE_LOCK (sizeof(unsigned long) * 8 - 1)
88#define NULLB_PAGE_FREE (sizeof(unsigned long) * 8 - 2)
Shaohua Li5bcd0e02017-08-14 15:04:56 -070089
Shaohua Li2984c862017-08-14 15:04:52 -070090struct nullb_device {
91 struct nullb *nullb;
Shaohua Li3bf2bd22017-08-14 15:04:53 -070092 struct config_item item;
Shaohua Li5bcd0e02017-08-14 15:04:56 -070093 struct radix_tree_root data; /* data stored in the disk */
Shaohua Lideb78b42017-08-14 15:04:59 -070094 struct radix_tree_root cache; /* disk cache data */
Shaohua Li3bf2bd22017-08-14 15:04:53 -070095 unsigned long flags; /* device flags */
Shaohua Lideb78b42017-08-14 15:04:59 -070096 unsigned int curr_cache;
Shaohua Li2f54a612017-08-14 15:05:00 -070097 struct badblocks badblocks;
Shaohua Li2984c862017-08-14 15:04:52 -070098
99 unsigned long size; /* device size in MB */
100 unsigned long completion_nsec; /* time in ns to complete a request */
Shaohua Lideb78b42017-08-14 15:04:59 -0700101 unsigned long cache_size; /* disk cache size in MB */
Shaohua Li2984c862017-08-14 15:04:52 -0700102 unsigned int submit_queues; /* number of submission queues */
103 unsigned int home_node; /* home node for the device */
104 unsigned int queue_mode; /* block interface */
105 unsigned int blocksize; /* block size */
106 unsigned int irqmode; /* IRQ completion handler */
107 unsigned int hw_queue_depth; /* queue depth */
Shaohua Licedcafa2017-08-14 15:04:54 -0700108 unsigned int index; /* index of the disk, only valid with a disk */
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700109 unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */
Shaohua Li2984c862017-08-14 15:04:52 -0700110 bool use_lightnvm; /* register as a LightNVM device */
111 bool blocking; /* blocking blk-mq device */
112 bool use_per_node_hctx; /* use per-node allocation for hardware context */
Shaohua Licedcafa2017-08-14 15:04:54 -0700113 bool power; /* power on/off the device */
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700114 bool memory_backed; /* if data is stored in memory */
Shaohua Li306eb6b2017-08-14 15:04:57 -0700115 bool discard; /* if support discard */
Shaohua Li2984c862017-08-14 15:04:52 -0700116};
117
Jens Axboef2298c02013-10-25 11:52:25 +0100118struct nullb {
Shaohua Li2984c862017-08-14 15:04:52 -0700119 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +0100120 struct list_head list;
121 unsigned int index;
122 struct request_queue *q;
123 struct gendisk *disk;
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200124 struct nvm_dev *ndev;
Jens Axboe82f402f2017-06-20 14:22:01 -0600125 struct blk_mq_tag_set *tag_set;
126 struct blk_mq_tag_set __tag_set;
Jens Axboef2298c02013-10-25 11:52:25 +0100127 unsigned int queue_depth;
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700128 atomic_long_t cur_bytes;
129 struct hrtimer bw_timer;
Shaohua Lideb78b42017-08-14 15:04:59 -0700130 unsigned long cache_flush_pos;
Jens Axboef2298c02013-10-25 11:52:25 +0100131 spinlock_t lock;
132
133 struct nullb_queue *queues;
134 unsigned int nr_queues;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +0100135 char disk_name[DISK_NAME_LEN];
Jens Axboef2298c02013-10-25 11:52:25 +0100136};
137
138static LIST_HEAD(nullb_list);
139static struct mutex lock;
140static int null_major;
Shaohua Li94bc02e2017-08-14 15:04:55 -0700141static DEFINE_IDA(nullb_indexes);
Matias Bjørling6bb95352015-11-19 12:50:08 +0100142static struct kmem_cache *ppa_cache;
Jens Axboe82f402f2017-06-20 14:22:01 -0600143static struct blk_mq_tag_set tag_set;
Jens Axboef2298c02013-10-25 11:52:25 +0100144
Jens Axboef2298c02013-10-25 11:52:25 +0100145enum {
146 NULL_IRQ_NONE = 0,
147 NULL_IRQ_SOFTIRQ = 1,
148 NULL_IRQ_TIMER = 2,
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800149};
Jens Axboef2298c02013-10-25 11:52:25 +0100150
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800151enum {
Jens Axboef2298c02013-10-25 11:52:25 +0100152 NULL_Q_BIO = 0,
153 NULL_Q_RQ = 1,
154 NULL_Q_MQ = 2,
155};
156
weiping zhangb3cffc32017-09-30 09:49:21 +0800157static int g_no_sched;
158module_param_named(no_sched, g_no_sched, int, S_IRUGO);
159MODULE_PARM_DESC(no_sched, "No io scheduler");
160
Shaohua Li2984c862017-08-14 15:04:52 -0700161static int g_submit_queues = 1;
162module_param_named(submit_queues, g_submit_queues, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100163MODULE_PARM_DESC(submit_queues, "Number of submission queues");
164
Shaohua Li2984c862017-08-14 15:04:52 -0700165static int g_home_node = NUMA_NO_NODE;
166module_param_named(home_node, g_home_node, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100167MODULE_PARM_DESC(home_node, "Home node for the device");
168
Shaohua Li2984c862017-08-14 15:04:52 -0700169static int g_queue_mode = NULL_Q_MQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700170
171static int null_param_store_val(const char *str, int *val, int min, int max)
172{
173 int ret, new_val;
174
175 ret = kstrtoint(str, 10, &new_val);
176 if (ret)
177 return -EINVAL;
178
179 if (new_val < min || new_val > max)
180 return -EINVAL;
181
182 *val = new_val;
183 return 0;
184}
185
186static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
187{
Shaohua Li2984c862017-08-14 15:04:52 -0700188 return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
Matias Bjorling709c8662014-11-26 14:45:48 -0700189}
190
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930191static const struct kernel_param_ops null_queue_mode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700192 .set = null_set_queue_mode,
193 .get = param_get_int,
194};
195
Shaohua Li2984c862017-08-14 15:04:52 -0700196device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, S_IRUGO);
Mike Snitzer54ae81c2014-06-11 17:13:50 -0400197MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
Jens Axboef2298c02013-10-25 11:52:25 +0100198
Shaohua Li2984c862017-08-14 15:04:52 -0700199static int g_gb = 250;
200module_param_named(gb, g_gb, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100201MODULE_PARM_DESC(gb, "Size in GB");
202
Shaohua Li2984c862017-08-14 15:04:52 -0700203static int g_bs = 512;
204module_param_named(bs, g_bs, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100205MODULE_PARM_DESC(bs, "Block size (in bytes)");
206
Jens Axboe82f402f2017-06-20 14:22:01 -0600207static int nr_devices = 1;
Jens Axboef2298c02013-10-25 11:52:25 +0100208module_param(nr_devices, int, S_IRUGO);
209MODULE_PARM_DESC(nr_devices, "Number of devices to register");
210
Shaohua Li2984c862017-08-14 15:04:52 -0700211static bool g_use_lightnvm;
212module_param_named(use_lightnvm, g_use_lightnvm, bool, S_IRUGO);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +0100213MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device");
214
Shaohua Li2984c862017-08-14 15:04:52 -0700215static bool g_blocking;
216module_param_named(blocking, g_blocking, bool, S_IRUGO);
Jens Axboedb5bcf82017-03-30 13:44:26 -0600217MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
218
Jens Axboe82f402f2017-06-20 14:22:01 -0600219static bool shared_tags;
220module_param(shared_tags, bool, S_IRUGO);
221MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");
222
Shaohua Li2984c862017-08-14 15:04:52 -0700223static int g_irqmode = NULL_IRQ_SOFTIRQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700224
225static int null_set_irqmode(const char *str, const struct kernel_param *kp)
226{
Shaohua Li2984c862017-08-14 15:04:52 -0700227 return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
Matias Bjorling709c8662014-11-26 14:45:48 -0700228 NULL_IRQ_TIMER);
229}
230
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930231static const struct kernel_param_ops null_irqmode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700232 .set = null_set_irqmode,
233 .get = param_get_int,
234};
235
Shaohua Li2984c862017-08-14 15:04:52 -0700236device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100237MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
238
Shaohua Li2984c862017-08-14 15:04:52 -0700239static unsigned long g_completion_nsec = 10000;
240module_param_named(completion_nsec, g_completion_nsec, ulong, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100241MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
242
Shaohua Li2984c862017-08-14 15:04:52 -0700243static int g_hw_queue_depth = 64;
244module_param_named(hw_queue_depth, g_hw_queue_depth, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100245MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
246
Shaohua Li2984c862017-08-14 15:04:52 -0700247static bool g_use_per_node_hctx;
248module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, S_IRUGO);
Matias Bjørling20005242013-12-21 00:11:00 +0100249MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
Jens Axboef2298c02013-10-25 11:52:25 +0100250
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700251static struct nullb_device *null_alloc_dev(void);
252static void null_free_dev(struct nullb_device *dev);
Shaohua Licedcafa2017-08-14 15:04:54 -0700253static void null_del_dev(struct nullb *nullb);
254static int null_add_dev(struct nullb_device *dev);
Shaohua Lideb78b42017-08-14 15:04:59 -0700255static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700256
257static inline struct nullb_device *to_nullb_device(struct config_item *item)
258{
259 return item ? container_of(item, struct nullb_device, item) : NULL;
260}
261
262static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page)
263{
264 return snprintf(page, PAGE_SIZE, "%u\n", val);
265}
266
267static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
268 char *page)
269{
270 return snprintf(page, PAGE_SIZE, "%lu\n", val);
271}
272
273static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
274{
275 return snprintf(page, PAGE_SIZE, "%u\n", val);
276}
277
278static ssize_t nullb_device_uint_attr_store(unsigned int *val,
279 const char *page, size_t count)
280{
281 unsigned int tmp;
282 int result;
283
284 result = kstrtouint(page, 0, &tmp);
285 if (result)
286 return result;
287
288 *val = tmp;
289 return count;
290}
291
292static ssize_t nullb_device_ulong_attr_store(unsigned long *val,
293 const char *page, size_t count)
294{
295 int result;
296 unsigned long tmp;
297
298 result = kstrtoul(page, 0, &tmp);
299 if (result)
300 return result;
301
302 *val = tmp;
303 return count;
304}
305
306static ssize_t nullb_device_bool_attr_store(bool *val, const char *page,
307 size_t count)
308{
309 bool tmp;
310 int result;
311
312 result = kstrtobool(page, &tmp);
313 if (result)
314 return result;
315
316 *val = tmp;
317 return count;
318}
319
320/* The following macro should only be used with TYPE = {uint, ulong, bool}. */
321#define NULLB_DEVICE_ATTR(NAME, TYPE) \
322static ssize_t \
323nullb_device_##NAME##_show(struct config_item *item, char *page) \
324{ \
325 return nullb_device_##TYPE##_attr_show( \
326 to_nullb_device(item)->NAME, page); \
327} \
328static ssize_t \
329nullb_device_##NAME##_store(struct config_item *item, const char *page, \
330 size_t count) \
331{ \
332 if (test_bit(NULLB_DEV_FL_CONFIGURED, &to_nullb_device(item)->flags)) \
333 return -EBUSY; \
334 return nullb_device_##TYPE##_attr_store( \
335 &to_nullb_device(item)->NAME, page, count); \
336} \
337CONFIGFS_ATTR(nullb_device_, NAME);
338
339NULLB_DEVICE_ATTR(size, ulong);
340NULLB_DEVICE_ATTR(completion_nsec, ulong);
341NULLB_DEVICE_ATTR(submit_queues, uint);
342NULLB_DEVICE_ATTR(home_node, uint);
343NULLB_DEVICE_ATTR(queue_mode, uint);
344NULLB_DEVICE_ATTR(blocksize, uint);
345NULLB_DEVICE_ATTR(irqmode, uint);
346NULLB_DEVICE_ATTR(hw_queue_depth, uint);
Shaohua Licedcafa2017-08-14 15:04:54 -0700347NULLB_DEVICE_ATTR(index, uint);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700348NULLB_DEVICE_ATTR(use_lightnvm, bool);
349NULLB_DEVICE_ATTR(blocking, bool);
350NULLB_DEVICE_ATTR(use_per_node_hctx, bool);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700351NULLB_DEVICE_ATTR(memory_backed, bool);
Shaohua Li306eb6b2017-08-14 15:04:57 -0700352NULLB_DEVICE_ATTR(discard, bool);
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700353NULLB_DEVICE_ATTR(mbps, uint);
Shaohua Lideb78b42017-08-14 15:04:59 -0700354NULLB_DEVICE_ATTR(cache_size, ulong);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700355
Shaohua Licedcafa2017-08-14 15:04:54 -0700356static ssize_t nullb_device_power_show(struct config_item *item, char *page)
357{
358 return nullb_device_bool_attr_show(to_nullb_device(item)->power, page);
359}
360
361static ssize_t nullb_device_power_store(struct config_item *item,
362 const char *page, size_t count)
363{
364 struct nullb_device *dev = to_nullb_device(item);
365 bool newp = false;
366 ssize_t ret;
367
368 ret = nullb_device_bool_attr_store(&newp, page, count);
369 if (ret < 0)
370 return ret;
371
372 if (!dev->power && newp) {
373 if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
374 return count;
375 if (null_add_dev(dev)) {
376 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
377 return -ENOMEM;
378 }
379
380 set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
381 dev->power = newp;
Jens Axboeb3c30512017-08-28 15:06:31 -0600382 } else if (dev->power && !newp) {
Shaohua Licedcafa2017-08-14 15:04:54 -0700383 mutex_lock(&lock);
384 dev->power = newp;
385 null_del_dev(dev->nullb);
386 mutex_unlock(&lock);
387 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
388 }
389
390 return count;
391}
392
393CONFIGFS_ATTR(nullb_device_, power);
394
Shaohua Li2f54a612017-08-14 15:05:00 -0700395static ssize_t nullb_device_badblocks_show(struct config_item *item, char *page)
396{
397 struct nullb_device *t_dev = to_nullb_device(item);
398
399 return badblocks_show(&t_dev->badblocks, page, 0);
400}
401
402static ssize_t nullb_device_badblocks_store(struct config_item *item,
403 const char *page, size_t count)
404{
405 struct nullb_device *t_dev = to_nullb_device(item);
406 char *orig, *buf, *tmp;
407 u64 start, end;
408 int ret;
409
410 orig = kstrndup(page, count, GFP_KERNEL);
411 if (!orig)
412 return -ENOMEM;
413
414 buf = strstrip(orig);
415
416 ret = -EINVAL;
417 if (buf[0] != '+' && buf[0] != '-')
418 goto out;
419 tmp = strchr(&buf[1], '-');
420 if (!tmp)
421 goto out;
422 *tmp = '\0';
423 ret = kstrtoull(buf + 1, 0, &start);
424 if (ret)
425 goto out;
426 ret = kstrtoull(tmp + 1, 0, &end);
427 if (ret)
428 goto out;
429 ret = -EINVAL;
430 if (start > end)
431 goto out;
432 /* enable badblocks */
433 cmpxchg(&t_dev->badblocks.shift, -1, 0);
434 if (buf[0] == '+')
435 ret = badblocks_set(&t_dev->badblocks, start,
436 end - start + 1, 1);
437 else
438 ret = badblocks_clear(&t_dev->badblocks, start,
439 end - start + 1);
440 if (ret == 0)
441 ret = count;
442out:
443 kfree(orig);
444 return ret;
445}
446CONFIGFS_ATTR(nullb_device_, badblocks);
447
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700448static struct configfs_attribute *nullb_device_attrs[] = {
449 &nullb_device_attr_size,
450 &nullb_device_attr_completion_nsec,
451 &nullb_device_attr_submit_queues,
452 &nullb_device_attr_home_node,
453 &nullb_device_attr_queue_mode,
454 &nullb_device_attr_blocksize,
455 &nullb_device_attr_irqmode,
456 &nullb_device_attr_hw_queue_depth,
Shaohua Licedcafa2017-08-14 15:04:54 -0700457 &nullb_device_attr_index,
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700458 &nullb_device_attr_use_lightnvm,
459 &nullb_device_attr_blocking,
460 &nullb_device_attr_use_per_node_hctx,
Shaohua Licedcafa2017-08-14 15:04:54 -0700461 &nullb_device_attr_power,
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700462 &nullb_device_attr_memory_backed,
Shaohua Li306eb6b2017-08-14 15:04:57 -0700463 &nullb_device_attr_discard,
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700464 &nullb_device_attr_mbps,
Shaohua Lideb78b42017-08-14 15:04:59 -0700465 &nullb_device_attr_cache_size,
Shaohua Li2f54a612017-08-14 15:05:00 -0700466 &nullb_device_attr_badblocks,
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700467 NULL,
468};
469
470static void nullb_device_release(struct config_item *item)
471{
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700472 struct nullb_device *dev = to_nullb_device(item);
473
Shaohua Li2f54a612017-08-14 15:05:00 -0700474 badblocks_exit(&dev->badblocks);
Shaohua Lideb78b42017-08-14 15:04:59 -0700475 null_free_device_storage(dev, false);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700476 null_free_dev(dev);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700477}
478
479static struct configfs_item_operations nullb_device_ops = {
480 .release = nullb_device_release,
481};
482
483static struct config_item_type nullb_device_type = {
484 .ct_item_ops = &nullb_device_ops,
485 .ct_attrs = nullb_device_attrs,
486 .ct_owner = THIS_MODULE,
487};
488
489static struct
490config_item *nullb_group_make_item(struct config_group *group, const char *name)
491{
492 struct nullb_device *dev;
493
494 dev = null_alloc_dev();
495 if (!dev)
496 return ERR_PTR(-ENOMEM);
497
498 config_item_init_type_name(&dev->item, name, &nullb_device_type);
499
500 return &dev->item;
501}
502
503static void
504nullb_group_drop_item(struct config_group *group, struct config_item *item)
505{
Shaohua Licedcafa2017-08-14 15:04:54 -0700506 struct nullb_device *dev = to_nullb_device(item);
507
508 if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
509 mutex_lock(&lock);
510 dev->power = false;
511 null_del_dev(dev->nullb);
512 mutex_unlock(&lock);
513 }
514
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700515 config_item_put(item);
516}
517
518static ssize_t memb_group_features_show(struct config_item *item, char *page)
519{
Shaohua Li2f54a612017-08-14 15:05:00 -0700520 return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache,badblocks\n");
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700521}
522
523CONFIGFS_ATTR_RO(memb_group_, features);
524
525static struct configfs_attribute *nullb_group_attrs[] = {
526 &memb_group_attr_features,
527 NULL,
528};
529
530static struct configfs_group_operations nullb_group_ops = {
531 .make_item = nullb_group_make_item,
532 .drop_item = nullb_group_drop_item,
533};
534
535static struct config_item_type nullb_group_type = {
536 .ct_group_ops = &nullb_group_ops,
537 .ct_attrs = nullb_group_attrs,
538 .ct_owner = THIS_MODULE,
539};
540
541static struct configfs_subsystem nullb_subsys = {
542 .su_group = {
543 .cg_item = {
544 .ci_namebuf = "nullb",
545 .ci_type = &nullb_group_type,
546 },
547 },
548};
549
Shaohua Lideb78b42017-08-14 15:04:59 -0700550static inline int null_cache_active(struct nullb *nullb)
551{
552 return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
553}
554
Shaohua Li2984c862017-08-14 15:04:52 -0700555static struct nullb_device *null_alloc_dev(void)
556{
557 struct nullb_device *dev;
558
559 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
560 if (!dev)
561 return NULL;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700562 INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
Shaohua Lideb78b42017-08-14 15:04:59 -0700563 INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
Shaohua Li2f54a612017-08-14 15:05:00 -0700564 if (badblocks_init(&dev->badblocks, 0)) {
565 kfree(dev);
566 return NULL;
567 }
568
Shaohua Li2984c862017-08-14 15:04:52 -0700569 dev->size = g_gb * 1024;
570 dev->completion_nsec = g_completion_nsec;
571 dev->submit_queues = g_submit_queues;
572 dev->home_node = g_home_node;
573 dev->queue_mode = g_queue_mode;
574 dev->blocksize = g_bs;
575 dev->irqmode = g_irqmode;
576 dev->hw_queue_depth = g_hw_queue_depth;
577 dev->use_lightnvm = g_use_lightnvm;
578 dev->blocking = g_blocking;
579 dev->use_per_node_hctx = g_use_per_node_hctx;
580 return dev;
581}
582
583static void null_free_dev(struct nullb_device *dev)
584{
585 kfree(dev);
586}
587
Jens Axboef2298c02013-10-25 11:52:25 +0100588static void put_tag(struct nullb_queue *nq, unsigned int tag)
589{
590 clear_bit_unlock(tag, nq->tag_map);
591
592 if (waitqueue_active(&nq->wait))
593 wake_up(&nq->wait);
594}
595
596static unsigned int get_tag(struct nullb_queue *nq)
597{
598 unsigned int tag;
599
600 do {
601 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
602 if (tag >= nq->queue_depth)
603 return -1U;
604 } while (test_and_set_bit_lock(tag, nq->tag_map));
605
606 return tag;
607}
608
609static void free_cmd(struct nullb_cmd *cmd)
610{
611 put_tag(cmd->nq, cmd->tag);
612}
613
Paolo Valente3c395a92015-12-01 11:48:17 +0100614static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
615
Jens Axboef2298c02013-10-25 11:52:25 +0100616static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
617{
618 struct nullb_cmd *cmd;
619 unsigned int tag;
620
621 tag = get_tag(nq);
622 if (tag != -1U) {
623 cmd = &nq->cmds[tag];
624 cmd->tag = tag;
625 cmd->nq = nq;
Shaohua Li2984c862017-08-14 15:04:52 -0700626 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +0100627 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
628 HRTIMER_MODE_REL);
629 cmd->timer.function = null_cmd_timer_expired;
630 }
Jens Axboef2298c02013-10-25 11:52:25 +0100631 return cmd;
632 }
633
634 return NULL;
635}
636
637static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
638{
639 struct nullb_cmd *cmd;
640 DEFINE_WAIT(wait);
641
642 cmd = __alloc_cmd(nq);
643 if (cmd || !can_wait)
644 return cmd;
645
646 do {
647 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
648 cmd = __alloc_cmd(nq);
649 if (cmd)
650 break;
651
652 io_schedule();
653 } while (1);
654
655 finish_wait(&nq->wait, &wait);
656 return cmd;
657}
658
659static void end_cmd(struct nullb_cmd *cmd)
660{
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100661 struct request_queue *q = NULL;
Shaohua Li2984c862017-08-14 15:04:52 -0700662 int queue_mode = cmd->nq->dev->queue_mode;
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100663
Mike Krinkine8271202015-12-15 12:56:40 +0300664 if (cmd->rq)
665 q = cmd->rq->q;
666
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800667 switch (queue_mode) {
668 case NULL_Q_MQ:
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700669 blk_mq_end_request(cmd->rq, cmd->error);
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800670 return;
671 case NULL_Q_RQ:
672 INIT_LIST_HEAD(&cmd->rq->queuelist);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700673 blk_end_request_all(cmd->rq, cmd->error);
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800674 break;
675 case NULL_Q_BIO:
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700676 cmd->bio->bi_status = cmd->error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200677 bio_endio(cmd->bio);
Jens Axboe48cc6612015-12-28 13:02:47 -0700678 break;
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800679 }
Jens Axboef2298c02013-10-25 11:52:25 +0100680
Jens Axboe48cc6612015-12-28 13:02:47 -0700681 free_cmd(cmd);
682
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100683 /* Restart queue if needed, as we are freeing a tag */
Jens Axboe48cc6612015-12-28 13:02:47 -0700684 if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100685 unsigned long flags;
686
687 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe48cc6612015-12-28 13:02:47 -0700688 blk_start_queue_async(q);
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100689 spin_unlock_irqrestore(q->queue_lock, flags);
690 }
Jens Axboef2298c02013-10-25 11:52:25 +0100691}
692
693static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
694{
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100695 end_cmd(container_of(timer, struct nullb_cmd, timer));
Jens Axboef2298c02013-10-25 11:52:25 +0100696
697 return HRTIMER_NORESTART;
698}
699
700static void null_cmd_end_timer(struct nullb_cmd *cmd)
701{
Shaohua Li2984c862017-08-14 15:04:52 -0700702 ktime_t kt = cmd->nq->dev->completion_nsec;
Jens Axboef2298c02013-10-25 11:52:25 +0100703
Paolo Valente3c395a92015-12-01 11:48:17 +0100704 hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
Jens Axboef2298c02013-10-25 11:52:25 +0100705}
706
707static void null_softirq_done_fn(struct request *rq)
708{
Shaohua Li2984c862017-08-14 15:04:52 -0700709 struct nullb *nullb = rq->q->queuedata;
710
711 if (nullb->dev->queue_mode == NULL_Q_MQ)
Jens Axboed891fa72014-06-16 11:40:25 -0600712 end_cmd(blk_mq_rq_to_pdu(rq));
713 else
714 end_cmd(rq->special);
Jens Axboef2298c02013-10-25 11:52:25 +0100715}
716
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700717static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
Jens Axboef2298c02013-10-25 11:52:25 +0100718{
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700719 struct nullb_page *t_page;
720
721 t_page = kmalloc(sizeof(struct nullb_page), gfp_flags);
722 if (!t_page)
723 goto out;
724
725 t_page->page = alloc_pages(gfp_flags, 0);
726 if (!t_page->page)
727 goto out_freepage;
728
729 t_page->bitmap = 0;
730 return t_page;
731out_freepage:
732 kfree(t_page);
733out:
734 return NULL;
735}
736
737static void null_free_page(struct nullb_page *t_page)
738{
Shaohua Lideb78b42017-08-14 15:04:59 -0700739 __set_bit(NULLB_PAGE_FREE, &t_page->bitmap);
740 if (test_bit(NULLB_PAGE_LOCK, &t_page->bitmap))
741 return;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700742 __free_page(t_page->page);
743 kfree(t_page);
744}
745
Shaohua Lideb78b42017-08-14 15:04:59 -0700746static void null_free_sector(struct nullb *nullb, sector_t sector,
747 bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700748{
749 unsigned int sector_bit;
750 u64 idx;
751 struct nullb_page *t_page, *ret;
752 struct radix_tree_root *root;
753
Shaohua Lideb78b42017-08-14 15:04:59 -0700754 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700755 idx = sector >> PAGE_SECTORS_SHIFT;
756 sector_bit = (sector & SECTOR_MASK);
757
758 t_page = radix_tree_lookup(root, idx);
759 if (t_page) {
760 __clear_bit(sector_bit, &t_page->bitmap);
761
762 if (!t_page->bitmap) {
763 ret = radix_tree_delete_item(root, idx, t_page);
764 WARN_ON(ret != t_page);
765 null_free_page(ret);
Shaohua Lideb78b42017-08-14 15:04:59 -0700766 if (is_cache)
767 nullb->dev->curr_cache -= PAGE_SIZE;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700768 }
769 }
770}
771
772static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
Shaohua Lideb78b42017-08-14 15:04:59 -0700773 struct nullb_page *t_page, bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700774{
775 struct radix_tree_root *root;
776
Shaohua Lideb78b42017-08-14 15:04:59 -0700777 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700778
779 if (radix_tree_insert(root, idx, t_page)) {
780 null_free_page(t_page);
781 t_page = radix_tree_lookup(root, idx);
782 WARN_ON(!t_page || t_page->page->index != idx);
Shaohua Lideb78b42017-08-14 15:04:59 -0700783 } else if (is_cache)
784 nullb->dev->curr_cache += PAGE_SIZE;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700785
786 return t_page;
787}
788
Shaohua Lideb78b42017-08-14 15:04:59 -0700789static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700790{
791 unsigned long pos = 0;
792 int nr_pages;
793 struct nullb_page *ret, *t_pages[FREE_BATCH];
794 struct radix_tree_root *root;
795
Shaohua Lideb78b42017-08-14 15:04:59 -0700796 root = is_cache ? &dev->cache : &dev->data;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700797
798 do {
799 int i;
800
801 nr_pages = radix_tree_gang_lookup(root,
802 (void **)t_pages, pos, FREE_BATCH);
803
804 for (i = 0; i < nr_pages; i++) {
805 pos = t_pages[i]->page->index;
806 ret = radix_tree_delete_item(root, pos, t_pages[i]);
807 WARN_ON(ret != t_pages[i]);
808 null_free_page(ret);
809 }
810
811 pos++;
812 } while (nr_pages == FREE_BATCH);
Shaohua Lideb78b42017-08-14 15:04:59 -0700813
814 if (is_cache)
815 dev->curr_cache = 0;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700816}
817
Shaohua Lideb78b42017-08-14 15:04:59 -0700818static struct nullb_page *__null_lookup_page(struct nullb *nullb,
819 sector_t sector, bool for_write, bool is_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700820{
821 unsigned int sector_bit;
822 u64 idx;
823 struct nullb_page *t_page;
Shaohua Lideb78b42017-08-14 15:04:59 -0700824 struct radix_tree_root *root;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700825
826 idx = sector >> PAGE_SECTORS_SHIFT;
827 sector_bit = (sector & SECTOR_MASK);
828
Shaohua Lideb78b42017-08-14 15:04:59 -0700829 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
830 t_page = radix_tree_lookup(root, idx);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700831 WARN_ON(t_page && t_page->page->index != idx);
832
833 if (t_page && (for_write || test_bit(sector_bit, &t_page->bitmap)))
834 return t_page;
835
836 return NULL;
837}
838
Shaohua Lideb78b42017-08-14 15:04:59 -0700839static struct nullb_page *null_lookup_page(struct nullb *nullb,
840 sector_t sector, bool for_write, bool ignore_cache)
841{
842 struct nullb_page *page = NULL;
843
844 if (!ignore_cache)
845 page = __null_lookup_page(nullb, sector, for_write, true);
846 if (page)
847 return page;
848 return __null_lookup_page(nullb, sector, for_write, false);
849}
850
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700851static struct nullb_page *null_insert_page(struct nullb *nullb,
Shaohua Lideb78b42017-08-14 15:04:59 -0700852 sector_t sector, bool ignore_cache)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700853{
854 u64 idx;
855 struct nullb_page *t_page;
856
Shaohua Lideb78b42017-08-14 15:04:59 -0700857 t_page = null_lookup_page(nullb, sector, true, ignore_cache);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700858 if (t_page)
859 return t_page;
860
861 spin_unlock_irq(&nullb->lock);
862
863 t_page = null_alloc_page(GFP_NOIO);
864 if (!t_page)
865 goto out_lock;
866
867 if (radix_tree_preload(GFP_NOIO))
868 goto out_freepage;
869
870 spin_lock_irq(&nullb->lock);
871 idx = sector >> PAGE_SECTORS_SHIFT;
872 t_page->page->index = idx;
Shaohua Lideb78b42017-08-14 15:04:59 -0700873 t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700874 radix_tree_preload_end();
875
876 return t_page;
877out_freepage:
878 null_free_page(t_page);
879out_lock:
880 spin_lock_irq(&nullb->lock);
Shaohua Lideb78b42017-08-14 15:04:59 -0700881 return null_lookup_page(nullb, sector, true, ignore_cache);
882}
883
884static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
885{
886 int i;
887 unsigned int offset;
888 u64 idx;
889 struct nullb_page *t_page, *ret;
890 void *dst, *src;
891
892 idx = c_page->page->index;
893
894 t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);
895
896 __clear_bit(NULLB_PAGE_LOCK, &c_page->bitmap);
897 if (test_bit(NULLB_PAGE_FREE, &c_page->bitmap)) {
898 null_free_page(c_page);
899 if (t_page && t_page->bitmap == 0) {
900 ret = radix_tree_delete_item(&nullb->dev->data,
901 idx, t_page);
902 null_free_page(t_page);
903 }
904 return 0;
905 }
906
907 if (!t_page)
908 return -ENOMEM;
909
910 src = kmap_atomic(c_page->page);
911 dst = kmap_atomic(t_page->page);
912
913 for (i = 0; i < PAGE_SECTORS;
914 i += (nullb->dev->blocksize >> SECTOR_SHIFT)) {
915 if (test_bit(i, &c_page->bitmap)) {
916 offset = (i << SECTOR_SHIFT);
917 memcpy(dst + offset, src + offset,
918 nullb->dev->blocksize);
919 __set_bit(i, &t_page->bitmap);
920 }
921 }
922
923 kunmap_atomic(dst);
924 kunmap_atomic(src);
925
926 ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page);
927 null_free_page(ret);
928 nullb->dev->curr_cache -= PAGE_SIZE;
929
930 return 0;
931}
932
933static int null_make_cache_space(struct nullb *nullb, unsigned long n)
934{
935 int i, err, nr_pages;
936 struct nullb_page *c_pages[FREE_BATCH];
937 unsigned long flushed = 0, one_round;
938
939again:
940 if ((nullb->dev->cache_size * 1024 * 1024) >
941 nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0)
942 return 0;
943
944 nr_pages = radix_tree_gang_lookup(&nullb->dev->cache,
945 (void **)c_pages, nullb->cache_flush_pos, FREE_BATCH);
946 /*
947 * nullb_flush_cache_page could unlock before using the c_pages. To
948 * avoid race, we don't allow page free
949 */
950 for (i = 0; i < nr_pages; i++) {
951 nullb->cache_flush_pos = c_pages[i]->page->index;
952 /*
953 * We found the page which is being flushed to disk by other
954 * threads
955 */
956 if (test_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap))
957 c_pages[i] = NULL;
958 else
959 __set_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap);
960 }
961
962 one_round = 0;
963 for (i = 0; i < nr_pages; i++) {
964 if (c_pages[i] == NULL)
965 continue;
966 err = null_flush_cache_page(nullb, c_pages[i]);
967 if (err)
968 return err;
969 one_round++;
970 }
971 flushed += one_round << PAGE_SHIFT;
972
973 if (n > flushed) {
974 if (nr_pages == 0)
975 nullb->cache_flush_pos = 0;
976 if (one_round == 0) {
977 /* give other threads a chance */
978 spin_unlock_irq(&nullb->lock);
979 spin_lock_irq(&nullb->lock);
980 }
981 goto again;
982 }
983 return 0;
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700984}
985
986static int copy_to_nullb(struct nullb *nullb, struct page *source,
Shaohua Lideb78b42017-08-14 15:04:59 -0700987 unsigned int off, sector_t sector, size_t n, bool is_fua)
Shaohua Li5bcd0e02017-08-14 15:04:56 -0700988{
989 size_t temp, count = 0;
990 unsigned int offset;
991 struct nullb_page *t_page;
992 void *dst, *src;
993
994 while (count < n) {
995 temp = min_t(size_t, nullb->dev->blocksize, n - count);
996
Shaohua Lideb78b42017-08-14 15:04:59 -0700997 if (null_cache_active(nullb) && !is_fua)
998 null_make_cache_space(nullb, PAGE_SIZE);
999
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001000 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
Shaohua Lideb78b42017-08-14 15:04:59 -07001001 t_page = null_insert_page(nullb, sector,
1002 !null_cache_active(nullb) || is_fua);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001003 if (!t_page)
1004 return -ENOSPC;
1005
1006 src = kmap_atomic(source);
1007 dst = kmap_atomic(t_page->page);
1008 memcpy(dst + offset, src + off + count, temp);
1009 kunmap_atomic(dst);
1010 kunmap_atomic(src);
1011
1012 __set_bit(sector & SECTOR_MASK, &t_page->bitmap);
1013
Shaohua Lideb78b42017-08-14 15:04:59 -07001014 if (is_fua)
1015 null_free_sector(nullb, sector, true);
1016
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001017 count += temp;
1018 sector += temp >> SECTOR_SHIFT;
1019 }
1020 return 0;
1021}
1022
1023static int copy_from_nullb(struct nullb *nullb, struct page *dest,
1024 unsigned int off, sector_t sector, size_t n)
1025{
1026 size_t temp, count = 0;
1027 unsigned int offset;
1028 struct nullb_page *t_page;
1029 void *dst, *src;
1030
1031 while (count < n) {
1032 temp = min_t(size_t, nullb->dev->blocksize, n - count);
1033
1034 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
Shaohua Lideb78b42017-08-14 15:04:59 -07001035 t_page = null_lookup_page(nullb, sector, false,
1036 !null_cache_active(nullb));
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001037
1038 dst = kmap_atomic(dest);
1039 if (!t_page) {
1040 memset(dst + off + count, 0, temp);
1041 goto next;
1042 }
1043 src = kmap_atomic(t_page->page);
1044 memcpy(dst + off + count, src + offset, temp);
1045 kunmap_atomic(src);
1046next:
1047 kunmap_atomic(dst);
1048
1049 count += temp;
1050 sector += temp >> SECTOR_SHIFT;
1051 }
1052 return 0;
1053}
1054
Shaohua Li306eb6b2017-08-14 15:04:57 -07001055static void null_handle_discard(struct nullb *nullb, sector_t sector, size_t n)
1056{
1057 size_t temp;
1058
1059 spin_lock_irq(&nullb->lock);
1060 while (n > 0) {
1061 temp = min_t(size_t, n, nullb->dev->blocksize);
Shaohua Lideb78b42017-08-14 15:04:59 -07001062 null_free_sector(nullb, sector, false);
1063 if (null_cache_active(nullb))
1064 null_free_sector(nullb, sector, true);
Shaohua Li306eb6b2017-08-14 15:04:57 -07001065 sector += temp >> SECTOR_SHIFT;
1066 n -= temp;
1067 }
1068 spin_unlock_irq(&nullb->lock);
1069}
1070
Shaohua Lideb78b42017-08-14 15:04:59 -07001071static int null_handle_flush(struct nullb *nullb)
1072{
1073 int err;
1074
1075 if (!null_cache_active(nullb))
1076 return 0;
1077
1078 spin_lock_irq(&nullb->lock);
1079 while (true) {
1080 err = null_make_cache_space(nullb,
1081 nullb->dev->cache_size * 1024 * 1024);
1082 if (err || nullb->dev->curr_cache == 0)
1083 break;
1084 }
1085
1086 WARN_ON(!radix_tree_empty(&nullb->dev->cache));
1087 spin_unlock_irq(&nullb->lock);
1088 return err;
1089}
1090
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001091static int null_transfer(struct nullb *nullb, struct page *page,
Shaohua Lideb78b42017-08-14 15:04:59 -07001092 unsigned int len, unsigned int off, bool is_write, sector_t sector,
1093 bool is_fua)
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001094{
1095 int err = 0;
1096
1097 if (!is_write) {
1098 err = copy_from_nullb(nullb, page, off, sector, len);
1099 flush_dcache_page(page);
1100 } else {
1101 flush_dcache_page(page);
Shaohua Lideb78b42017-08-14 15:04:59 -07001102 err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001103 }
1104
1105 return err;
1106}
1107
1108static int null_handle_rq(struct nullb_cmd *cmd)
1109{
1110 struct request *rq = cmd->rq;
1111 struct nullb *nullb = cmd->nq->dev->nullb;
1112 int err;
1113 unsigned int len;
1114 sector_t sector;
1115 struct req_iterator iter;
1116 struct bio_vec bvec;
1117
1118 sector = blk_rq_pos(rq);
1119
Shaohua Li306eb6b2017-08-14 15:04:57 -07001120 if (req_op(rq) == REQ_OP_DISCARD) {
1121 null_handle_discard(nullb, sector, blk_rq_bytes(rq));
1122 return 0;
1123 }
1124
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001125 spin_lock_irq(&nullb->lock);
1126 rq_for_each_segment(bvec, rq, iter) {
1127 len = bvec.bv_len;
1128 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
Shaohua Lideb78b42017-08-14 15:04:59 -07001129 op_is_write(req_op(rq)), sector,
1130 req_op(rq) & REQ_FUA);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001131 if (err) {
1132 spin_unlock_irq(&nullb->lock);
1133 return err;
1134 }
1135 sector += len >> SECTOR_SHIFT;
1136 }
1137 spin_unlock_irq(&nullb->lock);
1138
1139 return 0;
1140}
1141
1142static int null_handle_bio(struct nullb_cmd *cmd)
1143{
1144 struct bio *bio = cmd->bio;
1145 struct nullb *nullb = cmd->nq->dev->nullb;
1146 int err;
1147 unsigned int len;
1148 sector_t sector;
1149 struct bio_vec bvec;
1150 struct bvec_iter iter;
1151
1152 sector = bio->bi_iter.bi_sector;
1153
Shaohua Li306eb6b2017-08-14 15:04:57 -07001154 if (bio_op(bio) == REQ_OP_DISCARD) {
1155 null_handle_discard(nullb, sector,
1156 bio_sectors(bio) << SECTOR_SHIFT);
1157 return 0;
1158 }
1159
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001160 spin_lock_irq(&nullb->lock);
1161 bio_for_each_segment(bvec, bio, iter) {
1162 len = bvec.bv_len;
1163 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
Shaohua Lideb78b42017-08-14 15:04:59 -07001164 op_is_write(bio_op(bio)), sector,
1165 bio_op(bio) & REQ_FUA);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001166 if (err) {
1167 spin_unlock_irq(&nullb->lock);
1168 return err;
1169 }
1170 sector += len >> SECTOR_SHIFT;
1171 }
1172 spin_unlock_irq(&nullb->lock);
1173 return 0;
1174}
1175
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001176static void null_stop_queue(struct nullb *nullb)
1177{
1178 struct request_queue *q = nullb->q;
1179
1180 if (nullb->dev->queue_mode == NULL_Q_MQ)
1181 blk_mq_stop_hw_queues(q);
1182 else {
1183 spin_lock_irq(q->queue_lock);
1184 blk_stop_queue(q);
1185 spin_unlock_irq(q->queue_lock);
1186 }
1187}
1188
1189static void null_restart_queue_async(struct nullb *nullb)
1190{
1191 struct request_queue *q = nullb->q;
1192 unsigned long flags;
1193
1194 if (nullb->dev->queue_mode == NULL_Q_MQ)
1195 blk_mq_start_stopped_hw_queues(q, true);
1196 else {
1197 spin_lock_irqsave(q->queue_lock, flags);
1198 blk_start_queue_async(q);
1199 spin_unlock_irqrestore(q->queue_lock, flags);
1200 }
1201}
1202
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001203static blk_status_t null_handle_cmd(struct nullb_cmd *cmd)
1204{
1205 struct nullb_device *dev = cmd->nq->dev;
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001206 struct nullb *nullb = dev->nullb;
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001207 int err = 0;
1208
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001209 if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) {
1210 struct request *rq = cmd->rq;
1211
1212 if (!hrtimer_active(&nullb->bw_timer))
1213 hrtimer_restart(&nullb->bw_timer);
1214
1215 if (atomic_long_sub_return(blk_rq_bytes(rq),
1216 &nullb->cur_bytes) < 0) {
1217 null_stop_queue(nullb);
1218 /* race with timer */
1219 if (atomic_long_read(&nullb->cur_bytes) > 0)
1220 null_restart_queue_async(nullb);
1221 if (dev->queue_mode == NULL_Q_RQ) {
1222 struct request_queue *q = nullb->q;
1223
1224 spin_lock_irq(q->queue_lock);
1225 rq->rq_flags |= RQF_DONTPREP;
1226 blk_requeue_request(q, rq);
1227 spin_unlock_irq(q->queue_lock);
1228 return BLK_STS_OK;
1229 } else
1230 /* requeue request */
1231 return BLK_STS_RESOURCE;
1232 }
1233 }
1234
Shaohua Li2f54a612017-08-14 15:05:00 -07001235 if (nullb->dev->badblocks.shift != -1) {
1236 int bad_sectors;
1237 sector_t sector, size, first_bad;
1238 bool is_flush = true;
1239
1240 if (dev->queue_mode == NULL_Q_BIO &&
1241 bio_op(cmd->bio) != REQ_OP_FLUSH) {
1242 is_flush = false;
1243 sector = cmd->bio->bi_iter.bi_sector;
1244 size = bio_sectors(cmd->bio);
1245 }
1246 if (dev->queue_mode != NULL_Q_BIO &&
1247 req_op(cmd->rq) != REQ_OP_FLUSH) {
1248 is_flush = false;
1249 sector = blk_rq_pos(cmd->rq);
1250 size = blk_rq_sectors(cmd->rq);
1251 }
1252 if (!is_flush && badblocks_check(&nullb->dev->badblocks, sector,
1253 size, &first_bad, &bad_sectors)) {
1254 cmd->error = BLK_STS_IOERR;
1255 goto out;
1256 }
1257 }
1258
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001259 if (dev->memory_backed) {
Shaohua Lideb78b42017-08-14 15:04:59 -07001260 if (dev->queue_mode == NULL_Q_BIO) {
1261 if (bio_op(cmd->bio) == REQ_OP_FLUSH)
1262 err = null_handle_flush(nullb);
1263 else
1264 err = null_handle_bio(cmd);
1265 } else {
1266 if (req_op(cmd->rq) == REQ_OP_FLUSH)
1267 err = null_handle_flush(nullb);
1268 else
1269 err = null_handle_rq(cmd);
1270 }
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001271 }
1272 cmd->error = errno_to_blk_status(err);
Shaohua Li2f54a612017-08-14 15:05:00 -07001273out:
Jens Axboef2298c02013-10-25 11:52:25 +01001274 /* Complete IO by inline, softirq or timer */
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001275 switch (dev->irqmode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001276 case NULL_IRQ_SOFTIRQ:
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001277 switch (dev->queue_mode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001278 case NULL_Q_MQ:
Christoph Hellwig08e00292017-04-20 16:03:09 +02001279 blk_mq_complete_request(cmd->rq);
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001280 break;
1281 case NULL_Q_RQ:
1282 blk_complete_request(cmd->rq);
1283 break;
1284 case NULL_Q_BIO:
1285 /*
1286 * XXX: no proper submitting cpu information available.
1287 */
1288 end_cmd(cmd);
1289 break;
1290 }
1291 break;
Jens Axboef2298c02013-10-25 11:52:25 +01001292 case NULL_IRQ_NONE:
1293 end_cmd(cmd);
1294 break;
Jens Axboef2298c02013-10-25 11:52:25 +01001295 case NULL_IRQ_TIMER:
1296 null_cmd_end_timer(cmd);
1297 break;
1298 }
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001299 return BLK_STS_OK;
Jens Axboef2298c02013-10-25 11:52:25 +01001300}
1301
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001302static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
1303{
1304 struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
1305 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1306 unsigned int mbps = nullb->dev->mbps;
1307
1308 if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
1309 return HRTIMER_NORESTART;
1310
1311 atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
1312 null_restart_queue_async(nullb);
1313
1314 hrtimer_forward_now(&nullb->bw_timer, timer_interval);
1315
1316 return HRTIMER_RESTART;
1317}
1318
1319static void nullb_setup_bwtimer(struct nullb *nullb)
1320{
1321 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1322
1323 hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1324 nullb->bw_timer.function = nullb_bwtimer_fn;
1325 atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps));
1326 hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL);
Jens Axboef2298c02013-10-25 11:52:25 +01001327}
1328
1329static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
1330{
1331 int index = 0;
1332
1333 if (nullb->nr_queues != 1)
1334 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
1335
1336 return &nullb->queues[index];
1337}
1338
Jens Axboedece1632015-11-05 10:41:16 -07001339static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
Jens Axboef2298c02013-10-25 11:52:25 +01001340{
1341 struct nullb *nullb = q->queuedata;
1342 struct nullb_queue *nq = nullb_to_queue(nullb);
1343 struct nullb_cmd *cmd;
1344
1345 cmd = alloc_cmd(nq, 1);
1346 cmd->bio = bio;
1347
1348 null_handle_cmd(cmd);
Jens Axboedece1632015-11-05 10:41:16 -07001349 return BLK_QC_T_NONE;
Jens Axboef2298c02013-10-25 11:52:25 +01001350}
1351
1352static int null_rq_prep_fn(struct request_queue *q, struct request *req)
1353{
1354 struct nullb *nullb = q->queuedata;
1355 struct nullb_queue *nq = nullb_to_queue(nullb);
1356 struct nullb_cmd *cmd;
1357
1358 cmd = alloc_cmd(nq, 0);
1359 if (cmd) {
1360 cmd->rq = req;
1361 req->special = cmd;
1362 return BLKPREP_OK;
1363 }
Akinobu Mita8b70f452015-06-02 08:35:10 +09001364 blk_stop_queue(q);
Jens Axboef2298c02013-10-25 11:52:25 +01001365
1366 return BLKPREP_DEFER;
1367}
1368
1369static void null_request_fn(struct request_queue *q)
1370{
1371 struct request *rq;
1372
1373 while ((rq = blk_fetch_request(q)) != NULL) {
1374 struct nullb_cmd *cmd = rq->special;
1375
1376 spin_unlock_irq(q->queue_lock);
1377 null_handle_cmd(cmd);
1378 spin_lock_irq(q->queue_lock);
1379 }
1380}
1381
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001382static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -06001383 const struct blk_mq_queue_data *bd)
Jens Axboef2298c02013-10-25 11:52:25 +01001384{
Jens Axboe74c45052014-10-29 11:14:52 -06001385 struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Shaohua Li2984c862017-08-14 15:04:52 -07001386 struct nullb_queue *nq = hctx->driver_data;
Jens Axboef2298c02013-10-25 11:52:25 +01001387
Jens Axboedb5bcf82017-03-30 13:44:26 -06001388 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1389
Shaohua Li2984c862017-08-14 15:04:52 -07001390 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +01001391 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1392 cmd->timer.function = null_cmd_timer_expired;
1393 }
Jens Axboe74c45052014-10-29 11:14:52 -06001394 cmd->rq = bd->rq;
Shaohua Li2984c862017-08-14 15:04:52 -07001395 cmd->nq = nq;
Jens Axboef2298c02013-10-25 11:52:25 +01001396
Jens Axboe74c45052014-10-29 11:14:52 -06001397 blk_mq_start_request(bd->rq);
Christoph Hellwige2490072014-09-13 16:40:09 -07001398
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001399 return null_handle_cmd(cmd);
Jens Axboef2298c02013-10-25 11:52:25 +01001400}
1401
Eric Biggersf363b082017-03-30 13:39:16 -07001402static const struct blk_mq_ops null_mq_ops = {
Jens Axboef2298c02013-10-25 11:52:25 +01001403 .queue_rq = null_queue_rq,
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001404 .complete = null_softirq_done_fn,
Jens Axboef2298c02013-10-25 11:52:25 +01001405};
1406
Matias Bjørlingde65d2d2015-08-31 14:17:18 +02001407static void cleanup_queue(struct nullb_queue *nq)
1408{
1409 kfree(nq->tag_map);
1410 kfree(nq->cmds);
1411}
1412
1413static void cleanup_queues(struct nullb *nullb)
1414{
1415 int i;
1416
1417 for (i = 0; i < nullb->nr_queues; i++)
1418 cleanup_queue(&nullb->queues[i]);
1419
1420 kfree(nullb->queues);
1421}
1422
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001423#ifdef CONFIG_NVM
1424
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001425static void null_lnvm_end_io(struct request *rq, blk_status_t status)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001426{
1427 struct nvm_rq *rqd = rq->end_io_data;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001428
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001429 /* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */
1430 rqd->error = status ? -EIO : 0;
Matias Bjørling06894ef2017-01-31 13:17:17 +01001431 nvm_end_io(rqd);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001432
1433 blk_put_request(rq);
1434}
1435
Matias Bjørling16f26c32015-12-06 11:25:48 +01001436static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001437{
Matias Bjørling16f26c32015-12-06 11:25:48 +01001438 struct request_queue *q = dev->q;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001439 struct request *rq;
1440 struct bio *bio = rqd->bio;
1441
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001442 rq = blk_mq_alloc_request(q,
1443 op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001444 if (IS_ERR(rq))
1445 return -ENOMEM;
1446
Bart Van Assche2644a3c2017-04-19 14:01:25 -07001447 blk_init_request_from_bio(rq, bio);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001448
1449 rq->end_io_data = rqd;
1450
1451 blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io);
1452
1453 return 0;
1454}
1455
Matias Bjørling16f26c32015-12-06 11:25:48 +01001456static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001457{
Shaohua Li2984c862017-08-14 15:04:52 -07001458 struct nullb *nullb = dev->q->queuedata;
1459 sector_t size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001460 sector_t blksize;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001461 struct nvm_id_group *grp;
1462
1463 id->ver_id = 0x1;
1464 id->vmnt = 0;
Matias Bjørlingbf643182016-02-04 15:13:27 +01001465 id->cap = 0x2;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001466 id->dom = 0x1;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001467
1468 id->ppaf.blk_offset = 0;
1469 id->ppaf.blk_len = 16;
1470 id->ppaf.pg_offset = 16;
1471 id->ppaf.pg_len = 16;
1472 id->ppaf.sect_offset = 32;
1473 id->ppaf.sect_len = 8;
1474 id->ppaf.pln_offset = 40;
1475 id->ppaf.pln_len = 8;
1476 id->ppaf.lun_offset = 48;
1477 id->ppaf.lun_len = 8;
1478 id->ppaf.ch_offset = 56;
1479 id->ppaf.ch_len = 8;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001480
Shaohua Li2984c862017-08-14 15:04:52 -07001481 sector_div(size, nullb->dev->blocksize); /* convert size to pages */
Arnd Bergmanne93d12a2016-01-13 23:04:08 +01001482 size >>= 8; /* concert size to pgs pr blk */
Matias Bjørling19bd6fe2017-01-31 13:17:15 +01001483 grp = &id->grp;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001484 grp->mtype = 0;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001485 grp->fmtype = 0;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001486 grp->num_ch = 1;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001487 grp->num_pg = 256;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001488 blksize = size;
Arnd Bergmanne93d12a2016-01-13 23:04:08 +01001489 size >>= 16;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001490 grp->num_lun = size + 1;
Arnd Bergmanne93d12a2016-01-13 23:04:08 +01001491 sector_div(blksize, grp->num_lun);
Matias Bjørling5b40db92015-11-19 12:50:09 +01001492 grp->num_blk = blksize;
1493 grp->num_pln = 1;
1494
Shaohua Li2984c862017-08-14 15:04:52 -07001495 grp->fpg_sz = nullb->dev->blocksize;
1496 grp->csecs = nullb->dev->blocksize;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001497 grp->trdt = 25000;
1498 grp->trdm = 25000;
1499 grp->tprt = 500000;
1500 grp->tprm = 500000;
1501 grp->tbet = 1500000;
1502 grp->tbem = 1500000;
1503 grp->mpos = 0x010101; /* single plane rwe */
Shaohua Li2984c862017-08-14 15:04:52 -07001504 grp->cpar = nullb->dev->hw_queue_depth;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001505
1506 return 0;
1507}
1508
Matias Bjørling16f26c32015-12-06 11:25:48 +01001509static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001510{
1511 mempool_t *virtmem_pool;
1512
Matias Bjørling6bb95352015-11-19 12:50:08 +01001513 virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001514 if (!virtmem_pool) {
1515 pr_err("null_blk: Unable to create virtual memory pool\n");
1516 return NULL;
1517 }
1518
1519 return virtmem_pool;
1520}
1521
1522static void null_lnvm_destroy_dma_pool(void *pool)
1523{
1524 mempool_destroy(pool);
1525}
1526
Matias Bjørling16f26c32015-12-06 11:25:48 +01001527static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001528 gfp_t mem_flags, dma_addr_t *dma_handler)
1529{
1530 return mempool_alloc(pool, mem_flags);
1531}
1532
1533static void null_lnvm_dev_dma_free(void *pool, void *entry,
1534 dma_addr_t dma_handler)
1535{
1536 mempool_free(entry, pool);
1537}
1538
1539static struct nvm_dev_ops null_lnvm_dev_ops = {
1540 .identity = null_lnvm_id,
1541 .submit_io = null_lnvm_submit_io,
1542
1543 .create_dma_pool = null_lnvm_create_dma_pool,
1544 .destroy_dma_pool = null_lnvm_destroy_dma_pool,
1545 .dev_dma_alloc = null_lnvm_dev_dma_alloc,
1546 .dev_dma_free = null_lnvm_dev_dma_free,
1547
1548 /* Simulate nvme protocol restriction */
1549 .max_phys_sect = 64,
1550};
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001551
1552static int null_nvm_register(struct nullb *nullb)
1553{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001554 struct nvm_dev *dev;
1555 int rv;
1556
1557 dev = nvm_alloc_dev(0);
1558 if (!dev)
1559 return -ENOMEM;
1560
1561 dev->q = nullb->q;
1562 memcpy(dev->name, nullb->disk_name, DISK_NAME_LEN);
1563 dev->ops = &null_lnvm_dev_ops;
1564
1565 rv = nvm_register(dev);
1566 if (rv) {
1567 kfree(dev);
1568 return rv;
1569 }
1570 nullb->ndev = dev;
1571 return 0;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001572}
1573
1574static void null_nvm_unregister(struct nullb *nullb)
1575{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001576 nvm_unregister(nullb->ndev);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001577}
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001578#else
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001579static int null_nvm_register(struct nullb *nullb)
1580{
Yasuaki Ishimatsu92153d32016-11-16 08:26:11 -07001581 pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n");
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001582 return -EINVAL;
1583}
1584static void null_nvm_unregister(struct nullb *nullb) {}
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001585#endif /* CONFIG_NVM */
1586
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001587static void null_del_dev(struct nullb *nullb)
1588{
Shaohua Li2984c862017-08-14 15:04:52 -07001589 struct nullb_device *dev = nullb->dev;
1590
Shaohua Li94bc02e2017-08-14 15:04:55 -07001591 ida_simple_remove(&nullb_indexes, nullb->index);
1592
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001593 list_del_init(&nullb->list);
1594
Shaohua Li2984c862017-08-14 15:04:52 -07001595 if (dev->use_lightnvm)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001596 null_nvm_unregister(nullb);
1597 else
1598 del_gendisk(nullb->disk);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001599
1600 if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
1601 hrtimer_cancel(&nullb->bw_timer);
1602 atomic_long_set(&nullb->cur_bytes, LONG_MAX);
1603 null_restart_queue_async(nullb);
1604 }
1605
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001606 blk_cleanup_queue(nullb->q);
Shaohua Li2984c862017-08-14 15:04:52 -07001607 if (dev->queue_mode == NULL_Q_MQ &&
1608 nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001609 blk_mq_free_tag_set(nullb->tag_set);
Shaohua Li2984c862017-08-14 15:04:52 -07001610 if (!dev->use_lightnvm)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001611 put_disk(nullb->disk);
1612 cleanup_queues(nullb);
Shaohua Lideb78b42017-08-14 15:04:59 -07001613 if (null_cache_active(nullb))
1614 null_free_device_storage(nullb->dev, true);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001615 kfree(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001616 dev->nullb = NULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001617}
1618
Shaohua Li306eb6b2017-08-14 15:04:57 -07001619static void null_config_discard(struct nullb *nullb)
1620{
1621 if (nullb->dev->discard == false)
1622 return;
1623 nullb->q->limits.discard_granularity = nullb->dev->blocksize;
1624 nullb->q->limits.discard_alignment = nullb->dev->blocksize;
1625 blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9);
1626 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nullb->q);
Jens Axboef2298c02013-10-25 11:52:25 +01001627}
1628
1629static int null_open(struct block_device *bdev, fmode_t mode)
1630{
1631 return 0;
1632}
1633
1634static void null_release(struct gendisk *disk, fmode_t mode)
1635{
1636}
1637
1638static const struct block_device_operations null_fops = {
1639 .owner = THIS_MODULE,
1640 .open = null_open,
1641 .release = null_release,
1642};
1643
Jens Axboe82f402f2017-06-20 14:22:01 -06001644static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
1645{
1646 BUG_ON(!nullb);
1647 BUG_ON(!nq);
1648
1649 init_waitqueue_head(&nq->wait);
1650 nq->queue_depth = nullb->queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -07001651 nq->dev = nullb->dev;
Jens Axboe82f402f2017-06-20 14:22:01 -06001652}
1653
1654static void null_init_queues(struct nullb *nullb)
1655{
1656 struct request_queue *q = nullb->q;
1657 struct blk_mq_hw_ctx *hctx;
1658 struct nullb_queue *nq;
1659 int i;
1660
1661 queue_for_each_hw_ctx(q, hctx, i) {
1662 if (!hctx->nr_ctx || !hctx->tags)
1663 continue;
1664 nq = &nullb->queues[i];
1665 hctx->driver_data = nq;
1666 null_init_queue(nullb, nq);
1667 nullb->nr_queues++;
1668 }
1669}
1670
Jens Axboef2298c02013-10-25 11:52:25 +01001671static int setup_commands(struct nullb_queue *nq)
1672{
1673 struct nullb_cmd *cmd;
1674 int i, tag_size;
1675
1676 nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
1677 if (!nq->cmds)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001678 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001679
1680 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
1681 nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
1682 if (!nq->tag_map) {
1683 kfree(nq->cmds);
Matias Bjorling2d263a782013-12-18 13:41:43 +01001684 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001685 }
1686
1687 for (i = 0; i < nq->queue_depth; i++) {
1688 cmd = &nq->cmds[i];
1689 INIT_LIST_HEAD(&cmd->list);
1690 cmd->ll_list.next = NULL;
1691 cmd->tag = -1U;
1692 }
1693
1694 return 0;
1695}
1696
Jens Axboef2298c02013-10-25 11:52:25 +01001697static int setup_queues(struct nullb *nullb)
1698{
Shaohua Li2984c862017-08-14 15:04:52 -07001699 nullb->queues = kzalloc(nullb->dev->submit_queues *
1700 sizeof(struct nullb_queue), GFP_KERNEL);
Jens Axboef2298c02013-10-25 11:52:25 +01001701 if (!nullb->queues)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001702 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001703
1704 nullb->nr_queues = 0;
Shaohua Li2984c862017-08-14 15:04:52 -07001705 nullb->queue_depth = nullb->dev->hw_queue_depth;
Jens Axboef2298c02013-10-25 11:52:25 +01001706
Matias Bjorling2d263a782013-12-18 13:41:43 +01001707 return 0;
1708}
1709
1710static int init_driver_queues(struct nullb *nullb)
1711{
1712 struct nullb_queue *nq;
1713 int i, ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001714
Shaohua Li2984c862017-08-14 15:04:52 -07001715 for (i = 0; i < nullb->dev->submit_queues; i++) {
Jens Axboef2298c02013-10-25 11:52:25 +01001716 nq = &nullb->queues[i];
Matias Bjorling2d263a782013-12-18 13:41:43 +01001717
1718 null_init_queue(nullb, nq);
1719
1720 ret = setup_commands(nq);
1721 if (ret)
Jan Kara31f96902014-10-22 15:34:21 +02001722 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01001723 nullb->nr_queues++;
1724 }
Matias Bjorling2d263a782013-12-18 13:41:43 +01001725 return 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001726}
1727
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001728static int null_gendisk_register(struct nullb *nullb)
Jens Axboef2298c02013-10-25 11:52:25 +01001729{
1730 struct gendisk *disk;
Jens Axboef2298c02013-10-25 11:52:25 +01001731 sector_t size;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001732
Shaohua Li2984c862017-08-14 15:04:52 -07001733 disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001734 if (!disk)
1735 return -ENOMEM;
Shaohua Li2984c862017-08-14 15:04:52 -07001736 size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001737 set_capacity(disk, size >> 9);
1738
1739 disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
1740 disk->major = null_major;
1741 disk->first_minor = nullb->index;
1742 disk->fops = &null_fops;
1743 disk->private_data = nullb;
1744 disk->queue = nullb->q;
1745 strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
1746
1747 add_disk(disk);
1748 return 0;
1749}
1750
Shaohua Li2984c862017-08-14 15:04:52 -07001751static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001752{
1753 set->ops = &null_mq_ops;
Shaohua Li2984c862017-08-14 15:04:52 -07001754 set->nr_hw_queues = nullb ? nullb->dev->submit_queues :
1755 g_submit_queues;
1756 set->queue_depth = nullb ? nullb->dev->hw_queue_depth :
1757 g_hw_queue_depth;
1758 set->numa_node = nullb ? nullb->dev->home_node : g_home_node;
Jens Axboe82f402f2017-06-20 14:22:01 -06001759 set->cmd_size = sizeof(struct nullb_cmd);
1760 set->flags = BLK_MQ_F_SHOULD_MERGE;
weiping zhangb3cffc32017-09-30 09:49:21 +08001761 if (g_no_sched)
1762 set->flags |= BLK_MQ_F_NO_SCHED;
Jens Axboe82f402f2017-06-20 14:22:01 -06001763 set->driver_data = NULL;
1764
Shaohua Li0d06a422017-08-25 13:46:25 -07001765 if ((nullb && nullb->dev->blocking) || g_blocking)
Jens Axboe82f402f2017-06-20 14:22:01 -06001766 set->flags |= BLK_MQ_F_BLOCKING;
1767
1768 return blk_mq_alloc_tag_set(set);
1769}
1770
Shaohua Licedcafa2017-08-14 15:04:54 -07001771static void null_validate_conf(struct nullb_device *dev)
1772{
1773 dev->blocksize = round_down(dev->blocksize, 512);
1774 dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096);
1775 if (dev->use_lightnvm && dev->blocksize != 4096)
1776 dev->blocksize = 4096;
1777
1778 if (dev->use_lightnvm && dev->queue_mode != NULL_Q_MQ)
1779 dev->queue_mode = NULL_Q_MQ;
1780
1781 if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) {
1782 if (dev->submit_queues != nr_online_nodes)
1783 dev->submit_queues = nr_online_nodes;
1784 } else if (dev->submit_queues > nr_cpu_ids)
1785 dev->submit_queues = nr_cpu_ids;
1786 else if (dev->submit_queues == 0)
1787 dev->submit_queues = 1;
1788
1789 dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ);
1790 dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
Shaohua Li5bcd0e02017-08-14 15:04:56 -07001791
1792 /* Do memory allocation, so set blocking */
1793 if (dev->memory_backed)
1794 dev->blocking = true;
Shaohua Lideb78b42017-08-14 15:04:59 -07001795 else /* cache is meaningless */
1796 dev->cache_size = 0;
1797 dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
1798 dev->cache_size);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001799 dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
1800 /* can not stop a queue */
1801 if (dev->queue_mode == NULL_Q_BIO)
1802 dev->mbps = 0;
Shaohua Licedcafa2017-08-14 15:04:54 -07001803}
1804
Shaohua Li2984c862017-08-14 15:04:52 -07001805static int null_add_dev(struct nullb_device *dev)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001806{
1807 struct nullb *nullb;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001808 int rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001809
Shaohua Licedcafa2017-08-14 15:04:54 -07001810 null_validate_conf(dev);
1811
Shaohua Li2984c862017-08-14 15:04:52 -07001812 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001813 if (!nullb) {
1814 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001815 goto out;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001816 }
Shaohua Li2984c862017-08-14 15:04:52 -07001817 nullb->dev = dev;
1818 dev->nullb = nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001819
1820 spin_lock_init(&nullb->lock);
1821
Robert Elliottdc501dc2014-09-02 11:38:49 -05001822 rv = setup_queues(nullb);
1823 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001824 goto out_free_nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001825
Shaohua Li2984c862017-08-14 15:04:52 -07001826 if (dev->queue_mode == NULL_Q_MQ) {
Jens Axboe82f402f2017-06-20 14:22:01 -06001827 if (shared_tags) {
1828 nullb->tag_set = &tag_set;
1829 rv = 0;
1830 } else {
1831 nullb->tag_set = &nullb->__tag_set;
Shaohua Li2984c862017-08-14 15:04:52 -07001832 rv = null_init_tag_set(nullb, nullb->tag_set);
Jens Axboe82f402f2017-06-20 14:22:01 -06001833 }
Jens Axboef2298c02013-10-25 11:52:25 +01001834
Robert Elliottdc501dc2014-09-02 11:38:49 -05001835 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001836 goto out_cleanup_queues;
Jens Axboef2298c02013-10-25 11:52:25 +01001837
Jens Axboe82f402f2017-06-20 14:22:01 -06001838 nullb->q = blk_mq_init_queue(nullb->tag_set);
Ming Lei35b489d2015-01-02 14:25:27 +00001839 if (IS_ERR(nullb->q)) {
Robert Elliottdc501dc2014-09-02 11:38:49 -05001840 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001841 goto out_cleanup_tags;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001842 }
Jens Axboe82f402f2017-06-20 14:22:01 -06001843 null_init_queues(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001844 } else if (dev->queue_mode == NULL_Q_BIO) {
1845 nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001846 if (!nullb->q) {
1847 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001848 goto out_cleanup_queues;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001849 }
Jens Axboef2298c02013-10-25 11:52:25 +01001850 blk_queue_make_request(nullb->q, null_queue_bio);
Jan Kara31f96902014-10-22 15:34:21 +02001851 rv = init_driver_queues(nullb);
1852 if (rv)
1853 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001854 } else {
Shaohua Li2984c862017-08-14 15:04:52 -07001855 nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock,
1856 dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001857 if (!nullb->q) {
1858 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001859 goto out_cleanup_queues;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001860 }
Jens Axboef2298c02013-10-25 11:52:25 +01001861 blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001862 blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
Jan Kara31f96902014-10-22 15:34:21 +02001863 rv = init_driver_queues(nullb);
1864 if (rv)
1865 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001866 }
1867
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001868 if (dev->mbps) {
1869 set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags);
1870 nullb_setup_bwtimer(nullb);
1871 }
1872
Shaohua Lideb78b42017-08-14 15:04:59 -07001873 if (dev->cache_size > 0) {
1874 set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
1875 blk_queue_write_cache(nullb->q, true, true);
1876 blk_queue_flush_queueable(nullb->q, true);
1877 }
1878
Jens Axboef2298c02013-10-25 11:52:25 +01001879 nullb->q->queuedata = nullb;
1880 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
Mike Snitzerb277da02014-10-04 10:55:32 -06001881 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
Jens Axboef2298c02013-10-25 11:52:25 +01001882
Jens Axboef2298c02013-10-25 11:52:25 +01001883 mutex_lock(&lock);
Shaohua Li94bc02e2017-08-14 15:04:55 -07001884 nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
Shaohua Licedcafa2017-08-14 15:04:54 -07001885 dev->index = nullb->index;
Jens Axboef2298c02013-10-25 11:52:25 +01001886 mutex_unlock(&lock);
1887
Shaohua Li2984c862017-08-14 15:04:52 -07001888 blk_queue_logical_block_size(nullb->q, dev->blocksize);
1889 blk_queue_physical_block_size(nullb->q, dev->blocksize);
Jens Axboef2298c02013-10-25 11:52:25 +01001890
Shaohua Li306eb6b2017-08-14 15:04:57 -07001891 null_config_discard(nullb);
Jens Axboef2298c02013-10-25 11:52:25 +01001892
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001893 sprintf(nullb->disk_name, "nullb%d", nullb->index);
1894
Shaohua Li2984c862017-08-14 15:04:52 -07001895 if (dev->use_lightnvm)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001896 rv = null_nvm_register(nullb);
1897 else
1898 rv = null_gendisk_register(nullb);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001899
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001900 if (rv)
1901 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001902
Matias Bjørlinga5143792016-02-11 14:49:13 +01001903 mutex_lock(&lock);
1904 list_add_tail(&nullb->list, &nullb_list);
1905 mutex_unlock(&lock);
Wenwei Tao3681c85d2016-03-05 00:27:04 +08001906
Jens Axboef2298c02013-10-25 11:52:25 +01001907 return 0;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001908out_cleanup_blk_queue:
1909 blk_cleanup_queue(nullb->q);
1910out_cleanup_tags:
Shaohua Li2984c862017-08-14 15:04:52 -07001911 if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001912 blk_mq_free_tag_set(nullb->tag_set);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001913out_cleanup_queues:
1914 cleanup_queues(nullb);
1915out_free_nullb:
1916 kfree(nullb);
1917out:
Robert Elliottdc501dc2014-09-02 11:38:49 -05001918 return rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001919}
1920
1921static int __init null_init(void)
1922{
Minfei Huangaf096e22015-12-08 13:47:34 -07001923 int ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001924 unsigned int i;
Minfei Huangaf096e22015-12-08 13:47:34 -07001925 struct nullb *nullb;
Shaohua Li2984c862017-08-14 15:04:52 -07001926 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +01001927
Shaohua Lideb78b42017-08-14 15:04:59 -07001928 /* check for nullb_page.bitmap */
1929 if (sizeof(unsigned long) * 8 - 2 < (PAGE_SIZE >> SECTOR_SHIFT))
1930 return -EINVAL;
1931
Shaohua Li2984c862017-08-14 15:04:52 -07001932 if (g_bs > PAGE_SIZE) {
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301933 pr_warn("null_blk: invalid block size\n");
1934 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
Shaohua Li2984c862017-08-14 15:04:52 -07001935 g_bs = PAGE_SIZE;
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301936 }
Jens Axboef2298c02013-10-25 11:52:25 +01001937
Shaohua Li2984c862017-08-14 15:04:52 -07001938 if (g_use_lightnvm && g_bs != 4096) {
Matias Bjørling6bb95352015-11-19 12:50:08 +01001939 pr_warn("null_blk: LightNVM only supports 4k block size\n");
1940 pr_warn("null_blk: defaults block size to 4k\n");
Shaohua Li2984c862017-08-14 15:04:52 -07001941 g_bs = 4096;
Matias Bjørling6bb95352015-11-19 12:50:08 +01001942 }
1943
Shaohua Li2984c862017-08-14 15:04:52 -07001944 if (g_use_lightnvm && g_queue_mode != NULL_Q_MQ) {
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001945 pr_warn("null_blk: LightNVM only supported for blk-mq\n");
1946 pr_warn("null_blk: defaults queue mode to blk-mq\n");
Shaohua Li2984c862017-08-14 15:04:52 -07001947 g_queue_mode = NULL_Q_MQ;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001948 }
1949
Shaohua Li2984c862017-08-14 15:04:52 -07001950 if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) {
1951 if (g_submit_queues != nr_online_nodes) {
weiping zhang558ab3002017-08-03 00:26:39 +08001952 pr_warn("null_blk: submit_queues param is set to %u.\n",
Matias Bjorlingd15ee6b2013-12-18 13:41:44 +01001953 nr_online_nodes);
Shaohua Li2984c862017-08-14 15:04:52 -07001954 g_submit_queues = nr_online_nodes;
Matias Bjørlingfc1bc352013-12-21 00:11:01 +01001955 }
Shaohua Li2984c862017-08-14 15:04:52 -07001956 } else if (g_submit_queues > nr_cpu_ids)
1957 g_submit_queues = nr_cpu_ids;
1958 else if (g_submit_queues <= 0)
1959 g_submit_queues = 1;
Jens Axboef2298c02013-10-25 11:52:25 +01001960
Shaohua Li2984c862017-08-14 15:04:52 -07001961 if (g_queue_mode == NULL_Q_MQ && shared_tags) {
1962 ret = null_init_tag_set(NULL, &tag_set);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001963 if (ret)
1964 return ret;
1965 }
1966
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001967 config_group_init(&nullb_subsys.su_group);
1968 mutex_init(&nullb_subsys.su_mutex);
1969
1970 ret = configfs_register_subsystem(&nullb_subsys);
1971 if (ret)
1972 goto err_tagset;
1973
Jens Axboef2298c02013-10-25 11:52:25 +01001974 mutex_init(&lock);
1975
Jens Axboef2298c02013-10-25 11:52:25 +01001976 null_major = register_blkdev(0, "nullb");
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001977 if (null_major < 0) {
1978 ret = null_major;
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001979 goto err_conf;
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001980 }
Jens Axboef2298c02013-10-25 11:52:25 +01001981
Shaohua Li2984c862017-08-14 15:04:52 -07001982 if (g_use_lightnvm) {
Matias Bjørling6bb95352015-11-19 12:50:08 +01001983 ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64),
1984 0, 0, NULL);
1985 if (!ppa_cache) {
1986 pr_err("null_blk: unable to create ppa cache\n");
Minfei Huangaf096e22015-12-08 13:47:34 -07001987 ret = -ENOMEM;
Matias Bjørling6bb95352015-11-19 12:50:08 +01001988 goto err_ppa;
Jens Axboef2298c02013-10-25 11:52:25 +01001989 }
1990 }
1991
Minfei Huangaf096e22015-12-08 13:47:34 -07001992 for (i = 0; i < nr_devices; i++) {
Shaohua Li2984c862017-08-14 15:04:52 -07001993 dev = null_alloc_dev();
1994 if (!dev)
Minfei Huangaf096e22015-12-08 13:47:34 -07001995 goto err_dev;
Shaohua Li2984c862017-08-14 15:04:52 -07001996 ret = null_add_dev(dev);
1997 if (ret) {
1998 null_free_dev(dev);
1999 goto err_dev;
2000 }
Minfei Huangaf096e22015-12-08 13:47:34 -07002001 }
2002
Jens Axboef2298c02013-10-25 11:52:25 +01002003 pr_info("null: module loaded\n");
2004 return 0;
Minfei Huangaf096e22015-12-08 13:47:34 -07002005
2006err_dev:
2007 while (!list_empty(&nullb_list)) {
2008 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07002009 dev = nullb->dev;
Minfei Huangaf096e22015-12-08 13:47:34 -07002010 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07002011 null_free_dev(dev);
Minfei Huangaf096e22015-12-08 13:47:34 -07002012 }
Matias Bjørling6bb95352015-11-19 12:50:08 +01002013 kmem_cache_destroy(ppa_cache);
Minfei Huangaf096e22015-12-08 13:47:34 -07002014err_ppa:
2015 unregister_blkdev(null_major, "nullb");
Shaohua Li3bf2bd22017-08-14 15:04:53 -07002016err_conf:
2017 configfs_unregister_subsystem(&nullb_subsys);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03002018err_tagset:
Shaohua Li2984c862017-08-14 15:04:52 -07002019 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Max Gurtovoydb2d1532017-07-06 18:00:07 +03002020 blk_mq_free_tag_set(&tag_set);
Minfei Huangaf096e22015-12-08 13:47:34 -07002021 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01002022}
2023
2024static void __exit null_exit(void)
2025{
2026 struct nullb *nullb;
2027
Shaohua Li3bf2bd22017-08-14 15:04:53 -07002028 configfs_unregister_subsystem(&nullb_subsys);
2029
Jens Axboef2298c02013-10-25 11:52:25 +01002030 unregister_blkdev(null_major, "nullb");
2031
2032 mutex_lock(&lock);
2033 while (!list_empty(&nullb_list)) {
Shaohua Li2984c862017-08-14 15:04:52 -07002034 struct nullb_device *dev;
2035
Jens Axboef2298c02013-10-25 11:52:25 +01002036 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07002037 dev = nullb->dev;
Jens Axboef2298c02013-10-25 11:52:25 +01002038 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07002039 null_free_dev(dev);
Jens Axboef2298c02013-10-25 11:52:25 +01002040 }
2041 mutex_unlock(&lock);
Matias Bjørling6bb95352015-11-19 12:50:08 +01002042
Shaohua Li2984c862017-08-14 15:04:52 -07002043 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Jens Axboe82f402f2017-06-20 14:22:01 -06002044 blk_mq_free_tag_set(&tag_set);
2045
Matias Bjørling6bb95352015-11-19 12:50:08 +01002046 kmem_cache_destroy(ppa_cache);
Jens Axboef2298c02013-10-25 11:52:25 +01002047}
2048
2049module_init(null_init);
2050module_exit(null_exit);
2051
Jens Axboe231b3db2017-08-25 12:53:15 -06002052MODULE_AUTHOR("Jens Axboe <axboe@kernel.dk>");
Jens Axboef2298c02013-10-25 11:52:25 +01002053MODULE_LICENSE("GPL");