dm.c 26 KB

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