Back to home page

LXR

 
 

    


0001 /*
0002  * Functions related to segment and merge handling
0003  */
0004 #include <linux/kernel.h>
0005 #include <linux/module.h>
0006 #include <linux/bio.h>
0007 #include <linux/blkdev.h>
0008 #include <linux/scatterlist.h>
0009 
0010 #include <trace/events/block.h>
0011 
0012 #include "blk.h"
0013 
0014 static struct bio *blk_bio_discard_split(struct request_queue *q,
0015                      struct bio *bio,
0016                      struct bio_set *bs,
0017                      unsigned *nsegs)
0018 {
0019     unsigned int max_discard_sectors, granularity;
0020     int alignment;
0021     sector_t tmp;
0022     unsigned split_sectors;
0023 
0024     *nsegs = 1;
0025 
0026     /* Zero-sector (unknown) and one-sector granularities are the same.  */
0027     granularity = max(q->limits.discard_granularity >> 9, 1U);
0028 
0029     max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
0030     max_discard_sectors -= max_discard_sectors % granularity;
0031 
0032     if (unlikely(!max_discard_sectors)) {
0033         /* XXX: warn */
0034         return NULL;
0035     }
0036 
0037     if (bio_sectors(bio) <= max_discard_sectors)
0038         return NULL;
0039 
0040     split_sectors = max_discard_sectors;
0041 
0042     /*
0043      * If the next starting sector would be misaligned, stop the discard at
0044      * the previous aligned sector.
0045      */
0046     alignment = (q->limits.discard_alignment >> 9) % granularity;
0047 
0048     tmp = bio->bi_iter.bi_sector + split_sectors - alignment;
0049     tmp = sector_div(tmp, granularity);
0050 
0051     if (split_sectors > tmp)
0052         split_sectors -= tmp;
0053 
0054     return bio_split(bio, split_sectors, GFP_NOIO, bs);
0055 }
0056 
0057 static struct bio *blk_bio_write_same_split(struct request_queue *q,
0058                         struct bio *bio,
0059                         struct bio_set *bs,
0060                         unsigned *nsegs)
0061 {
0062     *nsegs = 1;
0063 
0064     if (!q->limits.max_write_same_sectors)
0065         return NULL;
0066 
0067     if (bio_sectors(bio) <= q->limits.max_write_same_sectors)
0068         return NULL;
0069 
0070     return bio_split(bio, q->limits.max_write_same_sectors, GFP_NOIO, bs);
0071 }
0072 
0073 static inline unsigned get_max_io_size(struct request_queue *q,
0074                        struct bio *bio)
0075 {
0076     unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector);
0077     unsigned mask = queue_logical_block_size(q) - 1;
0078 
0079     /* aligned to logical block size */
0080     sectors &= ~(mask >> 9);
0081 
0082     return sectors;
0083 }
0084 
0085 static struct bio *blk_bio_segment_split(struct request_queue *q,
0086                      struct bio *bio,
0087                      struct bio_set *bs,
0088                      unsigned *segs)
0089 {
0090     struct bio_vec bv, bvprv, *bvprvp = NULL;
0091     struct bvec_iter iter;
0092     unsigned seg_size = 0, nsegs = 0, sectors = 0;
0093     unsigned front_seg_size = bio->bi_seg_front_size;
0094     bool do_split = true;
0095     struct bio *new = NULL;
0096     const unsigned max_sectors = get_max_io_size(q, bio);
0097     unsigned bvecs = 0;
0098 
0099     bio_for_each_segment(bv, bio, iter) {
0100         /*
0101          * With arbitrary bio size, the incoming bio may be very
0102          * big. We have to split the bio into small bios so that
0103          * each holds at most BIO_MAX_PAGES bvecs because
0104          * bio_clone() can fail to allocate big bvecs.
0105          *
0106          * It should have been better to apply the limit per
0107          * request queue in which bio_clone() is involved,
0108          * instead of globally. The biggest blocker is the
0109          * bio_clone() in bio bounce.
0110          *
0111          * If bio is splitted by this reason, we should have
0112          * allowed to continue bios merging, but don't do
0113          * that now for making the change simple.
0114          *
0115          * TODO: deal with bio bounce's bio_clone() gracefully
0116          * and convert the global limit into per-queue limit.
0117          */
0118         if (bvecs++ >= BIO_MAX_PAGES)
0119             goto split;
0120 
0121         /*
0122          * If the queue doesn't support SG gaps and adding this
0123          * offset would create a gap, disallow it.
0124          */
0125         if (bvprvp && bvec_gap_to_prev(q, bvprvp, bv.bv_offset))
0126             goto split;
0127 
0128         if (sectors + (bv.bv_len >> 9) > max_sectors) {
0129             /*
0130              * Consider this a new segment if we're splitting in
0131              * the middle of this vector.
0132              */
0133             if (nsegs < queue_max_segments(q) &&
0134                 sectors < max_sectors) {
0135                 nsegs++;
0136                 sectors = max_sectors;
0137             }
0138             if (sectors)
0139                 goto split;
0140             /* Make this single bvec as the 1st segment */
0141         }
0142 
0143         if (bvprvp && blk_queue_cluster(q)) {
0144             if (seg_size + bv.bv_len > queue_max_segment_size(q))
0145                 goto new_segment;
0146             if (!BIOVEC_PHYS_MERGEABLE(bvprvp, &bv))
0147                 goto new_segment;
0148             if (!BIOVEC_SEG_BOUNDARY(q, bvprvp, &bv))
0149                 goto new_segment;
0150 
0151             seg_size += bv.bv_len;
0152             bvprv = bv;
0153             bvprvp = &bvprv;
0154             sectors += bv.bv_len >> 9;
0155 
0156             if (nsegs == 1 && seg_size > front_seg_size)
0157                 front_seg_size = seg_size;
0158             continue;
0159         }
0160 new_segment:
0161         if (nsegs == queue_max_segments(q))
0162             goto split;
0163 
0164         nsegs++;
0165         bvprv = bv;
0166         bvprvp = &bvprv;
0167         seg_size = bv.bv_len;
0168         sectors += bv.bv_len >> 9;
0169 
0170         if (nsegs == 1 && seg_size > front_seg_size)
0171             front_seg_size = seg_size;
0172     }
0173 
0174     do_split = false;
0175 split:
0176     *segs = nsegs;
0177 
0178     if (do_split) {
0179         new = bio_split(bio, sectors, GFP_NOIO, bs);
0180         if (new)
0181             bio = new;
0182     }
0183 
0184     bio->bi_seg_front_size = front_seg_size;
0185     if (seg_size > bio->bi_seg_back_size)
0186         bio->bi_seg_back_size = seg_size;
0187 
0188     return do_split ? new : NULL;
0189 }
0190 
0191 void blk_queue_split(struct request_queue *q, struct bio **bio,
0192              struct bio_set *bs)
0193 {
0194     struct bio *split, *res;
0195     unsigned nsegs;
0196 
0197     switch (bio_op(*bio)) {
0198     case REQ_OP_DISCARD:
0199     case REQ_OP_SECURE_ERASE:
0200         split = blk_bio_discard_split(q, *bio, bs, &nsegs);
0201         break;
0202     case REQ_OP_WRITE_ZEROES:
0203         split = NULL;
0204         nsegs = (*bio)->bi_phys_segments;
0205         break;
0206     case REQ_OP_WRITE_SAME:
0207         split = blk_bio_write_same_split(q, *bio, bs, &nsegs);
0208         break;
0209     default:
0210         split = blk_bio_segment_split(q, *bio, q->bio_split, &nsegs);
0211         break;
0212     }
0213 
0214     /* physical segments can be figured out during splitting */
0215     res = split ? split : *bio;
0216     res->bi_phys_segments = nsegs;
0217     bio_set_flag(res, BIO_SEG_VALID);
0218 
0219     if (split) {
0220         /* there isn't chance to merge the splitted bio */
0221         split->bi_opf |= REQ_NOMERGE;
0222 
0223         bio_chain(split, *bio);
0224         trace_block_split(q, split, (*bio)->bi_iter.bi_sector);
0225         generic_make_request(*bio);
0226         *bio = split;
0227     }
0228 }
0229 EXPORT_SYMBOL(blk_queue_split);
0230 
0231 static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
0232                          struct bio *bio,
0233                          bool no_sg_merge)
0234 {
0235     struct bio_vec bv, bvprv = { NULL };
0236     int cluster, prev = 0;
0237     unsigned int seg_size, nr_phys_segs;
0238     struct bio *fbio, *bbio;
0239     struct bvec_iter iter;
0240 
0241     if (!bio)
0242         return 0;
0243 
0244     switch (bio_op(bio)) {
0245     case REQ_OP_DISCARD:
0246     case REQ_OP_SECURE_ERASE:
0247     case REQ_OP_WRITE_ZEROES:
0248         return 0;
0249     case REQ_OP_WRITE_SAME:
0250         return 1;
0251     }
0252 
0253     fbio = bio;
0254     cluster = blk_queue_cluster(q);
0255     seg_size = 0;
0256     nr_phys_segs = 0;
0257     for_each_bio(bio) {
0258         bio_for_each_segment(bv, bio, iter) {
0259             /*
0260              * If SG merging is disabled, each bio vector is
0261              * a segment
0262              */
0263             if (no_sg_merge)
0264                 goto new_segment;
0265 
0266             if (prev && cluster) {
0267                 if (seg_size + bv.bv_len
0268                     > queue_max_segment_size(q))
0269                     goto new_segment;
0270                 if (!BIOVEC_PHYS_MERGEABLE(&bvprv, &bv))
0271                     goto new_segment;
0272                 if (!BIOVEC_SEG_BOUNDARY(q, &bvprv, &bv))
0273                     goto new_segment;
0274 
0275                 seg_size += bv.bv_len;
0276                 bvprv = bv;
0277                 continue;
0278             }
0279 new_segment:
0280             if (nr_phys_segs == 1 && seg_size >
0281                 fbio->bi_seg_front_size)
0282                 fbio->bi_seg_front_size = seg_size;
0283 
0284             nr_phys_segs++;
0285             bvprv = bv;
0286             prev = 1;
0287             seg_size = bv.bv_len;
0288         }
0289         bbio = bio;
0290     }
0291 
0292     if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
0293         fbio->bi_seg_front_size = seg_size;
0294     if (seg_size > bbio->bi_seg_back_size)
0295         bbio->bi_seg_back_size = seg_size;
0296 
0297     return nr_phys_segs;
0298 }
0299 
0300 void blk_recalc_rq_segments(struct request *rq)
0301 {
0302     bool no_sg_merge = !!test_bit(QUEUE_FLAG_NO_SG_MERGE,
0303             &rq->q->queue_flags);
0304 
0305     rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio,
0306             no_sg_merge);
0307 }
0308 
0309 void blk_recount_segments(struct request_queue *q, struct bio *bio)
0310 {
0311     unsigned short seg_cnt;
0312 
0313     /* estimate segment number by bi_vcnt for non-cloned bio */
0314     if (bio_flagged(bio, BIO_CLONED))
0315         seg_cnt = bio_segments(bio);
0316     else
0317         seg_cnt = bio->bi_vcnt;
0318 
0319     if (test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags) &&
0320             (seg_cnt < queue_max_segments(q)))
0321         bio->bi_phys_segments = seg_cnt;
0322     else {
0323         struct bio *nxt = bio->bi_next;
0324 
0325         bio->bi_next = NULL;
0326         bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio, false);
0327         bio->bi_next = nxt;
0328     }
0329 
0330     bio_set_flag(bio, BIO_SEG_VALID);
0331 }
0332 EXPORT_SYMBOL(blk_recount_segments);
0333 
0334 static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
0335                    struct bio *nxt)
0336 {
0337     struct bio_vec end_bv = { NULL }, nxt_bv;
0338 
0339     if (!blk_queue_cluster(q))
0340         return 0;
0341 
0342     if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
0343         queue_max_segment_size(q))
0344         return 0;
0345 
0346     if (!bio_has_data(bio))
0347         return 1;
0348 
0349     bio_get_last_bvec(bio, &end_bv);
0350     bio_get_first_bvec(nxt, &nxt_bv);
0351 
0352     if (!BIOVEC_PHYS_MERGEABLE(&end_bv, &nxt_bv))
0353         return 0;
0354 
0355     /*
0356      * bio and nxt are contiguous in memory; check if the queue allows
0357      * these two to be merged into one
0358      */
0359     if (BIOVEC_SEG_BOUNDARY(q, &end_bv, &nxt_bv))
0360         return 1;
0361 
0362     return 0;
0363 }
0364 
0365 static inline void
0366 __blk_segment_map_sg(struct request_queue *q, struct bio_vec *bvec,
0367              struct scatterlist *sglist, struct bio_vec *bvprv,
0368              struct scatterlist **sg, int *nsegs, int *cluster)
0369 {
0370 
0371     int nbytes = bvec->bv_len;
0372 
0373     if (*sg && *cluster) {
0374         if ((*sg)->length + nbytes > queue_max_segment_size(q))
0375             goto new_segment;
0376 
0377         if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
0378             goto new_segment;
0379         if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
0380             goto new_segment;
0381 
0382         (*sg)->length += nbytes;
0383     } else {
0384 new_segment:
0385         if (!*sg)
0386             *sg = sglist;
0387         else {
0388             /*
0389              * If the driver previously mapped a shorter
0390              * list, we could see a termination bit
0391              * prematurely unless it fully inits the sg
0392              * table on each mapping. We KNOW that there
0393              * must be more entries here or the driver
0394              * would be buggy, so force clear the
0395              * termination bit to avoid doing a full
0396              * sg_init_table() in drivers for each command.
0397              */
0398             sg_unmark_end(*sg);
0399             *sg = sg_next(*sg);
0400         }
0401 
0402         sg_set_page(*sg, bvec->bv_page, nbytes, bvec->bv_offset);
0403         (*nsegs)++;
0404     }
0405     *bvprv = *bvec;
0406 }
0407 
0408 static inline int __blk_bvec_map_sg(struct request_queue *q, struct bio_vec bv,
0409         struct scatterlist *sglist, struct scatterlist **sg)
0410 {
0411     *sg = sglist;
0412     sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
0413     return 1;
0414 }
0415 
0416 static int __blk_bios_map_sg(struct request_queue *q, struct bio *bio,
0417                  struct scatterlist *sglist,
0418                  struct scatterlist **sg)
0419 {
0420     struct bio_vec bvec, bvprv = { NULL };
0421     struct bvec_iter iter;
0422     int cluster = blk_queue_cluster(q), nsegs = 0;
0423 
0424     for_each_bio(bio)
0425         bio_for_each_segment(bvec, bio, iter)
0426             __blk_segment_map_sg(q, &bvec, sglist, &bvprv, sg,
0427                          &nsegs, &cluster);
0428 
0429     return nsegs;
0430 }
0431 
0432 /*
0433  * map a request to scatterlist, return number of sg entries setup. Caller
0434  * must make sure sg can hold rq->nr_phys_segments entries
0435  */
0436 int blk_rq_map_sg(struct request_queue *q, struct request *rq,
0437           struct scatterlist *sglist)
0438 {
0439     struct scatterlist *sg = NULL;
0440     int nsegs = 0;
0441 
0442     if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
0443         nsegs = __blk_bvec_map_sg(q, rq->special_vec, sglist, &sg);
0444     else if (rq->bio && bio_op(rq->bio) == REQ_OP_WRITE_SAME)
0445         nsegs = __blk_bvec_map_sg(q, bio_iovec(rq->bio), sglist, &sg);
0446     else if (rq->bio)
0447         nsegs = __blk_bios_map_sg(q, rq->bio, sglist, &sg);
0448 
0449     if (unlikely(rq->rq_flags & RQF_COPY_USER) &&
0450         (blk_rq_bytes(rq) & q->dma_pad_mask)) {
0451         unsigned int pad_len =
0452             (q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
0453 
0454         sg->length += pad_len;
0455         rq->extra_len += pad_len;
0456     }
0457 
0458     if (q->dma_drain_size && q->dma_drain_needed(rq)) {
0459         if (op_is_write(req_op(rq)))
0460             memset(q->dma_drain_buffer, 0, q->dma_drain_size);
0461 
0462         sg_unmark_end(sg);
0463         sg = sg_next(sg);
0464         sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
0465                 q->dma_drain_size,
0466                 ((unsigned long)q->dma_drain_buffer) &
0467                 (PAGE_SIZE - 1));
0468         nsegs++;
0469         rq->extra_len += q->dma_drain_size;
0470     }
0471 
0472     if (sg)
0473         sg_mark_end(sg);
0474 
0475     /*
0476      * Something must have been wrong if the figured number of
0477      * segment is bigger than number of req's physical segments
0478      */
0479     WARN_ON(nsegs > blk_rq_nr_phys_segments(rq));
0480 
0481     return nsegs;
0482 }
0483 EXPORT_SYMBOL(blk_rq_map_sg);
0484 
0485 static void req_set_nomerge(struct request_queue *q, struct request *req)
0486 {
0487     req->cmd_flags |= REQ_NOMERGE;
0488     if (req == q->last_merge)
0489         q->last_merge = NULL;
0490 }
0491 
0492 static inline int ll_new_hw_segment(struct request_queue *q,
0493                     struct request *req,
0494                     struct bio *bio)
0495 {
0496     int nr_phys_segs = bio_phys_segments(q, bio);
0497 
0498     if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q))
0499         goto no_merge;
0500 
0501     if (blk_integrity_merge_bio(q, req, bio) == false)
0502         goto no_merge;
0503 
0504     /*
0505      * This will form the start of a new hw segment.  Bump both
0506      * counters.
0507      */
0508     req->nr_phys_segments += nr_phys_segs;
0509     return 1;
0510 
0511 no_merge:
0512     req_set_nomerge(q, req);
0513     return 0;
0514 }
0515 
0516 int ll_back_merge_fn(struct request_queue *q, struct request *req,
0517              struct bio *bio)
0518 {
0519     if (req_gap_back_merge(req, bio))
0520         return 0;
0521     if (blk_integrity_rq(req) &&
0522         integrity_req_gap_back_merge(req, bio))
0523         return 0;
0524     if (blk_rq_sectors(req) + bio_sectors(bio) >
0525         blk_rq_get_max_sectors(req, blk_rq_pos(req))) {
0526         req_set_nomerge(q, req);
0527         return 0;
0528     }
0529     if (!bio_flagged(req->biotail, BIO_SEG_VALID))
0530         blk_recount_segments(q, req->biotail);
0531     if (!bio_flagged(bio, BIO_SEG_VALID))
0532         blk_recount_segments(q, bio);
0533 
0534     return ll_new_hw_segment(q, req, bio);
0535 }
0536 
0537 int ll_front_merge_fn(struct request_queue *q, struct request *req,
0538               struct bio *bio)
0539 {
0540 
0541     if (req_gap_front_merge(req, bio))
0542         return 0;
0543     if (blk_integrity_rq(req) &&
0544         integrity_req_gap_front_merge(req, bio))
0545         return 0;
0546     if (blk_rq_sectors(req) + bio_sectors(bio) >
0547         blk_rq_get_max_sectors(req, bio->bi_iter.bi_sector)) {
0548         req_set_nomerge(q, req);
0549         return 0;
0550     }
0551     if (!bio_flagged(bio, BIO_SEG_VALID))
0552         blk_recount_segments(q, bio);
0553     if (!bio_flagged(req->bio, BIO_SEG_VALID))
0554         blk_recount_segments(q, req->bio);
0555 
0556     return ll_new_hw_segment(q, req, bio);
0557 }
0558 
0559 /*
0560  * blk-mq uses req->special to carry normal driver per-request payload, it
0561  * does not indicate a prepared command that we cannot merge with.
0562  */
0563 static bool req_no_special_merge(struct request *req)
0564 {
0565     struct request_queue *q = req->q;
0566 
0567     return !q->mq_ops && req->special;
0568 }
0569 
0570 static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
0571                 struct request *next)
0572 {
0573     int total_phys_segments;
0574     unsigned int seg_size =
0575         req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
0576 
0577     /*
0578      * First check if the either of the requests are re-queued
0579      * requests.  Can't merge them if they are.
0580      */
0581     if (req_no_special_merge(req) || req_no_special_merge(next))
0582         return 0;
0583 
0584     if (req_gap_back_merge(req, next->bio))
0585         return 0;
0586 
0587     /*
0588      * Will it become too large?
0589      */
0590     if ((blk_rq_sectors(req) + blk_rq_sectors(next)) >
0591         blk_rq_get_max_sectors(req, blk_rq_pos(req)))
0592         return 0;
0593 
0594     total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
0595     if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
0596         if (req->nr_phys_segments == 1)
0597             req->bio->bi_seg_front_size = seg_size;
0598         if (next->nr_phys_segments == 1)
0599             next->biotail->bi_seg_back_size = seg_size;
0600         total_phys_segments--;
0601     }
0602 
0603     if (total_phys_segments > queue_max_segments(q))
0604         return 0;
0605 
0606     if (blk_integrity_merge_rq(q, req, next) == false)
0607         return 0;
0608 
0609     /* Merge is OK... */
0610     req->nr_phys_segments = total_phys_segments;
0611     return 1;
0612 }
0613 
0614 /**
0615  * blk_rq_set_mixed_merge - mark a request as mixed merge
0616  * @rq: request to mark as mixed merge
0617  *
0618  * Description:
0619  *     @rq is about to be mixed merged.  Make sure the attributes
0620  *     which can be mixed are set in each bio and mark @rq as mixed
0621  *     merged.
0622  */
0623 void blk_rq_set_mixed_merge(struct request *rq)
0624 {
0625     unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
0626     struct bio *bio;
0627 
0628     if (rq->rq_flags & RQF_MIXED_MERGE)
0629         return;
0630 
0631     /*
0632      * @rq will no longer represent mixable attributes for all the
0633      * contained bios.  It will just track those of the first one.
0634      * Distributes the attributs to each bio.
0635      */
0636     for (bio = rq->bio; bio; bio = bio->bi_next) {
0637         WARN_ON_ONCE((bio->bi_opf & REQ_FAILFAST_MASK) &&
0638                  (bio->bi_opf & REQ_FAILFAST_MASK) != ff);
0639         bio->bi_opf |= ff;
0640     }
0641     rq->rq_flags |= RQF_MIXED_MERGE;
0642 }
0643 
0644 static void blk_account_io_merge(struct request *req)
0645 {
0646     if (blk_do_io_stat(req)) {
0647         struct hd_struct *part;
0648         int cpu;
0649 
0650         cpu = part_stat_lock();
0651         part = req->part;
0652 
0653         part_round_stats(cpu, part);
0654         part_dec_in_flight(part, rq_data_dir(req));
0655 
0656         hd_struct_put(part);
0657         part_stat_unlock();
0658     }
0659 }
0660 
0661 /*
0662  * Has to be called with the request spinlock acquired
0663  */
0664 static int attempt_merge(struct request_queue *q, struct request *req,
0665               struct request *next)
0666 {
0667     if (!rq_mergeable(req) || !rq_mergeable(next))
0668         return 0;
0669 
0670     if (req_op(req) != req_op(next))
0671         return 0;
0672 
0673     /*
0674      * not contiguous
0675      */
0676     if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
0677         return 0;
0678 
0679     if (rq_data_dir(req) != rq_data_dir(next)
0680         || req->rq_disk != next->rq_disk
0681         || req_no_special_merge(next))
0682         return 0;
0683 
0684     if (req_op(req) == REQ_OP_WRITE_SAME &&
0685         !blk_write_same_mergeable(req->bio, next->bio))
0686         return 0;
0687 
0688     /*
0689      * If we are allowed to merge, then append bio list
0690      * from next to rq and release next. merge_requests_fn
0691      * will have updated segment counts, update sector
0692      * counts here.
0693      */
0694     if (!ll_merge_requests_fn(q, req, next))
0695         return 0;
0696 
0697     /*
0698      * If failfast settings disagree or any of the two is already
0699      * a mixed merge, mark both as mixed before proceeding.  This
0700      * makes sure that all involved bios have mixable attributes
0701      * set properly.
0702      */
0703     if (((req->rq_flags | next->rq_flags) & RQF_MIXED_MERGE) ||
0704         (req->cmd_flags & REQ_FAILFAST_MASK) !=
0705         (next->cmd_flags & REQ_FAILFAST_MASK)) {
0706         blk_rq_set_mixed_merge(req);
0707         blk_rq_set_mixed_merge(next);
0708     }
0709 
0710     /*
0711      * At this point we have either done a back merge
0712      * or front merge. We need the smaller start_time of
0713      * the merged requests to be the current request
0714      * for accounting purposes.
0715      */
0716     if (time_after(req->start_time, next->start_time))
0717         req->start_time = next->start_time;
0718 
0719     req->biotail->bi_next = next->bio;
0720     req->biotail = next->biotail;
0721 
0722     req->__data_len += blk_rq_bytes(next);
0723 
0724     elv_merge_requests(q, req, next);
0725 
0726     /*
0727      * 'next' is going away, so update stats accordingly
0728      */
0729     blk_account_io_merge(next);
0730 
0731     req->ioprio = ioprio_best(req->ioprio, next->ioprio);
0732     if (blk_rq_cpu_valid(next))
0733         req->cpu = next->cpu;
0734 
0735     /* owner-ship of bio passed from next to req */
0736     next->bio = NULL;
0737     __blk_put_request(q, next);
0738     return 1;
0739 }
0740 
0741 int attempt_back_merge(struct request_queue *q, struct request *rq)
0742 {
0743     struct request *next = elv_latter_request(q, rq);
0744 
0745     if (next)
0746         return attempt_merge(q, rq, next);
0747 
0748     return 0;
0749 }
0750 
0751 int attempt_front_merge(struct request_queue *q, struct request *rq)
0752 {
0753     struct request *prev = elv_former_request(q, rq);
0754 
0755     if (prev)
0756         return attempt_merge(q, prev, rq);
0757 
0758     return 0;
0759 }
0760 
0761 int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
0762               struct request *next)
0763 {
0764     struct elevator_queue *e = q->elevator;
0765 
0766     if (e->type->ops.elevator_allow_rq_merge_fn)
0767         if (!e->type->ops.elevator_allow_rq_merge_fn(q, rq, next))
0768             return 0;
0769 
0770     return attempt_merge(q, rq, next);
0771 }
0772 
0773 bool blk_rq_merge_ok(struct request *rq, struct bio *bio)
0774 {
0775     if (!rq_mergeable(rq) || !bio_mergeable(bio))
0776         return false;
0777 
0778     if (req_op(rq) != bio_op(bio))
0779         return false;
0780 
0781     /* different data direction or already started, don't merge */
0782     if (bio_data_dir(bio) != rq_data_dir(rq))
0783         return false;
0784 
0785     /* must be same device and not a special request */
0786     if (rq->rq_disk != bio->bi_bdev->bd_disk || req_no_special_merge(rq))
0787         return false;
0788 
0789     /* only merge integrity protected bio into ditto rq */
0790     if (blk_integrity_merge_bio(rq->q, rq, bio) == false)
0791         return false;
0792 
0793     /* must be using the same buffer */
0794     if (req_op(rq) == REQ_OP_WRITE_SAME &&
0795         !blk_write_same_mergeable(rq->bio, bio))
0796         return false;
0797 
0798     return true;
0799 }
0800 
0801 int blk_try_merge(struct request *rq, struct bio *bio)
0802 {
0803     if (blk_rq_pos(rq) + blk_rq_sectors(rq) == bio->bi_iter.bi_sector)
0804         return ELEVATOR_BACK_MERGE;
0805     else if (blk_rq_pos(rq) - bio_sectors(bio) == bio->bi_iter.bi_sector)
0806         return ELEVATOR_FRONT_MERGE;
0807     return ELEVATOR_NO_MERGE;
0808 }