~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to block/blk-core.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <linux/writeback.h>
28
28
#include <linux/task_io_accounting_ops.h>
29
29
#include <linux/fault-inject.h>
 
30
#include <linux/list_sort.h>
30
31
 
31
32
#define CREATE_TRACE_POINTS
32
33
#include <trace/events/block.h>
149
150
static void req_bio_endio(struct request *rq, struct bio *bio,
150
151
                          unsigned int nbytes, int error)
151
152
{
152
 
        struct request_queue *q = rq->q;
153
 
 
154
 
        if (&q->flush_rq != rq) {
155
 
                if (error)
156
 
                        clear_bit(BIO_UPTODATE, &bio->bi_flags);
157
 
                else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
158
 
                        error = -EIO;
159
 
 
160
 
                if (unlikely(nbytes > bio->bi_size)) {
161
 
                        printk(KERN_ERR "%s: want %u bytes done, %u left\n",
162
 
                               __func__, nbytes, bio->bi_size);
163
 
                        nbytes = bio->bi_size;
164
 
                }
165
 
 
166
 
                if (unlikely(rq->cmd_flags & REQ_QUIET))
167
 
                        set_bit(BIO_QUIET, &bio->bi_flags);
168
 
 
169
 
                bio->bi_size -= nbytes;
170
 
                bio->bi_sector += (nbytes >> 9);
171
 
 
172
 
                if (bio_integrity(bio))
173
 
                        bio_integrity_advance(bio, nbytes);
174
 
 
175
 
                if (bio->bi_size == 0)
176
 
                        bio_endio(bio, error);
177
 
        } else {
178
 
                /*
179
 
                 * Okay, this is the sequenced flush request in
180
 
                 * progress, just record the error;
181
 
                 */
182
 
                if (error && !q->flush_err)
183
 
                        q->flush_err = error;
 
153
        if (error)
 
154
                clear_bit(BIO_UPTODATE, &bio->bi_flags);
 
155
        else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
 
156
                error = -EIO;
 
157
 
 
158
        if (unlikely(nbytes > bio->bi_size)) {
 
159
                printk(KERN_ERR "%s: want %u bytes done, %u left\n",
 
160
                       __func__, nbytes, bio->bi_size);
 
161
                nbytes = bio->bi_size;
184
162
        }
 
163
 
 
164
        if (unlikely(rq->cmd_flags & REQ_QUIET))
 
165
                set_bit(BIO_QUIET, &bio->bi_flags);
 
166
 
 
167
        bio->bi_size -= nbytes;
 
168
        bio->bi_sector += (nbytes >> 9);
 
169
 
 
170
        if (bio_integrity(bio))
 
171
                bio_integrity_advance(bio, nbytes);
 
172
 
 
173
        /* don't actually finish bio if it's part of flush sequence */
 
174
        if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
 
175
                bio_endio(bio, error);
185
176
}
186
177
 
187
178
void blk_dump_rq_flags(struct request *rq, char *msg)
207
198
}
208
199
EXPORT_SYMBOL(blk_dump_rq_flags);
209
200
 
210
 
/*
211
 
 * "plug" the device if there are no outstanding requests: this will
212
 
 * force the transfer to start only after we have put all the requests
213
 
 * on the list.
214
 
 *
215
 
 * This is called with interrupts off and no requests on the queue and
216
 
 * with the queue lock held.
217
 
 */
218
 
void blk_plug_device(struct request_queue *q)
219
 
{
220
 
        WARN_ON(!irqs_disabled());
221
 
 
222
 
        /*
223
 
         * don't plug a stopped queue, it must be paired with blk_start_queue()
224
 
         * which will restart the queueing
225
 
         */
226
 
        if (blk_queue_stopped(q))
227
 
                return;
228
 
 
229
 
        if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
230
 
                mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
231
 
                trace_block_plug(q);
232
 
        }
233
 
}
234
 
EXPORT_SYMBOL(blk_plug_device);
235
 
 
236
 
/**
237
 
 * blk_plug_device_unlocked - plug a device without queue lock held
238
 
 * @q:    The &struct request_queue to plug
239
 
 *
240
 
 * Description:
241
 
 *   Like @blk_plug_device(), but grabs the queue lock and disables
242
 
 *   interrupts.
243
 
 **/
244
 
void blk_plug_device_unlocked(struct request_queue *q)
245
 
{
246
 
        unsigned long flags;
247
 
 
248
 
        spin_lock_irqsave(q->queue_lock, flags);
249
 
        blk_plug_device(q);
250
 
        spin_unlock_irqrestore(q->queue_lock, flags);
251
 
}
252
 
EXPORT_SYMBOL(blk_plug_device_unlocked);
253
 
 
254
 
/*
255
 
 * remove the queue from the plugged list, if present. called with
256
 
 * queue lock held and interrupts disabled.
257
 
 */
258
 
int blk_remove_plug(struct request_queue *q)
259
 
{
260
 
        WARN_ON(!irqs_disabled());
261
 
 
262
 
        if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
263
 
                return 0;
264
 
 
265
 
        del_timer(&q->unplug_timer);
266
 
        return 1;
267
 
}
268
 
