dm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. 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->lock);
  202. if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
  203. up_write(&md->lock);
  204. return 1;
  205. }
  206. bio_list_add(&md->deferred, bio);
  207. up_write(&md->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->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->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->lock);
  490. }
  491. __split_bio(md, bio);
  492. up_read(&md->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(md->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->lock);
  612. rwlock_init(&md->map_lock);
  613. atomic_set(&md->holders, 1);
  614. atomic_set(&md->event_nr, 0);
  615. md->queue = blk_alloc_queue(GFP_KERNEL);
  616. if (!md->queue)
  617. goto bad1;
  618. md->queue->queuedata = md;
  619. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  620. md->queue->backing_dev_info.congested_data = md;
  621. blk_queue_make_request(md->queue, dm_request);
  622. md->queue->unplug_fn = dm_unplug_all;
  623. md->queue->issue_flush_fn = dm_flush_all;
  624. md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
  625. mempool_free_slab, _io_cache);
  626. if (!md->io_pool)
  627. goto bad2;
  628. md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
  629. mempool_free_slab, _tio_cache);
  630. if (!md->tio_pool)
  631. goto bad3;
  632. md->disk = alloc_disk(1);
  633. if (!md->disk)
  634. goto bad4;
  635. md->disk->major = _major;
  636. md->disk->first_minor = minor;
  637. md->disk->fops = &dm_blk_dops;
  638. md->disk->queue = md->queue;
  639. md->disk->private_data = md;
  640. sprintf(md->disk->disk_name, "dm-%d", minor);
  641. add_disk(md->disk);
  642. atomic_set(&md->pending, 0);
  643. init_waitqueue_head(&md->wait);
  644. init_waitqueue_head(&md->eventq);
  645. return md;
  646. bad4:
  647. mempool_destroy(md->tio_pool);
  648. bad3:
  649. mempool_destroy(md->io_pool);
  650. bad2:
  651. blk_put_queue(md->queue);
  652. free_minor(minor);
  653. bad1:
  654. kfree(md);
  655. return NULL;
  656. }
  657. static void free_dev(struct mapped_device *md)
  658. {
  659. free_minor(md->disk->first_minor);
  660. mempool_destroy(md->tio_pool);
  661. mempool_destroy(md->io_pool);
  662. del_gendisk(md->disk);
  663. put_disk(md->disk);
  664. blk_put_queue(md->queue);
  665. kfree(md);
  666. }
  667. /*
  668. * Bind a table to the device.
  669. */
  670. static void event_callback(void *context)
  671. {
  672. struct mapped_device *md = (struct mapped_device *) context;
  673. atomic_inc(&md->event_nr);
  674. wake_up(&md->eventq);
  675. }
  676. static void __set_size(struct gendisk *disk, sector_t size)
  677. {
  678. struct block_device *bdev;
  679. set_capacity(disk, size);
  680. bdev = bdget_disk(disk, 0);
  681. if (bdev) {
  682. down(&bdev->bd_inode->i_sem);
  683. i_size_write(bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  684. up(&bdev->bd_inode->i_sem);
  685. bdput(bdev);
  686. }
  687. }
  688. static int __bind(struct mapped_device *md, struct dm_table *t)
  689. {
  690. request_queue_t *q = md->queue;
  691. sector_t size;
  692. size = dm_table_get_size(t);
  693. __set_size(md->disk, size);
  694. if (size == 0)
  695. return 0;
  696. write_lock(&md->map_lock);
  697. md->map = t;
  698. write_unlock(&md->map_lock);
  699. dm_table_get(t);
  700. dm_table_event_callback(md->map, event_callback, md);
  701. dm_table_set_restrictions(t, q);
  702. return 0;
  703. }
  704. static void __unbind(struct mapped_device *md)
  705. {
  706. struct dm_table *map = md->map;
  707. if (!map)
  708. return;
  709. dm_table_event_callback(map, NULL, NULL);
  710. write_lock(&md->map_lock);
  711. md->map = NULL;
  712. write_unlock(&md->map_lock);
  713. dm_table_put(map);
  714. }
  715. /*
  716. * Constructor for a new device.
  717. */
  718. static int create_aux(unsigned int minor, int persistent,
  719. struct mapped_device **result)
  720. {
  721. struct mapped_device *md;
  722. md = alloc_dev(minor, persistent);
  723. if (!md)
  724. return -ENXIO;
  725. *result = md;
  726. return 0;
  727. }
  728. int dm_create(struct mapped_device **result)
  729. {
  730. return create_aux(0, 0, result);
  731. }
  732. int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
  733. {
  734. return create_aux(minor, 1, result);
  735. }
  736. void *dm_get_mdptr(dev_t dev)
  737. {
  738. struct mapped_device *md;
  739. void *mdptr = NULL;
  740. unsigned minor = MINOR(dev);
  741. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  742. return NULL;
  743. down(&_minor_lock);
  744. md = idr_find(&_minor_idr, minor);
  745. if (md && (dm_disk(md)->first_minor == minor))
  746. mdptr = md->interface_ptr;
  747. up(&_minor_lock);
  748. return mdptr;
  749. }
  750. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  751. {
  752. md->interface_ptr = ptr;
  753. }
  754. void dm_get(struct mapped_device *md)
  755. {
  756. atomic_inc(&md->holders);
  757. }
  758. void dm_put(struct mapped_device *md)
  759. {
  760. struct dm_table *map = dm_get_table(md);
  761. if (atomic_dec_and_test(&md->holders)) {
  762. if (!test_bit(DMF_SUSPENDED, &md->flags) && map) {
  763. dm_table_presuspend_targets(map);
  764. dm_table_postsuspend_targets(map);
  765. }
  766. __unbind(md);
  767. free_dev(md);
  768. }
  769. dm_table_put(map);
  770. }
  771. /*
  772. * Process the deferred bios
  773. */
  774. static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
  775. {
  776. struct bio *n;
  777. while (c) {
  778. n = c->bi_next;
  779. c->bi_next = NULL;
  780. __split_bio(md, c);
  781. c = n;
  782. }
  783. }
  784. /*
  785. * Swap in a new table (destroying old one).
  786. */
  787. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  788. {
  789. int r;
  790. down_write(&md->lock);
  791. /* device must be suspended */
  792. if (!test_bit(DMF_SUSPENDED, &md->flags)) {
  793. up_write(&md->lock);
  794. return -EPERM;
  795. }
  796. __unbind(md);
  797. r = __bind(md, table);
  798. if (r)
  799. return r;
  800. up_write(&md->lock);
  801. return 0;
  802. }
  803. /*
  804. * Functions to lock and unlock any filesystem running on the
  805. * device.
  806. */
  807. static int __lock_fs(struct mapped_device *md)
  808. {
  809. int error = -ENOMEM;
  810. if (test_and_set_bit(DMF_FS_LOCKED, &md->flags))
  811. return 0;
  812. md->frozen_bdev = bdget_disk(md->disk, 0);
  813. if (!md->frozen_bdev) {
  814. DMWARN("bdget failed in __lock_fs");
  815. goto out;
  816. }
  817. WARN_ON(md->frozen_sb);
  818. md->frozen_sb = freeze_bdev(md->frozen_bdev);
  819. if (IS_ERR(md->frozen_sb)) {
  820. error = PTR_ERR(md->frozen_sb);
  821. goto out_bdput;
  822. }
  823. /* don't bdput right now, we don't want the bdev
  824. * to go away while it is locked. We'll bdput
  825. * in __unlock_fs
  826. */
  827. return 0;
  828. out_bdput:
  829. bdput(md->frozen_bdev);
  830. md->frozen_sb = NULL;
  831. md->frozen_bdev = NULL;
  832. out:
  833. clear_bit(DMF_FS_LOCKED, &md->flags);
  834. return error;
  835. }
  836. static void __unlock_fs(struct mapped_device *md)
  837. {
  838. if (!test_and_clear_bit(DMF_FS_LOCKED, &md->flags))
  839. return;
  840. thaw_bdev(md->frozen_bdev, md->frozen_sb);
  841. bdput(md->frozen_bdev);
  842. md->frozen_sb = NULL;
  843. md->frozen_bdev = NULL;
  844. }
  845. /*
  846. * We need to be able to change a mapping table under a mounted
  847. * filesystem. For example we might want to move some data in
  848. * the background. Before the table can be swapped with
  849. * dm_bind_table, dm_suspend must be called to flush any in
  850. * flight bios and ensure that any further io gets deferred.
  851. */
  852. int dm_suspend(struct mapped_device *md)
  853. {
  854. struct dm_table *map;
  855. DECLARE_WAITQUEUE(wait, current);
  856. int error = -EINVAL;
  857. /* Flush I/O to the device. */
  858. down_read(&md->lock);
  859. if (test_bit(DMF_BLOCK_IO, &md->flags))
  860. goto out_read_unlock;
  861. map = dm_get_table(md);
  862. if (map)
  863. /* This does not get reverted if there's an error later. */
  864. dm_table_presuspend_targets(map);
  865. error = __lock_fs(md);
  866. if (error) {
  867. dm_table_put(map);
  868. goto out_read_unlock;
  869. }
  870. up_read(&md->lock);
  871. /*
  872. * First we set the BLOCK_IO flag so no more ios will be mapped.
  873. *
  874. * If the flag is already set we know another thread is trying to
  875. * suspend as well, so we leave the fs locked for this thread.
  876. */
  877. error = -EINVAL;
  878. down_write(&md->lock);
  879. if (test_and_set_bit(DMF_BLOCK_IO, &md->flags)) {
  880. if (map)
  881. dm_table_put(map);
  882. goto out_write_unlock;
  883. }
  884. add_wait_queue(&md->wait, &wait);
  885. up_write(&md->lock);
  886. /* unplug */
  887. if (map) {
  888. dm_table_unplug_all(map);
  889. dm_table_put(map);
  890. }
  891. /*
  892. * Then we wait for the already mapped ios to
  893. * complete.
  894. */
  895. while (1) {
  896. set_current_state(TASK_INTERRUPTIBLE);
  897. if (!atomic_read(&md->pending) || signal_pending(current))
  898. break;
  899. io_schedule();
  900. }
  901. set_current_state(TASK_RUNNING);
  902. down_write(&md->lock);
  903. remove_wait_queue(&md->wait, &wait);
  904. /* were we interrupted ? */
  905. error = -EINTR;
  906. if (atomic_read(&md->pending))
  907. goto out_unfreeze;
  908. set_bit(DMF_SUSPENDED, &md->flags);
  909. map = dm_get_table(md);
  910. if (map)
  911. dm_table_postsuspend_targets(map);
  912. dm_table_put(map);
  913. up_write(&md->lock);
  914. return 0;
  915. out_unfreeze:
  916. __unlock_fs(md);
  917. clear_bit(DMF_BLOCK_IO, &md->flags);
  918. out_write_unlock:
  919. up_write(&md->lock);
  920. return error;
  921. out_read_unlock:
  922. up_read(&md->lock);
  923. return error;
  924. }
  925. int dm_resume(struct mapped_device *md)
  926. {
  927. struct bio *def;
  928. struct dm_table *map = dm_get_table(md);
  929. down_write(&md->lock);
  930. if (!map ||
  931. !test_bit(DMF_SUSPENDED, &md->flags) ||
  932. !dm_table_get_size(map)) {
  933. up_write(&md->lock);
  934. dm_table_put(map);
  935. return -EINVAL;
  936. }
  937. dm_table_resume_targets(map);
  938. clear_bit(DMF_SUSPENDED, &md->flags);
  939. clear_bit(DMF_BLOCK_IO, &md->flags);
  940. def = bio_list_get(&md->deferred);
  941. __flush_deferred_io(md, def);
  942. up_write(&md->lock);
  943. __unlock_fs(md);
  944. dm_table_unplug_all(map);
  945. dm_table_put(map);
  946. return 0;
  947. }
  948. /*-----------------------------------------------------------------
  949. * Event notification.
  950. *---------------------------------------------------------------*/
  951. uint32_t dm_get_event_nr(struct mapped_device *md)
  952. {
  953. return atomic_read(&md->event_nr);
  954. }
  955. int dm_wait_event(struct mapped_device *md, int event_nr)
  956. {
  957. return wait_event_interruptible(md->eventq,
  958. (event_nr != atomic_read(&md->event_nr)));
  959. }
  960. /*
  961. * The gendisk is only valid as long as you have a reference
  962. * count on 'md'.
  963. */
  964. struct gendisk *dm_disk(struct mapped_device *md)
  965. {
  966. return md->disk;
  967. }
  968. int dm_suspended(struct mapped_device *md)
  969. {
  970. return test_bit(DMF_SUSPENDED, &md->flags);
  971. }
  972. static struct block_device_operations dm_blk_dops = {
  973. .open = dm_blk_open,
  974. .release = dm_blk_close,
  975. .owner = THIS_MODULE
  976. };
  977. EXPORT_SYMBOL(dm_get_mapinfo);
  978. /*
  979. * module hooks
  980. */
  981. module_init(dm_init);
  982. module_exit(dm_exit);
  983. module_param(major, uint, 0);
  984. MODULE_PARM_DESC(major, "The major number of the device mapper");
  985. MODULE_DESCRIPTION(DM_NAME " driver");
  986. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  987. MODULE_LICENSE("GPL");