dm.c 23 KB

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