linux/block/blk-core.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 1991, 1992 Linus Torvalds
   3 * Copyright (C) 1994,      Karl Keyte: Added support for disk statistics
   4 * Elevator latency, (C) 2000  Andrea Arcangeli <andrea@suse.de> SuSE
   5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
   6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
   7 *      -  July2000
   8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
   9 */
  10
  11/*
  12 * This handles all read/write requests to block devices
  13 */
  14#include <linux/kernel.h>
  15#include <linux/module.h>
  16#include <linux/backing-dev.h>
  17#include <linux/bio.h>
  18#include <linux/blkdev.h>
  19#include <linux/blk-mq.h>
  20#include <linux/highmem.h>
  21#include <linux/mm.h>
  22#include <linux/kernel_stat.h>
  23#include <linux/string.h>
  24#include <linux/init.h>
  25#include <linux/completion.h>
  26#include <linux/slab.h>
  27#include <linux/swap.h>
  28#include <linux/writeback.h>
  29#include <linux/task_io_accounting_ops.h>
  30#include <linux/fault-inject.h>
  31#include <linux/list_sort.h>
  32#include <linux/delay.h>
  33#include <linux/ratelimit.h>
  34#include <linux/pm_runtime.h>
  35#include <linux/blk-cgroup.h>
  36#include <linux/t10-pi.h>
  37#include <linux/debugfs.h>
  38#include <linux/bpf.h>
  39
  40#define CREATE_TRACE_POINTS
  41#include <trace/events/block.h>
  42
  43#include "blk.h"
  44#include "blk-mq.h"
  45#include "blk-mq-sched.h"
  46#include "blk-pm.h"
  47#include "blk-rq-qos.h"
  48
  49#ifdef CONFIG_DEBUG_FS
  50struct dentry *blk_debugfs_root;
  51#endif
  52
  53EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
  54EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
  55EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
  56EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
  57EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
  58
  59DEFINE_IDA(blk_queue_ida);
  60
  61/*
  62 * For queue allocation
  63 */
  64struct kmem_cache *blk_requestq_cachep;
  65
  66/*
  67 * Controlling structure to kblockd
  68 */
  69static struct workqueue_struct *kblockd_workqueue;
  70
  71/**
  72 * blk_queue_flag_set - atomically set a queue flag
  73 * @flag: flag to be set
  74 * @q: request queue
  75 */
  76void blk_queue_flag_set(unsigned int flag, struct request_queue *q)
  77{
  78        set_bit(flag, &q->queue_flags);
  79}
  80EXPORT_SYMBOL(blk_queue_flag_set);
  81
  82/**
  83 * blk_queue_flag_clear - atomically clear a queue flag
  84 * @flag: flag to be cleared
  85 * @q: request queue
  86 */
  87void blk_queue_flag_clear(unsigned int flag, struct request_queue *q)
  88{
  89        clear_bit(flag, &q->queue_flags);
  90}
  91EXPORT_SYMBOL(blk_queue_flag_clear);
  92
  93/**
  94 * blk_queue_flag_test_and_set - atomically test and set a queue flag
  95 * @flag: flag to be set
  96 * @q: request queue
  97 *
  98 * Returns the previous value of @flag - 0 if the flag was not set and 1 if
  99 * the flag was already set.
 100 */
 101bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q)
 102{
 103        return test_and_set_bit(flag, &q->queue_flags);
 104}
 105EXPORT_SYMBOL_GPL(blk_queue_flag_test_and_set);
 106
 107void blk_rq_init(struct request_queue *q, struct request *rq)
 108{
 109        memset(rq, 0, sizeof(*rq));
 110
 111        INIT_LIST_HEAD(&rq->queuelist);
 112        rq->q = q;
 113        rq->__sector = (sector_t) -1;
 114        INIT_HLIST_NODE(&rq->hash);
 115        RB_CLEAR_NODE(&rq->rb_node);
 116        rq->tag = -1;
 117        rq->internal_tag = -1;
 118        rq->start_time_ns = ktime_get_ns();
 119        rq->part = NULL;
 120        refcount_set(&rq->ref, 1);
 121}
 122EXPORT_SYMBOL(blk_rq_init);
 123
 124#define REQ_OP_NAME(name) [REQ_OP_##name] = #name
 125static const char *const blk_op_name[] = {
 126        REQ_OP_NAME(READ),
 127        REQ_OP_NAME(WRITE),
 128        REQ_OP_NAME(FLUSH),
 129        REQ_OP_NAME(DISCARD),
 130        REQ_OP_NAME(SECURE_ERASE),
 131        REQ_OP_NAME(ZONE_RESET),
 132        REQ_OP_NAME(ZONE_RESET_ALL),
 133        REQ_OP_NAME(ZONE_OPEN),
 134        REQ_OP_NAME(ZONE_CLOSE),
 135        REQ_OP_NAME(ZONE_FINISH),
 136        REQ_OP_NAME(WRITE_SAME),
 137        REQ_OP_NAME(WRITE_ZEROES),
 138        REQ_OP_NAME(SCSI_IN),
 139        REQ_OP_NAME(SCSI_OUT),
 140        REQ_OP_NAME(DRV_IN),
 141        REQ_OP_NAME(DRV_OUT),
 142};
 143#undef REQ_OP_NAME
 144
 145/**
 146 * blk_op_str - Return string XXX in the REQ_OP_XXX.
 147 * @op: REQ_OP_XXX.
 148 *
 149 * Description: Centralize block layer function to convert REQ_OP_XXX into
 150 * string format. Useful in the debugging and tracing bio or request. For
 151 * invalid REQ_OP_XXX it returns string "UNKNOWN".
 152 */
 153inline const char *blk_op_str(unsigned int op)
 154{
 155        const char *op_str = "UNKNOWN";
 156
 157        if (op < ARRAY_SIZE(blk_op_name) && blk_op_name[op])
 158                op_str = blk_op_name[op];
 159
 160        return op_str;
 161}
 162EXPORT_SYMBOL_GPL(blk_op_str);
 163
 164static const struct {
 165        int             errno;
 166        const char      *name;
 167} blk_errors[] = {
 168        [BLK_STS_OK]            = { 0,          "" },
 169        [BLK_STS_NOTSUPP]       = { -EOPNOTSUPP, "operation not supported" },
 170        [BLK_STS_TIMEOUT]       = { -ETIMEDOUT, "timeout" },
 171        [BLK_STS_NOSPC]         = { -ENOSPC,    "critical space allocation" },
 172        [BLK_STS_TRANSPORT]     = { -ENOLINK,   "recoverable transport" },
 173        [BLK_STS_TARGET]        = { -EREMOTEIO, "critical target" },
 174        [BLK_STS_NEXUS]         = { -EBADE,     "critical nexus" },
 175        [BLK_STS_MEDIUM]        = { -ENODATA,   "critical medium" },
 176        [BLK_STS_PROTECTION]    = { -EILSEQ,    "protection" },
 177        [BLK_STS_RESOURCE]      = { -ENOMEM,    "kernel resource" },
 178        [BLK_STS_DEV_RESOURCE]  = { -EBUSY,     "device resource" },
 179        [BLK_STS_AGAIN]         = { -EAGAIN,    "nonblocking retry" },
 180
 181        /* device mapper special case, should not leak out: */
 182        [BLK_STS_DM_REQUEUE]    = { -EREMCHG, "dm internal retry" },
 183
 184        /* everything else not covered above: */
 185        [BLK_STS_IOERR]         = { -EIO,       "I/O" },
 186};
 187
 188blk_status_t errno_to_blk_status(int errno)
 189{
 190        int i;
 191
 192        for (i = 0; i < ARRAY_SIZE(blk_errors); i++) {
 193                if (blk_errors[i].errno == errno)
 194                        return (__force blk_status_t)i;
 195        }
 196
 197        return BLK_STS_IOERR;
 198}
 199EXPORT_SYMBOL_GPL(errno_to_blk_status);
 200
 201int blk_status_to_errno(blk_status_t status)
 202{
 203        int idx = (__force int)status;
 204
 205        if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
 206                return -EIO;
 207        return blk_errors[idx].errno;
 208}
 209EXPORT_SYMBOL_GPL(blk_status_to_errno);
 210
 211static void print_req_error(struct request *req, blk_status_t status,
 212                const char *caller)
 213{
 214        int idx = (__force int)status;
 215
 216        if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
 217                return;
 218
 219        printk_ratelimited(KERN_ERR
 220                "%s: %s error, dev %s, sector %llu op 0x%x:(%s) flags 0x%x "
 221                "phys_seg %u prio class %u\n",
 222                caller, blk_errors[idx].name,
 223                req->rq_disk ? req->rq_disk->disk_name : "?",
 224                blk_rq_pos(req), req_op(req), blk_op_str(req_op(req)),
 225                req->cmd_flags & ~REQ_OP_MASK,
 226                req->nr_phys_segments,
 227                IOPRIO_PRIO_CLASS(req->ioprio));
 228}
 229
 230static void req_bio_endio(struct request *rq, struct bio *bio,
 231                          unsigned int nbytes, blk_status_t error)
 232{
 233        if (error)
 234                bio->bi_status = error;
 235
 236        if (unlikely(rq->rq_flags & RQF_QUIET))
 237                bio_set_flag(bio, BIO_QUIET);
 238
 239        bio_advance(bio, nbytes);
 240
 241        /* don't actually finish bio if it's part of flush sequence */
 242        if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
 243                bio_endio(bio);
 244}
 245
 246void blk_dump_rq_flags(struct request *rq, char *msg)
 247{
 248        printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
 249                rq->rq_disk ? rq->rq_disk->disk_name : "?",
 250                (unsigned long long) rq->cmd_flags);
 251
 252        printk(KERN_INFO "  sector %llu, nr/cnr %u/%u\n",
 253               (unsigned long long)blk_rq_pos(rq),
 254               blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
 255        printk(KERN_INFO "  bio %p, biotail %p, len %u\n",
 256               rq->bio, rq->biotail, blk_rq_bytes(rq));
 257}
 258EXPORT_SYMBOL(blk_dump_rq_flags);
 259
 260/**
 261 * blk_sync_queue - cancel any pending callbacks on a queue
 262 * @q: the queue
 263 *
 264 * Description:
 265 *     The block layer may perform asynchronous callback activity
 266 *     on a queue, such as calling the unplug function after a timeout.
 267 *     A block device may call blk_sync_queue to ensure that any
 268 *     such activity is cancelled, thus allowing it to release resources
 269 *     that the callbacks might use. The caller must already have made sure
 270 *     that its ->make_request_fn will not re-add plugging prior to calling
 271 *     this function.
 272 *
 273 *     This function does not cancel any asynchronous activity arising
 274 *     out of elevator or throttling code. That would require elevator_exit()
 275 *     and blkcg_exit_queue() to be called with queue lock initialized.
 276 *
 277 */
 278void blk_sync_queue(struct request_queue *q)
 279{
 280        del_timer_sync(&q->timeout);
 281        cancel_work_sync(&q->timeout_work);
 282}
 283EXPORT_SYMBOL(blk_sync_queue);
 284
 285/**
 286 * blk_set_pm_only - increment pm_only counter
 287 * @q: request queue pointer
 288 */
 289void blk_set_pm_only(struct request_queue *q)
 290{
 291        atomic_inc(&q->pm_only);
 292}
 293EXPORT_SYMBOL_GPL(blk_set_pm_only);
 294
 295void blk_clear_pm_only(struct request_queue *q)
 296{
 297        int pm_only;
 298
 299        pm_only = atomic_dec_return(&q->pm_only);
 300        WARN_ON_ONCE(pm_only < 0);
 301        if (pm_only == 0)
 302                wake_up_all(&q->mq_freeze_wq);
 303}
 304EXPORT_SYMBOL_GPL(blk_clear_pm_only);
 305
 306/**
 307 * blk_put_queue - decrement the request_queue refcount
 308 * @q: the request_queue structure to decrement the refcount for
 309 *
 310 * Decrements the refcount of the request_queue kobject. When this reaches 0
 311 * we'll have blk_release_queue() called.
 312 *
 313 * Context: Any context, but the last reference must not be dropped from
 314 *          atomic context.
 315 */
 316void blk_put_queue(struct request_queue *q)
 317{
 318        kobject_put(&q->kobj);
 319}
 320EXPORT_SYMBOL(blk_put_queue);
 321
 322void blk_set_queue_dying(struct request_queue *q)
 323{
 324        blk_queue_flag_set(QUEUE_FLAG_DYING, q);
 325
 326        /*
 327         * When queue DYING flag is set, we need to block new req
 328         * entering queue, so we call blk_freeze_queue_start() to
 329         * prevent I/O from crossing blk_queue_enter().
 330         */
 331        blk_freeze_queue_start(q);
 332
 333        if (queue_is_mq(q))
 334                blk_mq_wake_waiters(q);
 335
 336        /* Make blk_queue_enter() reexamine the DYING flag. */
 337        wake_up_all(&q->mq_freeze_wq);
 338}
 339EXPORT_SYMBOL_GPL(blk_set_queue_dying);
 340
 341/**
 342 * blk_cleanup_queue - shutdown a request queue
 343 * @q: request queue to shutdown
 344 *
 345 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
 346 * put it.  All future requests will be failed immediately with -ENODEV.
 347 *
 348 * Context: can sleep
 349 */
 350void blk_cleanup_queue(struct request_queue *q)
 351{
 352        /* cannot be called from atomic context */
 353        might_sleep();
 354
 355        WARN_ON_ONCE(blk_queue_registered(q));
 356
 357        /* mark @q DYING, no new request or merges will be allowed afterwards */
 358        blk_set_queue_dying(q);
 359
 360        blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
 361        blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
 362        blk_queue_flag_set(QUEUE_FLAG_DYING, q);
 363
 364        /*
 365         * Drain all requests queued before DYING marking. Set DEAD flag to
 366         * prevent that blk_mq_run_hw_queues() accesses the hardware queues
 367         * after draining finished.
 368         */
 369        blk_freeze_queue(q);
 370
 371        rq_qos_exit(q);
 372
 373        blk_queue_flag_set(QUEUE_FLAG_DEAD, q);
 374
 375        /* for synchronous bio-based driver finish in-flight integrity i/o */
 376        blk_flush_integrity();
 377
 378        /* @q won't process any more request, flush async actions */
 379        del_timer_sync(&q->backing_dev_info->laptop_mode_wb_timer);
 380        blk_sync_queue(q);
 381
 382        if (queue_is_mq(q))
 383                blk_mq_exit_queue(q);
 384
 385        /*
 386         * In theory, request pool of sched_tags belongs to request queue.
 387         * However, the current implementation requires tag_set for freeing
 388         * requests, so free the pool now.
 389         *
 390         * Queue has become frozen, there can't be any in-queue requests, so
 391         * it is safe to free requests now.
 392         */
 393        mutex_lock(&q->sysfs_lock);
 394        if (q->elevator)
 395                blk_mq_sched_free_requests(q);
 396        mutex_unlock(&q->sysfs_lock);
 397
 398        percpu_ref_exit(&q->q_usage_counter);
 399
 400        /* @q is and will stay empty, shutdown and put */
 401        blk_put_queue(q);
 402}
 403EXPORT_SYMBOL(blk_cleanup_queue);
 404
 405struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
 406{
 407        return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
 408}
 409EXPORT_SYMBOL(blk_alloc_queue);
 410
 411/**
 412 * blk_queue_enter() - try to increase q->q_usage_counter
 413 * @q: request queue pointer
 414 * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PREEMPT
 415 */
 416int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
 417{
 418        const bool pm = flags & BLK_MQ_REQ_PREEMPT;
 419
 420        while (true) {
 421                bool success = false;
 422
 423                rcu_read_lock();
 424                if (percpu_ref_tryget_live(&q->q_usage_counter)) {
 425                        /*
 426                         * The code that increments the pm_only counter is
 427                         * responsible for ensuring that that counter is
 428                         * globally visible before the queue is unfrozen.
 429                         */
 430                        if (pm || !blk_queue_pm_only(q)) {
 431                                success = true;
 432                        } else {
 433                                percpu_ref_put(&q->q_usage_counter);
 434                        }
 435                }
 436                rcu_read_unlock();
 437
 438                if (success)
 439                        return 0;
 440
 441                if (flags & BLK_MQ_REQ_NOWAIT)
 442                        return -EBUSY;
 443
 444                /*
 445                 * read pair of barrier in blk_freeze_queue_start(),
 446                 * we need to order reading __PERCPU_REF_DEAD flag of
 447                 * .q_usage_counter and reading .mq_freeze_depth or
 448                 * queue dying flag, otherwise the following wait may
 449                 * never return if the two reads are reordered.
 450                 */
 451                smp_rmb();
 452
 453                wait_event(q->mq_freeze_wq,
 454                           (!q->mq_freeze_depth &&
 455                            (pm || (blk_pm_request_resume(q),
 456                                    !blk_queue_pm_only(q)))) ||
 457                           blk_queue_dying(q));
 458                if (blk_queue_dying(q))
 459                        return -ENODEV;
 460        }
 461}
 462
 463void blk_queue_exit(struct request_queue *q)
 464{
 465        percpu_ref_put(&q->q_usage_counter);
 466}
 467
 468static void blk_queue_usage_counter_release(struct percpu_ref *ref)
 469{
 470        struct request_queue *q =
 471                container_of(ref, struct request_queue, q_usage_counter);
 472
 473        wake_up_all(&q->mq_freeze_wq);
 474}
 475
 476static void blk_rq_timed_out_timer(struct timer_list *t)
 477{
 478        struct request_queue *q = from_timer(q, t, timeout);
 479
 480        kblockd_schedule_work(&q->timeout_work);
 481}
 482
 483/**
 484 * blk_alloc_queue_node - allocate a request queue
 485 * @gfp_mask: memory allocation flags
 486 * @node_id: NUMA node to allocate memory from
 487 */
 488struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
 489{
 490        struct request_queue *q;
 491        int ret;
 492
 493        q = kmem_cache_alloc_node(blk_requestq_cachep,
 494                                gfp_mask | __GFP_ZERO, node_id);
 495        if (!q)
 496                return NULL;
 497
 498        q->last_merge = NULL;
 499
 500        q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
 501        if (q->id < 0)
 502                goto fail_q;
 503
 504        ret = bioset_init(&q->bio_split, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
 505        if (ret)
 506                goto fail_id;
 507
 508        q->backing_dev_info = bdi_alloc_node(gfp_mask, node_id);
 509        if (!q->backing_dev_info)
 510                goto fail_split;
 511
 512        q->stats = blk_alloc_queue_stats();
 513        if (!q->stats)
 514                goto fail_stats;
 515
 516        q->backing_dev_info->ra_pages = VM_READAHEAD_PAGES;
 517        q->backing_dev_info->capabilities = BDI_CAP_CGROUP_WRITEBACK;
 518        q->backing_dev_info->name = "block";
 519        q->node = node_id;
 520
 521        timer_setup(&q->backing_dev_info->laptop_mode_wb_timer,
 522                    laptop_mode_timer_fn, 0);
 523        timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
 524        INIT_WORK(&q->timeout_work, NULL);
 525        INIT_LIST_HEAD(&q->icq_list);
 526#ifdef CONFIG_BLK_CGROUP
 527        INIT_LIST_HEAD(&q->blkg_list);
 528#endif
 529
 530        kobject_init(&q->kobj, &blk_queue_ktype);
 531
 532#ifdef CONFIG_BLK_DEV_IO_TRACE
 533        mutex_init(&q->blk_trace_mutex);
 534#endif
 535        mutex_init(&q->sysfs_lock);
 536        mutex_init(&q->sysfs_dir_lock);
 537        spin_lock_init(&q->queue_lock);
 538
 539        init_waitqueue_head(&q->mq_freeze_wq);
 540        mutex_init(&q->mq_freeze_lock);
 541
 542        /*
 543         * Init percpu_ref in atomic mode so that it's faster to shutdown.
 544         * See blk_register_queue() for details.
 545         */
 546        if (percpu_ref_init(&q->q_usage_counter,
 547                                blk_queue_usage_counter_release,
 548                                PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
 549                goto fail_bdi;
 550
 551        if (blkcg_init_queue(q))
 552                goto fail_ref;
 553
 554        return q;
 555
 556fail_ref:
 557        percpu_ref_exit(&q->q_usage_counter);
 558fail_bdi:
 559        blk_free_queue_stats(q->stats);
 560fail_stats:
 561        bdi_put(q->backing_dev_info);
 562fail_split:
 563        bioset_exit(&q->bio_split);
 564fail_id:
 565        ida_simple_remove(&blk_queue_ida, q->id);
 566fail_q:
 567        kmem_cache_free(blk_requestq_cachep, q);
 568        return NULL;
 569}
 570EXPORT_SYMBOL(blk_alloc_queue_node);
 571
 572/**
 573 * blk_get_queue - increment the request_queue refcount
 574 * @q: the request_queue structure to increment the refcount for
 575 *
 576 * Increment the refcount of the request_queue kobject.
 577 *
 578 * Context: Any context.
 579 */
 580bool blk_get_queue(struct request_queue *q)
 581{
 582        if (likely(!blk_queue_dying(q))) {
 583                __blk_get_queue(q);
 584                return true;
 585        }
 586
 587        return false;
 588}
 589EXPORT_SYMBOL(blk_get_queue);
 590
 591/**
 592 * blk_get_request - allocate a request
 593 * @q: request queue to allocate a request for
 594 * @op: operation (REQ_OP_*) and REQ_* flags, e.g. REQ_SYNC.
 595 * @flags: BLK_MQ_REQ_* flags, e.g. BLK_MQ_REQ_NOWAIT.
 596 */
 597struct request *blk_get_request(struct request_queue *q, unsigned int op,
 598                                blk_mq_req_flags_t flags)
 599{
 600        struct request *req;
 601
 602        WARN_ON_ONCE(op & REQ_NOWAIT);
 603        WARN_ON_ONCE(flags & ~(BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_PREEMPT));
 604
 605        req = blk_mq_alloc_request(q, op, flags);
 606        if (!IS_ERR(req) && q->mq_ops->initialize_rq_fn)
 607                q->mq_ops->initialize_rq_fn(req);
 608
 609        return req;
 610}
 611EXPORT_SYMBOL(blk_get_request);
 612
 613void blk_put_request(struct request *req)
 614{
 615        blk_mq_free_request(req);
 616}
 617EXPORT_SYMBOL(blk_put_request);
 618
 619bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
 620                            struct bio *bio)
 621{
 622        const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
 623
 624        if (!ll_back_merge_fn(q, req, bio))
 625                return false;
 626
 627        trace_block_bio_backmerge(q, req, bio);
 628        rq_qos_merge(req->q, req, bio);
 629
 630        if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
 631                blk_rq_set_mixed_merge(req);
 632
 633        req->biotail->bi_next = bio;
 634        req->biotail = bio;
 635        req->__data_len += bio->bi_iter.bi_size;
 636
 637        blk_account_io_start(req, false);
 638        return true;
 639}
 640
 641bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
 642                             struct bio *bio)
 643{
 644        const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
 645
 646        if (!ll_front_merge_fn(q, req, bio))
 647                return false;
 648
 649        trace_block_bio_frontmerge(q, req, bio);
 650        rq_qos_merge(req->q, req, bio);
 651
 652        if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
 653                blk_rq_set_mixed_merge(req);
 654
 655        bio->bi_next = req->bio;
 656        req->bio = bio;
 657
 658        req->__sector = bio->bi_iter.bi_sector;
 659        req->__data_len += bio->bi_iter.bi_size;
 660
 661        blk_account_io_start(req, false);
 662        return true;
 663}
 664
 665bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
 666                struct bio *bio)
 667{
 668        unsigned short segments = blk_rq_nr_discard_segments(req);
 669
 670        if (segments >= queue_max_discard_segments(q))
 671                goto no_merge;
 672        if (blk_rq_sectors(req) + bio_sectors(bio) >
 673            blk_rq_get_max_sectors(req, blk_rq_pos(req)))
 674                goto no_merge;
 675
 676        rq_qos_merge(q, req, bio);
 677
 678        req->biotail->bi_next = bio;
 679        req->biotail = bio;
 680        req->__data_len += bio->bi_iter.bi_size;
 681        req->nr_phys_segments = segments + 1;
 682
 683        blk_account_io_start(req, false);
 684        return true;
 685no_merge:
 686        req_set_nomerge(q, req);
 687        return false;
 688}
 689
 690/**
 691 * blk_attempt_plug_merge - try to merge with %current's plugged list
 692 * @q: request_queue new bio is being queued at
 693 * @bio: new bio being queued
 694 * @request_count: out parameter for number of traversed plugged requests
 695 * @same_queue_rq: pointer to &struct request that gets filled in when
 696 * another request associated with @q is found on the plug list
 697 * (optional, may be %NULL)
 698 *
 699 * Determine whether @bio being queued on @q can be merged with a request
 700 * on %current's plugged list.  Returns %true if merge was successful,
 701 * otherwise %false.
 702 *
 703 * Plugging coalesces IOs from the same issuer for the same purpose without
 704 * going through @q->queue_lock.  As such it's more of an issuing mechanism
 705 * than scheduling, and the request, while may have elvpriv data, is not
 706 * added on the elevator at this point.  In addition, we don't have
 707 * reliable access to the elevator outside queue lock.  Only check basic
 708 * merging parameters without querying the elevator.
 709 *
 710 * Caller must ensure !blk_queue_nomerges(q) beforehand.
 711 */
 712bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
 713                            struct request **same_queue_rq)
 714{
 715        struct blk_plug *plug;
 716        struct request *rq;
 717        struct list_head *plug_list;
 718
 719        plug = blk_mq_plug(q, bio);
 720        if (!plug)
 721                return false;
 722
 723        plug_list = &plug->mq_list;
 724
 725        list_for_each_entry_reverse(rq, plug_list, queuelist) {
 726                bool merged = false;
 727
 728                if (rq->q == q && same_queue_rq) {
 729                        /*
 730                         * Only blk-mq multiple hardware queues case checks the
 731                         * rq in the same queue, there should be only one such
 732                         * rq in a queue
 733                         **/
 734                        *same_queue_rq = rq;
 735                }
 736
 737                if (rq->q != q || !blk_rq_merge_ok(rq, bio))
 738                        continue;
 739
 740                switch (blk_try_merge(rq, bio)) {
 741                case ELEVATOR_BACK_MERGE:
 742                        merged = bio_attempt_back_merge(q, rq, bio);
 743                        break;
 744                case ELEVATOR_FRONT_MERGE:
 745                        merged = bio_attempt_front_merge(q, rq, bio);
 746                        break;
 747                case ELEVATOR_DISCARD_MERGE:
 748                        merged = bio_attempt_discard_merge(q, rq, bio);
 749                        break;
 750                default:
 751                        break;
 752                }
 753
 754                if (merged)
 755                        return true;
 756        }
 757
 758        return false;
 759}
 760
 761static void handle_bad_sector(struct bio *bio, sector_t maxsector)
 762{
 763        char b[BDEVNAME_SIZE];
 764
 765        printk(KERN_INFO "attempt to access beyond end of device\n");
 766        printk(KERN_INFO "%s: rw=%d, want=%Lu, limit=%Lu\n",
 767                        bio_devname(bio, b), bio->bi_opf,
 768                        (unsigned long long)bio_end_sector(bio),
 769                        (long long)maxsector);
 770}
 771
 772#ifdef CONFIG_FAIL_MAKE_REQUEST
 773
 774static DECLARE_FAULT_ATTR(fail_make_request);
 775
 776static int __init setup_fail_make_request(char *str)
 777{
 778        return setup_fault_attr(&fail_make_request, str);
 779}
 780__setup("fail_make_request=", setup_fail_make_request);
 781
 782static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
 783{
 784        return part->make_it_fail && should_fail(&fail_make_request, bytes);
 785}
 786
 787static int __init fail_make_request_debugfs(void)
 788{
 789        struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
 790                                                NULL, &fail_make_request);
 791
 792        return PTR_ERR_OR_ZERO(dir);
 793}
 794
 795late_initcall(fail_make_request_debugfs);
 796
 797#else /* CONFIG_FAIL_MAKE_REQUEST */
 798
 799static inline bool should_fail_request(struct hd_struct *part,
 800                                        unsigned int bytes)
 801{
 802        return false;
 803}
 804
 805#endif /* CONFIG_FAIL_MAKE_REQUEST */
 806
 807static inline bool bio_check_ro(struct bio *bio, struct hd_struct *part)
 808{
 809        const int op = bio_op(bio);
 810
 811        if (part->policy && op_is_write(op)) {
 812                char b[BDEVNAME_SIZE];
 813
 814                if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
 815                        return false;
 816
 817                WARN_ONCE(1,
 818                       "generic_make_request: Trying to write "
 819                        "to read-only block-device %s (partno %d)\n",
 820                        bio_devname(bio, b), part->partno);
 821                /* Older lvm-tools actually trigger this */
 822                return false;
 823        }
 824
 825        return false;
 826}
 827
 828static noinline int should_fail_bio(struct bio *bio)
 829{
 830        if (should_fail_request(&bio->bi_disk->part0, bio->bi_iter.bi_size))
 831                return -EIO;
 832        return 0;
 833}
 834ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
 835
 836/*
 837 * Check whether this bio extends beyond the end of the device or partition.
 838 * This may well happen - the kernel calls bread() without checking the size of
 839 * the device, e.g., when mounting a file system.
 840 */
 841static inline int bio_check_eod(struct bio *bio, sector_t maxsector)
 842{
 843        unsigned int nr_sectors = bio_sectors(bio);
 844
 845        if (nr_sectors && maxsector &&
 846            (nr_sectors > maxsector ||
 847             bio->bi_iter.bi_sector > maxsector - nr_sectors)) {
 848                handle_bad_sector(bio, maxsector);
 849                return -EIO;
 850        }
 851        return 0;
 852}
 853
 854/*
 855 * Remap block n of partition p to block n+start(p) of the disk.
 856 */
 857static inline int blk_partition_remap(struct bio *bio)
 858{
 859        struct hd_struct *p;
 860        int ret = -EIO;
 861
 862        rcu_read_lock();
 863        p = __disk_get_part(bio->bi_disk, bio->bi_partno);
 864        if (unlikely(!p))
 865                goto out;
 866        if (unlikely(should_fail_request(p, bio->bi_iter.bi_size)))
 867                goto out;
 868        if (unlikely(bio_check_ro(bio, p)))
 869                goto out;
 870
 871        if (bio_sectors(bio)) {
 872                if (bio_check_eod(bio, part_nr_sects_read(p)))
 873                        goto out;
 874                bio->bi_iter.bi_sector += p->start_sect;
 875                trace_block_bio_remap(bio->bi_disk->queue, bio, part_devt(p),
 876                                      bio->bi_iter.bi_sector - p->start_sect);
 877        }
 878        bio->bi_partno = 0;
 879        ret = 0;
 880out:
 881        rcu_read_unlock();
 882        return ret;
 883}
 884
 885static noinline_for_stack bool
 886generic_make_request_checks(struct bio *bio)
 887{
 888        struct request_queue *q;
 889        int nr_sectors = bio_sectors(bio);
 890        blk_status_t status = BLK_STS_IOERR;
 891        char b[BDEVNAME_SIZE];
 892
 893        might_sleep();
 894
 895        q = bio->bi_disk->queue;
 896        if (unlikely(!q)) {
 897                printk(KERN_ERR
 898                       "generic_make_request: Trying to access "
 899                        "nonexistent block-device %s (%Lu)\n",
 900                        bio_devname(bio, b), (long long)bio->bi_iter.bi_sector);
 901                goto end_io;
 902        }
 903
 904        /*
 905         * For a REQ_NOWAIT based request, return -EOPNOTSUPP
 906         * if queue is not a request based queue.
 907         */
 908        if ((bio->bi_opf & REQ_NOWAIT) && !queue_is_mq(q))
 909                goto not_supported;
 910
 911        if (should_fail_bio(bio))
 912                goto end_io;
 913
 914        if (bio->bi_partno) {
 915                if (unlikely(blk_partition_remap(bio)))
 916                        goto end_io;
 917        } else {
 918                if (unlikely(bio_check_ro(bio, &bio->bi_disk->part0)))
 919                        goto end_io;
 920                if (unlikely(bio_check_eod(bio, get_capacity(bio->bi_disk))))
 921                        goto end_io;
 922        }
 923
 924        /*
 925         * Filter flush bio's early so that make_request based
 926         * drivers without flush support don't have to worry
 927         * about them.
 928         */
 929        if (op_is_flush(bio->bi_opf) &&
 930            !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
 931                bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
 932                if (!nr_sectors) {
 933                        status = BLK_STS_OK;
 934                        goto end_io;
 935                }
 936        }
 937
 938        if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
 939                bio->bi_opf &= ~REQ_HIPRI;
 940
 941        switch (bio_op(bio)) {
 942        case REQ_OP_DISCARD:
 943                if (!blk_queue_discard(q))
 944                        goto not_supported;
 945                break;
 946        case REQ_OP_SECURE_ERASE:
 947                if (!blk_queue_secure_erase(q))
 948                        goto not_supported;
 949                break;
 950        case REQ_OP_WRITE_SAME:
 951                if (!q->limits.max_write_same_sectors)
 952                        goto not_supported;
 953                break;
 954        case REQ_OP_ZONE_RESET:
 955        case REQ_OP_ZONE_OPEN:
 956        case REQ_OP_ZONE_CLOSE:
 957        case REQ_OP_ZONE_FINISH:
 958                if (!blk_queue_is_zoned(q))
 959                        goto not_supported;
 960                break;
 961        case REQ_OP_ZONE_RESET_ALL:
 962                if (!blk_queue_is_zoned(q) || !blk_queue_zone_resetall(q))
 963                        goto not_supported;
 964                break;
 965        case REQ_OP_WRITE_ZEROES:
 966                if (!q->limits.max_write_zeroes_sectors)
 967                        goto not_supported;
 968                break;
 969        default:
 970                break;
 971        }
 972
 973        /*
 974         * Various block parts want %current->io_context and lazy ioc
 975         * allocation ends up trading a lot of pain for a small amount of
 976         * memory.  Just allocate it upfront.  This may fail and block
 977         * layer knows how to live with it.
 978         */
 979        create_io_context(GFP_ATOMIC, q->node);
 980
 981        if (!blkcg_bio_issue_check(q, bio))
 982                return false;
 983
 984        if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
 985                trace_block_bio_queue(q, bio);
 986                /* Now that enqueuing has been traced, we need to trace
 987                 * completion as well.
 988                 */
 989                bio_set_flag(bio, BIO_TRACE_COMPLETION);
 990        }
 991        return true;
 992
 993not_supported:
 994        status = BLK_STS_NOTSUPP;
 995end_io:
 996        bio->bi_status = status;
 997        bio_endio(bio);
 998        return false;
 999}
