dm.c 25 KB

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