dm.c 26 KB

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