1000
1001/**
1002 * generic_make_request - hand a buffer to its device driver for I/O
1003 * @bio:  The bio describing the location in memory and on the device.
1004 *
1005 * generic_make_request() is used to make I/O requests of block
1006 * devices. It is passed a &struct bio, which describes the I/O that needs
1007 * to be done.
1008 *
1009 * generic_make_request() does not return any status.  The
1010 * success/failure status of the request, along with notification of
1011 * completion, is delivered asynchronously through the bio->bi_end_io
1012 * function described (one day) else where.
1013 *
1014 * The caller of generic_make_request must make sure that bi_io_vec
1015 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1016 * set to describe the device address, and the
1017 * bi_end_io and optionally bi_private are set to describe how
1018 * completion notification should be signaled.
1019 *
1020 * generic_make_request and the drivers it calls may use bi_next if this
1021 * bio happens to be merged with someone else, and may resubmit the bio to
1022 * a lower device by calling into generic_make_request recursively, which
1023 * means the bio should NOT be touched after the call to ->make_request_fn.
1024 */
1025blk_qc_t generic_make_request(struct bio *bio)
1026{
1027        /*
1028         * bio_list_on_stack[0] contains bios submitted by the current
1029         * make_request_fn.
1030         * bio_list_on_stack[1] contains bios that were submitted before
1031         * the current make_request_fn, but that haven't been processed
1032         * yet.
1033         */
1034        struct bio_list bio_list_on_stack[2];
1035        blk_qc_t ret = BLK_QC_T_NONE;
1036
1037        if (!generic_make_request_checks(bio))
1038                goto out;
1039
1040        /*
1041         * We only want one ->make_request_fn to be active at a time, else
1042         * stack usage with stacked devices could be a problem.  So use
1043         * current->bio_list to keep a list of requests submited by a
1044         * make_request_fn function.  current->bio_list is also used as a
1045         * flag to say if generic_make_request is currently active in this
1046         * task or not.  If it is NULL, then no make_request is active.  If
1047         * it is non-NULL, then a make_request is active, and new requests
1048         * should be added at the tail
1049         */
1050        if (current->bio_list) {
1051                bio_list_add(&current->bio_list[0], bio);
1052                goto out;
1053        }
1054
1055        /* following loop may be a bit non-obvious, and so deserves some
1056         * explanation.
1057         * Before entering the loop, bio->bi_next is NULL (as all callers
1058         * ensure that) so we have a list with a single bio.
1059         * We pretend that we have just taken it off a longer list, so
1060         * we assign bio_list to a pointer to the bio_list_on_stack,
1061         * thus initialising the bio_list of new bios to be
1062         * added.  ->make_request() may indeed add some more bios
1063         * through a recursive call to generic_make_request.  If it
1064         * did, we find a non-NULL value in bio_list and re-enter the loop
1065         * from the top.  In this case we really did just take the bio
1066         * of the top of the list (no pretending) and so remove it from
1067         * bio_list, and call into ->make_request() again.
1068         */
1069        BUG_ON(bio->bi_next);
1070        bio_list_init(&bio_list_on_stack[0]);
1071        current->bio_list = bio_list_on_stack;
1072        do {
1073                struct request_queue *q = bio->bi_disk->queue;
1074                blk_mq_req_flags_t flags = bio->bi_opf & REQ_NOWAIT ?
1075                        BLK_MQ_REQ_NOWAIT : 0;
1076
1077                if (likely(blk_queue_enter(q, flags) == 0)) {
1078                        struct bio_list lower, same;
1079
1080                        /* Create a fresh bio_list for all subordinate requests */
1081                        bio_list_on_stack[1] = bio_list_on_stack[0];
1082                        bio_list_init(&bio_list_on_stack[0]);
1083                        ret = q->make_request_fn(q, bio);
1084
1085                        blk_queue_exit(q);
1086
1087                        /* sort new bios into those for a lower level
1088                         * and those for the same level
1089                         */
1090                        bio_list_init(&lower);
1091                        bio_list_init(&same);
1092                        while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
1093                                if (q == bio->bi_disk->queue)
1094                                        bio_list_add(&same, bio);
1095                                else
1096                                        bio_list_add(&lower, bio);
1097                        /* now assemble so we handle the lowest level first */
1098                        bio_list_merge(&bio_list_on_stack[0], &lower);
1099                        bio_list_merge(&bio_list_on_stack[0], &same);
1100                        bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
1101                } else {
1102                        if (unlikely(!blk_queue_dying(q) &&
1103                                        (bio->bi_opf & REQ_NOWAIT)))
1104                                bio_wouldblock_error(bio);
1105                        else
1106                                bio_io_error(bio);
1107                }
1108                bio = bio_list_pop(&bio_list_on_stack[0]);
1109        } while (bio);
1110        current->bio_list = NULL; /* deactivate */
1111
1112out:
1113        return ret;
1114}
1115EXPORT_SYMBOL(generic_make_request);
1116
1117/**
1118 * direct_make_request - hand a buffer directly to its device driver for I/O
1119 * @bio:  The bio describing the location in memory and on the device.
1120 *
1121 * This function behaves like generic_make_request(), but does not protect
1122 * against recursion.  Must only be used if the called driver is known
1123 * to not call generic_make_request (or direct_make_request) again from
1124 * its make_request function.  (Calling direct_make_request again from
1125 * a workqueue is perfectly fine as that doesn't recurse).
1126 */
1127blk_qc_t direct_make_request(struct bio *bio)
1128{
1129        struct request_queue *q = bio->bi_disk->queue;
1130        bool nowait = bio->bi_opf & REQ_NOWAIT;
1131        blk_qc_t ret;
1132
1133        if (!generic_make_request_checks(bio))
1134                return BLK_QC_T_NONE;
1135
1136        if (unlikely(blk_queue_enter(q, nowait ? BLK_MQ_REQ_NOWAIT : 0))) {
1137                if (nowait && !blk_queue_dying(q))
1138                        bio->bi_status = BLK_STS_AGAIN;
1139                else
1140                        bio->bi_status = BLK_STS_IOERR;
1141                bio_endio(bio);
1142                return BLK_QC_T_NONE;
1143        }
1144
1145        ret = q->make_request_fn(q, bio);
1146        blk_queue_exit(q);
1147        return ret;
1148}
1149EXPORT_SYMBOL_GPL(direct_make_request);
1150
1151/**
1152 * submit_bio - submit a bio to the block device layer for I/O
1153 * @bio: The &struct bio which describes the I/O
1154 *
1155 * submit_bio() is very similar in purpose to generic_make_request(), and
1156 * uses that function to do most of the work. Both are fairly rough
1157 * interfaces; @bio must be presetup and ready for I/O.
1158 *
1159 */
1160blk_qc_t submit_bio(struct bio *bio)
1161{
1162        if (blkcg_punt_bio_submit(bio))
1163                return BLK_QC_T_NONE;
1164
1165        /*
1166         * If it's a regular read/write or a barrier with data attached,
1167         * go through the normal accounting stuff before submission.
1168         */
1169        if (bio_has_data(bio)) {
1170                unsigned int count;
1171
1172                if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
1173                        count = queue_logical_block_size(bio->bi_disk->queue) >> 9;
1174                else
1175                        count = bio_sectors(bio);
1176
1177                if (op_is_write(bio_op(bio))) {
1178                        count_vm_events(PGPGOUT, count);
1179                } else {
1180                        task_io_account_read(bio->bi_iter.bi_size);
1181                        count_vm_events(PGPGIN, count);
1182                }
1183
1184                if (unlikely(block_dump)) {
1185                        char b[BDEVNAME_SIZE];
1186                        printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
1187                        current->comm, task_pid_nr(current),
1188                                op_is_write(bio_op(bio)) ? "WRITE" : "READ",
1189                                (unsigned long long)bio->bi_iter.bi_sector,
1190                                bio_devname(bio, b), count);
1191                }
1192        }
1193
1194        return generic_make_request(bio);
1195}
1196EXPORT_SYMBOL(submit_bio);
1197
1198/**
1199 * blk_cloned_rq_check_limits - Helper function to check a cloned request
1200 *                              for new the queue limits
1201 * @q:  the queue
1202 * @rq: the request being checked
1203 *
1204 * Description:
1205 *    @rq may have been made based on weaker limitations of upper-level queues
1206 *    in request stacking drivers, and it may violate the limitation of @q.
1207 *    Since the block layer and the underlying device driver trust @rq
1208 *    after it is inserted to @q, it should be checked against @q before
1209 *    the insertion using this generic function.
1210 *
1211 *    Request stacking drivers like request-based dm may change the queue
1212 *    limits when retrying requests on other queues. Those requests need
1213 *    to be checked against the new queue limits again during dispatch.
1214 */
1215static int blk_cloned_rq_check_limits(struct request_queue *q,
1216                                      struct request *rq)
1217{
1218        if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, req_op(rq))) {
1219                printk(KERN_ERR "%s: over max size limit. (%u > %u)\n",
1220                        __func__, blk_rq_sectors(rq),
1221                        blk_queue_get_max_sectors(q, req_op(rq)));
1222                return -EIO;
1223        }
1224
1225        /*
1226         * queue's settings related to segment counting like q->bounce_pfn
1227         * may differ from that of other stacking queues.
1228         * Recalculate it to check the request correctly on this queue's
1229         * limitation.
1230         */
1231        blk_recalc_rq_segments(rq);
1232        if (rq->nr_phys_segments > queue_max_segments(q)) {
1233                printk(KERN_ERR "%s: over max segments limit. (%hu > %hu)\n",
1234                        __func__, rq->nr_phys_segments, queue_max_segments(q));
1235                return -EIO;
1236        }
1237
1238        return 0;
1239}
1240
1241/**
1242 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
1243 * @q:  the queue to submit the request
1244 * @rq: the request being queued
1245 */
1246blk_status_t blk_insert_cloned_request(struct request_queue *q, struct request *rq)
1247{
1248        if (blk_cloned_rq_check_limits(q, rq))
1249                return BLK_STS_IOERR;
1250
1251        if (rq->rq_disk &&
1252            should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
1253                return BLK_STS_IOERR;
1254
1255        if (blk_queue_io_stat(q))
1256                blk_account_io_start(rq, true);
1257
1258        /*
1259         * Since we have a scheduler attached on the top device,
1260         * bypass a potential scheduler on the bottom device for
1261         * insert.
1262         */
1263        return blk_mq_request_issue_directly(rq, true);
1264}
1265EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1266
1267/**
1268 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
1269 * @rq: request to examine
1270 *
1271 * Description:
1272 *     A request could be merge of IOs which require different failure
1273 *     handling.  This function determines the number of bytes which
1274 *     can be failed from the beginning of the request without
1275 *     crossing into area which need to be retried further.
1276 *
1277 * Return:
1278 *     The number of bytes to fail.
1279 */
1280unsigned int blk_rq_err_bytes(const struct request *rq)
1281{
1282        unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
1283        unsigned int bytes = 0;
1284        struct bio *bio;
1285
1286        if (!(rq->rq_flags & RQF_MIXED_MERGE))
1287                return blk_rq_bytes(rq);
1288
1289        /*
1290         * Currently the only 'mixing' which can happen is between
1291         * different fastfail types.  We can safely fail portions
1292         * which have all the failfast bits that the first one has -
1293         * the ones which are at least as eager to fail as the first
1294         * one.
1295         */
1296        for (bio = rq->bio; bio; bio = bio->bi_next) {
1297                if ((bio->bi_opf & ff) != ff)
1298                        break;
1299                bytes += bio->bi_iter.bi_size;
1300        }
1301
1302        /* this could lead to infinite loop */
1303        BUG_ON(blk_rq_bytes(rq) && !bytes);
1304        return bytes;
1305}
1306EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1307
1308void blk_account_io_completion(struct request *req, unsigned int bytes)
1309{
1310        if (req->part && blk_do_io_stat(req)) {
1311                const int sgrp = op_stat_group(req_op(req));
1312                struct hd_struct *part;
1313
1314                part_stat_lock();
1315                part = req->part;
1316                part_stat_add(part, sectors[sgrp], bytes >> 9);
1317                part_stat_unlock();
1318        }
1319}
1320
1321void blk_account_io_done(struct request *req, u64 now)
1322{
1323        /*
1324         * Account IO completion.  flush_rq isn't accounted as a
1325         * normal IO on queueing nor completion.  Accounting the
1326         * containing request is enough.
1327         */
1328        if (req->part && blk_do_io_stat(req) &&
1329            !(req->rq_flags & RQF_FLUSH_SEQ)) {
1330                const int sgrp = op_stat_group(req_op(req));
1331                struct hd_struct *part;
1332
1333                part_stat_lock();
1334                part = req->part;
1335
1336                update_io_ticks(part, jiffies, true);
1337                part_stat_inc(part, ios[sgrp]);
1338                part_stat_add(part, nsecs[sgrp], now - req->start_time_ns);
1339                part_stat_add(part, time_in_queue, nsecs_to_jiffies64(now - req->start_time_ns));
1340                part_dec_in_flight(req->q, part, rq_data_dir(req));
1341
1342                hd_struct_put(part);
1343                part_stat_unlock();
1344        }
1345}
1346
1347void blk_account_io_start(struct request *rq, bool new_io)
1348{
1349        struct hd_struct *part;
1350        int rw = rq_data_dir(rq);
1351
1352        if (!blk_do_io_stat(rq))
1353                return;
1354
1355        part_stat_lock();
1356
1357        if (!new_io) {
1358                part = rq->part;
1359                part_stat_inc(part, merges[rw]);
1360        } else {
1361                part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
1362                part_inc_in_flight(rq->q, part, rw);
1363                rq->part = part;
1364        }
1365
1366        update_io_ticks(part, jiffies, false);
1367
1368        part_stat_unlock();
1369}
1370
1371/*
1372 * Steal bios from a request and add them to a bio list.
1373 * The request must not have been partially completed before.
1374 */
1375void blk_steal_bios(struct bio_list *list, struct request *rq)
1376{
1377        if (rq->bio) {
1378                if (list->tail)
1379                        list->tail->bi_next = rq->bio;
1380                else
1381                        list->head = rq->bio;
1382                list->tail = rq->biotail;
1383
1384                rq->bio = NULL;
1385                rq->biotail = NULL;
1386        }
1387
1388        rq->__data_len = 0;
1389}
1390EXPORT_SYMBOL_GPL(blk_steal_bios);
1391
1392/**
1393 * blk_update_request - Special helper function for request stacking drivers
1394 * @req:      the request being processed
1395 * @error:    block status code
1396 * @nr_bytes: number of bytes to complete @req
1397 *
1398 * Description:
1399 *     Ends I/O on a number of bytes attached to @req, but doesn't complete
1400 *     the request structure even if @req doesn't have leftover.
1401 *     If @req has leftover, sets it up for the next range of segments.
1402 *
1403 *     This special helper function is only for request stacking drivers
1404 *     (e.g. request-based dm) so that they can handle partial completion.
1405 *     Actual device drivers should use blk_mq_end_request instead.
1406 *
1407 *     Passing the result of blk_rq_bytes() as @nr_bytes guarantees
1408 *     %false return from this function.
1409 *
1410 * Note:
1411 *      The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in both
1412 *      blk_rq_bytes() and in blk_update_request().
1413 *
1414 * Return:
1415 *     %false - this request doesn't have any more data
1416 *     %true  - this request has more data
1417 **/
1418bool blk_update_request(struct request *req, blk_status_t error,
1419                unsigned int nr_bytes)
1420{
1421        int total_bytes;
1422
1423        trace_block_rq_complete(req, blk_status_to_errno(error), nr_bytes);
1424
1425        if (!req->bio)
1426                return false;
1427
1428#ifdef CONFIG_BLK_DEV_INTEGRITY
1429        if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ &&
1430            error == BLK_STS_OK)
1431                req->q->integrity.profile->ext_ops->complete_fn(req, nr_bytes);
1432#endif
1433
1434        if (unlikely(error && !blk_rq_is_passthrough(req) &&
1435                     !(req->rq_flags & RQF_QUIET)))
1436                print_req_error(req, error, __func__);
1437
1438        blk_account_io_completion(req, nr_bytes);
1439
1440        total_bytes = 0;
1441        while (req->bio) {
1442                struct bio *bio = req->bio;
1443                unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
1444
1445                if (bio_bytes == bio->bi_iter.bi_size)
1446                        req->bio = bio->bi_next;
1447
1448                /* Completion has already been traced */
1449                bio_clear_flag(bio, BIO_TRACE_COMPLETION);
1450                req_bio_endio(req, bio, bio_bytes, error);
1451
1452                total_bytes += bio_bytes;
1453                nr_bytes -= bio_bytes;
1454
1455                if (!nr_bytes)
1456                        break;
1457        }
1458
1459        /*
1460         * completely done
1461         */
1462        if (!req->bio) {
1463                /*
1464                 * Reset counters so that the request stacking driver
1465                 * can find how many bytes remain in the request
1466                 * later.
1467                 */
1468                req->__data_len = 0;
1469                return false;
1470        }
1471
1472        req->__data_len -= total_bytes;
1473
1474        /* update sector only for requests with clear definition of sector */
1475        if (!blk_rq_is_passthrough(req))
1476                req->__sector += total_bytes >> 9;
1477
1478        /* mixed attributes always follow the first bio */
1479        if (req->rq_flags & RQF_MIXED_MERGE) {
1480                req->cmd_flags &= ~REQ_FAILFAST_MASK;
1481                req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
1482        }
1483
1484        if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) {
1485                /*
1486                 * If total number of sectors is less than the first segment
1487                 * size, something has gone terribly wrong.
1488                 */
1489                if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
1490                        blk_dump_rq_flags(req, "request botched");
1491                        req->__data_len = blk_rq_cur_bytes(req);
1492                }
1493
1494                /* recalculate the number of segments */
1495                blk_recalc_rq_segments(req);
1496        }
1497
1498        return true;
1499}
1500EXPORT_SYMBOL_GPL(blk_update_request);
1501
1502void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
1503                     struct bio *bio)
1504{
1505        if (bio_has_data(bio))
1506                rq->nr_phys_segments = bio_phys_segments(q, bio);
1507        else if (bio_op(bio) == REQ_OP_DISCARD)
1508                rq->nr_phys_segments = 1;
1509
1510        rq->__data_len = bio->bi_iter.bi_size;
1511        rq->bio = rq->biotail = bio;
1512        rq->ioprio = bio_prio(bio);
1513
1514        if (bio->bi_disk)
1515                rq->rq_disk = bio->bi_disk;
1516}
1517
1518#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
1519/**
1520 * rq_flush_dcache_pages - Helper function to flush all pages in a request
1521 * @rq: the request to be flushed
1522 *
1523 * Description:
1524 *     Flush all pages in @rq.
1525 */
1526void rq_flush_dcache_pages(struct request *rq)
1527{
1528        struct req_iterator iter;
1529        struct bio_vec bvec;
1530
1531        rq_for_each_segment(bvec, rq, iter)
1532                flush_dcache_page(bvec.bv_page);
1533}
1534EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
1535#endif
1536
1537/**
1538 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
1539 * @q : the queue of the device being checked
1540 *
1541 * Description:
1542 *    Check if underlying low-level drivers of a device are busy.
1543 *    If the drivers want to export their busy state, they must set own
1544 *    exporting function using blk_queue_lld_busy() first.
1545 *
1546 *    Basically, this function is used only by request stacking drivers
1547 *    to stop dispatching requests to underlying devices when underlying
1548 *    devices are busy.  This behavior helps more I/O merging on the queue
1549 *    of the request stacking driver and prevents I/O throughput regression
1550 *    on burst I/O load.
1551 *
1552 * Return:
1553 *    0 - Not busy (The request stacking driver should dispatch request)
1554 *    1 - Busy (The request stacking driver should stop dispatching request)
1555 */
1556int blk_lld_busy(struct request_queue *q)
1557{
1558        if (queue_is_mq(q) && q->mq_ops->busy)
1559                return q->mq_ops->busy(q);
1560
1561        return 0;
1562}
1563EXPORT_SYMBOL_GPL(blk_lld_busy);
1564
1565/**
1566 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
1567 * @rq: the clone request to be cleaned up
1568 *
1569 * Description:
1570 *     Free all bios in @rq for a cloned request.
1571 */
1572void blk_rq_unprep_clone(struct request *rq)
1573{
1574        struct bio *bio;
1575
1576        while ((bio = rq->bio) != NULL) {
1577                rq->bio = bio->bi_next;
1578
1579                bio_put(bio);
1580        }
1581}
1582EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
1583
1584/*
1585 * Copy attributes of the original request to the clone request.
1586 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
1587 */
1588static void __blk_rq_prep_clone(struct request *dst, struct request *src)
1589{
1590        dst->__sector = blk_rq_pos(src);
1591        dst->__data_len = blk_rq_bytes(src);
1592        if (src->rq_flags & RQF_SPECIAL_PAYLOAD) {
1593                dst->rq_flags |= RQF_SPECIAL_PAYLOAD;
1594                dst->special_vec = src->special_vec;
1595        }
1596        dst->nr_phys_segments = src->nr_phys_segments;
1597        dst->ioprio = src->ioprio;
1598        dst->extra_len = src->extra_len;
1599}
1600
1601/**
1602 * blk_rq_prep_clone - Helper function to setup clone request
1603 * @rq: the request to be setup
1604 * @rq_src: original request to be cloned
1605 * @bs: bio_set that bios for clone are allocated from
1606 * @gfp_mask: memory allocation mask for bio
1607 * @bio_ctr: setup function to be called for each clone bio.
1608 *           Returns %0 for success, non %0 for failure.
1609 * @data: private data to be passed to @bio_ctr
1610 *
1611 * Description:
1612 *     Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
1613 *     The actual data parts of @rq_src (e.g. ->cmd, ->sense)
1614 *     are not copied, and copying such parts is the caller's responsibility.
1615 *     Also, pages which the original bios are pointing to are not copied
1616 *     and the cloned bios just point same pages.
1617 *     So cloned bios must be completed before original bios, which means
1618 *     the caller must complete @rq before @rq_src.
1619 */
1620int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
1621                      struct bio_set *bs, gfp_t gfp_mask,
1622                      int (*bio_ctr)(struct bio *, struct bio *, void *),
1623                      void *data)
1624{
1625        struct bio *bio, *bio_src;
1626
1627        if (!bs)
1628                bs = &fs_bio_set;
1629
1630        __rq_for_each_bio(bio_src, rq_src) {
1631                bio = bio_clone_fast(bio_src, gfp_mask, bs);
1632                if (!bio)
1633                        goto free_and_out;
1634
1635                if (bio_ctr && bio_ctr(bio, bio_src, data))
1636                        goto free_and_out;
1637
1638                if (rq->bio) {
1639                        rq->biotail->bi_next = bio;
1640                        rq->biotail = bio;
1641                } else
1642                        rq->bio = rq->biotail = bio;
1643        }
1644
1645        __blk_rq_prep_clone(rq, rq_src);
1646
1647        return 0;
1648
1649free_and_out:
1650        if (bio)
1651                bio_put(bio);
1652        blk_rq_unprep_clone(rq);
1653
1654        return -ENOMEM;
1655}
1656EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
1657
1658int kblockd_schedule_work(struct work_struct *work)
1659{
1660        return queue_work(kblockd_workqueue, work);
1661}
1662EXPORT_SYMBOL(kblockd_schedule_work);
1663
1664int kblockd_schedule_work_on(int cpu, struct work_struct *work)
1665{
1666        return queue_work_on(cpu, kblockd_workqueue, work);
1667}
1668EXPORT_SYMBOL(kblockd_schedule_work_on);
1669
1670int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
1671                                unsigned long delay)
1672{
1673        return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
1674}
1675EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
1676
1677/**
1678 * blk_start_plug - initialize blk_plug and track it inside the task_struct
1679 * @plug:       The &struct blk_plug that needs to be initialized
1680 *
1681 * Description:
1682 *   Tracking blk_plug inside the task_struct will help with auto-flushing the
1683 *   pending I/O should the task end up blocking between blk_start_plug() and
1684 *   blk_finish_plug(). This is important from a performance perspective, but
1685 *   also ensures that we don't deadlock. For instance, if the task is blocking
1686 *   for a memory allocation, memory reclaim could end up wanting to free a
1687 *   page belonging to that request that is currently residing in our private
1688 *   plug. By flushing the pending I/O when the process goes to sleep, we avoid
1689 *   this kind of deadlock.
1690 */
1691void blk_start_plug(struct blk_plug *plug)
1692{
1693        struct task_struct *tsk = current;
1694
1695        /*
1696         * If this is a nested plug, don't actually assign it.
1697         */
1698        if (tsk->plug)
1699                return;
1700
1701        INIT_LIST_HEAD(&plug->mq_list);
1702        INIT_LIST_HEAD(&plug->cb_list);
1703        plug->rq_count = 0;
1704        plug->multiple_queues = false;
1705
1706        /*
1707         * Store ordering should not be needed here, since a potential
1708         * preempt will imply a full memory barrier
1709         */
1710        tsk->plug = plug;
1711}
1712EXPORT_SYMBOL(blk_start_plug);
1713
1714static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
1715{
1716        LIST_HEAD(callbacks);
1717
1718        while (!list_empty(&plug->cb_list)) {
1719                list_splice_init(&plug->cb_list, &callbacks);
1720
1721                while (!list_empty(&callbacks)) {
1722                        struct blk_plug_cb *cb = list_first_entry(&callbacks,
1723                                                          struct blk_plug_cb,
1724                                                          list);
1725                        list_del(&cb->list);
1726                        cb->callback(cb, from_schedule);
1727                }
1728        }
1729}
1730
1731struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
1732                                      int size)
1733{
1734        struct blk_plug *plug = current->plug;
1735        struct blk_plug_cb *cb;
1736
1737        if (!plug)
1738                return NULL;
1739
1740        list_for_each_entry(cb, &plug->cb_list, list)
1741                if (cb->callback == unplug && cb->data == data)
1742                        return cb;
1743
1744        /* Not currently on the callback list */
1745        BUG_ON(size < sizeof(*cb));
1746        cb = kzalloc(size, GFP_ATOMIC);
1747        if (cb) {
1748                cb->data = data;
1749                cb->callback = unplug;
1750                list_add(&cb->list, &plug->cb_list);
1751        }
1752        return cb;
1753}
1754EXPORT_SYMBOL(blk_check_plugged);
1755
1756void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1757{
1758        flush_plug_callbacks(plug, from_schedule);
1759
1760        if (!list_empty(&plug->mq_list))
1761                blk_mq_flush_plug_list(plug, from_schedule);
1762}
1763
1764void blk_finish_plug(struct blk_plug *plug)
1765{
1766        if (plug != current->plug)
1767                return;
1768        blk_flush_plug_list(plug, false);
1769
1770        current->plug = NULL;
1771}
1772EXPORT_SYMBOL(blk_finish_plug);
1773
1774/*
1775 * For 3rd party modules to access the extended fields of 'struct request'
1776 */
1777struct request_aux *blk_rq_aux(const struct request *rq)
1778{
1779        return (struct request_aux *)((void *)rq - sizeof(struct request_aux));
1780}
1781EXPORT_SYMBOL(blk_rq_aux);
1782
1783int __init blk_dev_init(void)
1784{
1785        BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
1786        BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
1787                        FIELD_SIZEOF(struct request, cmd_flags));
1788        BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
1789                        FIELD_SIZEOF(struct bio, bi_opf));
1790
1791        /* used for unplugging and affects IO latency/throughput - HIGHPRI */
1792        kblockd_workqueue = alloc_workqueue("kblockd",
1793                                            WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
1794        if (!kblockd_workqueue)
1795                panic("Failed to create kblockd\n");
1796
1797        blk_requestq_cachep = kmem_cache_create("request_queue",
1798                        sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
1799
1800#ifdef CONFIG_DEBUG_FS
1801        blk_debugfs_root = debugfs_create_dir("block", NULL);
1802#endif
1803
1804        return 0;
1805}
1806