Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm-core.h"
9#include "dm-rq.h"
10
11#include <linux/blk-mq.h>
12
13#define DM_MSG_PREFIX "core-rq"
14
15/*
16 * One of these is allocated per request.
17 */
18struct dm_rq_target_io {
19 struct mapped_device *md;
20 struct dm_target *ti;
21 struct request *orig, *clone;
22 struct kthread_work work;
23 blk_status_t error;
24 union map_info info;
25 struct dm_stats_aux stats_aux;
26 unsigned long duration_jiffies;
27 unsigned int n_sectors;
28 unsigned int completed;
29};
30
31#define DM_MQ_NR_HW_QUEUES 1
32#define DM_MQ_QUEUE_DEPTH 2048
33static unsigned int dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
34static unsigned int dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
35
36/*
37 * Request-based DM's mempools' reserved IOs set by the user.
38 */
39#define RESERVED_REQUEST_BASED_IOS 256
40static unsigned int reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
41
42unsigned int dm_get_reserved_rq_based_ios(void)
43{
44 return __dm_get_module_param(&reserved_rq_based_ios,
45 RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS);
46}
47
48static unsigned int dm_get_blk_mq_nr_hw_queues(void)
49{
50 return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
51}
52
53static unsigned int dm_get_blk_mq_queue_depth(void)
54{
55 return __dm_get_module_param(&dm_mq_queue_depth,
56 DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
57}
58
59int dm_request_based(struct mapped_device *md)
60{
61 return queue_is_mq(md->queue);
62}
63
64void dm_start_queue(struct request_queue *q)
65{
66 blk_mq_unquiesce_queue(q);
67 blk_mq_kick_requeue_list(q);
68}
69
70void dm_stop_queue(struct request_queue *q)
71{
72 blk_mq_quiesce_queue(q);
73}
74
75/*
76 * Partial completion handling for request-based dm
77 */
78static void end_clone_bio(struct bio *clone)
79{
80 struct dm_rq_clone_bio_info *info =
81 container_of(clone, struct dm_rq_clone_bio_info, clone);
82 struct dm_rq_target_io *tio = info->tio;
83 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
84 blk_status_t error = clone->bi_status;
85 bool is_last = !clone->bi_next;
86
87 bio_put(clone);
88
89 if (tio->error)
90 /*
91 * An error has already been detected on the request.
92 * Once error occurred, just let clone->end_io() handle
93 * the remainder.
94 */
95 return;
96 else if (error) {
97 /*
98 * Don't notice the error to the upper layer yet.
99 * The error handling decision is made by the target driver,
100 * when the request is completed.
101 */
102 tio->error = error;
103 goto exit;
104 }
105
106 /*
107 * I/O for the bio successfully completed.
108 * Notice the data completion to the upper layer.
109 */
110 tio->completed += nr_bytes;
111
112 /*
113 * Update the original request.
114 * Do not use blk_mq_end_request() here, because it may complete
115 * the original request before the clone, and break the ordering.
116 */
117 if (is_last)
118 exit:
119 blk_update_request(tio->orig, BLK_STS_OK, tio->completed);
120}
121
122static struct dm_rq_target_io *tio_from_request(struct request *rq)
123{
124 return blk_mq_rq_to_pdu(rq);
125}
126
127static void rq_end_stats(struct mapped_device *md, struct request *orig)
128{
129 if (unlikely(dm_stats_used(&md->stats))) {
130 struct dm_rq_target_io *tio = tio_from_request(orig);
131
132 tio->duration_jiffies = jiffies - tio->duration_jiffies;
133 dm_stats_account_io(&md->stats, rq_data_dir(orig),
134 blk_rq_pos(orig), tio->n_sectors, true,
135 tio->duration_jiffies, &tio->stats_aux);
136 }
137}
138
139/*
140 * Don't touch any member of the md after calling this function because
141 * the md may be freed in dm_put() at the end of this function.
142 * Or do dm_get() before calling this function and dm_put() later.
143 */
144static void rq_completed(struct mapped_device *md)
145{
146 /*
147 * dm_put() must be at the end of this function. See the comment above
148 */
149 dm_put(md);
150}
151
152/*
153 * Complete the clone and the original request.
154 * Must be called without clone's queue lock held,
155 * see end_clone_request() for more details.
156 */
157static void dm_end_request(struct request *clone, blk_status_t error)
158{
159 struct dm_rq_target_io *tio = clone->end_io_data;
160 struct mapped_device *md = tio->md;
161 struct request *rq = tio->orig;
162
163 blk_rq_unprep_clone(clone);
164 tio->ti->type->release_clone_rq(clone, NULL);
165
166 rq_end_stats(md, rq);
167 blk_mq_end_request(rq, error);
168 rq_completed(md);
169}
170
171static void __dm_mq_kick_requeue_list(struct request_queue *q, unsigned long msecs)
172{
173 blk_mq_delay_kick_requeue_list(q, msecs);
174}
175
176void dm_mq_kick_requeue_list(struct mapped_device *md)
177{
178 __dm_mq_kick_requeue_list(md->queue, 0);
179}
180EXPORT_SYMBOL(dm_mq_kick_requeue_list);
181
182static void dm_mq_delay_requeue_request(struct request *rq, unsigned long msecs)
183{
184 blk_mq_requeue_request(rq, false);
185 __dm_mq_kick_requeue_list(rq->q, msecs);
186}
187
188static void dm_requeue_original_request(struct dm_rq_target_io *tio, bool delay_requeue)
189{
190 struct mapped_device *md = tio->md;
191 struct request *rq = tio->orig;
192 unsigned long delay_ms = delay_requeue ? 100 : 0;
193
194 rq_end_stats(md, rq);
195 if (tio->clone) {
196 blk_rq_unprep_clone(tio->clone);
197 tio->ti->type->release_clone_rq(tio->clone, NULL);
198 }
199
200 dm_mq_delay_requeue_request(rq, delay_ms);
201 rq_completed(md);
202}
203
204static void dm_done(struct request *clone, blk_status_t error, bool mapped)
205{
206 int r = DM_ENDIO_DONE;
207 struct dm_rq_target_io *tio = clone->end_io_data;
208 dm_request_endio_fn rq_end_io = NULL;
209
210 if (tio->ti) {
211 rq_end_io = tio->ti->type->rq_end_io;
212
213 if (mapped && rq_end_io)
214 r = rq_end_io(tio->ti, clone, error, &tio->info);
215 }
216
217 if (unlikely(error == BLK_STS_TARGET)) {
218 if (req_op(clone) == REQ_OP_DISCARD &&
219 !clone->q->limits.max_discard_sectors)
220 disable_discard(tio->md);
221 else if (req_op(clone) == REQ_OP_WRITE_ZEROES &&
222 !clone->q->limits.max_write_zeroes_sectors)
223 disable_write_zeroes(tio->md);
224 }
225
226 switch (r) {
227 case DM_ENDIO_DONE:
228 /* The target wants to complete the I/O */
229 dm_end_request(clone, error);
230 break;
231 case DM_ENDIO_INCOMPLETE:
232 /* The target will handle the I/O */
233 return;
234 case DM_ENDIO_REQUEUE:
235 /* The target wants to requeue the I/O */
236 dm_requeue_original_request(tio, false);
237 break;
238 case DM_ENDIO_DELAY_REQUEUE:
239 /* The target wants to requeue the I/O after a delay */
240 dm_requeue_original_request(tio, true);
241 break;
242 default:
243 DMCRIT("unimplemented target endio return value: %d", r);
244 BUG();
245 }
246}
247
248/*
249 * Request completion handler for request-based dm
250 */
251static void dm_softirq_done(struct request *rq)
252{
253 bool mapped = true;
254 struct dm_rq_target_io *tio = tio_from_request(rq);
255 struct request *clone = tio->clone;
256
257 if (!clone) {
258 struct mapped_device *md = tio->md;
259
260 rq_end_stats(md, rq);
261 blk_mq_end_request(rq, tio->error);
262 rq_completed(md);
263 return;
264 }
265
266 if (rq->rq_flags & RQF_FAILED)
267 mapped = false;
268
269 dm_done(clone, tio->error, mapped);
270}
271
272/*
273 * Complete the clone and the original request with the error status
274 * through softirq context.
275 */
276static void dm_complete_request(struct request *rq, blk_status_t error)
277{
278 struct dm_rq_target_io *tio = tio_from_request(rq);
279
280 tio->error = error;
281 if (likely(!blk_should_fake_timeout(rq->q)))
282 blk_mq_complete_request(rq);
283}
284
285/*
286 * Complete the not-mapped clone and the original request with the error status
287 * through softirq context.
288 * Target's rq_end_io() function isn't called.
289 * This may be used when the target's clone_and_map_rq() function fails.
290 */
291static void dm_kill_unmapped_request(struct request *rq, blk_status_t error)
292{
293 rq->rq_flags |= RQF_FAILED;
294 dm_complete_request(rq, error);
295}
296
297static enum rq_end_io_ret end_clone_request(struct request *clone,
298 blk_status_t error)
299{
300 struct dm_rq_target_io *tio = clone->end_io_data;
301
302 dm_complete_request(tio->orig, error);
303 return RQ_END_IO_NONE;
304}
305
306static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
307 void *data)
308{
309 struct dm_rq_target_io *tio = data;
310 struct dm_rq_clone_bio_info *info =
311 container_of(bio, struct dm_rq_clone_bio_info, clone);
312
313 info->orig = bio_orig;
314 info->tio = tio;
315 bio->bi_end_io = end_clone_bio;
316
317 return 0;
318}
319
320static int setup_clone(struct request *clone, struct request *rq,
321 struct dm_rq_target_io *tio, gfp_t gfp_mask)
322{
323 int r;
324
325 r = blk_rq_prep_clone(clone, rq, &tio->md->mempools->bs, gfp_mask,
326 dm_rq_bio_constructor, tio);
327 if (r)
328 return r;
329
330 clone->end_io = end_clone_request;
331 clone->end_io_data = tio;
332
333 tio->clone = clone;
334
335 return 0;
336}
337
338static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
339 struct mapped_device *md)
340{
341 tio->md = md;
342 tio->ti = NULL;
343 tio->clone = NULL;
344 tio->orig = rq;
345 tio->error = 0;
346 tio->completed = 0;
347 /*
348 * Avoid initializing info for blk-mq; it passes
349 * target-specific data through info.ptr
350 * (see: dm_mq_init_request)
351 */
352 if (!md->init_tio_pdu)
353 memset(&tio->info, 0, sizeof(tio->info));
354}
355
356/*
357 * Returns:
358 * DM_MAPIO_* : the request has been processed as indicated
359 * DM_MAPIO_REQUEUE : the original request needs to be immediately requeued
360 * < 0 : the request was completed due to failure
361 */
362static int map_request(struct dm_rq_target_io *tio)
363{
364 int r;
365 struct dm_target *ti = tio->ti;
366 struct mapped_device *md = tio->md;
367 struct request *rq = tio->orig;
368 struct request *clone = NULL;
369 blk_status_t ret;
370
371 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
372 switch (r) {
373 case DM_MAPIO_SUBMITTED:
374 /* The target has taken the I/O to submit by itself later */
375 break;
376 case DM_MAPIO_REMAPPED:
377 if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
378 /* -ENOMEM */
379 ti->type->release_clone_rq(clone, &tio->info);
380 return DM_MAPIO_REQUEUE;
381 }
382
383 /* The target has remapped the I/O so dispatch it */
384 trace_block_rq_remap(clone, disk_devt(dm_disk(md)),
385 blk_rq_pos(rq));
386 ret = blk_insert_cloned_request(clone);
387 switch (ret) {
388 case BLK_STS_OK:
389 break;
390 case BLK_STS_RESOURCE:
391 case BLK_STS_DEV_RESOURCE:
392 blk_rq_unprep_clone(clone);
393 blk_mq_cleanup_rq(clone);
394 tio->ti->type->release_clone_rq(clone, &tio->info);
395 tio->clone = NULL;
396 return DM_MAPIO_REQUEUE;
397 default:
398 /* must complete clone in terms of original request */
399 dm_complete_request(rq, ret);
400 }
401 break;
402 case DM_MAPIO_REQUEUE:
403 /* The target wants to requeue the I/O */
404 break;
405 case DM_MAPIO_DELAY_REQUEUE:
406 /* The target wants to requeue the I/O after a delay */
407 dm_requeue_original_request(tio, true);
408 break;
409 case DM_MAPIO_KILL:
410 /* The target wants to complete the I/O */
411 dm_kill_unmapped_request(rq, BLK_STS_IOERR);
412 break;
413 default:
414 DMCRIT("unimplemented target map return value: %d", r);
415 BUG();
416 }
417
418 return r;
419}
420
421/* DEPRECATED: previously used for request-based merge heuristic in dm_request_fn() */
422ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
423{
424 return sprintf(buf, "%u\n", 0);
425}
426
427ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
428 const char *buf, size_t count)
429{
430 return count;
431}
432
433static void dm_start_request(struct mapped_device *md, struct request *orig)
434{
435 blk_mq_start_request(orig);
436
437 if (unlikely(dm_stats_used(&md->stats))) {
438 struct dm_rq_target_io *tio = tio_from_request(orig);
439
440 tio->duration_jiffies = jiffies;
441 tio->n_sectors = blk_rq_sectors(orig);
442 dm_stats_account_io(&md->stats, rq_data_dir(orig),
443 blk_rq_pos(orig), tio->n_sectors, false, 0,
444 &tio->stats_aux);
445 }
446
447 /*
448 * Hold the md reference here for the in-flight I/O.
449 * We can't rely on the reference count by device opener,
450 * because the device may be closed during the request completion
451 * when all bios are completed.
452 * See the comment in rq_completed() too.
453 */
454 dm_get(md);
455}
456
457static int dm_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
458 unsigned int hctx_idx, unsigned int numa_node)
459{
460 struct mapped_device *md = set->driver_data;
461 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
462
463 /*
464 * Must initialize md member of tio, otherwise it won't
465 * be available in dm_mq_queue_rq.
466 */
467 tio->md = md;
468
469 if (md->init_tio_pdu) {
470 /* target-specific per-io data is immediately after the tio */
471 tio->info.ptr = tio + 1;
472 }
473
474 return 0;
475}
476
477static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
478 const struct blk_mq_queue_data *bd)
479{
480 struct request *rq = bd->rq;
481 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
482 struct mapped_device *md = tio->md;
483 struct dm_target *ti = md->immutable_target;
484
485 /*
486 * blk-mq's unquiesce may come from outside events, such as
487 * elevator switch, updating nr_requests or others, and request may
488 * come during suspend, so simply ask for blk-mq to requeue it.
489 */
490 if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)))
491 return BLK_STS_RESOURCE;
492
493 if (unlikely(!ti)) {
494 int srcu_idx;
495 struct dm_table *map;
496
497 map = dm_get_live_table(md, &srcu_idx);
498 if (unlikely(!map)) {
499 dm_put_live_table(md, srcu_idx);
500 return BLK_STS_RESOURCE;
501 }
502 ti = dm_table_find_target(map, 0);
503 dm_put_live_table(md, srcu_idx);
504 }
505
506 if (ti->type->busy && ti->type->busy(ti))
507 return BLK_STS_RESOURCE;
508
509 dm_start_request(md, rq);
510
511 /* Init tio using md established in .init_request */
512 init_tio(tio, rq, md);
513
514 /*
515 * Establish tio->ti before calling map_request().
516 */
517 tio->ti = ti;
518
519 /* Direct call is fine since .queue_rq allows allocations */
520 if (map_request(tio) == DM_MAPIO_REQUEUE) {
521 /* Undo dm_start_request() before requeuing */
522 rq_end_stats(md, rq);
523 rq_completed(md);
524 return BLK_STS_RESOURCE;
525 }
526
527 return BLK_STS_OK;
528}
529
530static const struct blk_mq_ops dm_mq_ops = {
531 .queue_rq = dm_mq_queue_rq,
532 .complete = dm_softirq_done,
533 .init_request = dm_mq_init_request,
534};
535
536int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
537{
538 struct dm_target *immutable_tgt;
539 int err;
540
541 md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
542 if (!md->tag_set)
543 return -ENOMEM;
544
545 md->tag_set->ops = &dm_mq_ops;
546 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
547 md->tag_set->numa_node = md->numa_node_id;
548 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_STACKING;
549 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
550 md->tag_set->driver_data = md;
551
552 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
553 immutable_tgt = dm_table_get_immutable_target(t);
554 if (immutable_tgt && immutable_tgt->per_io_data_size) {
555 /* any target-specific per-io data is immediately after the tio */
556 md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
557 md->init_tio_pdu = true;
558 }
559
560 err = blk_mq_alloc_tag_set(md->tag_set);
561 if (err)
562 goto out_kfree_tag_set;
563
564 err = blk_mq_init_allocated_queue(md->tag_set, md->queue);
565 if (err)
566 goto out_tag_set;
567 return 0;
568
569out_tag_set:
570 blk_mq_free_tag_set(md->tag_set);
571out_kfree_tag_set:
572 kfree(md->tag_set);
573 md->tag_set = NULL;
574
575 return err;
576}
577
578void dm_mq_cleanup_mapped_device(struct mapped_device *md)
579{
580 if (md->tag_set) {
581 blk_mq_free_tag_set(md->tag_set);
582 kfree(md->tag_set);
583 md->tag_set = NULL;
584 }
585}
586
587module_param(reserved_rq_based_ios, uint, 0644);
588MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
589
590/* Unused, but preserved for userspace compatibility */
591static bool use_blk_mq = true;
592module_param(use_blk_mq, bool, 0644);
593MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
594
595module_param(dm_mq_nr_hw_queues, uint, 0644);
596MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
597
598module_param(dm_mq_queue_depth, uint, 0644);
599MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");
1/*
2 * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-core.h"
8#include "dm-rq.h"
9
10#include <linux/elevator.h> /* for rq_end_sector() */
11#include <linux/blk-mq.h>
12
13#define DM_MSG_PREFIX "core-rq"
14
15#define DM_MQ_NR_HW_QUEUES 1
16#define DM_MQ_QUEUE_DEPTH 2048
17static unsigned dm_mq_nr_hw_queues = DM_MQ_NR_HW_QUEUES;
18static unsigned dm_mq_queue_depth = DM_MQ_QUEUE_DEPTH;
19
20/*
21 * Request-based DM's mempools' reserved IOs set by the user.
22 */
23#define RESERVED_REQUEST_BASED_IOS 256
24static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
25
26static bool use_blk_mq = IS_ENABLED(CONFIG_DM_MQ_DEFAULT);
27
28bool dm_use_blk_mq_default(void)
29{
30 return use_blk_mq;
31}
32
33bool dm_use_blk_mq(struct mapped_device *md)
34{
35 return md->use_blk_mq;
36}
37EXPORT_SYMBOL_GPL(dm_use_blk_mq);
38
39unsigned dm_get_reserved_rq_based_ios(void)
40{
41 return __dm_get_module_param(&reserved_rq_based_ios,
42 RESERVED_REQUEST_BASED_IOS, DM_RESERVED_MAX_IOS);
43}
44EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
45
46static unsigned dm_get_blk_mq_nr_hw_queues(void)
47{
48 return __dm_get_module_param(&dm_mq_nr_hw_queues, 1, 32);
49}
50
51static unsigned dm_get_blk_mq_queue_depth(void)
52{
53 return __dm_get_module_param(&dm_mq_queue_depth,
54 DM_MQ_QUEUE_DEPTH, BLK_MQ_MAX_DEPTH);
55}
56
57int dm_request_based(struct mapped_device *md)
58{
59 return blk_queue_stackable(md->queue);
60}
61
62static void dm_old_start_queue(struct request_queue *q)
63{
64 unsigned long flags;
65
66 spin_lock_irqsave(q->queue_lock, flags);
67 if (blk_queue_stopped(q))
68 blk_start_queue(q);
69 spin_unlock_irqrestore(q->queue_lock, flags);
70}
71
72static void dm_mq_start_queue(struct request_queue *q)
73{
74 blk_mq_start_stopped_hw_queues(q, true);
75 blk_mq_kick_requeue_list(q);
76}
77
78void dm_start_queue(struct request_queue *q)
79{
80 if (!q->mq_ops)
81 dm_old_start_queue(q);
82 else
83 dm_mq_start_queue(q);
84}
85
86static void dm_old_stop_queue(struct request_queue *q)
87{
88 unsigned long flags;
89
90 spin_lock_irqsave(q->queue_lock, flags);
91 if (!blk_queue_stopped(q))
92 blk_stop_queue(q);
93 spin_unlock_irqrestore(q->queue_lock, flags);
94}
95
96static void dm_mq_stop_queue(struct request_queue *q)
97{
98 if (blk_mq_queue_stopped(q))
99 return;
100
101 blk_mq_quiesce_queue(q);
102}
103
104void dm_stop_queue(struct request_queue *q)
105{
106 if (!q->mq_ops)
107 dm_old_stop_queue(q);
108 else
109 dm_mq_stop_queue(q);
110}
111
112static struct dm_rq_target_io *alloc_old_rq_tio(struct mapped_device *md,
113 gfp_t gfp_mask)
114{
115 return mempool_alloc(md->io_pool, gfp_mask);
116}
117
118static void free_old_rq_tio(struct dm_rq_target_io *tio)
119{
120 mempool_free(tio, tio->md->io_pool);
121}
122
123static struct request *alloc_old_clone_request(struct mapped_device *md,
124 gfp_t gfp_mask)
125{
126 return mempool_alloc(md->rq_pool, gfp_mask);
127}
128
129static void free_old_clone_request(struct mapped_device *md, struct request *rq)
130{
131 mempool_free(rq, md->rq_pool);
132}
133
134/*
135 * Partial completion handling for request-based dm
136 */
137static void end_clone_bio(struct bio *clone)
138{
139 struct dm_rq_clone_bio_info *info =
140 container_of(clone, struct dm_rq_clone_bio_info, clone);
141 struct dm_rq_target_io *tio = info->tio;
142 struct bio *bio = info->orig;
143 unsigned int nr_bytes = info->orig->bi_iter.bi_size;
144 int error = clone->bi_error;
145
146 bio_put(clone);
147
148 if (tio->error)
149 /*
150 * An error has already been detected on the request.
151 * Once error occurred, just let clone->end_io() handle
152 * the remainder.
153 */
154 return;
155 else if (error) {
156 /*
157 * Don't notice the error to the upper layer yet.
158 * The error handling decision is made by the target driver,
159 * when the request is completed.
160 */
161 tio->error = error;
162 return;
163 }
164
165 /*
166 * I/O for the bio successfully completed.
167 * Notice the data completion to the upper layer.
168 */
169
170 /*
171 * bios are processed from the head of the list.
172 * So the completing bio should always be rq->bio.
173 * If it's not, something wrong is happening.
174 */
175 if (tio->orig->bio != bio)
176 DMERR("bio completion is going in the middle of the request");
177
178 /*
179 * Update the original request.
180 * Do not use blk_end_request() here, because it may complete
181 * the original request before the clone, and break the ordering.
182 */
183 blk_update_request(tio->orig, 0, nr_bytes);
184}
185
186static struct dm_rq_target_io *tio_from_request(struct request *rq)
187{
188 return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
189}
190
191static void rq_end_stats(struct mapped_device *md, struct request *orig)
192{
193 if (unlikely(dm_stats_used(&md->stats))) {
194 struct dm_rq_target_io *tio = tio_from_request(orig);
195 tio->duration_jiffies = jiffies - tio->duration_jiffies;
196 dm_stats_account_io(&md->stats, rq_data_dir(orig),
197 blk_rq_pos(orig), tio->n_sectors, true,
198 tio->duration_jiffies, &tio->stats_aux);
199 }
200}
201
202/*
203 * Don't touch any member of the md after calling this function because
204 * the md may be freed in dm_put() at the end of this function.
205 * Or do dm_get() before calling this function and dm_put() later.
206 */
207static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
208{
209 struct request_queue *q = md->queue;
210 unsigned long flags;
211
212 atomic_dec(&md->pending[rw]);
213
214 /* nudge anyone waiting on suspend queue */
215 if (!md_in_flight(md))
216 wake_up(&md->wait);
217
218 /*
219 * Run this off this callpath, as drivers could invoke end_io while
220 * inside their request_fn (and holding the queue lock). Calling
221 * back into ->request_fn() could deadlock attempting to grab the
222 * queue lock again.
223 */
224 if (!q->mq_ops && run_queue) {
225 spin_lock_irqsave(q->queue_lock, flags);
226 blk_run_queue_async(q);
227 spin_unlock_irqrestore(q->queue_lock, flags);
228 }
229
230 /*
231 * dm_put() must be at the end of this function. See the comment above
232 */
233 dm_put(md);
234}
235
236static void free_rq_clone(struct request *clone)
237{
238 struct dm_rq_target_io *tio = clone->end_io_data;
239 struct mapped_device *md = tio->md;
240
241 blk_rq_unprep_clone(clone);
242
243 /*
244 * It is possible for a clone_old_rq() allocated clone to
245 * get passed in -- it may not yet have a request_queue.
246 * This is known to occur if the error target replaces
247 * a multipath target that has a request_fn queue stacked
248 * on blk-mq queue(s).
249 */
250 if (clone->q && clone->q->mq_ops)
251 /* stacked on blk-mq queue(s) */
252 tio->ti->type->release_clone_rq(clone);
253 else if (!md->queue->mq_ops)
254 /* request_fn queue stacked on request_fn queue(s) */
255 free_old_clone_request(md, clone);
256
257 if (!md->queue->mq_ops)
258 free_old_rq_tio(tio);
259}
260
261/*
262 * Complete the clone and the original request.
263 * Must be called without clone's queue lock held,
264 * see end_clone_request() for more details.
265 */
266static void dm_end_request(struct request *clone, int error)
267{
268 int rw = rq_data_dir(clone);
269 struct dm_rq_target_io *tio = clone->end_io_data;
270 struct mapped_device *md = tio->md;
271 struct request *rq = tio->orig;
272
273 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
274 rq->errors = clone->errors;
275 rq->resid_len = clone->resid_len;
276
277 if (rq->sense)
278 /*
279 * We are using the sense buffer of the original
280 * request.
281 * So setting the length of the sense data is enough.
282 */
283 rq->sense_len = clone->sense_len;
284 }
285
286 free_rq_clone(clone);
287 rq_end_stats(md, rq);
288 if (!rq->q->mq_ops)
289 blk_end_request_all(rq, error);
290 else
291 blk_mq_end_request(rq, error);
292 rq_completed(md, rw, true);
293}
294
295static void dm_unprep_request(struct request *rq)
296{
297 struct dm_rq_target_io *tio = tio_from_request(rq);
298 struct request *clone = tio->clone;
299
300 if (!rq->q->mq_ops) {
301 rq->special = NULL;
302 rq->rq_flags &= ~RQF_DONTPREP;
303 }
304
305 if (clone)
306 free_rq_clone(clone);
307 else if (!tio->md->queue->mq_ops)
308 free_old_rq_tio(tio);
309}
310
311/*
312 * Requeue the original request of a clone.
313 */
314static void dm_old_requeue_request(struct request *rq)
315{
316 struct request_queue *q = rq->q;
317 unsigned long flags;
318
319 spin_lock_irqsave(q->queue_lock, flags);
320 blk_requeue_request(q, rq);
321 blk_run_queue_async(q);
322 spin_unlock_irqrestore(q->queue_lock, flags);
323}
324
325static void __dm_mq_kick_requeue_list(struct request_queue *q, unsigned long msecs)
326{
327 blk_mq_delay_kick_requeue_list(q, msecs);
328}
329
330void dm_mq_kick_requeue_list(struct mapped_device *md)
331{
332 __dm_mq_kick_requeue_list(dm_get_md_queue(md), 0);
333}
334EXPORT_SYMBOL(dm_mq_kick_requeue_list);
335
336static void dm_mq_delay_requeue_request(struct request *rq, unsigned long msecs)
337{
338 blk_mq_requeue_request(rq, false);
339 __dm_mq_kick_requeue_list(rq->q, msecs);
340}
341
342static void dm_requeue_original_request(struct dm_rq_target_io *tio, bool delay_requeue)
343{
344 struct mapped_device *md = tio->md;
345 struct request *rq = tio->orig;
346 int rw = rq_data_dir(rq);
347
348 rq_end_stats(md, rq);
349 dm_unprep_request(rq);
350
351 if (!rq->q->mq_ops)
352 dm_old_requeue_request(rq);
353 else
354 dm_mq_delay_requeue_request(rq, delay_requeue ? 5000 : 0);
355
356 rq_completed(md, rw, false);
357}
358
359static void dm_done(struct request *clone, int error, bool mapped)
360{
361 int r = error;
362 struct dm_rq_target_io *tio = clone->end_io_data;
363 dm_request_endio_fn rq_end_io = NULL;
364
365 if (tio->ti) {
366 rq_end_io = tio->ti->type->rq_end_io;
367
368 if (mapped && rq_end_io)
369 r = rq_end_io(tio->ti, clone, error, &tio->info);
370 }
371
372 if (unlikely(r == -EREMOTEIO && (req_op(clone) == REQ_OP_WRITE_SAME) &&
373 !clone->q->limits.max_write_same_sectors))
374 disable_write_same(tio->md);
375
376 if (r <= 0)
377 /* The target wants to complete the I/O */
378 dm_end_request(clone, r);
379 else if (r == DM_ENDIO_INCOMPLETE)
380 /* The target will handle the I/O */
381 return;
382 else if (r == DM_ENDIO_REQUEUE)
383 /* The target wants to requeue the I/O */
384 dm_requeue_original_request(tio, false);
385 else {
386 DMWARN("unimplemented target endio return value: %d", r);
387 BUG();
388 }
389}
390
391/*
392 * Request completion handler for request-based dm
393 */
394static void dm_softirq_done(struct request *rq)
395{
396 bool mapped = true;
397 struct dm_rq_target_io *tio = tio_from_request(rq);
398 struct request *clone = tio->clone;
399 int rw;
400
401 if (!clone) {
402 rq_end_stats(tio->md, rq);
403 rw = rq_data_dir(rq);
404 if (!rq->q->mq_ops) {
405 blk_end_request_all(rq, tio->error);
406 rq_completed(tio->md, rw, false);
407 free_old_rq_tio(tio);
408 } else {
409 blk_mq_end_request(rq, tio->error);
410 rq_completed(tio->md, rw, false);
411 }
412 return;
413 }
414
415 if (rq->rq_flags & RQF_FAILED)
416 mapped = false;
417
418 dm_done(clone, tio->error, mapped);
419}
420
421/*
422 * Complete the clone and the original request with the error status
423 * through softirq context.
424 */
425static void dm_complete_request(struct request *rq, int error)
426{
427 struct dm_rq_target_io *tio = tio_from_request(rq);
428
429 tio->error = error;
430 if (!rq->q->mq_ops)
431 blk_complete_request(rq);
432 else
433 blk_mq_complete_request(rq, error);
434}
435
436/*
437 * Complete the not-mapped clone and the original request with the error status
438 * through softirq context.
439 * Target's rq_end_io() function isn't called.
440 * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
441 */
442static void dm_kill_unmapped_request(struct request *rq, int error)
443{
444 rq->rq_flags |= RQF_FAILED;
445 dm_complete_request(rq, error);
446}
447
448/*
449 * Called with the clone's queue lock held (in the case of .request_fn)
450 */
451static void end_clone_request(struct request *clone, int error)
452{
453 struct dm_rq_target_io *tio = clone->end_io_data;
454
455 if (!clone->q->mq_ops) {
456 /*
457 * For just cleaning up the information of the queue in which
458 * the clone was dispatched.
459 * The clone is *NOT* freed actually here because it is alloced
460 * from dm own mempool (RQF_ALLOCED isn't set).
461 */
462 __blk_put_request(clone->q, clone);
463 }
464
465 /*
466 * Actual request completion is done in a softirq context which doesn't
467 * hold the clone's queue lock. Otherwise, deadlock could occur because:
468 * - another request may be submitted by the upper level driver
469 * of the stacking during the completion
470 * - the submission which requires queue lock may be done
471 * against this clone's queue
472 */
473 dm_complete_request(tio->orig, error);
474}
475
476static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
477{
478 int r;
479
480 if (blk_queue_io_stat(clone->q))
481 clone->rq_flags |= RQF_IO_STAT;
482
483 clone->start_time = jiffies;
484 r = blk_insert_cloned_request(clone->q, clone);
485 if (r)
486 /* must complete clone in terms of original request */
487 dm_complete_request(rq, r);
488}
489
490static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
491 void *data)
492{
493 struct dm_rq_target_io *tio = data;
494 struct dm_rq_clone_bio_info *info =
495 container_of(bio, struct dm_rq_clone_bio_info, clone);
496
497 info->orig = bio_orig;
498 info->tio = tio;
499 bio->bi_end_io = end_clone_bio;
500
501 return 0;
502}
503
504static int setup_clone(struct request *clone, struct request *rq,
505 struct dm_rq_target_io *tio, gfp_t gfp_mask)
506{
507 int r;
508
509 r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
510 dm_rq_bio_constructor, tio);
511 if (r)
512 return r;
513
514 clone->cmd = rq->cmd;
515 clone->cmd_len = rq->cmd_len;
516 clone->sense = rq->sense;
517 clone->end_io = end_clone_request;
518 clone->end_io_data = tio;
519
520 tio->clone = clone;
521
522 return 0;
523}
524
525static struct request *clone_old_rq(struct request *rq, struct mapped_device *md,
526 struct dm_rq_target_io *tio, gfp_t gfp_mask)
527{
528 /*
529 * Create clone for use with .request_fn request_queue
530 */
531 struct request *clone;
532
533 clone = alloc_old_clone_request(md, gfp_mask);
534 if (!clone)
535 return NULL;
536
537 blk_rq_init(NULL, clone);
538 if (setup_clone(clone, rq, tio, gfp_mask)) {
539 /* -ENOMEM */
540 free_old_clone_request(md, clone);
541 return NULL;
542 }
543
544 return clone;
545}
546
547static void map_tio_request(struct kthread_work *work);
548
549static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
550 struct mapped_device *md)
551{
552 tio->md = md;
553 tio->ti = NULL;
554 tio->clone = NULL;
555 tio->orig = rq;
556 tio->error = 0;
557 /*
558 * Avoid initializing info for blk-mq; it passes
559 * target-specific data through info.ptr
560 * (see: dm_mq_init_request)
561 */
562 if (!md->init_tio_pdu)
563 memset(&tio->info, 0, sizeof(tio->info));
564 if (md->kworker_task)
565 kthread_init_work(&tio->work, map_tio_request);
566}
567
568static struct dm_rq_target_io *dm_old_prep_tio(struct request *rq,
569 struct mapped_device *md,
570 gfp_t gfp_mask)
571{
572 struct dm_rq_target_io *tio;
573 int srcu_idx;
574 struct dm_table *table;
575
576 tio = alloc_old_rq_tio(md, gfp_mask);
577 if (!tio)
578 return NULL;
579
580 init_tio(tio, rq, md);
581
582 table = dm_get_live_table(md, &srcu_idx);
583 /*
584 * Must clone a request if this .request_fn DM device
585 * is stacked on .request_fn device(s).
586 */
587 if (!dm_table_all_blk_mq_devices(table)) {
588 if (!clone_old_rq(rq, md, tio, gfp_mask)) {
589 dm_put_live_table(md, srcu_idx);
590 free_old_rq_tio(tio);
591 return NULL;
592 }
593 }
594 dm_put_live_table(md, srcu_idx);
595
596 return tio;
597}
598
599/*
600 * Called with the queue lock held.
601 */
602static int dm_old_prep_fn(struct request_queue *q, struct request *rq)
603{
604 struct mapped_device *md = q->queuedata;
605 struct dm_rq_target_io *tio;
606
607 if (unlikely(rq->special)) {
608 DMWARN("Already has something in rq->special.");
609 return BLKPREP_KILL;
610 }
611
612 tio = dm_old_prep_tio(rq, md, GFP_ATOMIC);
613 if (!tio)
614 return BLKPREP_DEFER;
615
616 rq->special = tio;
617 rq->rq_flags |= RQF_DONTPREP;
618
619 return BLKPREP_OK;
620}
621
622/*
623 * Returns:
624 * DM_MAPIO_* : the request has been processed as indicated
625 * DM_MAPIO_REQUEUE : the original request needs to be immediately requeued
626 * < 0 : the request was completed due to failure
627 */
628static int map_request(struct dm_rq_target_io *tio)
629{
630 int r;
631 struct dm_target *ti = tio->ti;
632 struct mapped_device *md = tio->md;
633 struct request *rq = tio->orig;
634 struct request *clone = NULL;
635
636 if (tio->clone) {
637 clone = tio->clone;
638 r = ti->type->map_rq(ti, clone, &tio->info);
639 if (r == DM_MAPIO_DELAY_REQUEUE)
640 return DM_MAPIO_REQUEUE; /* .request_fn requeue is always immediate */
641 } else {
642 r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
643 if (r < 0) {
644 /* The target wants to complete the I/O */
645 dm_kill_unmapped_request(rq, r);
646 return r;
647 }
648 if (r == DM_MAPIO_REMAPPED &&
649 setup_clone(clone, rq, tio, GFP_ATOMIC)) {
650 /* -ENOMEM */
651 ti->type->release_clone_rq(clone);
652 return DM_MAPIO_REQUEUE;
653 }
654 }
655
656 switch (r) {
657 case DM_MAPIO_SUBMITTED:
658 /* The target has taken the I/O to submit by itself later */
659 break;
660 case DM_MAPIO_REMAPPED:
661 /* The target has remapped the I/O so dispatch it */
662 trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
663 blk_rq_pos(rq));
664 dm_dispatch_clone_request(clone, rq);
665 break;
666 case DM_MAPIO_REQUEUE:
667 /* The target wants to requeue the I/O */
668 break;
669 case DM_MAPIO_DELAY_REQUEUE:
670 /* The target wants to requeue the I/O after a delay */
671 dm_requeue_original_request(tio, true);
672 break;
673 default:
674 if (r > 0) {
675 DMWARN("unimplemented target map return value: %d", r);
676 BUG();
677 }
678
679 /* The target wants to complete the I/O */
680 dm_kill_unmapped_request(rq, r);
681 }
682
683 return r;
684}
685
686static void dm_start_request(struct mapped_device *md, struct request *orig)
687{
688 if (!orig->q->mq_ops)
689 blk_start_request(orig);
690 else
691 blk_mq_start_request(orig);
692 atomic_inc(&md->pending[rq_data_dir(orig)]);
693
694 if (md->seq_rq_merge_deadline_usecs) {
695 md->last_rq_pos = rq_end_sector(orig);
696 md->last_rq_rw = rq_data_dir(orig);
697 md->last_rq_start_time = ktime_get();
698 }
699
700 if (unlikely(dm_stats_used(&md->stats))) {
701 struct dm_rq_target_io *tio = tio_from_request(orig);
702 tio->duration_jiffies = jiffies;
703 tio->n_sectors = blk_rq_sectors(orig);
704 dm_stats_account_io(&md->stats, rq_data_dir(orig),
705 blk_rq_pos(orig), tio->n_sectors, false, 0,
706 &tio->stats_aux);
707 }
708
709 /*
710 * Hold the md reference here for the in-flight I/O.
711 * We can't rely on the reference count by device opener,
712 * because the device may be closed during the request completion
713 * when all bios are completed.
714 * See the comment in rq_completed() too.
715 */
716 dm_get(md);
717}
718
719static void map_tio_request(struct kthread_work *work)
720{
721 struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
722
723 if (map_request(tio) == DM_MAPIO_REQUEUE)
724 dm_requeue_original_request(tio, false);
725}
726
727ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
728{
729 return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
730}
731
732#define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
733
734ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
735 const char *buf, size_t count)
736{
737 unsigned deadline;
738
739 if (dm_get_md_type(md) != DM_TYPE_REQUEST_BASED)
740 return count;
741
742 if (kstrtouint(buf, 10, &deadline))
743 return -EINVAL;
744
745 if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
746 deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
747
748 md->seq_rq_merge_deadline_usecs = deadline;
749
750 return count;
751}
752
753static bool dm_old_request_peeked_before_merge_deadline(struct mapped_device *md)
754{
755 ktime_t kt_deadline;
756
757 if (!md->seq_rq_merge_deadline_usecs)
758 return false;
759
760 kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
761 kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
762
763 return !ktime_after(ktime_get(), kt_deadline);
764}
765
766/*
767 * q->request_fn for old request-based dm.
768 * Called with the queue lock held.
769 */
770static void dm_old_request_fn(struct request_queue *q)
771{
772 struct mapped_device *md = q->queuedata;
773 struct dm_target *ti = md->immutable_target;
774 struct request *rq;
775 struct dm_rq_target_io *tio;
776 sector_t pos = 0;
777
778 if (unlikely(!ti)) {
779 int srcu_idx;
780 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
781
782 if (unlikely(!map)) {
783 dm_put_live_table(md, srcu_idx);
784 return;
785 }
786 ti = dm_table_find_target(map, pos);
787 dm_put_live_table(md, srcu_idx);
788 }
789
790 /*
791 * For suspend, check blk_queue_stopped() and increment
792 * ->pending within a single queue_lock not to increment the
793 * number of in-flight I/Os after the queue is stopped in
794 * dm_suspend().
795 */
796 while (!blk_queue_stopped(q)) {
797 rq = blk_peek_request(q);
798 if (!rq)
799 return;
800
801 /* always use block 0 to find the target for flushes for now */
802 pos = 0;
803 if (req_op(rq) != REQ_OP_FLUSH)
804 pos = blk_rq_pos(rq);
805
806 if ((dm_old_request_peeked_before_merge_deadline(md) &&
807 md_in_flight(md) && rq->bio && !bio_multiple_segments(rq->bio) &&
808 md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq)) ||
809 (ti->type->busy && ti->type->busy(ti))) {
810 blk_delay_queue(q, 10);
811 return;
812 }
813
814 dm_start_request(md, rq);
815
816 tio = tio_from_request(rq);
817 /* Establish tio->ti before queuing work (map_tio_request) */
818 tio->ti = ti;
819 kthread_queue_work(&md->kworker, &tio->work);
820 BUG_ON(!irqs_disabled());
821 }
822}
823
824/*
825 * Fully initialize a .request_fn request-based queue.
826 */
827int dm_old_init_request_queue(struct mapped_device *md)
828{
829 /* Fully initialize the queue */
830 if (!blk_init_allocated_queue(md->queue, dm_old_request_fn, NULL))
831 return -EINVAL;
832
833 /* disable dm_old_request_fn's merge heuristic by default */
834 md->seq_rq_merge_deadline_usecs = 0;
835
836 dm_init_normal_md_queue(md);
837 blk_queue_softirq_done(md->queue, dm_softirq_done);
838 blk_queue_prep_rq(md->queue, dm_old_prep_fn);
839
840 /* Initialize the request-based DM worker thread */
841 kthread_init_worker(&md->kworker);
842 md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
843 "kdmwork-%s", dm_device_name(md));
844 if (IS_ERR(md->kworker_task)) {
845 int error = PTR_ERR(md->kworker_task);
846 md->kworker_task = NULL;
847 return error;
848 }
849
850 elv_register_queue(md->queue);
851
852 return 0;
853}
854
855static int dm_mq_init_request(void *data, struct request *rq,
856 unsigned int hctx_idx, unsigned int request_idx,
857 unsigned int numa_node)
858{
859 struct mapped_device *md = data;
860 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
861
862 /*
863 * Must initialize md member of tio, otherwise it won't
864 * be available in dm_mq_queue_rq.
865 */
866 tio->md = md;
867
868 if (md->init_tio_pdu) {
869 /* target-specific per-io data is immediately after the tio */
870 tio->info.ptr = tio + 1;
871 }
872
873 return 0;
874}
875
876static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
877 const struct blk_mq_queue_data *bd)
878{
879 struct request *rq = bd->rq;
880 struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
881 struct mapped_device *md = tio->md;
882 struct dm_target *ti = md->immutable_target;
883
884 if (unlikely(!ti)) {
885 int srcu_idx;
886 struct dm_table *map = dm_get_live_table(md, &srcu_idx);
887
888 ti = dm_table_find_target(map, 0);
889 dm_put_live_table(md, srcu_idx);
890 }
891
892 if (ti->type->busy && ti->type->busy(ti))
893 return BLK_MQ_RQ_QUEUE_BUSY;
894
895 dm_start_request(md, rq);
896
897 /* Init tio using md established in .init_request */
898 init_tio(tio, rq, md);
899
900 /*
901 * Establish tio->ti before calling map_request().
902 */
903 tio->ti = ti;
904
905 /* Direct call is fine since .queue_rq allows allocations */
906 if (map_request(tio) == DM_MAPIO_REQUEUE) {
907 /* Undo dm_start_request() before requeuing */
908 rq_end_stats(md, rq);
909 rq_completed(md, rq_data_dir(rq), false);
910 return BLK_MQ_RQ_QUEUE_BUSY;
911 }
912
913 return BLK_MQ_RQ_QUEUE_OK;
914}
915
916static struct blk_mq_ops dm_mq_ops = {
917 .queue_rq = dm_mq_queue_rq,
918 .complete = dm_softirq_done,
919 .init_request = dm_mq_init_request,
920};
921
922int dm_mq_init_request_queue(struct mapped_device *md, struct dm_table *t)
923{
924 struct request_queue *q;
925 struct dm_target *immutable_tgt;
926 int err;
927
928 if (!dm_table_all_blk_mq_devices(t)) {
929 DMERR("request-based dm-mq may only be stacked on blk-mq device(s)");
930 return -EINVAL;
931 }
932
933 md->tag_set = kzalloc_node(sizeof(struct blk_mq_tag_set), GFP_KERNEL, md->numa_node_id);
934 if (!md->tag_set)
935 return -ENOMEM;
936
937 md->tag_set->ops = &dm_mq_ops;
938 md->tag_set->queue_depth = dm_get_blk_mq_queue_depth();
939 md->tag_set->numa_node = md->numa_node_id;
940 md->tag_set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
941 md->tag_set->nr_hw_queues = dm_get_blk_mq_nr_hw_queues();
942 md->tag_set->driver_data = md;
943
944 md->tag_set->cmd_size = sizeof(struct dm_rq_target_io);
945 immutable_tgt = dm_table_get_immutable_target(t);
946 if (immutable_tgt && immutable_tgt->per_io_data_size) {
947 /* any target-specific per-io data is immediately after the tio */
948 md->tag_set->cmd_size += immutable_tgt->per_io_data_size;
949 md->init_tio_pdu = true;
950 }
951
952 err = blk_mq_alloc_tag_set(md->tag_set);
953 if (err)
954 goto out_kfree_tag_set;
955
956 q = blk_mq_init_allocated_queue(md->tag_set, md->queue);
957 if (IS_ERR(q)) {
958 err = PTR_ERR(q);
959 goto out_tag_set;
960 }
961 dm_init_md_queue(md);
962
963 /* backfill 'mq' sysfs registration normally done in blk_register_queue */
964 blk_mq_register_dev(disk_to_dev(md->disk), q);
965
966 return 0;
967
968out_tag_set:
969 blk_mq_free_tag_set(md->tag_set);
970out_kfree_tag_set:
971 kfree(md->tag_set);
972
973 return err;
974}
975
976void dm_mq_cleanup_mapped_device(struct mapped_device *md)
977{
978 if (md->tag_set) {
979 blk_mq_free_tag_set(md->tag_set);
980 kfree(md->tag_set);
981 }
982}
983
984module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
985MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
986
987module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
988MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
989
990module_param(dm_mq_nr_hw_queues, uint, S_IRUGO | S_IWUSR);
991MODULE_PARM_DESC(dm_mq_nr_hw_queues, "Number of hardware queues for request-based dm-mq devices");
992
993module_param(dm_mq_queue_depth, uint, S_IRUGO | S_IWUSR);
994MODULE_PARM_DESC(dm_mq_queue_depth, "Queue depth for request-based dm-mq devices");