dm.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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. 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(unsigned 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, unsigned 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, unsigned int *minor)
  653. {
  654. int r;
  655. unsigned int m;
  656. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  657. if (!r)
  658. return -ENOMEM;
  659. spin_lock(&_minor_lock);
  660. r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
  661. if (r) {
  662. goto out;
  663. }
  664. if (m >= (1 << MINORBITS)) {
  665. idr_remove(&_minor_idr, m);
  666. r = -ENOSPC;
  667. goto out;
  668. }
  669. *minor = m;
  670. out:
  671. spin_unlock(&_minor_lock);
  672. return r;
  673. }
  674. static struct block_device_operations dm_blk_dops;
  675. /*
  676. * Allocate and initialise a blank device with a given minor.
  677. */
  678. static struct mapped_device *alloc_dev(unsigned int minor, int persistent)
  679. {
  680. int r;
  681. struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
  682. void *old_md;
  683. if (!md) {
  684. DMWARN("unable to allocate device, out of memory.");
  685. return NULL;
  686. }
  687. if (!try_module_get(THIS_MODULE))
  688. goto bad0;
  689. /* get a minor number for the dev */
  690. r = persistent ? specific_minor(md, minor) : next_free_minor(md, &minor);
  691. if (r < 0)
  692. goto bad1;
  693. memset(md, 0, sizeof(*md));
  694. init_rwsem(&md->io_lock);
  695. init_MUTEX(&md->suspend_lock);
  696. rwlock_init(&md->map_lock);
  697. atomic_set(&md->holders, 1);
  698. atomic_set(&md->event_nr, 0);
  699. md->queue = blk_alloc_queue(GFP_KERNEL);
  700. if (!md->queue)
  701. goto bad1;
  702. md->queue->queuedata = md;
  703. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  704. md->queue->backing_dev_info.congested_data = md;
  705. blk_queue_make_request(md->queue, dm_request);
  706. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  707. md->queue->unplug_fn = dm_unplug_all;
  708. md->queue->issue_flush_fn = dm_flush_all;
  709. md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
  710. if (!md->io_pool)
  711. goto bad2;
  712. md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
  713. if (!md->tio_pool)
  714. goto bad3;
  715. md->disk = alloc_disk(1);
  716. if (!md->disk)
  717. goto bad4;
  718. atomic_set(&md->pending, 0);
  719. init_waitqueue_head(&md->wait);
  720. init_waitqueue_head(&md->eventq);
  721. md->disk->major = _major;
  722. md->disk->first_minor = minor;
  723. md->disk->fops = &dm_blk_dops;
  724. md->disk->queue = md->queue;
  725. md->disk->private_data = md;
  726. sprintf(md->disk->disk_name, "dm-%d", minor);
  727. add_disk(md->disk);
  728. format_dev_t(md->name, MKDEV(_major, minor));
  729. /* Populate the mapping, nobody knows we exist yet */
  730. spin_lock(&_minor_lock);
  731. old_md = idr_replace(&_minor_idr, md, minor);
  732. spin_unlock(&_minor_lock);
  733. BUG_ON(old_md != MINOR_ALLOCED);
  734. return md;
  735. bad4:
  736. mempool_destroy(md->tio_pool);
  737. bad3:
  738. mempool_destroy(md->io_pool);
  739. bad2:
  740. blk_cleanup_queue(md->queue);
  741. free_minor(minor);
  742. bad1:
  743. module_put(THIS_MODULE);
  744. bad0:
  745. kfree(md);
  746. return NULL;
  747. }
  748. static void free_dev(struct mapped_device *md)
  749. {
  750. unsigned int minor = md->disk->first_minor;
  751. if (md->suspended_bdev) {
  752. thaw_bdev(md->suspended_bdev, NULL);
  753. bdput(md->suspended_bdev);
  754. }
  755. mempool_destroy(md->tio_pool);
  756. mempool_destroy(md->io_pool);
  757. del_gendisk(md->disk);
  758. free_minor(minor);
  759. spin_lock(&_minor_lock);
  760. md->disk->private_data = NULL;
  761. spin_unlock(&_minor_lock);
  762. put_disk(md->disk);
  763. blk_cleanup_queue(md->queue);
  764. module_put(THIS_MODULE);
  765. kfree(md);
  766. }
  767. /*
  768. * Bind a table to the device.
  769. */
  770. static void event_callback(void *context)
  771. {
  772. struct mapped_device *md = (struct mapped_device *) context;
  773. atomic_inc(&md->event_nr);
  774. wake_up(&md->eventq);
  775. }
  776. static void __set_size(struct mapped_device *md, sector_t size)
  777. {
  778. set_capacity(md->disk, size);
  779. mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
  780. i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  781. mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
  782. }
  783. static int __bind(struct mapped_device *md, struct dm_table *t)
  784. {
  785. request_queue_t *q = md->queue;
  786. sector_t size;
  787. size = dm_table_get_size(t);
  788. /*
  789. * Wipe any geometry if the size of the table changed.
  790. */
  791. if (size != get_capacity(md->disk))
  792. memset(&md->geometry, 0, sizeof(md->geometry));
  793. __set_size(md, size);
  794. if (size == 0)
  795. return 0;
  796. dm_table_get(t);
  797. dm_table_event_callback(t, event_callback, md);
  798. write_lock(&md->map_lock);
  799. md->map = t;
  800. dm_table_set_restrictions(t, q);
  801. write_unlock(&md->map_lock);
  802. return 0;
  803. }
  804. static void __unbind(struct mapped_device *md)
  805. {
  806. struct dm_table *map = md->map;
  807. if (!map)
  808. return;
  809. dm_table_event_callback(map, NULL, NULL);
  810. write_lock(&md->map_lock);
  811. md->map = NULL;
  812. write_unlock(&md->map_lock);
  813. dm_table_put(map);
  814. }
  815. /*
  816. * Constructor for a new device.
  817. */
  818. static int create_aux(unsigned int minor, int persistent,
  819. struct mapped_device **result)
  820. {
  821. struct mapped_device *md;
  822. md = alloc_dev(minor, persistent);
  823. if (!md)
  824. return -ENXIO;
  825. *result = md;
  826. return 0;
  827. }
  828. int dm_create(struct mapped_device **result)
  829. {
  830. return create_aux(0, 0, result);
  831. }
  832. int dm_create_with_minor(unsigned int minor, struct mapped_device **result)
  833. {
  834. return create_aux(minor, 1, result);
  835. }
  836. static struct mapped_device *dm_find_md(dev_t dev)
  837. {
  838. struct mapped_device *md;
  839. unsigned minor = MINOR(dev);
  840. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  841. return NULL;
  842. spin_lock(&_minor_lock);
  843. md = idr_find(&_minor_idr, minor);
  844. if (md && (md == MINOR_ALLOCED ||
  845. (dm_disk(md)->first_minor != minor) ||
  846. test_bit(DMF_FREEING, &md->flags))) {
  847. md = NULL;
  848. goto out;
  849. }
  850. out:
  851. spin_unlock(&_minor_lock);
  852. return md;
  853. }
  854. struct mapped_device *dm_get_md(dev_t dev)
  855. {
  856. struct mapped_device *md = dm_find_md(dev);
  857. if (md)
  858. dm_get(md);
  859. return md;
  860. }
  861. void *dm_get_mdptr(struct mapped_device *md)
  862. {
  863. return md->interface_ptr;
  864. }
  865. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  866. {
  867. md->interface_ptr = ptr;
  868. }
  869. void dm_get(struct mapped_device *md)
  870. {
  871. atomic_inc(&md->holders);
  872. }
  873. void dm_put(struct mapped_device *md)
  874. {
  875. struct dm_table *map;
  876. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  877. if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
  878. map = dm_get_table(md);
  879. idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
  880. set_bit(DMF_FREEING, &md->flags);
  881. spin_unlock(&_minor_lock);
  882. if (!dm_suspended(md)) {
  883. dm_table_presuspend_targets(map);
  884. dm_table_postsuspend_targets(map);
  885. }
  886. __unbind(md);
  887. dm_table_put(map);
  888. free_dev(md);
  889. }
  890. }
  891. /*
  892. * Process the deferred bios
  893. */
  894. static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
  895. {
  896. struct bio *n;
  897. while (c) {
  898. n = c->bi_next;
  899. c->bi_next = NULL;
  900. __split_bio(md, c);
  901. c = n;
  902. }
  903. }
  904. /*
  905. * Swap in a new table (destroying old one).
  906. */
  907. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  908. {
  909. int r = -EINVAL;
  910. down(&md->suspend_lock);
  911. /* device must be suspended */
  912. if (!dm_suspended(md))
  913. goto out;
  914. __unbind(md);
  915. r = __bind(md, table);
  916. out:
  917. up(&md->suspend_lock);
  918. return r;
  919. }
  920. /*
  921. * Functions to lock and unlock any filesystem running on the
  922. * device.
  923. */
  924. static int lock_fs(struct mapped_device *md)
  925. {
  926. int r;
  927. WARN_ON(md->frozen_sb);
  928. md->frozen_sb = freeze_bdev(md->suspended_bdev);
  929. if (IS_ERR(md->frozen_sb)) {
  930. r = PTR_ERR(md->frozen_sb);
  931. md->frozen_sb = NULL;
  932. return r;
  933. }
  934. set_bit(DMF_FROZEN, &md->flags);
  935. /* don't bdput right now, we don't want the bdev
  936. * to go away while it is locked.
  937. */
  938. return 0;
  939. }
  940. static void unlock_fs(struct mapped_device *md)
  941. {
  942. if (!test_bit(DMF_FROZEN, &md->flags))
  943. return;
  944. thaw_bdev(md->suspended_bdev, md->frozen_sb);
  945. md->frozen_sb = NULL;
  946. clear_bit(DMF_FROZEN, &md->flags);
  947. }
  948. /*
  949. * We need to be able to change a mapping table under a mounted
  950. * filesystem. For example we might want to move some data in
  951. * the background. Before the table can be swapped with
  952. * dm_bind_table, dm_suspend must be called to flush any in
  953. * flight bios and ensure that any further io gets deferred.
  954. */
  955. int dm_suspend(struct mapped_device *md, int do_lockfs)
  956. {
  957. struct dm_table *map = NULL;
  958. DECLARE_WAITQUEUE(wait, current);
  959. struct bio *def;
  960. int r = -EINVAL;
  961. down(&md->suspend_lock);
  962. if (dm_suspended(md))
  963. goto out;
  964. map = dm_get_table(md);
  965. /* This does not get reverted if there's an error later. */
  966. dm_table_presuspend_targets(map);
  967. md->suspended_bdev = bdget_disk(md->disk, 0);
  968. if (!md->suspended_bdev) {
  969. DMWARN("bdget failed in dm_suspend");
  970. r = -ENOMEM;
  971. goto out;
  972. }
  973. /* Flush I/O to the device. */
  974. if (do_lockfs) {
  975. r = lock_fs(md);
  976. if (r)
  977. goto out;
  978. }
  979. /*
  980. * First we set the BLOCK_IO flag so no more ios will be mapped.
  981. */
  982. down_write(&md->io_lock);
  983. set_bit(DMF_BLOCK_IO, &md->flags);
  984. add_wait_queue(&md->wait, &wait);
  985. up_write(&md->io_lock);
  986. /* unplug */
  987. if (map)
  988. dm_table_unplug_all(map);
  989. /*
  990. * Then we wait for the already mapped ios to
  991. * complete.
  992. */
  993. while (1) {
  994. set_current_state(TASK_INTERRUPTIBLE);
  995. if (!atomic_read(&md->pending) || signal_pending(current))
  996. break;
  997. io_schedule();
  998. }
  999. set_current_state(TASK_RUNNING);
  1000. down_write(&md->io_lock);
  1001. remove_wait_queue(&md->wait, &wait);
  1002. /* were we interrupted ? */
  1003. r = -EINTR;
  1004. if (atomic_read(&md->pending)) {
  1005. clear_bit(DMF_BLOCK_IO, &md->flags);
  1006. def = bio_list_get(&md->deferred);
  1007. __flush_deferred_io(md, def);
  1008. up_write(&md->io_lock);
  1009. unlock_fs(md);
  1010. goto out;
  1011. }
  1012. up_write(&md->io_lock);
  1013. dm_table_postsuspend_targets(map);
  1014. set_bit(DMF_SUSPENDED, &md->flags);
  1015. r = 0;
  1016. out:
  1017. if (r && md->suspended_bdev) {
  1018. bdput(md->suspended_bdev);
  1019. md->suspended_bdev = NULL;
  1020. }
  1021. dm_table_put(map);
  1022. up(&md->suspend_lock);
  1023. return r;
  1024. }
  1025. int dm_resume(struct mapped_device *md)
  1026. {
  1027. int r = -EINVAL;
  1028. struct bio *def;
  1029. struct dm_table *map = NULL;
  1030. down(&md->suspend_lock);
  1031. if (!dm_suspended(md))
  1032. goto out;
  1033. map = dm_get_table(md);
  1034. if (!map || !dm_table_get_size(map))
  1035. goto out;
  1036. dm_table_resume_targets(map);
  1037. down_write(&md->io_lock);
  1038. clear_bit(DMF_BLOCK_IO, &md->flags);
  1039. def = bio_list_get(&md->deferred);
  1040. __flush_deferred_io(md, def);
  1041. up_write(&md->io_lock);
  1042. unlock_fs(md);
  1043. bdput(md->suspended_bdev);
  1044. md->suspended_bdev = NULL;
  1045. clear_bit(DMF_SUSPENDED, &md->flags);
  1046. dm_table_unplug_all(map);
  1047. r = 0;
  1048. out:
  1049. dm_table_put(map);
  1050. up(&md->suspend_lock);
  1051. return r;
  1052. }
  1053. /*-----------------------------------------------------------------
  1054. * Event notification.
  1055. *---------------------------------------------------------------*/
  1056. uint32_t dm_get_event_nr(struct mapped_device *md)
  1057. {
  1058. return atomic_read(&md->event_nr);
  1059. }
  1060. int dm_wait_event(struct mapped_device *md, int event_nr)
  1061. {
  1062. return wait_event_interruptible(md->eventq,
  1063. (event_nr != atomic_read(&md->event_nr)));
  1064. }
  1065. /*
  1066. * The gendisk is only valid as long as you have a reference
  1067. * count on 'md'.
  1068. */
  1069. struct gendisk *dm_disk(struct mapped_device *md)
  1070. {
  1071. return md->disk;
  1072. }
  1073. int dm_suspended(struct mapped_device *md)
  1074. {
  1075. return test_bit(DMF_SUSPENDED, &md->flags);
  1076. }
  1077. static struct block_device_operations dm_blk_dops = {
  1078. .open = dm_blk_open,
  1079. .release = dm_blk_close,
  1080. .getgeo = dm_blk_getgeo,
  1081. .owner = THIS_MODULE
  1082. };
  1083. EXPORT_SYMBOL(dm_get_mapinfo);
  1084. /*
  1085. * module hooks
  1086. */
  1087. module_init(dm_init);
  1088. module_exit(dm_exit);
  1089. module_param(major, uint, 0);
  1090. MODULE_PARM_DESC(major, "The major number of the device mapper");
  1091. MODULE_DESCRIPTION(DM_NAME " driver");
  1092. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  1093. MODULE_LICENSE("GPL");