dm.c 25 KB

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