1/*
2 * Functions related to segment and merge handling
3 */
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/bio.h>
7#include <linux/blkdev.h>
8#include <linux/scatterlist.h>
9
10#include "blk.h"
11
12static unsigned int __blk_recalc_rq_segments(struct request_queue *q,
13					     struct bio *bio)
14{
15	struct bio_vec *bv, *bvprv = NULL;
16	int cluster, i, high, highprv = 1;
17	unsigned int seg_size, nr_phys_segs;
18	struct bio *fbio, *bbio;
19
20	if (!bio)
21		return 0;
22
23	fbio = bio;
24	cluster = blk_queue_cluster(q);
25	seg_size = 0;
26	nr_phys_segs = 0;
27	for_each_bio(bio) {
28		bio_for_each_segment(bv, bio, i) {
29			/*
30			 * the trick here is making sure that a high page is
31			 * never considered part of another segment, since that
32			 * might change with the bounce page.
33			 */
34			high = page_to_pfn(bv->bv_page) > queue_bounce_pfn(q);
35			if (high || highprv)
36				goto new_segment;
37			if (cluster) {
38				if (seg_size + bv->bv_len
39				    > queue_max_segment_size(q))
40					goto new_segment;
41				if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
42					goto new_segment;
43				if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
44					goto new_segment;
45
46				seg_size += bv->bv_len;
47				bvprv = bv;
48				continue;
49			}
50new_segment:
51			if (nr_phys_segs == 1 && seg_size >
52			    fbio->bi_seg_front_size)
53				fbio->bi_seg_front_size = seg_size;
54
55			nr_phys_segs++;
56			bvprv = bv;
57			seg_size = bv->bv_len;
58			highprv = high;
59		}
60		bbio = bio;
61	}
62
63	if (nr_phys_segs == 1 && seg_size > fbio->bi_seg_front_size)
64		fbio->bi_seg_front_size = seg_size;
65	if (seg_size > bbio->bi_seg_back_size)
66		bbio->bi_seg_back_size = seg_size;
67
68	return nr_phys_segs;
69}
70
71void blk_recalc_rq_segments(struct request *rq)
72{
73	rq->nr_phys_segments = __blk_recalc_rq_segments(rq->q, rq->bio);
74}
75
76void blk_recount_segments(struct request_queue *q, struct bio *bio)
77{
78	struct bio *nxt = bio->bi_next;
79
80	bio->bi_next = NULL;
81	bio->bi_phys_segments = __blk_recalc_rq_segments(q, bio);
82	bio->bi_next = nxt;
83	bio->bi_flags |= (1 << BIO_SEG_VALID);
84}
85EXPORT_SYMBOL(blk_recount_segments);
86
87static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
88				   struct bio *nxt)
89{
90	if (!blk_queue_cluster(q))
91		return 0;
92
93	if (bio->bi_seg_back_size + nxt->bi_seg_front_size >
94	    queue_max_segment_size(q))
95		return 0;
96
97	if (!bio_has_data(bio))
98		return 1;
99
100	if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
101		return 0;
102
103	/*
104	 * bio and nxt are contiguous in memory; check if the queue allows
105	 * these two to be merged into one
106	 */
107	if (BIO_SEG_BOUNDARY(q, bio, nxt))
108		return 1;
109
110	return 0;
111}
112
113/*
114 * map a request to scatterlist, return number of sg entries setup. Caller
115 * must make sure sg can hold rq->nr_phys_segments entries
116 */
117int blk_rq_map_sg(struct request_queue *q, struct request *rq,
118		  struct scatterlist *sglist)
119{
120	struct bio_vec *bvec, *bvprv;
121	struct req_iterator iter;
122	struct scatterlist *sg;
123	int nsegs, cluster;
124
125	nsegs = 0;
126	cluster = blk_queue_cluster(q);
127
128	/*
129	 * for each bio in rq
130	 */
131	bvprv = NULL;
132	sg = NULL;
133	rq_for_each_segment(bvec, rq, iter) {
134		int nbytes = bvec->bv_len;
135
136		if (bvprv && cluster) {
137			if (sg->length + nbytes > queue_max_segment_size(q))
138				goto new_segment;
139
140			if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
141				goto new_segment;
142			if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
143				goto new_segment;
144
145			sg->length += nbytes;
146		} else {
147new_segment:
148			if (!sg)
149				sg = sglist;
150			else {
151				/*
152				 * If the driver previously mapped a shorter
153				 * list, we could see a termination bit
154				 * prematurely unless it fully inits the sg
155				 * table on each mapping. We KNOW that there
156				 * must be more entries here or the driver
157				 * would be buggy, so force clear the
158				 * termination bit to avoid doing a full
159				 * sg_init_table() in drivers for each command.
160				 */
161				sg->page_link &= ~0x02;
162				sg = sg_next(sg);
163			}
164
165			sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
166			nsegs++;
167		}
168		bvprv = bvec;
169	} /* segments in rq */
170
171
172	if (unlikely(rq->cmd_flags & REQ_COPY_USER) &&
173	    (blk_rq_bytes(rq) & q->dma_pad_mask)) {
174		unsigned int pad_len =
175			(q->dma_pad_mask & ~blk_rq_bytes(rq)) + 1;
176
177		sg->length += pad_len;
178		rq->extra_len += pad_len;
179	}
180
181	if (q->dma_drain_size && q->dma_drain_needed(rq)) {
182		if (rq->cmd_flags & REQ_WRITE)
183			memset(q->dma_drain_buffer, 0, q->dma_drain_size);
184
185		sg->page_link &= ~0x02;
186		sg = sg_next(sg);
187		sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
188			    q->dma_drain_size,
189			    ((unsigned long)q->dma_drain_buffer) &
190			    (PAGE_SIZE - 1));
191		nsegs++;
192		rq->extra_len += q->dma_drain_size;
193	}
194
195	if (sg)
196		sg_mark_end(sg);
197
198	return nsegs;
199}
200EXPORT_SYMBOL(blk_rq_map_sg);
201
202static inline int ll_new_hw_segment(struct request_queue *q,
203				    struct request *req,
204				    struct bio *bio)
205{
206	int nr_phys_segs = bio_phys_segments(q, bio);
207
208	if (req->nr_phys_segments + nr_phys_segs > queue_max_segments(q)) {
209		req->cmd_flags |= REQ_NOMERGE;
210		if (req == q->last_merge)
211			q->last_merge = NULL;
212		return 0;
213	}
214
215	/*
216	 * This will form the start of a new hw segment.  Bump both
217	 * counters.
218	 */
219	req->nr_phys_segments += nr_phys_segs;
220	return 1;
221}
222
223int ll_back_merge_fn(struct request_queue *q, struct request *req,
224		     struct bio *bio)
225{
226	unsigned short max_sectors;
227
228	if (unlikely(req->cmd_type == REQ_TYPE_BLOCK_PC))
229		max_sectors = queue_max_hw_sectors(q);
230	else
231		max_sectors = queue_max_sectors(q);
232
233	if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
234		req->cmd_flags |= REQ_NOMERGE;
235		if (req == q->last_merge)
236			q->last_merge = NULL;
237		return 0;
238	}
239	if (!bio_flagged(req->biotail, BIO_SEG_VALID))
240		blk_recount_segments(q, req->biotail);
241	if (!bio_flagged(bio, BIO_SEG_VALID))
242		blk_recount_segments(q, bio);
243
244	return ll_new_hw_segment(q, req, bio);
245}
246
247int ll_front_merge_fn(struct request_queue *q, struct request *req,
248		      struct bio *bio)
249{
250	unsigned short max_sectors;
251
252	if (unlikely(req->cmd_type == REQ_TYPE_BLOCK_PC))
253		max_sectors = queue_max_hw_sectors(q);
254	else
255		max_sectors = queue_max_sectors(q);
256
257
258	if (blk_rq_sectors(req) + bio_sectors(bio) > max_sectors) {
259		req->cmd_flags |= REQ_NOMERGE;
260		if (req == q->last_merge)
261			q->last_merge = NULL;
262		return 0;
263	}
264	if (!bio_flagged(bio, BIO_SEG_VALID))
265		blk_recount_segments(q, bio);
266	if (!bio_flagged(req->bio, BIO_SEG_VALID))
267		blk_recount_segments(q, req->bio);
268
269	return ll_new_hw_segment(q, req, bio);
270}
271
272static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
273				struct request *next)
274{
275	int total_phys_segments;
276	unsigned int seg_size =
277		req->biotail->bi_seg_back_size + next->bio->bi_seg_front_size;
278
279	/*
280	 * First check if the either of the requests are re-queued
281	 * requests.  Can't merge them if they are.
282	 */
283	if (req->special || next->special)
284		return 0;
285
286	/*
287	 * Will it become too large?
288	 */
289	if ((blk_rq_sectors(req) + blk_rq_sectors(next)) > queue_max_sectors(q))
290		return 0;
291
292	total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
293	if (blk_phys_contig_segment(q, req->biotail, next->bio)) {
294		if (req->nr_phys_segments == 1)
295			req->bio->bi_seg_front_size = seg_size;
296		if (next->nr_phys_segments == 1)
297			next->biotail->bi_seg_back_size = seg_size;
298		total_phys_segments--;
299	}
300
301	if (total_phys_segments > queue_max_segments(q))
302		return 0;
303
304	/* Merge is OK... */
305	req->nr_phys_segments = total_phys_segments;
306	return 1;
307}
308
309/**
310 * blk_rq_set_mixed_merge - mark a request as mixed merge
311 * @rq: request to mark as mixed merge
312 *
313 * Description:
314 *     @rq is about to be mixed merged.  Make sure the attributes
315 *     which can be mixed are set in each bio and mark @rq as mixed
316 *     merged.
317 */
318void blk_rq_set_mixed_merge(struct request *rq)
319{
320	unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
321	struct bio *bio;
322
323	if (rq->cmd_flags & REQ_MIXED_MERGE)
324		return;
325
326	/*
327	 * @rq will no longer represent mixable attributes for all the
328	 * contained bios.  It will just track those of the first one.
329	 * Distributes the attributs to each bio.
330	 */
331	for (bio = rq->bio; bio; bio = bio->bi_next) {
332		WARN_ON_ONCE((bio->bi_rw & REQ_FAILFAST_MASK) &&
333			     (bio->bi_rw & REQ_FAILFAST_MASK) != ff);
334		bio->bi_rw |= ff;
335	}
336	rq->cmd_flags |= REQ_MIXED_MERGE;
337}
338
339static void blk_account_io_merge(struct request *req)
340{
341	if (blk_do_io_stat(req)) {
342		struct hd_struct *part;
343		int cpu;
344
345		cpu = part_stat_lock();
346		part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
347
348		part_round_stats(cpu, part);
349		part_dec_in_flight(part, rq_data_dir(req));
350
351		part_stat_unlock();
352	}
353}
354
355/*
356 * Has to be called with the request spinlock acquired
357 */
358static int attempt_merge(struct request_queue *q, struct request *req,
359			  struct request *next)
360{
361	if (!rq_mergeable(req) || !rq_mergeable(next))
362		return 0;
363
364	/*
365	 * Don't merge file system requests and discard requests
366	 */
367	if ((req->cmd_flags & REQ_DISCARD) != (next->cmd_flags & REQ_DISCARD))
368		return 0;
369
370	/*
371	 * Don't merge discard requests and secure discard requests
372	 */
373	if ((req->cmd_flags & REQ_SECURE) != (next->cmd_flags & REQ_SECURE))
374		return 0;
375
376	/*
377	 * not contiguous
378	 */
379	if (blk_rq_pos(req) + blk_rq_sectors(req) != blk_rq_pos(next))
380		return 0;
381
382	if (rq_data_dir(req) != rq_data_dir(next)
383	    || req->rq_disk != next->rq_disk
384	    || next->special)
385		return 0;
386
387	if (blk_integrity_rq(req) != blk_integrity_rq(next))
388		return 0;
389
390	/*
391	 * If we are allowed to merge, then append bio list
392	 * from next to rq and release next. merge_requests_fn
393	 * will have updated segment counts, update sector
394	 * counts here.
395	 */
396	if (!ll_merge_requests_fn(q, req, next))
397		return 0;
398
399	/*
400	 * If failfast settings disagree or any of the two is already
401	 * a mixed merge, mark both as mixed before proceeding.  This
402	 * makes sure that all involved bios have mixable attributes
403	 * set properly.
404	 */
405	if ((req->cmd_flags | next->cmd_flags) & REQ_MIXED_MERGE ||
406	    (req->cmd_flags & REQ_FAILFAST_MASK) !=
407	    (next->cmd_flags & REQ_FAILFAST_MASK)) {
408		blk_rq_set_mixed_merge(req);
409		blk_rq_set_mixed_merge(next);
410	}
411
412	/*
413	 * At this point we have either done a back merge
414	 * or front merge. We need the smaller start_time of
415	 * the merged requests to be the current request
416	 * for accounting purposes.
417	 */
418	if (time_after(req->start_time, next->start_time))
419		req->start_time = next->start_time;
420
421	req->biotail->bi_next = next->bio;
422	req->biotail = next->biotail;
423
424	req->__data_len += blk_rq_bytes(next);
425
426	elv_merge_requests(q, req, next);
427
428	/*
429	 * 'next' is going away, so update stats accordingly
430	 */
431	blk_account_io_merge(next);
432
433	req->ioprio = ioprio_best(req->ioprio, next->ioprio);
434	if (blk_rq_cpu_valid(next))
435		req->cpu = next->cpu;
436
437	/* owner-ship of bio passed from next to req */
438	next->bio = NULL;
439	__blk_put_request(q, next);
440	return 1;
441}
442
443int attempt_back_merge(struct request_queue *q, struct request *rq)
444{
445	struct request *next = elv_latter_request(q, rq);
446
447	if (next)
448		return attempt_merge(q, rq, next);
449
450	return 0;
451}
452
453int attempt_front_merge(struct request_queue *q, struct request *rq)
454{
455	struct request *prev = elv_former_request(q, rq);
456
457	if (prev)
458		return attempt_merge(q, prev, rq);
459
460	return 0;
461}
462