]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/bio.c
Better bio_alloc_bioset()
[bcachefs-tools-debian] / linux / bio.c
1 /*
2  * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public Licens
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
16  *
17  */
18 #include <linux/bio.h>
19 #include <linux/blkdev.h>
20 #include <linux/slab.h>
21 #include <linux/kernel.h>
22
23 static const struct {
24         int             err;
25         const char      *name;
26 } blk_errors[] = {
27         [BLK_STS_OK]            = { 0,          "" },
28         [BLK_STS_NOTSUPP]       = { -EOPNOTSUPP, "operation not supported" },
29         [BLK_STS_TIMEOUT]       = { -ETIMEDOUT, "timeout" },
30         [BLK_STS_NOSPC]         = { -ENOSPC,    "critical space allocation" },
31         [BLK_STS_TRANSPORT]     = { -ENOLINK,   "recoverable transport" },
32         [BLK_STS_TARGET]        = { -EREMOTEIO, "critical target" },
33         [BLK_STS_NEXUS]         = { -EBADE,     "critical nexus" },
34         [BLK_STS_MEDIUM]        = { -ENODATA,   "critical medium" },
35         [BLK_STS_PROTECTION]    = { -EILSEQ,    "protection" },
36         [BLK_STS_RESOURCE]      = { -ENOMEM,    "kernel resource" },
37         [BLK_STS_AGAIN]         = { -EAGAIN,    "nonblocking retry" },
38
39         /* device mapper special case, should not leak out: */
40         [BLK_STS_DM_REQUEUE]    = { -EREMCHG, "dm internal retry" },
41
42         /* everything else not covered above: */
43         [BLK_STS_IOERR]         = { -EIO,       "I/O" },
44 };
45
46 int blk_status_to_errno(blk_status_t status)
47 {
48         int idx = (__force int)status;
49
50         if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
51                 return -EIO;
52         return blk_errors[idx].err;
53 }
54
55 const char *blk_status_to_str(blk_status_t status)
56 {
57         int idx = (__force int)status;
58
59         if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
60                 return "(invalid error)";
61         return blk_errors[idx].name;
62 }
63
64 void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
65                         struct bio *src, struct bvec_iter *src_iter)
66 {
67         struct bio_vec src_bv, dst_bv;
68         void *src_p, *dst_p;
69         unsigned bytes;
70
71         while (src_iter->bi_size && dst_iter->bi_size) {
72                 src_bv = bio_iter_iovec(src, *src_iter);
73                 dst_bv = bio_iter_iovec(dst, *dst_iter);
74
75                 bytes = min(src_bv.bv_len, dst_bv.bv_len);
76
77                 src_p = kmap_atomic(src_bv.bv_page);
78                 dst_p = kmap_atomic(dst_bv.bv_page);
79
80                 memcpy(dst_p + dst_bv.bv_offset,
81                        src_p + src_bv.bv_offset,
82                        bytes);
83
84                 kunmap_atomic(dst_p);
85                 kunmap_atomic(src_p);
86
87                 flush_dcache_page(dst_bv.bv_page);
88
89                 bio_advance_iter(src, src_iter, bytes);
90                 bio_advance_iter(dst, dst_iter, bytes);
91         }
92 }
93
94 /**
95  * bio_copy_data - copy contents of data buffers from one bio to another
96  * @src: source bio
97  * @dst: destination bio
98  *
99  * Stops when it reaches the end of either @src or @dst - that is, copies
100  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
101  */
102 void bio_copy_data(struct bio *dst, struct bio *src)
103 {
104         struct bvec_iter src_iter = src->bi_iter;
105         struct bvec_iter dst_iter = dst->bi_iter;
106
107         bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
108 }
109
110 void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
111 {
112         unsigned long flags;
113         struct bio_vec bv;
114         struct bvec_iter iter;
115
116         __bio_for_each_segment(bv, bio, iter, start) {
117                 char *data = bvec_kmap_irq(&bv, &flags);
118                 memset(data, 0, bv.bv_len);
119                 bvec_kunmap_irq(data, &flags);
120         }
121 }
122
123 void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
124 {
125         /*
126          * most users will be overriding ->bi_bdev with a new target,
127          * so we don't set nor calculate new physical/hw segment counts here
128          */
129         bio->bi_bdev = bio_src->bi_bdev;
130         bio_set_flag(bio, BIO_CLONED);
131         bio->bi_opf = bio_src->bi_opf;
132         bio->bi_iter = bio_src->bi_iter;
133         bio->bi_io_vec = bio_src->bi_io_vec;
134 }
135
136 struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
137 {
138         struct bio *b;
139
140         b = bio_alloc_bioset(gfp_mask, 0, bs);
141         if (!b)
142                 return NULL;
143
144         __bio_clone_fast(b, bio);
145         return b;
146 }
147
148 struct bio *bio_split(struct bio *bio, int sectors,
149                       gfp_t gfp, struct bio_set *bs)
150 {
151         struct bio *split = NULL;
152
153         BUG_ON(sectors <= 0);
154         BUG_ON(sectors >= bio_sectors(bio));
155
156         /*
157          * Discards need a mutable bio_vec to accommodate the payload
158          * required by the DSM TRIM and UNMAP commands.
159          */
160         if (bio_op(bio) == REQ_OP_DISCARD || bio_op(bio) == REQ_OP_SECURE_ERASE)
161                 split = bio_clone_bioset(bio, gfp, bs);
162         else
163                 split = bio_clone_fast(bio, gfp, bs);
164
165         if (!split)
166                 return NULL;
167
168         split->bi_iter.bi_size = sectors << 9;
169
170         bio_advance(bio, split->bi_iter.bi_size);
171
172         return split;
173 }
174
175 void bio_free_pages(struct bio *bio)
176 {
177         struct bvec_iter_all iter;
178         struct bio_vec *bvec;
179
180         bio_for_each_segment_all(bvec, bio, iter)
181                 __free_page(bvec->bv_page);
182 }
183
184 void bio_advance(struct bio *bio, unsigned bytes)
185 {
186         bio_advance_iter(bio, &bio->bi_iter, bytes);
187 }
188
189 static void bio_free(struct bio *bio)
190 {
191         struct bio_set *bs = bio->bi_pool;
192
193         if (bs) {
194                 if (bio->bi_max_vecs > BIO_INLINE_VECS)
195                         mempool_free(bio->bi_io_vec, &bs->bvec_pool);
196
197                 mempool_free((void *) bio - bs->front_pad, &bs->bio_pool);
198         } else {
199                 kfree(bio);
200         }
201 }
202
203 void bio_put(struct bio *bio)
204 {
205         if (!bio_flagged(bio, BIO_REFFED))
206                 bio_free(bio);
207         else {
208                 BUG_ON(!atomic_read(&bio->__bi_cnt));
209
210                 /*
211                  * last put frees it
212                  */
213                 if (atomic_dec_and_test(&bio->__bi_cnt))
214                         bio_free(bio);
215         }
216 }
217
218 int bio_add_page(struct bio *bio, struct page *page,
219                  unsigned int len, unsigned int off)
220 {
221         struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt];
222
223         WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
224         WARN_ON_ONCE(bio->bi_vcnt >= bio->bi_max_vecs);
225
226         bv->bv_page = page;
227         bv->bv_offset = off;
228         bv->bv_len = len;
229
230         bio->bi_iter.bi_size += len;
231         bio->bi_vcnt++;
232         return len;
233 }
234
235 static inline bool bio_remaining_done(struct bio *bio)
236 {
237         /*
238          * If we're not chaining, then ->__bi_remaining is always 1 and
239          * we always end io on the first invocation.
240          */
241         if (!bio_flagged(bio, BIO_CHAIN))
242                 return true;
243
244         BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
245
246         if (atomic_dec_and_test(&bio->__bi_remaining)) {
247                 bio_clear_flag(bio, BIO_CHAIN);
248                 return true;
249         }
250
251         return false;
252 }
253
254 static struct bio *__bio_chain_endio(struct bio *bio)
255 {
256         struct bio *parent = bio->bi_private;
257
258         if (!parent->bi_status)
259                 parent->bi_status = bio->bi_status;
260         bio_put(bio);
261         return parent;
262 }
263
264 static void bio_chain_endio(struct bio *bio)
265 {
266         bio_endio(__bio_chain_endio(bio));
267 }
268
269 void bio_endio(struct bio *bio)
270 {
271 again:
272         if (!bio_remaining_done(bio))
273                 return;
274
275         /*
276          * Need to have a real endio function for chained bios, otherwise
277          * various corner cases will break (like stacking block devices that
278          * save/restore bi_end_io) - however, we want to avoid unbounded
279          * recursion and blowing the stack. Tail call optimization would
280          * handle this, but compiling with frame pointers also disables
281          * gcc's sibling call optimization.
282          */
283         if (bio->bi_end_io == bio_chain_endio) {
284                 bio = __bio_chain_endio(bio);
285                 goto again;
286         }
287
288         if (bio->bi_end_io)
289                 bio->bi_end_io(bio);
290 }
291
292 void bio_reset(struct bio *bio)
293 {
294         unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
295
296         memset(bio, 0, BIO_RESET_BYTES);
297         bio->bi_flags = flags;
298         atomic_set(&bio->__bi_remaining, 1);
299 }
300
301 struct bio *bio_kmalloc(gfp_t gfp_mask, unsigned int nr_iovecs)
302 {
303         struct bio *bio;
304
305         bio = kmalloc(sizeof(struct bio) +
306                       sizeof(struct bio_vec) * nr_iovecs, gfp_mask);
307         if (unlikely(!bio))
308                 return NULL;
309         bio_init(bio, nr_iovecs ? bio->bi_inline_vecs : NULL, nr_iovecs);
310         bio->bi_pool = NULL;
311         return bio;
312 }
313
314 static struct bio_vec *bvec_alloc(mempool_t *pool, int *nr_vecs,
315                 gfp_t gfp_mask)
316 {
317         *nr_vecs = roundup_pow_of_two(*nr_vecs);
318         /*
319          * Try a slab allocation first for all smaller allocations.  If that
320          * fails and __GFP_DIRECT_RECLAIM is set retry with the mempool.
321          * The mempool is sized to handle up to BIO_MAX_VECS entries.
322          */
323         if (*nr_vecs < BIO_MAX_VECS) {
324                 struct bio_vec *bvl;
325
326                 bvl = kmalloc(sizeof(*bvl) * *nr_vecs, gfp_mask);
327                 if (likely(bvl))
328                         return bvl;
329                 *nr_vecs = BIO_MAX_VECS;
330         }
331
332         return mempool_alloc(pool, gfp_mask);
333 }
334
335 struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
336 {
337         struct bio *bio;
338         void *p;
339
340         if (nr_iovecs > BIO_MAX_VECS)
341                 return NULL;
342
343         p = mempool_alloc(&bs->bio_pool, gfp_mask);
344         if (unlikely(!p))
345                 return NULL;
346
347         bio = p + bs->front_pad;
348         if (nr_iovecs > BIO_INLINE_VECS) {
349                 struct bio_vec *bvl = NULL;
350
351                 bvl = bvec_alloc(&bs->bvec_pool, &nr_iovecs, gfp_mask);
352                 if (unlikely(!bvl))
353                         goto err_free;
354
355                 bio_init(bio, bvl, nr_iovecs);
356         } else if (nr_iovecs) {
357                 bio_init(bio, bio->bi_inline_vecs, BIO_INLINE_VECS);
358         } else {
359                 bio_init(bio, NULL, 0);
360         }
361
362         bio->bi_pool = bs;
363         return bio;
364
365 err_free:
366         mempool_free(p, &bs->bio_pool);
367         return NULL;
368 }
369
370 struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask,
371                              struct bio_set *bs)
372 {
373         struct bvec_iter iter;
374         struct bio_vec bv;
375         struct bio *bio;
376
377         bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs);
378         if (!bio)
379                 return NULL;
380
381         bio->bi_bdev            = bio_src->bi_bdev;
382         bio->bi_opf             = bio_src->bi_opf;
383         bio->bi_iter.bi_sector  = bio_src->bi_iter.bi_sector;
384         bio->bi_iter.bi_size    = bio_src->bi_iter.bi_size;
385
386         switch (bio_op(bio)) {
387         case REQ_OP_DISCARD:
388         case REQ_OP_SECURE_ERASE:
389                 break;
390         case REQ_OP_WRITE_SAME:
391                 bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0];
392                 break;
393         default:
394                 bio_for_each_segment(bv, bio_src, iter)
395                         bio->bi_io_vec[bio->bi_vcnt++] = bv;
396                 break;
397         }
398
399         return bio;
400 }
401
402 void bioset_exit(struct bio_set *bs)
403 {
404         mempool_exit(&bs->bio_pool);
405         mempool_exit(&bs->bvec_pool);
406 }
407
408 int bioset_init(struct bio_set *bs,
409                 unsigned int pool_size,
410                 unsigned int front_pad,
411                 int flags)
412 {
413         int ret;
414
415         bs->front_pad = front_pad;
416         if (flags & BIOSET_NEED_BVECS)
417                 bs->back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
418         else
419                 bs->back_pad = 0;
420
421         ret   = mempool_init_kmalloc_pool(&bs->bio_pool, pool_size, bs->front_pad +
422                                           sizeof(struct bio) + bs->back_pad) ?:
423                 mempool_init_kmalloc_pool(&bs->bvec_pool, pool_size,
424                                           sizeof(struct bio_vec) * BIO_MAX_VECS);
425         if (ret)
426                 bioset_exit(bs);
427         return ret;
428 }