]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/bio.c
Update bcachefs sources to b91a514413 bcachefs: Don't try to delete stripes when RO
[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 void bio_copy_data_iter(struct bio *dst, struct bvec_iter *dst_iter,
56                         struct bio *src, struct bvec_iter *src_iter)
57 {
58         struct bio_vec src_bv, dst_bv;
59         void *src_p, *dst_p;
60         unsigned bytes;
61
62         while (src_iter->bi_size && dst_iter->bi_size) {
63                 src_bv = bio_iter_iovec(src, *src_iter);
64                 dst_bv = bio_iter_iovec(dst, *dst_iter);
65
66                 bytes = min(src_bv.bv_len, dst_bv.bv_len);
67
68                 src_p = kmap_atomic(src_bv.bv_page);
69                 dst_p = kmap_atomic(dst_bv.bv_page);
70
71                 memcpy(dst_p + dst_bv.bv_offset,
72                        src_p + src_bv.bv_offset,
73                        bytes);
74
75                 kunmap_atomic(dst_p);
76                 kunmap_atomic(src_p);
77
78                 flush_dcache_page(dst_bv.bv_page);
79
80                 bio_advance_iter(src, src_iter, bytes);
81                 bio_advance_iter(dst, dst_iter, bytes);
82         }
83 }
84
85 /**
86  * bio_copy_data - copy contents of data buffers from one bio to another
87  * @src: source bio
88  * @dst: destination bio
89  *
90  * Stops when it reaches the end of either @src or @dst - that is, copies
91  * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
92  */
93 void bio_copy_data(struct bio *dst, struct bio *src)
94 {
95         struct bvec_iter src_iter = src->bi_iter;
96         struct bvec_iter dst_iter = dst->bi_iter;
97
98         bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
99 }
100
101 void zero_fill_bio_iter(struct bio *bio, struct bvec_iter start)
102 {
103         unsigned long flags;
104         struct bio_vec bv;
105         struct bvec_iter iter;
106
107         __bio_for_each_segment(bv, bio, iter, start) {
108                 char *data = bvec_kmap_irq(&bv, &flags);
109                 memset(data, 0, bv.bv_len);
110                 bvec_kunmap_irq(data, &flags);
111         }
112 }
113
114 void __bio_clone_fast(struct bio *bio, struct bio *bio_src)
115 {
116         /*
117          * most users will be overriding ->bi_bdev with a new target,
118          * so we don't set nor calculate new physical/hw segment counts here
119          */
120         bio->bi_bdev = bio_src->bi_bdev;
121         bio_set_flag(bio, BIO_CLONED);
122         bio->bi_opf = bio_src->bi_opf;
123         bio->bi_iter = bio_src->bi_iter;
124         bio->bi_io_vec = bio_src->bi_io_vec;
125 }
126
127 struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs)
128 {
129         struct bio *b;
130
131         b = bio_alloc_bioset(gfp_mask, 0, bs);
132         if (!b)
133                 return NULL;
134
135         __bio_clone_fast(b, bio);
136         return b;
137 }
138
139 struct bio *bio_split(struct bio *bio, int sectors,
140                       gfp_t gfp, struct bio_set *bs)
141 {
142         struct bio *split = NULL;
143
144         BUG_ON(sectors <= 0);
145         BUG_ON(sectors >= bio_sectors(bio));
146
147         /*
148          * Discards need a mutable bio_vec to accommodate the payload
149          * required by the DSM TRIM and UNMAP commands.
150          */
151         if (bio_op(bio) == REQ_OP_DISCARD || bio_op(bio) == REQ_OP_SECURE_ERASE)
152                 split = bio_clone_bioset(bio, gfp, bs);
153         else
154                 split = bio_clone_fast(bio, gfp, bs);
155
156         if (!split)
157                 return NULL;
158
159         split->bi_iter.bi_size = sectors << 9;
160
161         bio_advance(bio, split->bi_iter.bi_size);
162
163         return split;
164 }
165
166 void bio_free_pages(struct bio *bio)
167 {
168         struct bvec_iter_all iter;
169         struct bio_vec *bvec;
170         int i;
171
172         bio_for_each_segment_all(bvec, bio, i, iter)
173                 __free_page(bvec->bv_page);
174 }
175
176 void bio_advance(struct bio *bio, unsigned bytes)
177 {
178         bio_advance_iter(bio, &bio->bi_iter, bytes);
179 }
180
181 static void bio_free(struct bio *bio)
182 {
183         unsigned front_pad = bio->bi_pool ? bio->bi_pool->front_pad : 0;
184
185         kfree((void *) bio - front_pad);
186 }
187
188 void bio_put(struct bio *bio)
189 {
190         if (!bio_flagged(bio, BIO_REFFED))
191                 bio_free(bio);
192         else {
193                 BUG_ON(!atomic_read(&bio->__bi_cnt));
194
195                 /*
196                  * last put frees it
197                  */
198                 if (atomic_dec_and_test(&bio->__bi_cnt))
199                         bio_free(bio);
200         }
201 }
202
203 int bio_add_page(struct bio *bio, struct page *page,
204                  unsigned int len, unsigned int off)
205 {
206         struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt];
207
208         WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED));
209         WARN_ON_ONCE(bio->bi_vcnt >= bio->bi_max_vecs);
210
211         bv->bv_page = page;
212         bv->bv_offset = off;
213         bv->bv_len = len;
214
215         bio->bi_iter.bi_size += len;
216         bio->bi_vcnt++;
217         return len;
218 }
219
220 static inline bool bio_remaining_done(struct bio *bio)
221 {
222         /*
223          * If we're not chaining, then ->__bi_remaining is always 1 and
224          * we always end io on the first invocation.
225          */
226         if (!bio_flagged(bio, BIO_CHAIN))
227                 return true;
228
229         BUG_ON(atomic_read(&bio->__bi_remaining) <= 0);
230
231         if (atomic_dec_and_test(&bio->__bi_remaining)) {
232                 bio_clear_flag(bio, BIO_CHAIN);
233                 return true;
234         }
235
236         return false;
237 }
238
239 static struct bio *__bio_chain_endio(struct bio *bio)
240 {
241         struct bio *parent = bio->bi_private;
242
243         if (!parent->bi_status)
244                 parent->bi_status = bio->bi_status;
245         bio_put(bio);
246         return parent;
247 }
248
249 static void bio_chain_endio(struct bio *bio)
250 {
251         bio_endio(__bio_chain_endio(bio));
252 }
253
254 void bio_endio(struct bio *bio)
255 {
256 again:
257         if (!bio_remaining_done(bio))
258                 return;
259
260         /*
261          * Need to have a real endio function for chained bios, otherwise
262          * various corner cases will break (like stacking block devices that
263          * save/restore bi_end_io) - however, we want to avoid unbounded
264          * recursion and blowing the stack. Tail call optimization would
265          * handle this, but compiling with frame pointers also disables
266          * gcc's sibling call optimization.
267          */
268         if (bio->bi_end_io == bio_chain_endio) {
269                 bio = __bio_chain_endio(bio);
270                 goto again;
271         }
272
273         if (bio->bi_end_io)
274                 bio->bi_end_io(bio);
275 }
276
277 void bio_reset(struct bio *bio)
278 {
279         unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
280
281         memset(bio, 0, BIO_RESET_BYTES);
282         bio->bi_flags = flags;
283         atomic_set(&bio->__bi_remaining, 1);
284 }
285
286 struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
287 {
288         unsigned front_pad = bs ? bs->front_pad : 0;
289         struct bio *bio;
290         void *p;
291
292         p = kmalloc(front_pad +
293                     sizeof(struct bio) +
294                     nr_iovecs * sizeof(struct bio_vec),
295                     gfp_mask);
296
297         if (unlikely(!p))
298                 return NULL;
299
300         bio = p + front_pad;
301         bio_init(bio, bio->bi_inline_vecs, nr_iovecs);
302         bio->bi_pool = bs;
303
304         return bio;
305 }
306
307 struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask,
308                              struct bio_set *bs)
309 {
310         struct bvec_iter iter;
311         struct bio_vec bv;
312         struct bio *bio;
313
314         bio = bio_alloc_bioset(gfp_mask, bio_segments(bio_src), bs);
315         if (!bio)
316                 return NULL;
317
318         bio->bi_bdev            = bio_src->bi_bdev;
319         bio->bi_opf             = bio_src->bi_opf;
320         bio->bi_iter.bi_sector  = bio_src->bi_iter.bi_sector;
321         bio->bi_iter.bi_size    = bio_src->bi_iter.bi_size;
322
323         switch (bio_op(bio)) {
324         case REQ_OP_DISCARD:
325         case REQ_OP_SECURE_ERASE:
326                 break;
327         case REQ_OP_WRITE_SAME:
328                 bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0];
329                 break;
330         default:
331                 bio_for_each_segment(bv, bio_src, iter)
332                         bio->bi_io_vec[bio->bi_vcnt++] = bv;
333                 break;
334         }
335
336         return bio;
337 }