dm.c 24 KB

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