blob: e138a670a2a4b0713591ffdcf95f66ec7862f760 [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>
Jens Axboef2298c02013-10-25 11:52:25 +010017
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -070018#define SECTOR_SHIFT 9
19#define PAGE_SECTORS_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
20#define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)
21#define SECTOR_SIZE (1 << SECTOR_SHIFT)
22#define SECTOR_MASK (PAGE_SECTORS - 1)
23
24#define FREE_BATCH 16
25
Shaohua Lieff2c4f2017-08-14 15:04:58 -070026#define TICKS_PER_SEC 50ULL
27#define TIMER_INTERVAL (NSEC_PER_SEC / TICKS_PER_SEC)
28
29static inline u64 mb_per_tick(int mbps)
30{
31 return (1 << 20) / TICKS_PER_SEC * ((u64) mbps);
32}
33
Jens Axboef2298c02013-10-25 11:52:25 +010034struct nullb_cmd {
35 struct list_head list;
36 struct llist_node ll_list;
37 struct call_single_data csd;
38 struct request *rq;
39 struct bio *bio;
40 unsigned int tag;
41 struct nullb_queue *nq;
Paolo Valente3c395a92015-12-01 11:48:17 +010042 struct hrtimer timer;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -070043 blk_status_t error;
Jens Axboef2298c02013-10-25 11:52:25 +010044};
45
46struct nullb_queue {
47 unsigned long *tag_map;
48 wait_queue_head_t wait;
49 unsigned int queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -070050 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +010051
52 struct nullb_cmd *cmds;
53};
54
Shaohua Li3bf2bd22017-08-14 15:04:53 -070055/*
56 * Status flags for nullb_device.
57 *
58 * CONFIGURED: Device has been configured and turned on. Cannot reconfigure.
59 * UP: Device is currently on and visible in userspace.
Shaohua Lieff2c4f2017-08-14 15:04:58 -070060 * THROTTLED: Device is being throttled.
Shaohua Lideb78b42017-08-14 15:04:59 -070061 * CACHE: Device is using a write-back cache.
Shaohua Li3bf2bd22017-08-14 15:04:53 -070062 */
63enum nullb_device_flags {
64 NULLB_DEV_FL_CONFIGURED = 0,
65 NULLB_DEV_FL_UP = 1,
Shaohua Lieff2c4f2017-08-14 15:04:58 -070066 NULLB_DEV_FL_THROTTLED = 2,
Shaohua Lideb78b42017-08-14 15:04:59 -070067 NULLB_DEV_FL_CACHE = 3,
Shaohua Li3bf2bd22017-08-14 15:04:53 -070068};
69
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -070070/*
71 * nullb_page is a page in memory for nullb devices.
72 *
73 * @page: The page holding the data.
74 * @bitmap: The bitmap represents which sector in the page has data.
75 * Each bit represents one block size. For example, sector 8
76 * will use the 7th bit
Shaohua Lideb78b42017-08-14 15:04:59 -070077 * The highest 2 bits of bitmap are for special purpose. LOCK means the cache
78 * page is being flushing to storage. FREE means the cache page is freed and
79 * should be skipped from flushing to storage. Please see
80 * null_make_cache_space
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -070081 */
82struct nullb_page {
83 struct page *page;
84 unsigned long bitmap;
85};
Shaohua Lideb78b42017-08-14 15:04:59 -070086#define NULLB_PAGE_LOCK (sizeof(unsigned long) * 8 - 1)
87#define NULLB_PAGE_FREE (sizeof(unsigned long) * 8 - 2)
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -070088
Shaohua Li2984c862017-08-14 15:04:52 -070089struct nullb_device {
90 struct nullb *nullb;
Shaohua Li3bf2bd22017-08-14 15:04:53 -070091 struct config_item item;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -070092 struct radix_tree_root data; /* data stored in the disk */
Shaohua Lideb78b42017-08-14 15:04:59 -070093 struct radix_tree_root cache; /* disk cache data */
Shaohua Li3bf2bd22017-08-14 15:04:53 -070094 unsigned long flags; /* device flags */
Shaohua Lideb78b42017-08-14 15:04:59 -070095 unsigned int curr_cache;
Shaohua Li2984c862017-08-14 15:04:52 -070096
97 unsigned long size; /* device size in MB */
98 unsigned long completion_nsec; /* time in ns to complete a request */
Shaohua Lideb78b42017-08-14 15:04:59 -070099 unsigned long cache_size; /* disk cache size in MB */
Shaohua Li2984c862017-08-14 15:04:52 -0700100 unsigned int submit_queues; /* number of submission queues */
101 unsigned int home_node; /* home node for the device */
102 unsigned int queue_mode; /* block interface */
103 unsigned int blocksize; /* block size */
104 unsigned int irqmode; /* IRQ completion handler */
105 unsigned int hw_queue_depth; /* queue depth */
Shaohua Licedcafa2017-08-14 15:04:54 -0700106 unsigned int index; /* index of the disk, only valid with a disk */
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700107 unsigned int mbps; /* Bandwidth throttle cap (in MB/s) */
Shaohua Li2984c862017-08-14 15:04:52 -0700108 bool use_lightnvm; /* register as a LightNVM device */
109 bool blocking; /* blocking blk-mq device */
110 bool use_per_node_hctx; /* use per-node allocation for hardware context */
Shaohua Licedcafa2017-08-14 15:04:54 -0700111 bool power; /* power on/off the device */
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700112 bool memory_backed; /* if data is stored in memory */
Shaohua Li306eb6b2017-08-14 15:04:57 -0700113 bool discard; /* if support discard */
Shaohua Li2984c862017-08-14 15:04:52 -0700114};
115
Jens Axboef2298c02013-10-25 11:52:25 +0100116struct nullb {
Shaohua Li2984c862017-08-14 15:04:52 -0700117 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +0100118 struct list_head list;
119 unsigned int index;
120 struct request_queue *q;
121 struct gendisk *disk;
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200122 struct nvm_dev *ndev;
Jens Axboe82f402f2017-06-20 14:22:01 -0600123 struct blk_mq_tag_set *tag_set;
124 struct blk_mq_tag_set __tag_set;
Jens Axboef2298c02013-10-25 11:52:25 +0100125 unsigned int queue_depth;
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700126 atomic_long_t cur_bytes;
127 struct hrtimer bw_timer;
Shaohua Lideb78b42017-08-14 15:04:59 -0700128 unsigned long cache_flush_pos;
Jens Axboef2298c02013-10-25 11:52:25 +0100129 spinlock_t lock;
130
131 struct nullb_queue *queues;
132 unsigned int nr_queues;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +0100133 char disk_name[DISK_NAME_LEN];
Jens Axboef2298c02013-10-25 11:52:25 +0100134};
135
136static LIST_HEAD(nullb_list);
137static struct mutex lock;
138static int null_major;
Shaohua Li94bc02e2017-08-14 15:04:55 -0700139static DEFINE_IDA(nullb_indexes);
Matias Bjørling6bb95352015-11-19 12:50:08 +0100140static struct kmem_cache *ppa_cache;
Jens Axboe82f402f2017-06-20 14:22:01 -0600141static struct blk_mq_tag_set tag_set;
Jens Axboef2298c02013-10-25 11:52:25 +0100142
Jens Axboef2298c02013-10-25 11:52:25 +0100143enum {
144 NULL_IRQ_NONE = 0,
145 NULL_IRQ_SOFTIRQ = 1,
146 NULL_IRQ_TIMER = 2,
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800147};
Jens Axboef2298c02013-10-25 11:52:25 +0100148
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800149enum {
Jens Axboef2298c02013-10-25 11:52:25 +0100150 NULL_Q_BIO = 0,
151 NULL_Q_RQ = 1,
152 NULL_Q_MQ = 2,
153};
154
Shaohua Li2984c862017-08-14 15:04:52 -0700155static int g_submit_queues = 1;
156module_param_named(submit_queues, g_submit_queues, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100157MODULE_PARM_DESC(submit_queues, "Number of submission queues");
158
Shaohua Li2984c862017-08-14 15:04:52 -0700159static int g_home_node = NUMA_NO_NODE;
160module_param_named(home_node, g_home_node, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100161MODULE_PARM_DESC(home_node, "Home node for the device");
162
Shaohua Li2984c862017-08-14 15:04:52 -0700163static int g_queue_mode = NULL_Q_MQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700164
165static int null_param_store_val(const char *str, int *val, int min, int max)
166{
167 int ret, new_val;
168
169 ret = kstrtoint(str, 10, &new_val);
170 if (ret)
171 return -EINVAL;
172
173 if (new_val < min || new_val > max)
174 return -EINVAL;
175
176 *val = new_val;
177 return 0;
178}
179
180static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
181{
Shaohua Li2984c862017-08-14 15:04:52 -0700182 return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
Matias Bjorling709c8662014-11-26 14:45:48 -0700183}
184
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930185static const struct kernel_param_ops null_queue_mode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700186 .set = null_set_queue_mode,
187 .get = param_get_int,
188};
189
Shaohua Li2984c862017-08-14 15:04:52 -0700190device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, S_IRUGO);
Mike Snitzer54ae81c2014-06-11 17:13:50 -0400191MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
Jens Axboef2298c02013-10-25 11:52:25 +0100192
Shaohua Li2984c862017-08-14 15:04:52 -0700193static int g_gb = 250;
194module_param_named(gb, g_gb, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100195MODULE_PARM_DESC(gb, "Size in GB");
196
Shaohua Li2984c862017-08-14 15:04:52 -0700197static int g_bs = 512;
198module_param_named(bs, g_bs, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100199MODULE_PARM_DESC(bs, "Block size (in bytes)");
200
Jens Axboe82f402f2017-06-20 14:22:01 -0600201static int nr_devices = 1;
Jens Axboef2298c02013-10-25 11:52:25 +0100202module_param(nr_devices, int, S_IRUGO);
203MODULE_PARM_DESC(nr_devices, "Number of devices to register");
204
Shaohua Li2984c862017-08-14 15:04:52 -0700205static bool g_use_lightnvm;
206module_param_named(use_lightnvm, g_use_lightnvm, bool, S_IRUGO);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +0100207MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device");
208
Shaohua Li2984c862017-08-14 15:04:52 -0700209static bool g_blocking;
210module_param_named(blocking, g_blocking, bool, S_IRUGO);
Jens Axboedb5bcf82017-03-30 13:44:26 -0600211MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");
212
Jens Axboe82f402f2017-06-20 14:22:01 -0600213static bool shared_tags;
214module_param(shared_tags, bool, S_IRUGO);
215MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");
216
Shaohua Li2984c862017-08-14 15:04:52 -0700217static int g_irqmode = NULL_IRQ_SOFTIRQ;
Matias Bjorling709c8662014-11-26 14:45:48 -0700218
219static int null_set_irqmode(const char *str, const struct kernel_param *kp)
220{
Shaohua Li2984c862017-08-14 15:04:52 -0700221 return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
Matias Bjorling709c8662014-11-26 14:45:48 -0700222 NULL_IRQ_TIMER);
223}
224
Luis R. Rodriguez9c278472015-05-27 11:09:38 +0930225static const struct kernel_param_ops null_irqmode_param_ops = {
Matias Bjorling709c8662014-11-26 14:45:48 -0700226 .set = null_set_irqmode,
227 .get = param_get_int,
228};
229
Shaohua Li2984c862017-08-14 15:04:52 -0700230device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100231MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
232
Shaohua Li2984c862017-08-14 15:04:52 -0700233static unsigned long g_completion_nsec = 10000;
234module_param_named(completion_nsec, g_completion_nsec, ulong, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100235MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
236
Shaohua Li2984c862017-08-14 15:04:52 -0700237static int g_hw_queue_depth = 64;
238module_param_named(hw_queue_depth, g_hw_queue_depth, int, S_IRUGO);
Jens Axboef2298c02013-10-25 11:52:25 +0100239MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
240
Shaohua Li2984c862017-08-14 15:04:52 -0700241static bool g_use_per_node_hctx;
242module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, S_IRUGO);
Matias Bjørling20005242013-12-21 00:11:00 +0100243MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
Jens Axboef2298c02013-10-25 11:52:25 +0100244
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700245static struct nullb_device *null_alloc_dev(void);
246static void null_free_dev(struct nullb_device *dev);
Shaohua Licedcafa2017-08-14 15:04:54 -0700247static void null_del_dev(struct nullb *nullb);
248static int null_add_dev(struct nullb_device *dev);
Shaohua Lideb78b42017-08-14 15:04:59 -0700249static void null_free_device_storage(struct nullb_device *dev, bool is_cache);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700250
251static inline struct nullb_device *to_nullb_device(struct config_item *item)
252{
253 return item ? container_of(item, struct nullb_device, item) : NULL;
254}
255
256static inline ssize_t nullb_device_uint_attr_show(unsigned int val, char *page)
257{
258 return snprintf(page, PAGE_SIZE, "%u\n", val);
259}
260
261static inline ssize_t nullb_device_ulong_attr_show(unsigned long val,
262 char *page)
263{
264 return snprintf(page, PAGE_SIZE, "%lu\n", val);
265}
266
267static inline ssize_t nullb_device_bool_attr_show(bool val, char *page)
268{
269 return snprintf(page, PAGE_SIZE, "%u\n", val);
270}
271
272static ssize_t nullb_device_uint_attr_store(unsigned int *val,
273 const char *page, size_t count)
274{
275 unsigned int tmp;
276 int result;
277
278 result = kstrtouint(page, 0, &tmp);
279 if (result)
280 return result;
281
282 *val = tmp;
283 return count;
284}
285
286static ssize_t nullb_device_ulong_attr_store(unsigned long *val,
287 const char *page, size_t count)
288{
289 int result;
290 unsigned long tmp;
291
292 result = kstrtoul(page, 0, &tmp);
293 if (result)
294 return result;
295
296 *val = tmp;
297 return count;
298}
299
300static ssize_t nullb_device_bool_attr_store(bool *val, const char *page,
301 size_t count)
302{
303 bool tmp;
304 int result;
305
306 result = kstrtobool(page, &tmp);
307 if (result)
308 return result;
309
310 *val = tmp;
311 return count;
312}
313
314/* The following macro should only be used with TYPE = {uint, ulong, bool}. */
315#define NULLB_DEVICE_ATTR(NAME, TYPE) \
316static ssize_t \
317nullb_device_##NAME##_show(struct config_item *item, char *page) \
318{ \
319 return nullb_device_##TYPE##_attr_show( \
320 to_nullb_device(item)->NAME, page); \
321} \
322static ssize_t \
323nullb_device_##NAME##_store(struct config_item *item, const char *page, \
324 size_t count) \
325{ \
326 if (test_bit(NULLB_DEV_FL_CONFIGURED, &to_nullb_device(item)->flags)) \
327 return -EBUSY; \
328 return nullb_device_##TYPE##_attr_store( \
329 &to_nullb_device(item)->NAME, page, count); \
330} \
331CONFIGFS_ATTR(nullb_device_, NAME);
332
333NULLB_DEVICE_ATTR(size, ulong);
334NULLB_DEVICE_ATTR(completion_nsec, ulong);
335NULLB_DEVICE_ATTR(submit_queues, uint);
336NULLB_DEVICE_ATTR(home_node, uint);
337NULLB_DEVICE_ATTR(queue_mode, uint);
338NULLB_DEVICE_ATTR(blocksize, uint);
339NULLB_DEVICE_ATTR(irqmode, uint);
340NULLB_DEVICE_ATTR(hw_queue_depth, uint);
Shaohua Licedcafa2017-08-14 15:04:54 -0700341NULLB_DEVICE_ATTR(index, uint);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700342NULLB_DEVICE_ATTR(use_lightnvm, bool);
343NULLB_DEVICE_ATTR(blocking, bool);
344NULLB_DEVICE_ATTR(use_per_node_hctx, bool);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700345NULLB_DEVICE_ATTR(memory_backed, bool);
Shaohua Li306eb6b2017-08-14 15:04:57 -0700346NULLB_DEVICE_ATTR(discard, bool);
Shaohua Lieff2c4f2017-08-14 15:04:58 -0700347NULLB_DEVICE_ATTR(mbps, uint);
Shaohua Lideb78b42017-08-14 15:04:59 -0700348NULLB_DEVICE_ATTR(cache_size, ulong);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700349
Shaohua Licedcafa2017-08-14 15:04:54 -0700350static ssize_t nullb_device_power_show(struct config_item *item, char *page)
351{
352 return nullb_device_bool_attr_show(to_nullb_device(item)->power, page);
353}
354
355static ssize_t nullb_device_power_store(struct config_item *item,
356 const char *page, size_t count)
357{
358 struct nullb_device *dev = to_nullb_device(item);
359 bool newp = false;
360 ssize_t ret;
361
362 ret = nullb_device_bool_attr_store(&newp, page, count);
363 if (ret < 0)
364 return ret;
365
366 if (!dev->power && newp) {
367 if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
368 return count;
369 if (null_add_dev(dev)) {
370 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
371 return -ENOMEM;
372 }
373
374 set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
375 dev->power = newp;
376 } else if (to_nullb_device(item)->power && !newp) {
377 mutex_lock(&lock);
378 dev->power = newp;
379 null_del_dev(dev->nullb);
380 mutex_unlock(&lock);
381 clear_bit(NULLB_DEV_FL_UP, &dev->flags);
382 }
383
384 return count;
385}
386
387CONFIGFS_ATTR(nullb_device_, power);
388
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700389static struct configfs_attribute *nullb_device_attrs[] = {
390 &nullb_device_attr_size,
391 &nullb_device_attr_completion_nsec,
392 &nullb_device_attr_submit_queues,
393 &nullb_device_attr_home_node,
394 &nullb_device_attr_queue_mode,
395 &nullb_device_attr_blocksize,
396 &nullb_device_attr_irqmode,
397 &nullb_device_attr_hw_queue_depth,
Shaohua Licedcafa2017-08-14 15:04:54 -0700398 &nullb_device_attr_index,
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700399 &nullb_device_attr_use_lightnvm,
400 &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 Li5bcd0e0c72017-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 Li3bf2bd22017-08-14 15:04:53 -0700407 NULL,
408};
409
410static void nullb_device_release(struct config_item *item)
411{
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700412 struct nullb_device *dev = to_nullb_device(item);
413
Shaohua Lideb78b42017-08-14 15:04:59 -0700414 null_free_device_storage(dev, false);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700415 null_free_dev(dev);
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700416}
417
418static struct configfs_item_operations nullb_device_ops = {
419 .release = nullb_device_release,
420};
421
422static struct config_item_type nullb_device_type = {
423 .ct_item_ops = &nullb_device_ops,
424 .ct_attrs = nullb_device_attrs,
425 .ct_owner = THIS_MODULE,
426};
427
428static struct
429config_item *nullb_group_make_item(struct config_group *group, const char *name)
430{
431 struct nullb_device *dev;
432
433 dev = null_alloc_dev();
434 if (!dev)
435 return ERR_PTR(-ENOMEM);
436
437 config_item_init_type_name(&dev->item, name, &nullb_device_type);
438
439 return &dev->item;
440}
441
442static void
443nullb_group_drop_item(struct config_group *group, struct config_item *item)
444{
Shaohua Licedcafa2017-08-14 15:04:54 -0700445 struct nullb_device *dev = to_nullb_device(item);
446
447 if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
448 mutex_lock(&lock);
449 dev->power = false;
450 null_del_dev(dev->nullb);
451 mutex_unlock(&lock);
452 }
453
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700454 config_item_put(item);
455}
456
457static ssize_t memb_group_features_show(struct config_item *item, char *page)
458{
Shaohua Lideb78b42017-08-14 15:04:59 -0700459 return snprintf(page, PAGE_SIZE, "memory_backed,discard,bandwidth,cache\n");
Shaohua Li3bf2bd22017-08-14 15:04:53 -0700460}
461
462CONFIGFS_ATTR_RO(memb_group_, features);
463
464static struct configfs_attribute *nullb_group_attrs[] = {
465 &memb_group_attr_features,
466 NULL,
467};
468
469static struct configfs_group_operations nullb_group_ops = {
470 .make_item = nullb_group_make_item,
471 .drop_item = nullb_group_drop_item,
472};
473
474static struct config_item_type nullb_group_type = {
475 .ct_group_ops = &nullb_group_ops,
476 .ct_attrs = nullb_group_attrs,
477 .ct_owner = THIS_MODULE,
478};
479
480static struct configfs_subsystem nullb_subsys = {
481 .su_group = {
482 .cg_item = {
483 .ci_namebuf = "nullb",
484 .ci_type = &nullb_group_type,
485 },
486 },
487};
488
Shaohua Lideb78b42017-08-14 15:04:59 -0700489static inline int null_cache_active(struct nullb *nullb)
490{
491 return test_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
492}
493
Shaohua Li2984c862017-08-14 15:04:52 -0700494static struct nullb_device *null_alloc_dev(void)
495{
496 struct nullb_device *dev;
497
498 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
499 if (!dev)
500 return NULL;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700501 INIT_RADIX_TREE(&dev->data, GFP_ATOMIC);
Shaohua Lideb78b42017-08-14 15:04:59 -0700502 INIT_RADIX_TREE(&dev->cache, GFP_ATOMIC);
Shaohua Li2984c862017-08-14 15:04:52 -0700503 dev->size = g_gb * 1024;
504 dev->completion_nsec = g_completion_nsec;
505 dev->submit_queues = g_submit_queues;
506 dev->home_node = g_home_node;
507 dev->queue_mode = g_queue_mode;
508 dev->blocksize = g_bs;
509 dev->irqmode = g_irqmode;
510 dev->hw_queue_depth = g_hw_queue_depth;
511 dev->use_lightnvm = g_use_lightnvm;
512 dev->blocking = g_blocking;
513 dev->use_per_node_hctx = g_use_per_node_hctx;
514 return dev;
515}
516
517static void null_free_dev(struct nullb_device *dev)
518{
519 kfree(dev);
520}
521
Jens Axboef2298c02013-10-25 11:52:25 +0100522static void put_tag(struct nullb_queue *nq, unsigned int tag)
523{
524 clear_bit_unlock(tag, nq->tag_map);
525
526 if (waitqueue_active(&nq->wait))
527 wake_up(&nq->wait);
528}
529
530static unsigned int get_tag(struct nullb_queue *nq)
531{
532 unsigned int tag;
533
534 do {
535 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
536 if (tag >= nq->queue_depth)
537 return -1U;
538 } while (test_and_set_bit_lock(tag, nq->tag_map));
539
540 return tag;
541}
542
543static void free_cmd(struct nullb_cmd *cmd)
544{
545 put_tag(cmd->nq, cmd->tag);
546}
547
Paolo Valente3c395a92015-12-01 11:48:17 +0100548static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
549
Jens Axboef2298c02013-10-25 11:52:25 +0100550static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
551{
552 struct nullb_cmd *cmd;
553 unsigned int tag;
554
555 tag = get_tag(nq);
556 if (tag != -1U) {
557 cmd = &nq->cmds[tag];
558 cmd->tag = tag;
559 cmd->nq = nq;
Shaohua Li2984c862017-08-14 15:04:52 -0700560 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +0100561 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
562 HRTIMER_MODE_REL);
563 cmd->timer.function = null_cmd_timer_expired;
564 }
Jens Axboef2298c02013-10-25 11:52:25 +0100565 return cmd;
566 }
567
568 return NULL;
569}
570
571static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
572{
573 struct nullb_cmd *cmd;
574 DEFINE_WAIT(wait);
575
576 cmd = __alloc_cmd(nq);
577 if (cmd || !can_wait)
578 return cmd;
579
580 do {
581 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
582 cmd = __alloc_cmd(nq);
583 if (cmd)
584 break;
585
586 io_schedule();
587 } while (1);
588
589 finish_wait(&nq->wait, &wait);
590 return cmd;
591}
592
593static void end_cmd(struct nullb_cmd *cmd)
594{
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100595 struct request_queue *q = NULL;
Shaohua Li2984c862017-08-14 15:04:52 -0700596 int queue_mode = cmd->nq->dev->queue_mode;
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100597
Mike Krinkine8271202015-12-15 12:56:40 +0300598 if (cmd->rq)
599 q = cmd->rq->q;
600
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800601 switch (queue_mode) {
602 case NULL_Q_MQ:
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700603 blk_mq_end_request(cmd->rq, cmd->error);
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800604 return;
605 case NULL_Q_RQ:
606 INIT_LIST_HEAD(&cmd->rq->queuelist);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700607 blk_end_request_all(cmd->rq, cmd->error);
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800608 break;
609 case NULL_Q_BIO:
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700610 cmd->bio->bi_status = cmd->error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200611 bio_endio(cmd->bio);
Jens Axboe48cc6612015-12-28 13:02:47 -0700612 break;
Christoph Hellwigce2c3502014-02-10 03:24:40 -0800613 }
Jens Axboef2298c02013-10-25 11:52:25 +0100614
Jens Axboe48cc6612015-12-28 13:02:47 -0700615 free_cmd(cmd);
616
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100617 /* Restart queue if needed, as we are freeing a tag */
Jens Axboe48cc6612015-12-28 13:02:47 -0700618 if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100619 unsigned long flags;
620
621 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe48cc6612015-12-28 13:02:47 -0700622 blk_start_queue_async(q);
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100623 spin_unlock_irqrestore(q->queue_lock, flags);
624 }
Jens Axboef2298c02013-10-25 11:52:25 +0100625}
626
627static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
628{
Arianna Avanzinicf8ecc52015-12-01 11:48:18 +0100629 end_cmd(container_of(timer, struct nullb_cmd, timer));
Jens Axboef2298c02013-10-25 11:52:25 +0100630
631 return HRTIMER_NORESTART;
632}
633
634static void null_cmd_end_timer(struct nullb_cmd *cmd)
635{
Shaohua Li2984c862017-08-14 15:04:52 -0700636 ktime_t kt = cmd->nq->dev->completion_nsec;
Jens Axboef2298c02013-10-25 11:52:25 +0100637
Paolo Valente3c395a92015-12-01 11:48:17 +0100638 hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
Jens Axboef2298c02013-10-25 11:52:25 +0100639}
640
641static void null_softirq_done_fn(struct request *rq)
642{
Shaohua Li2984c862017-08-14 15:04:52 -0700643 struct nullb *nullb = rq->q->queuedata;
644
645 if (nullb->dev->queue_mode == NULL_Q_MQ)
Jens Axboed891fa72014-06-16 11:40:25 -0600646 end_cmd(blk_mq_rq_to_pdu(rq));
647 else
648 end_cmd(rq->special);
Jens Axboef2298c02013-10-25 11:52:25 +0100649}
650
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700651static struct nullb_page *null_alloc_page(gfp_t gfp_flags)
Jens Axboef2298c02013-10-25 11:52:25 +0100652{
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700653 struct nullb_page *t_page;
654
655 t_page = kmalloc(sizeof(struct nullb_page), gfp_flags);
656 if (!t_page)
657 goto out;
658
659 t_page->page = alloc_pages(gfp_flags, 0);
660 if (!t_page->page)
661 goto out_freepage;
662
663 t_page->bitmap = 0;
664 return t_page;
665out_freepage:
666 kfree(t_page);
667out:
668 return NULL;
669}
670
671static void null_free_page(struct nullb_page *t_page)
672{
Shaohua Lideb78b42017-08-14 15:04:59 -0700673 __set_bit(NULLB_PAGE_FREE, &t_page->bitmap);
674 if (test_bit(NULLB_PAGE_LOCK, &t_page->bitmap))
675 return;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700676 __free_page(t_page->page);
677 kfree(t_page);
678}
679
Shaohua Lideb78b42017-08-14 15:04:59 -0700680static void null_free_sector(struct nullb *nullb, sector_t sector,
681 bool is_cache)
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700682{
683 unsigned int sector_bit;
684 u64 idx;
685 struct nullb_page *t_page, *ret;
686 struct radix_tree_root *root;
687
Shaohua Lideb78b42017-08-14 15:04:59 -0700688 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700689 idx = sector >> PAGE_SECTORS_SHIFT;
690 sector_bit = (sector & SECTOR_MASK);
691
692 t_page = radix_tree_lookup(root, idx);
693 if (t_page) {
694 __clear_bit(sector_bit, &t_page->bitmap);
695
696 if (!t_page->bitmap) {
697 ret = radix_tree_delete_item(root, idx, t_page);
698 WARN_ON(ret != t_page);
699 null_free_page(ret);
Shaohua Lideb78b42017-08-14 15:04:59 -0700700 if (is_cache)
701 nullb->dev->curr_cache -= PAGE_SIZE;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700702 }
703 }
704}
705
706static struct nullb_page *null_radix_tree_insert(struct nullb *nullb, u64 idx,
Shaohua Lideb78b42017-08-14 15:04:59 -0700707 struct nullb_page *t_page, bool is_cache)
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700708{
709 struct radix_tree_root *root;
710
Shaohua Lideb78b42017-08-14 15:04:59 -0700711 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700712
713 if (radix_tree_insert(root, idx, t_page)) {
714 null_free_page(t_page);
715 t_page = radix_tree_lookup(root, idx);
716 WARN_ON(!t_page || t_page->page->index != idx);
Shaohua Lideb78b42017-08-14 15:04:59 -0700717 } else if (is_cache)
718 nullb->dev->curr_cache += PAGE_SIZE;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700719
720 return t_page;
721}
722
Shaohua Lideb78b42017-08-14 15:04:59 -0700723static void null_free_device_storage(struct nullb_device *dev, bool is_cache)
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700724{
725 unsigned long pos = 0;
726 int nr_pages;
727 struct nullb_page *ret, *t_pages[FREE_BATCH];
728 struct radix_tree_root *root;
729
Shaohua Lideb78b42017-08-14 15:04:59 -0700730 root = is_cache ? &dev->cache : &dev->data;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700731
732 do {
733 int i;
734
735 nr_pages = radix_tree_gang_lookup(root,
736 (void **)t_pages, pos, FREE_BATCH);
737
738 for (i = 0; i < nr_pages; i++) {
739 pos = t_pages[i]->page->index;
740 ret = radix_tree_delete_item(root, pos, t_pages[i]);
741 WARN_ON(ret != t_pages[i]);
742 null_free_page(ret);
743 }
744
745 pos++;
746 } while (nr_pages == FREE_BATCH);
Shaohua Lideb78b42017-08-14 15:04:59 -0700747
748 if (is_cache)
749 dev->curr_cache = 0;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700750}
751
Shaohua Lideb78b42017-08-14 15:04:59 -0700752static struct nullb_page *__null_lookup_page(struct nullb *nullb,
753 sector_t sector, bool for_write, bool is_cache)
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700754{
755 unsigned int sector_bit;
756 u64 idx;
757 struct nullb_page *t_page;
Shaohua Lideb78b42017-08-14 15:04:59 -0700758 struct radix_tree_root *root;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700759
760 idx = sector >> PAGE_SECTORS_SHIFT;
761 sector_bit = (sector & SECTOR_MASK);
762
Shaohua Lideb78b42017-08-14 15:04:59 -0700763 root = is_cache ? &nullb->dev->cache : &nullb->dev->data;
764 t_page = radix_tree_lookup(root, idx);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700765 WARN_ON(t_page && t_page->page->index != idx);
766
767 if (t_page && (for_write || test_bit(sector_bit, &t_page->bitmap)))
768 return t_page;
769
770 return NULL;
771}
772
Shaohua Lideb78b42017-08-14 15:04:59 -0700773static struct nullb_page *null_lookup_page(struct nullb *nullb,
774 sector_t sector, bool for_write, bool ignore_cache)
775{
776 struct nullb_page *page = NULL;
777
778 if (!ignore_cache)
779 page = __null_lookup_page(nullb, sector, for_write, true);
780 if (page)
781 return page;
782 return __null_lookup_page(nullb, sector, for_write, false);
783}
784
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700785static struct nullb_page *null_insert_page(struct nullb *nullb,
Shaohua Lideb78b42017-08-14 15:04:59 -0700786 sector_t sector, bool ignore_cache)
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700787{
788 u64 idx;
789 struct nullb_page *t_page;
790
Shaohua Lideb78b42017-08-14 15:04:59 -0700791 t_page = null_lookup_page(nullb, sector, true, ignore_cache);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700792 if (t_page)
793 return t_page;
794
795 spin_unlock_irq(&nullb->lock);
796
797 t_page = null_alloc_page(GFP_NOIO);
798 if (!t_page)
799 goto out_lock;
800
801 if (radix_tree_preload(GFP_NOIO))
802 goto out_freepage;
803
804 spin_lock_irq(&nullb->lock);
805 idx = sector >> PAGE_SECTORS_SHIFT;
806 t_page->page->index = idx;
Shaohua Lideb78b42017-08-14 15:04:59 -0700807 t_page = null_radix_tree_insert(nullb, idx, t_page, !ignore_cache);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700808 radix_tree_preload_end();
809
810 return t_page;
811out_freepage:
812 null_free_page(t_page);
813out_lock:
814 spin_lock_irq(&nullb->lock);
Shaohua Lideb78b42017-08-14 15:04:59 -0700815 return null_lookup_page(nullb, sector, true, ignore_cache);
816}
817
818static int null_flush_cache_page(struct nullb *nullb, struct nullb_page *c_page)
819{
820 int i;
821 unsigned int offset;
822 u64 idx;
823 struct nullb_page *t_page, *ret;
824 void *dst, *src;
825
826 idx = c_page->page->index;
827
828 t_page = null_insert_page(nullb, idx << PAGE_SECTORS_SHIFT, true);
829
830 __clear_bit(NULLB_PAGE_LOCK, &c_page->bitmap);
831 if (test_bit(NULLB_PAGE_FREE, &c_page->bitmap)) {
832 null_free_page(c_page);
833 if (t_page && t_page->bitmap == 0) {
834 ret = radix_tree_delete_item(&nullb->dev->data,
835 idx, t_page);
836 null_free_page(t_page);
837 }
838 return 0;
839 }
840
841 if (!t_page)
842 return -ENOMEM;
843
844 src = kmap_atomic(c_page->page);
845 dst = kmap_atomic(t_page->page);
846
847 for (i = 0; i < PAGE_SECTORS;
848 i += (nullb->dev->blocksize >> SECTOR_SHIFT)) {
849 if (test_bit(i, &c_page->bitmap)) {
850 offset = (i << SECTOR_SHIFT);
851 memcpy(dst + offset, src + offset,
852 nullb->dev->blocksize);
853 __set_bit(i, &t_page->bitmap);
854 }
855 }
856
857 kunmap_atomic(dst);
858 kunmap_atomic(src);
859
860 ret = radix_tree_delete_item(&nullb->dev->cache, idx, c_page);
861 null_free_page(ret);
862 nullb->dev->curr_cache -= PAGE_SIZE;
863
864 return 0;
865}
866
867static int null_make_cache_space(struct nullb *nullb, unsigned long n)
868{
869 int i, err, nr_pages;
870 struct nullb_page *c_pages[FREE_BATCH];
871 unsigned long flushed = 0, one_round;
872
873again:
874 if ((nullb->dev->cache_size * 1024 * 1024) >
875 nullb->dev->curr_cache + n || nullb->dev->curr_cache == 0)
876 return 0;
877
878 nr_pages = radix_tree_gang_lookup(&nullb->dev->cache,
879 (void **)c_pages, nullb->cache_flush_pos, FREE_BATCH);
880 /*
881 * nullb_flush_cache_page could unlock before using the c_pages. To
882 * avoid race, we don't allow page free
883 */
884 for (i = 0; i < nr_pages; i++) {
885 nullb->cache_flush_pos = c_pages[i]->page->index;
886 /*
887 * We found the page which is being flushed to disk by other
888 * threads
889 */
890 if (test_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap))
891 c_pages[i] = NULL;
892 else
893 __set_bit(NULLB_PAGE_LOCK, &c_pages[i]->bitmap);
894 }
895
896 one_round = 0;
897 for (i = 0; i < nr_pages; i++) {
898 if (c_pages[i] == NULL)
899 continue;
900 err = null_flush_cache_page(nullb, c_pages[i]);
901 if (err)
902 return err;
903 one_round++;
904 }
905 flushed += one_round << PAGE_SHIFT;
906
907 if (n > flushed) {
908 if (nr_pages == 0)
909 nullb->cache_flush_pos = 0;
910 if (one_round == 0) {
911 /* give other threads a chance */
912 spin_unlock_irq(&nullb->lock);
913 spin_lock_irq(&nullb->lock);
914 }
915 goto again;
916 }
917 return 0;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700918}
919
920static int copy_to_nullb(struct nullb *nullb, struct page *source,
Shaohua Lideb78b42017-08-14 15:04:59 -0700921 unsigned int off, sector_t sector, size_t n, bool is_fua)
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700922{
923 size_t temp, count = 0;
924 unsigned int offset;
925 struct nullb_page *t_page;
926 void *dst, *src;
927
928 while (count < n) {
929 temp = min_t(size_t, nullb->dev->blocksize, n - count);
930
Shaohua Lideb78b42017-08-14 15:04:59 -0700931 if (null_cache_active(nullb) && !is_fua)
932 null_make_cache_space(nullb, PAGE_SIZE);
933
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700934 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
Shaohua Lideb78b42017-08-14 15:04:59 -0700935 t_page = null_insert_page(nullb, sector,
936 !null_cache_active(nullb) || is_fua);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700937 if (!t_page)
938 return -ENOSPC;
939
940 src = kmap_atomic(source);
941 dst = kmap_atomic(t_page->page);
942 memcpy(dst + offset, src + off + count, temp);
943 kunmap_atomic(dst);
944 kunmap_atomic(src);
945
946 __set_bit(sector & SECTOR_MASK, &t_page->bitmap);
947
Shaohua Lideb78b42017-08-14 15:04:59 -0700948 if (is_fua)
949 null_free_sector(nullb, sector, true);
950
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700951 count += temp;
952 sector += temp >> SECTOR_SHIFT;
953 }
954 return 0;
955}
956
957static int copy_from_nullb(struct nullb *nullb, struct page *dest,
958 unsigned int off, sector_t sector, size_t n)
959{
960 size_t temp, count = 0;
961 unsigned int offset;
962 struct nullb_page *t_page;
963 void *dst, *src;
964
965 while (count < n) {
966 temp = min_t(size_t, nullb->dev->blocksize, n - count);
967
968 offset = (sector & SECTOR_MASK) << SECTOR_SHIFT;
Shaohua Lideb78b42017-08-14 15:04:59 -0700969 t_page = null_lookup_page(nullb, sector, false,
970 !null_cache_active(nullb));
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -0700971
972 dst = kmap_atomic(dest);
973 if (!t_page) {
974 memset(dst + off + count, 0, temp);
975 goto next;
976 }
977 src = kmap_atomic(t_page->page);
978 memcpy(dst + off + count, src + offset, temp);
979 kunmap_atomic(src);
980next:
981 kunmap_atomic(dst);
982
983 count += temp;
984 sector += temp >> SECTOR_SHIFT;
985 }
986 return 0;
987}
988
Shaohua Li306eb6b2017-08-14 15:04:57 -0700989static void null_handle_discard(struct nullb *nullb, sector_t sector, size_t n)
990{
991 size_t temp;
992
993 spin_lock_irq(&nullb->lock);
994 while (n > 0) {
995 temp = min_t(size_t, n, nullb->dev->blocksize);
Shaohua Lideb78b42017-08-14 15:04:59 -0700996 null_free_sector(nullb, sector, false);
997 if (null_cache_active(nullb))
998 null_free_sector(nullb, sector, true);
Shaohua Li306eb6b2017-08-14 15:04:57 -0700999 sector += temp >> SECTOR_SHIFT;
1000 n -= temp;
1001 }
1002 spin_unlock_irq(&nullb->lock);
1003}
1004
Shaohua Lideb78b42017-08-14 15:04:59 -07001005static int null_handle_flush(struct nullb *nullb)
1006{
1007 int err;
1008
1009 if (!null_cache_active(nullb))
1010 return 0;
1011
1012 spin_lock_irq(&nullb->lock);
1013 while (true) {
1014 err = null_make_cache_space(nullb,
1015 nullb->dev->cache_size * 1024 * 1024);
1016 if (err || nullb->dev->curr_cache == 0)
1017 break;
1018 }
1019
1020 WARN_ON(!radix_tree_empty(&nullb->dev->cache));
1021 spin_unlock_irq(&nullb->lock);
1022 return err;
1023}
1024
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001025static int null_transfer(struct nullb *nullb, struct page *page,
Shaohua Lideb78b42017-08-14 15:04:59 -07001026 unsigned int len, unsigned int off, bool is_write, sector_t sector,
1027 bool is_fua)
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001028{
1029 int err = 0;
1030
1031 if (!is_write) {
1032 err = copy_from_nullb(nullb, page, off, sector, len);
1033 flush_dcache_page(page);
1034 } else {
1035 flush_dcache_page(page);
Shaohua Lideb78b42017-08-14 15:04:59 -07001036 err = copy_to_nullb(nullb, page, off, sector, len, is_fua);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001037 }
1038
1039 return err;
1040}
1041
1042static int null_handle_rq(struct nullb_cmd *cmd)
1043{
1044 struct request *rq = cmd->rq;
1045 struct nullb *nullb = cmd->nq->dev->nullb;
1046 int err;
1047 unsigned int len;
1048 sector_t sector;
1049 struct req_iterator iter;
1050 struct bio_vec bvec;
1051
1052 sector = blk_rq_pos(rq);
1053
Shaohua Li306eb6b2017-08-14 15:04:57 -07001054 if (req_op(rq) == REQ_OP_DISCARD) {
1055 null_handle_discard(nullb, sector, blk_rq_bytes(rq));
1056 return 0;
1057 }
1058
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001059 spin_lock_irq(&nullb->lock);
1060 rq_for_each_segment(bvec, rq, iter) {
1061 len = bvec.bv_len;
1062 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
Shaohua Lideb78b42017-08-14 15:04:59 -07001063 op_is_write(req_op(rq)), sector,
1064 req_op(rq) & REQ_FUA);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001065 if (err) {
1066 spin_unlock_irq(&nullb->lock);
1067 return err;
1068 }
1069 sector += len >> SECTOR_SHIFT;
1070 }
1071 spin_unlock_irq(&nullb->lock);
1072
1073 return 0;
1074}
1075
1076static int null_handle_bio(struct nullb_cmd *cmd)
1077{
1078 struct bio *bio = cmd->bio;
1079 struct nullb *nullb = cmd->nq->dev->nullb;
1080 int err;
1081 unsigned int len;
1082 sector_t sector;
1083 struct bio_vec bvec;
1084 struct bvec_iter iter;
1085
1086 sector = bio->bi_iter.bi_sector;
1087
Shaohua Li306eb6b2017-08-14 15:04:57 -07001088 if (bio_op(bio) == REQ_OP_DISCARD) {
1089 null_handle_discard(nullb, sector,
1090 bio_sectors(bio) << SECTOR_SHIFT);
1091 return 0;
1092 }
1093
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001094 spin_lock_irq(&nullb->lock);
1095 bio_for_each_segment(bvec, bio, iter) {
1096 len = bvec.bv_len;
1097 err = null_transfer(nullb, bvec.bv_page, len, bvec.bv_offset,
Shaohua Lideb78b42017-08-14 15:04:59 -07001098 op_is_write(bio_op(bio)), sector,
1099 bio_op(bio) & REQ_FUA);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001100 if (err) {
1101 spin_unlock_irq(&nullb->lock);
1102 return err;
1103 }
1104 sector += len >> SECTOR_SHIFT;
1105 }
1106 spin_unlock_irq(&nullb->lock);
1107 return 0;
1108}
1109
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001110static void null_stop_queue(struct nullb *nullb)
1111{
1112 struct request_queue *q = nullb->q;
1113
1114 if (nullb->dev->queue_mode == NULL_Q_MQ)
1115 blk_mq_stop_hw_queues(q);
1116 else {
1117 spin_lock_irq(q->queue_lock);
1118 blk_stop_queue(q);
1119 spin_unlock_irq(q->queue_lock);
1120 }
1121}
1122
1123static void null_restart_queue_async(struct nullb *nullb)
1124{
1125 struct request_queue *q = nullb->q;
1126 unsigned long flags;
1127
1128 if (nullb->dev->queue_mode == NULL_Q_MQ)
1129 blk_mq_start_stopped_hw_queues(q, true);
1130 else {
1131 spin_lock_irqsave(q->queue_lock, flags);
1132 blk_start_queue_async(q);
1133 spin_unlock_irqrestore(q->queue_lock, flags);
1134 }
1135}
1136
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001137static blk_status_t null_handle_cmd(struct nullb_cmd *cmd)
1138{
1139 struct nullb_device *dev = cmd->nq->dev;
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001140 struct nullb *nullb = dev->nullb;
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001141 int err = 0;
1142
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001143 if (test_bit(NULLB_DEV_FL_THROTTLED, &dev->flags)) {
1144 struct request *rq = cmd->rq;
1145
1146 if (!hrtimer_active(&nullb->bw_timer))
1147 hrtimer_restart(&nullb->bw_timer);
1148
1149 if (atomic_long_sub_return(blk_rq_bytes(rq),
1150 &nullb->cur_bytes) < 0) {
1151 null_stop_queue(nullb);
1152 /* race with timer */
1153 if (atomic_long_read(&nullb->cur_bytes) > 0)
1154 null_restart_queue_async(nullb);
1155 if (dev->queue_mode == NULL_Q_RQ) {
1156 struct request_queue *q = nullb->q;
1157
1158 spin_lock_irq(q->queue_lock);
1159 rq->rq_flags |= RQF_DONTPREP;
1160 blk_requeue_request(q, rq);
1161 spin_unlock_irq(q->queue_lock);
1162 return BLK_STS_OK;
1163 } else
1164 /* requeue request */
1165 return BLK_STS_RESOURCE;
1166 }
1167 }
1168
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001169 if (dev->memory_backed) {
Shaohua Lideb78b42017-08-14 15:04:59 -07001170 if (dev->queue_mode == NULL_Q_BIO) {
1171 if (bio_op(cmd->bio) == REQ_OP_FLUSH)
1172 err = null_handle_flush(nullb);
1173 else
1174 err = null_handle_bio(cmd);
1175 } else {
1176 if (req_op(cmd->rq) == REQ_OP_FLUSH)
1177 err = null_handle_flush(nullb);
1178 else
1179 err = null_handle_rq(cmd);
1180 }
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001181 }
1182 cmd->error = errno_to_blk_status(err);
Jens Axboef2298c02013-10-25 11:52:25 +01001183 /* Complete IO by inline, softirq or timer */
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001184 switch (dev->irqmode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001185 case NULL_IRQ_SOFTIRQ:
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001186 switch (dev->queue_mode) {
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001187 case NULL_Q_MQ:
Christoph Hellwig08e00292017-04-20 16:03:09 +02001188 blk_mq_complete_request(cmd->rq);
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001189 break;
1190 case NULL_Q_RQ:
1191 blk_complete_request(cmd->rq);
1192 break;
1193 case NULL_Q_BIO:
1194 /*
1195 * XXX: no proper submitting cpu information available.
1196 */
1197 end_cmd(cmd);
1198 break;
1199 }
1200 break;
Jens Axboef2298c02013-10-25 11:52:25 +01001201 case NULL_IRQ_NONE:
1202 end_cmd(cmd);
1203 break;
Jens Axboef2298c02013-10-25 11:52:25 +01001204 case NULL_IRQ_TIMER:
1205 null_cmd_end_timer(cmd);
1206 break;
1207 }
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001208 return BLK_STS_OK;
Jens Axboef2298c02013-10-25 11:52:25 +01001209}
1210
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001211static enum hrtimer_restart nullb_bwtimer_fn(struct hrtimer *timer)
1212{
1213 struct nullb *nullb = container_of(timer, struct nullb, bw_timer);
1214 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1215 unsigned int mbps = nullb->dev->mbps;
1216
1217 if (atomic_long_read(&nullb->cur_bytes) == mb_per_tick(mbps))
1218 return HRTIMER_NORESTART;
1219
1220 atomic_long_set(&nullb->cur_bytes, mb_per_tick(mbps));
1221 null_restart_queue_async(nullb);
1222
1223 hrtimer_forward_now(&nullb->bw_timer, timer_interval);
1224
1225 return HRTIMER_RESTART;
1226}
1227
1228static void nullb_setup_bwtimer(struct nullb *nullb)
1229{
1230 ktime_t timer_interval = ktime_set(0, TIMER_INTERVAL);
1231
1232 hrtimer_init(&nullb->bw_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1233 nullb->bw_timer.function = nullb_bwtimer_fn;
1234 atomic_long_set(&nullb->cur_bytes, mb_per_tick(nullb->dev->mbps));
1235 hrtimer_start(&nullb->bw_timer, timer_interval, HRTIMER_MODE_REL);
1236}
1237
Jens Axboef2298c02013-10-25 11:52:25 +01001238static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
1239{
1240 int index = 0;
1241
1242 if (nullb->nr_queues != 1)
1243 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
1244
1245 return &nullb->queues[index];
1246}
1247
Jens Axboedece1632015-11-05 10:41:16 -07001248static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
Jens Axboef2298c02013-10-25 11:52:25 +01001249{
1250 struct nullb *nullb = q->queuedata;
1251 struct nullb_queue *nq = nullb_to_queue(nullb);
1252 struct nullb_cmd *cmd;
1253
1254 cmd = alloc_cmd(nq, 1);
1255 cmd->bio = bio;
1256
1257 null_handle_cmd(cmd);
Jens Axboedece1632015-11-05 10:41:16 -07001258 return BLK_QC_T_NONE;
Jens Axboef2298c02013-10-25 11:52:25 +01001259}
1260
1261static int null_rq_prep_fn(struct request_queue *q, struct request *req)
1262{
1263 struct nullb *nullb = q->queuedata;
1264 struct nullb_queue *nq = nullb_to_queue(nullb);
1265 struct nullb_cmd *cmd;
1266
1267 cmd = alloc_cmd(nq, 0);
1268 if (cmd) {
1269 cmd->rq = req;
1270 req->special = cmd;
1271 return BLKPREP_OK;
1272 }
Akinobu Mita8b70f452015-06-02 08:35:10 +09001273 blk_stop_queue(q);
Jens Axboef2298c02013-10-25 11:52:25 +01001274
1275 return BLKPREP_DEFER;
1276}
1277
1278static void null_request_fn(struct request_queue *q)
1279{
1280 struct request *rq;
1281
1282 while ((rq = blk_fetch_request(q)) != NULL) {
1283 struct nullb_cmd *cmd = rq->special;
1284
1285 spin_unlock_irq(q->queue_lock);
1286 null_handle_cmd(cmd);
1287 spin_lock_irq(q->queue_lock);
1288 }
1289}
1290
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001291static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -06001292 const struct blk_mq_queue_data *bd)
Jens Axboef2298c02013-10-25 11:52:25 +01001293{
Jens Axboe74c45052014-10-29 11:14:52 -06001294 struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
Shaohua Li2984c862017-08-14 15:04:52 -07001295 struct nullb_queue *nq = hctx->driver_data;
Jens Axboef2298c02013-10-25 11:52:25 +01001296
Jens Axboedb5bcf82017-03-30 13:44:26 -06001297 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1298
Shaohua Li2984c862017-08-14 15:04:52 -07001299 if (nq->dev->irqmode == NULL_IRQ_TIMER) {
Paolo Valente3c395a92015-12-01 11:48:17 +01001300 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1301 cmd->timer.function = null_cmd_timer_expired;
1302 }
Jens Axboe74c45052014-10-29 11:14:52 -06001303 cmd->rq = bd->rq;
Shaohua Li2984c862017-08-14 15:04:52 -07001304 cmd->nq = nq;
Jens Axboef2298c02013-10-25 11:52:25 +01001305
Jens Axboe74c45052014-10-29 11:14:52 -06001306 blk_mq_start_request(bd->rq);
Christoph Hellwige2490072014-09-13 16:40:09 -07001307
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001308 return null_handle_cmd(cmd);
Jens Axboef2298c02013-10-25 11:52:25 +01001309}
1310
Eric Biggersf363b082017-03-30 13:39:16 -07001311static const struct blk_mq_ops null_mq_ops = {
Jens Axboef2298c02013-10-25 11:52:25 +01001312 .queue_rq = null_queue_rq,
Christoph Hellwigce2c3502014-02-10 03:24:40 -08001313 .complete = null_softirq_done_fn,
Jens Axboef2298c02013-10-25 11:52:25 +01001314};
1315
Matias Bjørlingde65d2d2015-08-31 14:17:18 +02001316static void cleanup_queue(struct nullb_queue *nq)
1317{
1318 kfree(nq->tag_map);
1319 kfree(nq->cmds);
1320}
1321
1322static void cleanup_queues(struct nullb *nullb)
1323{
1324 int i;
1325
1326 for (i = 0; i < nullb->nr_queues; i++)
1327 cleanup_queue(&nullb->queues[i]);
1328
1329 kfree(nullb->queues);
1330}
1331
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001332#ifdef CONFIG_NVM
1333
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001334static void null_lnvm_end_io(struct request *rq, blk_status_t status)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001335{
1336 struct nvm_rq *rqd = rq->end_io_data;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001337
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001338 /* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */
1339 rqd->error = status ? -EIO : 0;
Matias Bjørling06894ef2017-01-31 13:17:17 +01001340 nvm_end_io(rqd);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001341
1342 blk_put_request(rq);
1343}
1344
Matias Bjørling16f26c32015-12-06 11:25:48 +01001345static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001346{
Matias Bjørling16f26c32015-12-06 11:25:48 +01001347 struct request_queue *q = dev->q;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001348 struct request *rq;
1349 struct bio *bio = rqd->bio;
1350
Christoph Hellwigaebf5262017-01-31 16:57:31 +01001351 rq = blk_mq_alloc_request(q,
1352 op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001353 if (IS_ERR(rq))
1354 return -ENOMEM;
1355
Bart Van Assche2644a3c2017-04-19 14:01:25 -07001356 blk_init_request_from_bio(rq, bio);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001357
1358 rq->end_io_data = rqd;
1359
1360 blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io);
1361
1362 return 0;
1363}
1364
Matias Bjørling16f26c32015-12-06 11:25:48 +01001365static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001366{
Shaohua Li2984c862017-08-14 15:04:52 -07001367 struct nullb *nullb = dev->q->queuedata;
1368 sector_t size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001369 sector_t blksize;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001370 struct nvm_id_group *grp;
1371
1372 id->ver_id = 0x1;
1373 id->vmnt = 0;
Matias Bjørlingbf643182016-02-04 15:13:27 +01001374 id->cap = 0x2;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001375 id->dom = 0x1;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001376
1377 id->ppaf.blk_offset = 0;
1378 id->ppaf.blk_len = 16;
1379 id->ppaf.pg_offset = 16;
1380 id->ppaf.pg_len = 16;
1381 id->ppaf.sect_offset = 32;
1382 id->ppaf.sect_len = 8;
1383 id->ppaf.pln_offset = 40;
1384 id->ppaf.pln_len = 8;
1385 id->ppaf.lun_offset = 48;
1386 id->ppaf.lun_len = 8;
1387 id->ppaf.ch_offset = 56;
1388 id->ppaf.ch_len = 8;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001389
Shaohua Li2984c862017-08-14 15:04:52 -07001390 sector_div(size, nullb->dev->blocksize); /* convert size to pages */
Arnd Bergmanne93d12a2016-01-13 23:04:08 +01001391 size >>= 8; /* concert size to pgs pr blk */
Matias Bjørling19bd6fe2017-01-31 13:17:15 +01001392 grp = &id->grp;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001393 grp->mtype = 0;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001394 grp->fmtype = 0;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001395 grp->num_ch = 1;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001396 grp->num_pg = 256;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001397 blksize = size;
Arnd Bergmanne93d12a2016-01-13 23:04:08 +01001398 size >>= 16;
Matias Bjørling5b40db92015-11-19 12:50:09 +01001399 grp->num_lun = size + 1;
Arnd Bergmanne93d12a2016-01-13 23:04:08 +01001400 sector_div(blksize, grp->num_lun);
Matias Bjørling5b40db92015-11-19 12:50:09 +01001401 grp->num_blk = blksize;
1402 grp->num_pln = 1;
1403
Shaohua Li2984c862017-08-14 15:04:52 -07001404 grp->fpg_sz = nullb->dev->blocksize;
1405 grp->csecs = nullb->dev->blocksize;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001406 grp->trdt = 25000;
1407 grp->trdm = 25000;
1408 grp->tprt = 500000;
1409 grp->tprm = 500000;
1410 grp->tbet = 1500000;
1411 grp->tbem = 1500000;
1412 grp->mpos = 0x010101; /* single plane rwe */
Shaohua Li2984c862017-08-14 15:04:52 -07001413 grp->cpar = nullb->dev->hw_queue_depth;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001414
1415 return 0;
1416}
1417
Matias Bjørling16f26c32015-12-06 11:25:48 +01001418static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001419{
1420 mempool_t *virtmem_pool;
1421
Matias Bjørling6bb95352015-11-19 12:50:08 +01001422 virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001423 if (!virtmem_pool) {
1424 pr_err("null_blk: Unable to create virtual memory pool\n");
1425 return NULL;
1426 }
1427
1428 return virtmem_pool;
1429}
1430
1431static void null_lnvm_destroy_dma_pool(void *pool)
1432{
1433 mempool_destroy(pool);
1434}
1435
Matias Bjørling16f26c32015-12-06 11:25:48 +01001436static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001437 gfp_t mem_flags, dma_addr_t *dma_handler)
1438{
1439 return mempool_alloc(pool, mem_flags);
1440}
1441
1442static void null_lnvm_dev_dma_free(void *pool, void *entry,
1443 dma_addr_t dma_handler)
1444{
1445 mempool_free(entry, pool);
1446}
1447
1448static struct nvm_dev_ops null_lnvm_dev_ops = {
1449 .identity = null_lnvm_id,
1450 .submit_io = null_lnvm_submit_io,
1451
1452 .create_dma_pool = null_lnvm_create_dma_pool,
1453 .destroy_dma_pool = null_lnvm_destroy_dma_pool,
1454 .dev_dma_alloc = null_lnvm_dev_dma_alloc,
1455 .dev_dma_free = null_lnvm_dev_dma_free,
1456
1457 /* Simulate nvme protocol restriction */
1458 .max_phys_sect = 64,
1459};
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001460
1461static int null_nvm_register(struct nullb *nullb)
1462{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001463 struct nvm_dev *dev;
1464 int rv;
1465
1466 dev = nvm_alloc_dev(0);
1467 if (!dev)
1468 return -ENOMEM;
1469
1470 dev->q = nullb->q;
1471 memcpy(dev->name, nullb->disk_name, DISK_NAME_LEN);
1472 dev->ops = &null_lnvm_dev_ops;
1473
1474 rv = nvm_register(dev);
1475 if (rv) {
1476 kfree(dev);
1477 return rv;
1478 }
1479 nullb->ndev = dev;
1480 return 0;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001481}
1482
1483static void null_nvm_unregister(struct nullb *nullb)
1484{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001485 nvm_unregister(nullb->ndev);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001486}
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001487#else
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001488static int null_nvm_register(struct nullb *nullb)
1489{
Yasuaki Ishimatsu92153d32016-11-16 08:26:11 -07001490 pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n");
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001491 return -EINVAL;
1492}
1493static void null_nvm_unregister(struct nullb *nullb) {}
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001494#endif /* CONFIG_NVM */
1495
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001496static void null_del_dev(struct nullb *nullb)
1497{
Shaohua Li2984c862017-08-14 15:04:52 -07001498 struct nullb_device *dev = nullb->dev;
1499
Shaohua Li94bc02e2017-08-14 15:04:55 -07001500 ida_simple_remove(&nullb_indexes, nullb->index);
1501
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001502 list_del_init(&nullb->list);
1503
Shaohua Li2984c862017-08-14 15:04:52 -07001504 if (dev->use_lightnvm)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001505 null_nvm_unregister(nullb);
1506 else
1507 del_gendisk(nullb->disk);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001508
1509 if (test_bit(NULLB_DEV_FL_THROTTLED, &nullb->dev->flags)) {
1510 hrtimer_cancel(&nullb->bw_timer);
1511 atomic_long_set(&nullb->cur_bytes, LONG_MAX);
1512 null_restart_queue_async(nullb);
1513 }
1514
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001515 blk_cleanup_queue(nullb->q);
Shaohua Li2984c862017-08-14 15:04:52 -07001516 if (dev->queue_mode == NULL_Q_MQ &&
1517 nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001518 blk_mq_free_tag_set(nullb->tag_set);
Shaohua Li2984c862017-08-14 15:04:52 -07001519 if (!dev->use_lightnvm)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001520 put_disk(nullb->disk);
1521 cleanup_queues(nullb);
Shaohua Lideb78b42017-08-14 15:04:59 -07001522 if (null_cache_active(nullb))
1523 null_free_device_storage(nullb->dev, true);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001524 kfree(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001525 dev->nullb = NULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001526}
1527
Shaohua Li306eb6b2017-08-14 15:04:57 -07001528static void null_config_discard(struct nullb *nullb)
1529{
1530 if (nullb->dev->discard == false)
1531 return;
1532 nullb->q->limits.discard_granularity = nullb->dev->blocksize;
1533 nullb->q->limits.discard_alignment = nullb->dev->blocksize;
1534 blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9);
1535 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, nullb->q);
1536}
1537
Jens Axboef2298c02013-10-25 11:52:25 +01001538static int null_open(struct block_device *bdev, fmode_t mode)
1539{
1540 return 0;
1541}
1542
1543static void null_release(struct gendisk *disk, fmode_t mode)
1544{
1545}
1546
1547static const struct block_device_operations null_fops = {
1548 .owner = THIS_MODULE,
1549 .open = null_open,
1550 .release = null_release,
1551};
1552
Jens Axboe82f402f2017-06-20 14:22:01 -06001553static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
1554{
1555 BUG_ON(!nullb);
1556 BUG_ON(!nq);
1557
1558 init_waitqueue_head(&nq->wait);
1559 nq->queue_depth = nullb->queue_depth;
Shaohua Li2984c862017-08-14 15:04:52 -07001560 nq->dev = nullb->dev;
Jens Axboe82f402f2017-06-20 14:22:01 -06001561}
1562
1563static void null_init_queues(struct nullb *nullb)
1564{
1565 struct request_queue *q = nullb->q;
1566 struct blk_mq_hw_ctx *hctx;
1567 struct nullb_queue *nq;
1568 int i;
1569
1570 queue_for_each_hw_ctx(q, hctx, i) {
1571 if (!hctx->nr_ctx || !hctx->tags)
1572 continue;
1573 nq = &nullb->queues[i];
1574 hctx->driver_data = nq;
1575 null_init_queue(nullb, nq);
1576 nullb->nr_queues++;
1577 }
1578}
1579
Jens Axboef2298c02013-10-25 11:52:25 +01001580static int setup_commands(struct nullb_queue *nq)
1581{
1582 struct nullb_cmd *cmd;
1583 int i, tag_size;
1584
1585 nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
1586 if (!nq->cmds)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001587 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001588
1589 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
1590 nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
1591 if (!nq->tag_map) {
1592 kfree(nq->cmds);
Matias Bjorling2d263a782013-12-18 13:41:43 +01001593 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001594 }
1595
1596 for (i = 0; i < nq->queue_depth; i++) {
1597 cmd = &nq->cmds[i];
1598 INIT_LIST_HEAD(&cmd->list);
1599 cmd->ll_list.next = NULL;
1600 cmd->tag = -1U;
1601 }
1602
1603 return 0;
1604}
1605
Jens Axboef2298c02013-10-25 11:52:25 +01001606static int setup_queues(struct nullb *nullb)
1607{
Shaohua Li2984c862017-08-14 15:04:52 -07001608 nullb->queues = kzalloc(nullb->dev->submit_queues *
1609 sizeof(struct nullb_queue), GFP_KERNEL);
Jens Axboef2298c02013-10-25 11:52:25 +01001610 if (!nullb->queues)
Matias Bjorling2d263a782013-12-18 13:41:43 +01001611 return -ENOMEM;
Jens Axboef2298c02013-10-25 11:52:25 +01001612
1613 nullb->nr_queues = 0;
Shaohua Li2984c862017-08-14 15:04:52 -07001614 nullb->queue_depth = nullb->dev->hw_queue_depth;
Jens Axboef2298c02013-10-25 11:52:25 +01001615
Matias Bjorling2d263a782013-12-18 13:41:43 +01001616 return 0;
1617}
1618
1619static int init_driver_queues(struct nullb *nullb)
1620{
1621 struct nullb_queue *nq;
1622 int i, ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001623
Shaohua Li2984c862017-08-14 15:04:52 -07001624 for (i = 0; i < nullb->dev->submit_queues; i++) {
Jens Axboef2298c02013-10-25 11:52:25 +01001625 nq = &nullb->queues[i];
Matias Bjorling2d263a782013-12-18 13:41:43 +01001626
1627 null_init_queue(nullb, nq);
1628
1629 ret = setup_commands(nq);
1630 if (ret)
Jan Kara31f96902014-10-22 15:34:21 +02001631 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01001632 nullb->nr_queues++;
1633 }
Matias Bjorling2d263a782013-12-18 13:41:43 +01001634 return 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001635}
1636
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001637static int null_gendisk_register(struct nullb *nullb)
Jens Axboef2298c02013-10-25 11:52:25 +01001638{
1639 struct gendisk *disk;
Jens Axboef2298c02013-10-25 11:52:25 +01001640 sector_t size;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001641
Shaohua Li2984c862017-08-14 15:04:52 -07001642 disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node);
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001643 if (!disk)
1644 return -ENOMEM;
Shaohua Li2984c862017-08-14 15:04:52 -07001645 size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001646 set_capacity(disk, size >> 9);
1647
1648 disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
1649 disk->major = null_major;
1650 disk->first_minor = nullb->index;
1651 disk->fops = &null_fops;
1652 disk->private_data = nullb;
1653 disk->queue = nullb->q;
1654 strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
1655
1656 add_disk(disk);
1657 return 0;
1658}
1659
Shaohua Li2984c862017-08-14 15:04:52 -07001660static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001661{
1662 set->ops = &null_mq_ops;
Shaohua Li2984c862017-08-14 15:04:52 -07001663 set->nr_hw_queues = nullb ? nullb->dev->submit_queues :
1664 g_submit_queues;
1665 set->queue_depth = nullb ? nullb->dev->hw_queue_depth :
1666 g_hw_queue_depth;
1667 set->numa_node = nullb ? nullb->dev->home_node : g_home_node;
Jens Axboe82f402f2017-06-20 14:22:01 -06001668 set->cmd_size = sizeof(struct nullb_cmd);
1669 set->flags = BLK_MQ_F_SHOULD_MERGE;
1670 set->driver_data = NULL;
1671
Shaohua Li2984c862017-08-14 15:04:52 -07001672 if (nullb->dev->blocking)
Jens Axboe82f402f2017-06-20 14:22:01 -06001673 set->flags |= BLK_MQ_F_BLOCKING;
1674
1675 return blk_mq_alloc_tag_set(set);
1676}
1677
Shaohua Licedcafa2017-08-14 15:04:54 -07001678static void null_validate_conf(struct nullb_device *dev)
1679{
1680 dev->blocksize = round_down(dev->blocksize, 512);
1681 dev->blocksize = clamp_t(unsigned int, dev->blocksize, 512, 4096);
1682 if (dev->use_lightnvm && dev->blocksize != 4096)
1683 dev->blocksize = 4096;
1684
1685 if (dev->use_lightnvm && dev->queue_mode != NULL_Q_MQ)
1686 dev->queue_mode = NULL_Q_MQ;
1687
1688 if (dev->queue_mode == NULL_Q_MQ && dev->use_per_node_hctx) {
1689 if (dev->submit_queues != nr_online_nodes)
1690 dev->submit_queues = nr_online_nodes;
1691 } else if (dev->submit_queues > nr_cpu_ids)
1692 dev->submit_queues = nr_cpu_ids;
1693 else if (dev->submit_queues == 0)
1694 dev->submit_queues = 1;
1695
1696 dev->queue_mode = min_t(unsigned int, dev->queue_mode, NULL_Q_MQ);
1697 dev->irqmode = min_t(unsigned int, dev->irqmode, NULL_IRQ_TIMER);
Shaohua Li5bcd0e0c72017-08-14 15:04:56 -07001698
1699 /* Do memory allocation, so set blocking */
1700 if (dev->memory_backed)
1701 dev->blocking = true;
Shaohua Lideb78b42017-08-14 15:04:59 -07001702 else /* cache is meaningless */
1703 dev->cache_size = 0;
1704 dev->cache_size = min_t(unsigned long, ULONG_MAX / 1024 / 1024,
1705 dev->cache_size);
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001706 dev->mbps = min_t(unsigned int, 1024 * 40, dev->mbps);
1707 /* can not stop a queue */
1708 if (dev->queue_mode == NULL_Q_BIO)
1709 dev->mbps = 0;
Shaohua Licedcafa2017-08-14 15:04:54 -07001710}
1711
Shaohua Li2984c862017-08-14 15:04:52 -07001712static int null_add_dev(struct nullb_device *dev)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001713{
1714 struct nullb *nullb;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001715 int rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001716
Shaohua Licedcafa2017-08-14 15:04:54 -07001717 null_validate_conf(dev);
1718
Shaohua Li2984c862017-08-14 15:04:52 -07001719 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001720 if (!nullb) {
1721 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001722 goto out;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001723 }
Shaohua Li2984c862017-08-14 15:04:52 -07001724 nullb->dev = dev;
1725 dev->nullb = nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001726
1727 spin_lock_init(&nullb->lock);
1728
Robert Elliottdc501dc2014-09-02 11:38:49 -05001729 rv = setup_queues(nullb);
1730 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001731 goto out_free_nullb;
Jens Axboef2298c02013-10-25 11:52:25 +01001732
Shaohua Li2984c862017-08-14 15:04:52 -07001733 if (dev->queue_mode == NULL_Q_MQ) {
Jens Axboe82f402f2017-06-20 14:22:01 -06001734 if (shared_tags) {
1735 nullb->tag_set = &tag_set;
1736 rv = 0;
1737 } else {
1738 nullb->tag_set = &nullb->__tag_set;
Shaohua Li2984c862017-08-14 15:04:52 -07001739 rv = null_init_tag_set(nullb, nullb->tag_set);
Jens Axboe82f402f2017-06-20 14:22:01 -06001740 }
Jens Axboef2298c02013-10-25 11:52:25 +01001741
Robert Elliottdc501dc2014-09-02 11:38:49 -05001742 if (rv)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001743 goto out_cleanup_queues;
Jens Axboef2298c02013-10-25 11:52:25 +01001744
Jens Axboe82f402f2017-06-20 14:22:01 -06001745 nullb->q = blk_mq_init_queue(nullb->tag_set);
Ming Lei35b489d2015-01-02 14:25:27 +00001746 if (IS_ERR(nullb->q)) {
Robert Elliottdc501dc2014-09-02 11:38:49 -05001747 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001748 goto out_cleanup_tags;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001749 }
Jens Axboe82f402f2017-06-20 14:22:01 -06001750 null_init_queues(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001751 } else if (dev->queue_mode == NULL_Q_BIO) {
1752 nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001753 if (!nullb->q) {
1754 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001755 goto out_cleanup_queues;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001756 }
Jens Axboef2298c02013-10-25 11:52:25 +01001757 blk_queue_make_request(nullb->q, null_queue_bio);
Jan Kara31f96902014-10-22 15:34:21 +02001758 rv = init_driver_queues(nullb);
1759 if (rv)
1760 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001761 } else {
Shaohua Li2984c862017-08-14 15:04:52 -07001762 nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock,
1763 dev->home_node);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001764 if (!nullb->q) {
1765 rv = -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001766 goto out_cleanup_queues;
Robert Elliottdc501dc2014-09-02 11:38:49 -05001767 }
Jens Axboef2298c02013-10-25 11:52:25 +01001768 blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001769 blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
Jan Kara31f96902014-10-22 15:34:21 +02001770 rv = init_driver_queues(nullb);
1771 if (rv)
1772 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001773 }
1774
Shaohua Lieff2c4f2017-08-14 15:04:58 -07001775 if (dev->mbps) {
1776 set_bit(NULLB_DEV_FL_THROTTLED, &dev->flags);
1777 nullb_setup_bwtimer(nullb);
1778 }
1779
Shaohua Lideb78b42017-08-14 15:04:59 -07001780 if (dev->cache_size > 0) {
1781 set_bit(NULLB_DEV_FL_CACHE, &nullb->dev->flags);
1782 blk_queue_write_cache(nullb->q, true, true);
1783 blk_queue_flush_queueable(nullb->q, true);
1784 }
1785
Jens Axboef2298c02013-10-25 11:52:25 +01001786 nullb->q->queuedata = nullb;
1787 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
Mike Snitzerb277da02014-10-04 10:55:32 -06001788 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
Jens Axboef2298c02013-10-25 11:52:25 +01001789
Jens Axboef2298c02013-10-25 11:52:25 +01001790 mutex_lock(&lock);
Shaohua Li94bc02e2017-08-14 15:04:55 -07001791 nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
Shaohua Licedcafa2017-08-14 15:04:54 -07001792 dev->index = nullb->index;
Jens Axboef2298c02013-10-25 11:52:25 +01001793 mutex_unlock(&lock);
1794
Shaohua Li2984c862017-08-14 15:04:52 -07001795 blk_queue_logical_block_size(nullb->q, dev->blocksize);
1796 blk_queue_physical_block_size(nullb->q, dev->blocksize);
Jens Axboef2298c02013-10-25 11:52:25 +01001797
Shaohua Li306eb6b2017-08-14 15:04:57 -07001798 null_config_discard(nullb);
1799
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001800 sprintf(nullb->disk_name, "nullb%d", nullb->index);
1801
Shaohua Li2984c862017-08-14 15:04:52 -07001802 if (dev->use_lightnvm)
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001803 rv = null_nvm_register(nullb);
1804 else
1805 rv = null_gendisk_register(nullb);
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001806
Matias Bjørling9ae2d0a2016-09-16 14:25:05 +02001807 if (rv)
1808 goto out_cleanup_blk_queue;
Jens Axboef2298c02013-10-25 11:52:25 +01001809
Matias Bjørlinga5143792016-02-11 14:49:13 +01001810 mutex_lock(&lock);
1811 list_add_tail(&nullb->list, &nullb_list);
1812 mutex_unlock(&lock);
Wenwei Tao3681c85d2016-03-05 00:27:04 +08001813
Jens Axboef2298c02013-10-25 11:52:25 +01001814 return 0;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001815out_cleanup_blk_queue:
1816 blk_cleanup_queue(nullb->q);
1817out_cleanup_tags:
Shaohua Li2984c862017-08-14 15:04:52 -07001818 if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set)
Jens Axboe82f402f2017-06-20 14:22:01 -06001819 blk_mq_free_tag_set(nullb->tag_set);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001820out_cleanup_queues:
1821 cleanup_queues(nullb);
1822out_free_nullb:
1823 kfree(nullb);
1824out:
Shaohua Li2984c862017-08-14 15:04:52 -07001825 null_free_dev(dev);
Robert Elliottdc501dc2014-09-02 11:38:49 -05001826 return rv;
Jens Axboef2298c02013-10-25 11:52:25 +01001827}
1828
1829static int __init null_init(void)
1830{
Minfei Huangaf096e22015-12-08 13:47:34 -07001831 int ret = 0;
Jens Axboef2298c02013-10-25 11:52:25 +01001832 unsigned int i;
Minfei Huangaf096e22015-12-08 13:47:34 -07001833 struct nullb *nullb;
Shaohua Li2984c862017-08-14 15:04:52 -07001834 struct nullb_device *dev;
Jens Axboef2298c02013-10-25 11:52:25 +01001835
Shaohua Lideb78b42017-08-14 15:04:59 -07001836 /* check for nullb_page.bitmap */
1837 if (sizeof(unsigned long) * 8 - 2 < (PAGE_SIZE >> SECTOR_SHIFT))
1838 return -EINVAL;
1839
Shaohua Li2984c862017-08-14 15:04:52 -07001840 if (g_bs > PAGE_SIZE) {
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301841 pr_warn("null_blk: invalid block size\n");
1842 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
Shaohua Li2984c862017-08-14 15:04:52 -07001843 g_bs = PAGE_SIZE;
Raghavendra K T9967d8a2014-01-21 16:59:59 +05301844 }
Jens Axboef2298c02013-10-25 11:52:25 +01001845
Shaohua Li2984c862017-08-14 15:04:52 -07001846 if (g_use_lightnvm && g_bs != 4096) {
Matias Bjørling6bb95352015-11-19 12:50:08 +01001847 pr_warn("null_blk: LightNVM only supports 4k block size\n");
1848 pr_warn("null_blk: defaults block size to 4k\n");
Shaohua Li2984c862017-08-14 15:04:52 -07001849 g_bs = 4096;
Matias Bjørling6bb95352015-11-19 12:50:08 +01001850 }
1851
Shaohua Li2984c862017-08-14 15:04:52 -07001852 if (g_use_lightnvm && g_queue_mode != NULL_Q_MQ) {
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001853 pr_warn("null_blk: LightNVM only supported for blk-mq\n");
1854 pr_warn("null_blk: defaults queue mode to blk-mq\n");
Shaohua Li2984c862017-08-14 15:04:52 -07001855 g_queue_mode = NULL_Q_MQ;
Matias Bjørlingb2b7e002015-11-12 20:25:10 +01001856 }
1857
Shaohua Li2984c862017-08-14 15:04:52 -07001858 if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) {
1859 if (g_submit_queues != nr_online_nodes) {
weiping zhang558ab3002017-08-03 00:26:39 +08001860 pr_warn("null_blk: submit_queues param is set to %u.\n",
Matias Bjorlingd15ee6b2013-12-18 13:41:44 +01001861 nr_online_nodes);
Shaohua Li2984c862017-08-14 15:04:52 -07001862 g_submit_queues = nr_online_nodes;
Matias Bjørlingfc1bc352013-12-21 00:11:01 +01001863 }
Shaohua Li2984c862017-08-14 15:04:52 -07001864 } else if (g_submit_queues > nr_cpu_ids)
1865 g_submit_queues = nr_cpu_ids;
1866 else if (g_submit_queues <= 0)
1867 g_submit_queues = 1;
Jens Axboef2298c02013-10-25 11:52:25 +01001868
Shaohua Li2984c862017-08-14 15:04:52 -07001869 if (g_queue_mode == NULL_Q_MQ && shared_tags) {
1870 ret = null_init_tag_set(NULL, &tag_set);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001871 if (ret)
1872 return ret;
1873 }
1874
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001875 config_group_init(&nullb_subsys.su_group);
1876 mutex_init(&nullb_subsys.su_mutex);
1877
1878 ret = configfs_register_subsystem(&nullb_subsys);
1879 if (ret)
1880 goto err_tagset;
1881
Jens Axboef2298c02013-10-25 11:52:25 +01001882 mutex_init(&lock);
1883
Jens Axboef2298c02013-10-25 11:52:25 +01001884 null_major = register_blkdev(0, "nullb");
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001885 if (null_major < 0) {
1886 ret = null_major;
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001887 goto err_conf;
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001888 }
Jens Axboef2298c02013-10-25 11:52:25 +01001889
Shaohua Li2984c862017-08-14 15:04:52 -07001890 if (g_use_lightnvm) {
Matias Bjørling6bb95352015-11-19 12:50:08 +01001891 ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64),
1892 0, 0, NULL);
1893 if (!ppa_cache) {
1894 pr_err("null_blk: unable to create ppa cache\n");
Minfei Huangaf096e22015-12-08 13:47:34 -07001895 ret = -ENOMEM;
Matias Bjørling6bb95352015-11-19 12:50:08 +01001896 goto err_ppa;
Jens Axboef2298c02013-10-25 11:52:25 +01001897 }
1898 }
1899
Minfei Huangaf096e22015-12-08 13:47:34 -07001900 for (i = 0; i < nr_devices; i++) {
Shaohua Li2984c862017-08-14 15:04:52 -07001901 dev = null_alloc_dev();
1902 if (!dev)
Minfei Huangaf096e22015-12-08 13:47:34 -07001903 goto err_dev;
Shaohua Li2984c862017-08-14 15:04:52 -07001904 ret = null_add_dev(dev);
1905 if (ret) {
1906 null_free_dev(dev);
1907 goto err_dev;
1908 }
Minfei Huangaf096e22015-12-08 13:47:34 -07001909 }
1910
Jens Axboef2298c02013-10-25 11:52:25 +01001911 pr_info("null: module loaded\n");
1912 return 0;
Minfei Huangaf096e22015-12-08 13:47:34 -07001913
1914err_dev:
1915 while (!list_empty(&nullb_list)) {
1916 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07001917 dev = nullb->dev;
Minfei Huangaf096e22015-12-08 13:47:34 -07001918 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001919 null_free_dev(dev);
Minfei Huangaf096e22015-12-08 13:47:34 -07001920 }
Matias Bjørling6bb95352015-11-19 12:50:08 +01001921 kmem_cache_destroy(ppa_cache);
Minfei Huangaf096e22015-12-08 13:47:34 -07001922err_ppa:
1923 unregister_blkdev(null_major, "nullb");
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001924err_conf:
1925 configfs_unregister_subsystem(&nullb_subsys);
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001926err_tagset:
Shaohua Li2984c862017-08-14 15:04:52 -07001927 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Max Gurtovoydb2d1532017-07-06 18:00:07 +03001928 blk_mq_free_tag_set(&tag_set);
Minfei Huangaf096e22015-12-08 13:47:34 -07001929 return ret;
Jens Axboef2298c02013-10-25 11:52:25 +01001930}
1931
1932static void __exit null_exit(void)
1933{
1934 struct nullb *nullb;
1935
Shaohua Li3bf2bd22017-08-14 15:04:53 -07001936 configfs_unregister_subsystem(&nullb_subsys);
1937
Jens Axboef2298c02013-10-25 11:52:25 +01001938 unregister_blkdev(null_major, "nullb");
1939
1940 mutex_lock(&lock);
1941 while (!list_empty(&nullb_list)) {
Shaohua Li2984c862017-08-14 15:04:52 -07001942 struct nullb_device *dev;
1943
Jens Axboef2298c02013-10-25 11:52:25 +01001944 nullb = list_entry(nullb_list.next, struct nullb, list);
Shaohua Li2984c862017-08-14 15:04:52 -07001945 dev = nullb->dev;
Jens Axboef2298c02013-10-25 11:52:25 +01001946 null_del_dev(nullb);
Shaohua Li2984c862017-08-14 15:04:52 -07001947 null_free_dev(dev);
Jens Axboef2298c02013-10-25 11:52:25 +01001948 }
1949 mutex_unlock(&lock);
Matias Bjørling6bb95352015-11-19 12:50:08 +01001950
Shaohua Li2984c862017-08-14 15:04:52 -07001951 if (g_queue_mode == NULL_Q_MQ && shared_tags)
Jens Axboe82f402f2017-06-20 14:22:01 -06001952 blk_mq_free_tag_set(&tag_set);
1953
Matias Bjørling6bb95352015-11-19 12:50:08 +01001954 kmem_cache_destroy(ppa_cache);
Jens Axboef2298c02013-10-25 11:52:25 +01001955}
1956
1957module_init(null_init);
1958module_exit(null_exit);
1959
1960MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
1961MODULE_LICENSE("GPL");