dm.c 32 KB

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