dm.c 29 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 struct kmem_cache *_io_cache;
  103. static struct kmem_cache *_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. } else if (r < 0) {
  439. /* error the io and bail out */
  440. md = tio->io->md;
  441. dec_pending(tio->io, r);
  442. /*
  443. * Store bio_set for cleanup.
  444. */
  445. clone->bi_private = md->bs;
  446. bio_put(clone);
  447. free_tio(md, tio);
  448. }
  449. }
  450. struct clone_info {
  451. struct mapped_device *md;
  452. struct dm_table *map;
  453. struct bio *bio;
  454. struct dm_io *io;
  455. sector_t sector;
  456. sector_t sector_count;
  457. unsigned short idx;
  458. };
  459. static void dm_bio_destructor(struct bio *bio)
  460. {
  461. struct bio_set *bs = bio->bi_private;
  462. bio_free(bio, bs);
  463. }
  464. /*
  465. * Creates a little bio that is just does part of a bvec.
  466. */
  467. static struct bio *split_bvec(struct bio *bio, sector_t sector,
  468. unsigned short idx, unsigned int offset,
  469. unsigned int len, struct bio_set *bs)
  470. {
  471. struct bio *clone;
  472. struct bio_vec *bv = bio->bi_io_vec + idx;
  473. clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
  474. clone->bi_destructor = dm_bio_destructor;
  475. *clone->bi_io_vec = *bv;
  476. clone->bi_sector = sector;
  477. clone->bi_bdev = bio->bi_bdev;
  478. clone->bi_rw = bio->bi_rw;
  479. clone->bi_vcnt = 1;
  480. clone->bi_size = to_bytes(len);
  481. clone->bi_io_vec->bv_offset = offset;
  482. clone->bi_io_vec->bv_len = clone->bi_size;
  483. return clone;
  484. }
  485. /*
  486. * Creates a bio that consists of range of complete bvecs.
  487. */
  488. static struct bio *clone_bio(struct bio *bio, sector_t sector,
  489. unsigned short idx, unsigned short bv_count,
  490. unsigned int len, struct bio_set *bs)
  491. {
  492. struct bio *clone;
  493. clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
  494. __bio_clone(clone, bio);
  495. clone->bi_destructor = dm_bio_destructor;
  496. clone->bi_sector = sector;
  497. clone->bi_idx = idx;
  498. clone->bi_vcnt = idx + bv_count;
  499. clone->bi_size = to_bytes(len);
  500. clone->bi_flags &= ~(1 << BIO_SEG_VALID);
  501. return clone;
  502. }
  503. static void __clone_and_map(struct clone_info *ci)
  504. {
  505. struct bio *clone, *bio = ci->bio;
  506. struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
  507. sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
  508. struct target_io *tio;
  509. /*
  510. * Allocate a target io object.
  511. */
  512. tio = alloc_tio(ci->md);
  513. tio->io = ci->io;
  514. tio->ti = ti;
  515. memset(&tio->info, 0, sizeof(tio->info));
  516. if (ci->sector_count <= max) {
  517. /*
  518. * Optimise for the simple case where we can do all of
  519. * the remaining io with a single clone.
  520. */
  521. clone = clone_bio(bio, ci->sector, ci->idx,
  522. bio->bi_vcnt - ci->idx, ci->sector_count,
  523. ci->md->bs);
  524. __map_bio(ti, clone, tio);
  525. ci->sector_count = 0;
  526. } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
  527. /*
  528. * There are some bvecs that don't span targets.
  529. * Do as many of these as possible.
  530. */
  531. int i;
  532. sector_t remaining = max;
  533. sector_t bv_len;
  534. for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
  535. bv_len = to_sector(bio->bi_io_vec[i].bv_len);
  536. if (bv_len > remaining)
  537. break;
  538. remaining -= bv_len;
  539. len += bv_len;
  540. }
  541. clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
  542. ci->md->bs);
  543. __map_bio(ti, clone, tio);
  544. ci->sector += len;
  545. ci->sector_count -= len;
  546. ci->idx = i;
  547. } else {
  548. /*
  549. * Handle a bvec that must be split between two or more targets.
  550. */
  551. struct bio_vec *bv = bio->bi_io_vec + ci->idx;
  552. sector_t remaining = to_sector(bv->bv_len);
  553. unsigned int offset = 0;
  554. do {
  555. if (offset) {
  556. ti = dm_table_find_target(ci->map, ci->sector);
  557. max = max_io_len(ci->md, ci->sector, ti);
  558. tio = alloc_tio(ci->md);
  559. tio->io = ci->io;
  560. tio->ti = ti;
  561. memset(&tio->info, 0, sizeof(tio->info));
  562. }
  563. len = min(remaining, max);
  564. clone = split_bvec(bio, ci->sector, ci->idx,
  565. bv->bv_offset + offset, len,
  566. ci->md->bs);
  567. __map_bio(ti, clone, tio);
  568. ci->sector += len;
  569. ci->sector_count -= len;
  570. offset += to_bytes(len);
  571. } while (remaining -= len);
  572. ci->idx++;
  573. }
  574. }
  575. /*
  576. * Split the bio into several clones.
  577. */
  578. static void __split_bio(struct mapped_device *md, struct bio *bio)
  579. {
  580. struct clone_info ci;
  581. ci.map = dm_get_table(md);
  582. if (!ci.map) {
  583. bio_io_error(bio, bio->bi_size);
  584. return;
  585. }
  586. ci.md = md;
  587. ci.bio = bio;
  588. ci.io = alloc_io(md);
  589. ci.io->error = 0;
  590. atomic_set(&ci.io->io_count, 1);
  591. ci.io->bio = bio;
  592. ci.io->md = md;
  593. ci.sector = bio->bi_sector;
  594. ci.sector_count = bio_sectors(bio);
  595. ci.idx = bio->bi_idx;
  596. start_io_acct(ci.io);
  597. while (ci.sector_count)
  598. __clone_and_map(&ci);
  599. /* drop the extra reference count */
  600. dec_pending(ci.io, 0);
  601. dm_table_put(ci.map);
  602. }
  603. /*-----------------------------------------------------------------
  604. * CRUD END
  605. *---------------------------------------------------------------*/
  606. /*
  607. * The request function that just remaps the bio built up by
  608. * dm_merge_bvec.
  609. */
  610. static int dm_request(request_queue_t *q, struct bio *bio)
  611. {
  612. int r;
  613. int rw = bio_data_dir(bio);
  614. struct mapped_device *md = q->queuedata;
  615. down_read(&md->io_lock);
  616. disk_stat_inc(dm_disk(md), ios[rw]);
  617. disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
  618. /*
  619. * If we're suspended we have to queue
  620. * this io for later.
  621. */
  622. while (test_bit(DMF_BLOCK_IO, &md->flags)) {
  623. up_read(&md->io_lock);
  624. if (bio_rw(bio) == READA) {
  625. bio_io_error(bio, bio->bi_size);
  626. return 0;
  627. }
  628. r = queue_io(md, bio);
  629. if (r < 0) {
  630. bio_io_error(bio, bio->bi_size);
  631. return 0;
  632. } else if (r == 0)
  633. return 0; /* deferred successfully */
  634. /*
  635. * We're in a while loop, because someone could suspend
  636. * before we get to the following read lock.
  637. */
  638. down_read(&md->io_lock);
  639. }
  640. __split_bio(md, bio);
  641. up_read(&md->io_lock);
  642. return 0;
  643. }
  644. static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
  645. sector_t *error_sector)
  646. {
  647. struct mapped_device *md = q->queuedata;
  648. struct dm_table *map = dm_get_table(md);
  649. int ret = -ENXIO;
  650. if (map) {
  651. ret = dm_table_flush_all(map);
  652. dm_table_put(map);
  653. }
  654. return ret;
  655. }
  656. static void dm_unplug_all(request_queue_t *q)
  657. {
  658. struct mapped_device *md = q->queuedata;
  659. struct dm_table *map = dm_get_table(md);
  660. if (map) {
  661. dm_table_unplug_all(map);
  662. dm_table_put(map);
  663. }
  664. }
  665. static int dm_any_congested(void *congested_data, int bdi_bits)
  666. {
  667. int r;
  668. struct mapped_device *md = (struct mapped_device *) congested_data;
  669. struct dm_table *map = dm_get_table(md);
  670. if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
  671. r = bdi_bits;
  672. else
  673. r = dm_table_any_congested(map, bdi_bits);
  674. dm_table_put(map);
  675. return r;
  676. }
  677. /*-----------------------------------------------------------------
  678. * An IDR is used to keep track of allocated minor numbers.
  679. *---------------------------------------------------------------*/
  680. static DEFINE_IDR(_minor_idr);
  681. static void free_minor(int minor)
  682. {
  683. spin_lock(&_minor_lock);
  684. idr_remove(&_minor_idr, minor);
  685. spin_unlock(&_minor_lock);
  686. }
  687. /*
  688. * See if the device with a specific minor # is free.
  689. */
  690. static int specific_minor(struct mapped_device *md, int minor)
  691. {
  692. int r, m;
  693. if (minor >= (1 << MINORBITS))
  694. return -EINVAL;
  695. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  696. if (!r)
  697. return -ENOMEM;
  698. spin_lock(&_minor_lock);
  699. if (idr_find(&_minor_idr, minor)) {
  700. r = -EBUSY;
  701. goto out;
  702. }
  703. r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
  704. if (r)
  705. goto out;
  706. if (m != minor) {
  707. idr_remove(&_minor_idr, m);
  708. r = -EBUSY;
  709. goto out;
  710. }
  711. out:
  712. spin_unlock(&_minor_lock);
  713. return r;
  714. }
  715. static int next_free_minor(struct mapped_device *md, int *minor)
  716. {
  717. int r, m;
  718. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  719. if (!r)
  720. return -ENOMEM;
  721. spin_lock(&_minor_lock);
  722. r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
  723. if (r) {
  724. goto out;
  725. }
  726. if (m >= (1 << MINORBITS)) {
  727. idr_remove(&_minor_idr, m);
  728. r = -ENOSPC;
  729. goto out;
  730. }
  731. *minor = m;
  732. out:
  733. spin_unlock(&_minor_lock);
  734. return r;
  735. }
  736. static struct block_device_operations dm_blk_dops;
  737. /*
  738. * Allocate and initialise a blank device with a given minor.
  739. */
  740. static struct mapped_device *alloc_dev(int minor)
  741. {
  742. int r;
  743. struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
  744. void *old_md;
  745. if (!md) {
  746. DMWARN("unable to allocate device, out of memory.");
  747. return NULL;
  748. }
  749. if (!try_module_get(THIS_MODULE))
  750. goto bad0;
  751. /* get a minor number for the dev */
  752. if (minor == DM_ANY_MINOR)
  753. r = next_free_minor(md, &minor);
  754. else
  755. r = specific_minor(md, minor);
  756. if (r < 0)
  757. goto bad1;
  758. memset(md, 0, sizeof(*md));
  759. init_rwsem(&md->io_lock);
  760. init_MUTEX(&md->suspend_lock);
  761. rwlock_init(&md->map_lock);
  762. atomic_set(&md->holders, 1);
  763. atomic_set(&md->open_count, 0);
  764. atomic_set(&md->event_nr, 0);
  765. md->queue = blk_alloc_queue(GFP_KERNEL);
  766. if (!md->queue)
  767. goto bad1_free_minor;
  768. md->queue->queuedata = md;
  769. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  770. md->queue->backing_dev_info.congested_data = md;
  771. blk_queue_make_request(md->queue, dm_request);
  772. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  773. md->queue->unplug_fn = dm_unplug_all;
  774. md->queue->issue_flush_fn = dm_flush_all;
  775. md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
  776. if (!md->io_pool)
  777. goto bad2;
  778. md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
  779. if (!md->tio_pool)
  780. goto bad3;
  781. md->bs = bioset_create(16, 16, 4);
  782. if (!md->bs)
  783. goto bad_no_bioset;
  784. md->disk = alloc_disk(1);
  785. if (!md->disk)
  786. goto bad4;
  787. atomic_set(&md->pending, 0);
  788. init_waitqueue_head(&md->wait);
  789. init_waitqueue_head(&md->eventq);
  790. md->disk->major = _major;
  791. md->disk->first_minor = minor;
  792. md->disk->fops = &dm_blk_dops;
  793. md->disk->queue = md->queue;
  794. md->disk->private_data = md;
  795. sprintf(md->disk->disk_name, "dm-%d", minor);
  796. add_disk(md->disk);
  797. format_dev_t(md->name, MKDEV(_major, minor));
  798. /* Populate the mapping, nobody knows we exist yet */
  799. spin_lock(&_minor_lock);
  800. old_md = idr_replace(&_minor_idr, md, minor);
  801. spin_unlock(&_minor_lock);
  802. BUG_ON(old_md != MINOR_ALLOCED);
  803. return md;
  804. bad4:
  805. bioset_free(md->bs);
  806. bad_no_bioset:
  807. mempool_destroy(md->tio_pool);
  808. bad3:
  809. mempool_destroy(md->io_pool);
  810. bad2:
  811. blk_cleanup_queue(md->queue);
  812. bad1_free_minor:
  813. free_minor(minor);
  814. bad1:
  815. module_put(THIS_MODULE);
  816. bad0:
  817. kfree(md);
  818. return NULL;
  819. }
  820. static void free_dev(struct mapped_device *md)
  821. {
  822. int minor = md->disk->first_minor;
  823. if (md->suspended_bdev) {
  824. thaw_bdev(md->suspended_bdev, NULL);
  825. bdput(md->suspended_bdev);
  826. }
  827. mempool_destroy(md->tio_pool);
  828. mempool_destroy(md->io_pool);
  829. bioset_free(md->bs);
  830. del_gendisk(md->disk);
  831. free_minor(minor);
  832. spin_lock(&_minor_lock);
  833. md->disk->private_data = NULL;
  834. spin_unlock(&_minor_lock);
  835. put_disk(md->disk);
  836. blk_cleanup_queue(md->queue);
  837. module_put(THIS_MODULE);
  838. kfree(md);
  839. }
  840. /*
  841. * Bind a table to the device.
  842. */
  843. static void event_callback(void *context)
  844. {
  845. struct mapped_device *md = (struct mapped_device *) context;
  846. atomic_inc(&md->event_nr);
  847. wake_up(&md->eventq);
  848. }
  849. static void __set_size(struct mapped_device *md, sector_t size)
  850. {
  851. set_capacity(md->disk, size);
  852. mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
  853. i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  854. mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
  855. }
  856. static int __bind(struct mapped_device *md, struct dm_table *t)
  857. {
  858. request_queue_t *q = md->queue;
  859. sector_t size;
  860. size = dm_table_get_size(t);
  861. /*
  862. * Wipe any geometry if the size of the table changed.
  863. */
  864. if (size != get_capacity(md->disk))
  865. memset(&md->geometry, 0, sizeof(md->geometry));
  866. __set_size(md, size);
  867. if (size == 0)
  868. return 0;
  869. dm_table_get(t);
  870. dm_table_event_callback(t, event_callback, md);
  871. write_lock(&md->map_lock);
  872. md->map = t;
  873. dm_table_set_restrictions(t, q);
  874. write_unlock(&md->map_lock);
  875. return 0;
  876. }
  877. static void __unbind(struct mapped_device *md)
  878. {
  879. struct dm_table *map = md->map;
  880. if (!map)
  881. return;
  882. dm_table_event_callback(map, NULL, NULL);
  883. write_lock(&md->map_lock);
  884. md->map = NULL;
  885. write_unlock(&md->map_lock);
  886. dm_table_put(map);
  887. }
  888. /*
  889. * Constructor for a new device.
  890. */
  891. int dm_create(int minor, struct mapped_device **result)
  892. {
  893. struct mapped_device *md;
  894. md = alloc_dev(minor);
  895. if (!md)
  896. return -ENXIO;
  897. *result = md;
  898. return 0;
  899. }
  900. static struct mapped_device *dm_find_md(dev_t dev)
  901. {
  902. struct mapped_device *md;
  903. unsigned minor = MINOR(dev);
  904. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  905. return NULL;
  906. spin_lock(&_minor_lock);
  907. md = idr_find(&_minor_idr, minor);
  908. if (md && (md == MINOR_ALLOCED ||
  909. (dm_disk(md)->first_minor != minor) ||
  910. test_bit(DMF_FREEING, &md->flags))) {
  911. md = NULL;
  912. goto out;
  913. }
  914. out:
  915. spin_unlock(&_minor_lock);
  916. return md;
  917. }
  918. struct mapped_device *dm_get_md(dev_t dev)
  919. {
  920. struct mapped_device *md = dm_find_md(dev);
  921. if (md)
  922. dm_get(md);
  923. return md;
  924. }
  925. void *dm_get_mdptr(struct mapped_device *md)
  926. {
  927. return md->interface_ptr;
  928. }
  929. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  930. {
  931. md->interface_ptr = ptr;
  932. }
  933. void dm_get(struct mapped_device *md)
  934. {
  935. atomic_inc(&md->holders);
  936. }
  937. const char *dm_device_name(struct mapped_device *md)
  938. {
  939. return md->name;
  940. }
  941. EXPORT_SYMBOL_GPL(dm_device_name);
  942. void dm_put(struct mapped_device *md)
  943. {
  944. struct dm_table *map;
  945. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  946. if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
  947. map = dm_get_table(md);
  948. idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
  949. set_bit(DMF_FREEING, &md->flags);
  950. spin_unlock(&_minor_lock);
  951. if (!dm_suspended(md)) {
  952. dm_table_presuspend_targets(map);
  953. dm_table_postsuspend_targets(map);
  954. }
  955. __unbind(md);
  956. dm_table_put(map);
  957. free_dev(md);
  958. }
  959. }
  960. /*
  961. * Process the deferred bios
  962. */
  963. static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
  964. {
  965. struct bio *n;
  966. while (c) {
  967. n = c->bi_next;
  968. c->bi_next = NULL;
  969. __split_bio(md, c);
  970. c = n;
  971. }
  972. }
  973. /*
  974. * Swap in a new table (destroying old one).
  975. */
  976. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  977. {
  978. int r = -EINVAL;
  979. down(&md->suspend_lock);
  980. /* device must be suspended */
  981. if (!dm_suspended(md))
  982. goto out;
  983. __unbind(md);
  984. r = __bind(md, table);
  985. out:
  986. up(&md->suspend_lock);
  987. return r;
  988. }
  989. /*
  990. * Functions to lock and unlock any filesystem running on the
  991. * device.
  992. */
  993. static int lock_fs(struct mapped_device *md)
  994. {
  995. int r;
  996. WARN_ON(md->frozen_sb);
  997. md->frozen_sb = freeze_bdev(md->suspended_bdev);
  998. if (IS_ERR(md->frozen_sb)) {
  999. r = PTR_ERR(md->frozen_sb);
  1000. md->frozen_sb = NULL;
  1001. return r;
  1002. }
  1003. set_bit(DMF_FROZEN, &md->flags);
  1004. /* don't bdput right now, we don't want the bdev
  1005. * to go away while it is locked.
  1006. */
  1007. return 0;
  1008. }
  1009. static void unlock_fs(struct mapped_device *md)
  1010. {
  1011. if (!test_bit(DMF_FROZEN, &md->flags))
  1012. return;
  1013. thaw_bdev(md->suspended_bdev, md->frozen_sb);
  1014. md->frozen_sb = NULL;
  1015. clear_bit(DMF_FROZEN, &md->flags);
  1016. }
  1017. /*
  1018. * We need to be able to change a mapping table under a mounted
  1019. * filesystem. For example we might want to move some data in
  1020. * the background. Before the table can be swapped with
  1021. * dm_bind_table, dm_suspend must be called to flush any in
  1022. * flight bios and ensure that any further io gets deferred.
  1023. */
  1024. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  1025. {
  1026. struct dm_table *map = NULL;
  1027. DECLARE_WAITQUEUE(wait, current);
  1028. struct bio *def;
  1029. int r = -EINVAL;
  1030. int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
  1031. down(&md->suspend_lock);
  1032. if (dm_suspended(md))
  1033. goto out_unlock;
  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. out_unlock:
  1093. up(&md->suspend_lock);
  1094. return r;
  1095. }
  1096. int dm_resume(struct mapped_device *md)
  1097. {
  1098. int r = -EINVAL;
  1099. struct bio *def;
  1100. struct dm_table *map = NULL;
  1101. down(&md->suspend_lock);
  1102. if (!dm_suspended(md))
  1103. goto out;
  1104. map = dm_get_table(md);
  1105. if (!map || !dm_table_get_size(map))
  1106. goto out;
  1107. r = dm_table_resume_targets(map);
  1108. if (r)
  1109. goto out;
  1110. down_write(&md->io_lock);
  1111. clear_bit(DMF_BLOCK_IO, &md->flags);
  1112. def = bio_list_get(&md->deferred);
  1113. __flush_deferred_io(md, def);
  1114. up_write(&md->io_lock);
  1115. unlock_fs(md);
  1116. bdput(md->suspended_bdev);
  1117. md->suspended_bdev = NULL;
  1118. clear_bit(DMF_SUSPENDED, &md->flags);
  1119. dm_table_unplug_all(map);
  1120. kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
  1121. r = 0;
  1122. out:
  1123. dm_table_put(map);
  1124. up(&md->suspend_lock);
  1125. return r;
  1126. }
  1127. /*-----------------------------------------------------------------
  1128. * Event notification.
  1129. *---------------------------------------------------------------*/
  1130. uint32_t dm_get_event_nr(struct mapped_device *md)
  1131. {
  1132. return atomic_read(&md->event_nr);
  1133. }
  1134. int dm_wait_event(struct mapped_device *md, int event_nr)
  1135. {
  1136. return wait_event_interruptible(md->eventq,
  1137. (event_nr != atomic_read(&md->event_nr)));
  1138. }
  1139. /*
  1140. * The gendisk is only valid as long as you have a reference
  1141. * count on 'md'.
  1142. */
  1143. struct gendisk *dm_disk(struct mapped_device *md)
  1144. {
  1145. return md->disk;
  1146. }
  1147. int dm_suspended(struct mapped_device *md)
  1148. {
  1149. return test_bit(DMF_SUSPENDED, &md->flags);
  1150. }
  1151. static struct block_device_operations dm_blk_dops = {
  1152. .open = dm_blk_open,
  1153. .release = dm_blk_close,
  1154. .ioctl = dm_blk_ioctl,
  1155. .getgeo = dm_blk_getgeo,
  1156. .owner = THIS_MODULE
  1157. };
  1158. EXPORT_SYMBOL(dm_get_mapinfo);
  1159. /*
  1160. * module hooks
  1161. */
  1162. module_init(dm_init);
  1163. module_exit(dm_exit);
  1164. module_param(major, uint, 0);
  1165. MODULE_PARM_DESC(major, "The major number of the device mapper");
  1166. MODULE_DESCRIPTION(DM_NAME " driver");
  1167. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  1168. MODULE_LICENSE("GPL");