EXPORT_SYMBOL(blk_remove_plug);
269
 
 
270
 
/*
271
 
 * remove the plug and let it rip..
272
 
 */
273
 
void __generic_unplug_device(struct request_queue *q)
274
 
{
275
 
        if (unlikely(blk_queue_stopped(q)))
276
 
                return;
277
 
        if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
278
 
                return;
279
 
 
280
 
        q->request_fn(q);
281
 
}
282
 
 
283
 
/**
284
 
 * generic_unplug_device - fire a request queue
285
 
 * @q:    The &struct request_queue in question
286
 
 *
287
 
 * Description:
288
 
 *   Linux uses plugging to build bigger requests queues before letting
289
 
 *   the device have at them. If a queue is plugged, the I/O scheduler
290
 
 *   is still adding and merging requests on the queue. Once the queue
291
 
 *   gets unplugged, the request_fn defined for the queue is invoked and
292
 
 *   transfers started.
293
 
 **/
294
 
void generic_unplug_device(struct request_queue *q)
295
 
{
296
 
        if (blk_queue_plugged(q)) {
297
 
                spin_lock_irq(q->queue_lock);
298
 
                __generic_unplug_device(q);
299
 
                spin_unlock_irq(q->queue_lock);
300
 
        }
301
 
}
302
 
EXPORT_SYMBOL(generic_unplug_device);
303
 
 
304
 
static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
305
 
                                   struct page *page)
306
 
{
307
 
        struct request_queue *q = bdi->unplug_io_data;
308
 
 
309
 
        blk_unplug(q);
310
 
}
311
 
 
312
 
void blk_unplug_work(struct work_struct *work)
313
 
{
314
 
        struct request_queue *q =
315
 
                container_of(work, struct request_queue, unplug_work);
316
 
 
317
 
        trace_block_unplug_io(q);
318
 
        q->unplug_fn(q);
319
 
}
320
 
 
321
 
void blk_unplug_timeout(unsigned long data)
322
 
{
323
 
        struct request_queue *q = (struct request_queue *)data;
324
 
 
325
 
        trace_block_unplug_timer(q);
326
 
        kblockd_schedule_work(q, &q->unplug_work);
327
 
}
328
 
 
329
 
void blk_unplug(struct request_queue *q)
330
 
{
331
 
        /*
332
 
         * devices don't necessarily have an ->unplug_fn defined
333
 
         */
334
 
        if (q->unplug_fn) {
335
 
                trace_block_unplug_io(q);
336
 
                q->unplug_fn(q);
337
 
        }
338
 
}
339
 
EXPORT_SYMBOL(blk_unplug);
 
201
static void blk_delay_work(struct work_struct *work)
 
202
{
 
203
        struct request_queue *q;
 
204
 
 
205
        q = container_of(work, struct request_queue, delay_work.work);
 
206
        spin_lock_irq(q->queue_lock);
 
207
        __blk_run_queue(q);
 
208
        spin_unlock_irq(q->queue_lock);
 
209
}
 
210
 
 
211
/**
 
212
 * blk_delay_queue - restart queueing after defined interval
 
213
 * @q:          The &struct request_queue in question
 
214
 * @msecs:      Delay in msecs
 
215
 *
 
216
 * Description:
 
217
 *   Sometimes queueing needs to be postponed for a little while, to allow
 
218
 *   resources to come back. This function will make sure that queueing is
 
219
 *   restarted around the specified time.
 
220
 */
 
221
void blk_delay_queue(struct request_queue *q, unsigned long msecs)
 
222
{
 
223
        queue_delayed_work(kblockd_workqueue, &q->delay_work,
 
224
                                msecs_to_jiffies(msecs));
 
225
}
 
226
EXPORT_SYMBOL(blk_delay_queue);
340
227
 
341
228
/**
342
229
 * blk_start_queue - restart a previously stopped queue
352
239
        WARN_ON(!irqs_disabled());
353
240
 
354
241
        queue_flag_clear(QUEUE_FLAG_STOPPED, q);
355
 
        __blk_run_queue(q, false);
 
242
        __blk_run_queue(q);
356
243
}
357
244
EXPORT_SYMBOL(blk_start_queue);
358
245
 
372
259
 **/
373
260
void blk_stop_queue(struct request_queue *q)
374
261
{
375
 
        blk_remove_plug(q);
 
262
        __cancel_delayed_work(&q->delay_work);
376
263
        queue_flag_set(QUEUE_FLAG_STOPPED, q);
377
264
}
378
265
EXPORT_SYMBOL(blk_stop_queue);
390
277
 *     that its ->make_request_fn will not re-add plugging prior to calling
391
278
 *     this function.
392
279
 *
 
280
 *     This function does not cancel any asynchronous activity arising
 
281
 *     out of elevator or throttling code. That would require elevaotor_exit()
 
282
 *     and blk_throtl_exit() to be called with queue lock initialized.
 
283
 *
393
284
 */
