dm.c 27 KB

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