dm.c 27 KB

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