394
285
void blk_sync_queue(struct request_queue *q)
395
286
{
396
 
        del_timer_sync(&q->unplug_timer);
397
287
        del_timer_sync(&q->timeout);
398
 
        cancel_work_sync(&q->unplug_work);
399
 
        throtl_shutdown_timer_wq(q);
 
288
        cancel_delayed_work_sync(&q->delay_work);
400
289
}
401
290
EXPORT_SYMBOL(blk_sync_queue);
402
291
 
403
292
/**
404
293
 * __blk_run_queue - run a single device queue
405
294
 * @q:  The queue to run
406
 
 * @force_kblockd: Don't run @q->request_fn directly.  Use kblockd.
407
295
 *
408
296
 * Description:
409
297
 *    See @blk_run_queue. This variant must be called with the queue lock
410
298
 *    held and interrupts disabled.
411
 
 *
412
299
 */
413
 
void __blk_run_queue(struct request_queue *q, bool force_kblockd)
 
300
void __blk_run_queue(struct request_queue *q)
414
301
{
415
 
        blk_remove_plug(q);
416
 
 
417
302
        if (unlikely(blk_queue_stopped(q)))
418
303
                return;
419
304
 
420
 
        if (elv_queue_empty(q))
421
 
                return;
422
 
 
423
 
        /*
424
 
         * Only recurse once to avoid overrunning the stack, let the unplug
425
 
         * handling reinvoke the handler shortly if we already got there.
426
 
         */
427
 
        if (!force_kblockd && !queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
428
 
                q->request_fn(q);
429
 
                queue_flag_clear(QUEUE_FLAG_REENTER, q);
430
 
        } else {
431
 
                queue_flag_set(QUEUE_FLAG_PLUGGED, q);
432
 
                kblockd_schedule_work(q, &q->unplug_work);
433
 
        }
 
305
        q->request_fn(q);
434
306
}
435
307
EXPORT_SYMBOL(__blk_run_queue);
436
308
 
437
309
/**
 
310
 * blk_run_queue_async - run a single device queue in workqueue context
 
311
 * @q:  The queue to run
 
312
 *
 
313
 * Description:
 
314
 *    Tells kblockd to perform the equivalent of @blk_run_queue on behalf
 
315
 *    of us.
 
316
 */
 
317
void blk_run_queue_async(struct request_queue *q)
 
318
{
 
319
        if (likely(!blk_queue_stopped(q))) {
 
320
                __cancel_delayed_work(&q->delay_work);
 
321
                queue_delayed_work(kblockd_workqueue, &q->delay_work, 0);
 
322
        }
 
323
}
 
324
EXPORT_SYMBOL(blk_run_queue_async);
 
325
 
 
326
/**
438
327
 * blk_run_queue - run a single device queue
439
328
 * @q: The queue to run
440
329
 *
447
336
        unsigned long flags;
448
337
 
449
338
        spin_lock_irqsave(q->queue_lock, flags);
450
 
        __blk_run_queue(q, false);
 
339
        __blk_run_queue(q);
451
340
        spin_unlock_irqrestore(q->queue_lock, flags);
452
341
}
453
342
EXPORT_SYMBOL(blk_run_queue);
456
345
{
457
346
        kobject_put(&q->kobj);
458
347
}
 
348
EXPORT_SYMBOL(blk_put_queue);
459
349
 
 
350
/*
 
351
 * Note: If a driver supplied the queue lock, it should not zap that lock
 
352
 * unexpectedly as some queue cleanup components like elevator_exit() and
 
353
 * blk_throtl_exit() need queue lock.
 
354
 */
460
355
void blk_cleanup_queue(struct request_queue *q)
461
356
{
462
357
        /*
475
370
        if (q->elevator)
476
371
                elevator_exit(q->elevator);
477
372
 
 
373
        blk_throtl_exit(q);
 
374
 
478
375
        blk_put_queue(q);
479
376
}
480
377
EXPORT_SYMBOL(blk_cleanup_queue);
517
414
        if (!q)
518
415
                return NULL;
519
416
 
520
 
        q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
521
 
        q->backing_dev_info.unplug_io_data = q;
522
417
        q->backing_dev_info.ra_pages =
523
418
                        (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
524
419
        q->backing_dev_info.state = 0;
538
433
 
539
434
        setup_timer(&q->backing_dev_info.laptop_mode_wb_timer,
540
435
                    laptop_mode_timer_fn, (unsigned long) q);
541
 
        init_timer(&q->unplug_timer);
542
436
        setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
543
437
        INIT_LIST_HEAD(&q->timeout_list);
544
 
        INIT_LIST_HEAD(&q->pending_flushes);
545
 
        INIT_WORK(&q->unplug_work, blk_unplug_work);
 
438
        INIT_LIST_HEAD(&q->flush_queue[0]);
 
439
        INIT_LIST_HEAD(&q->flush_queue[1]);
 
440
        INIT_LIST_HEAD(&q->flush_data_in_flight);
 
441
        INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
546
442
 
547
443
        kobject_init(&q->kobj, &blk_queue_ktype);
548
444
 
549
445
        mutex_init(&q->sysfs_lock);
550
446
        spin_lock_init(&q->__queue_lock);
551
447
 
 
448
        /*
 
449
         * By default initialize queue_lock to internal lock and driver can
 
450
         * override it later if need be.
 
451
         */
 
452
        q->queue_lock = &q->__queue_lock;
 
453
 
552
454
        return q;
553
455
}
554
456
EXPORT_SYMBOL(blk_alloc_queue_node);
631
533
        q->request_fn           = rfn;
