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