dm.c 26 KB

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