632
534
        q->prep_rq_fn           = NULL;
633
535
        q->unprep_rq_fn         = NULL;
634
 
        q->unplug_fn            = generic_unplug_device;
635
536
        q->queue_flags          = QUEUE_FLAG_DEFAULT;
636
 
        q->queue_lock           = lock;
 
537
 
 
538
        /* Override internal queue lock with supplied lock pointer */
 
539
        if (lock)
 
540
                q->queue_lock           = lock;
637
541
 
638
542
        /*
639
543
         * This also sets hw/phys segments, boundary and size
663
567
 
664
568
        return 1;
665
569
}
 
570
EXPORT_SYMBOL(blk_get_queue);
666
571
 
667
572
static inline void blk_free_request(struct request_queue *q, struct request *rq)
668
573
{
762
667
}
763
668
 
764
669
/*
 
670
 * Determine if elevator data should be initialized when allocating the
 
671
 * request associated with @bio.
 
672
 */
 
673
static bool blk_rq_should_init_elevator(struct bio *bio)
 
674
{
 
675
        if (!bio)
 
676
                return true;
 
677
 
 
678
        /*
 
679
         * Flush requests do not use the elevator so skip initialization.
 
680
         * This allows a request to share the flush and elevator data.
 
681
         */
 
682
        if (bio->bi_rw & (REQ_FLUSH | REQ_FUA))
 
683
                return false;
 
684
 
 
685
        return true;
 
686
}
 
687
 
 
688
/*
765
689
 * Get a free request, queue_lock must be held.
766
690
 * Returns NULL on failure, with queue_lock held.
767
691
 * Returns !NULL on success, with queue_lock *not held*.
773
697
        struct request_list *rl = &q->rq;
774
698
        struct io_context *ioc = NULL;
775
699
        const bool is_sync = rw_is_sync(rw_flags) != 0;
776
 
        int may_queue, priv;
 
700
        int may_queue, priv = 0;
777
701
 
778
702
        may_queue = elv_may_queue(q, rw_flags);
779
703
        if (may_queue == ELV_MQUEUE_NO)
817
741
        rl->count[is_sync]++;
818
742
        rl->starved[is_sync] = 0;
819
743
 
820
 
        priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
821
 
        if (priv)
822
 
                rl->elvpriv++;
 
744
        if (blk_rq_should_init_elevator(bio)) {
 
745
                priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
 
746
                if (priv)
 
747
                        rl->elvpriv++;
 
748
        }
823
749
 
824
750
        if (blk_queue_io_stat(q))
825
751
                rw_flags |= REQ_IO_STAT;
866
792
}
867
793
 
868
794
/*
869
 
 * No available requests for this queue, unplug the device and wait for some
870
 
 * requests to become available.
 
795
 * No available requests for this queue, wait for some requests to become
 
796
 * available.
871
797
 *
872
798
 * Called with q->queue_lock held, and returns with it unlocked.
873
799
 */
888
814
 
889
815
                trace_block_sleeprq(q, bio, rw_flags & 1);
890
816
 
891
 
                __generic_unplug_device(q);
892
817
                spin_unlock_irq(q->queue_lock);
893
818
                io_schedule();
894
819
 
1010
935
}
1011
936
EXPORT_SYMBOL(blk_requeue_request);
1012
937
 
 
938
static void add_acct_request(struct request_queue *q, struct request *rq,
 
939
                             int where)
 
940
{
 
941
        drive_stat_acct(rq, 1);
 
942
        __elv_add_request(q, rq, where);
 
943
}
 
944
 
1013
945
/**
1014
946
 * blk_insert_request - insert a special request into a request queue
1015
947
 * @q:          request queue where request should be inserted
1052
984
        if (blk_rq_tagged(rq))
1053
985
                blk_queue_end_tag(q, rq);
1054
986
 
1055
 
        drive_stat_acct(rq, 1);
1056
 
        __elv_add_request(q, rq, where, 0);
1057
 
        __blk_run_queue(q, false);
 
987
        add_acct_request(q, rq, where);
 
988
        __blk_run_queue(q);
1058
989
        spin_unlock_irqrestore(q->queue_lock, flags);
1059
990
}
1060
991
EXPORT_SYMBOL(blk_insert_request);
1174
1105
}
1175
1106
EXPORT_SYMBOL_GPL(blk_add_request_payload);
1176
1107
 
 
1108
static bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
 
1109
                                   struct bio *bio)
 
1110
{
 
1111
        const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
 
1112
 
 
1113
        if (!ll_back_merge_fn(q, req, bio))
 
1114
                return false;
 
1115
 
 
1116
        trace_block_bio_backmerge(q, bio);
 
1117
 
 
1118
        if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
 
1119
                blk_rq_set_mixed_merge(req);
 
1120
 
 
1121
        req->biotail->bi_next = bio;
 
1122
        req->biotail = bio;
 
1123
        req->__data_len += bio->bi_size;
 
1124
        req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
 
1125
 
 
1126
        drive_stat_acct(req, 0);
 
1127
        elv_bio_merged(q, req, bio);
 
1128
        return true;
 
1129
}
 
1130
 
 
1131
static bool bio_attempt_front_merge(struct request_queue *q,
 
1132
                                    struct request *req, struct bio *bio)
 
1133
{
 
1134
        const int ff = bio->bi_rw & REQ_FAILFAST_MASK;
 
1135
 
 
1136
        if (!ll_front_merge_fn(q, req, bio))
 
1137
                return false;
 
1138
 
 
1139
        trace_block_bio_frontmerge(q, bio);
 
1140
 
 
1141
        if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
 
1142
                blk_rq_set_mixed_merge(req);
 
1143
 
 
1144
        bio->bi_next = req->bio;
 
1145
        req->bio = bio;
 
1146
 
 
1147
        /*
 
1148
         * may not be valid. if the low level driver said
 
1149
         * it didn't need a bounce buffer then it better
 
1150
         * not touch req->buffer either...
 
1151
         */
 
