dm.c 23 KB

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