dm.c 24 KB

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