1152
        req->buffer = bio_data(bio);
 
1153
        req->__sector = bio->bi_sector;
 
1154
        req->__data_len += bio->bi_size;
 
1155
        req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
 
1156
 
 
1157
        drive_stat_acct(req, 0);
 
1158
        elv_bio_merged(q, req, bio);
 
1159
        return true;
 
1160
}
 
1161
 
 
1162
/*
 
1163
 * Attempts to merge with the plugged list in the current process. Returns
 
1164
 * true if merge was successful, otherwise false.
 
1165
 */
 
1166
static bool attempt_plug_merge(struct task_struct *tsk, struct request_queue *q,
 
1167
                               struct bio *bio)
 
1168
{
 
1169
        struct blk_plug *plug;
 
1170
        struct request *rq;
 
1171
        bool ret = false;
 
1172
 
 
1173
        plug = tsk->plug;
 
1174
        if (!plug)
 
1175
                goto out;
 
1176
 
 
1177
        list_for_each_entry_reverse(rq, &plug->list, queuelist) {
 
1178
                int el_ret;
 
1179
 
 
1180
                if (rq->q != q)
 
1181
                        continue;
 
1182
 
 
1183
                el_ret = elv_try_merge(rq, bio);
 
1184
                if (el_ret == ELEVATOR_BACK_MERGE) {
 
1185
                        ret = bio_attempt_back_merge(q, rq, bio);
 
1186
                        if (ret)
 
1187
                                break;
 
1188
                } else if (el_ret == ELEVATOR_FRONT_MERGE) {
 
1189
                        ret = bio_attempt_front_merge(q, rq, bio);
 
1190
                        if (ret)
 
1191
                                break;
 
1192
                }
 
1193
        }
 
1194
out:
 
1195
        return ret;
 
1196
}
 
1197
 
1177
1198
void init_request_from_bio(struct request *req, struct bio *bio)
1178
1199
{
1179
1200
        req->cpu = bio->bi_comp_cpu;
1189
1210
        blk_rq_bio_prep(req->q, req, bio);
1190
1211
}
1191
1212
 
1192
 
/*
1193
 
 * Only disabling plugging for non-rotational devices if it does tagging
1194
 
 * as well, otherwise we do need the proper merging
1195
 
 */
1196
 
static inline bool queue_should_plug(struct request_queue *q)
1197
 
{
1198
 
        return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1199
 
}
1200
 
 
1201
1213
static int __make_request(struct request_queue *q, struct bio *bio)
1202
1214
{
 
1215
        const bool sync = !!(bio->bi_rw & REQ_SYNC);
 
1216
        struct blk_plug *plug;
 
1217
        int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT;
1203
1218
        struct request *req;
1204
 
        int el_ret;
1205
 
        unsigned int bytes = bio->bi_size;
1206
 
        const unsigned short prio = bio_prio(bio);
1207
 
        const bool sync = !!(bio->bi_rw & REQ_SYNC);
1208
 
        const bool unplug = !!(bio->bi_rw & REQ_UNPLUG);
1209
 
        const unsigned long ff = bio->bi_rw & REQ_FAILFAST_MASK;
1210
 
        int where = ELEVATOR_INSERT_SORT;
1211
 
        int rw_flags;
1212
1219
 
1213
1220
        /*
1214
1221
         * low level driver can indicate that it wants pages above a
1217
1224
         */
1218
1225
        blk_queue_bounce(q, &bio);
1219
1226
 
1220
 
        spin_lock_irq(q->queue_lock);
1221
 
 
1222
1227
        if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
1223
 
                where = ELEVATOR_INSERT_FRONT;
 
1228
                spin_lock_irq(q->queue_lock);
 
1229
                where = ELEVATOR_INSERT_FLUSH;
1224
1230
                goto get_rq;
1225
1231
        }
1226
1232
 
1227
 
        if (elv_queue_empty(q))
1228
 
                goto get_rq;
 
