bio.c 41 KB

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