bio.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. /*
  2. * Copyright (C) 2001 Jens Axboe <axboe@kernel.dk>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public Licens
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  16. *
  17. */
  18. #include <linux/mm.h>
  19. #include <linux/swap.h>
  20. #include <linux/bio.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/uio.h>
  23. #include <linux/iocontext.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/export.h>
  28. #include <linux/mempool.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/cgroup.h>
  31. #include <scsi/sg.h> /* for struct sg_iovec */
  32. #include <trace/events/block.h>
  33. /*
  34. * Test patch to inline a certain number of bi_io_vec's inside the bio
  35. * itself, to shrink a bio data allocation from two mempool calls to one
  36. */
  37. #define BIO_INLINE_VECS 4
  38. static mempool_t *bio_split_pool __read_mostly;
  39. /*
  40. * if you change this list, also change bvec_alloc or things will
  41. * break badly! cannot be bigger than what you can fit into an
  42. * unsigned short
  43. */
  44. #define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
  45. static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
  46. BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
  47. };
  48. #undef BV
  49. /*
  50. * fs_bio_set is the bio_set containing bio and iovec memory pools used by
  51. * IO code that does not need private memory pools.
  52. */
  53. struct bio_set *fs_bio_set;
  54. EXPORT_SYMBOL(fs_bio_set);
  55. /*
  56. * Our slab pool management
  57. */
  58. struct bio_slab {
  59. struct kmem_cache *slab;
  60. unsigned int slab_ref;
  61. unsigned int slab_size;
  62. char name[8];
  63. };
  64. static DEFINE_MUTEX(bio_slab_lock);
  65. static struct bio_slab *bio_slabs;
  66. static unsigned int bio_slab_nr, bio_slab_max;
  67. static struct kmem_cache *bio_find_or_create_slab(unsigned int extra_size)
  68. {
  69. unsigned int sz = sizeof(struct bio) + extra_size;
  70. struct kmem_cache *slab = NULL;
  71. struct bio_slab *bslab, *new_bio_slabs;
  72. unsigned int new_bio_slab_max;
  73. unsigned int i, entry = -1;
  74. mutex_lock(&bio_slab_lock);
  75. i = 0;
  76. while (i < bio_slab_nr) {
  77. bslab = &bio_slabs[i];
  78. if (!bslab->slab && entry == -1)
  79. entry = i;
  80. else if (bslab->slab_size == sz) {
  81. slab = bslab->slab;
  82. bslab->slab_ref++;
  83. break;
  84. }
  85. i++;
  86. }
  87. if (slab)
  88. goto out_unlock;
  89. if (bio_slab_nr == bio_slab_max && entry == -1) {
  90. new_bio_slab_max = bio_slab_max << 1;
  91. new_bio_slabs = krealloc(bio_slabs,
  92. new_bio_slab_max * sizeof(struct bio_slab),
  93. GFP_KERNEL);
  94. if (!new_bio_slabs)
  95. goto out_unlock;
  96. bio_slab_max = new_bio_slab_max;
  97. bio_slabs = new_bio_slabs;
  98. }
  99. if (entry == -1)
  100. entry = bio_slab_nr++;
  101. bslab = &bio_slabs[entry];
  102. snprintf(bslab->name, sizeof(bslab->name), "bio-%d", entry);
  103. slab = kmem_cache_create(bslab->name, sz, 0, SLAB_HWCACHE_ALIGN, NULL);
  104. if (!slab)
  105. goto out_unlock;
  106. printk(KERN_INFO "bio: create slab <%s> at %d\n", bslab->name, entry);
  107. bslab->slab = slab;
  108. bslab->slab_ref = 1;
  109. bslab->slab_size = sz;
  110. out_unlock:
  111. mutex_unlock(&bio_slab_lock);
  112. return slab;
  113. }
  114. static void bio_put_slab(struct bio_set *bs)
  115. {
  116. struct bio_slab *bslab = NULL;
  117. unsigned int i;
  118. mutex_lock(&bio_slab_lock);
  119. for (i = 0; i < bio_slab_nr; i++) {
  120. if (bs->bio_slab == bio_slabs[i].slab) {
  121. bslab = &bio_slabs[i];
  122. break;
  123. }
  124. }
  125. if (WARN(!bslab, KERN_ERR "bio: unable to find slab!\n"))
  126. goto out;
  127. WARN_ON(!bslab->slab_ref);
  128. if (--bslab->slab_ref)
  129. goto out;
  130. kmem_cache_destroy(bslab->slab);
  131. bslab->slab = NULL;
  132. out:
  133. mutex_unlock(&bio_slab_lock);
  134. }
  135. unsigned int bvec_nr_vecs(unsigned short idx)
  136. {
  137. return bvec_slabs[idx].nr_vecs;
  138. }
  139. void bvec_free(mempool_t *pool, struct bio_vec *bv, unsigned int idx)
  140. {
  141. BIO_BUG_ON(idx >= BIOVEC_NR_POOLS);
  142. if (idx == BIOVEC_MAX_IDX)
  143. mempool_free(bv, pool);
  144. else {
  145. struct biovec_slab *bvs = bvec_slabs + idx;
  146. kmem_cache_free(bvs->slab, bv);
  147. }
  148. }
  149. struct bio_vec *bvec_alloc(gfp_t gfp_mask, int nr, unsigned long *idx,
  150. mempool_t *pool)
  151. {
  152. struct bio_vec *bvl;
  153. /*
  154. * see comment near bvec_array define!
  155. */
  156. switch (nr) {
  157. case 1:
  158. *idx = 0;
  159. break;
  160. case 2 ... 4:
  161. *idx = 1;
  162. break;
  163. case 5 ... 16:
  164. *idx = 2;
  165. break;
  166. case 17 ... 64:
  167. *idx = 3;
  168. break;
  169. case 65 ... 128:
  170. *idx = 4;
  171. break;
  172. case 129 ... BIO_MAX_PAGES:
  173. *idx = 5;
  174. break;
  175. default:
  176. return NULL;
  177. }
  178. /*
  179. * idx now points to the pool we want to allocate from. only the
  180. * 1-vec entry pool is mempool backed.
  181. */
  182. if (*idx == BIOVEC_MAX_IDX) {
  183. fallback:
  184. bvl = mempool_alloc(pool, gfp_mask);
  185. } else {
  186. struct biovec_slab *bvs = bvec_slabs + *idx;
  187. gfp_t __gfp_mask = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
  188. /*
  189. * Make this allocation restricted and don't dump info on
  190. * allocation failures, since we'll fallback to the mempool
  191. * in case of failure.
  192. */
  193. __gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
  194. /*
  195. * Try a slab allocation. If this fails and __GFP_WAIT
  196. * is set, retry with the 1-entry mempool
  197. */
  198. bvl = kmem_cache_alloc(bvs->slab, __gfp_mask);
  199. if (unlikely(!bvl && (gfp_mask & __GFP_WAIT))) {
  200. *idx = BIOVEC_MAX_IDX;
  201. goto fallback;
  202. }
  203. }
  204. return bvl;
  205. }
  206. static void __bio_free(struct bio *bio)
  207. {
  208. bio_disassociate_task(bio);
  209. if (bio_integrity(bio))
  210. bio_integrity_free(bio);
  211. }
  212. static void bio_free(struct bio *bio)
  213. {
  214. struct bio_set *bs = bio->bi_pool;
  215. void *p;
  216. __bio_free(bio);
  217. if (bs) {
  218. if (bio_flagged(bio, BIO_OWNS_VEC))
  219. bvec_free(bs->bvec_pool, bio->bi_io_vec, BIO_POOL_IDX(bio));
  220. /*
  221. * If we have front padding, adjust the bio pointer before freeing
  222. */
  223. p = bio;
  224. p -= bs->front_pad;
  225. mempool_free(p, bs->bio_pool);
  226. } else {
  227. /* Bio was allocated by bio_kmalloc() */
  228. kfree(bio);
  229. }
  230. }
  231. void bio_init(struct bio *bio)
  232. {
  233. memset(bio, 0, sizeof(*bio));
  234. bio->bi_flags = 1 << BIO_UPTODATE;
  235. atomic_set(&bio->bi_cnt, 1);
  236. }
  237. EXPORT_SYMBOL(bio_init);
  238. /**
  239. * bio_reset - reinitialize a bio
  240. * @bio: bio to reset
  241. *
  242. * Description:
  243. * After calling bio_reset(), @bio will be in the same state as a freshly
  244. * allocated bio returned bio bio_alloc_bioset() - the only fields that are
  245. * preserved are the ones that are initialized by bio_alloc_bioset(). See
  246. * comment in struct bio.
  247. */
  248. void bio_reset(struct bio *bio)
  249. {
  250. unsigned long flags = bio->bi_flags & (~0UL << BIO_RESET_BITS);
  251. __bio_free(bio);
  252. memset(bio, 0, BIO_RESET_BYTES);
  253. bio->bi_flags = flags|(1 << BIO_UPTODATE);
  254. }
  255. EXPORT_SYMBOL(bio_reset);
  256. static void bio_alloc_rescue(struct work_struct *work)
  257. {
  258. struct bio_set *bs = container_of(work, struct bio_set, rescue_work);
  259. struct bio *bio;
  260. while (1) {
  261. spin_lock(&bs->rescue_lock);
  262. bio = bio_list_pop(&bs->rescue_list);
  263. spin_unlock(&bs->rescue_lock);
  264. if (!bio)
  265. break;
  266. generic_make_request(bio);
  267. }
  268. }
  269. static void punt_bios_to_rescuer(struct bio_set *bs)
  270. {
  271. struct bio_list punt, nopunt;
  272. struct bio *bio;
  273. /*
  274. * In order to guarantee forward progress we must punt only bios that
  275. * were allocated from this bio_set; otherwise, if there was a bio on
  276. * there for a stacking driver higher up in the stack, processing it
  277. * could require allocating bios from this bio_set, and doing that from
  278. * our own rescuer would be bad.
  279. *
  280. * Since bio lists are singly linked, pop them all instead of trying to
  281. * remove from the middle of the list:
  282. */
  283. bio_list_init(&punt);
  284. bio_list_init(&nopunt);
  285. while ((bio = bio_list_pop(current->bio_list)))
  286. bio_list_add(bio->bi_pool == bs ? &punt : &nopunt, bio);
  287. *current->bio_list = nopunt;
  288. spin_lock(&bs->rescue_lock);
  289. bio_list_merge(&bs->rescue_list, &punt);
  290. spin_unlock(&bs->rescue_lock);
  291. queue_work(bs->rescue_workqueue, &bs->rescue_work);
  292. }
  293. /**
  294. * bio_alloc_bioset - allocate a bio for I/O
  295. * @gfp_mask: the GFP_ mask given to the slab allocator
  296. * @nr_iovecs: number of iovecs to pre-allocate
  297. * @bs: the bio_set to allocate from.
  298. *
  299. * Description:
  300. * If @bs is NULL, uses kmalloc() to allocate the bio; else the allocation is
  301. * backed by the @bs's mempool.
  302. *
  303. * When @bs is not NULL, if %__GFP_WAIT is set then bio_alloc will always be
  304. * able to allocate a bio. This is due to the mempool guarantees. To make this
  305. * work, callers must never allocate more than 1 bio at a time from this pool.
  306. * Callers that need to allocate more than 1 bio must always submit the
  307. * previously allocated bio for IO before attempting to allocate a new one.
  308. * Failure to do so can cause deadlocks under memory pressure.
  309. *
  310. * Note that when running under generic_make_request() (i.e. any block
  311. * driver), bios are not submitted until after you return - see the code in
  312. * generic_make_request() that converts recursion into iteration, to prevent
  313. * stack overflows.
  314. *
  315. * This would normally mean allocating multiple bios under
  316. * generic_make_request() would be susceptible to deadlocks, but we have
  317. * deadlock avoidance code that resubmits any blocked bios from a rescuer
  318. * thread.
  319. *
  320. * However, we do not guarantee forward progress for allocations from other
  321. * mempools. Doing multiple allocations from the same mempool under
  322. * generic_make_request() should be avoided - instead, use bio_set's front_pad
  323. * for per bio allocations.
  324. *
  325. * RETURNS:
  326. * Pointer to new bio on success, NULL on failure.
  327. */
  328. struct bio *bio_alloc_bioset(gfp_t gfp_mask, int nr_iovecs, struct bio_set *bs)
  329. {
  330. gfp_t saved_gfp = gfp_mask;
  331. unsigned front_pad;
  332. unsigned inline_vecs;
  333. unsigned long idx = BIO_POOL_NONE;
  334. struct bio_vec *bvl = NULL;
  335. struct bio *bio;
  336. void *p;
  337. if (!bs) {
  338. if (nr_iovecs > UIO_MAXIOV)
  339. return NULL;
  340. p = kmalloc(sizeof(struct bio) +
  341. nr_iovecs * sizeof(struct bio_vec),
  342. gfp_mask);
  343. front_pad = 0;
  344. inline_vecs = nr_iovecs;
  345. } else {
  346. /*
  347. * generic_make_request() converts recursion to iteration; this
  348. * means if we're running beneath it, any bios we allocate and
  349. * submit will not be submitted (and thus freed) until after we
  350. * return.
  351. *
  352. * This exposes us to a potential deadlock if we allocate
  353. * multiple bios from the same bio_set() while running
  354. * underneath generic_make_request(). If we were to allocate
  355. * multiple bios (say a stacking block driver that was splitting
  356. * bios), we would deadlock if we exhausted the mempool's
  357. * reserve.
  358. *
  359. * We solve this, and guarantee forward progress, with a rescuer
  360. * workqueue per bio_set. If we go to allocate and there are
  361. * bios on current->bio_list, we first try the allocation
  362. * without __GFP_WAIT; if that fails, we punt those bios we
  363. * would be blocking to the rescuer workqueue before we retry
  364. * with the original gfp_flags.
  365. */
  366. if (current->bio_list && !bio_list_empty(current->bio_list))
  367. gfp_mask &= ~__GFP_WAIT;
  368. p = mempool_alloc(bs->bio_pool, gfp_mask);
  369. if (!p && gfp_mask != saved_gfp) {
  370. punt_bios_to_rescuer(bs);
  371. gfp_mask = saved_gfp;
  372. p = mempool_alloc(bs->bio_pool, gfp_mask);
  373. }
  374. front_pad = bs->front_pad;
  375. inline_vecs = BIO_INLINE_VECS;
  376. }
  377. if (unlikely(!p))
  378. return NULL;
  379. bio = p + front_pad;
  380. bio_init(bio);
  381. if (nr_iovecs > inline_vecs) {
  382. bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
  383. if (!bvl && gfp_mask != saved_gfp) {
  384. punt_bios_to_rescuer(bs);
  385. gfp_mask = saved_gfp;
  386. bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx, bs->bvec_pool);
  387. }
  388. if (unlikely(!bvl))
  389. goto err_free;
  390. bio->bi_flags |= 1 << BIO_OWNS_VEC;
  391. } else if (nr_iovecs) {
  392. bvl = bio->bi_inline_vecs;
  393. }
  394. bio->bi_pool = bs;
  395. bio->bi_flags |= idx << BIO_POOL_OFFSET;
  396. bio->bi_max_vecs = nr_iovecs;
  397. bio->bi_io_vec = bvl;
  398. return bio;
  399. err_free:
  400. mempool_free(p, bs->bio_pool);
  401. return NULL;
  402. }
  403. EXPORT_SYMBOL(bio_alloc_bioset);
  404. void zero_fill_bio(struct bio *bio)
  405. {
  406. unsigned long flags;
  407. struct bio_vec *bv;
  408. int i;
  409. bio_for_each_segment(bv, bio, i) {
  410. char *data = bvec_kmap_irq(bv, &flags);
  411. memset(data, 0, bv->bv_len);
  412. flush_dcache_page(bv->bv_page);
  413. bvec_kunmap_irq(data, &flags);
  414. }
  415. }
  416. EXPORT_SYMBOL(zero_fill_bio);
  417. /**
  418. * bio_put - release a reference to a bio
  419. * @bio: bio to release reference to
  420. *
  421. * Description:
  422. * Put a reference to a &struct bio, either one you have gotten with
  423. * bio_alloc, bio_get or bio_clone. The last put of a bio will free it.
  424. **/
  425. void bio_put(struct bio *bio)
  426. {
  427. BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
  428. /*
  429. * last put frees it
  430. */
  431. if (atomic_dec_and_test(&bio->bi_cnt))
  432. bio_free(bio);
  433. }
  434. EXPORT_SYMBOL(bio_put);
  435. inline int bio_phys_segments(struct request_queue *q, struct bio *bio)
  436. {
  437. if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
  438. blk_recount_segments(q, bio);
  439. return bio->bi_phys_segments;
  440. }
  441. EXPORT_SYMBOL(bio_phys_segments);
  442. /**
  443. * __bio_clone - clone a bio
  444. * @bio: destination bio
  445. * @bio_src: bio to clone
  446. *
  447. * Clone a &bio. Caller will own the returned bio, but not
  448. * the actual data it points to. Reference count of returned
  449. * bio will be one.
  450. */
  451. void __bio_clone(struct bio *bio, struct bio *bio_src)
  452. {
  453. memcpy(bio->bi_io_vec, bio_src->bi_io_vec,
  454. bio_src->bi_max_vecs * sizeof(struct bio_vec));
  455. /*
  456. * most users will be overriding ->bi_bdev with a new target,
  457. * so we don't set nor calculate new physical/hw segment counts here
  458. */
  459. bio->bi_sector = bio_src->bi_sector;
  460. bio->bi_bdev = bio_src->bi_bdev;
  461. bio->bi_flags |= 1 << BIO_CLONED;
  462. bio->bi_rw = bio_src->bi_rw;
  463. bio->bi_vcnt = bio_src->bi_vcnt;
  464. bio->bi_size = bio_src->bi_size;
  465. bio->bi_idx = bio_src->bi_idx;
  466. }
  467. EXPORT_SYMBOL(__bio_clone);
  468. /**
  469. * bio_clone_bioset - clone a bio
  470. * @bio: bio to clone
  471. * @gfp_mask: allocation priority
  472. * @bs: bio_set to allocate from
  473. *
  474. * Like __bio_clone, only also allocates the returned bio
  475. */
  476. struct bio *bio_clone_bioset(struct bio *bio, gfp_t gfp_mask,
  477. struct bio_set *bs)
  478. {
  479. struct bio *b;
  480. b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs, bs);
  481. if (!b)
  482. return NULL;
  483. __bio_clone(b, bio);
  484. if (bio_integrity(bio)) {
  485. int ret;
  486. ret = bio_integrity_clone(b, bio, gfp_mask);
  487. if (ret < 0) {
  488. bio_put(b);
  489. return NULL;
  490. }
  491. }
  492. return b;
  493. }
  494. EXPORT_SYMBOL(bio_clone_bioset);
  495. /**
  496. * bio_get_nr_vecs - return approx number of vecs
  497. * @bdev: I/O target
  498. *
  499. * Return the approximate number of pages we can send to this target.
  500. * There's no guarantee that you will be able to fit this number of pages
  501. * into a bio, it does not account for dynamic restrictions that vary
  502. * on offset.
  503. */
  504. int bio_get_nr_vecs(struct block_device *bdev)
  505. {
  506. struct request_queue *q = bdev_get_queue(bdev);
  507. int nr_pages;
  508. nr_pages = min_t(unsigned,
  509. queue_max_segments(q),
  510. queue_max_sectors(q) / (PAGE_SIZE >> 9) + 1);
  511. return min_t(unsigned, nr_pages, BIO_MAX_PAGES);
  512. }
  513. EXPORT_SYMBOL(bio_get_nr_vecs);
  514. static int __bio_add_page(struct request_queue *q, struct bio *bio, struct page
  515. *page, unsigned int len, unsigned int offset,
  516. unsigned short max_sectors)
  517. {
  518. int retried_segments = 0;
  519. struct bio_vec *bvec;
  520. /*
  521. * cloned bio must not modify vec list
  522. */
  523. if (unlikely(bio_flagged(bio, BIO_CLONED)))
  524. return 0;
  525. if (((bio->bi_size + len) >> 9) > max_sectors)
  526. return 0;
  527. /*
  528. * For filesystems with a blocksize smaller than the pagesize
  529. * we will often be called with the same page as last time and
  530. * a consecutive offset. Optimize this special case.
  531. */
  532. if (bio->bi_vcnt > 0) {
  533. struct bio_vec *prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
  534. if (page == prev->bv_page &&
  535. offset == prev->bv_offset + prev->bv_len) {
  536. unsigned int prev_bv_len = prev->bv_len;
  537. prev->bv_len += len;
  538. if (q->merge_bvec_fn) {
  539. struct bvec_merge_data bvm = {
  540. /* prev_bvec is already charged in
  541. bi_size, discharge it in order to
  542. simulate merging updated prev_bvec
  543. as new bvec. */
  544. .bi_bdev = bio->bi_bdev,
  545. .bi_sector = bio->bi_sector,
  546. .bi_size = bio->bi_size - prev_bv_len,
  547. .bi_rw = bio->bi_rw,
  548. };
  549. if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len) {
  550. prev->bv_len -= len;
  551. return 0;
  552. }
  553. }
  554. goto done;
  555. }
  556. }
  557. if (bio->bi_vcnt >= bio->bi_max_vecs)
  558. return 0;
  559. /*
  560. * we might lose a segment or two here, but rather that than
  561. * make this too complex.
  562. */
  563. while (bio->bi_phys_segments >= queue_max_segments(q)) {
  564. if (retried_segments)
  565. return 0;
  566. retried_segments = 1;
  567. blk_recount_segments(q, bio);
  568. }
  569. /*
  570. * setup the new entry, we might clear it again later if we
  571. * cannot add the page
  572. */
  573. bvec = &bio->bi_io_vec[bio->bi_vcnt];
  574. bvec->bv_page = page;
  575. bvec->bv_len = len;
  576. bvec->bv_offset = offset;
  577. /*
  578. * if queue has other restrictions (eg varying max sector size
  579. * depending on offset), it can specify a merge_bvec_fn in the
  580. * queue to get further control
  581. */
  582. if (q->merge_bvec_fn) {
  583. struct bvec_merge_data bvm = {
  584. .bi_bdev = bio->bi_bdev,
  585. .bi_sector = bio->bi_sector,
  586. .bi_size = bio->bi_size,
  587. .bi_rw = bio->bi_rw,
  588. };
  589. /*
  590. * merge_bvec_fn() returns number of bytes it can accept
  591. * at this offset
  592. */
  593. if (q->merge_bvec_fn(q, &bvm, bvec) < bvec->bv_len) {
  594. bvec->bv_page = NULL;
  595. bvec->bv_len = 0;
  596. bvec->bv_offset = 0;
  597. return 0;
  598. }
  599. }
  600. /* If we may be able to merge these biovecs, force a recount */
  601. if (bio->bi_vcnt && (BIOVEC_PHYS_MERGEABLE(bvec-1, bvec)))
  602. bio->bi_flags &= ~(1 << BIO_SEG_VALID);
  603. bio->bi_vcnt++;
  604. bio->bi_phys_segments++;
  605. done:
  606. bio->bi_size += len;
  607. return len;
  608. }
  609. /**
  610. * bio_add_pc_page - attempt to add page to bio
  611. * @q: the target queue
  612. * @bio: destination bio
  613. * @page: page to add
  614. * @len: vec entry length
  615. * @offset: vec entry offset
  616. *
  617. * Attempt to add a page to the bio_vec maplist. This can fail for a
  618. * number of reasons, such as the bio being full or target block device
  619. * limitations. The target block device must allow bio's up to PAGE_SIZE,
  620. * so it is always possible to add a single page to an empty bio.
  621. *
  622. * This should only be used by REQ_PC bios.
  623. */
  624. int bio_add_pc_page(struct request_queue *q, struct bio *bio, struct page *page,
  625. unsigned int len, unsigned int offset)
  626. {
  627. return __bio_add_page(q, bio, page, len, offset,
  628. queue_max_hw_sectors(q));
  629. }
  630. EXPORT_SYMBOL(bio_add_pc_page);
  631. /**
  632. * bio_add_page - attempt to add page to bio
  633. * @bio: destination bio
  634. * @page: page to add
  635. * @len: vec entry length
  636. * @offset: vec entry offset
  637. *
  638. * Attempt to add a page to the bio_vec maplist. This can fail for a
  639. * number of reasons, such as the bio being full or target block device
  640. * limitations. The target block device must allow bio's up to PAGE_SIZE,
  641. * so it is always possible to add a single page to an empty bio.
  642. */
  643. int bio_add_page(struct bio *bio, struct page *page, unsigned int len,
  644. unsigned int offset)
  645. {
  646. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  647. return __bio_add_page(q, bio, page, len, offset, queue_max_sectors(q));
  648. }
  649. EXPORT_SYMBOL(bio_add_page);
  650. struct submit_bio_ret {
  651. struct completion event;
  652. int error;
  653. };
  654. static void submit_bio_wait_endio(struct bio *bio, int error)
  655. {
  656. struct submit_bio_ret *ret = bio->bi_private;
  657. ret->error = error;
  658. complete(&ret->event);
  659. }
  660. /**
  661. * submit_bio_wait - submit a bio, and wait until it completes
  662. * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
  663. * @bio: The &struct bio which describes the I/O
  664. *
  665. * Simple wrapper around submit_bio(). Returns 0 on success, or the error from
  666. * bio_endio() on failure.
  667. */
  668. int submit_bio_wait(int rw, struct bio *bio)
  669. {
  670. struct submit_bio_ret ret;
  671. rw |= REQ_SYNC;
  672. init_completion(&ret.event);
  673. bio->bi_private = &ret;
  674. bio->bi_end_io = submit_bio_wait_endio;
  675. submit_bio(rw, bio);
  676. wait_for_completion(&ret.event);
  677. return ret.error;
  678. }
  679. EXPORT_SYMBOL(submit_bio_wait);
  680. /**
  681. * bio_advance - increment/complete a bio by some number of bytes
  682. * @bio: bio to advance
  683. * @bytes: number of bytes to complete
  684. *
  685. * This updates bi_sector, bi_size and bi_idx; if the number of bytes to
  686. * complete doesn't align with a bvec boundary, then bv_len and bv_offset will
  687. * be updated on the last bvec as well.
  688. *
  689. * @bio will then represent the remaining, uncompleted portion of the io.
  690. */
  691. void bio_advance(struct bio *bio, unsigned bytes)
  692. {
  693. if (bio_integrity(bio))
  694. bio_integrity_advance(bio, bytes);
  695. bio->bi_sector += bytes >> 9;
  696. bio->bi_size -= bytes;
  697. if (bio->bi_rw & BIO_NO_ADVANCE_ITER_MASK)
  698. return;
  699. while (bytes) {
  700. if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
  701. WARN_ONCE(1, "bio idx %d >= vcnt %d\n",
  702. bio->bi_idx, bio->bi_vcnt);
  703. break;
  704. }
  705. if (bytes >= bio_iovec(bio)->bv_len) {
  706. bytes -= bio_iovec(bio)->bv_len;
  707. bio->bi_idx++;
  708. } else {
  709. bio_iovec(bio)->bv_len -= bytes;
  710. bio_iovec(bio)->bv_offset += bytes;
  711. bytes = 0;
  712. }
  713. }
  714. }
  715. EXPORT_SYMBOL(bio_advance);
  716. /**
  717. * bio_alloc_pages - allocates a single page for each bvec in a bio
  718. * @bio: bio to allocate pages for
  719. * @gfp_mask: flags for allocation
  720. *
  721. * Allocates pages up to @bio->bi_vcnt.
  722. *
  723. * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
  724. * freed.
  725. */
  726. int bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
  727. {
  728. int i;
  729. struct bio_vec *bv;
  730. bio_for_each_segment_all(bv, bio, i) {
  731. bv->bv_page = alloc_page(gfp_mask);
  732. if (!bv->bv_page) {
  733. while (--bv >= bio->bi_io_vec)
  734. __free_page(bv->bv_page);
  735. return -ENOMEM;
  736. }
  737. }
  738. return 0;
  739. }
  740. EXPORT_SYMBOL(bio_alloc_pages);
  741. /**
  742. * bio_copy_data - copy contents of data buffers from one chain of bios to
  743. * another
  744. * @src: source bio list
  745. * @dst: destination bio list
  746. *
  747. * If @src and @dst are single bios, bi_next must be NULL - otherwise, treats
  748. * @src and @dst as linked lists of bios.
  749. *
  750. * Stops when it reaches the end of either @src or @dst - that is, copies
  751. * min(src->bi_size, dst->bi_size) bytes (or the equivalent for lists of bios).
  752. */
  753. void bio_copy_data(struct bio *dst, struct bio *src)
  754. {
  755. struct bio_vec *src_bv, *dst_bv;
  756. unsigned src_offset, dst_offset, bytes;
  757. void *src_p, *dst_p;
  758. src_bv = bio_iovec(src);
  759. dst_bv = bio_iovec(dst);
  760. src_offset = src_bv->bv_offset;
  761. dst_offset = dst_bv->bv_offset;
  762. while (1) {
  763. if (src_offset == src_bv->bv_offset + src_bv->bv_len) {
  764. src_bv++;
  765. if (src_bv == bio_iovec_idx(src, src->bi_vcnt)) {
  766. src = src->bi_next;
  767. if (!src)
  768. break;
  769. src_bv = bio_iovec(src);
  770. }
  771. src_offset = src_bv->bv_offset;
  772. }
  773. if (dst_offset == dst_bv->bv_offset + dst_bv->bv_len) {
  774. dst_bv++;
  775. if (dst_bv == bio_iovec_idx(dst, dst->bi_vcnt)) {
  776. dst = dst->bi_next;
  777. if (!dst)
  778. break;
  779. dst_bv = bio_iovec(dst);
  780. }
  781. dst_offset = dst_bv->bv_offset;
  782. }
  783. bytes = min(dst_bv->bv_offset + dst_bv->bv_len - dst_offset,
  784. src_bv->bv_offset + src_bv->bv_len - src_offset);
  785. src_p = kmap_atomic(src_bv->bv_page);
  786. dst_p = kmap_atomic(dst_bv->bv_page);
  787. memcpy(dst_p + dst_bv->bv_offset,
  788. src_p + src_bv->bv_offset,
  789. bytes);
  790. kunmap_atomic(dst_p);
  791. kunmap_atomic(src_p);
  792. src_offset += bytes;
  793. dst_offset += bytes;
  794. }
  795. }
  796. EXPORT_SYMBOL(bio_copy_data);
  797. struct bio_map_data {
  798. struct bio_vec *iovecs;
  799. struct sg_iovec *sgvecs;
  800. int nr_sgvecs;
  801. int is_our_pages;
  802. };
  803. static void bio_set_map_data(struct bio_map_data *bmd, struct bio *bio,
  804. struct sg_iovec *iov, int iov_count,
  805. int is_our_pages)
  806. {
  807. memcpy(bmd->iovecs, bio->bi_io_vec, sizeof(struct bio_vec) * bio->bi_vcnt);
  808. memcpy(bmd->sgvecs, iov, sizeof(struct sg_iovec) * iov_count);
  809. bmd->nr_sgvecs = iov_count;
  810. bmd->is_our_pages = is_our_pages;
  811. bio->bi_private = bmd;
  812. }
  813. static void bio_free_map_data(struct bio_map_data *bmd)
  814. {
  815. kfree(bmd->iovecs);
  816. kfree(bmd->sgvecs);
  817. kfree(bmd);
  818. }
  819. static struct bio_map_data *bio_alloc_map_data(int nr_segs,
  820. unsigned int iov_count,
  821. gfp_t gfp_mask)
  822. {
  823. struct bio_map_data *bmd;
  824. if (iov_count > UIO_MAXIOV)
  825. return NULL;
  826. bmd = kmalloc(sizeof(*bmd), gfp_mask);
  827. if (!bmd)
  828. return NULL;
  829. bmd->iovecs = kmalloc(sizeof(struct bio_vec) * nr_segs, gfp_mask);
  830. if (!bmd->iovecs) {
  831. kfree(bmd);
  832. return NULL;
  833. }
  834. bmd->sgvecs = kmalloc(sizeof(struct sg_iovec) * iov_count, gfp_mask);
  835. if (bmd->sgvecs)
  836. return bmd;
  837. kfree(bmd->iovecs);
  838. kfree(bmd);
  839. return NULL;
  840. }
  841. static int __bio_copy_iov(struct bio *bio, struct bio_vec *iovecs,
  842. struct sg_iovec *iov, int iov_count,
  843. int to_user, int from_user, int do_free_page)
  844. {
  845. int ret = 0, i;
  846. struct bio_vec *bvec;
  847. int iov_idx = 0;
  848. unsigned int iov_off = 0;
  849. bio_for_each_segment_all(bvec, bio, i) {
  850. char *bv_addr = page_address(bvec->bv_page);
  851. unsigned int bv_len = iovecs[i].bv_len;
  852. while (bv_len && iov_idx < iov_count) {
  853. unsigned int bytes;
  854. char __user *iov_addr;
  855. bytes = min_t(unsigned int,
  856. iov[iov_idx].iov_len - iov_off, bv_len);
  857. iov_addr = iov[iov_idx].iov_base + iov_off;
  858. if (!ret) {
  859. if (to_user)
  860. ret = copy_to_user(iov_addr, bv_addr,
  861. bytes);
  862. if (from_user)
  863. ret = copy_from_user(bv_addr, iov_addr,
  864. bytes);
  865. if (ret)
  866. ret = -EFAULT;
  867. }
  868. bv_len -= bytes;
  869. bv_addr += bytes;
  870. iov_addr += bytes;
  871. iov_off += bytes;
  872. if (iov[iov_idx].iov_len == iov_off) {
  873. iov_idx++;
  874. iov_off = 0;
  875. }
  876. }
  877. if (do_free_page)
  878. __free_page(bvec->bv_page);
  879. }
  880. return ret;
  881. }
  882. /**
  883. * bio_uncopy_user - finish previously mapped bio
  884. * @bio: bio being terminated
  885. *
  886. * Free pages allocated from bio_copy_user() and write back data
  887. * to user space in case of a read.
  888. */
  889. int bio_uncopy_user(struct bio *bio)
  890. {
  891. struct bio_map_data *bmd = bio->bi_private;
  892. int ret = 0;
  893. if (!bio_flagged(bio, BIO_NULL_MAPPED))
  894. ret = __bio_copy_iov(bio, bmd->iovecs, bmd->sgvecs,
  895. bmd->nr_sgvecs, bio_data_dir(bio) == READ,
  896. 0, bmd->is_our_pages);
  897. bio_free_map_data(bmd);
  898. bio_put(bio);
  899. return ret;
  900. }
  901. EXPORT_SYMBOL(bio_uncopy_user);
  902. /**
  903. * bio_copy_user_iov - copy user data to bio
  904. * @q: destination block queue
  905. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  906. * @iov: the iovec.
  907. * @iov_count: number of elements in the iovec
  908. * @write_to_vm: bool indicating writing to pages or not
  909. * @gfp_mask: memory allocation flags
  910. *
  911. * Prepares and returns a bio for indirect user io, bouncing data
  912. * to/from kernel pages as necessary. Must be paired with
  913. * call bio_uncopy_user() on io completion.
  914. */
  915. struct bio *bio_copy_user_iov(struct request_queue *q,
  916. struct rq_map_data *map_data,
  917. struct sg_iovec *iov, int iov_count,
  918. int write_to_vm, gfp_t gfp_mask)
  919. {
  920. struct bio_map_data *bmd;
  921. struct bio_vec *bvec;
  922. struct page *page;
  923. struct bio *bio;
  924. int i, ret;
  925. int nr_pages = 0;
  926. unsigned int len = 0;
  927. unsigned int offset = map_data ? map_data->offset & ~PAGE_MASK : 0;
  928. for (i = 0; i < iov_count; i++) {
  929. unsigned long uaddr;
  930. unsigned long end;
  931. unsigned long start;
  932. uaddr = (unsigned long)iov[i].iov_base;
  933. end = (uaddr + iov[i].iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  934. start = uaddr >> PAGE_SHIFT;
  935. /*
  936. * Overflow, abort
  937. */
  938. if (end < start)
  939. return ERR_PTR(-EINVAL);
  940. nr_pages += end - start;
  941. len += iov[i].iov_len;
  942. }
  943. if (offset)
  944. nr_pages++;
  945. bmd = bio_alloc_map_data(nr_pages, iov_count, gfp_mask);
  946. if (!bmd)
  947. return ERR_PTR(-ENOMEM);
  948. ret = -ENOMEM;
  949. bio = bio_kmalloc(gfp_mask, nr_pages);
  950. if (!bio)
  951. goto out_bmd;
  952. if (!write_to_vm)
  953. bio->bi_rw |= REQ_WRITE;
  954. ret = 0;
  955. if (map_data) {
  956. nr_pages = 1 << map_data->page_order;
  957. i = map_data->offset / PAGE_SIZE;
  958. }
  959. while (len) {
  960. unsigned int bytes = PAGE_SIZE;
  961. bytes -= offset;
  962. if (bytes > len)
  963. bytes = len;
  964. if (map_data) {
  965. if (i == map_data->nr_entries * nr_pages) {
  966. ret = -ENOMEM;
  967. break;
  968. }
  969. page = map_data->pages[i / nr_pages];
  970. page += (i % nr_pages);
  971. i++;
  972. } else {
  973. page = alloc_page(q->bounce_gfp | gfp_mask);
  974. if (!page) {
  975. ret = -ENOMEM;
  976. break;
  977. }
  978. }
  979. if (bio_add_pc_page(q, bio, page, bytes, offset) < bytes)
  980. break;
  981. len -= bytes;
  982. offset = 0;
  983. }
  984. if (ret)
  985. goto cleanup;
  986. /*
  987. * success
  988. */
  989. if ((!write_to_vm && (!map_data || !map_data->null_mapped)) ||
  990. (map_data && map_data->from_user)) {
  991. ret = __bio_copy_iov(bio, bio->bi_io_vec, iov, iov_count, 0, 1, 0);
  992. if (ret)
  993. goto cleanup;
  994. }
  995. bio_set_map_data(bmd, bio, iov, iov_count, map_data ? 0 : 1);
  996. return bio;
  997. cleanup:
  998. if (!map_data)
  999. bio_for_each_segment_all(bvec, bio, i)
  1000. __free_page(bvec->bv_page);
  1001. bio_put(bio);
  1002. out_bmd:
  1003. bio_free_map_data(bmd);
  1004. return ERR_PTR(ret);
  1005. }
  1006. /**
  1007. * bio_copy_user - copy user data to bio
  1008. * @q: destination block queue
  1009. * @map_data: pointer to the rq_map_data holding pages (if necessary)
  1010. * @uaddr: start of user address
  1011. * @len: length in bytes
  1012. * @write_to_vm: bool indicating writing to pages or not
  1013. * @gfp_mask: memory allocation flags
  1014. *
  1015. * Prepares and returns a bio for indirect user io, bouncing data
  1016. * to/from kernel pages as necessary. Must be paired with
  1017. * call bio_uncopy_user() on io completion.
  1018. */
  1019. struct bio *bio_copy_user(struct request_queue *q, struct rq_map_data *map_data,
  1020. unsigned long uaddr, unsigned int len,
  1021. int write_to_vm, gfp_t gfp_mask)
  1022. {
  1023. struct sg_iovec iov;
  1024. iov.iov_base = (void __user *)uaddr;
  1025. iov.iov_len = len;
  1026. return bio_copy_user_iov(q, map_data, &iov, 1, write_to_vm, gfp_mask);
  1027. }
  1028. EXPORT_SYMBOL(bio_copy_user);
  1029. static struct bio *__bio_map_user_iov(struct request_queue *q,
  1030. struct block_device *bdev,
  1031. struct sg_iovec *iov, int iov_count,
  1032. int write_to_vm, gfp_t gfp_mask)
  1033. {
  1034. int i, j;
  1035. int nr_pages = 0;
  1036. struct page **pages;
  1037. struct bio *bio;
  1038. int cur_page = 0;
  1039. int ret, offset;
  1040. for (i = 0; i < iov_count; i++) {
  1041. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  1042. unsigned long len = iov[i].iov_len;
  1043. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1044. unsigned long start = uaddr >> PAGE_SHIFT;
  1045. /*
  1046. * Overflow, abort
  1047. */
  1048. if (end < start)
  1049. return ERR_PTR(-EINVAL);
  1050. nr_pages += end - start;
  1051. /*
  1052. * buffer must be aligned to at least hardsector size for now
  1053. */
  1054. if (uaddr & queue_dma_alignment(q))
  1055. return ERR_PTR(-EINVAL);
  1056. }
  1057. if (!nr_pages)
  1058. return ERR_PTR(-EINVAL);
  1059. bio = bio_kmalloc(gfp_mask, nr_pages);
  1060. if (!bio)
  1061. return ERR_PTR(-ENOMEM);
  1062. ret = -ENOMEM;
  1063. pages = kcalloc(nr_pages, sizeof(struct page *), gfp_mask);
  1064. if (!pages)
  1065. goto out;
  1066. for (i = 0; i < iov_count; i++) {
  1067. unsigned long uaddr = (unsigned long)iov[i].iov_base;
  1068. unsigned long len = iov[i].iov_len;
  1069. unsigned long end = (uaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1070. unsigned long start = uaddr >> PAGE_SHIFT;
  1071. const int local_nr_pages = end - start;
  1072. const int page_limit = cur_page + local_nr_pages;
  1073. ret = get_user_pages_fast(uaddr, local_nr_pages,
  1074. write_to_vm, &pages[cur_page]);
  1075. if (ret < local_nr_pages) {
  1076. ret = -EFAULT;
  1077. goto out_unmap;
  1078. }
  1079. offset = uaddr & ~PAGE_MASK;
  1080. for (j = cur_page; j < page_limit; j++) {
  1081. unsigned int bytes = PAGE_SIZE - offset;
  1082. if (len <= 0)
  1083. break;
  1084. if (bytes > len)
  1085. bytes = len;
  1086. /*
  1087. * sorry...
  1088. */
  1089. if (bio_add_pc_page(q, bio, pages[j], bytes, offset) <
  1090. bytes)
  1091. break;
  1092. len -= bytes;
  1093. offset = 0;
  1094. }
  1095. cur_page = j;
  1096. /*
  1097. * release the pages we didn't map into the bio, if any
  1098. */
  1099. while (j < page_limit)
  1100. page_cache_release(pages[j++]);
  1101. }
  1102. kfree(pages);
  1103. /*
  1104. * set data direction, and check if mapped pages need bouncing
  1105. */
  1106. if (!write_to_vm)
  1107. bio->bi_rw |= REQ_WRITE;
  1108. bio->bi_bdev = bdev;
  1109. bio->bi_flags |= (1 << BIO_USER_MAPPED);
  1110. return bio;
  1111. out_unmap:
  1112. for (i = 0; i < nr_pages; i++) {
  1113. if(!pages[i])
  1114. break;
  1115. page_cache_release(pages[i]);
  1116. }
  1117. out:
  1118. kfree(pages);
  1119. bio_put(bio);
  1120. return ERR_PTR(ret);
  1121. }
  1122. /**
  1123. * bio_map_user - map user address into bio
  1124. * @q: the struct request_queue for the bio
  1125. * @bdev: destination block device
  1126. * @uaddr: start of user address
  1127. * @len: length in bytes
  1128. * @write_to_vm: bool indicating writing to pages or not
  1129. * @gfp_mask: memory allocation flags
  1130. *
  1131. * Map the user space address into a bio suitable for io to a block
  1132. * device. Returns an error pointer in case of error.
  1133. */
  1134. struct bio *bio_map_user(struct request_queue *q, struct block_device *bdev,
  1135. unsigned long uaddr, unsigned int len, int write_to_vm,
  1136. gfp_t gfp_mask)
  1137. {
  1138. struct sg_iovec iov;
  1139. iov.iov_base = (void __user *)uaddr;
  1140. iov.iov_len = len;
  1141. return bio_map_user_iov(q, bdev, &iov, 1, write_to_vm, gfp_mask);
  1142. }
  1143. EXPORT_SYMBOL(bio_map_user);
  1144. /**
  1145. * bio_map_user_iov - map user sg_iovec table into bio
  1146. * @q: the struct request_queue for the bio
  1147. * @bdev: destination block device
  1148. * @iov: the iovec.
  1149. * @iov_count: number of elements in the iovec
  1150. * @write_to_vm: bool indicating writing to pages or not
  1151. * @gfp_mask: memory allocation flags
  1152. *
  1153. * Map the user space address into a bio suitable for io to a block
  1154. * device. Returns an error pointer in case of error.
  1155. */
  1156. struct bio *bio_map_user_iov(struct request_queue *q, struct block_device *bdev,
  1157. struct sg_iovec *iov, int iov_count,
  1158. int write_to_vm, gfp_t gfp_mask)
  1159. {
  1160. struct bio *bio;
  1161. bio = __bio_map_user_iov(q, bdev, iov, iov_count, write_to_vm,
  1162. gfp_mask);
  1163. if (IS_ERR(bio))
  1164. return bio;
  1165. /*
  1166. * subtle -- if __bio_map_user() ended up bouncing a bio,
  1167. * it would normally disappear when its bi_end_io is run.
  1168. * however, we need it for the unmap, so grab an extra
  1169. * reference to it
  1170. */
  1171. bio_get(bio);
  1172. return bio;
  1173. }
  1174. static void __bio_unmap_user(struct bio *bio)
  1175. {
  1176. struct bio_vec *bvec;
  1177. int i;
  1178. /*
  1179. * make sure we dirty pages we wrote to
  1180. */
  1181. bio_for_each_segment_all(bvec, bio, i) {
  1182. if (bio_data_dir(bio) == READ)
  1183. set_page_dirty_lock(bvec->bv_page);
  1184. page_cache_release(bvec->bv_page);
  1185. }
  1186. bio_put(bio);
  1187. }
  1188. /**
  1189. * bio_unmap_user - unmap a bio
  1190. * @bio: the bio being unmapped
  1191. *
  1192. * Unmap a bio previously mapped by bio_map_user(). Must be called with
  1193. * a process context.
  1194. *
  1195. * bio_unmap_user() may sleep.
  1196. */
  1197. void bio_unmap_user(struct bio *bio)
  1198. {
  1199. __bio_unmap_user(bio);
  1200. bio_put(bio);
  1201. }
  1202. EXPORT_SYMBOL(bio_unmap_user);
  1203. static void bio_map_kern_endio(struct bio *bio, int err)
  1204. {
  1205. bio_put(bio);
  1206. }
  1207. static struct bio *__bio_map_kern(struct request_queue *q, void *data,
  1208. unsigned int len, gfp_t gfp_mask)
  1209. {
  1210. unsigned long kaddr = (unsigned long)data;
  1211. unsigned long end = (kaddr + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1212. unsigned long start = kaddr >> PAGE_SHIFT;
  1213. const int nr_pages = end - start;
  1214. int offset, i;
  1215. struct bio *bio;
  1216. bio = bio_kmalloc(gfp_mask, nr_pages);
  1217. if (!bio)
  1218. return ERR_PTR(-ENOMEM);
  1219. offset = offset_in_page(kaddr);
  1220. for (i = 0; i < nr_pages; i++) {
  1221. unsigned int bytes = PAGE_SIZE - offset;
  1222. if (len <= 0)
  1223. break;
  1224. if (bytes > len)
  1225. bytes = len;
  1226. if (bio_add_pc_page(q, bio, virt_to_page(data), bytes,
  1227. offset) < bytes)
  1228. break;
  1229. data += bytes;
  1230. len -= bytes;
  1231. offset = 0;
  1232. }
  1233. bio->bi_end_io = bio_map_kern_endio;
  1234. return bio;
  1235. }
  1236. /**
  1237. * bio_map_kern - map kernel address into bio
  1238. * @q: the struct request_queue for the bio
  1239. * @data: pointer to buffer to map
  1240. * @len: length in bytes
  1241. * @gfp_mask: allocation flags for bio allocation
  1242. *
  1243. * Map the kernel address into a bio suitable for io to a block
  1244. * device. Returns an error pointer in case of error.
  1245. */
  1246. struct bio *bio_map_kern(struct request_queue *q, void *data, unsigned int len,
  1247. gfp_t gfp_mask)
  1248. {
  1249. struct bio *bio;
  1250. bio = __bio_map_kern(q, data, len, gfp_mask);
  1251. if (IS_ERR(bio))
  1252. return bio;
  1253. if (bio->bi_size == len)
  1254. return bio;
  1255. /*
  1256. * Don't support partial mappings.
  1257. */
  1258. bio_put(bio);
  1259. return ERR_PTR(-EINVAL);
  1260. }
  1261. EXPORT_SYMBOL(bio_map_kern);
  1262. static void bio_copy_kern_endio(struct bio *bio, int err)
  1263. {
  1264. struct bio_vec *bvec;
  1265. const int read = bio_data_dir(bio) == READ;
  1266. struct bio_map_data *bmd = bio->bi_private;
  1267. int i;
  1268. char *p = bmd->sgvecs[0].iov_base;
  1269. bio_for_each_segment_all(bvec, bio, i) {
  1270. char *addr = page_address(bvec->bv_page);
  1271. int len = bmd->iovecs[i].bv_len;
  1272. if (read)
  1273. memcpy(p, addr, len);
  1274. __free_page(bvec->bv_page);
  1275. p += len;
  1276. }
  1277. bio_free_map_data(bmd);
  1278. bio_put(bio);
  1279. }
  1280. /**
  1281. * bio_copy_kern - copy kernel address into bio
  1282. * @q: the struct request_queue for the bio
  1283. * @data: pointer to buffer to copy
  1284. * @len: length in bytes
  1285. * @gfp_mask: allocation flags for bio and page allocation
  1286. * @reading: data direction is READ
  1287. *
  1288. * copy the kernel address into a bio suitable for io to a block
  1289. * device. Returns an error pointer in case of error.
  1290. */
  1291. struct bio *bio_copy_kern(struct request_queue *q, void *data, unsigned int len,
  1292. gfp_t gfp_mask, int reading)
  1293. {
  1294. struct bio *bio;
  1295. struct bio_vec *bvec;
  1296. int i;
  1297. bio = bio_copy_user(q, NULL, (unsigned long)data, len, 1, gfp_mask);
  1298. if (IS_ERR(bio))
  1299. return bio;
  1300. if (!reading) {
  1301. void *p = data;
  1302. bio_for_each_segment_all(bvec, bio, i) {
  1303. char *addr = page_address(bvec->bv_page);
  1304. memcpy(addr, p, bvec->bv_len);
  1305. p += bvec->bv_len;
  1306. }
  1307. }
  1308. bio->bi_end_io = bio_copy_kern_endio;
  1309. return bio;
  1310. }
  1311. EXPORT_SYMBOL(bio_copy_kern);
  1312. /*
  1313. * bio_set_pages_dirty() and bio_check_pages_dirty() are support functions
  1314. * for performing direct-IO in BIOs.
  1315. *
  1316. * The problem is that we cannot run set_page_dirty() from interrupt context
  1317. * because the required locks are not interrupt-safe. So what we can do is to
  1318. * mark the pages dirty _before_ performing IO. And in interrupt context,
  1319. * check that the pages are still dirty. If so, fine. If not, redirty them
  1320. * in process context.
  1321. *
  1322. * We special-case compound pages here: normally this means reads into hugetlb
  1323. * pages. The logic in here doesn't really work right for compound pages
  1324. * because the VM does not uniformly chase down the head page in all cases.
  1325. * But dirtiness of compound pages is pretty meaningless anyway: the VM doesn't
  1326. * handle them at all. So we skip compound pages here at an early stage.
  1327. *
  1328. * Note that this code is very hard to test under normal circumstances because
  1329. * direct-io pins the pages with get_user_pages(). This makes
  1330. * is_page_cache_freeable return false, and the VM will not clean the pages.
  1331. * But other code (eg, flusher threads) could clean the pages if they are mapped
  1332. * pagecache.
  1333. *
  1334. * Simply disabling the call to bio_set_pages_dirty() is a good way to test the
  1335. * deferred bio dirtying paths.
  1336. */
  1337. /*
  1338. * bio_set_pages_dirty() will mark all the bio's pages as dirty.
  1339. */
  1340. void bio_set_pages_dirty(struct bio *bio)
  1341. {
  1342. struct bio_vec *bvec;
  1343. int i;
  1344. bio_for_each_segment_all(bvec, bio, i) {
  1345. struct page *page = bvec->bv_page;
  1346. if (page && !PageCompound(page))
  1347. set_page_dirty_lock(page);
  1348. }
  1349. }
  1350. static void bio_release_pages(struct bio *bio)
  1351. {
  1352. struct bio_vec *bvec;
  1353. int i;
  1354. bio_for_each_segment_all(bvec, bio, i) {
  1355. struct page *page = bvec->bv_page;
  1356. if (page)
  1357. put_page(page);
  1358. }
  1359. }
  1360. /*
  1361. * bio_check_pages_dirty() will check that all the BIO's pages are still dirty.
  1362. * If they are, then fine. If, however, some pages are clean then they must
  1363. * have been written out during the direct-IO read. So we take another ref on
  1364. * the BIO and the offending pages and re-dirty the pages in process context.
  1365. *
  1366. * It is expected that bio_check_pages_dirty() will wholly own the BIO from
  1367. * here on. It will run one page_cache_release() against each page and will
  1368. * run one bio_put() against the BIO.
  1369. */
  1370. static void bio_dirty_fn(struct work_struct *work);
  1371. static DECLARE_WORK(bio_dirty_work, bio_dirty_fn);
  1372. static DEFINE_SPINLOCK(bio_dirty_lock);
  1373. static struct bio *bio_dirty_list;
  1374. /*
  1375. * This runs in process context
  1376. */
  1377. static void bio_dirty_fn(struct work_struct *work)
  1378. {
  1379. unsigned long flags;
  1380. struct bio *bio;
  1381. spin_lock_irqsave(&bio_dirty_lock, flags);
  1382. bio = bio_dirty_list;
  1383. bio_dirty_list = NULL;
  1384. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1385. while (bio) {
  1386. struct bio *next = bio->bi_private;
  1387. bio_set_pages_dirty(bio);
  1388. bio_release_pages(bio);
  1389. bio_put(bio);
  1390. bio = next;
  1391. }
  1392. }
  1393. void bio_check_pages_dirty(struct bio *bio)
  1394. {
  1395. struct bio_vec *bvec;
  1396. int nr_clean_pages = 0;
  1397. int i;
  1398. bio_for_each_segment_all(bvec, bio, i) {
  1399. struct page *page = bvec->bv_page;
  1400. if (PageDirty(page) || PageCompound(page)) {
  1401. page_cache_release(page);
  1402. bvec->bv_page = NULL;
  1403. } else {
  1404. nr_clean_pages++;
  1405. }
  1406. }
  1407. if (nr_clean_pages) {
  1408. unsigned long flags;
  1409. spin_lock_irqsave(&bio_dirty_lock, flags);
  1410. bio->bi_private = bio_dirty_list;
  1411. bio_dirty_list = bio;
  1412. spin_unlock_irqrestore(&bio_dirty_lock, flags);
  1413. schedule_work(&bio_dirty_work);
  1414. } else {
  1415. bio_put(bio);
  1416. }
  1417. }
  1418. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
  1419. void bio_flush_dcache_pages(struct bio *bi)
  1420. {
  1421. int i;
  1422. struct bio_vec *bvec;
  1423. bio_for_each_segment(bvec, bi, i)
  1424. flush_dcache_page(bvec->bv_page);
  1425. }
  1426. EXPORT_SYMBOL(bio_flush_dcache_pages);
  1427. #endif
  1428. /**
  1429. * bio_endio - end I/O on a bio
  1430. * @bio: bio
  1431. * @error: error, if any
  1432. *
  1433. * Description:
  1434. * bio_endio() will end I/O on the whole bio. bio_endio() is the
  1435. * preferred way to end I/O on a bio, it takes care of clearing
  1436. * BIO_UPTODATE on error. @error is 0 on success, and and one of the
  1437. * established -Exxxx (-EIO, for instance) error values in case
  1438. * something went wrong. No one should call bi_end_io() directly on a
  1439. * bio unless they own it and thus know that it has an end_io
  1440. * function.
  1441. **/
  1442. void bio_endio(struct bio *bio, int error)
  1443. {
  1444. if (error)
  1445. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  1446. else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
  1447. error = -EIO;
  1448. if (bio->bi_end_io)
  1449. bio->bi_end_io(bio, error);
  1450. }
  1451. EXPORT_SYMBOL(bio_endio);
  1452. void bio_pair_release(struct bio_pair *bp)
  1453. {
  1454. if (atomic_dec_and_test(&bp->cnt)) {
  1455. struct bio *master = bp->bio1.bi_private;
  1456. bio_endio(master, bp->error);
  1457. mempool_free(bp, bp->bio2.bi_private);
  1458. }
  1459. }
  1460. EXPORT_SYMBOL(bio_pair_release);
  1461. static void bio_pair_end_1(struct bio *bi, int err)
  1462. {
  1463. struct bio_pair *bp = container_of(bi, struct bio_pair, bio1);
  1464. if (err)
  1465. bp->error = err;
  1466. bio_pair_release(bp);
  1467. }
  1468. static void bio_pair_end_2(struct bio *bi, int err)
  1469. {
  1470. struct bio_pair *bp = container_of(bi, struct bio_pair, bio2);
  1471. if (err)
  1472. bp->error = err;
  1473. bio_pair_release(bp);
  1474. }
  1475. /*
  1476. * split a bio - only worry about a bio with a single page in its iovec
  1477. */
  1478. struct bio_pair *bio_split(struct bio *bi, int first_sectors)
  1479. {
  1480. struct bio_pair *bp = mempool_alloc(bio_split_pool, GFP_NOIO);
  1481. if (!bp)
  1482. return bp;
  1483. trace_block_split(bdev_get_queue(bi->bi_bdev), bi,
  1484. bi->bi_sector + first_sectors);
  1485. BUG_ON(bio_segments(bi) > 1);
  1486. atomic_set(&bp->cnt, 3);
  1487. bp->error = 0;
  1488. bp->bio1 = *bi;
  1489. bp->bio2 = *bi;
  1490. bp->bio2.bi_sector += first_sectors;
  1491. bp->bio2.bi_size -= first_sectors << 9;
  1492. bp->bio1.bi_size = first_sectors << 9;
  1493. if (bi->bi_vcnt != 0) {
  1494. bp->bv1 = *bio_iovec(bi);
  1495. bp->bv2 = *bio_iovec(bi);
  1496. if (bio_is_rw(bi)) {
  1497. bp->bv2.bv_offset += first_sectors << 9;
  1498. bp->bv2.bv_len -= first_sectors << 9;
  1499. bp->bv1.bv_len = first_sectors << 9;
  1500. }
  1501. bp->bio1.bi_io_vec = &bp->bv1;
  1502. bp->bio2.bi_io_vec = &bp->bv2;
  1503. bp->bio1.bi_max_vecs = 1;
  1504. bp->bio2.bi_max_vecs = 1;
  1505. }
  1506. bp->bio1.bi_end_io = bio_pair_end_1;
  1507. bp->bio2.bi_end_io = bio_pair_end_2;
  1508. bp->bio1.bi_private = bi;
  1509. bp->bio2.bi_private = bio_split_pool;
  1510. if (bio_integrity(bi))
  1511. bio_integrity_split(bi, bp, first_sectors);
  1512. return bp;
  1513. }
  1514. EXPORT_SYMBOL(bio_split);
  1515. /**
  1516. * bio_sector_offset - Find hardware sector offset in bio
  1517. * @bio: bio to inspect
  1518. * @index: bio_vec index
  1519. * @offset: offset in bv_page
  1520. *
  1521. * Return the number of hardware sectors between beginning of bio
  1522. * and an end point indicated by a bio_vec index and an offset
  1523. * within that vector's page.
  1524. */
  1525. sector_t bio_sector_offset(struct bio *bio, unsigned short index,
  1526. unsigned int offset)
  1527. {
  1528. unsigned int sector_sz;
  1529. struct bio_vec *bv;
  1530. sector_t sectors;
  1531. int i;
  1532. sector_sz = queue_logical_block_size(bio->bi_bdev->bd_disk->queue);
  1533. sectors = 0;
  1534. if (index >= bio->bi_idx)
  1535. index = bio->bi_vcnt - 1;
  1536. bio_for_each_segment_all(bv, bio, i) {
  1537. if (i == index) {
  1538. if (offset > bv->bv_offset)
  1539. sectors += (offset - bv->bv_offset) / sector_sz;
  1540. break;
  1541. }
  1542. sectors += bv->bv_len / sector_sz;
  1543. }
  1544. return sectors;
  1545. }
  1546. EXPORT_SYMBOL(bio_sector_offset);
  1547. /*
  1548. * create memory pools for biovec's in a bio_set.
  1549. * use the global biovec slabs created for general use.
  1550. */
  1551. mempool_t *biovec_create_pool(struct bio_set *bs, int pool_entries)
  1552. {
  1553. struct biovec_slab *bp = bvec_slabs + BIOVEC_MAX_IDX;
  1554. return mempool_create_slab_pool(pool_entries, bp->slab);
  1555. }
  1556. void bioset_free(struct bio_set *bs)
  1557. {
  1558. if (bs->rescue_workqueue)
  1559. destroy_workqueue(bs->rescue_workqueue);
  1560. if (bs->bio_pool)
  1561. mempool_destroy(bs->bio_pool);
  1562. if (bs->bvec_pool)
  1563. mempool_destroy(bs->bvec_pool);
  1564. bioset_integrity_free(bs);
  1565. bio_put_slab(bs);
  1566. kfree(bs);
  1567. }
  1568. EXPORT_SYMBOL(bioset_free);
  1569. /**
  1570. * bioset_create - Create a bio_set
  1571. * @pool_size: Number of bio and bio_vecs to cache in the mempool
  1572. * @front_pad: Number of bytes to allocate in front of the returned bio
  1573. *
  1574. * Description:
  1575. * Set up a bio_set to be used with @bio_alloc_bioset. Allows the caller
  1576. * to ask for a number of bytes to be allocated in front of the bio.
  1577. * Front pad allocation is useful for embedding the bio inside
  1578. * another structure, to avoid allocating extra data to go with the bio.
  1579. * Note that the bio must be embedded at the END of that structure always,
  1580. * or things will break badly.
  1581. */
  1582. struct bio_set *bioset_create(unsigned int pool_size, unsigned int front_pad)
  1583. {
  1584. unsigned int back_pad = BIO_INLINE_VECS * sizeof(struct bio_vec);
  1585. struct bio_set *bs;
  1586. bs = kzalloc(sizeof(*bs), GFP_KERNEL);
  1587. if (!bs)
  1588. return NULL;
  1589. bs->front_pad = front_pad;
  1590. spin_lock_init(&bs->rescue_lock);
  1591. bio_list_init(&bs->rescue_list);
  1592. INIT_WORK(&bs->rescue_work, bio_alloc_rescue);
  1593. bs->bio_slab = bio_find_or_create_slab(front_pad + back_pad);
  1594. if (!bs->bio_slab) {
  1595. kfree(bs);
  1596. return NULL;
  1597. }
  1598. bs->bio_pool = mempool_create_slab_pool(pool_size, bs->bio_slab);
  1599. if (!bs->bio_pool)
  1600. goto bad;
  1601. bs->bvec_pool = biovec_create_pool(bs, pool_size);
  1602. if (!bs->bvec_pool)
  1603. goto bad;
  1604. bs->rescue_workqueue = alloc_workqueue("bioset", WQ_MEM_RECLAIM, 0);
  1605. if (!bs->rescue_workqueue)
  1606. goto bad;
  1607. return bs;
  1608. bad:
  1609. bioset_free(bs);
  1610. return NULL;
  1611. }
  1612. EXPORT_SYMBOL(bioset_create);
  1613. #ifdef CONFIG_BLK_CGROUP
  1614. /**
  1615. * bio_associate_current - associate a bio with %current
  1616. * @bio: target bio
  1617. *
  1618. * Associate @bio with %current if it hasn't been associated yet. Block
  1619. * layer will treat @bio as if it were issued by %current no matter which
  1620. * task actually issues it.
  1621. *
  1622. * This function takes an extra reference of @task's io_context and blkcg
  1623. * which will be put when @bio is released. The caller must own @bio,
  1624. * ensure %current->io_context exists, and is responsible for synchronizing
  1625. * calls to this function.
  1626. */
  1627. int bio_associate_current(struct bio *bio)
  1628. {
  1629. struct io_context *ioc;
  1630. struct cgroup_subsys_state *css;
  1631. if (bio->bi_ioc)
  1632. return -EBUSY;
  1633. ioc = current->io_context;
  1634. if (!ioc)
  1635. return -ENOENT;
  1636. /* acquire active ref on @ioc and associate */
  1637. get_io_context_active(ioc);
  1638. bio->bi_ioc = ioc;
  1639. /* associate blkcg if exists */
  1640. rcu_read_lock();
  1641. css = task_subsys_state(current, blkio_subsys_id);
  1642. if (css && css_tryget(css))
  1643. bio->bi_css = css;
  1644. rcu_read_unlock();
  1645. return 0;
  1646. }
  1647. /**
  1648. * bio_disassociate_task - undo bio_associate_current()
  1649. * @bio: target bio
  1650. */
  1651. void bio_disassociate_task(struct bio *bio)
  1652. {
  1653. if (bio->bi_ioc) {
  1654. put_io_context(bio->bi_ioc);
  1655. bio->bi_ioc = NULL;
  1656. }
  1657. if (bio->bi_css) {
  1658. css_put(bio->bi_css);
  1659. bio->bi_css = NULL;
  1660. }
  1661. }
  1662. #endif /* CONFIG_BLK_CGROUP */
  1663. static void __init biovec_init_slabs(void)
  1664. {
  1665. int i;
  1666. for (i = 0; i < BIOVEC_NR_POOLS; i++) {
  1667. int size;
  1668. struct biovec_slab *bvs = bvec_slabs + i;
  1669. if (bvs->nr_vecs <= BIO_INLINE_VECS) {
  1670. bvs->slab = NULL;
  1671. continue;
  1672. }
  1673. size = bvs->nr_vecs * sizeof(struct bio_vec);
  1674. bvs->slab = kmem_cache_create(bvs->name, size, 0,
  1675. SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  1676. }
  1677. }
  1678. static int __init init_bio(void)
  1679. {
  1680. bio_slab_max = 2;
  1681. bio_slab_nr = 0;
  1682. bio_slabs = kzalloc(bio_slab_max * sizeof(struct bio_slab), GFP_KERNEL);
  1683. if (!bio_slabs)
  1684. panic("bio: can't allocate bios\n");
  1685. bio_integrity_init();
  1686. biovec_init_slabs();
  1687. fs_bio_set = bioset_create(BIO_POOL_SIZE, 0);
  1688. if (!fs_bio_set)
  1689. panic("bio: can't allocate bios\n");
  1690. if (bioset_integrity_create(fs_bio_set, BIO_POOL_SIZE))
  1691. panic("bio: can't create integrity pool\n");
  1692. bio_split_pool = mempool_create_kmalloc_pool(BIO_SPLIT_ENTRIES,
  1693. sizeof(struct bio_pair));
  1694. if (!bio_split_pool)
  1695. panic("bio: can't create split pool\n");
  1696. return 0;
  1697. }
  1698. subsys_initcall(init_bio);