1233
        /*
 
1234
         * Check if we can merge with the plugged list before grabbing
 
1235
         * any locks.
 
1236
         */
 
1237
        if (attempt_plug_merge(current, q, bio))
 
1238
                goto out;
 
1239
 
 
1240
        spin_lock_irq(q->queue_lock);
1229
1241
 
1230
1242
        el_ret = elv_merge(q, &req, bio);
1231
 
        switch (el_ret) {
1232
 
        case ELEVATOR_BACK_MERGE:
1233
 
                BUG_ON(!rq_mergeable(req));
1234
 
 
1235
 
                if (!ll_back_merge_fn(q, req, bio))
1236
 
                        break;
1237
 
 
1238
 
                trace_block_bio_backmerge(q, bio);
1239
 
 
1240
 
                if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1241
 
                        blk_rq_set_mixed_merge(req);
1242
 
 
1243
 
                req->biotail->bi_next = bio;
1244
 
                req->biotail = bio;
1245
 
                req->__data_len += bytes;
1246
 
                req->ioprio = ioprio_best(req->ioprio, prio);
1247
 
                if (!blk_rq_cpu_valid(req))
1248
 
                        req->cpu = bio->bi_comp_cpu;
1249
 
                drive_stat_acct(req, 0);
1250
 
                elv_bio_merged(q, req, bio);
1251
 
                if (!attempt_back_merge(q, req))
1252
 
                        elv_merged_request(q, req, el_ret);
1253
 
                goto out;
1254
 
 
1255
 
        case ELEVATOR_FRONT_MERGE:
1256
 
                BUG_ON(!rq_mergeable(req));
1257
 
 
1258
 
                if (!ll_front_merge_fn(q, req, bio))
1259
 
                        break;
1260
 
 
1261
 
                trace_block_bio_frontmerge(q, bio);
1262
 
 
1263
 
                if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) {
1264
 
                        blk_rq_set_mixed_merge(req);
1265
 
                        req->cmd_flags &= ~REQ_FAILFAST_MASK;
1266
 
                        req->cmd_flags |= ff;
1267
 
                }
1268
 
 
1269
 
                bio->bi_next = req->bio;
1270
 
                req->bio = bio;
1271
 
 
1272
 
                /*
1273
 
                 * may not be valid. if the low level driver said
1274
 
                 * it didn't need a bounce buffer then it better
1275
 
                 * not touch req->buffer either...
1276
 
                 */
1277
 
                req->buffer = bio_data(bio);
1278
 
                req->__sector = bio->bi_sector;
1279
 
                req->__data_len += bytes;
1280
 
                req->ioprio = ioprio_best(req->ioprio, prio);
1281
 
                if (!blk_rq_cpu_valid(req))
1282
 
                        req->cpu = bio->bi_comp_cpu;
1283
 
                drive_stat_acct(req, 0);
1284
 
                elv_bio_merged(q, req, bio);
1285
 
                if (!attempt_front_merge(q, req))
1286
 
                        elv_merged_request(q, req, el_ret);
1287
 
                goto out;
1288
 
 
1289
 
        /* ELV_NO_MERGE: elevator says don't/can't merge. */
1290
 
        default:
1291
 
                ;
 
1243
        if (el_ret == ELEVATOR_BACK_MERGE) {
 
1244
                if (bio_attempt_back_merge(q, req, bio)) {
 
1245
                        if (!attempt_back_merge(q, req))
 
1246
                                elv_merged_request(q, req, el_ret);
 
1247
                        goto out_unlock;
 
1248
                }
 
1249
        } else if (el_ret == ELEVATOR_FRONT_MERGE) {
 
1250
                if (bio_attempt_front_merge(q, req, bio)) {
 
1251
                        if (!attempt_front_merge(q, req))
 
1252
                                elv_merged_request(q, req, el_ret);
 
1253
                        goto out_unlock;
 
1254
                }
1292
1255
        }
1293
1256
 
1294
1257
get_rq:
1315
1278
         */
1316
1279
        init_request_from_bio(req, bio);
1317
1280
 
1318
 
        spin_lock_irq(q->queue_lock);
1319
1281
        if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
1320
 
            bio_flagged(bio, BIO_CPU_AFFINE))
1321
 
                req->cpu = blk_cpu_to_group(smp_processor_id());
1322
 
        if (queue_should_plug(q) && elv_queue_empty(q))
1323
 
                blk_plug_device(q);
1324
 
 
1325
 
        /* insert the request into the elevator */
1326
 
        drive_stat_acct(req, 1);
1327
 
        __elv_add_request(q, req, where, 0);
 
1282
            bio_flagged(bio, BIO_CPU_AFFINE)) {
 
1283
                req->cpu = blk_cpu_to_group(get_cpu());
 
1284
                put_cpu();
 
1285
        }
 
1286
 
 
1287
        plug = current->plug;
 
