dm.c 28 KB

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