dm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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. /*
  327. * Creates a little bio that is just does part of a bvec.
  328. */
  329. static struct bio *split_bvec(struct bio *bio, sector_t sector,
  330. unsigned short idx, unsigned int offset,
  331. unsigned int len)
  332. {
  333. struct bio *clone;
  334. struct bio_vec *bv = bio->bi_io_vec + idx;
  335. clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
  336. *clone->bi_io_vec = *bv;
  337. clone->bi_sector = sector;
  338. clone->bi_bdev = bio->bi_bdev;
  339. clone->bi_rw = bio->bi_rw;
  340. clone->bi_vcnt = 1;
  341. clone->bi_size = to_bytes(len);
  342. clone->bi_io_vec->bv_offset = offset;
  343. clone->bi_io_vec->bv_len = clone->bi_size;
  344. return clone;
  345. }
  346. /*
  347. * Creates a bio that consists of range of complete bvecs.
  348. */
  349. static struct bio *clone_bio(struct bio *bio, sector_t sector,
  350. unsigned short idx, unsigned short bv_count,
  351. unsigned int len)
  352. {
  353. struct bio *clone;
  354. clone = bio_clone(bio, GFP_NOIO);
  355. clone->bi_sector = sector;
  356. clone->bi_idx = idx;
  357. clone->bi_vcnt = idx + bv_count;
  358. clone->bi_size = to_bytes(len);
  359. clone->bi_flags &= ~(1 << BIO_SEG_VALID);
  360. return clone;
  361. }
  362. static void __clone_and_map(struct clone_info *ci)
  363. {
  364. struct bio *clone, *bio = ci->bio;
  365. struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
  366. sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
  367. struct target_io *tio;
  368. /*
  369. * Allocate a target io object.
  370. */
  371. tio = alloc_tio(ci->md);
  372. tio->io = ci->io;
  373. tio->ti = ti;
  374. memset(&tio->info, 0, sizeof(tio->info));
  375. if (ci->sector_count <= max) {
  376. /*
  377. * Optimise for the simple case where we can do all of
  378. * the remaining io with a single clone.
  379. */
  380. clone = clone_bio(bio, ci->sector, ci->idx,
  381. bio->bi_vcnt - ci->idx, ci->sector_count);
  382. __map_bio(ti, clone, tio);
  383. ci->sector_count = 0;
  384. } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
  385. /*
  386. * There are some bvecs that don't span targets.
  387. * Do as many of these as possible.
  388. */
  389. int i;
  390. sector_t remaining = max;
  391. sector_t bv_len;
  392. for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
  393. bv_len = to_sector(bio->bi_io_vec[i].bv_len);
  394. if (bv_len > remaining)
  395. break;
  396. remaining -= bv_len;
  397. len += bv_len;
  398. }
  399. clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
  400. __map_bio(ti, clone, tio);
  401. ci->sector += len;
  402. ci->sector_count -= len;
  403. ci->idx = i;
  404. } else {
  405. /*
  406. * Create two copy bios to deal with io that has
  407. * been split across a target.
  408. */
  409. struct bio_vec *bv = bio->bi_io_vec + ci->idx;
  410. clone = split_bvec(bio, ci->sector, ci->idx,
  411. bv->bv_offset, max);
  412. __map_bio(ti, clone, tio);
  413. ci->sector += max;
  414. ci->sector_count -= max;
  415. ti = dm_table_find_target(ci->map, ci->sector);
  416. len = to_sector(bv->bv_len) - max;
  417. clone = split_bvec(bio, ci->sector, ci->idx,
  418. bv->bv_offset + to_bytes(max), len);
  419. tio = alloc_tio(ci->md);
  420. tio->io = ci->io;
  421. tio->ti = ti;
  422. memset(&tio->info, 0, sizeof(tio->info));
  423. __map_bio(ti, clone, tio);
  424. ci->sector += len;
  425. ci->sector_count -= len;
  426. ci->idx++;
  427. }
  428. }
  429. /*
  430. * Split the bio into several clones.
  431. */
  432. static void __split_bio(struct mapped_device *md, struct bio *bio)
  433. {
  434. struct clone_info ci;
  435. ci.map = dm_get_table(md);
  436. if (!ci.map) {
  437. bio_io_error(bio, bio->bi_size);
  438. return;
  439. }
  440. ci.md = md;
  441. ci.bio = bio;
  442. ci.io = alloc_io(md);
  443. ci.io->error = 0;
  444. atomic_set(&ci.io->io_count, 1);
  445. ci.io->bio = bio;
  446. ci.io->md = md;
  447. ci.sector = bio->bi_sector;
  448. ci.sector_count = bio_sectors(bio);
  449. ci.idx = bio->bi_idx;
  450. atomic_inc(&md->pending);
  451. while (ci.sector_count)
  452. __clone_and_map(&ci);
  453. /* drop the extra reference count */
  454. dec_pending(ci.io, 0);
  455. dm_table_put(ci.map);
  456. }
  457. /*-----------------------------------------------------------------
  458. * CRUD END
  459. *---------------------------------------------------------------*/
  460. /*
  461. * The request function that just remaps the bio built up by
  462. * dm_merge_bvec.
  463. */
  464. static int dm_request(request_queue_t *q, struct bio *bio)
  465. {
  466. int r;
  467. struct mapped_device *md = q->queuedata;
  468. down_read(&md->io_lock);
  469. /*
  470. * If we're suspended we have to queue
  471. * this io for later.
  472. */
  473. while (test_bit(DMF_BLOCK_IO, &md->flags)) {
  474. up_read(&md->io_lock);
  475. if (bio_rw(bio) == READA) {
  476. bio_io_error(bio, bio->bi_size);
  477. return 0;
  478. }
  479. r = queue_io(md, bio);
  480. if (r < 0) {
  481. bio_io_error(bio, bio->bi_size);
  482. return 0;
  483. } else if (r == 0)
  484. return 0; /* deferred successfully */
  485. /*
  486. * We're in a while loop, because someone could suspend
  487. * before we get to the following read lock.
  488. */
  489. down_read(&md->io_lock);
  490. }
  491. __split_bio(md, bio);
  492. up_read(&md->io_lock);
  493. return 0;
  494. }
  495. static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
  496. sector_t *error_sector)
  497. {
  498. struct mapped_device *md = q->queuedata;
  499. struct dm_table *map = dm_get_table(md);
  500. int ret = -ENXIO;
  501. if (map) {
  502. ret = dm_table_flush_all(map);
  503. dm_table_put(map);
  504. }
  505. return ret;
  506. }
  507. static void dm_unplug_all(request_queue_t *q)
  508. {
  509. struct mapped_device *md = q->queuedata;
  510. struct dm_table *map = dm_get_table(md);
  511. if (map) {
  512. dm_table_unplug_all(map);
  513. dm_table_put(map);
  514. }
  515. }
  516. static int dm_any_congested(void *congested_data, int bdi_bits)
  517. {
  518. int r;
  519. struct mapped_device *md = (struct mapped_device *) congested_data;
  520. struct dm_table *map = dm_get_table(md);
  521. if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
  522. r = bdi_bits;
  523. else
  524. r = dm_table_any_congested(map, bdi_bits);
  525. dm_table_put(map);
  526. return r;
  527. }
  528. /*-----------------------------------------------------------------
  529. * An IDR is used to keep track of allocated minor numbers.
  530. *---------------------------------------------------------------*/
  531. static DECLARE_MUTEX(_minor_lock);
  532. static DEFINE_IDR(_minor_idr);
  533. static void free_minor(unsigned int minor)
  534. {
  535. down(&_minor_lock);
  536. idr_remove(&_minor_idr, minor);
  537. up(&_minor_lock);
  538. }
  539. /*
  540. * See if the device with a specific minor # is free.
  541. */
  542. static int specific_minor(struct mapped_device *md, unsigned int minor)
  543. {
  544. int r, m;
  545. if (minor >= (1 << MINORBITS))
  546. return -EINVAL;
  547. down(&_minor_lock);
  548. if (idr_find(&_minor_idr, minor)) {
  549. r = -EBUSY;
  550. goto out;
  551. }
  552. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  553. if (!r) {
  554. r = -ENOMEM;
  555. goto out;
  556. }
  557. r = idr_get_new_above(&_minor_idr, md, minor, &m);
  558. if (r) {
  559. goto out;
  560. }
  561. if (m != minor) {
  562. idr_remove(&_minor_idr, m);
  563. r = -EBUSY;
  564. goto out;
  565. }
  566. out:
  567. up(&_minor_lock);
  568. return r;
  569. }
  570. static int next_free_minor(struct mapped_device *md, unsigned int *minor)
  571. {
  572. int r;
  573. unsigned int m;
  574. down(&_minor_lock);
  575. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  576. if (!r) {
  577. r = -ENOMEM;
  578. goto out;
  579. }
  580. r = idr_get_new(&_minor_idr, md, &m);
  581. if (r) {
  582. goto out;
  583. }
  584. if (m >= (1 << MINORBITS)) {
  585. idr_remove(&_minor_idr, m);
  586. r = -ENOSPC;
  587. goto out;
  588. }
  589. *minor = m;
  590. out:
  591. up(&_minor_lock);
  592. return r;
  593. }
  594. static struct block_device_operations dm_blk_dops;
  595. /*
  596. * Allocate and initialise a blank device with a given minor.
  597. */
  598. static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
  599. {
  600. int r;
  601. struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
  602. if (!md) {
  603. DMWARN("unable to allocate device, out of memory.");
  604. return NULL;
  605. }
  606. /* get a minor number for the dev */
  607. r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
  608. if (r < 0)
  609. goto bad1;
  610. memset(md, 0, sizeof(*md));
  611. init_rwsem(&md->io_lock);
  612. init_MUTEX(&md->suspend_lock);
  613. rwlock_init(&md->map_lock);
  614. atomic_set(&md->holders, 1);
  615. atomic_set(&md->event_nr, 0);
  616. md->queue = blk_alloc_queue(GFP_KERNEL);
  617. if (!md->queue)
  618. goto bad1;
  619. md->queue->queuedata = md;
  620. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  621. md->queue->backing_dev_info.congested_data = md;
  622. blk_queue_make_request(md->queue, dm_request);
  623. md->queue->unplug_fn = dm_unplug_all;
  624. md->queue->issue_flush_fn = dm_flush_all;
  625. md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
  626. mempool_free_slab, _io_cache);
  627. if (!md->io_pool)
  628. goto bad2;
  629. md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
  630. mempool_free_slab, _tio_cache);
  631. if (!md->tio_pool)
  632. goto bad3;
  633. md->disk = alloc_disk(1);
  634. if (!md->disk)
  635. goto bad4;
  636. md->disk->major = _major;
  637. md->disk->first_minor = minor;
  638. md->disk->fops = &dm_blk_dops;
  639. md->disk->queue = md->queue;
  640. md->disk->private_data = md;
  641. sprintf(md->disk->disk_name, "dm-%d", minor);
  642. add_disk(md->disk);
  643. atomic_set(&md->pending, 0);
  644. init_waitqueue_head(&md->wait);
  645. init_waitqueue_head(&md->eventq);
  646. return md;
  647. bad4:
  648. mempool_destroy(md->tio_pool);
  649. bad3:
  650. mempool_destroy(md->io_pool);
  651. bad2:
  652. blk_put_queue(md->queue);
  653. free_minor(minor);
  654. bad1:
  655. kfree(md);
  656. return NULL;
  657. }
  658. static void free_dev(struct mapped_device *md)
  659. {
  660. free_minor(md->disk->first_minor);
  661. mempool_destroy(md->tio_pool);
  662. mempool_destroy(md->io_pool);
  663. del_gendisk(md->disk);
  664. put_disk(md->disk);
  665. blk_put_queue(md->queue);
  666. kfree(md);
  667. }
  668. /*
  669. * Bind a table to the device.
  670. */
  671. static void event_callback(void *context)
  672. {
  673. struct mapped_device *md = (struct mapped_device *) context;
  674. atomic_inc(&md->event_nr);
  675. wake_up(&md->eventq);
  676. }
  677. static void __set_size(struct mapped_device *md, sector_t size)
  678. {
  679. set_capacity(md->disk, size);
  680. down(&md->frozen_bdev->bd_inode->i_sem);
  681. i_size_write(md->frozen_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  682. up(&md->frozen_bdev->bd_inode->i_sem);
  683. }
  684. static int __bind(struct mapped_device *md, struct dm_table *t)
  685. {
  686. request_queue_t *q = md->queue;
  687. sector_t size;
  688. size = dm_table_get_size(t);
  689. __set_size(md, size);
  690. if (size == 0)
  691. return 0;
  692. dm_table_get(t);
  693. dm_table_event_callback(t, event_callback, md);
  694. write_lock(&md->map_lock);
  695. md->map = t;
  696. dm_table_set_restrictions(t, q);
  697. write_unlock(&md->map_lock);
  698. return 0;
  699. }
  700. static void __unbind(struct mapped_device *md)
  701. {
  702. struct dm_table *map = md->map;
  703. if (!map)
  704. return;
  705. dm_table_event_callback(map, NULL, NULL);
  706. write_lock(&md->map_lock);
  707. md->map = NULL;
  708. write_unlock(&md->map_lock);
  709. dm_table_put(map);
  710. }
  711. /*
  712. * Constructor for a new device.
  713. */
  714. static int create_aux(unsigned int minor, int persistent,
  715. struct mapped_device **result)
  716. {
  717. struct mapped_device *md;
  718. md = alloc_dev(minor, persistent);
  719. if (!md)
  720. return -ENXIO;
  721. *result = md;
  722. return 0;
  723. }
  724. int dm_create(struct mapped_device **result)
  725. {
  726. return create_aux(0, 0, result);
  727. }
  728. int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
  729. {
  730. return create_aux(minor, 1, result);
  731. }
  732. void *dm_get_mdptr(dev_t dev)
  733. {
  734. struct mapped_device *md;
  735. void *mdptr = NULL;
  736. unsigned minor = MINOR(dev);
  737. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  738. return NULL;
  739. down(&_minor_lock);
  740. md = idr_find(&_minor_idr, minor);
  741. if (md && (dm_disk(md)->first_minor == minor))
  742. mdptr = md->interface_ptr;
  743. up(&_minor_lock);
  744. return mdptr;
  745. }
  746. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  747. {
  748. md->interface_ptr = ptr;
  749. }
  750. void dm_get(struct mapped_device *md)
  751. {
  752. atomic_inc(&md->holders);
  753. }
  754. void dm_put(struct mapped_device *md)
  755. {
  756. struct dm_table *map = dm_get_table(md);
  757. if (atomic_dec_and_test(&md->holders)) {
  758. if (!dm_suspended(md)) {
  759. dm_table_presuspend_targets(map);
  760. dm_table_postsuspend_targets(map);
  761. }
  762. __unbind(md);
  763. free_dev(md);
  764. }
  765. dm_table_put(map);
  766. }
  767. /*
  768. * Process the deferred bios
  769. */
  770. static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
  771. {
  772. struct bio *n;
  773. while (c) {
  774. n = c->bi_next;
  775. c->bi_next = NULL;
  776. __split_bio(md, c);
  777. c = n;
  778. }
  779. }
  780. /*
  781. * Swap in a new table (destroying old one).
  782. */
  783. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  784. {
  785. int r = -EINVAL;
  786. down(&md->suspend_lock);
  787. /* device must be suspended */
  788. if (!dm_suspended(md))
  789. goto out;
  790. __unbind(md);
  791. r = __bind(md, table);
  792. out:
  793. up(&md->suspend_lock);
  794. return r;
  795. }
  796. /*
  797. * Functions to lock and unlock any filesystem running on the
  798. * device.
  799. */
  800. static int lock_fs(struct mapped_device *md)
  801. {
  802. int r = -ENOMEM;
  803. md->frozen_bdev = bdget_disk(md->disk, 0);
  804. if (!md->frozen_bdev) {
  805. DMWARN("bdget failed in lock_fs");
  806. goto out;
  807. }
  808. WARN_ON(md->frozen_sb);
  809. md->frozen_sb = freeze_bdev(md->frozen_bdev);
  810. if (IS_ERR(md->frozen_sb)) {
  811. r = PTR_ERR(md->frozen_sb);
  812. goto out_bdput;
  813. }
  814. /* don't bdput right now, we don't want the bdev
  815. * to go away while it is locked. We'll bdput
  816. * in unlock_fs
  817. */
  818. return 0;
  819. out_bdput:
  820. bdput(md->frozen_bdev);
  821. md->frozen_sb = NULL;
  822. md->frozen_bdev = NULL;
  823. out:
  824. return r;
  825. }
  826. static void unlock_fs(struct mapped_device *md)
  827. {
  828. thaw_bdev(md->frozen_bdev, md->frozen_sb);
  829. bdput(md->frozen_bdev);
  830. md->frozen_sb = NULL;
  831. md->frozen_bdev = NULL;
  832. }
  833. /*
  834. * We need to be able to change a mapping table under a mounted
  835. * filesystem. For example we might want to move some data in
  836. * the background. Before the table can be swapped with
  837. * dm_bind_table, dm_suspend must be called to flush any in
  838. * flight bios and ensure that any further io gets deferred.
  839. */
  840. int dm_suspend(struct mapped_device *md)
  841. {
  842. struct dm_table *map = NULL;
  843. DECLARE_WAITQUEUE(wait, current);
  844. int r = -EINVAL;
  845. down(&md->suspend_lock);
  846. if (dm_suspended(md))
  847. goto out;
  848. map = dm_get_table(md);
  849. /* This does not get reverted if there's an error later. */
  850. dm_table_presuspend_targets(map);
  851. /* Flush I/O to the device. */
  852. r = lock_fs(md);
  853. if (r)
  854. goto out;
  855. /*
  856. * First we set the BLOCK_IO flag so no more ios will be mapped.
  857. */
  858. down_write(&md->io_lock);
  859. set_bit(DMF_BLOCK_IO, &md->flags);
  860. add_wait_queue(&md->wait, &wait);
  861. up_write(&md->io_lock);
  862. /* unplug */
  863. if (map)
  864. dm_table_unplug_all(map);
  865. /*
  866. * Then we wait for the already mapped ios to
  867. * complete.
  868. */
  869. while (1) {
  870. set_current_state(TASK_INTERRUPTIBLE);
  871. if (!atomic_read(&md->pending) || signal_pending(current))
  872. break;
  873. io_schedule();
  874. }
  875. set_current_state(TASK_RUNNING);
  876. down_write(&md->io_lock);
  877. remove_wait_queue(&md->wait, &wait);
  878. /* were we interrupted ? */
  879. r = -EINTR;
  880. if (atomic_read(&md->pending)) {
  881. up_write(&md->io_lock);
  882. unlock_fs(md);
  883. clear_bit(DMF_BLOCK_IO, &md->flags);
  884. goto out;
  885. }
  886. up_write(&md->io_lock);
  887. dm_table_postsuspend_targets(map);
  888. set_bit(DMF_SUSPENDED, &md->flags);
  889. r = 0;
  890. out:
  891. dm_table_put(map);
  892. up(&md->suspend_lock);
  893. return r;
  894. }
  895. int dm_resume(struct mapped_device *md)
  896. {
  897. int r = -EINVAL;
  898. struct bio *def;
  899. struct dm_table *map = NULL;
  900. down(&md->suspend_lock);
  901. if (!dm_suspended(md))
  902. goto out;
  903. map = dm_get_table(md);
  904. if (!map || !dm_table_get_size(map))
  905. goto out;
  906. dm_table_resume_targets(map);
  907. down_write(&md->io_lock);
  908. clear_bit(DMF_BLOCK_IO, &md->flags);
  909. def = bio_list_get(&md->deferred);
  910. __flush_deferred_io(md, def);
  911. up_write(&md->io_lock);
  912. unlock_fs(md);
  913. clear_bit(DMF_SUSPENDED, &md->flags);
  914. dm_table_unplug_all(map);
  915. r = 0;
  916. out:
  917. dm_table_put(map);
  918. up(&md->suspend_lock);
  919. return r;
  920. }
  921. /*-----------------------------------------------------------------
  922. * Event notification.
  923. *---------------------------------------------------------------*/
  924. uint32_t dm_get_event_nr(struct mapped_device *md)
  925. {
  926. return atomic_read(&md->event_nr);
  927. }
  928. int dm_wait_event(struct mapped_device *md, int event_nr)
  929. {
  930. return wait_event_interruptible(md->eventq,
  931. (event_nr != atomic_read(&md->event_nr)));
  932. }
  933. /*
  934. * The gendisk is only valid as long as you have a reference
  935. * count on 'md'.
  936. */
  937. struct gendisk *dm_disk(struct mapped_device *md)
  938. {
  939. return md->disk;
  940. }
  941. int dm_suspended(struct mapped_device *md)
  942. {
  943. return test_bit(DMF_SUSPENDED, &md->flags);
  944. }
  945. static struct block_device_operations dm_blk_dops = {
  946. .open = dm_blk_open,
  947. .release = dm_blk_close,
  948. .owner = THIS_MODULE
  949. };
  950. EXPORT_SYMBOL(dm_get_mapinfo);
  951. /*
  952. * module hooks
  953. */
  954. module_init(dm_init);
  955. module_exit(dm_exit);
  956. module_param(major, uint, 0);
  957. MODULE_PARM_DESC(major, "The major number of the device mapper");
  958. MODULE_DESCRIPTION(DM_NAME " driver");
  959. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  960. MODULE_LICENSE("GPL");