1288
        if (plug) {
 
1289
                /*
 
1290
                 * If this is the first request added after a plug, fire
 
1291
                 * of a plug trace. If others have been added before, check
 
1292
                 * if we have multiple devices in this plug. If so, make a
 
1293
                 * note to sort the list before dispatch.
 
1294
                 */
 
1295
                if (list_empty(&plug->list))
 
1296
                        trace_block_plug(q);
 
1297
                else if (!plug->should_sort) {
 
1298
                        struct request *__rq;
 
1299
 
 
1300
                        __rq = list_entry_rq(plug->list.prev);
 
1301
                        if (__rq->q != q)
 
1302
                                plug->should_sort = 1;
 
1303
                }
 
1304
                list_add_tail(&req->queuelist, &plug->list);
 
1305
                drive_stat_acct(req, 1);
 
1306
        } else {
 
1307
                spin_lock_irq(q->queue_lock);
 
1308
                add_acct_request(q, req, where);
 
1309
                __blk_run_queue(q);
 
1310
out_unlock:
 
1311
                spin_unlock_irq(q->queue_lock);
 
1312
        }
1328
1313
out:
1329
 
        if (unplug || !queue_should_plug(q))
1330
 
                __generic_unplug_device(q);
1331
 
        spin_unlock_irq(q->queue_lock);
1332
1314
        return 0;
1333
1315
}
1334
1316
 
1545
1527
                        goto end_io;
1546
1528
                }
1547
1529
 
1548
 
                blk_throtl_bio(q, &bio);
 
1530
                if (blk_throtl_bio(q, &bio))
 
1531
                        goto end_io;
1549
1532
 
1550
1533
                /*
1551
1534
                 * If bio = NULL, bio has been throttled and will be submitted
1731
1714
         */
1732
1715
        BUG_ON(blk_queued_rq(rq));
1733
1716
 
1734
 
        drive_stat_acct(rq, 1);
1735
 
        __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1736
 
 
 
1717
        add_acct_request(q, rq, ELEVATOR_INSERT_BACK);
1737
1718
        spin_unlock_irqrestore(q->queue_lock, flags);
1738
1719
 
1739
1720
        return 0;
1805
1786
         * normal IO on queueing nor completion.  Accounting the
1806
1787
         * containing request is enough.
1807
1788
         */
1808
 
        if (blk_do_io_stat(req) && req != &req->q->flush_rq) {
 
1789
        if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
1809
1790
                unsigned long duration = jiffies - req->start_time;
1810
1791
                const int rw = rq_data_dir(req);
1811
1792
                struct hd_struct *part;
2045
2026
 
2046
2027
        if (error && req->cmd_type == REQ_TYPE_FS &&
2047
2028
            !(req->cmd_flags & REQ_QUIET)) {
2048
 
                printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
2049
 
                                req->rq_disk ? req->rq_disk->disk_name : "?",
2050
 
                                (unsigned long long)blk_rq_pos(req));
 
2029
                char *error_type;
 
2030
 
 
2031
                switch (error) {
 
2032
                case -ENOLINK:
 
2033
                        error_type = "recoverable transport";
 
2034
                        break;
 
2035
                case -EREMOTEIO:
 
2036
                        error_type = "critical target";
 
2037
                        break;
 
2038
                case -EBADE:
 
2039
                        error_type = "critical nexus";
 
2040
                        break;
 
2041
                case -EIO:
 
2042
                default:
 
2043
                        error_type = "I/O";
 
2044
                        break;
 
2045
                }
 
2046
                printk(KERN_ERR "end_request: %s error, dev %s, sector %llu\n",
 
2047
                       error_type, req->rq_disk ? req->rq_disk->disk_name : "?",
 
2048
                       (unsigned long long)blk_rq_pos(req));
2051
2049
        }
2052
2050
 
2053
2051
        blk_account_io_completion(req, nr_bytes);
2145
2143
         * size, something has gone terribly wrong.
2146
2144
         */
2147
2145
        if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2148
 
                printk(KERN_ERR "blk: request botched\n");
 
2146
                blk_dump_rq_flags(req, "request botched");
2149
2147
                req->__data_len = blk_rq_cur_bytes(req);
2150
2148
        }
2151
2149
 
2611
2609
}
2612
2610
EXPORT_SYMBOL(kblockd_schedule_work);
2613
2611
 
 
2612
int kblockd_schedule_delayed_work(struct request_queue *q,
 
2613
                        struct delayed_work *dwork, unsigned long delay)
 
2614
{
 
2615
        return queue_delayed_work(kblockd_workqueue, dwork, delay);
 
2616
}
 
2617
EXPORT_SYMBOL(kblockd_schedule_delayed_work);
 
2618
 
 
2619
#define PLUG_MAGIC      0x91827364
 
2620
 
 
2621
void blk_start_plug(struct blk_plug *plug)
 
2622
{
 
2623
        struct task_struct *tsk = current;
 
2624
 
 
2625
        plug->magic = PLUG_MAGIC;
 
2626
        INIT_LIST_HEAD(&plug->list);
 
2627
        INIT_LIST_HEAD(&plug->cb_list);
 
2628
        plug->should_sort = 0;
 
2629
 
 
2630
        /*
 
2631
         * If this is a nested plug, don't actually assign it. It will be
 
2632
         * flushed on its own.
 
2633
         */
 
2634
        if (!tsk->plug) {
 
2635
                /*
 
2636
                 * Store ordering should not be needed here, since a potential
 
2637
                 * preempt will imply a full memory barrier
 
2638
                 */
 
2639
                tsk->plug = plug;
 
2640
        }
 
2641
}
 
