1f9c78b2bSJens Axboe /* 2f9c78b2bSJens Axboe * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk> 3f9c78b2bSJens Axboe * 4f9c78b2bSJens Axboe * This program is free software; you can redistribute it and/or modify 5f9c78b2bSJens Axboe * it under the terms of the GNU General Public License version 2 as 6f9c78b2bSJens Axboe * published by the Free Software Foundation. 7f9c78b2bSJens Axboe * 8f9c78b2bSJens Axboe * This program is distributed in the hope that it will be useful, 9f9c78b2bSJens Axboe * but WITHOUT ANY WARRANTY; without even the implied warranty of 10f9c78b2bSJens Axboe * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11f9c78b2bSJens Axboe * GNU General Public License for more details. 12f9c78b2bSJens Axboe * 13f9c78b2bSJens Axboe * You should have received a copy of the GNU General Public Licens 14f9c78b2bSJens Axboe * along with this program; if not, write to the Free Software 15f9c78b2bSJens Axboe * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- 16f9c78b2bSJens Axboe * 17f9c78b2bSJens Axboe */ 18f9c78b2bSJens Axboe #include <linux/mm.h> 19f9c78b2bSJens Axboe #include <linux/swap.h> 20f9c78b2bSJens Axboe #include <linux/bio.h> 21f9c78b2bSJens Axboe #include <linux/blkdev.h> 22f9c78b2bSJens Axboe #include <linux/uio.h> 23f9c78b2bSJens Axboe #include <linux/iocontext.h> 24f9c78b2bSJens Axboe #include <linux/slab.h> 25f9c78b2bSJens Axboe #include <linux/init.h> 26f9c78b2bSJens Axboe #include <linux/kernel.h> 27f9c78b2bSJens Axboe #include <linux/export.h> 28f9c78b2bSJens Axboe #include <linux/mempool.h> 29f9c78b2bSJens Axboe #include <linux/workqueue.h> 30f9c78b2bSJens Axboe #include <linux/cgroup.h> 31f9c78b2bSJens Axboe 32f9c78b2bSJens Axboe #include <trace/events/block.h> 33f9c78b2bSJens Axboe 34f9c78b2bSJens Axboe /* 35f9c78b2bSJens Axboe * Test patch to inline a certain number of bi_io_vec's inside the bio 36f9c78b2bSJens Axboe * itself, to shrink a bio data allocation from two mempool calls to one 37f9c78b2bSJens Axboe */ 38f9c78b2bSJens Axboe #define BIO_INLINE_VECS 4 39f9c78b2bSJens Axboe 40f9c78b2bSJens Axboe /* 41f9c78b2bSJens Axboe * if you change this list, also change bvec_alloc or things will 42f9c78b2bSJens Axboe * break badly! cannot be bigger than what you can fit into an 43f9c78b2bSJens Axboe * unsigned short 44f9c78b2bSJens Axboe */ 45f9c78b2bSJens Axboe #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) } 46ed996a52SChristoph Hellwig static struct biovec_slab bvec_slabs[BVEC_POOL_NR] __read_mostly = { 47f9c78b2bSJens Axboe BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES), 48f9c78b2bSJens Axboe }; 49f9c78b2bSJens Axboe #undef BV 50f9c78b2bSJens Axboe 51f9c78b2bSJens Axboe /* 52f9c78b2bSJens Axboe * fs_bio_set is the bio_set containing bio and iovec memory pools used by 53f9c78b2bSJens Axboe * IO code that does not need private memory pools. 54f9c78b2bSJens Axboe */ 55f9c78b2bSJens Axboe struct bio_set *fs_bio_set; 56f9c78b2bSJens Axboe EXPORT_SYMBOL(fs_bio_set); 57f9c78b2bSJens Axboe 58f9c78b2bSJens Axboe /* 59f9c78b2bSJens Axboe * Our slab pool management 60f9c78b2bSJens Axboe */ 61f9c78b2bSJens Axboe struct bio_slab { 62f9c78b2bSJens Axboe struct kmem_cache *slab; 63f9c78b2bSJens Axboe unsigned int slab_ref; 64f9c78b2bSJens Axboe unsigned int slab_size; 65f9c78b2bSJens Axboe char name[8]; 66f9c78b2bSJens Axboe }; 67f9c78b2bSJens Axboe static DEFINE_MUTEX(bio_slab_lock); 68f9c78b2bSJens Axboe static struct bio_slab *bio_slabs; 69f9c78b2bSJens Axboe static unsigned int bio_slab_nr, bio_slab_max; 70f9c78b2bSJens Axboe 71f9c78b2bSJens Axboe static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size) 72f9c78b2bSJens Axboe { 73f9c78b2bSJens Axboe unsigned int sz = sizeof(struct bio) + extra_size; 74f9c78b2bSJens Axboe struct kmem_cache *slab = NULL; 75f9c78b2bSJens Axboe struct bio_slab *bslab, *new_bio_slabs; 76f9c78b2bSJens Axboe unsigned int new_bio_slab_max; 77f9c78b2bSJens Axboe unsigned int i, entry = -1; 78f9c78b2bSJens Axboe 79f9c78b2bSJens Axboe mutex_lock(&bio_slab_lock); 80f9c78b2bSJens Axboe 81f9c78b2bSJens Axboe i = 0; 82f9c78b2bSJens Axboe while (i < bio_slab_nr) { 83f9c78b2bSJens Axboe bslab = &bio_slabs[i]; 84f9c78b2bSJens Axboe 85f9c78b2bSJens Axboe if (!bslab->slab && entry == -1) 86f9c78b2bSJens Axboe entry = i; 87f9c78b2bSJens Axboe else if (bslab->slab_size == sz) { 88f9c78b2bSJens Axboe slab = bslab->slab; 89f9c78b2bSJens Axboe bslab->slab_ref++; 90f9c78b2bSJens Axboe break; 91f9c78b2bSJens Axboe } 92f9c78b2bSJens Axboe i++; 93f9c78b2bSJens Axboe } 94f9c78b2bSJens Axboe 95f9c78b2bSJens Axboe if (slab) 96f9c78b2bSJens Axboe goto out_unlock; 97f9c78b2bSJens Axboe 98f9c78b2bSJens Axboe if (bio_slab_nr == bio_slab_max && entry == -1) { 99f9c78b2bSJens Axboe new_bio_slab_max = bio_slab_max << 1; 100f9c78b2bSJens Axboe new_bio_slabs = krealloc(bio_slabs, 101f9c78b2bSJens Axboe new_bio_slab_max * sizeof(struct bio_slab), 102f9c78b2bSJens Axboe GFP_KERNEL); 103f9c78b2bSJens Axboe if (!new_bio_slabs) 104f9c78b2bSJens Axboe goto out_unlock; 105f9c78b2bSJens Axboe bio_slab_max = new_bio_slab_max; 106f9c78b2bSJens Axboe bio_slabs = new_bio_slabs; 107f9c78b2bSJens Axboe } 108f9c78b2bSJens Axboe if (entry == -1) 109f9c78b2bSJens Axboe entry = bio_slab_nr++; 110f9c78b2bSJens Axboe 111f9c78b2bSJens Axboe bslab = &bio_slabs[entry]; 112f9c78b2bSJens Axboe 113f9c78b2bSJens Axboe snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry); 1146a241483SMikulas Patocka slab = kmem_cache_create(bslab->name, sz, ARCH_KMALLOC_MINALIGN, 1156a241483SMikulas Patocka SLAB_HWCACHE_ALIGN, NULL); 116f9c78b2bSJens Axboe if (!slab) 117f9c78b2bSJens Axboe goto out_unlock; 118f9c78b2bSJens Axboe 119f9c78b2bSJens Axboe bslab->slab = slab; 120f9c78b2bSJens Axboe bslab->slab_ref = 1; 121f9c78b2bSJens Axboe bslab->slab_size = sz; 122f9c78b2bSJens Axboe out_unlock: 123f9c78b2bSJens Axboe mutex_unlock(&bio_slab_lock); 124f9c78b2bSJens Axboe return slab; 125f9c78b2bSJens Axboe } 126f9c78b2bSJens Axboe 127f9c78b2bSJens Axboe static void bio_put_slab(struct bio_set *bs) 128f9c78b2bSJens Axboe { 129f9c78b2bSJens Axboe struct bio_slab *bslab = NULL; 130f9c78b2bSJens Axboe unsigned int i; 131f9c78b2bSJens Axboe 132f9c78b2bSJens Axboe mutex_lock(&bio_slab_lock); 133f9c78b2bSJens Axboe 134f9c78b2bSJens Axboe for (i = 0; i < bio_slab_nr; i++) { 135f9c78b2bSJens Axboe if (bs->bio_slab == bio_slabs[i].slab) { 136f9c78b2bSJens Axboe bslab = &bio_slabs[i]; 137f9c78b2bSJens Axboe break; 138f9c78b2bSJens Axboe } 139f9c78b2bSJens Axboe } 140f9c78b2bSJens Axboe 141f9c78b2bSJens Axboe if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n")) 142f9c78b2bSJens Axboe goto out; 143f9c78b2bSJens Axboe 144f9c78b2bSJens Axboe WARN_ON(!bslab->slab_ref); 145f9c78b2bSJens Axboe 146f9c78b2bSJens Axboe if (--bslab->slab_ref) 147f9c78b2bSJens Axboe goto out; 148f9c78b2bSJens Axboe 149f9c78b2bSJens Axboe kmem_cache_destroy(bslab->slab); 150f9c78b2bSJens Axboe bslab->slab = NULL; 151f9c78b2bSJens Axboe 152f9c78b2bSJens Axboe out: 153f9c78b2bSJens Axboe mutex_unlock(&bio_slab_lock); 154f9c78b2bSJens Axboe } 155f9c78b2bSJens Axboe 156f9c78b2bSJens Axboe unsigned int bvec_nr_vecs(unsigned short idx) 157f9c78b2bSJens Axboe { 158f9c78b2bSJens Axboe return bvec_slabs[idx].nr_vecs; 159f9c78b2bSJens Axboe } 160f9c78b2bSJens Axboe 161f9c78b2bSJens Axboe void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx) 162f9c78b2bSJens Axboe { 163ed996a52SChristoph Hellwig if (!idx) 164ed996a52SChristoph Hellwig return; 165ed996a52SChristoph Hellwig idx--; 166f9c78b2bSJens Axboe 167ed996a52SChristoph Hellwig BIO_BUG_ON(idx >= BVEC_POOL_NR); 168ed996a52SChristoph Hellwig 169ed996a52SChristoph Hellwig if (idx == BVEC_POOL_MAX) { 170f9c78b2bSJens Axboe mempool_free(bv, pool); 171ed996a52SChristoph Hellwig } else { 172f9c78b2bSJens Axboe struct biovec_slab *bvs = bvec_slabs + idx; 173f9c78b2bSJens Axboe 174f9c78b2bSJens Axboe kmem_cache_free(bvs->slab, bv); 175f9c78b2bSJens Axboe } 176f9c78b2bSJens Axboe } 177f9c78b2bSJens Axboe 178f9c78b2bSJens Axboe struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx, 179f9c78b2bSJens Axboe mempool_t *pool) 180f9c78b2bSJens Axboe { 181f9c78b2bSJens Axboe struct bio_vec *bvl; 182f9c78b2bSJens Axboe 183f9c78b2bSJens Axboe /* 184f9c78b2bSJens Axboe * see comment near bvec_array define! 185f9c78b2bSJens Axboe */ 186f9c78b2bSJens Axboe switch (nr) { 187f9c78b2bSJens Axboe case 1: 188f9c78b2bSJens Axboe *idx = 0; 189f9c78b2bSJens Axboe break; 190f9c78b2bSJens Axboe case 2 ... 4: 191f9c78b2bSJens Axboe *idx = 1; 192f9c78b2bSJens Axboe break; 193f9c78b2bSJens Axboe case 5 ... 16: 194f9c78b2bSJens Axboe *idx = 2; 195f9c78b2bSJens Axboe break; 196f9c78b2bSJens Axboe case 17 ... 64: 197f9c78b2bSJens Axboe *idx = 3; 198f9c78b2bSJens Axboe break; 199f9c78b2bSJens Axboe case 65 ... 128: 200f9c78b2bSJens Axboe *idx = 4; 201f9c78b2bSJens Axboe break; 202f9c78b2bSJens Axboe case 129 ... BIO_MAX_PAGES: 203f9c78b2bSJens Axboe *idx = 5; 204f9c78b2bSJens Axboe break; 205f9c78b2bSJens Axboe default: 206f9c78b2bSJens Axboe return NULL; 207f9c78b2bSJens Axboe } 208f9c78b2bSJens Axboe 209f9c78b2bSJens Axboe /* 210f9c78b2bSJens Axboe * idx now points to the pool we want to allocate from. only the 211f9c78b2bSJens Axboe * 1-vec entry pool is mempool backed. 212f9c78b2bSJens Axboe */ 213ed996a52SChristoph Hellwig if (*idx == BVEC_POOL_MAX) { 214f9c78b2bSJens Axboe fallback: 215f9c78b2bSJens Axboe bvl = mempool_alloc(pool, gfp_mask); 216f9c78b2bSJens Axboe } else { 217f9c78b2bSJens Axboe struct biovec_slab *bvs = bvec_slabs + *idx; 218d0164adcSMel Gorman gfp_t __gfp_mask = gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO); 219f9c78b2bSJens Axboe 220f9c78b2bSJens Axboe /* 221f9c78b2bSJens Axboe * Make this allocation restricted and don't dump info on 222f9c78b2bSJens Axboe * allocation failures, since we'll fallback to the mempool 223f9c78b2bSJens Axboe * in case of failure. 224f9c78b2bSJens Axboe */ 225f9c78b2bSJens Axboe __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN; 226f9c78b2bSJens Axboe 227f9c78b2bSJens Axboe /* 228d0164adcSMel Gorman * Try a slab allocation. If this fails and __GFP_DIRECT_RECLAIM 229f9c78b2bSJens Axboe * is set, retry with the 1-entry mempool 230f9c78b2bSJens Axboe */ 231f9c78b2bSJens Axboe bvl = kmem_cache_alloc(bvs->slab, __gfp_mask); 232d0164adcSMel Gorman if (unlikely(!bvl && (gfp_mask & __GFP_DIRECT_RECLAIM))) { 233ed996a52SChristoph Hellwig *idx = BVEC_POOL_MAX; 234f9c78b2bSJens Axboe goto fallback; 235f9c78b2bSJens Axboe } 236f9c78b2bSJens Axboe } 237f9c78b2bSJens Axboe 238ed996a52SChristoph Hellwig (*idx)++; 239f9c78b2bSJens Axboe return bvl; 240f9c78b2bSJens Axboe } 241f9c78b2bSJens Axboe 242f9c78b2bSJens Axboe static void __bio_free(struct bio *bio) 243f9c78b2bSJens Axboe { 244f9c78b2bSJens Axboe bio_disassociate_task(bio); 245f9c78b2bSJens Axboe 246f9c78b2bSJens Axboe if (bio_integrity(bio)) 247f9c78b2bSJens Axboe bio_integrity_free(bio); 248f9c78b2bSJens Axboe } 249f9c78b2bSJens Axboe 250f9c78b2bSJens Axboe static void bio_free(struct bio *bio) 251f9c78b2bSJens Axboe { 252f9c78b2bSJens Axboe struct bio_set *bs = bio->bi_pool; 253f9c78b2bSJens Axboe void *p; 254f9c78b2bSJens Axboe 255f9c78b2bSJens Axboe __bio_free(bio); 256f9c78b2bSJens Axboe 257f9c78b2bSJens Axboe if (bs) { 258ed996a52SChristoph Hellwig bvec_free(bs->bvec_pool, bio->bi_io_vec, BVEC_POOL_IDX(bio)); 259f9c78b2bSJens Axboe 260f9c78b2bSJens Axboe /* 261f9c78b2bSJens Axboe * If we have front padding, adjust the bio pointer before freeing 262f9c78b2bSJens Axboe */ 263f9c78b2bSJens Axboe p = bio; 264f9c78b2bSJens Axboe p -= bs->front_pad; 265f9c78b2bSJens Axboe 266f9c78b2bSJens Axboe mempool_free(p, bs->bio_pool); 267f9c78b2bSJens Axboe } else { 268f9c78b2bSJens Axboe /* Bio was allocated by bio_kmalloc() */ 269f9c78b2bSJens Axboe kfree(bio); 270f9c78b2bSJens Axboe } 271f9c78b2bSJens Axboe } 272f9c78b2bSJens Axboe 2733a83f467SMing Lei void bio_init(struct bio *bio, struct bio_vec *table, 2743a83f467SMing Lei unsigned short max_vecs) 275f9c78b2bSJens Axboe { 276f9c78b2bSJens Axboe memset(bio, 0, sizeof(*bio)); 277c4cf5261SJens Axboe atomic_set(&bio->__bi_remaining, 1); 278dac56212SJens Axboe atomic_set(&bio->__bi_cnt, 1); 2793a83f467SMing Lei 2803a83f467SMing Lei bio->bi_io_vec = table; 2813a83f467SMing Lei bio->bi_max_vecs = max_vecs; 282f9c78b2bSJens Axboe } 283f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_init); 284f9c78b2bSJens Axboe 285f9c78b2bSJens Axboe /** 286f9c78b2bSJens Axboe * bio_reset - reinitialize a bio 287f9c78b2bSJens Axboe * @bio: bio to reset 288f9c78b2bSJens Axboe * 289f9c78b2bSJens Axboe * Description: 290f9c78b2bSJens Axboe * After calling bio_reset(), @bio will be in the same state as a freshly 291f9c78b2bSJens Axboe * allocated bio returned bio bio_alloc_bioset() - the only fields that are 292f9c78b2bSJens Axboe * preserved are the ones that are initialized by bio_alloc_bioset(). See 293f9c78b2bSJens Axboe * comment in struct bio. 294f9c78b2bSJens Axboe */ 295f9c78b2bSJens Axboe void bio_reset(struct bio *bio) 296f9c78b2bSJens Axboe { 297f9c78b2bSJens Axboe unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS); 298f9c78b2bSJens Axboe 299f9c78b2bSJens Axboe __bio_free(bio); 300f9c78b2bSJens Axboe 301f9c78b2bSJens Axboe memset(bio, 0, BIO_RESET_BYTES); 3024246a0b6SChristoph Hellwig bio->bi_flags = flags; 303c4cf5261SJens Axboe atomic_set(&bio->__bi_remaining, 1); 304f9c78b2bSJens Axboe } 305f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_reset); 306f9c78b2bSJens Axboe 30738f8baaeSChristoph Hellwig static struct bio *__bio_chain_endio(struct bio *bio) 308f9c78b2bSJens Axboe { 3094246a0b6SChristoph Hellwig struct bio *parent = bio->bi_private; 3104246a0b6SChristoph Hellwig 311af3e3a52SChristoph Hellwig if (!parent->bi_error) 3124246a0b6SChristoph Hellwig parent->bi_error = bio->bi_error; 313f9c78b2bSJens Axboe bio_put(bio); 31438f8baaeSChristoph Hellwig return parent; 31538f8baaeSChristoph Hellwig } 31638f8baaeSChristoph Hellwig 31738f8baaeSChristoph Hellwig static void bio_chain_endio(struct bio *bio) 31838f8baaeSChristoph Hellwig { 31938f8baaeSChristoph Hellwig bio_endio(__bio_chain_endio(bio)); 320f9c78b2bSJens Axboe } 321f9c78b2bSJens Axboe 322f9c78b2bSJens Axboe /** 323f9c78b2bSJens Axboe * bio_chain - chain bio completions 324f9c78b2bSJens Axboe * @bio: the target bio 325f9c78b2bSJens Axboe * @parent: the @bio's parent bio 326f9c78b2bSJens Axboe * 327f9c78b2bSJens Axboe * The caller won't have a bi_end_io called when @bio completes - instead, 328f9c78b2bSJens Axboe * @parent's bi_end_io won't be called until both @parent and @bio have 329f9c78b2bSJens Axboe * completed; the chained bio will also be freed when it completes. 330f9c78b2bSJens Axboe * 331f9c78b2bSJens Axboe * The caller must not set bi_private or bi_end_io in @bio. 332f9c78b2bSJens Axboe */ 333f9c78b2bSJens Axboe void bio_chain(struct bio *bio, struct bio *parent) 334f9c78b2bSJens Axboe { 335f9c78b2bSJens Axboe BUG_ON(bio->bi_private || bio->bi_end_io); 336f9c78b2bSJens Axboe 337f9c78b2bSJens Axboe bio->bi_private = parent; 338f9c78b2bSJens Axboe bio->bi_end_io = bio_chain_endio; 339c4cf5261SJens Axboe bio_inc_remaining(parent); 340f9c78b2bSJens Axboe } 341f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_chain); 342f9c78b2bSJens Axboe 343f9c78b2bSJens Axboe static void bio_alloc_rescue(struct work_struct *work) 344f9c78b2bSJens Axboe { 345f9c78b2bSJens Axboe struct bio_set *bs = container_of(work, struct bio_set, rescue_work); 346f9c78b2bSJens Axboe struct bio *bio; 347f9c78b2bSJens Axboe 348f9c78b2bSJens Axboe while (1) { 349f9c78b2bSJens Axboe spin_lock(&bs->rescue_lock); 350f9c78b2bSJens Axboe bio = bio_list_pop(&bs->rescue_list); 351f9c78b2bSJens Axboe spin_unlock(&bs->rescue_lock); 352f9c78b2bSJens Axboe 353f9c78b2bSJens Axboe if (!bio) 354f9c78b2bSJens Axboe break; 355f9c78b2bSJens Axboe 356f9c78b2bSJens Axboe generic_make_request(bio); 357f9c78b2bSJens Axboe } 358f9c78b2bSJens Axboe } 359f9c78b2bSJens Axboe 360f9c78b2bSJens Axboe static void punt_bios_to_rescuer(struct bio_set *bs) 361f9c78b2bSJens Axboe { 362f9c78b2bSJens Axboe struct bio_list punt, nopunt; 363f9c78b2bSJens Axboe struct bio *bio; 364f9c78b2bSJens Axboe 365f9c78b2bSJens Axboe /* 366f9c78b2bSJens Axboe * In order to guarantee forward progress we must punt only bios that 367f9c78b2bSJens Axboe * were allocated from this bio_set; otherwise, if there was a bio on 368f9c78b2bSJens Axboe * there for a stacking driver higher up in the stack, processing it 369f9c78b2bSJens Axboe * could require allocating bios from this bio_set, and doing that from 370f9c78b2bSJens Axboe * our own rescuer would be bad. 371f9c78b2bSJens Axboe * 372f9c78b2bSJens Axboe * Since bio lists are singly linked, pop them all instead of trying to 373f9c78b2bSJens Axboe * remove from the middle of the list: 374f9c78b2bSJens Axboe */ 375f9c78b2bSJens Axboe 376f9c78b2bSJens Axboe bio_list_init(&punt); 377f9c78b2bSJens Axboe bio_list_init(&nopunt); 378f9c78b2bSJens Axboe 379f5fe1b51SNeilBrown while ((bio = bio_list_pop(¤t->bio_list[0]))) 380f9c78b2bSJens Axboe bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio); 381f5fe1b51SNeilBrown current->bio_list[0] = nopunt; 382f9c78b2bSJens Axboe 383f5fe1b51SNeilBrown bio_list_init(&nopunt); 384f5fe1b51SNeilBrown while ((bio = bio_list_pop(¤t->bio_list[1]))) 385f5fe1b51SNeilBrown bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio); 386f5fe1b51SNeilBrown current->bio_list[1] = nopunt; 387f9c78b2bSJens Axboe 388f9c78b2bSJens Axboe spin_lock(&bs->rescue_lock); 389f9c78b2bSJens Axboe bio_list_merge(&bs->rescue_list, &punt); 390f9c78b2bSJens Axboe spin_unlock(&bs->rescue_lock); 391f9c78b2bSJens Axboe 392f9c78b2bSJens Axboe queue_work(bs->rescue_workqueue, &bs->rescue_work); 393f9c78b2bSJens Axboe } 394f9c78b2bSJens Axboe 395f9c78b2bSJens Axboe /** 396f9c78b2bSJens Axboe * bio_alloc_bioset - allocate a bio for I/O 397f9c78b2bSJens Axboe * @gfp_mask: the GFP_ mask given to the slab allocator 398f9c78b2bSJens Axboe * @nr_iovecs: number of iovecs to pre-allocate 399f9c78b2bSJens Axboe * @bs: the bio_set to allocate from. 400f9c78b2bSJens Axboe * 401f9c78b2bSJens Axboe * Description: 402f9c78b2bSJens Axboe * If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is 403f9c78b2bSJens Axboe * backed by the @bs's mempool. 404f9c78b2bSJens Axboe * 405d0164adcSMel Gorman * When @bs is not NULL, if %__GFP_DIRECT_RECLAIM is set then bio_alloc will 406d0164adcSMel Gorman * always be able to allocate a bio. This is due to the mempool guarantees. 407d0164adcSMel Gorman * To make this work, callers must never allocate more than 1 bio at a time 408d0164adcSMel Gorman * from this pool. Callers that need to allocate more than 1 bio must always 409d0164adcSMel Gorman * submit the previously allocated bio for IO before attempting to allocate 410d0164adcSMel Gorman * a new one. Failure to do so can cause deadlocks under memory pressure. 411f9c78b2bSJens Axboe * 412f9c78b2bSJens Axboe * Note that when running under generic_make_request() (i.e. any block 413f9c78b2bSJens Axboe * driver), bios are not submitted until after you return - see the code in 414f9c78b2bSJens Axboe * generic_make_request() that converts recursion into iteration, to prevent 415f9c78b2bSJens Axboe * stack overflows. 416f9c78b2bSJens Axboe * 417f9c78b2bSJens Axboe * This would normally mean allocating multiple bios under 418f9c78b2bSJens Axboe * generic_make_request() would be susceptible to deadlocks, but we have 419f9c78b2bSJens Axboe * deadlock avoidance code that resubmits any blocked bios from a rescuer 420f9c78b2bSJens Axboe * thread. 421f9c78b2bSJens Axboe * 422f9c78b2bSJens Axboe * However, we do not guarantee forward progress for allocations from other 423f9c78b2bSJens Axboe * mempools. Doing multiple allocations from the same mempool under 424f9c78b2bSJens Axboe * generic_make_request() should be avoided - instead, use bio_set's front_pad 425f9c78b2bSJens Axboe * for per bio allocations. 426f9c78b2bSJens Axboe * 427f9c78b2bSJens Axboe * RETURNS: 428f9c78b2bSJens Axboe * Pointer to new bio on success, NULL on failure. 429f9c78b2bSJens Axboe */ 4307a88fa19SDan Carpenter struct bio *bio_alloc_bioset(gfp_t gfp_mask, unsigned int nr_iovecs, 4317a88fa19SDan Carpenter struct bio_set *bs) 432f9c78b2bSJens Axboe { 433f9c78b2bSJens Axboe gfp_t saved_gfp = gfp_mask; 434f9c78b2bSJens Axboe unsigned front_pad; 435f9c78b2bSJens Axboe unsigned inline_vecs; 436f9c78b2bSJens Axboe struct bio_vec *bvl = NULL; 437f9c78b2bSJens Axboe struct bio *bio; 438f9c78b2bSJens Axboe void *p; 439f9c78b2bSJens Axboe 440f9c78b2bSJens Axboe if (!bs) { 441f9c78b2bSJens Axboe if (nr_iovecs > UIO_MAXIOV) 442f9c78b2bSJens Axboe return NULL; 443f9c78b2bSJens Axboe 444f9c78b2bSJens Axboe p = kmalloc(sizeof(struct bio) + 445f9c78b2bSJens Axboe nr_iovecs * sizeof(struct bio_vec), 446f9c78b2bSJens Axboe gfp_mask); 447f9c78b2bSJens Axboe front_pad = 0; 448f9c78b2bSJens Axboe inline_vecs = nr_iovecs; 449f9c78b2bSJens Axboe } else { 450d8f429e1SJunichi Nomura /* should not use nobvec bioset for nr_iovecs > 0 */ 451d8f429e1SJunichi Nomura if (WARN_ON_ONCE(!bs->bvec_pool && nr_iovecs > 0)) 452d8f429e1SJunichi Nomura return NULL; 453f9c78b2bSJens Axboe /* 454f9c78b2bSJens Axboe * generic_make_request() converts recursion to iteration; this 455f9c78b2bSJens Axboe * means if we're running beneath it, any bios we allocate and 456f9c78b2bSJens Axboe * submit will not be submitted (and thus freed) until after we 457f9c78b2bSJens Axboe * return. 458f9c78b2bSJens Axboe * 459f9c78b2bSJens Axboe * This exposes us to a potential deadlock if we allocate 460f9c78b2bSJens Axboe * multiple bios from the same bio_set() while running 461f9c78b2bSJens Axboe * underneath generic_make_request(). If we were to allocate 462f9c78b2bSJens Axboe * multiple bios (say a stacking block driver that was splitting 463f9c78b2bSJens Axboe * bios), we would deadlock if we exhausted the mempool's 464f9c78b2bSJens Axboe * reserve. 465f9c78b2bSJens Axboe * 466f9c78b2bSJens Axboe * We solve this, and guarantee forward progress, with a rescuer 467f9c78b2bSJens Axboe * workqueue per bio_set. If we go to allocate and there are 468f9c78b2bSJens Axboe * bios on current->bio_list, we first try the allocation 469d0164adcSMel Gorman * without __GFP_DIRECT_RECLAIM; if that fails, we punt those 470d0164adcSMel Gorman * bios we would be blocking to the rescuer workqueue before 471d0164adcSMel Gorman * we retry with the original gfp_flags. 472f9c78b2bSJens Axboe */ 473f9c78b2bSJens Axboe 474f5fe1b51SNeilBrown if (current->bio_list && 475f5fe1b51SNeilBrown (!bio_list_empty(¤t->bio_list[0]) || 476f5fe1b51SNeilBrown !bio_list_empty(¤t->bio_list[1]))) 477d0164adcSMel Gorman gfp_mask &= ~__GFP_DIRECT_RECLAIM; 478f9c78b2bSJens Axboe 479f9c78b2bSJens Axboe p = mempool_alloc(bs->bio_pool, gfp_mask); 480f9c78b2bSJens Axboe if (!p && gfp_mask != saved_gfp) { 481f9c78b2bSJens Axboe punt_bios_to_rescuer(bs); 482f9c78b2bSJens Axboe gfp_mask = saved_gfp; 483f9c78b2bSJens Axboe p = mempool_alloc(bs->bio_pool, gfp_mask); 484f9c78b2bSJens Axboe } 485f9c78b2bSJens Axboe 486f9c78b2bSJens Axboe front_pad = bs->front_pad; 487f9c78b2bSJens Axboe inline_vecs = BIO_INLINE_VECS; 488f9c78b2bSJens Axboe } 489f9c78b2bSJens Axboe 490f9c78b2bSJens Axboe if (unlikely(!p)) 491f9c78b2bSJens Axboe return NULL; 492f9c78b2bSJens Axboe 493f9c78b2bSJens Axboe bio = p + front_pad; 4943a83f467SMing Lei bio_init(bio, NULL, 0); 495f9c78b2bSJens Axboe 496f9c78b2bSJens Axboe if (nr_iovecs > inline_vecs) { 497ed996a52SChristoph Hellwig unsigned long idx = 0; 498ed996a52SChristoph Hellwig 499f9c78b2bSJens Axboe bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool); 500f9c78b2bSJens Axboe if (!bvl && gfp_mask != saved_gfp) { 501f9c78b2bSJens Axboe punt_bios_to_rescuer(bs); 502f9c78b2bSJens Axboe gfp_mask = saved_gfp; 503f9c78b2bSJens Axboe bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool); 504f9c78b2bSJens Axboe } 505f9c78b2bSJens Axboe 506f9c78b2bSJens Axboe if (unlikely(!bvl)) 507f9c78b2bSJens Axboe goto err_free; 508f9c78b2bSJens Axboe 509ed996a52SChristoph Hellwig bio->bi_flags |= idx << BVEC_POOL_OFFSET; 510f9c78b2bSJens Axboe } else if (nr_iovecs) { 511f9c78b2bSJens Axboe bvl = bio->bi_inline_vecs; 512f9c78b2bSJens Axboe } 513f9c78b2bSJens Axboe 514f9c78b2bSJens Axboe bio->bi_pool = bs; 515f9c78b2bSJens Axboe bio->bi_max_vecs = nr_iovecs; 516f9c78b2bSJens Axboe bio->bi_io_vec = bvl; 517f9c78b2bSJens Axboe return bio; 518f9c78b2bSJens Axboe 519f9c78b2bSJens Axboe err_free: 520f9c78b2bSJens Axboe mempool_free(p, bs->bio_pool); 521f9c78b2bSJens Axboe return NULL; 522f9c78b2bSJens Axboe } 523f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_bioset); 524f9c78b2bSJens Axboe 525f9c78b2bSJens Axboe void zero_fill_bio(struct bio *bio) 526f9c78b2bSJens Axboe { 527f9c78b2bSJens Axboe unsigned long flags; 528f9c78b2bSJens Axboe struct bio_vec bv; 529f9c78b2bSJens Axboe struct bvec_iter iter; 530f9c78b2bSJens Axboe 531f9c78b2bSJens Axboe bio_for_each_segment(bv, bio, iter) { 532f9c78b2bSJens Axboe char *data = bvec_kmap_irq(&bv, &flags); 533f9c78b2bSJens Axboe memset(data, 0, bv.bv_len); 534f9c78b2bSJens Axboe flush_dcache_page(bv.bv_page); 535f9c78b2bSJens Axboe bvec_kunmap_irq(data, &flags); 536f9c78b2bSJens Axboe } 537f9c78b2bSJens Axboe } 538f9c78b2bSJens Axboe EXPORT_SYMBOL(zero_fill_bio); 539f9c78b2bSJens Axboe 540f9c78b2bSJens Axboe /** 541f9c78b2bSJens Axboe * bio_put - release a reference to a bio 542f9c78b2bSJens Axboe * @bio: bio to release reference to 543f9c78b2bSJens Axboe * 544f9c78b2bSJens Axboe * Description: 545f9c78b2bSJens Axboe * Put a reference to a &struct bio, either one you have gotten with 546f9c78b2bSJens Axboe * bio_alloc, bio_get or bio_clone. The last put of a bio will free it. 547f9c78b2bSJens Axboe **/ 548f9c78b2bSJens Axboe void bio_put(struct bio *bio) 549f9c78b2bSJens Axboe { 550dac56212SJens Axboe if (!bio_flagged(bio, BIO_REFFED)) 551dac56212SJens Axboe bio_free(bio); 552dac56212SJens Axboe else { 553dac56212SJens Axboe BIO_BUG_ON(!atomic_read(&bio->__bi_cnt)); 554f9c78b2bSJens Axboe 555f9c78b2bSJens Axboe /* 556f9c78b2bSJens Axboe * last put frees it 557f9c78b2bSJens Axboe */ 558dac56212SJens Axboe if (atomic_dec_and_test(&bio->__bi_cnt)) 559f9c78b2bSJens Axboe bio_free(bio); 560f9c78b2bSJens Axboe } 561dac56212SJens Axboe } 562f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_put); 563f9c78b2bSJens Axboe 564f9c78b2bSJens Axboe inline int bio_phys_segments(struct request_queue *q, struct bio *bio) 565f9c78b2bSJens Axboe { 566f9c78b2bSJens Axboe if (unlikely(!bio_flagged(bio, BIO_SEG_VALID))) 567f9c78b2bSJens Axboe blk_recount_segments(q, bio); 568f9c78b2bSJens Axboe 569f9c78b2bSJens Axboe return bio->bi_phys_segments; 570f9c78b2bSJens Axboe } 571f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_phys_segments); 572f9c78b2bSJens Axboe 573f9c78b2bSJens Axboe /** 574f9c78b2bSJens Axboe * __bio_clone_fast - clone a bio that shares the original bio's biovec 575f9c78b2bSJens Axboe * @bio: destination bio 576f9c78b2bSJens Axboe * @bio_src: bio to clone 577f9c78b2bSJens Axboe * 578f9c78b2bSJens Axboe * Clone a &bio. Caller will own the returned bio, but not 579f9c78b2bSJens Axboe * the actual data it points to. Reference count of returned 580f9c78b2bSJens Axboe * bio will be one. 581f9c78b2bSJens Axboe * 582f9c78b2bSJens Axboe * Caller must ensure that @bio_src is not freed before @bio. 583f9c78b2bSJens Axboe */ 584f9c78b2bSJens Axboe void __bio_clone_fast(struct bio *bio, struct bio *bio_src) 585f9c78b2bSJens Axboe { 586ed996a52SChristoph Hellwig BUG_ON(bio->bi_pool && BVEC_POOL_IDX(bio)); 587f9c78b2bSJens Axboe 588f9c78b2bSJens Axboe /* 589f9c78b2bSJens Axboe * most users will be overriding ->bi_bdev with a new target, 590f9c78b2bSJens Axboe * so we don't set nor calculate new physical/hw segment counts here 591f9c78b2bSJens Axboe */ 592f9c78b2bSJens Axboe bio->bi_bdev = bio_src->bi_bdev; 593b7c44ed9SJens Axboe bio_set_flag(bio, BIO_CLONED); 5941eff9d32SJens Axboe bio->bi_opf = bio_src->bi_opf; 595f9c78b2bSJens Axboe bio->bi_iter = bio_src->bi_iter; 596f9c78b2bSJens Axboe bio->bi_io_vec = bio_src->bi_io_vec; 59720bd723eSPaolo Valente 59820bd723eSPaolo Valente bio_clone_blkcg_association(bio, bio_src); 599f9c78b2bSJens Axboe } 600f9c78b2bSJens Axboe EXPORT_SYMBOL(__bio_clone_fast); 601f9c78b2bSJens Axboe 602f9c78b2bSJens Axboe /** 603f9c78b2bSJens Axboe * bio_clone_fast - clone a bio that shares the original bio's biovec 604f9c78b2bSJens Axboe * @bio: bio to clone 605f9c78b2bSJens Axboe * @gfp_mask: allocation priority 606f9c78b2bSJens Axboe * @bs: bio_set to allocate from 607f9c78b2bSJens Axboe * 608f9c78b2bSJens Axboe * Like __bio_clone_fast, only also allocates the returned bio 609f9c78b2bSJens Axboe */ 610f9c78b2bSJens Axboe struct bio *bio_clone_fast(struct bio *bio, gfp_t gfp_mask, struct bio_set *bs) 611f9c78b2bSJens Axboe { 612f9c78b2bSJens Axboe struct bio *b; 613f9c78b2bSJens Axboe 614f9c78b2bSJens Axboe b = bio_alloc_bioset(gfp_mask, 0, bs); 615f9c78b2bSJens Axboe if (!b) 616f9c78b2bSJens Axboe return NULL; 617f9c78b2bSJens Axboe 618f9c78b2bSJens Axboe __bio_clone_fast(b, bio); 619f9c78b2bSJens Axboe 620f9c78b2bSJens Axboe if (bio_integrity(bio)) { 621f9c78b2bSJens Axboe int ret; 622f9c78b2bSJens Axboe 623f9c78b2bSJens Axboe ret = bio_integrity_clone(b, bio, gfp_mask); 624f9c78b2bSJens Axboe 625f9c78b2bSJens Axboe if (ret < 0) { 626f9c78b2bSJens Axboe bio_put(b); 627f9c78b2bSJens Axboe return NULL; 628f9c78b2bSJens Axboe } 629f9c78b2bSJens Axboe } 630f9c78b2bSJens Axboe 631f9c78b2bSJens Axboe return b; 632f9c78b2bSJens Axboe } 633f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_fast); 634f9c78b2bSJens Axboe 635c18a1e09SMing Lei static struct bio *__bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask, 636c18a1e09SMing Lei struct bio_set *bs, int offset, 637c18a1e09SMing Lei int size) 638f9c78b2bSJens Axboe { 639f9c78b2bSJens Axboe struct bvec_iter iter; 640f9c78b2bSJens Axboe struct bio_vec bv; 641f9c78b2bSJens Axboe struct bio *bio; 642c18a1e09SMing Lei struct bvec_iter iter_src = bio_src->bi_iter; 643c18a1e09SMing Lei 644c18a1e09SMing Lei /* for supporting partial clone */ 645c18a1e09SMing Lei if (offset || size != bio_src->bi_iter.bi_size) { 646c18a1e09SMing Lei bio_advance_iter(bio_src, &iter_src, offset); 647c18a1e09SMing Lei iter_src.bi_size = size; 648c18a1e09SMing Lei } 649f9c78b2bSJens Axboe 650f9c78b2bSJens Axboe /* 651f9c78b2bSJens Axboe * Pre immutable biovecs, __bio_clone() used to just do a memcpy from 652f9c78b2bSJens Axboe * bio_src->bi_io_vec to bio->bi_io_vec. 653f9c78b2bSJens Axboe * 654f9c78b2bSJens Axboe * We can't do that anymore, because: 655f9c78b2bSJens Axboe * 656f9c78b2bSJens Axboe * - The point of cloning the biovec is to produce a bio with a biovec 657f9c78b2bSJens Axboe * the caller can modify: bi_idx and bi_bvec_done should be 0. 658f9c78b2bSJens Axboe * 659f9c78b2bSJens Axboe * - The original bio could've had more than BIO_MAX_PAGES biovecs; if 660f9c78b2bSJens Axboe * we tried to clone the whole thing bio_alloc_bioset() would fail. 661f9c78b2bSJens Axboe * But the clone should succeed as long as the number of biovecs we 662f9c78b2bSJens Axboe * actually need to allocate is fewer than BIO_MAX_PAGES. 663f9c78b2bSJens Axboe * 664f9c78b2bSJens Axboe * - Lastly, bi_vcnt should not be looked at or relied upon by code 665f9c78b2bSJens Axboe * that does not own the bio - reason being drivers don't use it for 666f9c78b2bSJens Axboe * iterating over the biovec anymore, so expecting it to be kept up 667f9c78b2bSJens Axboe * to date (i.e. for clones that share the parent biovec) is just 668f9c78b2bSJens Axboe * asking for trouble and would force extra work on 669f9c78b2bSJens Axboe * __bio_clone_fast() anyways. 670f9c78b2bSJens Axboe */ 671f9c78b2bSJens Axboe 672c18a1e09SMing Lei bio = bio_alloc_bioset(gfp_mask, __bio_segments(bio_src, 673c18a1e09SMing Lei &iter_src), bs); 674f9c78b2bSJens Axboe if (!bio) 675f9c78b2bSJens Axboe return NULL; 676f9c78b2bSJens Axboe bio->bi_bdev = bio_src->bi_bdev; 6771eff9d32SJens Axboe bio->bi_opf = bio_src->bi_opf; 678f9c78b2bSJens Axboe bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector; 679f9c78b2bSJens Axboe bio->bi_iter.bi_size = bio_src->bi_iter.bi_size; 680f9c78b2bSJens Axboe 6817afafc8aSAdrian Hunter switch (bio_op(bio)) { 6827afafc8aSAdrian Hunter case REQ_OP_DISCARD: 6837afafc8aSAdrian Hunter case REQ_OP_SECURE_ERASE: 684a6f0788eSChaitanya Kulkarni case REQ_OP_WRITE_ZEROES: 6857afafc8aSAdrian Hunter break; 6867afafc8aSAdrian Hunter case REQ_OP_WRITE_SAME: 687f9c78b2bSJens Axboe bio->bi_io_vec[bio->bi_vcnt++] = bio_src->bi_io_vec[0]; 6887afafc8aSAdrian Hunter break; 6897afafc8aSAdrian Hunter default: 690c18a1e09SMing Lei __bio_for_each_segment(bv, bio_src, iter, iter_src) 691f9c78b2bSJens Axboe bio->bi_io_vec[bio->bi_vcnt++] = bv; 6927afafc8aSAdrian Hunter break; 6937afafc8aSAdrian Hunter } 694f9c78b2bSJens Axboe 695f9c78b2bSJens Axboe if (bio_integrity(bio_src)) { 696f9c78b2bSJens Axboe int ret; 697f9c78b2bSJens Axboe 698f9c78b2bSJens Axboe ret = bio_integrity_clone(bio, bio_src, gfp_mask); 699f9c78b2bSJens Axboe if (ret < 0) { 700f9c78b2bSJens Axboe bio_put(bio); 701f9c78b2bSJens Axboe return NULL; 702f9c78b2bSJens Axboe } 703f9c78b2bSJens Axboe } 704f9c78b2bSJens Axboe 70520bd723eSPaolo Valente bio_clone_blkcg_association(bio, bio_src); 70620bd723eSPaolo Valente 707f9c78b2bSJens Axboe return bio; 708f9c78b2bSJens Axboe } 709c18a1e09SMing Lei 710c18a1e09SMing Lei /** 711c18a1e09SMing Lei * bio_clone_bioset - clone a bio 712c18a1e09SMing Lei * @bio_src: bio to clone 713c18a1e09SMing Lei * @gfp_mask: allocation priority 714c18a1e09SMing Lei * @bs: bio_set to allocate from 715c18a1e09SMing Lei * 716c18a1e09SMing Lei * Clone bio. Caller will own the returned bio, but not the actual data it 717c18a1e09SMing Lei * points to. Reference count of returned bio will be one. 718c18a1e09SMing Lei */ 719c18a1e09SMing Lei struct bio *bio_clone_bioset(struct bio *bio_src, gfp_t gfp_mask, 720c18a1e09SMing Lei struct bio_set *bs) 721c18a1e09SMing Lei { 722c18a1e09SMing Lei return __bio_clone_bioset(bio_src, gfp_mask, bs, 0, 723c18a1e09SMing Lei bio_src->bi_iter.bi_size); 724c18a1e09SMing Lei } 725f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_clone_bioset); 726f9c78b2bSJens Axboe 727f9c78b2bSJens Axboe /** 728c18a1e09SMing Lei * bio_clone_bioset_partial - clone a partial bio 729c18a1e09SMing Lei * @bio_src: bio to clone 730c18a1e09SMing Lei * @gfp_mask: allocation priority 731c18a1e09SMing Lei * @bs: bio_set to allocate from 732c18a1e09SMing Lei * @offset: cloned starting from the offset 733c18a1e09SMing Lei * @size: size for the cloned bio 734c18a1e09SMing Lei * 735c18a1e09SMing Lei * Clone bio. Caller will own the returned bio, but not the actual data it 736c18a1e09SMing Lei * points to. Reference count of returned bio will be one. 737c18a1e09SMing Lei */ 738c18a1e09SMing Lei struct bio *bio_clone_bioset_partial(struct bio *bio_src, gfp_t gfp_mask, 739c18a1e09SMing Lei struct bio_set *bs, int offset, 740c18a1e09SMing Lei int size) 741c18a1e09SMing Lei { 742c18a1e09SMing Lei return __bio_clone_bioset(bio_src, gfp_mask, bs, offset, size); 743c18a1e09SMing Lei } 744c18a1e09SMing Lei EXPORT_SYMBOL(bio_clone_bioset_partial); 745c18a1e09SMing Lei 746c18a1e09SMing Lei /** 747c66a14d0SKent Overstreet * bio_add_pc_page - attempt to add page to bio 748c66a14d0SKent Overstreet * @q: the target queue 749c66a14d0SKent Overstreet * @bio: destination bio 750c66a14d0SKent Overstreet * @page: page to add 751c66a14d0SKent Overstreet * @len: vec entry length 752c66a14d0SKent Overstreet * @offset: vec entry offset 753f9c78b2bSJens Axboe * 754c66a14d0SKent Overstreet * Attempt to add a page to the bio_vec maplist. This can fail for a 755c66a14d0SKent Overstreet * number of reasons, such as the bio being full or target block device 756c66a14d0SKent Overstreet * limitations. The target block device must allow bio's up to PAGE_SIZE, 757c66a14d0SKent Overstreet * so it is always possible to add a single page to an empty bio. 758c66a14d0SKent Overstreet * 759c66a14d0SKent Overstreet * This should only be used by REQ_PC bios. 760f9c78b2bSJens Axboe */ 761c66a14d0SKent Overstreet int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page 762c66a14d0SKent Overstreet *page, unsigned int len, unsigned int offset) 763f9c78b2bSJens Axboe { 764f9c78b2bSJens Axboe int retried_segments = 0; 765f9c78b2bSJens Axboe struct bio_vec *bvec; 766f9c78b2bSJens Axboe 767f9c78b2bSJens Axboe /* 768f9c78b2bSJens Axboe * cloned bio must not modify vec list 769f9c78b2bSJens Axboe */ 770f9c78b2bSJens Axboe if (unlikely(bio_flagged(bio, BIO_CLONED))) 771f9c78b2bSJens Axboe return 0; 772f9c78b2bSJens Axboe 773c66a14d0SKent Overstreet if (((bio->bi_iter.bi_size + len) >> 9) > queue_max_hw_sectors(q)) 774f9c78b2bSJens Axboe return 0; 775f9c78b2bSJens Axboe 776f9c78b2bSJens Axboe /* 777f9c78b2bSJens Axboe * For filesystems with a blocksize smaller than the pagesize 778f9c78b2bSJens Axboe * we will often be called with the same page as last time and 779f9c78b2bSJens Axboe * a consecutive offset. Optimize this special case. 780f9c78b2bSJens Axboe */ 781f9c78b2bSJens Axboe if (bio->bi_vcnt > 0) { 782f9c78b2bSJens Axboe struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1]; 783f9c78b2bSJens Axboe 784f9c78b2bSJens Axboe if (page == prev->bv_page && 785f9c78b2bSJens Axboe offset == prev->bv_offset + prev->bv_len) { 786f9c78b2bSJens Axboe prev->bv_len += len; 787fcbf6a08SMaurizio Lombardi bio->bi_iter.bi_size += len; 788f9c78b2bSJens Axboe goto done; 789f9c78b2bSJens Axboe } 79066cb45aaSJens Axboe 79166cb45aaSJens Axboe /* 79266cb45aaSJens Axboe * If the queue doesn't support SG gaps and adding this 79366cb45aaSJens Axboe * offset would create a gap, disallow it. 79466cb45aaSJens Axboe */ 79503100aadSKeith Busch if (bvec_gap_to_prev(q, prev, offset)) 79666cb45aaSJens Axboe return 0; 797f9c78b2bSJens Axboe } 798f9c78b2bSJens Axboe 799f9c78b2bSJens Axboe if (bio->bi_vcnt >= bio->bi_max_vecs) 800f9c78b2bSJens Axboe return 0; 801f9c78b2bSJens Axboe 802f9c78b2bSJens Axboe /* 803f9c78b2bSJens Axboe * setup the new entry, we might clear it again later if we 804f9c78b2bSJens Axboe * cannot add the page 805f9c78b2bSJens Axboe */ 806f9c78b2bSJens Axboe bvec = &bio->bi_io_vec[bio->bi_vcnt]; 807f9c78b2bSJens Axboe bvec->bv_page = page; 808f9c78b2bSJens Axboe bvec->bv_len = len; 809f9c78b2bSJens Axboe bvec->bv_offset = offset; 810fcbf6a08SMaurizio Lombardi bio->bi_vcnt++; 811fcbf6a08SMaurizio Lombardi bio->bi_phys_segments++; 812fcbf6a08SMaurizio Lombardi bio->bi_iter.bi_size += len; 813fcbf6a08SMaurizio Lombardi 814fcbf6a08SMaurizio Lombardi /* 815fcbf6a08SMaurizio Lombardi * Perform a recount if the number of segments is greater 816fcbf6a08SMaurizio Lombardi * than queue_max_segments(q). 817fcbf6a08SMaurizio Lombardi */ 818fcbf6a08SMaurizio Lombardi 819fcbf6a08SMaurizio Lombardi while (bio->bi_phys_segments > queue_max_segments(q)) { 820fcbf6a08SMaurizio Lombardi 821fcbf6a08SMaurizio Lombardi if (retried_segments) 822fcbf6a08SMaurizio Lombardi goto failed; 823fcbf6a08SMaurizio Lombardi 824fcbf6a08SMaurizio Lombardi retried_segments = 1; 825fcbf6a08SMaurizio Lombardi blk_recount_segments(q, bio); 826fcbf6a08SMaurizio Lombardi } 827f9c78b2bSJens Axboe 828f9c78b2bSJens Axboe /* If we may be able to merge these biovecs, force a recount */ 829fcbf6a08SMaurizio Lombardi if (bio->bi_vcnt > 1 && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec))) 830b7c44ed9SJens Axboe bio_clear_flag(bio, BIO_SEG_VALID); 831f9c78b2bSJens Axboe 832f9c78b2bSJens Axboe done: 833f9c78b2bSJens Axboe return len; 834fcbf6a08SMaurizio Lombardi 835fcbf6a08SMaurizio Lombardi failed: 836fcbf6a08SMaurizio Lombardi bvec->bv_page = NULL; 837fcbf6a08SMaurizio Lombardi bvec->bv_len = 0; 838fcbf6a08SMaurizio Lombardi bvec->bv_offset = 0; 839fcbf6a08SMaurizio Lombardi bio->bi_vcnt--; 840fcbf6a08SMaurizio Lombardi bio->bi_iter.bi_size -= len; 841fcbf6a08SMaurizio Lombardi blk_recount_segments(q, bio); 842fcbf6a08SMaurizio Lombardi return 0; 843f9c78b2bSJens Axboe } 844f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_pc_page); 845f9c78b2bSJens Axboe 846f9c78b2bSJens Axboe /** 847f9c78b2bSJens Axboe * bio_add_page - attempt to add page to bio 848f9c78b2bSJens Axboe * @bio: destination bio 849f9c78b2bSJens Axboe * @page: page to add 850f9c78b2bSJens Axboe * @len: vec entry length 851f9c78b2bSJens Axboe * @offset: vec entry offset 852f9c78b2bSJens Axboe * 853c66a14d0SKent Overstreet * Attempt to add a page to the bio_vec maplist. This will only fail 854c66a14d0SKent Overstreet * if either bio->bi_vcnt == bio->bi_max_vecs or it's a cloned bio. 855f9c78b2bSJens Axboe */ 856c66a14d0SKent Overstreet int bio_add_page(struct bio *bio, struct page *page, 857c66a14d0SKent Overstreet unsigned int len, unsigned int offset) 858f9c78b2bSJens Axboe { 859c66a14d0SKent Overstreet struct bio_vec *bv; 860762380adSJens Axboe 861c66a14d0SKent Overstreet /* 862c66a14d0SKent Overstreet * cloned bio must not modify vec list 863c66a14d0SKent Overstreet */ 864c66a14d0SKent Overstreet if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED))) 865c66a14d0SKent Overstreet return 0; 86658a4915aSJens Axboe 867c66a14d0SKent Overstreet /* 868c66a14d0SKent Overstreet * For filesystems with a blocksize smaller than the pagesize 869c66a14d0SKent Overstreet * we will often be called with the same page as last time and 870c66a14d0SKent Overstreet * a consecutive offset. Optimize this special case. 871c66a14d0SKent Overstreet */ 872c66a14d0SKent Overstreet if (bio->bi_vcnt > 0) { 873c66a14d0SKent Overstreet bv = &bio->bi_io_vec[bio->bi_vcnt - 1]; 874c66a14d0SKent Overstreet 875c66a14d0SKent Overstreet if (page == bv->bv_page && 876c66a14d0SKent Overstreet offset == bv->bv_offset + bv->bv_len) { 877c66a14d0SKent Overstreet bv->bv_len += len; 878c66a14d0SKent Overstreet goto done; 879c66a14d0SKent Overstreet } 880c66a14d0SKent Overstreet } 881c66a14d0SKent Overstreet 882c66a14d0SKent Overstreet if (bio->bi_vcnt >= bio->bi_max_vecs) 883c66a14d0SKent Overstreet return 0; 884c66a14d0SKent Overstreet 885c66a14d0SKent Overstreet bv = &bio->bi_io_vec[bio->bi_vcnt]; 886c66a14d0SKent Overstreet bv->bv_page = page; 887c66a14d0SKent Overstreet bv->bv_len = len; 888c66a14d0SKent Overstreet bv->bv_offset = offset; 889c66a14d0SKent Overstreet 890c66a14d0SKent Overstreet bio->bi_vcnt++; 891c66a14d0SKent Overstreet done: 892c66a14d0SKent Overstreet bio->bi_iter.bi_size += len; 893c66a14d0SKent Overstreet return len; 894f9c78b2bSJens Axboe } 895f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_add_page); 896f9c78b2bSJens Axboe 8972cefe4dbSKent Overstreet /** 8982cefe4dbSKent Overstreet * bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio 8992cefe4dbSKent Overstreet * @bio: bio to add pages to 9002cefe4dbSKent Overstreet * @iter: iov iterator describing the region to be mapped 9012cefe4dbSKent Overstreet * 9022cefe4dbSKent Overstreet * Pins as many pages from *iter and appends them to @bio's bvec array. The 9032cefe4dbSKent Overstreet * pages will have to be released using put_page() when done. 9042cefe4dbSKent Overstreet */ 9052cefe4dbSKent Overstreet int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) 9062cefe4dbSKent Overstreet { 9072cefe4dbSKent Overstreet unsigned short nr_pages = bio->bi_max_vecs - bio->bi_vcnt; 9082cefe4dbSKent Overstreet struct bio_vec *bv = bio->bi_io_vec + bio->bi_vcnt; 9092cefe4dbSKent Overstreet struct page **pages = (struct page **)bv; 9102cefe4dbSKent Overstreet size_t offset, diff; 9112cefe4dbSKent Overstreet ssize_t size; 9122cefe4dbSKent Overstreet 9132cefe4dbSKent Overstreet size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset); 9142cefe4dbSKent Overstreet if (unlikely(size <= 0)) 9152cefe4dbSKent Overstreet return size ? size : -EFAULT; 9162cefe4dbSKent Overstreet nr_pages = (size + offset + PAGE_SIZE - 1) / PAGE_SIZE; 9172cefe4dbSKent Overstreet 9182cefe4dbSKent Overstreet /* 9192cefe4dbSKent Overstreet * Deep magic below: We need to walk the pinned pages backwards 9202cefe4dbSKent Overstreet * because we are abusing the space allocated for the bio_vecs 9212cefe4dbSKent Overstreet * for the page array. Because the bio_vecs are larger than the 9222cefe4dbSKent Overstreet * page pointers by definition this will always work. But it also 9232cefe4dbSKent Overstreet * means we can't use bio_add_page, so any changes to it's semantics 9242cefe4dbSKent Overstreet * need to be reflected here as well. 9252cefe4dbSKent Overstreet */ 9262cefe4dbSKent Overstreet bio->bi_iter.bi_size += size; 9272cefe4dbSKent Overstreet bio->bi_vcnt += nr_pages; 9282cefe4dbSKent Overstreet 9292cefe4dbSKent Overstreet diff = (nr_pages * PAGE_SIZE - offset) - size; 9302cefe4dbSKent Overstreet while (nr_pages--) { 9312cefe4dbSKent Overstreet bv[nr_pages].bv_page = pages[nr_pages]; 9322cefe4dbSKent Overstreet bv[nr_pages].bv_len = PAGE_SIZE; 9332cefe4dbSKent Overstreet bv[nr_pages].bv_offset = 0; 9342cefe4dbSKent Overstreet } 9352cefe4dbSKent Overstreet 9362cefe4dbSKent Overstreet bv[0].bv_offset += offset; 9372cefe4dbSKent Overstreet bv[0].bv_len -= offset; 9382cefe4dbSKent Overstreet if (diff) 9392cefe4dbSKent Overstreet bv[bio->bi_vcnt - 1].bv_len -= diff; 9402cefe4dbSKent Overstreet 9412cefe4dbSKent Overstreet iov_iter_advance(iter, size); 9422cefe4dbSKent Overstreet return 0; 9432cefe4dbSKent Overstreet } 9442cefe4dbSKent Overstreet EXPORT_SYMBOL_GPL(bio_iov_iter_get_pages); 9452cefe4dbSKent Overstreet 946f9c78b2bSJens Axboe struct submit_bio_ret { 947f9c78b2bSJens Axboe struct completion event; 948f9c78b2bSJens Axboe int error; 949f9c78b2bSJens Axboe }; 950f9c78b2bSJens Axboe 9514246a0b6SChristoph Hellwig static void submit_bio_wait_endio(struct bio *bio) 952f9c78b2bSJens Axboe { 953f9c78b2bSJens Axboe struct submit_bio_ret *ret = bio->bi_private; 954f9c78b2bSJens Axboe 9554246a0b6SChristoph Hellwig ret->error = bio->bi_error; 956f9c78b2bSJens Axboe complete(&ret->event); 957f9c78b2bSJens Axboe } 958f9c78b2bSJens Axboe 959f9c78b2bSJens Axboe /** 960f9c78b2bSJens Axboe * submit_bio_wait - submit a bio, and wait until it completes 961f9c78b2bSJens Axboe * @bio: The &struct bio which describes the I/O 962f9c78b2bSJens Axboe * 963f9c78b2bSJens Axboe * Simple wrapper around submit_bio(). Returns 0 on success, or the error from 964f9c78b2bSJens Axboe * bio_endio() on failure. 965f9c78b2bSJens Axboe */ 9664e49ea4aSMike Christie int submit_bio_wait(struct bio *bio) 967f9c78b2bSJens Axboe { 968f9c78b2bSJens Axboe struct submit_bio_ret ret; 969f9c78b2bSJens Axboe 970f9c78b2bSJens Axboe init_completion(&ret.event); 971f9c78b2bSJens Axboe bio->bi_private = &ret; 972f9c78b2bSJens Axboe bio->bi_end_io = submit_bio_wait_endio; 9731eff9d32SJens Axboe bio->bi_opf |= REQ_SYNC; 9744e49ea4aSMike Christie submit_bio(bio); 975d57d6115SStephane Gasparini wait_for_completion_io(&ret.event); 976f9c78b2bSJens Axboe 977f9c78b2bSJens Axboe return ret.error; 978f9c78b2bSJens Axboe } 979f9c78b2bSJens Axboe EXPORT_SYMBOL(submit_bio_wait); 980f9c78b2bSJens Axboe 981f9c78b2bSJens Axboe /** 982f9c78b2bSJens Axboe * bio_advance - increment/complete a bio by some number of bytes 983f9c78b2bSJens Axboe * @bio: bio to advance 984f9c78b2bSJens Axboe * @bytes: number of bytes to complete 985f9c78b2bSJens Axboe * 986f9c78b2bSJens Axboe * This updates bi_sector, bi_size and bi_idx; if the number of bytes to 987f9c78b2bSJens Axboe * complete doesn't align with a bvec boundary, then bv_len and bv_offset will 988f9c78b2bSJens Axboe * be updated on the last bvec as well. 989f9c78b2bSJens Axboe * 990f9c78b2bSJens Axboe * @bio will then represent the remaining, uncompleted portion of the io. 991f9c78b2bSJens Axboe */ 992f9c78b2bSJens Axboe void bio_advance(struct bio *bio, unsigned bytes) 993f9c78b2bSJens Axboe { 994f9c78b2bSJens Axboe if (bio_integrity(bio)) 995f9c78b2bSJens Axboe bio_integrity_advance(bio, bytes); 996f9c78b2bSJens Axboe 997f9c78b2bSJens Axboe bio_advance_iter(bio, &bio->bi_iter, bytes); 998f9c78b2bSJens Axboe } 999f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_advance); 1000f9c78b2bSJens Axboe 1001f9c78b2bSJens Axboe /** 1002f9c78b2bSJens Axboe * bio_alloc_pages - allocates a single page for each bvec in a bio 1003f9c78b2bSJens Axboe * @bio: bio to allocate pages for 1004f9c78b2bSJens Axboe * @gfp_mask: flags for allocation 1005f9c78b2bSJens Axboe * 1006f9c78b2bSJens Axboe * Allocates pages up to @bio->bi_vcnt. 1007f9c78b2bSJens Axboe * 1008f9c78b2bSJens Axboe * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are 1009f9c78b2bSJens Axboe * freed. 1010f9c78b2bSJens Axboe */ 1011f9c78b2bSJens Axboe int bio_alloc_pages(struct bio *bio, gfp_t gfp_mask) 1012f9c78b2bSJens Axboe { 1013f9c78b2bSJens Axboe int i; 1014f9c78b2bSJens Axboe struct bio_vec *bv; 1015f9c78b2bSJens Axboe 1016f9c78b2bSJens Axboe bio_for_each_segment_all(bv, bio, i) { 1017f9c78b2bSJens Axboe bv->bv_page = alloc_page(gfp_mask); 1018f9c78b2bSJens Axboe if (!bv->bv_page) { 1019f9c78b2bSJens Axboe while (--bv >= bio->bi_io_vec) 1020f9c78b2bSJens Axboe __free_page(bv->bv_page); 1021f9c78b2bSJens Axboe return -ENOMEM; 1022f9c78b2bSJens Axboe } 1023f9c78b2bSJens Axboe } 1024f9c78b2bSJens Axboe 1025f9c78b2bSJens Axboe return 0; 1026f9c78b2bSJens Axboe } 1027f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_alloc_pages); 1028f9c78b2bSJens Axboe 1029f9c78b2bSJens Axboe /** 1030f9c78b2bSJens Axboe * bio_copy_data - copy contents of data buffers from one chain of bios to 1031f9c78b2bSJens Axboe * another 1032f9c78b2bSJens Axboe * @src: source bio list 1033f9c78b2bSJens Axboe * @dst: destination bio list 1034f9c78b2bSJens Axboe * 1035f9c78b2bSJens Axboe * If @src and @dst are single bios, bi_next must be NULL - otherwise, treats 1036f9c78b2bSJens Axboe * @src and @dst as linked lists of bios. 1037f9c78b2bSJens Axboe * 1038f9c78b2bSJens Axboe * Stops when it reaches the end of either @src or @dst - that is, copies 1039f9c78b2bSJens Axboe * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios). 1040f9c78b2bSJens Axboe */ 1041f9c78b2bSJens Axboe void bio_copy_data(struct bio *dst, struct bio *src) 1042f9c78b2bSJens Axboe { 1043f9c78b2bSJens Axboe struct bvec_iter src_iter, dst_iter; 1044f9c78b2bSJens Axboe struct bio_vec src_bv, dst_bv; 1045f9c78b2bSJens Axboe void *src_p, *dst_p; 1046f9c78b2bSJens Axboe unsigned bytes; 1047f9c78b2bSJens Axboe 1048f9c78b2bSJens Axboe src_iter = src->bi_iter; 1049f9c78b2bSJens Axboe dst_iter = dst->bi_iter; 1050f9c78b2bSJens Axboe 1051f9c78b2bSJens Axboe while (1) { 1052f9c78b2bSJens Axboe if (!src_iter.bi_size) { 1053f9c78b2bSJens Axboe src = src->bi_next; 1054f9c78b2bSJens Axboe if (!src) 1055f9c78b2bSJens Axboe break; 1056f9c78b2bSJens Axboe 1057f9c78b2bSJens Axboe src_iter = src->bi_iter; 1058f9c78b2bSJens Axboe } 1059f9c78b2bSJens Axboe 1060f9c78b2bSJens Axboe if (!dst_iter.bi_size) { 1061f9c78b2bSJens Axboe dst = dst->bi_next; 1062f9c78b2bSJens Axboe if (!dst) 1063f9c78b2bSJens Axboe break; 1064f9c78b2bSJens Axboe 1065f9c78b2bSJens Axboe dst_iter = dst->bi_iter; 1066f9c78b2bSJens Axboe } 1067f9c78b2bSJens Axboe 1068f9c78b2bSJens Axboe src_bv = bio_iter_iovec(src, src_iter); 1069f9c78b2bSJens Axboe dst_bv = bio_iter_iovec(dst, dst_iter); 1070f9c78b2bSJens Axboe 1071f9c78b2bSJens Axboe bytes = min(src_bv.bv_len, dst_bv.bv_len); 1072f9c78b2bSJens Axboe 1073f9c78b2bSJens Axboe src_p = kmap_atomic(src_bv.bv_page); 1074f9c78b2bSJens Axboe dst_p = kmap_atomic(dst_bv.bv_page); 1075f9c78b2bSJens Axboe 1076f9c78b2bSJens Axboe memcpy(dst_p + dst_bv.bv_offset, 1077f9c78b2bSJens Axboe src_p + src_bv.bv_offset, 1078f9c78b2bSJens Axboe bytes); 1079f9c78b2bSJens Axboe 1080f9c78b2bSJens Axboe kunmap_atomic(dst_p); 1081f9c78b2bSJens Axboe kunmap_atomic(src_p); 1082f9c78b2bSJens Axboe 1083f9c78b2bSJens Axboe bio_advance_iter(src, &src_iter, bytes); 1084f9c78b2bSJens Axboe bio_advance_iter(dst, &dst_iter, bytes); 1085f9c78b2bSJens Axboe } 1086f9c78b2bSJens Axboe } 1087f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_copy_data); 1088f9c78b2bSJens Axboe 1089f9c78b2bSJens Axboe struct bio_map_data { 1090f9c78b2bSJens Axboe int is_our_pages; 109126e49cfcSKent Overstreet struct iov_iter iter; 109226e49cfcSKent Overstreet struct iovec iov[]; 1093f9c78b2bSJens Axboe }; 1094f9c78b2bSJens Axboe 1095f9c78b2bSJens Axboe static struct bio_map_data *bio_alloc_map_data(unsigned int iov_count, 1096f9c78b2bSJens Axboe gfp_t gfp_mask) 1097f9c78b2bSJens Axboe { 1098f9c78b2bSJens Axboe if (iov_count > UIO_MAXIOV) 1099f9c78b2bSJens Axboe return NULL; 1100f9c78b2bSJens Axboe 1101f9c78b2bSJens Axboe return kmalloc(sizeof(struct bio_map_data) + 110226e49cfcSKent Overstreet sizeof(struct iovec) * iov_count, gfp_mask); 1103f9c78b2bSJens Axboe } 1104f9c78b2bSJens Axboe 11059124d3feSDongsu Park /** 11069124d3feSDongsu Park * bio_copy_from_iter - copy all pages from iov_iter to bio 11079124d3feSDongsu Park * @bio: The &struct bio which describes the I/O as destination 11089124d3feSDongsu Park * @iter: iov_iter as source 11099124d3feSDongsu Park * 11109124d3feSDongsu Park * Copy all pages from iov_iter to bio. 11119124d3feSDongsu Park * Returns 0 on success, or error on failure. 11129124d3feSDongsu Park */ 11139124d3feSDongsu Park static int bio_copy_from_iter(struct bio *bio, struct iov_iter iter) 1114f9c78b2bSJens Axboe { 11159124d3feSDongsu Park int i; 1116f9c78b2bSJens Axboe struct bio_vec *bvec; 1117f9c78b2bSJens Axboe 1118f9c78b2bSJens Axboe bio_for_each_segment_all(bvec, bio, i) { 11199124d3feSDongsu Park ssize_t ret; 1120f9c78b2bSJens Axboe 11219124d3feSDongsu Park ret = copy_page_from_iter(bvec->bv_page, 11229124d3feSDongsu Park bvec->bv_offset, 11239124d3feSDongsu Park bvec->bv_len, 11249124d3feSDongsu Park &iter); 1125f9c78b2bSJens Axboe 11269124d3feSDongsu Park if (!iov_iter_count(&iter)) 11279124d3feSDongsu Park break; 1128f9c78b2bSJens Axboe 11299124d3feSDongsu Park if (ret < bvec->bv_len) 11309124d3feSDongsu Park return -EFAULT; 1131f9c78b2bSJens Axboe } 1132f9c78b2bSJens Axboe 11339124d3feSDongsu Park return 0; 1134f9c78b2bSJens Axboe } 1135f9c78b2bSJens Axboe 11369124d3feSDongsu Park /** 11379124d3feSDongsu Park * bio_copy_to_iter - copy all pages from bio to iov_iter 11389124d3feSDongsu Park * @bio: The &struct bio which describes the I/O as source 11399124d3feSDongsu Park * @iter: iov_iter as destination 11409124d3feSDongsu Park * 11419124d3feSDongsu Park * Copy all pages from bio to iov_iter. 11429124d3feSDongsu Park * Returns 0 on success, or error on failure. 11439124d3feSDongsu Park */ 11449124d3feSDongsu Park static int bio_copy_to_iter(struct bio *bio, struct iov_iter iter) 11459124d3feSDongsu Park { 11469124d3feSDongsu Park int i; 11479124d3feSDongsu Park struct bio_vec *bvec; 11489124d3feSDongsu Park 11499124d3feSDongsu Park bio_for_each_segment_all(bvec, bio, i) { 11509124d3feSDongsu Park ssize_t ret; 11519124d3feSDongsu Park 11529124d3feSDongsu Park ret = copy_page_to_iter(bvec->bv_page, 11539124d3feSDongsu Park bvec->bv_offset, 11549124d3feSDongsu Park bvec->bv_len, 11559124d3feSDongsu Park &iter); 11569124d3feSDongsu Park 11579124d3feSDongsu Park if (!iov_iter_count(&iter)) 11589124d3feSDongsu Park break; 11599124d3feSDongsu Park 11609124d3feSDongsu Park if (ret < bvec->bv_len) 11619124d3feSDongsu Park return -EFAULT; 11629124d3feSDongsu Park } 11639124d3feSDongsu Park 11649124d3feSDongsu Park return 0; 1165f9c78b2bSJens Axboe } 1166f9c78b2bSJens Axboe 1167491221f8SGuoqing Jiang void bio_free_pages(struct bio *bio) 11681dfa0f68SChristoph Hellwig { 11691dfa0f68SChristoph Hellwig struct bio_vec *bvec; 11701dfa0f68SChristoph Hellwig int i; 11711dfa0f68SChristoph Hellwig 11721dfa0f68SChristoph Hellwig bio_for_each_segment_all(bvec, bio, i) 11731dfa0f68SChristoph Hellwig __free_page(bvec->bv_page); 11741dfa0f68SChristoph Hellwig } 1175491221f8SGuoqing Jiang EXPORT_SYMBOL(bio_free_pages); 11761dfa0f68SChristoph Hellwig 1177f9c78b2bSJens Axboe /** 1178f9c78b2bSJens Axboe * bio_uncopy_user - finish previously mapped bio 1179f9c78b2bSJens Axboe * @bio: bio being terminated 1180f9c78b2bSJens Axboe * 1181ddad8dd0SChristoph Hellwig * Free pages allocated from bio_copy_user_iov() and write back data 1182f9c78b2bSJens Axboe * to user space in case of a read. 1183f9c78b2bSJens Axboe */ 1184f9c78b2bSJens Axboe int bio_uncopy_user(struct bio *bio) 1185f9c78b2bSJens Axboe { 1186f9c78b2bSJens Axboe struct bio_map_data *bmd = bio->bi_private; 11871dfa0f68SChristoph Hellwig int ret = 0; 1188f9c78b2bSJens Axboe 1189f9c78b2bSJens Axboe if (!bio_flagged(bio, BIO_NULL_MAPPED)) { 1190f9c78b2bSJens Axboe /* 1191f9c78b2bSJens Axboe * if we're in a workqueue, the request is orphaned, so 11922d99b55dSHannes Reinecke * don't copy into a random user address space, just free 11932d99b55dSHannes Reinecke * and return -EINTR so user space doesn't expect any data. 1194f9c78b2bSJens Axboe */ 11952d99b55dSHannes Reinecke if (!current->mm) 11962d99b55dSHannes Reinecke ret = -EINTR; 11972d99b55dSHannes Reinecke else if (bio_data_dir(bio) == READ) 11989124d3feSDongsu Park ret = bio_copy_to_iter(bio, bmd->iter); 11991dfa0f68SChristoph Hellwig if (bmd->is_our_pages) 12001dfa0f68SChristoph Hellwig bio_free_pages(bio); 1201f9c78b2bSJens Axboe } 1202f9c78b2bSJens Axboe kfree(bmd); 1203f9c78b2bSJens Axboe bio_put(bio); 1204f9c78b2bSJens Axboe return ret; 1205f9c78b2bSJens Axboe } 1206f9c78b2bSJens Axboe 1207f9c78b2bSJens Axboe /** 1208f9c78b2bSJens Axboe * bio_copy_user_iov - copy user data to bio 1209f9c78b2bSJens Axboe * @q: destination block queue 1210f9c78b2bSJens Axboe * @map_data: pointer to the rq_map_data holding pages (if necessary) 121126e49cfcSKent Overstreet * @iter: iovec iterator 1212f9c78b2bSJens Axboe * @gfp_mask: memory allocation flags 1213f9c78b2bSJens Axboe * 1214f9c78b2bSJens Axboe * Prepares and returns a bio for indirect user io, bouncing data 1215f9c78b2bSJens Axboe * to/from kernel pages as necessary. Must be paired with 1216f9c78b2bSJens Axboe * call bio_uncopy_user() on io completion. 1217f9c78b2bSJens Axboe */ 1218f9c78b2bSJens Axboe struct bio *bio_copy_user_iov(struct request_queue *q, 1219f9c78b2bSJens Axboe struct rq_map_data *map_data, 122026e49cfcSKent Overstreet const struct iov_iter *iter, 122126e49cfcSKent Overstreet gfp_t gfp_mask) 1222f9c78b2bSJens Axboe { 1223f9c78b2bSJens Axboe struct bio_map_data *bmd; 1224f9c78b2bSJens Axboe struct page *page; 1225f9c78b2bSJens Axboe struct bio *bio; 1226f9c78b2bSJens Axboe int i, ret; 1227f9c78b2bSJens Axboe int nr_pages = 0; 122826e49cfcSKent Overstreet unsigned int len = iter->count; 1229bd5ceceaSGeliang Tang unsigned int offset = map_data ? offset_in_page(map_data->offset) : 0; 1230f9c78b2bSJens Axboe 123126e49cfcSKent Overstreet for (i = 0; i < iter->nr_segs; i++) { 1232f9c78b2bSJens Axboe unsigned long uaddr; 1233f9c78b2bSJens Axboe unsigned long end; 1234f9c78b2bSJens Axboe unsigned long start; 1235f9c78b2bSJens Axboe 123626e49cfcSKent Overstreet uaddr = (unsigned long) iter->iov[i].iov_base; 123726e49cfcSKent Overstreet end = (uaddr + iter->iov[i].iov_len + PAGE_SIZE - 1) 123826e49cfcSKent Overstreet >> PAGE_SHIFT; 1239f9c78b2bSJens Axboe start = uaddr >> PAGE_SHIFT; 1240f9c78b2bSJens Axboe 1241f9c78b2bSJens Axboe /* 1242f9c78b2bSJens Axboe * Overflow, abort 1243f9c78b2bSJens Axboe */ 1244f9c78b2bSJens Axboe if (end < start) 1245f9c78b2bSJens Axboe return ERR_PTR(-EINVAL); 1246f9c78b2bSJens Axboe 1247f9c78b2bSJens Axboe nr_pages += end - start; 1248f9c78b2bSJens Axboe } 1249f9c78b2bSJens Axboe 1250f9c78b2bSJens Axboe if (offset) 1251f9c78b2bSJens Axboe nr_pages++; 1252f9c78b2bSJens Axboe 125326e49cfcSKent Overstreet bmd = bio_alloc_map_data(iter->nr_segs, gfp_mask); 1254f9c78b2bSJens Axboe if (!bmd) 1255f9c78b2bSJens Axboe return ERR_PTR(-ENOMEM); 1256f9c78b2bSJens Axboe 125726e49cfcSKent Overstreet /* 125826e49cfcSKent Overstreet * We need to do a deep copy of the iov_iter including the iovecs. 125926e49cfcSKent Overstreet * The caller provided iov might point to an on-stack or otherwise 126026e49cfcSKent Overstreet * shortlived one. 126126e49cfcSKent Overstreet */ 126226e49cfcSKent Overstreet bmd->is_our_pages = map_data ? 0 : 1; 126326e49cfcSKent Overstreet memcpy(bmd->iov, iter->iov, sizeof(struct iovec) * iter->nr_segs); 126426e49cfcSKent Overstreet iov_iter_init(&bmd->iter, iter->type, bmd->iov, 126526e49cfcSKent Overstreet iter->nr_segs, iter->count); 126626e49cfcSKent Overstreet 1267f9c78b2bSJens Axboe ret = -ENOMEM; 1268f9c78b2bSJens Axboe bio = bio_kmalloc(gfp_mask, nr_pages); 1269f9c78b2bSJens Axboe if (!bio) 1270f9c78b2bSJens Axboe goto out_bmd; 1271f9c78b2bSJens Axboe 1272f9c78b2bSJens Axboe ret = 0; 1273f9c78b2bSJens Axboe 1274f9c78b2bSJens Axboe if (map_data) { 1275f9c78b2bSJens Axboe nr_pages = 1 << map_data->page_order; 1276f9c78b2bSJens Axboe i = map_data->offset / PAGE_SIZE; 1277f9c78b2bSJens Axboe } 1278f9c78b2bSJens Axboe while (len) { 1279f9c78b2bSJens Axboe unsigned int bytes = PAGE_SIZE; 1280f9c78b2bSJens Axboe 1281f9c78b2bSJens Axboe bytes -= offset; 1282f9c78b2bSJens Axboe 1283f9c78b2bSJens Axboe if (bytes > len) 1284f9c78b2bSJens Axboe bytes = len; 1285f9c78b2bSJens Axboe 1286f9c78b2bSJens Axboe if (map_data) { 1287f9c78b2bSJens Axboe if (i == map_data->nr_entries * nr_pages) { 1288f9c78b2bSJens Axboe ret = -ENOMEM; 1289f9c78b2bSJens Axboe break; 1290f9c78b2bSJens Axboe } 1291f9c78b2bSJens Axboe 1292f9c78b2bSJens Axboe page = map_data->pages[i / nr_pages]; 1293f9c78b2bSJens Axboe page += (i % nr_pages); 1294f9c78b2bSJens Axboe 1295f9c78b2bSJens Axboe i++; 1296f9c78b2bSJens Axboe } else { 1297f9c78b2bSJens Axboe page = alloc_page(q->bounce_gfp | gfp_mask); 1298f9c78b2bSJens Axboe if (!page) { 1299f9c78b2bSJens Axboe ret = -ENOMEM; 1300f9c78b2bSJens Axboe break; 1301f9c78b2bSJens Axboe } 1302f9c78b2bSJens Axboe } 1303f9c78b2bSJens Axboe 1304f9c78b2bSJens Axboe if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes) 1305f9c78b2bSJens Axboe break; 1306f9c78b2bSJens Axboe 1307f9c78b2bSJens Axboe len -= bytes; 1308f9c78b2bSJens Axboe offset = 0; 1309f9c78b2bSJens Axboe } 1310f9c78b2bSJens Axboe 1311f9c78b2bSJens Axboe if (ret) 1312f9c78b2bSJens Axboe goto cleanup; 1313f9c78b2bSJens Axboe 1314f9c78b2bSJens Axboe /* 1315f9c78b2bSJens Axboe * success 1316f9c78b2bSJens Axboe */ 131726e49cfcSKent Overstreet if (((iter->type & WRITE) && (!map_data || !map_data->null_mapped)) || 1318f9c78b2bSJens Axboe (map_data && map_data->from_user)) { 13199124d3feSDongsu Park ret = bio_copy_from_iter(bio, *iter); 1320f9c78b2bSJens Axboe if (ret) 1321f9c78b2bSJens Axboe goto cleanup; 1322f9c78b2bSJens Axboe } 1323f9c78b2bSJens Axboe 132426e49cfcSKent Overstreet bio->bi_private = bmd; 1325f9c78b2bSJens Axboe return bio; 1326f9c78b2bSJens Axboe cleanup: 1327f9c78b2bSJens Axboe if (!map_data) 13281dfa0f68SChristoph Hellwig bio_free_pages(bio); 1329f9c78b2bSJens Axboe bio_put(bio); 1330f9c78b2bSJens Axboe out_bmd: 1331f9c78b2bSJens Axboe kfree(bmd); 1332f9c78b2bSJens Axboe return ERR_PTR(ret); 1333f9c78b2bSJens Axboe } 1334f9c78b2bSJens Axboe 133537f19e57SChristoph Hellwig /** 133637f19e57SChristoph Hellwig * bio_map_user_iov - map user iovec into bio 133737f19e57SChristoph Hellwig * @q: the struct request_queue for the bio 133837f19e57SChristoph Hellwig * @iter: iovec iterator 133937f19e57SChristoph Hellwig * @gfp_mask: memory allocation flags 134037f19e57SChristoph Hellwig * 134137f19e57SChristoph Hellwig * Map the user space address into a bio suitable for io to a block 134237f19e57SChristoph Hellwig * device. Returns an error pointer in case of error. 134337f19e57SChristoph Hellwig */ 134437f19e57SChristoph Hellwig struct bio *bio_map_user_iov(struct request_queue *q, 134526e49cfcSKent Overstreet const struct iov_iter *iter, 134626e49cfcSKent Overstreet gfp_t gfp_mask) 1347f9c78b2bSJens Axboe { 134826e49cfcSKent Overstreet int j; 1349f9c78b2bSJens Axboe int nr_pages = 0; 1350f9c78b2bSJens Axboe struct page **pages; 1351f9c78b2bSJens Axboe struct bio *bio; 1352f9c78b2bSJens Axboe int cur_page = 0; 1353f9c78b2bSJens Axboe int ret, offset; 135426e49cfcSKent Overstreet struct iov_iter i; 135526e49cfcSKent Overstreet struct iovec iov; 1356f9c78b2bSJens Axboe 135726e49cfcSKent Overstreet iov_for_each(iov, i, *iter) { 135826e49cfcSKent Overstreet unsigned long uaddr = (unsigned long) iov.iov_base; 135926e49cfcSKent Overstreet unsigned long len = iov.iov_len; 1360f9c78b2bSJens Axboe unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 1361f9c78b2bSJens Axboe unsigned long start = uaddr >> PAGE_SHIFT; 1362f9c78b2bSJens Axboe 1363f9c78b2bSJens Axboe /* 1364f9c78b2bSJens Axboe * Overflow, abort 1365f9c78b2bSJens Axboe */ 1366f9c78b2bSJens Axboe if (end < start) 1367f9c78b2bSJens Axboe return ERR_PTR(-EINVAL); 1368f9c78b2bSJens Axboe 1369f9c78b2bSJens Axboe nr_pages += end - start; 1370f9c78b2bSJens Axboe /* 1371a441b0d0SLinus Walleij * buffer must be aligned to at least logical block size for now 1372f9c78b2bSJens Axboe */ 1373f9c78b2bSJens Axboe if (uaddr & queue_dma_alignment(q)) 1374f9c78b2bSJens Axboe return ERR_PTR(-EINVAL); 1375f9c78b2bSJens Axboe } 1376f9c78b2bSJens Axboe 1377f9c78b2bSJens Axboe if (!nr_pages) 1378f9c78b2bSJens Axboe return ERR_PTR(-EINVAL); 1379f9c78b2bSJens Axboe 1380f9c78b2bSJens Axboe bio = bio_kmalloc(gfp_mask, nr_pages); 1381f9c78b2bSJens Axboe if (!bio) 1382f9c78b2bSJens Axboe return ERR_PTR(-ENOMEM); 1383f9c78b2bSJens Axboe 1384f9c78b2bSJens Axboe ret = -ENOMEM; 1385f9c78b2bSJens Axboe pages = kcalloc(nr_pages, sizeof(struct page *), gfp_mask); 1386f9c78b2bSJens Axboe if (!pages) 1387f9c78b2bSJens Axboe goto out; 1388f9c78b2bSJens Axboe 138926e49cfcSKent Overstreet iov_for_each(iov, i, *iter) { 139026e49cfcSKent Overstreet unsigned long uaddr = (unsigned long) iov.iov_base; 139126e49cfcSKent Overstreet unsigned long len = iov.iov_len; 1392f9c78b2bSJens Axboe unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 1393f9c78b2bSJens Axboe unsigned long start = uaddr >> PAGE_SHIFT; 1394f9c78b2bSJens Axboe const int local_nr_pages = end - start; 1395f9c78b2bSJens Axboe const int page_limit = cur_page + local_nr_pages; 1396f9c78b2bSJens Axboe 1397f9c78b2bSJens Axboe ret = get_user_pages_fast(uaddr, local_nr_pages, 139826e49cfcSKent Overstreet (iter->type & WRITE) != WRITE, 139926e49cfcSKent Overstreet &pages[cur_page]); 1400f9c78b2bSJens Axboe if (ret < local_nr_pages) { 1401f9c78b2bSJens Axboe ret = -EFAULT; 1402f9c78b2bSJens Axboe goto out_unmap; 1403f9c78b2bSJens Axboe } 1404f9c78b2bSJens Axboe 1405bd5ceceaSGeliang Tang offset = offset_in_page(uaddr); 1406f9c78b2bSJens Axboe for (j = cur_page; j < page_limit; j++) { 1407f9c78b2bSJens Axboe unsigned int bytes = PAGE_SIZE - offset; 1408f9c78b2bSJens Axboe 1409f9c78b2bSJens Axboe if (len <= 0) 1410f9c78b2bSJens Axboe break; 1411f9c78b2bSJens Axboe 1412f9c78b2bSJens Axboe if (bytes > len) 1413f9c78b2bSJens Axboe bytes = len; 1414f9c78b2bSJens Axboe 1415f9c78b2bSJens Axboe /* 1416f9c78b2bSJens Axboe * sorry... 1417f9c78b2bSJens Axboe */ 1418f9c78b2bSJens Axboe if (bio_add_pc_page(q, bio, pages[j], bytes, offset) < 1419f9c78b2bSJens Axboe bytes) 1420f9c78b2bSJens Axboe break; 1421f9c78b2bSJens Axboe 1422f9c78b2bSJens Axboe len -= bytes; 1423f9c78b2bSJens Axboe offset = 0; 1424f9c78b2bSJens Axboe } 1425f9c78b2bSJens Axboe 1426f9c78b2bSJens Axboe cur_page = j; 1427f9c78b2bSJens Axboe /* 1428f9c78b2bSJens Axboe * release the pages we didn't map into the bio, if any 1429f9c78b2bSJens Axboe */ 1430f9c78b2bSJens Axboe while (j < page_limit) 143109cbfeafSKirill A. Shutemov put_page(pages[j++]); 1432f9c78b2bSJens Axboe } 1433f9c78b2bSJens Axboe 1434f9c78b2bSJens Axboe kfree(pages); 1435f9c78b2bSJens Axboe 1436b7c44ed9SJens Axboe bio_set_flag(bio, BIO_USER_MAPPED); 143737f19e57SChristoph Hellwig 143837f19e57SChristoph Hellwig /* 14395fad1b64SBart Van Assche * subtle -- if bio_map_user_iov() ended up bouncing a bio, 144037f19e57SChristoph Hellwig * it would normally disappear when its bi_end_io is run. 144137f19e57SChristoph Hellwig * however, we need it for the unmap, so grab an extra 144237f19e57SChristoph Hellwig * reference to it 144337f19e57SChristoph Hellwig */ 144437f19e57SChristoph Hellwig bio_get(bio); 1445f9c78b2bSJens Axboe return bio; 1446f9c78b2bSJens Axboe 1447f9c78b2bSJens Axboe out_unmap: 144826e49cfcSKent Overstreet for (j = 0; j < nr_pages; j++) { 144926e49cfcSKent Overstreet if (!pages[j]) 1450f9c78b2bSJens Axboe break; 145109cbfeafSKirill A. Shutemov put_page(pages[j]); 1452f9c78b2bSJens Axboe } 1453f9c78b2bSJens Axboe out: 1454f9c78b2bSJens Axboe kfree(pages); 1455f9c78b2bSJens Axboe bio_put(bio); 1456f9c78b2bSJens Axboe return ERR_PTR(ret); 1457f9c78b2bSJens Axboe } 1458f9c78b2bSJens Axboe 1459f9c78b2bSJens Axboe static void __bio_unmap_user(struct bio *bio) 1460f9c78b2bSJens Axboe { 1461f9c78b2bSJens Axboe struct bio_vec *bvec; 1462f9c78b2bSJens Axboe int i; 1463f9c78b2bSJens Axboe 1464f9c78b2bSJens Axboe /* 1465f9c78b2bSJens Axboe * make sure we dirty pages we wrote to 1466f9c78b2bSJens Axboe */ 1467f9c78b2bSJens Axboe bio_for_each_segment_all(bvec, bio, i) { 1468f9c78b2bSJens Axboe if (bio_data_dir(bio) == READ) 1469f9c78b2bSJens Axboe set_page_dirty_lock(bvec->bv_page); 1470f9c78b2bSJens Axboe 147109cbfeafSKirill A. Shutemov put_page(bvec->bv_page); 1472f9c78b2bSJens Axboe } 1473f9c78b2bSJens Axboe 1474f9c78b2bSJens Axboe bio_put(bio); 1475f9c78b2bSJens Axboe } 1476f9c78b2bSJens Axboe 1477f9c78b2bSJens Axboe /** 1478f9c78b2bSJens Axboe * bio_unmap_user - unmap a bio 1479f9c78b2bSJens Axboe * @bio: the bio being unmapped 1480f9c78b2bSJens Axboe * 14815fad1b64SBart Van Assche * Unmap a bio previously mapped by bio_map_user_iov(). Must be called from 14825fad1b64SBart Van Assche * process context. 1483f9c78b2bSJens Axboe * 1484f9c78b2bSJens Axboe * bio_unmap_user() may sleep. 1485f9c78b2bSJens Axboe */ 1486f9c78b2bSJens Axboe void bio_unmap_user(struct bio *bio) 1487f9c78b2bSJens Axboe { 1488f9c78b2bSJens Axboe __bio_unmap_user(bio); 1489f9c78b2bSJens Axboe bio_put(bio); 1490f9c78b2bSJens Axboe } 1491f9c78b2bSJens Axboe 14924246a0b6SChristoph Hellwig static void bio_map_kern_endio(struct bio *bio) 1493f9c78b2bSJens Axboe { 1494f9c78b2bSJens Axboe bio_put(bio); 1495f9c78b2bSJens Axboe } 1496f9c78b2bSJens Axboe 149775c72b83SChristoph Hellwig /** 149875c72b83SChristoph Hellwig * bio_map_kern - map kernel address into bio 149975c72b83SChristoph Hellwig * @q: the struct request_queue for the bio 150075c72b83SChristoph Hellwig * @data: pointer to buffer to map 150175c72b83SChristoph Hellwig * @len: length in bytes 150275c72b83SChristoph Hellwig * @gfp_mask: allocation flags for bio allocation 150375c72b83SChristoph Hellwig * 150475c72b83SChristoph Hellwig * Map the kernel address into a bio suitable for io to a block 150575c72b83SChristoph Hellwig * device. Returns an error pointer in case of error. 150675c72b83SChristoph Hellwig */ 150775c72b83SChristoph Hellwig struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len, 150875c72b83SChristoph Hellwig gfp_t gfp_mask) 1509f9c78b2bSJens Axboe { 1510f9c78b2bSJens Axboe unsigned long kaddr = (unsigned long)data; 1511f9c78b2bSJens Axboe unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 1512f9c78b2bSJens Axboe unsigned long start = kaddr >> PAGE_SHIFT; 1513f9c78b2bSJens Axboe const int nr_pages = end - start; 1514f9c78b2bSJens Axboe int offset, i; 1515f9c78b2bSJens Axboe struct bio *bio; 1516f9c78b2bSJens Axboe 1517f9c78b2bSJens Axboe bio = bio_kmalloc(gfp_mask, nr_pages); 1518f9c78b2bSJens Axboe if (!bio) 1519f9c78b2bSJens Axboe return ERR_PTR(-ENOMEM); 1520f9c78b2bSJens Axboe 1521f9c78b2bSJens Axboe offset = offset_in_page(kaddr); 1522f9c78b2bSJens Axboe for (i = 0; i < nr_pages; i++) { 1523f9c78b2bSJens Axboe unsigned int bytes = PAGE_SIZE - offset; 1524f9c78b2bSJens Axboe 1525f9c78b2bSJens Axboe if (len <= 0) 1526f9c78b2bSJens Axboe break; 1527f9c78b2bSJens Axboe 1528f9c78b2bSJens Axboe if (bytes > len) 1529f9c78b2bSJens Axboe bytes = len; 1530f9c78b2bSJens Axboe 1531f9c78b2bSJens Axboe if (bio_add_pc_page(q, bio, virt_to_page(data), bytes, 153275c72b83SChristoph Hellwig offset) < bytes) { 153375c72b83SChristoph Hellwig /* we don't support partial mappings */ 153475c72b83SChristoph Hellwig bio_put(bio); 153575c72b83SChristoph Hellwig return ERR_PTR(-EINVAL); 153675c72b83SChristoph Hellwig } 1537f9c78b2bSJens Axboe 1538f9c78b2bSJens Axboe data += bytes; 1539f9c78b2bSJens Axboe len -= bytes; 1540f9c78b2bSJens Axboe offset = 0; 1541f9c78b2bSJens Axboe } 1542f9c78b2bSJens Axboe 1543f9c78b2bSJens Axboe bio->bi_end_io = bio_map_kern_endio; 1544f9c78b2bSJens Axboe return bio; 1545f9c78b2bSJens Axboe } 1546f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_map_kern); 1547f9c78b2bSJens Axboe 15484246a0b6SChristoph Hellwig static void bio_copy_kern_endio(struct bio *bio) 1549f9c78b2bSJens Axboe { 15501dfa0f68SChristoph Hellwig bio_free_pages(bio); 15511dfa0f68SChristoph Hellwig bio_put(bio); 15521dfa0f68SChristoph Hellwig } 15531dfa0f68SChristoph Hellwig 15544246a0b6SChristoph Hellwig static void bio_copy_kern_endio_read(struct bio *bio) 15551dfa0f68SChristoph Hellwig { 155642d2683aSChristoph Hellwig char *p = bio->bi_private; 15571dfa0f68SChristoph Hellwig struct bio_vec *bvec; 1558f9c78b2bSJens Axboe int i; 1559f9c78b2bSJens Axboe 1560f9c78b2bSJens Axboe bio_for_each_segment_all(bvec, bio, i) { 15611dfa0f68SChristoph Hellwig memcpy(p, page_address(bvec->bv_page), bvec->bv_len); 1562f9c78b2bSJens Axboe p += bvec->bv_len; 1563f9c78b2bSJens Axboe } 1564f9c78b2bSJens Axboe 15654246a0b6SChristoph Hellwig bio_copy_kern_endio(bio); 1566f9c78b2bSJens Axboe } 1567f9c78b2bSJens Axboe 1568f9c78b2bSJens Axboe /** 1569f9c78b2bSJens Axboe * bio_copy_kern - copy kernel address into bio 1570f9c78b2bSJens Axboe * @q: the struct request_queue for the bio 1571f9c78b2bSJens Axboe * @data: pointer to buffer to copy 1572f9c78b2bSJens Axboe * @len: length in bytes 1573f9c78b2bSJens Axboe * @gfp_mask: allocation flags for bio and page allocation 1574f9c78b2bSJens Axboe * @reading: data direction is READ 1575f9c78b2bSJens Axboe * 1576f9c78b2bSJens Axboe * copy the kernel address into a bio suitable for io to a block 1577f9c78b2bSJens Axboe * device. Returns an error pointer in case of error. 1578f9c78b2bSJens Axboe */ 1579f9c78b2bSJens Axboe struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len, 1580f9c78b2bSJens Axboe gfp_t gfp_mask, int reading) 1581f9c78b2bSJens Axboe { 158242d2683aSChristoph Hellwig unsigned long kaddr = (unsigned long)data; 158342d2683aSChristoph Hellwig unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 158442d2683aSChristoph Hellwig unsigned long start = kaddr >> PAGE_SHIFT; 158542d2683aSChristoph Hellwig struct bio *bio; 1586f9c78b2bSJens Axboe void *p = data; 15871dfa0f68SChristoph Hellwig int nr_pages = 0; 1588f9c78b2bSJens Axboe 158942d2683aSChristoph Hellwig /* 159042d2683aSChristoph Hellwig * Overflow, abort 159142d2683aSChristoph Hellwig */ 159242d2683aSChristoph Hellwig if (end < start) 159342d2683aSChristoph Hellwig return ERR_PTR(-EINVAL); 1594f9c78b2bSJens Axboe 159542d2683aSChristoph Hellwig nr_pages = end - start; 159642d2683aSChristoph Hellwig bio = bio_kmalloc(gfp_mask, nr_pages); 159742d2683aSChristoph Hellwig if (!bio) 159842d2683aSChristoph Hellwig return ERR_PTR(-ENOMEM); 159942d2683aSChristoph Hellwig 160042d2683aSChristoph Hellwig while (len) { 160142d2683aSChristoph Hellwig struct page *page; 160242d2683aSChristoph Hellwig unsigned int bytes = PAGE_SIZE; 160342d2683aSChristoph Hellwig 160442d2683aSChristoph Hellwig if (bytes > len) 160542d2683aSChristoph Hellwig bytes = len; 160642d2683aSChristoph Hellwig 160742d2683aSChristoph Hellwig page = alloc_page(q->bounce_gfp | gfp_mask); 160842d2683aSChristoph Hellwig if (!page) 160942d2683aSChristoph Hellwig goto cleanup; 161042d2683aSChristoph Hellwig 161142d2683aSChristoph Hellwig if (!reading) 161242d2683aSChristoph Hellwig memcpy(page_address(page), p, bytes); 161342d2683aSChristoph Hellwig 161442d2683aSChristoph Hellwig if (bio_add_pc_page(q, bio, page, bytes, 0) < bytes) 161542d2683aSChristoph Hellwig break; 161642d2683aSChristoph Hellwig 161742d2683aSChristoph Hellwig len -= bytes; 161842d2683aSChristoph Hellwig p += bytes; 1619f9c78b2bSJens Axboe } 1620f9c78b2bSJens Axboe 16211dfa0f68SChristoph Hellwig if (reading) { 16221dfa0f68SChristoph Hellwig bio->bi_end_io = bio_copy_kern_endio_read; 162342d2683aSChristoph Hellwig bio->bi_private = data; 16241dfa0f68SChristoph Hellwig } else { 1625f9c78b2bSJens Axboe bio->bi_end_io = bio_copy_kern_endio; 16261dfa0f68SChristoph Hellwig } 16271dfa0f68SChristoph Hellwig 1628f9c78b2bSJens Axboe return bio; 162942d2683aSChristoph Hellwig 163042d2683aSChristoph Hellwig cleanup: 16311dfa0f68SChristoph Hellwig bio_free_pages(bio); 163242d2683aSChristoph Hellwig bio_put(bio); 163342d2683aSChristoph Hellwig return ERR_PTR(-ENOMEM); 1634f9c78b2bSJens Axboe } 1635f9c78b2bSJens Axboe 1636f9c78b2bSJens Axboe /* 1637f9c78b2bSJens Axboe * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions 1638f9c78b2bSJens Axboe * for performing direct-IO in BIOs. 1639f9c78b2bSJens Axboe * 1640f9c78b2bSJens Axboe * The problem is that we cannot run set_page_dirty() from interrupt context 1641f9c78b2bSJens Axboe * because the required locks are not interrupt-safe. So what we can do is to 1642f9c78b2bSJens Axboe * mark the pages dirty _before_ performing IO. And in interrupt context, 1643f9c78b2bSJens Axboe * check that the pages are still dirty. If so, fine. If not, redirty them 1644f9c78b2bSJens Axboe * in process context. 1645f9c78b2bSJens Axboe * 1646f9c78b2bSJens Axboe * We special-case compound pages here: normally this means reads into hugetlb 1647f9c78b2bSJens Axboe * pages. The logic in here doesn't really work right for compound pages 1648f9c78b2bSJens Axboe * because the VM does not uniformly chase down the head page in all cases. 1649f9c78b2bSJens Axboe * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't 1650f9c78b2bSJens Axboe * handle them at all. So we skip compound pages here at an early stage. 1651f9c78b2bSJens Axboe * 1652f9c78b2bSJens Axboe * Note that this code is very hard to test under normal circumstances because 1653f9c78b2bSJens Axboe * direct-io pins the pages with get_user_pages(). This makes 1654f9c78b2bSJens Axboe * is_page_cache_freeable return false, and the VM will not clean the pages. 1655f9c78b2bSJens Axboe * But other code (eg, flusher threads) could clean the pages if they are mapped 1656f9c78b2bSJens Axboe * pagecache. 1657f9c78b2bSJens Axboe * 1658f9c78b2bSJens Axboe * Simply disabling the call to bio_set_pages_dirty() is a good way to test the 1659f9c78b2bSJens Axboe * deferred bio dirtying paths. 1660f9c78b2bSJens Axboe */ 1661f9c78b2bSJens Axboe 1662f9c78b2bSJens Axboe /* 1663f9c78b2bSJens Axboe * bio_set_pages_dirty() will mark all the bio's pages as dirty. 1664f9c78b2bSJens Axboe */ 1665f9c78b2bSJens Axboe void bio_set_pages_dirty(struct bio *bio) 1666f9c78b2bSJens Axboe { 1667f9c78b2bSJens Axboe struct bio_vec *bvec; 1668f9c78b2bSJens Axboe int i; 1669f9c78b2bSJens Axboe 1670f9c78b2bSJens Axboe bio_for_each_segment_all(bvec, bio, i) { 1671f9c78b2bSJens Axboe struct page *page = bvec->bv_page; 1672f9c78b2bSJens Axboe 1673f9c78b2bSJens Axboe if (page && !PageCompound(page)) 1674f9c78b2bSJens Axboe set_page_dirty_lock(page); 1675f9c78b2bSJens Axboe } 1676f9c78b2bSJens Axboe } 1677f9c78b2bSJens Axboe 1678f9c78b2bSJens Axboe static void bio_release_pages(struct bio *bio) 1679f9c78b2bSJens Axboe { 1680f9c78b2bSJens Axboe struct bio_vec *bvec; 1681f9c78b2bSJens Axboe int i; 1682f9c78b2bSJens Axboe 1683f9c78b2bSJens Axboe bio_for_each_segment_all(bvec, bio, i) { 1684f9c78b2bSJens Axboe struct page *page = bvec->bv_page; 1685f9c78b2bSJens Axboe 1686f9c78b2bSJens Axboe if (page) 1687f9c78b2bSJens Axboe put_page(page); 1688f9c78b2bSJens Axboe } 1689f9c78b2bSJens Axboe } 1690f9c78b2bSJens Axboe 1691f9c78b2bSJens Axboe /* 1692f9c78b2bSJens Axboe * bio_check_pages_dirty() will check that all the BIO's pages are still dirty. 1693f9c78b2bSJens Axboe * If they are, then fine. If, however, some pages are clean then they must 1694f9c78b2bSJens Axboe * have been written out during the direct-IO read. So we take another ref on 1695f9c78b2bSJens Axboe * the BIO and the offending pages and re-dirty the pages in process context. 1696f9c78b2bSJens Axboe * 1697f9c78b2bSJens Axboe * It is expected that bio_check_pages_dirty() will wholly own the BIO from 1698ea1754a0SKirill A. Shutemov * here on. It will run one put_page() against each page and will run one 1699ea1754a0SKirill A. Shutemov * bio_put() against the BIO. 1700f9c78b2bSJens Axboe */ 1701f9c78b2bSJens Axboe 1702f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work); 1703f9c78b2bSJens Axboe 1704f9c78b2bSJens Axboe static DECLARE_WORK(bio_dirty_work, bio_dirty_fn); 1705f9c78b2bSJens Axboe static DEFINE_SPINLOCK(bio_dirty_lock); 1706f9c78b2bSJens Axboe static struct bio *bio_dirty_list; 1707f9c78b2bSJens Axboe 1708f9c78b2bSJens Axboe /* 1709f9c78b2bSJens Axboe * This runs in process context 1710f9c78b2bSJens Axboe */ 1711f9c78b2bSJens Axboe static void bio_dirty_fn(struct work_struct *work) 1712f9c78b2bSJens Axboe { 1713f9c78b2bSJens Axboe unsigned long flags; 1714f9c78b2bSJens Axboe struct bio *bio; 1715f9c78b2bSJens Axboe 1716f9c78b2bSJens Axboe spin_lock_irqsave(&bio_dirty_lock, flags); 1717f9c78b2bSJens Axboe bio = bio_dirty_list; 1718f9c78b2bSJens Axboe bio_dirty_list = NULL; 1719f9c78b2bSJens Axboe spin_unlock_irqrestore(&bio_dirty_lock, flags); 1720f9c78b2bSJens Axboe 1721f9c78b2bSJens Axboe while (bio) { 1722f9c78b2bSJens Axboe struct bio *next = bio->bi_private; 1723f9c78b2bSJens Axboe 1724f9c78b2bSJens Axboe bio_set_pages_dirty(bio); 1725f9c78b2bSJens Axboe bio_release_pages(bio); 1726f9c78b2bSJens Axboe bio_put(bio); 1727f9c78b2bSJens Axboe bio = next; 1728f9c78b2bSJens Axboe } 1729f9c78b2bSJens Axboe } 1730f9c78b2bSJens Axboe 1731f9c78b2bSJens Axboe void bio_check_pages_dirty(struct bio *bio) 1732f9c78b2bSJens Axboe { 1733f9c78b2bSJens Axboe struct bio_vec *bvec; 1734f9c78b2bSJens Axboe int nr_clean_pages = 0; 1735f9c78b2bSJens Axboe int i; 1736f9c78b2bSJens Axboe 1737f9c78b2bSJens Axboe bio_for_each_segment_all(bvec, bio, i) { 1738f9c78b2bSJens Axboe struct page *page = bvec->bv_page; 1739f9c78b2bSJens Axboe 1740f9c78b2bSJens Axboe if (PageDirty(page) || PageCompound(page)) { 174109cbfeafSKirill A. Shutemov put_page(page); 1742f9c78b2bSJens Axboe bvec->bv_page = NULL; 1743f9c78b2bSJens Axboe } else { 1744f9c78b2bSJens Axboe nr_clean_pages++; 1745f9c78b2bSJens Axboe } 1746f9c78b2bSJens Axboe } 1747f9c78b2bSJens Axboe 1748f9c78b2bSJens Axboe if (nr_clean_pages) { 1749f9c78b2bSJens Axboe unsigned long flags; 1750f9c78b2bSJens Axboe 1751f9c78b2bSJens Axboe spin_lock_irqsave(&bio_dirty_lock, flags); 1752f9c78b2bSJens Axboe bio->bi_private = bio_dirty_list; 1753f9c78b2bSJens Axboe bio_dirty_list = bio; 1754f9c78b2bSJens Axboe spin_unlock_irqrestore(&bio_dirty_lock, flags); 1755f9c78b2bSJens Axboe schedule_work(&bio_dirty_work); 1756f9c78b2bSJens Axboe } else { 1757f9c78b2bSJens Axboe bio_put(bio); 1758f9c78b2bSJens Axboe } 1759f9c78b2bSJens Axboe } 1760f9c78b2bSJens Axboe 1761394ffa50SGu Zheng void generic_start_io_acct(int rw, unsigned long sectors, 1762394ffa50SGu Zheng struct hd_struct *part) 1763394ffa50SGu Zheng { 1764394ffa50SGu Zheng int cpu = part_stat_lock(); 1765394ffa50SGu Zheng 1766394ffa50SGu Zheng part_round_stats(cpu, part); 1767394ffa50SGu Zheng part_stat_inc(cpu, part, ios[rw]); 1768394ffa50SGu Zheng part_stat_add(cpu, part, sectors[rw], sectors); 1769394ffa50SGu Zheng part_inc_in_flight(part, rw); 1770394ffa50SGu Zheng 1771394ffa50SGu Zheng part_stat_unlock(); 1772394ffa50SGu Zheng } 1773394ffa50SGu Zheng EXPORT_SYMBOL(generic_start_io_acct); 1774394ffa50SGu Zheng 1775394ffa50SGu Zheng void generic_end_io_acct(int rw, struct hd_struct *part, 1776394ffa50SGu Zheng unsigned long start_time) 1777394ffa50SGu Zheng { 1778394ffa50SGu Zheng unsigned long duration = jiffies - start_time; 1779394ffa50SGu Zheng int cpu = part_stat_lock(); 1780394ffa50SGu Zheng 1781394ffa50SGu Zheng part_stat_add(cpu, part, ticks[rw], duration); 1782394ffa50SGu Zheng part_round_stats(cpu, part); 1783394ffa50SGu Zheng part_dec_in_flight(part, rw); 1784394ffa50SGu Zheng 1785394ffa50SGu Zheng part_stat_unlock(); 1786394ffa50SGu Zheng } 1787394ffa50SGu Zheng EXPORT_SYMBOL(generic_end_io_acct); 1788394ffa50SGu Zheng 1789f9c78b2bSJens Axboe #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1790f9c78b2bSJens Axboe void bio_flush_dcache_pages(struct bio *bi) 1791f9c78b2bSJens Axboe { 1792f9c78b2bSJens Axboe struct bio_vec bvec; 1793f9c78b2bSJens Axboe struct bvec_iter iter; 1794f9c78b2bSJens Axboe 1795f9c78b2bSJens Axboe bio_for_each_segment(bvec, bi, iter) 1796f9c78b2bSJens Axboe flush_dcache_page(bvec.bv_page); 1797f9c78b2bSJens Axboe } 1798f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_flush_dcache_pages); 1799f9c78b2bSJens Axboe #endif 1800f9c78b2bSJens Axboe 1801c4cf5261SJens Axboe static inline bool bio_remaining_done(struct bio *bio) 1802c4cf5261SJens Axboe { 1803c4cf5261SJens Axboe /* 1804c4cf5261SJens Axboe * If we're not chaining, then ->__bi_remaining is always 1 and 1805c4cf5261SJens Axboe * we always end io on the first invocation. 1806c4cf5261SJens Axboe */ 1807c4cf5261SJens Axboe if (!bio_flagged(bio, BIO_CHAIN)) 1808c4cf5261SJens Axboe return true; 1809c4cf5261SJens Axboe 1810c4cf5261SJens Axboe BUG_ON(atomic_read(&bio->__bi_remaining) <= 0); 1811c4cf5261SJens Axboe 1812326e1dbbSMike Snitzer if (atomic_dec_and_test(&bio->__bi_remaining)) { 1813b7c44ed9SJens Axboe bio_clear_flag(bio, BIO_CHAIN); 1814c4cf5261SJens Axboe return true; 1815326e1dbbSMike Snitzer } 1816c4cf5261SJens Axboe 1817c4cf5261SJens Axboe return false; 1818c4cf5261SJens Axboe } 1819c4cf5261SJens Axboe 1820f9c78b2bSJens Axboe /** 1821f9c78b2bSJens Axboe * bio_endio - end I/O on a bio 1822f9c78b2bSJens Axboe * @bio: bio 1823f9c78b2bSJens Axboe * 1824f9c78b2bSJens Axboe * Description: 18254246a0b6SChristoph Hellwig * bio_endio() will end I/O on the whole bio. bio_endio() is the preferred 18264246a0b6SChristoph Hellwig * way to end I/O on a bio. No one should call bi_end_io() directly on a 18274246a0b6SChristoph Hellwig * bio unless they own it and thus know that it has an end_io function. 1828f9c78b2bSJens Axboe **/ 18294246a0b6SChristoph Hellwig void bio_endio(struct bio *bio) 1830f9c78b2bSJens Axboe { 1831ba8c6967SChristoph Hellwig again: 18322b885517SChristoph Hellwig if (!bio_remaining_done(bio)) 1833ba8c6967SChristoph Hellwig return; 1834f9c78b2bSJens Axboe 1835f9c78b2bSJens Axboe /* 1836ba8c6967SChristoph Hellwig * Need to have a real endio function for chained bios, otherwise 1837ba8c6967SChristoph Hellwig * various corner cases will break (like stacking block devices that 1838ba8c6967SChristoph Hellwig * save/restore bi_end_io) - however, we want to avoid unbounded 1839ba8c6967SChristoph Hellwig * recursion and blowing the stack. Tail call optimization would 1840ba8c6967SChristoph Hellwig * handle this, but compiling with frame pointers also disables 1841ba8c6967SChristoph Hellwig * gcc's sibling call optimization. 1842f9c78b2bSJens Axboe */ 1843f9c78b2bSJens Axboe if (bio->bi_end_io == bio_chain_endio) { 184438f8baaeSChristoph Hellwig bio = __bio_chain_endio(bio); 1845ba8c6967SChristoph Hellwig goto again; 1846ba8c6967SChristoph Hellwig } 1847ba8c6967SChristoph Hellwig 1848f9c78b2bSJens Axboe if (bio->bi_end_io) 18494246a0b6SChristoph Hellwig bio->bi_end_io(bio); 1850f9c78b2bSJens Axboe } 1851f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_endio); 1852f9c78b2bSJens Axboe 1853f9c78b2bSJens Axboe /** 1854f9c78b2bSJens Axboe * bio_split - split a bio 1855f9c78b2bSJens Axboe * @bio: bio to split 1856f9c78b2bSJens Axboe * @sectors: number of sectors to split from the front of @bio 1857f9c78b2bSJens Axboe * @gfp: gfp mask 1858f9c78b2bSJens Axboe * @bs: bio set to allocate from 1859f9c78b2bSJens Axboe * 1860f9c78b2bSJens Axboe * Allocates and returns a new bio which represents @sectors from the start of 1861f9c78b2bSJens Axboe * @bio, and updates @bio to represent the remaining sectors. 1862f9c78b2bSJens Axboe * 1863f3f5da62SMartin K. Petersen * Unless this is a discard request the newly allocated bio will point 1864f3f5da62SMartin K. Petersen * to @bio's bi_io_vec; it is the caller's responsibility to ensure that 1865f3f5da62SMartin K. Petersen * @bio is not freed before the split. 1866f9c78b2bSJens Axboe */ 1867f9c78b2bSJens Axboe struct bio *bio_split(struct bio *bio, int sectors, 1868f9c78b2bSJens Axboe gfp_t gfp, struct bio_set *bs) 1869f9c78b2bSJens Axboe { 1870f9c78b2bSJens Axboe struct bio *split = NULL; 1871f9c78b2bSJens Axboe 1872f9c78b2bSJens Axboe BUG_ON(sectors <= 0); 1873f9c78b2bSJens Axboe BUG_ON(sectors >= bio_sectors(bio)); 1874f9c78b2bSJens Axboe 1875f9c78b2bSJens Axboe split = bio_clone_fast(bio, gfp, bs); 1876f9c78b2bSJens Axboe if (!split) 1877f9c78b2bSJens Axboe return NULL; 1878f9c78b2bSJens Axboe 1879f9c78b2bSJens Axboe split->bi_iter.bi_size = sectors << 9; 1880f9c78b2bSJens Axboe 1881f9c78b2bSJens Axboe if (bio_integrity(split)) 1882f9c78b2bSJens Axboe bio_integrity_trim(split, 0, sectors); 1883f9c78b2bSJens Axboe 1884f9c78b2bSJens Axboe bio_advance(bio, split->bi_iter.bi_size); 1885f9c78b2bSJens Axboe 1886f9c78b2bSJens Axboe return split; 1887f9c78b2bSJens Axboe } 1888f9c78b2bSJens Axboe EXPORT_SYMBOL(bio_split); 1889f9c78b2bSJens Axboe 1890f9c78b2bSJens Axboe /** 1891f9c78b2bSJens Axboe * bio_trim - trim a bio 1892f9c78b2bSJens Axboe * @bio: bio to trim 1893f9c78b2bSJens Axboe * @offset: number of sectors to trim from the front of @bio 1894f9c78b2bSJens Axboe * @size: size we want to trim @bio to, in sectors 1895f9c78b2bSJens Axboe */ 1896f9c78b2bSJens Axboe void bio_trim(struct bio *bio, int offset, int size) 1897f9c78b2bSJens Axboe { 1898f9c78b2bSJens Axboe /* 'bio' is a cloned bio which we need to trim to match 1899f9c78b2bSJens Axboe * the given offset and size. 1900f9c78b2bSJens Axboe */ 1901f9c78b2bSJens Axboe 1902f9c78b2bSJens Axboe size <<= 9; 1903f9c78b2bSJens Axboe if (offset == 0 && size == bio->bi_iter.bi_size) 1904f9c78b2bSJens Axboe return; 1905f9c78b2bSJens Axboe 1906b7c44ed9SJens Axboe bio_clear_flag(bio, BIO_SEG_VALID); 1907f9c78b2bSJens Axboe 1908f9c78b2bSJens Axboe bio_advance(bio, offset << 9); 1909f9c78b2bSJens Axboe 1910f9c78b2bSJens Axboe bio->bi_iter.bi_size = size; 1911f9c78b2bSJens Axboe } 1912f9c78b2bSJens Axboe EXPORT_SYMBOL_GPL(bio_trim); 1913f9c78b2bSJens Axboe 1914f9c78b2bSJens Axboe /* 1915f9c78b2bSJens Axboe * create memory pools for biovec's in a bio_set. 1916f9c78b2bSJens Axboe * use the global biovec slabs created for general use. 1917f9c78b2bSJens Axboe */ 1918f9c78b2bSJens Axboe mempool_t *biovec_create_pool(int pool_entries) 1919f9c78b2bSJens Axboe { 1920ed996a52SChristoph Hellwig struct biovec_slab *bp = bvec_slabs + BVEC_POOL_MAX; 1921f9c78b2bSJens Axboe 1922f9c78b2bSJens Axboe return mempool_create_slab_pool(pool_entries, bp->slab); 1923f9c78b2bSJens Axboe } 1924f9c78b2bSJens Axboe 1925f9c78b2bSJens Axboe void bioset_free(struct bio_set *bs) 1926f9c78b2bSJens Axboe { 1927f9c78b2bSJens Axboe if (bs->rescue_workqueue) 1928f9c78b2bSJens Axboe destroy_workqueue(bs->rescue_workqueue); 1929f9c78b2bSJens Axboe 1930f9c78b2bSJens Axboe if (bs->bio_pool) 1931f9c78b2bSJens Axboe mempool_destroy(bs->bio_pool); 1932f9c78b2bSJens Axboe 1933f9c78b2bSJens Axboe if (bs->bvec_pool) 1934f9c78b2bSJens Axboe mempool_destroy(bs->bvec_pool); 1935f9c78b2bSJens Axboe 1936f9c78b2bSJens Axboe bioset_integrity_free(bs); 1937f9c78b2bSJens Axboe bio_put_slab(bs); 1938f9c78b2bSJens Axboe 1939f9c78b2bSJens Axboe kfree(bs); 1940f9c78b2bSJens Axboe } 1941f9c78b2bSJens Axboe EXPORT_SYMBOL(bioset_free); 1942f9c78b2bSJens Axboe 1943d8f429e1SJunichi Nomura static struct bio_set *__bioset_create(unsigned int pool_size, 1944d8f429e1SJunichi Nomura unsigned int front_pad, 1945d8f429e1SJunichi Nomura bool create_bvec_pool) 1946f9c78b2bSJens Axboe { 1947f9c78b2bSJens Axboe unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec); 1948f9c78b2bSJens Axboe struct bio_set *bs; 1949f9c78b2bSJens Axboe 1950f9c78b2bSJens Axboe bs = kzalloc(sizeof(*bs), GFP_KERNEL); 1951f9c78b2bSJens Axboe if (!bs) 1952f9c78b2bSJens Axboe return NULL; 1953f9c78b2bSJens Axboe 1954f9c78b2bSJens Axboe bs->front_pad = front_pad; 1955f9c78b2bSJens Axboe 1956f9c78b2bSJens Axboe spin_lock_init(&bs->rescue_lock); 1957f9c78b2bSJens Axboe bio_list_init(&bs->rescue_list); 1958f9c78b2bSJens Axboe INIT_WORK(&bs->rescue_work, bio_alloc_rescue); 1959f9c78b2bSJens Axboe 1960f9c78b2bSJens Axboe bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad); 1961f9c78b2bSJens Axboe if (!bs->bio_slab) { 1962f9c78b2bSJens Axboe kfree(bs); 1963f9c78b2bSJens Axboe return NULL; 1964f9c78b2bSJens Axboe } 1965f9c78b2bSJens Axboe 1966f9c78b2bSJens Axboe bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab); 1967f9c78b2bSJens Axboe if (!bs->bio_pool) 1968f9c78b2bSJens Axboe goto bad; 1969f9c78b2bSJens Axboe 1970d8f429e1SJunichi Nomura if (create_bvec_pool) { 1971f9c78b2bSJens Axboe bs->bvec_pool = biovec_create_pool(pool_size); 1972f9c78b2bSJens Axboe if (!bs->bvec_pool) 1973f9c78b2bSJens Axboe goto bad; 1974d8f429e1SJunichi Nomura } 1975f9c78b2bSJens Axboe 1976f9c78b2bSJens Axboe bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0); 1977f9c78b2bSJens Axboe if (!bs->rescue_workqueue) 1978f9c78b2bSJens Axboe goto bad; 1979f9c78b2bSJens Axboe 1980f9c78b2bSJens Axboe return bs; 1981f9c78b2bSJens Axboe bad: 1982f9c78b2bSJens Axboe bioset_free(bs); 1983f9c78b2bSJens Axboe return NULL; 1984f9c78b2bSJens Axboe } 1985d8f429e1SJunichi Nomura 1986d8f429e1SJunichi Nomura /** 1987d8f429e1SJunichi Nomura * bioset_create - Create a bio_set 1988d8f429e1SJunichi Nomura * @pool_size: Number of bio and bio_vecs to cache in the mempool 1989d8f429e1SJunichi Nomura * @front_pad: Number of bytes to allocate in front of the returned bio 1990d8f429e1SJunichi Nomura * 1991d8f429e1SJunichi Nomura * Description: 1992d8f429e1SJunichi Nomura * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller 1993d8f429e1SJunichi Nomura * to ask for a number of bytes to be allocated in front of the bio. 1994d8f429e1SJunichi Nomura * Front pad allocation is useful for embedding the bio inside 1995d8f429e1SJunichi Nomura * another structure, to avoid allocating extra data to go with the bio. 1996d8f429e1SJunichi Nomura * Note that the bio must be embedded at the END of that structure always, 1997d8f429e1SJunichi Nomura * or things will break badly. 1998d8f429e1SJunichi Nomura */ 1999d8f429e1SJunichi Nomura struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad) 2000d8f429e1SJunichi Nomura { 2001d8f429e1SJunichi Nomura return __bioset_create(pool_size, front_pad, true); 2002d8f429e1SJunichi Nomura } 2003f9c78b2bSJens Axboe EXPORT_SYMBOL(bioset_create); 2004f9c78b2bSJens Axboe 2005d8f429e1SJunichi Nomura /** 2006d8f429e1SJunichi Nomura * bioset_create_nobvec - Create a bio_set without bio_vec mempool 2007d8f429e1SJunichi Nomura * @pool_size: Number of bio to cache in the mempool 2008d8f429e1SJunichi Nomura * @front_pad: Number of bytes to allocate in front of the returned bio 2009d8f429e1SJunichi Nomura * 2010d8f429e1SJunichi Nomura * Description: 2011d8f429e1SJunichi Nomura * Same functionality as bioset_create() except that mempool is not 2012d8f429e1SJunichi Nomura * created for bio_vecs. Saving some memory for bio_clone_fast() users. 2013d8f429e1SJunichi Nomura */ 2014d8f429e1SJunichi Nomura struct bio_set *bioset_create_nobvec(unsigned int pool_size, unsigned int front_pad) 2015d8f429e1SJunichi Nomura { 2016d8f429e1SJunichi Nomura return __bioset_create(pool_size, front_pad, false); 2017d8f429e1SJunichi Nomura } 2018d8f429e1SJunichi Nomura EXPORT_SYMBOL(bioset_create_nobvec); 2019d8f429e1SJunichi Nomura 2020f9c78b2bSJens Axboe #ifdef CONFIG_BLK_CGROUP 20211d933cf0STejun Heo 20221d933cf0STejun Heo /** 20231d933cf0STejun Heo * bio_associate_blkcg - associate a bio with the specified blkcg 20241d933cf0STejun Heo * @bio: target bio 20251d933cf0STejun Heo * @blkcg_css: css of the blkcg to associate 20261d933cf0STejun Heo * 20271d933cf0STejun Heo * Associate @bio with the blkcg specified by @blkcg_css. Block layer will 20281d933cf0STejun Heo * treat @bio as if it were issued by a task which belongs to the blkcg. 20291d933cf0STejun Heo * 20301d933cf0STejun Heo * This function takes an extra reference of @blkcg_css which will be put 20311d933cf0STejun Heo * when @bio is released. The caller must own @bio and is responsible for 20321d933cf0STejun Heo * synchronizing calls to this function. 20331d933cf0STejun Heo */ 20341d933cf0STejun Heo int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css) 20351d933cf0STejun Heo { 20361d933cf0STejun Heo if (unlikely(bio->bi_css)) 20371d933cf0STejun Heo return -EBUSY; 20381d933cf0STejun Heo css_get(blkcg_css); 20391d933cf0STejun Heo bio->bi_css = blkcg_css; 20401d933cf0STejun Heo return 0; 20411d933cf0STejun Heo } 20425aa2a96bSTejun Heo EXPORT_SYMBOL_GPL(bio_associate_blkcg); 20431d933cf0STejun Heo 2044f9c78b2bSJens Axboe /** 2045f9c78b2bSJens Axboe * bio_associate_current - associate a bio with %current 2046f9c78b2bSJens Axboe * @bio: target bio 2047f9c78b2bSJens Axboe * 2048f9c78b2bSJens Axboe * Associate @bio with %current if it hasn't been associated yet. Block 2049f9c78b2bSJens Axboe * layer will treat @bio as if it were issued by %current no matter which 2050f9c78b2bSJens Axboe * task actually issues it. 2051f9c78b2bSJens Axboe * 2052f9c78b2bSJens Axboe * This function takes an extra reference of @task's io_context and blkcg 2053f9c78b2bSJens Axboe * which will be put when @bio is released. The caller must own @bio, 2054f9c78b2bSJens Axboe * ensure %current->io_context exists, and is responsible for synchronizing 2055f9c78b2bSJens Axboe * calls to this function. 2056f9c78b2bSJens Axboe */ 2057f9c78b2bSJens Axboe int bio_associate_current(struct bio *bio) 2058f9c78b2bSJens Axboe { 2059f9c78b2bSJens Axboe struct io_context *ioc; 2060f9c78b2bSJens Axboe 20611d933cf0STejun Heo if (bio->bi_css) 2062f9c78b2bSJens Axboe return -EBUSY; 2063f9c78b2bSJens Axboe 2064f9c78b2bSJens Axboe ioc = current->io_context; 2065f9c78b2bSJens Axboe if (!ioc) 2066f9c78b2bSJens Axboe return -ENOENT; 2067f9c78b2bSJens Axboe 2068f9c78b2bSJens Axboe get_io_context_active(ioc); 2069f9c78b2bSJens Axboe bio->bi_ioc = ioc; 2070c165b3e3STejun Heo bio->bi_css = task_get_css(current, io_cgrp_id); 2071f9c78b2bSJens Axboe return 0; 2072f9c78b2bSJens Axboe } 20735aa2a96bSTejun Heo EXPORT_SYMBOL_GPL(bio_associate_current); 2074f9c78b2bSJens Axboe 2075f9c78b2bSJens Axboe /** 2076f9c78b2bSJens Axboe * bio_disassociate_task - undo bio_associate_current() 2077f9c78b2bSJens Axboe * @bio: target bio 2078f9c78b2bSJens Axboe */ 2079f9c78b2bSJens Axboe void bio_disassociate_task(struct bio *bio) 2080f9c78b2bSJens Axboe { 2081f9c78b2bSJens Axboe if (bio->bi_ioc) { 2082f9c78b2bSJens Axboe put_io_context(bio->bi_ioc); 2083f9c78b2bSJens Axboe bio->bi_ioc = NULL; 2084f9c78b2bSJens Axboe } 2085f9c78b2bSJens Axboe if (bio->bi_css) { 2086f9c78b2bSJens Axboe css_put(bio->bi_css); 2087f9c78b2bSJens Axboe bio->bi_css = NULL; 2088f9c78b2bSJens Axboe } 2089f9c78b2bSJens Axboe } 2090f9c78b2bSJens Axboe 209120bd723eSPaolo Valente /** 209220bd723eSPaolo Valente * bio_clone_blkcg_association - clone blkcg association from src to dst bio 209320bd723eSPaolo Valente * @dst: destination bio 209420bd723eSPaolo Valente * @src: source bio 209520bd723eSPaolo Valente */ 209620bd723eSPaolo Valente void bio_clone_blkcg_association(struct bio *dst, struct bio *src) 209720bd723eSPaolo Valente { 209820bd723eSPaolo Valente if (src->bi_css) 209920bd723eSPaolo Valente WARN_ON(bio_associate_blkcg(dst, src->bi_css)); 210020bd723eSPaolo Valente } 210120bd723eSPaolo Valente 2102f9c78b2bSJens Axboe #endif /* CONFIG_BLK_CGROUP */ 2103f9c78b2bSJens Axboe 2104f9c78b2bSJens Axboe static void __init biovec_init_slabs(void) 2105f9c78b2bSJens Axboe { 2106f9c78b2bSJens Axboe int i; 2107f9c78b2bSJens Axboe 2108ed996a52SChristoph Hellwig for (i = 0; i < BVEC_POOL_NR; i++) { 2109f9c78b2bSJens Axboe int size; 2110f9c78b2bSJens Axboe struct biovec_slab *bvs = bvec_slabs + i; 2111f9c78b2bSJens Axboe 2112f9c78b2bSJens Axboe if (bvs->nr_vecs <= BIO_INLINE_VECS) { 2113f9c78b2bSJens Axboe bvs->slab = NULL; 2114f9c78b2bSJens Axboe continue; 2115f9c78b2bSJens Axboe } 2116f9c78b2bSJens Axboe 2117f9c78b2bSJens Axboe size = bvs->nr_vecs * sizeof(struct bio_vec); 2118f9c78b2bSJens Axboe bvs->slab = kmem_cache_create(bvs->name, size, 0, 2119f9c78b2bSJens Axboe SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL); 2120f9c78b2bSJens Axboe } 2121f9c78b2bSJens Axboe } 2122f9c78b2bSJens Axboe 2123f9c78b2bSJens Axboe static int __init init_bio(void) 2124f9c78b2bSJens Axboe { 2125f9c78b2bSJens Axboe bio_slab_max = 2; 2126f9c78b2bSJens Axboe bio_slab_nr = 0; 2127f9c78b2bSJens Axboe bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL); 2128f9c78b2bSJens Axboe if (!bio_slabs) 2129f9c78b2bSJens Axboe panic("bio: can't allocate bios\n"); 2130f9c78b2bSJens Axboe 2131f9c78b2bSJens Axboe bio_integrity_init(); 2132f9c78b2bSJens Axboe biovec_init_slabs(); 2133f9c78b2bSJens Axboe 2134f9c78b2bSJens Axboe fs_bio_set = bioset_create(BIO_POOL_SIZE, 0); 2135f9c78b2bSJens Axboe if (!fs_bio_set) 2136f9c78b2bSJens Axboe panic("bio: can't allocate bios\n"); 2137f9c78b2bSJens Axboe 2138f9c78b2bSJens Axboe if (bioset_integrity_create(fs_bio_set, BIO_POOL_SIZE)) 2139f9c78b2bSJens Axboe panic("bio: can't create integrity pool\n"); 2140f9c78b2bSJens Axboe 2141f9c78b2bSJens Axboe return 0; 2142f9c78b2bSJens Axboe } 2143f9c78b2bSJens Axboe subsys_initcall(init_bio); 2144