dm.c 28 KB

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