dm.c 26 KB

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