dm.c 24 KB

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