dm.c 28 KB

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