bio.c 41 KB

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