dm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /*
  2. * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2004 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm.h"
  8. #include "dm-bio-list.h"
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/blkpg.h>
  13. #include <linux/bio.h>
  14. #include <linux/buffer_head.h>
  15. #include <linux/mempool.h>
  16. #include <linux/slab.h>
  17. #include <linux/idr.h>
  18. static const char *_name = DM_NAME;
  19. static unsigned int major = 0;
  20. static unsigned int _major = 0;
  21. /*
  22. * One of these is allocated per bio.
  23. */
  24. struct dm_io {
  25. struct mapped_device *md;
  26. int error;
  27. struct bio *bio;
  28. atomic_t io_count;
  29. };
  30. /*
  31. * One of these is allocated per target within a bio. Hopefully
  32. * this will be simplified out one day.
  33. */
  34. struct target_io {
  35. struct dm_io *io;
  36. struct dm_target *ti;
  37. union map_info info;
  38. };
  39. union map_info *dm_get_mapinfo(struct bio *bio)
  40. {
  41. if (bio && bio->bi_private)
  42. return &((struct target_io *)bio->bi_private)->info;
  43. return NULL;
  44. }
  45. /*
  46. * Bits for the md->flags field.
  47. */
  48. #define DMF_BLOCK_IO 0
  49. #define DMF_SUSPENDED 1
  50. struct mapped_device {
  51. struct rw_semaphore io_lock;
  52. struct semaphore suspend_lock;
  53. rwlock_t map_lock;
  54. atomic_t holders;
  55. unsigned long flags;
  56. request_queue_t *queue;
  57. struct gendisk *disk;
  58. void *interface_ptr;
  59. /*
  60. * A list of ios that arrived while we were suspended.
  61. */
  62. atomic_t pending;
  63. wait_queue_head_t wait;
  64. struct bio_list deferred;
  65. /*
  66. * The current mapping.
  67. */
  68. struct dm_table *map;
  69. /*
  70. * io objects are allocated from here.
  71. */
  72. mempool_t *io_pool;
  73. mempool_t *tio_pool;
  74. /*
  75. * Event handling.
  76. */
  77. atomic_t event_nr;
  78. wait_queue_head_t eventq;
  79. /*
  80. * freeze/thaw support require holding onto a super block
  81. */
  82. struct super_block *frozen_sb;
  83. struct block_device *frozen_bdev;
  84. };
  85. #define MIN_IOS 256
  86. static kmem_cache_t *_io_cache;
  87. static kmem_cache_t *_tio_cache;
  88. static struct bio_set *dm_set;
  89. static int __init local_init(void)
  90. {
  91. int r;
  92. dm_set = bioset_create(16, 16, 4);
  93. if (!dm_set)
  94. return -ENOMEM;
  95. /* allocate a slab for the dm_ios */
  96. _io_cache = kmem_cache_create("dm_io",
  97. sizeof(struct dm_io), 0, 0, NULL, NULL);
  98. if (!_io_cache)
  99. return -ENOMEM;
  100. /* allocate a slab for the target ios */
  101. _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
  102. 0, 0, NULL, NULL);
  103. if (!_tio_cache) {
  104. kmem_cache_destroy(_io_cache);
  105. return -ENOMEM;
  106. }
  107. _major = major;
  108. r = register_blkdev(_major, _name);
  109. if (r < 0) {
  110. kmem_cache_destroy(_tio_cache);
  111. kmem_cache_destroy(_io_cache);
  112. return r;
  113. }
  114. if (!_major)
  115. _major = r;
  116. return 0;
  117. }
  118. static void local_exit(void)
  119. {
  120. kmem_cache_destroy(_tio_cache);
  121. kmem_cache_destroy(_io_cache);
  122. bioset_free(dm_set);
  123. if (unregister_blkdev(_major, _name) < 0)
  124. DMERR("devfs_unregister_blkdev failed");
  125. _major = 0;
  126. DMINFO("cleaned up");
  127. }
  128. int (*_inits[])(void) __initdata = {
  129. local_init,
  130. dm_target_init,
  131. dm_linear_init,
  132. dm_stripe_init,
  133. dm_interface_init,
  134. };
  135. void (*_exits[])(void) = {
  136. local_exit,
  137. dm_target_exit,
  138. dm_linear_exit,
  139. dm_stripe_exit,
  140. dm_interface_exit,
  141. };
  142. static int __init dm_init(void)
  143. {
  144. const int count = ARRAY_SIZE(_inits);
  145. int r, i;
  146. for (i = 0; i < count; i++) {
  147. r = _inits[i]();
  148. if (r)
  149. goto bad;
  150. }
  151. return 0;
  152. bad:
  153. while (i--)
  154. _exits[i]();
  155. return r;
  156. }
  157. static void __exit dm_exit(void)
  158. {
  159. int i = ARRAY_SIZE(_exits);
  160. while (i--)
  161. _exits[i]();
  162. }
  163. /*
  164. * Block device functions
  165. */
  166. static int dm_blk_open(struct inode *inode, struct file *file)
  167. {
  168. struct mapped_device *md;
  169. md = inode->i_bdev->bd_disk->private_data;
  170. dm_get(md);
  171. return 0;
  172. }
  173. static int dm_blk_close(struct inode *inode, struct file *file)
  174. {
  175. struct mapped_device *md;
  176. md = inode->i_bdev->bd_disk->private_data;
  177. dm_put(md);
  178. return 0;
  179. }
  180. static inline struct dm_io *alloc_io(struct mapped_device *md)
  181. {
  182. return mempool_alloc(md->io_pool, GFP_NOIO);
  183. }
  184. static inline void free_io(struct mapped_device *md, struct dm_io *io)
  185. {
  186. mempool_free(io, md->io_pool);
  187. }
  188. static inline struct target_io *alloc_tio(struct mapped_device *md)
  189. {
  190. return mempool_alloc(md->tio_pool, GFP_NOIO);
  191. }
  192. static inline void free_tio(struct mapped_device *md, struct target_io *tio)
  193. {
  194. mempool_free(tio, md->tio_pool);
  195. }
  196. /*
  197. * Add the bio to the list of deferred io.
  198. */
  199. static int queue_io(struct mapped_device *md, struct bio *bio)
  200. {
  201. down_write(&md->io_lock);
  202. if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
  203. up_write(&md->io_lock);
  204. return 1;
  205. }
  206. bio_list_add(&md->deferred, bio);
  207. up_write(&md->io_lock);
  208. return 0; /* deferred successfully */
  209. }
  210. /*
  211. * Everyone (including functions in this file), should use this
  212. * function to access the md->map field, and make sure they call
  213. * dm_table_put() when finished.
  214. */
  215. struct dm_table *dm_get_table(struct mapped_device *md)
  216. {
  217. struct dm_table *t;
  218. read_lock(&md->map_lock);
  219. t = md->map;
  220. if (t)
  221. dm_table_get(t);
  222. read_unlock(&md->map_lock);
  223. return t;
  224. }
  225. /*-----------------------------------------------------------------
  226. * CRUD START:
  227. * A more elegant soln is in the works that uses the queue
  228. * merge fn, unfortunately there are a couple of changes to
  229. * the block layer that I want to make for this. So in the
  230. * interests of getting something for people to use I give
  231. * you this clearly demarcated crap.
  232. *---------------------------------------------------------------*/
  233. /*
  234. * Decrements the number of outstanding ios that a bio has been
  235. * cloned into, completing the original io if necc.
  236. */
  237. static inline void dec_pending(struct dm_io *io, int error)
  238. {
  239. if (error)
  240. io->error = error;
  241. if (atomic_dec_and_test(&io->io_count)) {
  242. if (atomic_dec_and_test(&io->md->pending))
  243. /* nudge anyone waiting on suspend queue */
  244. wake_up(&io->md->wait);
  245. bio_endio(io->bio, io->bio->bi_size, io->error);
  246. free_io(io->md, io);
  247. }
  248. }
  249. static int clone_endio(struct bio *bio, unsigned int done, int error)
  250. {
  251. int r = 0;
  252. struct target_io *tio = bio->bi_private;
  253. struct dm_io *io = tio->io;
  254. dm_endio_fn endio = tio->ti->type->end_io;
  255. if (bio->bi_size)
  256. return 1;
  257. if (!bio_flagged(bio, BIO_UPTODATE) && !error)
  258. error = -EIO;
  259. if (endio) {
  260. r = endio(tio->ti, bio, error, &tio->info);
  261. if (r < 0)
  262. error = r;
  263. else if (r > 0)
  264. /* the target wants another shot at the io */
  265. return 1;
  266. }
  267. free_tio(io->md, tio);
  268. dec_pending(io, error);
  269. bio_put(bio);
  270. return r;
  271. }
  272. static sector_t max_io_len(struct mapped_device *md,
  273. sector_t sector, struct dm_target *ti)
  274. {
  275. sector_t offset = sector - ti->begin;
  276. sector_t len = ti->len - offset;
  277. /*
  278. * Does the target need to split even further ?
  279. */
  280. if (ti->split_io) {
  281. sector_t boundary;
  282. boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
  283. - offset;
  284. if (len > boundary)
  285. len = boundary;
  286. }
  287. return len;
  288. }
  289. static void __map_bio(struct dm_target *ti, struct bio *clone,
  290. struct target_io *tio)
  291. {
  292. int r;
  293. /*
  294. * Sanity checks.
  295. */
  296. BUG_ON(!clone->bi_size);
  297. clone->bi_end_io = clone_endio;
  298. clone->bi_private = tio;
  299. /*
  300. * Map the clone. If r == 0 we don't need to do
  301. * anything, the target has assumed ownership of
  302. * this io.
  303. */
  304. atomic_inc(&tio->io->io_count);
  305. r = ti->type->map(ti, clone, &tio->info);
  306. if (r > 0)
  307. /* the bio has been remapped so dispatch it */
  308. generic_make_request(clone);
  309. else if (r < 0) {
  310. /* error the io and bail out */
  311. struct dm_io *io = tio->io;
  312. free_tio(tio->io->md, tio);
  313. dec_pending(io, r);
  314. bio_put(clone);
  315. }
  316. }
  317. struct clone_info {
  318. struct mapped_device *md;
  319. struct dm_table *map;
  320. struct bio *bio;
  321. struct dm_io *io;
  322. sector_t sector;
  323. sector_t sector_count;
  324. unsigned short idx;
  325. };
  326. static void dm_bio_destructor(struct bio *bio)
  327. {
  328. bio_free(bio, dm_set);
  329. }
  330. /*
  331. * Creates a little bio that is just does part of a bvec.
  332. */
  333. static struct bio *split_bvec(struct bio *bio, sector_t sector,
  334. unsigned short idx, unsigned int offset,
  335. unsigned int len)
  336. {
  337. struct bio *clone;
  338. struct bio_vec *bv = bio->bi_io_vec + idx;
  339. clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
  340. clone->bi_destructor = dm_bio_destructor;
  341. *clone->bi_io_vec = *bv;
  342. clone->bi_sector = sector;
  343. clone->bi_bdev = bio->bi_bdev;
  344. clone->bi_rw = bio->bi_rw;
  345. clone->bi_vcnt = 1;
  346. clone->bi_size = to_bytes(len);
  347. clone->bi_io_vec->bv_offset = offset;
  348. clone->bi_io_vec->bv_len = clone->bi_size;
  349. return clone;
  350. }
  351. /*
  352. * Creates a bio that consists of range of complete bvecs.
  353. */
  354. static struct bio *clone_bio(struct bio *bio, sector_t sector,
  355. unsigned short idx, unsigned short bv_count,
  356. unsigned int len)
  357. {
  358. struct bio *clone;
  359. clone = bio_clone(bio, GFP_NOIO);
  360. clone->bi_sector = sector;
  361. clone->bi_idx = idx;
  362. clone->bi_vcnt = idx + bv_count;
  363. clone->bi_size = to_bytes(len);
  364. clone->bi_flags &= ~(1 << BIO_SEG_VALID);
  365. return clone;
  366. }
  367. static void __clone_and_map(struct clone_info *ci)
  368. {
  369. struct bio *clone, *bio = ci->bio;
  370. struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
  371. sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
  372. struct target_io *tio;
  373. /*
  374. * Allocate a target io object.
  375. */
  376. tio = alloc_tio(ci->md);
  377. tio->io = ci->io;
  378. tio->ti = ti;
  379. memset(&tio->info, 0, sizeof(tio->info));
  380. if (ci->sector_count <= max) {
  381. /*
  382. * Optimise for the simple case where we can do all of
  383. * the remaining io with a single clone.
  384. */
  385. clone = clone_bio(bio, ci->sector, ci->idx,
  386. bio->bi_vcnt - ci->idx, ci->sector_count);
  387. __map_bio(ti, clone, tio);
  388. ci->sector_count = 0;
  389. } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
  390. /*
  391. * There are some bvecs that don't span targets.
  392. * Do as many of these as possible.
  393. */
  394. int i;
  395. sector_t remaining = max;
  396. sector_t bv_len;
  397. for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
  398. bv_len = to_sector(bio->bi_io_vec[i].bv_len);
  399. if (bv_len > remaining)
  400. break;
  401. remaining -= bv_len;
  402. len += bv_len;
  403. }
  404. clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
  405. __map_bio(ti, clone, tio);
  406. ci->sector += len;
  407. ci->sector_count -= len;
  408. ci->idx = i;
  409. } else {
  410. /*
  411. * Create two copy bios to deal with io that has
  412. * been split across a target.
  413. */
  414. struct bio_vec *bv = bio->bi_io_vec + ci->idx;
  415. clone = split_bvec(bio, ci->sector, ci->idx,
  416. bv->bv_offset, max);
  417. __map_bio(ti, clone, tio);
  418. ci->sector += max;
  419. ci->sector_count -= max;
  420. ti = dm_table_find_target(ci->map, ci->sector);
  421. len = to_sector(bv->bv_len) - max;
  422. clone = split_bvec(bio, ci->sector, ci->idx,
  423. bv->bv_offset + to_bytes(max), len);
  424. tio = alloc_tio(ci->md);
  425. tio->io = ci->io;
  426. tio->ti = ti;
  427. memset(&tio->info, 0, sizeof(tio->info));
  428. __map_bio(ti, clone, tio);
  429. ci->sector += len;
  430. ci->sector_count -= len;
  431. ci->idx++;
  432. }
  433. }
  434. /*
  435. * Split the bio into several clones.
  436. */
  437. static void __split_bio(struct mapped_device *md, struct bio *bio)
  438. {
  439. struct clone_info ci;
  440. ci.map = dm_get_table(md);
  441. if (!ci.map) {
  442. bio_io_error(bio, bio->bi_size);
  443. return;
  444. }
  445. ci.md = md;
  446. ci.bio = bio;
  447. ci.io = alloc_io(md);
  448. ci.io->error = 0;
  449. atomic_set(&ci.io->io_count, 1);
  450. ci.io->bio = bio;
  451. ci.io->md = md;
  452. ci.sector = bio->bi_sector;
  453. ci.sector_count = bio_sectors(bio);
  454. ci.idx = bio->bi_idx;
  455. atomic_inc(&md->pending);
  456. while (ci.sector_count)
  457. __clone_and_map(&ci);
  458. /* drop the extra reference count */
  459. dec_pending(ci.io, 0);
  460. dm_table_put(ci.map);
  461. }
  462. /*-----------------------------------------------------------------
  463. * CRUD END
  464. *---------------------------------------------------------------*/
  465. /*
  466. * The request function that just remaps the bio built up by
  467. * dm_merge_bvec.
  468. */
  469. static int dm_request(request_queue_t *q, struct bio *bio)
  470. {
  471. int r;
  472. struct mapped_device *md = q->queuedata;
  473. down_read(&md->io_lock);
  474. /*
  475. * If we're suspended we have to queue
  476. * this io for later.
  477. */
  478. while (test_bit(DMF_BLOCK_IO, &md->flags)) {
  479. up_read(&md->io_lock);
  480. if (bio_rw(bio) == READA) {
  481. bio_io_error(bio, bio->bi_size);
  482. return 0;
  483. }
  484. r = queue_io(md, bio);
  485. if (r < 0) {
  486. bio_io_error(bio, bio->bi_size);
  487. return 0;
  488. } else if (r == 0)
  489. return 0; /* deferred successfully */
  490. /*
  491. * We're in a while loop, because someone could suspend
  492. * before we get to the following read lock.
  493. */
  494. down_read(&md->io_lock);
  495. }
  496. __split_bio(md, bio);
  497. up_read(&md->io_lock);
  498. return 0;
  499. }
  500. static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
  501. sector_t *error_sector)
  502. {
  503. struct mapped_device *md = q->queuedata;
  504. struct dm_table *map = dm_get_table(md);
  505. int ret = -ENXIO;
  506. if (map) {
  507. ret = dm_table_flush_all(map);
  508. dm_table_put(map);
  509. }
  510. return ret;
  511. }
  512. static void dm_unplug_all(request_queue_t *q)
  513. {
  514. struct mapped_device *md = q->queuedata;
  515. struct dm_table *map = dm_get_table(md);
  516. if (map) {
  517. dm_table_unplug_all(map);
  518. dm_table_put(map);
  519. }
  520. }
  521. static int dm_any_congested(void *congested_data, int bdi_bits)
  522. {
  523. int r;
  524. struct mapped_device *md = (struct mapped_device *) congested_data;
  525. struct dm_table *map = dm_get_table(md);
  526. if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
  527. r = bdi_bits;
  528. else
  529. r = dm_table_any_congested(map, bdi_bits);
  530. dm_table_put(map);
  531. return r;
  532. }
  533. /*-----------------------------------------------------------------
  534. * An IDR is used to keep track of allocated minor numbers.
  535. *---------------------------------------------------------------*/
  536. static DECLARE_MUTEX(_minor_lock);
  537. static DEFINE_IDR(_minor_idr);
  538. static void free_minor(unsigned int minor)
  539. {
  540. down(&_minor_lock);
  541. idr_remove(&_minor_idr, minor);
  542. up(&_minor_lock);
  543. }
  544. /*
  545. * See if the device with a specific minor # is free.
  546. */
  547. static int specific_minor(struct mapped_device *md, unsigned int minor)
  548. {
  549. int r, m;
  550. if (minor >= (1 << MINORBITS))
  551. return -EINVAL;
  552. down(&_minor_lock);
  553. if (idr_find(&_minor_idr, minor)) {
  554. r = -EBUSY;
  555. goto out;
  556. }
  557. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  558. if (!r) {
  559. r = -ENOMEM;
  560. goto out;
  561. }
  562. r = idr_get_new_above(&_minor_idr, md, minor, &m);
  563. if (r) {
  564. goto out;
  565. }
  566. if (m != minor) {
  567. idr_remove(&_minor_idr, m);
  568. r = -EBUSY;
  569. goto out;
  570. }
  571. out:
  572. up(&_minor_lock);
  573. return r;
  574. }
  575. static int next_free_minor(struct mapped_device *md, unsigned int *minor)
  576. {
  577. int r;
  578. unsigned int m;
  579. down(&_minor_lock);
  580. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  581. if (!r) {
  582. r = -ENOMEM;
  583. goto out;
  584. }
  585. r = idr_get_new(&_minor_idr, md, &m);
  586. if (r) {
  587. goto out;
  588. }
  589. if (m >= (1 << MINORBITS)) {
  590. idr_remove(&_minor_idr, m);
  591. r = -ENOSPC;
  592. goto out;
  593. }
  594. *minor = m;
  595. out:
  596. up(&_minor_lock);
  597. return r;
  598. }
  599. static struct block_device_operations dm_blk_dops;
  600. /*
  601. * Allocate and initialise a blank device with a given minor.
  602. */
  603. static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
  604. {
  605. int r;
  606. struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
  607. if (!md) {
  608. DMWARN("unable to allocate device, out of memory.");
  609. return NULL;
  610. }
  611. /* get a minor number for the dev */
  612. r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
  613. if (r < 0)
  614. goto bad1;
  615. memset(md, 0, sizeof(*md));
  616. init_rwsem(&md->io_lock);
  617. init_MUTEX(&md->suspend_lock);
  618. rwlock_init(&md->map_lock);
  619. atomic_set(&md->holders, 1);
  620. atomic_set(&md->event_nr, 0);
  621. md->queue = blk_alloc_queue(GFP_KERNEL);
  622. if (!md->queue)
  623. goto bad1;
  624. md->queue->queuedata = md;
  625. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  626. md->queue->backing_dev_info.congested_data = md;
  627. blk_queue_make_request(md->queue, dm_request);
  628. md->queue->unplug_fn = dm_unplug_all;
  629. md->queue->issue_flush_fn = dm_flush_all;
  630. md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
  631. mempool_free_slab, _io_cache);
  632. if (!md->io_pool)
  633. goto bad2;
  634. md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
  635. mempool_free_slab, _tio_cache);
  636. if (!md->tio_pool)
  637. goto bad3;
  638. md->disk = alloc_disk(1);
  639. if (!md->disk)
  640. goto bad4;
  641. md->disk->major = _major;
  642. md->disk->first_minor = minor;
  643. md->disk->fops = &dm_blk_dops;
  644. md->disk->queue = md->queue;
  645. md->disk->private_data = md;
  646. sprintf(md->disk->disk_name, "dm-%d", minor);
  647. add_disk(md->disk);
  648. atomic_set(&md->pending, 0);
  649. init_waitqueue_head(&md->wait);
  650. init_waitqueue_head(&md->eventq);
  651. return md;
  652. bad4:
  653. mempool_destroy(md->tio_pool);
  654. bad3:
  655. mempool_destroy(md->io_pool);
  656. bad2:
  657. blk_put_queue(md->queue);
  658. free_minor(minor);
  659. bad1:
  660. kfree(md);
  661. return NULL;
  662. }
  663. static void free_dev(struct mapped_device *md)
  664. {
  665. free_minor(md->disk->first_minor);
  666. mempool_destroy(md->tio_pool);
  667. mempool_destroy(md->io_pool);
  668. del_gendisk(md->disk);
  669. put_disk(md->disk);
  670. blk_put_queue(md->queue);
  671. kfree(md);
  672. }
  673. /*
  674. * Bind a table to the device.
  675. */
  676. static void event_callback(void *context)
  677. {
  678. struct mapped_device *md = (struct mapped_device *) context;
  679. atomic_inc(&md->event_nr);
  680. wake_up(&md->eventq);
  681. }
  682. static void __set_size(struct mapped_device *md, sector_t size)
  683. {
  684. set_capacity(md->disk, size);
  685. down(&md->frozen_bdev->bd_inode->i_sem);
  686. i_size_write(md->frozen_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  687. up(&md->frozen_bdev->bd_inode->i_sem);
  688. }
  689. static int __bind(struct mapped_device *md, struct dm_table *t)
  690. {
  691. request_queue_t *q = md->queue;
  692. sector_t size;
  693. size = dm_table_get_size(t);
  694. __set_size(md, size);
  695. if (size == 0)
  696. return 0;
  697. dm_table_get(t);
  698. dm_table_event_callback(t, event_callback, md);
  699. write_lock(&md->map_lock);
  700. md->map = t;
  701. dm_table_set_restrictions(t, q);
  702. write_unlock(&md->map_lock);
  703. return 0;
  704. }
  705. static void __unbind(struct mapped_device *md)
  706. {
  707. struct dm_table *map = md->map;
  708. if (!map)
  709. return;
  710. dm_table_event_callback(map, NULL, NULL);
  711. write_lock(&md->map_lock);
  712. md->map = NULL;
  713. write_unlock(&md->map_lock);
  714. dm_table_put(map);
  715. }
  716. /*
  717. * Constructor for a new device.
  718. */
  719. static int create_aux(unsigned int minor, int persistent,
  720. struct mapped_device **result)
  721. {
  722. struct mapped_device *md;
  723. md = alloc_dev(minor, persistent);
  724. if (!md)
  725. return -ENXIO;
  726. *result = md;
  727. return 0;
  728. }
  729. int dm_create(struct mapped_device **result)
  730. {
  731. return create_aux(0, 0, result);
  732. }
  733. int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
  734. {
  735. return create_aux(minor, 1, result);
  736. }
  737. void *dm_get_mdptr(dev_t dev)
  738. {
  739. struct mapped_device *md;
  740. void *mdptr = NULL;
  741. unsigned minor = MINOR(dev);
  742. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  743. return NULL;
  744. down(&_minor_lock);
  745. md = idr_find(&_minor_idr, minor);
  746. if (md && (dm_disk(md)->first_minor == minor))
  747. mdptr = md->interface_ptr;
  748. up(&_minor_lock);
  749. return mdptr;
  750. }
  751. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  752. {
  753. md->interface_ptr = ptr;
  754. }
  755. void dm_get(struct mapped_device *md)
  756. {
  757. atomic_inc(&md->holders);
  758. }
  759. void dm_put(struct mapped_device *md)
  760. {
  761. struct dm_table *map = dm_get_table(md);
  762. if (atomic_dec_and_test(&md->holders)) {
  763. if (!dm_suspended(md)) {
  764. dm_table_presuspend_targets(map);
  765. dm_table_postsuspend_targets(map);
  766. }
  767. __unbind(md);
  768. free_dev(md);
  769. }
  770. dm_table_put(map);
  771. }
  772. /*
  773. * Process the deferred bios
  774. */
  775. static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
  776. {
  777. struct bio *n;
  778. while (c) {
  779. n = c->bi_next;
  780. c->bi_next = NULL;
  781. __split_bio(md, c);
  782. c = n;
  783. }
  784. }
  785. /*
  786. * Swap in a new table (destroying old one).
  787. */
  788. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  789. {
  790. int r = -EINVAL;
  791. down(&md->suspend_lock);
  792. /* device must be suspended */
  793. if (!dm_suspended(md))
  794. goto out;
  795. __unbind(md);
  796. r = __bind(md, table);
  797. out:
  798. up(&md->suspend_lock);
  799. return r;
  800. }
  801. /*
  802. * Functions to lock and unlock any filesystem running on the
  803. * device.
  804. */
  805. static int lock_fs(struct mapped_device *md)
  806. {
  807. int r = -ENOMEM;
  808. md->frozen_bdev = bdget_disk(md->disk, 0);
  809. if (!md->frozen_bdev) {
  810. DMWARN("bdget failed in lock_fs");
  811. goto out;
  812. }
  813. WARN_ON(md->frozen_sb);
  814. md->frozen_sb = freeze_bdev(md->frozen_bdev);
  815. if (IS_ERR(md->frozen_sb)) {
  816. r = PTR_ERR(md->frozen_sb);
  817. goto out_bdput;
  818. }
  819. /* don't bdput right now, we don't want the bdev
  820. * to go away while it is locked. We'll bdput
  821. * in unlock_fs
  822. */
  823. return 0;
  824. out_bdput:
  825. bdput(md->frozen_bdev);
  826. md->frozen_sb = NULL;
  827. md->frozen_bdev = NULL;
  828. out:
  829. return r;
  830. }
  831. static void unlock_fs(struct mapped_device *md)
  832. {
  833. thaw_bdev(md->frozen_bdev, md->frozen_sb);
  834. bdput(md->frozen_bdev);
  835. md->frozen_sb = NULL;
  836. md->frozen_bdev = NULL;
  837. }
  838. /*
  839. * We need to be able to change a mapping table under a mounted
  840. * filesystem. For example we might want to move some data in
  841. * the background. Before the table can be swapped with
  842. * dm_bind_table, dm_suspend must be called to flush any in
  843. * flight bios and ensure that any further io gets deferred.
  844. */
  845. int dm_suspend(struct mapped_device *md)
  846. {
  847. struct dm_table *map = NULL;
  848. DECLARE_WAITQUEUE(wait, current);
  849. int r = -EINVAL;
  850. down(&md->suspend_lock);
  851. if (dm_suspended(md))
  852. goto out;
  853. map = dm_get_table(md);
  854. /* This does not get reverted if there's an error later. */
  855. dm_table_presuspend_targets(map);
  856. /* Flush I/O to the device. */
  857. r = lock_fs(md);
  858. if (r)
  859. goto out;
  860. /*
  861. * First we set the BLOCK_IO flag so no more ios will be mapped.
  862. */
  863. down_write(&md->io_lock);
  864. set_bit(DMF_BLOCK_IO, &md->flags);
  865. add_wait_queue(&md->wait, &wait);
  866. up_write(&md->io_lock);
  867. /* unplug */
  868. if (map)
  869. dm_table_unplug_all(map);
  870. /*
  871. * Then we wait for the already mapped ios to
  872. * complete.
  873. */
  874. while (1) {
  875. set_current_state(TASK_INTERRUPTIBLE);
  876. if (!atomic_read(&md->pending) || signal_pending(current))
  877. break;
  878. io_schedule();
  879. }
  880. set_current_state(TASK_RUNNING);
  881. down_write(&md->io_lock);
  882. remove_wait_queue(&md->wait, &wait);
  883. /* were we interrupted ? */
  884. r = -EINTR;
  885. if (atomic_read(&md->pending)) {
  886. up_write(&md->io_lock);
  887. unlock_fs(md);
  888. clear_bit(DMF_BLOCK_IO, &md->flags);
  889. goto out;
  890. }
  891. up_write(&md->io_lock);
  892. dm_table_postsuspend_targets(map);
  893. set_bit(DMF_SUSPENDED, &md->flags);
  894. r = 0;
  895. out:
  896. dm_table_put(map);
  897. up(&md->suspend_lock);
  898. return r;
  899. }
  900. int dm_resume(struct mapped_device *md)
  901. {
  902. int r = -EINVAL;
  903. struct bio *def;
  904. struct dm_table *map = NULL;
  905. down(&md->suspend_lock);
  906. if (!dm_suspended(md))
  907. goto out;
  908. map = dm_get_table(md);
  909. if (!map || !dm_table_get_size(map))
  910. goto out;
  911. dm_table_resume_targets(map);
  912. down_write(&md->io_lock);
  913. clear_bit(DMF_BLOCK_IO, &md->flags);
  914. def = bio_list_get(&md->deferred);
  915. __flush_deferred_io(md, def);
  916. up_write(&md->io_lock);
  917. unlock_fs(md);
  918. clear_bit(DMF_SUSPENDED, &md->flags);
  919. dm_table_unplug_all(map);
  920. r = 0;
  921. out:
  922. dm_table_put(map);
  923. up(&md->suspend_lock);
  924. return r;
  925. }
  926. /*-----------------------------------------------------------------
  927. * Event notification.
  928. *---------------------------------------------------------------*/
  929. uint32_t dm_get_event_nr(struct mapped_device *md)
  930. {
  931. return atomic_read(&md->event_nr);
  932. }
  933. int dm_wait_event(struct mapped_device *md, int event_nr)
  934. {
  935. return wait_event_interruptible(md->eventq,
  936. (event_nr != atomic_read(&md->event_nr)));
  937. }
  938. /*
  939. * The gendisk is only valid as long as you have a reference
  940. * count on 'md'.
  941. */
  942. struct gendisk *dm_disk(struct mapped_device *md)
  943. {
  944. return md->disk;
  945. }
  946. int dm_suspended(struct mapped_device *md)
  947. {
  948. return test_bit(DMF_SUSPENDED, &md->flags);
  949. }
  950. static struct block_device_operations dm_blk_dops = {
  951. .open = dm_blk_open,
  952. .release = dm_blk_close,
  953. .owner = THIS_MODULE
  954. };
  955. EXPORT_SYMBOL(dm_get_mapinfo);
  956. /*
  957. * module hooks
  958. */
  959. module_init(dm_init);
  960. module_exit(dm_exit);
  961. module_param(major, uint, 0);
  962. MODULE_PARM_DESC(major, "The major number of the device mapper");
  963. MODULE_DESCRIPTION(DM_NAME " driver");
  964. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  965. MODULE_LICENSE("GPL");