dm.c 26 KB

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