dm.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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 dm_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 dm_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. #define DMF_NOFLUSH_SUSPENDING 5
  62. struct mapped_device {
  63. struct rw_semaphore io_lock;
  64. struct semaphore suspend_lock;
  65. spinlock_t pushback_lock;
  66. rwlock_t map_lock;
  67. atomic_t holders;
  68. atomic_t open_count;
  69. unsigned long flags;
  70. request_queue_t *queue;
  71. struct gendisk *disk;
  72. char name[16];
  73. void *interface_ptr;
  74. /*
  75. * A list of ios that arrived while we were suspended.
  76. */
  77. atomic_t pending;
  78. wait_queue_head_t wait;
  79. struct bio_list deferred;
  80. struct bio_list pushback;
  81. /*
  82. * The current mapping.
  83. */
  84. struct dm_table *map;
  85. /*
  86. * io objects are allocated from here.
  87. */
  88. mempool_t *io_pool;
  89. mempool_t *tio_pool;
  90. struct bio_set *bs;
  91. /*
  92. * Event handling.
  93. */
  94. atomic_t event_nr;
  95. wait_queue_head_t eventq;
  96. /*
  97. * freeze/thaw support require holding onto a super block
  98. */
  99. struct super_block *frozen_sb;
  100. struct block_device *suspended_bdev;
  101. /* forced geometry settings */
  102. struct hd_geometry geometry;
  103. };
  104. #define MIN_IOS 256
  105. static struct kmem_cache *_io_cache;
  106. static struct kmem_cache *_tio_cache;
  107. static int __init local_init(void)
  108. {
  109. int r;
  110. /* allocate a slab for the dm_ios */
  111. _io_cache = KMEM_CACHE(dm_io, 0);
  112. if (!_io_cache)
  113. return -ENOMEM;
  114. /* allocate a slab for the target ios */
  115. _tio_cache = KMEM_CACHE(dm_target_io, 0);
  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. unregister_blkdev(_major, _name);
  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 struct dm_io *alloc_io(struct mapped_device *md)
  255. {
  256. return mempool_alloc(md->io_pool, GFP_NOIO);
  257. }
  258. static void free_io(struct mapped_device *md, struct dm_io *io)
  259. {
  260. mempool_free(io, md->io_pool);
  261. }
  262. static struct dm_target_io *alloc_tio(struct mapped_device *md)
  263. {
  264. return mempool_alloc(md->tio_pool, GFP_NOIO);
  265. }
  266. static void free_tio(struct mapped_device *md, struct dm_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. static int __noflush_suspending(struct mapped_device *md)
  352. {
  353. return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  354. }
  355. /*
  356. * Decrements the number of outstanding ios that a bio has been
  357. * cloned into, completing the original io if necc.
  358. */
  359. static void dec_pending(struct dm_io *io, int error)
  360. {
  361. unsigned long flags;
  362. /* Push-back supersedes any I/O errors */
  363. if (error && !(io->error > 0 && __noflush_suspending(io->md)))
  364. io->error = error;
  365. if (atomic_dec_and_test(&io->io_count)) {
  366. if (io->error == DM_ENDIO_REQUEUE) {
  367. /*
  368. * Target requested pushing back the I/O.
  369. * This must be handled before the sleeper on
  370. * suspend queue merges the pushback list.
  371. */
  372. spin_lock_irqsave(&io->md->pushback_lock, flags);
  373. if (__noflush_suspending(io->md))
  374. bio_list_add(&io->md->pushback, io->bio);
  375. else
  376. /* noflush suspend was interrupted. */
  377. io->error = -EIO;
  378. spin_unlock_irqrestore(&io->md->pushback_lock, flags);
  379. }
  380. if (end_io_acct(io))
  381. /* nudge anyone waiting on suspend queue */
  382. wake_up(&io->md->wait);
  383. if (io->error != DM_ENDIO_REQUEUE) {
  384. blk_add_trace_bio(io->md->queue, io->bio,
  385. BLK_TA_COMPLETE);
  386. bio_endio(io->bio, io->bio->bi_size, io->error);
  387. }
  388. free_io(io->md, io);
  389. }
  390. }
  391. static int clone_endio(struct bio *bio, unsigned int done, int error)
  392. {
  393. int r = 0;
  394. struct dm_target_io *tio = bio->bi_private;
  395. struct mapped_device *md = tio->io->md;
  396. dm_endio_fn endio = tio->ti->type->end_io;
  397. if (bio->bi_size)
  398. return 1;
  399. if (!bio_flagged(bio, BIO_UPTODATE) && !error)
  400. error = -EIO;
  401. if (endio) {
  402. r = endio(tio->ti, bio, error, &tio->info);
  403. if (r < 0 || r == DM_ENDIO_REQUEUE)
  404. /*
  405. * error and requeue request are handled
  406. * in dec_pending().
  407. */
  408. error = r;
  409. else if (r == DM_ENDIO_INCOMPLETE)
  410. /* The target will handle the io */
  411. return 1;
  412. else if (r) {
  413. DMWARN("unimplemented target endio return value: %d", r);
  414. BUG();
  415. }
  416. }
  417. dec_pending(tio->io, error);
  418. /*
  419. * Store md for cleanup instead of tio which is about to get freed.
  420. */
  421. bio->bi_private = md->bs;
  422. bio_put(bio);
  423. free_tio(md, tio);
  424. return r;
  425. }
  426. static sector_t max_io_len(struct mapped_device *md,
  427. sector_t sector, struct dm_target *ti)
  428. {
  429. sector_t offset = sector - ti->begin;
  430. sector_t len = ti->len - offset;
  431. /*
  432. * Does the target need to split even further ?
  433. */
  434. if (ti->split_io) {
  435. sector_t boundary;
  436. boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
  437. - offset;
  438. if (len > boundary)
  439. len = boundary;
  440. }
  441. return len;
  442. }
  443. static void __map_bio(struct dm_target *ti, struct bio *clone,
  444. struct dm_target_io *tio)
  445. {
  446. int r;
  447. sector_t sector;
  448. struct mapped_device *md;
  449. /*
  450. * Sanity checks.
  451. */
  452. BUG_ON(!clone->bi_size);
  453. clone->bi_end_io = clone_endio;
  454. clone->bi_private = tio;
  455. /*
  456. * Map the clone. If r == 0 we don't need to do
  457. * anything, the target has assumed ownership of
  458. * this io.
  459. */
  460. atomic_inc(&tio->io->io_count);
  461. sector = clone->bi_sector;
  462. r = ti->type->map(ti, clone, &tio->info);
  463. if (r == DM_MAPIO_REMAPPED) {
  464. /* the bio has been remapped so dispatch it */
  465. blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
  466. tio->io->bio->bi_bdev->bd_dev, sector,
  467. clone->bi_sector);
  468. generic_make_request(clone);
  469. } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
  470. /* error the io and bail out, or requeue it if needed */
  471. md = tio->io->md;
  472. dec_pending(tio->io, r);
  473. /*
  474. * Store bio_set for cleanup.
  475. */
  476. clone->bi_private = md->bs;
  477. bio_put(clone);
  478. free_tio(md, tio);
  479. } else if (r) {
  480. DMWARN("unimplemented target map return value: %d", r);
  481. BUG();
  482. }
  483. }
  484. struct clone_info {
  485. struct mapped_device *md;
  486. struct dm_table *map;
  487. struct bio *bio;
  488. struct dm_io *io;
  489. sector_t sector;
  490. sector_t sector_count;
  491. unsigned short idx;
  492. };
  493. static void dm_bio_destructor(struct bio *bio)
  494. {
  495. struct bio_set *bs = bio->bi_private;
  496. bio_free(bio, bs);
  497. }
  498. /*
  499. * Creates a little bio that is just does part of a bvec.
  500. */
  501. static struct bio *split_bvec(struct bio *bio, sector_t sector,
  502. unsigned short idx, unsigned int offset,
  503. unsigned int len, struct bio_set *bs)
  504. {
  505. struct bio *clone;
  506. struct bio_vec *bv = bio->bi_io_vec + idx;
  507. clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
  508. clone->bi_destructor = dm_bio_destructor;
  509. *clone->bi_io_vec = *bv;
  510. clone->bi_sector = sector;
  511. clone->bi_bdev = bio->bi_bdev;
  512. clone->bi_rw = bio->bi_rw;
  513. clone->bi_vcnt = 1;
  514. clone->bi_size = to_bytes(len);
  515. clone->bi_io_vec->bv_offset = offset;
  516. clone->bi_io_vec->bv_len = clone->bi_size;
  517. return clone;
  518. }
  519. /*
  520. * Creates a bio that consists of range of complete bvecs.
  521. */
  522. static struct bio *clone_bio(struct bio *bio, sector_t sector,
  523. unsigned short idx, unsigned short bv_count,
  524. unsigned int len, struct bio_set *bs)
  525. {
  526. struct bio *clone;
  527. clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
  528. __bio_clone(clone, bio);
  529. clone->bi_destructor = dm_bio_destructor;
  530. clone->bi_sector = sector;
  531. clone->bi_idx = idx;
  532. clone->bi_vcnt = idx + bv_count;
  533. clone->bi_size = to_bytes(len);
  534. clone->bi_flags &= ~(1 << BIO_SEG_VALID);
  535. return clone;
  536. }
  537. static void __clone_and_map(struct clone_info *ci)
  538. {
  539. struct bio *clone, *bio = ci->bio;
  540. struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
  541. sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
  542. struct dm_target_io *tio;
  543. /*
  544. * Allocate a target io object.
  545. */
  546. tio = alloc_tio(ci->md);
  547. tio->io = ci->io;
  548. tio->ti = ti;
  549. memset(&tio->info, 0, sizeof(tio->info));
  550. if (ci->sector_count <= max) {
  551. /*
  552. * Optimise for the simple case where we can do all of
  553. * the remaining io with a single clone.
  554. */
  555. clone = clone_bio(bio, ci->sector, ci->idx,
  556. bio->bi_vcnt - ci->idx, ci->sector_count,
  557. ci->md->bs);
  558. __map_bio(ti, clone, tio);
  559. ci->sector_count = 0;
  560. } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
  561. /*
  562. * There are some bvecs that don't span targets.
  563. * Do as many of these as possible.
  564. */
  565. int i;
  566. sector_t remaining = max;
  567. sector_t bv_len;
  568. for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
  569. bv_len = to_sector(bio->bi_io_vec[i].bv_len);
  570. if (bv_len > remaining)
  571. break;
  572. remaining -= bv_len;
  573. len += bv_len;
  574. }
  575. clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
  576. ci->md->bs);
  577. __map_bio(ti, clone, tio);
  578. ci->sector += len;
  579. ci->sector_count -= len;
  580. ci->idx = i;
  581. } else {
  582. /*
  583. * Handle a bvec that must be split between two or more targets.
  584. */
  585. struct bio_vec *bv = bio->bi_io_vec + ci->idx;
  586. sector_t remaining = to_sector(bv->bv_len);
  587. unsigned int offset = 0;
  588. do {
  589. if (offset) {
  590. ti = dm_table_find_target(ci->map, ci->sector);
  591. max = max_io_len(ci->md, ci->sector, ti);
  592. tio = alloc_tio(ci->md);
  593. tio->io = ci->io;
  594. tio->ti = ti;
  595. memset(&tio->info, 0, sizeof(tio->info));
  596. }
  597. len = min(remaining, max);
  598. clone = split_bvec(bio, ci->sector, ci->idx,
  599. bv->bv_offset + offset, len,
  600. ci->md->bs);
  601. __map_bio(ti, clone, tio);
  602. ci->sector += len;
  603. ci->sector_count -= len;
  604. offset += to_bytes(len);
  605. } while (remaining -= len);
  606. ci->idx++;
  607. }
  608. }
  609. /*
  610. * Split the bio into several clones.
  611. */
  612. static void __split_bio(struct mapped_device *md, struct bio *bio)
  613. {
  614. struct clone_info ci;
  615. ci.map = dm_get_table(md);
  616. if (!ci.map) {
  617. bio_io_error(bio, bio->bi_size);
  618. return;
  619. }
  620. ci.md = md;
  621. ci.bio = bio;
  622. ci.io = alloc_io(md);
  623. ci.io->error = 0;
  624. atomic_set(&ci.io->io_count, 1);
  625. ci.io->bio = bio;
  626. ci.io->md = md;
  627. ci.sector = bio->bi_sector;
  628. ci.sector_count = bio_sectors(bio);
  629. ci.idx = bio->bi_idx;
  630. start_io_acct(ci.io);
  631. while (ci.sector_count)
  632. __clone_and_map(&ci);
  633. /* drop the extra reference count */
  634. dec_pending(ci.io, 0);
  635. dm_table_put(ci.map);
  636. }
  637. /*-----------------------------------------------------------------
  638. * CRUD END
  639. *---------------------------------------------------------------*/
  640. /*
  641. * The request function that just remaps the bio built up by
  642. * dm_merge_bvec.
  643. */
  644. static int dm_request(request_queue_t *q, struct bio *bio)
  645. {
  646. int r;
  647. int rw = bio_data_dir(bio);
  648. struct mapped_device *md = q->queuedata;
  649. /*
  650. * There is no use in forwarding any barrier request since we can't
  651. * guarantee it is (or can be) handled by the targets correctly.
  652. */
  653. if (unlikely(bio_barrier(bio))) {
  654. bio_endio(bio, bio->bi_size, -EOPNOTSUPP);
  655. return 0;
  656. }
  657. down_read(&md->io_lock);
  658. disk_stat_inc(dm_disk(md), ios[rw]);
  659. disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
  660. /*
  661. * If we're suspended we have to queue
  662. * this io for later.
  663. */
  664. while (test_bit(DMF_BLOCK_IO, &md->flags)) {
  665. up_read(&md->io_lock);
  666. if (bio_rw(bio) == READA) {
  667. bio_io_error(bio, bio->bi_size);
  668. return 0;
  669. }
  670. r = queue_io(md, bio);
  671. if (r < 0) {
  672. bio_io_error(bio, bio->bi_size);
  673. return 0;
  674. } else if (r == 0)
  675. return 0; /* deferred successfully */
  676. /*
  677. * We're in a while loop, because someone could suspend
  678. * before we get to the following read lock.
  679. */
  680. down_read(&md->io_lock);
  681. }
  682. __split_bio(md, bio);
  683. up_read(&md->io_lock);
  684. return 0;
  685. }
  686. static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
  687. sector_t *error_sector)
  688. {
  689. struct mapped_device *md = q->queuedata;
  690. struct dm_table *map = dm_get_table(md);
  691. int ret = -ENXIO;
  692. if (map) {
  693. ret = dm_table_flush_all(map);
  694. dm_table_put(map);
  695. }
  696. return ret;
  697. }
  698. static void dm_unplug_all(request_queue_t *q)
  699. {
  700. struct mapped_device *md = q->queuedata;
  701. struct dm_table *map = dm_get_table(md);
  702. if (map) {
  703. dm_table_unplug_all(map);
  704. dm_table_put(map);
  705. }
  706. }
  707. static int dm_any_congested(void *congested_data, int bdi_bits)
  708. {
  709. int r;
  710. struct mapped_device *md = (struct mapped_device *) congested_data;
  711. struct dm_table *map = dm_get_table(md);
  712. if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
  713. r = bdi_bits;
  714. else
  715. r = dm_table_any_congested(map, bdi_bits);
  716. dm_table_put(map);
  717. return r;
  718. }
  719. /*-----------------------------------------------------------------
  720. * An IDR is used to keep track of allocated minor numbers.
  721. *---------------------------------------------------------------*/
  722. static DEFINE_IDR(_minor_idr);
  723. static void free_minor(int minor)
  724. {
  725. spin_lock(&_minor_lock);
  726. idr_remove(&_minor_idr, minor);
  727. spin_unlock(&_minor_lock);
  728. }
  729. /*
  730. * See if the device with a specific minor # is free.
  731. */
  732. static int specific_minor(struct mapped_device *md, int minor)
  733. {
  734. int r, m;
  735. if (minor >= (1 << MINORBITS))
  736. return -EINVAL;
  737. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  738. if (!r)
  739. return -ENOMEM;
  740. spin_lock(&_minor_lock);
  741. if (idr_find(&_minor_idr, minor)) {
  742. r = -EBUSY;
  743. goto out;
  744. }
  745. r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
  746. if (r)
  747. goto out;
  748. if (m != minor) {
  749. idr_remove(&_minor_idr, m);
  750. r = -EBUSY;
  751. goto out;
  752. }
  753. out:
  754. spin_unlock(&_minor_lock);
  755. return r;
  756. }
  757. static int next_free_minor(struct mapped_device *md, int *minor)
  758. {
  759. int r, m;
  760. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  761. if (!r)
  762. return -ENOMEM;
  763. spin_lock(&_minor_lock);
  764. r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
  765. if (r) {
  766. goto out;
  767. }
  768. if (m >= (1 << MINORBITS)) {
  769. idr_remove(&_minor_idr, m);
  770. r = -ENOSPC;
  771. goto out;
  772. }
  773. *minor = m;
  774. out:
  775. spin_unlock(&_minor_lock);
  776. return r;
  777. }
  778. static struct block_device_operations dm_blk_dops;
  779. /*
  780. * Allocate and initialise a blank device with a given minor.
  781. */
  782. static struct mapped_device *alloc_dev(int minor)
  783. {
  784. int r;
  785. struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
  786. void *old_md;
  787. if (!md) {
  788. DMWARN("unable to allocate device, out of memory.");
  789. return NULL;
  790. }
  791. if (!try_module_get(THIS_MODULE))
  792. goto bad0;
  793. /* get a minor number for the dev */
  794. if (minor == DM_ANY_MINOR)
  795. r = next_free_minor(md, &minor);
  796. else
  797. r = specific_minor(md, minor);
  798. if (r < 0)
  799. goto bad1;
  800. memset(md, 0, sizeof(*md));
  801. init_rwsem(&md->io_lock);
  802. init_MUTEX(&md->suspend_lock);
  803. spin_lock_init(&md->pushback_lock);
  804. rwlock_init(&md->map_lock);
  805. atomic_set(&md->holders, 1);
  806. atomic_set(&md->open_count, 0);
  807. atomic_set(&md->event_nr, 0);
  808. md->queue = blk_alloc_queue(GFP_KERNEL);
  809. if (!md->queue)
  810. goto bad1_free_minor;
  811. md->queue->queuedata = md;
  812. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  813. md->queue->backing_dev_info.congested_data = md;
  814. blk_queue_make_request(md->queue, dm_request);
  815. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  816. md->queue->unplug_fn = dm_unplug_all;
  817. md->queue->issue_flush_fn = dm_flush_all;
  818. md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
  819. if (!md->io_pool)
  820. goto bad2;
  821. md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
  822. if (!md->tio_pool)
  823. goto bad3;
  824. md->bs = bioset_create(16, 16);
  825. if (!md->bs)
  826. goto bad_no_bioset;
  827. md->disk = alloc_disk(1);
  828. if (!md->disk)
  829. goto bad4;
  830. atomic_set(&md->pending, 0);
  831. init_waitqueue_head(&md->wait);
  832. init_waitqueue_head(&md->eventq);
  833. md->disk->major = _major;
  834. md->disk->first_minor = minor;
  835. md->disk->fops = &dm_blk_dops;
  836. md->disk->queue = md->queue;
  837. md->disk->private_data = md;
  838. sprintf(md->disk->disk_name, "dm-%d", minor);
  839. add_disk(md->disk);
  840. format_dev_t(md->name, MKDEV(_major, minor));
  841. /* Populate the mapping, nobody knows we exist yet */
  842. spin_lock(&_minor_lock);
  843. old_md = idr_replace(&_minor_idr, md, minor);
  844. spin_unlock(&_minor_lock);
  845. BUG_ON(old_md != MINOR_ALLOCED);
  846. return md;
  847. bad4:
  848. bioset_free(md->bs);
  849. bad_no_bioset:
  850. mempool_destroy(md->tio_pool);
  851. bad3:
  852. mempool_destroy(md->io_pool);
  853. bad2:
  854. blk_cleanup_queue(md->queue);
  855. bad1_free_minor:
  856. free_minor(minor);
  857. bad1:
  858. module_put(THIS_MODULE);
  859. bad0:
  860. kfree(md);
  861. return NULL;
  862. }
  863. static void free_dev(struct mapped_device *md)
  864. {
  865. int minor = md->disk->first_minor;
  866. if (md->suspended_bdev) {
  867. thaw_bdev(md->suspended_bdev, NULL);
  868. bdput(md->suspended_bdev);
  869. }
  870. mempool_destroy(md->tio_pool);
  871. mempool_destroy(md->io_pool);
  872. bioset_free(md->bs);
  873. del_gendisk(md->disk);
  874. free_minor(minor);
  875. spin_lock(&_minor_lock);
  876. md->disk->private_data = NULL;
  877. spin_unlock(&_minor_lock);
  878. put_disk(md->disk);
  879. blk_cleanup_queue(md->queue);
  880. module_put(THIS_MODULE);
  881. kfree(md);
  882. }
  883. /*
  884. * Bind a table to the device.
  885. */
  886. static void event_callback(void *context)
  887. {
  888. struct mapped_device *md = (struct mapped_device *) context;
  889. atomic_inc(&md->event_nr);
  890. wake_up(&md->eventq);
  891. }
  892. static void __set_size(struct mapped_device *md, sector_t size)
  893. {
  894. set_capacity(md->disk, size);
  895. mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
  896. i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  897. mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
  898. }
  899. static int __bind(struct mapped_device *md, struct dm_table *t)
  900. {
  901. request_queue_t *q = md->queue;
  902. sector_t size;
  903. size = dm_table_get_size(t);
  904. /*
  905. * Wipe any geometry if the size of the table changed.
  906. */
  907. if (size != get_capacity(md->disk))
  908. memset(&md->geometry, 0, sizeof(md->geometry));
  909. if (md->suspended_bdev)
  910. __set_size(md, size);
  911. if (size == 0)
  912. return 0;
  913. dm_table_get(t);
  914. dm_table_event_callback(t, event_callback, md);
  915. write_lock(&md->map_lock);
  916. md->map = t;
  917. dm_table_set_restrictions(t, q);
  918. write_unlock(&md->map_lock);
  919. return 0;
  920. }
  921. static void __unbind(struct mapped_device *md)
  922. {
  923. struct dm_table *map = md->map;
  924. if (!map)
  925. return;
  926. dm_table_event_callback(map, NULL, NULL);
  927. write_lock(&md->map_lock);
  928. md->map = NULL;
  929. write_unlock(&md->map_lock);
  930. dm_table_put(map);
  931. }
  932. /*
  933. * Constructor for a new device.
  934. */
  935. int dm_create(int minor, struct mapped_device **result)
  936. {
  937. struct mapped_device *md;
  938. md = alloc_dev(minor);
  939. if (!md)
  940. return -ENXIO;
  941. *result = md;
  942. return 0;
  943. }
  944. static struct mapped_device *dm_find_md(dev_t dev)
  945. {
  946. struct mapped_device *md;
  947. unsigned minor = MINOR(dev);
  948. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  949. return NULL;
  950. spin_lock(&_minor_lock);
  951. md = idr_find(&_minor_idr, minor);
  952. if (md && (md == MINOR_ALLOCED ||
  953. (dm_disk(md)->first_minor != minor) ||
  954. test_bit(DMF_FREEING, &md->flags))) {
  955. md = NULL;
  956. goto out;
  957. }
  958. out:
  959. spin_unlock(&_minor_lock);
  960. return md;
  961. }
  962. struct mapped_device *dm_get_md(dev_t dev)
  963. {
  964. struct mapped_device *md = dm_find_md(dev);
  965. if (md)
  966. dm_get(md);
  967. return md;
  968. }
  969. void *dm_get_mdptr(struct mapped_device *md)
  970. {
  971. return md->interface_ptr;
  972. }
  973. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  974. {
  975. md->interface_ptr = ptr;
  976. }
  977. void dm_get(struct mapped_device *md)
  978. {
  979. atomic_inc(&md->holders);
  980. }
  981. const char *dm_device_name(struct mapped_device *md)
  982. {
  983. return md->name;
  984. }
  985. EXPORT_SYMBOL_GPL(dm_device_name);
  986. void dm_put(struct mapped_device *md)
  987. {
  988. struct dm_table *map;
  989. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  990. if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
  991. map = dm_get_table(md);
  992. idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
  993. set_bit(DMF_FREEING, &md->flags);
  994. spin_unlock(&_minor_lock);
  995. if (!dm_suspended(md)) {
  996. dm_table_presuspend_targets(map);
  997. dm_table_postsuspend_targets(map);
  998. }
  999. __unbind(md);
  1000. dm_table_put(map);
  1001. free_dev(md);
  1002. }
  1003. }
  1004. EXPORT_SYMBOL_GPL(dm_put);
  1005. /*
  1006. * Process the deferred bios
  1007. */
  1008. static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
  1009. {
  1010. struct bio *n;
  1011. while (c) {
  1012. n = c->bi_next;
  1013. c->bi_next = NULL;
  1014. __split_bio(md, c);
  1015. c = n;
  1016. }
  1017. }
  1018. /*
  1019. * Swap in a new table (destroying old one).
  1020. */
  1021. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  1022. {
  1023. int r = -EINVAL;
  1024. down(&md->suspend_lock);
  1025. /* device must be suspended */
  1026. if (!dm_suspended(md))
  1027. goto out;
  1028. /* without bdev, the device size cannot be changed */
  1029. if (!md->suspended_bdev)
  1030. if (get_capacity(md->disk) != dm_table_get_size(table))
  1031. goto out;
  1032. __unbind(md);
  1033. r = __bind(md, table);
  1034. out:
  1035. up(&md->suspend_lock);
  1036. return r;
  1037. }
  1038. /*
  1039. * Functions to lock and unlock any filesystem running on the
  1040. * device.
  1041. */
  1042. static int lock_fs(struct mapped_device *md)
  1043. {
  1044. int r;
  1045. WARN_ON(md->frozen_sb);
  1046. md->frozen_sb = freeze_bdev(md->suspended_bdev);
  1047. if (IS_ERR(md->frozen_sb)) {
  1048. r = PTR_ERR(md->frozen_sb);
  1049. md->frozen_sb = NULL;
  1050. return r;
  1051. }
  1052. set_bit(DMF_FROZEN, &md->flags);
  1053. /* don't bdput right now, we don't want the bdev
  1054. * to go away while it is locked.
  1055. */
  1056. return 0;
  1057. }
  1058. static void unlock_fs(struct mapped_device *md)
  1059. {
  1060. if (!test_bit(DMF_FROZEN, &md->flags))
  1061. return;
  1062. thaw_bdev(md->suspended_bdev, md->frozen_sb);
  1063. md->frozen_sb = NULL;
  1064. clear_bit(DMF_FROZEN, &md->flags);
  1065. }
  1066. /*
  1067. * We need to be able to change a mapping table under a mounted
  1068. * filesystem. For example we might want to move some data in
  1069. * the background. Before the table can be swapped with
  1070. * dm_bind_table, dm_suspend must be called to flush any in
  1071. * flight bios and ensure that any further io gets deferred.
  1072. */
  1073. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  1074. {
  1075. struct dm_table *map = NULL;
  1076. unsigned long flags;
  1077. DECLARE_WAITQUEUE(wait, current);
  1078. struct bio *def;
  1079. int r = -EINVAL;
  1080. int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
  1081. int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
  1082. down(&md->suspend_lock);
  1083. if (dm_suspended(md))
  1084. goto out_unlock;
  1085. map = dm_get_table(md);
  1086. /*
  1087. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  1088. * This flag is cleared before dm_suspend returns.
  1089. */
  1090. if (noflush)
  1091. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1092. /* This does not get reverted if there's an error later. */
  1093. dm_table_presuspend_targets(map);
  1094. /* bdget() can stall if the pending I/Os are not flushed */
  1095. if (!noflush) {
  1096. md->suspended_bdev = bdget_disk(md->disk, 0);
  1097. if (!md->suspended_bdev) {
  1098. DMWARN("bdget failed in dm_suspend");
  1099. r = -ENOMEM;
  1100. goto flush_and_out;
  1101. }
  1102. }
  1103. /*
  1104. * Flush I/O to the device.
  1105. * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
  1106. */
  1107. if (do_lockfs && !noflush) {
  1108. r = lock_fs(md);
  1109. if (r)
  1110. goto out;
  1111. }
  1112. /*
  1113. * First we set the BLOCK_IO flag so no more ios will be mapped.
  1114. */
  1115. down_write(&md->io_lock);
  1116. set_bit(DMF_BLOCK_IO, &md->flags);
  1117. add_wait_queue(&md->wait, &wait);
  1118. up_write(&md->io_lock);
  1119. /* unplug */
  1120. if (map)
  1121. dm_table_unplug_all(map);
  1122. /*
  1123. * Then we wait for the already mapped ios to
  1124. * complete.
  1125. */
  1126. while (1) {
  1127. set_current_state(TASK_INTERRUPTIBLE);
  1128. if (!atomic_read(&md->pending) || signal_pending(current))
  1129. break;
  1130. io_schedule();
  1131. }
  1132. set_current_state(TASK_RUNNING);
  1133. down_write(&md->io_lock);
  1134. remove_wait_queue(&md->wait, &wait);
  1135. if (noflush) {
  1136. spin_lock_irqsave(&md->pushback_lock, flags);
  1137. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1138. bio_list_merge_head(&md->deferred, &md->pushback);
  1139. bio_list_init(&md->pushback);
  1140. spin_unlock_irqrestore(&md->pushback_lock, flags);
  1141. }
  1142. /* were we interrupted ? */
  1143. r = -EINTR;
  1144. if (atomic_read(&md->pending)) {
  1145. clear_bit(DMF_BLOCK_IO, &md->flags);
  1146. def = bio_list_get(&md->deferred);
  1147. __flush_deferred_io(md, def);
  1148. up_write(&md->io_lock);
  1149. unlock_fs(md);
  1150. goto out; /* pushback list is already flushed, so skip flush */
  1151. }
  1152. up_write(&md->io_lock);
  1153. dm_table_postsuspend_targets(map);
  1154. set_bit(DMF_SUSPENDED, &md->flags);
  1155. r = 0;
  1156. flush_and_out:
  1157. if (r && noflush) {
  1158. /*
  1159. * Because there may be already I/Os in the pushback list,
  1160. * flush them before return.
  1161. */
  1162. down_write(&md->io_lock);
  1163. spin_lock_irqsave(&md->pushback_lock, flags);
  1164. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1165. bio_list_merge_head(&md->deferred, &md->pushback);
  1166. bio_list_init(&md->pushback);
  1167. spin_unlock_irqrestore(&md->pushback_lock, flags);
  1168. def = bio_list_get(&md->deferred);
  1169. __flush_deferred_io(md, def);
  1170. up_write(&md->io_lock);
  1171. }
  1172. out:
  1173. if (r && md->suspended_bdev) {
  1174. bdput(md->suspended_bdev);
  1175. md->suspended_bdev = NULL;
  1176. }
  1177. dm_table_put(map);
  1178. out_unlock:
  1179. up(&md->suspend_lock);
  1180. return r;
  1181. }
  1182. int dm_resume(struct mapped_device *md)
  1183. {
  1184. int r = -EINVAL;
  1185. struct bio *def;
  1186. struct dm_table *map = NULL;
  1187. down(&md->suspend_lock);
  1188. if (!dm_suspended(md))
  1189. goto out;
  1190. map = dm_get_table(md);
  1191. if (!map || !dm_table_get_size(map))
  1192. goto out;
  1193. r = dm_table_resume_targets(map);
  1194. if (r)
  1195. goto out;
  1196. down_write(&md->io_lock);
  1197. clear_bit(DMF_BLOCK_IO, &md->flags);
  1198. def = bio_list_get(&md->deferred);
  1199. __flush_deferred_io(md, def);
  1200. up_write(&md->io_lock);
  1201. unlock_fs(md);
  1202. if (md->suspended_bdev) {
  1203. bdput(md->suspended_bdev);
  1204. md->suspended_bdev = NULL;
  1205. }
  1206. clear_bit(DMF_SUSPENDED, &md->flags);
  1207. dm_table_unplug_all(map);
  1208. kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
  1209. r = 0;
  1210. out:
  1211. dm_table_put(map);
  1212. up(&md->suspend_lock);
  1213. return r;
  1214. }
  1215. /*-----------------------------------------------------------------
  1216. * Event notification.
  1217. *---------------------------------------------------------------*/
  1218. uint32_t dm_get_event_nr(struct mapped_device *md)
  1219. {
  1220. return atomic_read(&md->event_nr);
  1221. }
  1222. int dm_wait_event(struct mapped_device *md, int event_nr)
  1223. {
  1224. return wait_event_interruptible(md->eventq,
  1225. (event_nr != atomic_read(&md->event_nr)));
  1226. }
  1227. /*
  1228. * The gendisk is only valid as long as you have a reference
  1229. * count on 'md'.
  1230. */
  1231. struct gendisk *dm_disk(struct mapped_device *md)
  1232. {
  1233. return md->disk;
  1234. }
  1235. int dm_suspended(struct mapped_device *md)
  1236. {
  1237. return test_bit(DMF_SUSPENDED, &md->flags);
  1238. }
  1239. int dm_noflush_suspending(struct dm_target *ti)
  1240. {
  1241. struct mapped_device *md = dm_table_get_md(ti->table);
  1242. int r = __noflush_suspending(md);
  1243. dm_put(md);
  1244. return r;
  1245. }
  1246. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  1247. static struct block_device_operations dm_blk_dops = {
  1248. .open = dm_blk_open,
  1249. .release = dm_blk_close,
  1250. .ioctl = dm_blk_ioctl,
  1251. .getgeo = dm_blk_getgeo,
  1252. .owner = THIS_MODULE
  1253. };
  1254. EXPORT_SYMBOL(dm_get_mapinfo);
  1255. /*
  1256. * module hooks
  1257. */
  1258. module_init(dm_init);
  1259. module_exit(dm_exit);
  1260. module_param(major, uint, 0);
  1261. MODULE_PARM_DESC(major, "The major number of the device mapper");
  1262. MODULE_DESCRIPTION(DM_NAME " driver");
  1263. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  1264. MODULE_LICENSE("GPL");