dm.c 25 KB

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