2642
EXPORT_SYMBOL(blk_start_plug);
 
2643
 
 
2644
static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
 
2645
{
 
2646
        struct request *rqa = container_of(a, struct request, queuelist);
 
2647
        struct request *rqb = container_of(b, struct request, queuelist);
 
2648
 
 
2649
        return !(rqa->q <= rqb->q);
 
2650
}
 
2651
 
 
2652
/*
 
2653
 * If 'from_schedule' is true, then postpone the dispatch of requests
 
2654
 * until a safe kblockd context. We due this to avoid accidental big
 
2655
 * additional stack usage in driver dispatch, in places where the originally
 
2656
 * plugger did not intend it.
 
2657
 */
 
2658
static void queue_unplugged(struct request_queue *q, unsigned int depth,
 
2659
                            bool from_schedule)
 
2660
        __releases(q->queue_lock)
 
2661
{
 
2662
        trace_block_unplug(q, depth, !from_schedule);
 
2663
 
 
2664
        /*
 
2665
         * If we are punting this to kblockd, then we can safely drop
 
2666
         * the queue_lock before waking kblockd (which needs to take
 
2667
         * this lock).
 
2668
         */
 
2669
        if (from_schedule) {
 
2670
                spin_unlock(q->queue_lock);
 
2671
                blk_run_queue_async(q);
 
2672
        } else {
 
2673
                __blk_run_queue(q);
 
2674
                spin_unlock(q->queue_lock);
 
2675
        }
 
2676
 
 
2677
}
 
2678
 
 
2679
static void flush_plug_callbacks(struct blk_plug *plug)
 
2680
{
 
2681
        LIST_HEAD(callbacks);
 
2682
 
 
2683
        if (list_empty(&plug->cb_list))
 
2684
                return;
 
2685
 
 
2686
        list_splice_init(&plug->cb_list, &callbacks);
 
2687
 
 
2688
        while (!list_empty(&callbacks)) {
 
2689
                struct blk_plug_cb *cb = list_first_entry(&callbacks,
 
2690
                                                          struct blk_plug_cb,
 
2691
                                                          list);
 
2692
                list_del(&cb->list);
 
2693
                cb->callback(cb);
 
2694
        }
 
2695
}
 
2696
 
 
2697
void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
 
2698
{
 
2699
        struct request_queue *q;
 
2700
        unsigned long flags;
 
2701
        struct request *rq;
 
2702
        LIST_HEAD(list);
 
2703
        unsigned int depth;
 
2704
 
 
2705
        BUG_ON(plug->magic != PLUG_MAGIC);
 
2706
 
 
2707
        flush_plug_callbacks(plug);
 
2708
        if (list_empty(&plug->list))
 
2709
                return;
 
2710
 
 
2711
        list_splice_init(&plug->list, &list);
 
2712
 
 
2713
        if (plug->should_sort) {
 
2714
                list_sort(NULL, &list, plug_rq_cmp);
 
2715
                plug->should_sort = 0;
 
2716
        }
 
2717
 
 
2718
        q = NULL;
 
2719
        depth = 0;
 
2720
 
 
2721
        /*
 
2722
         * Save and disable interrupts here, to avoid doing it for every
 
2723
         * queue lock we have to take.
 
2724
         */
 
2725
        local_irq_save(flags);
 
2726
        while (!list_empty(&list)) {
 
2727
                rq = list_entry_rq(list.next);
 
2728
                list_del_init(&rq->queuelist);
 
2729
                BUG_ON(!rq->q);
 
2730
                if (rq->q != q) {
 
2731
                        /*
 
2732
                         * This drops the queue lock
 
2733
                         */
 
2734
                        if (q)
 
2735
                                queue_unplugged(q, depth, from_schedule);
 
2736
                        q = rq->q;
 
2737
                        depth = 0;
 
2738
                        spin_lock(q->queue_lock);
 
2739
                }
 
2740
                /*
 
2741
                 * rq is already accounted, so use raw insert
 
2742
                 */
 
2743
                if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA))
 
2744
                        __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
 
2745
                else
 
2746
                        __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
 
2747
 
 
2748
                depth++;
 
2749
        }
 
2750
 
 
2751
        /*
 
2752
         * This drops the queue lock
 
2753
         */
 
2754
        if (q)
 
2755
                queue_unplugged(q, depth, from_schedule);
 
2756
 
 
2757
        local_irq_restore(flags);
 
2758
}
 
2759
 
 
2760
void blk_finish_plug(struct blk_plug *plug)
 
2761
{
 
2762
        blk_flush_plug_list(plug, false);
 
2763
 
 
2764
        if (plug == current->plug)
 
2765
                current->plug = NULL;
 
2766
}
 
2767
EXPORT_SYMBOL(blk_finish_plug);
 
2768
 
2614
2769
int __init blk_dev_init(void)
2615
2770
{
2616
2771
        BUILD_BUG_ON(__REQ_NR_BITS > 8 *