dm.c 25 KB

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