dm.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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 mutex 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. static 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. static 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 bad_module_get;
  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 bad_minor;
  801. memset(md, 0, sizeof(*md));
  802. init_rwsem(&md->io_lock);
  803. mutex_init(&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 bad_queue;
  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 bad_io_pool;
  824. md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
  825. if (!md->tio_pool)
  826. goto bad_tio_pool;
  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 bad_disk;
  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. bad_disk:
  851. bioset_free(md->bs);
  852. bad_no_bioset:
  853. mempool_destroy(md->tio_pool);
  854. bad_tio_pool:
  855. mempool_destroy(md->io_pool);
  856. bad_io_pool:
  857. blk_cleanup_queue(md->queue);
  858. bad_queue:
  859. free_minor(minor);
  860. bad_minor:
  861. module_put(THIS_MODULE);
  862. bad_module_get:
  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. static int dm_wait_for_completion(struct mapped_device *md)
  1016. {
  1017. int r = 0;
  1018. while (1) {
  1019. set_current_state(TASK_INTERRUPTIBLE);
  1020. smp_mb();
  1021. if (!atomic_read(&md->pending))
  1022. break;
  1023. if (signal_pending(current)) {
  1024. r = -EINTR;
  1025. break;
  1026. }
  1027. io_schedule();
  1028. }
  1029. set_current_state(TASK_RUNNING);
  1030. return r;
  1031. }
  1032. /*
  1033. * Process the deferred bios
  1034. */
  1035. static void __flush_deferred_io(struct mapped_device *md)
  1036. {
  1037. struct bio *c;
  1038. while ((c = bio_list_pop(&md->deferred))) {
  1039. if (__split_bio(md, c))
  1040. bio_io_error(c);
  1041. }
  1042. clear_bit(DMF_BLOCK_IO, &md->flags);
  1043. }
  1044. static void __merge_pushback_list(struct mapped_device *md)
  1045. {
  1046. unsigned long flags;
  1047. spin_lock_irqsave(&md->pushback_lock, flags);
  1048. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1049. bio_list_merge_head(&md->deferred, &md->pushback);
  1050. bio_list_init(&md->pushback);
  1051. spin_unlock_irqrestore(&md->pushback_lock, flags);
  1052. }
  1053. /*
  1054. * Swap in a new table (destroying old one).
  1055. */
  1056. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  1057. {
  1058. int r = -EINVAL;
  1059. mutex_lock(&md->suspend_lock);
  1060. /* device must be suspended */
  1061. if (!dm_suspended(md))
  1062. goto out;
  1063. /* without bdev, the device size cannot be changed */
  1064. if (!md->suspended_bdev)
  1065. if (get_capacity(md->disk) != dm_table_get_size(table))
  1066. goto out;
  1067. __unbind(md);
  1068. r = __bind(md, table);
  1069. out:
  1070. mutex_unlock(&md->suspend_lock);
  1071. return r;
  1072. }
  1073. /*
  1074. * Functions to lock and unlock any filesystem running on the
  1075. * device.
  1076. */
  1077. static int lock_fs(struct mapped_device *md)
  1078. {
  1079. int r;
  1080. WARN_ON(md->frozen_sb);
  1081. md->frozen_sb = freeze_bdev(md->suspended_bdev);
  1082. if (IS_ERR(md->frozen_sb)) {
  1083. r = PTR_ERR(md->frozen_sb);
  1084. md->frozen_sb = NULL;
  1085. return r;
  1086. }
  1087. set_bit(DMF_FROZEN, &md->flags);
  1088. /* don't bdput right now, we don't want the bdev
  1089. * to go away while it is locked.
  1090. */
  1091. return 0;
  1092. }
  1093. static void unlock_fs(struct mapped_device *md)
  1094. {
  1095. if (!test_bit(DMF_FROZEN, &md->flags))
  1096. return;
  1097. thaw_bdev(md->suspended_bdev, md->frozen_sb);
  1098. md->frozen_sb = NULL;
  1099. clear_bit(DMF_FROZEN, &md->flags);
  1100. }
  1101. /*
  1102. * We need to be able to change a mapping table under a mounted
  1103. * filesystem. For example we might want to move some data in
  1104. * the background. Before the table can be swapped with
  1105. * dm_bind_table, dm_suspend must be called to flush any in
  1106. * flight bios and ensure that any further io gets deferred.
  1107. */
  1108. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  1109. {
  1110. struct dm_table *map = NULL;
  1111. DECLARE_WAITQUEUE(wait, current);
  1112. int r = 0;
  1113. int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
  1114. int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
  1115. mutex_lock(&md->suspend_lock);
  1116. if (dm_suspended(md)) {
  1117. r = -EINVAL;
  1118. goto out_unlock;
  1119. }
  1120. map = dm_get_table(md);
  1121. /*
  1122. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  1123. * This flag is cleared before dm_suspend returns.
  1124. */
  1125. if (noflush)
  1126. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1127. /* This does not get reverted if there's an error later. */
  1128. dm_table_presuspend_targets(map);
  1129. /* bdget() can stall if the pending I/Os are not flushed */
  1130. if (!noflush) {
  1131. md->suspended_bdev = bdget_disk(md->disk, 0);
  1132. if (!md->suspended_bdev) {
  1133. DMWARN("bdget failed in dm_suspend");
  1134. r = -ENOMEM;
  1135. goto flush_and_out;
  1136. }
  1137. /*
  1138. * Flush I/O to the device. noflush supersedes do_lockfs,
  1139. * because lock_fs() needs to flush I/Os.
  1140. */
  1141. if (do_lockfs) {
  1142. r = lock_fs(md);
  1143. if (r)
  1144. goto out;
  1145. }
  1146. }
  1147. /*
  1148. * First we set the BLOCK_IO flag so no more ios will be mapped.
  1149. */
  1150. down_write(&md->io_lock);
  1151. set_bit(DMF_BLOCK_IO, &md->flags);
  1152. add_wait_queue(&md->wait, &wait);
  1153. up_write(&md->io_lock);
  1154. /* unplug */
  1155. if (map)
  1156. dm_table_unplug_all(map);
  1157. /*
  1158. * Wait for the already-mapped ios to complete.
  1159. */
  1160. r = dm_wait_for_completion(md);
  1161. down_write(&md->io_lock);
  1162. remove_wait_queue(&md->wait, &wait);
  1163. if (noflush)
  1164. __merge_pushback_list(md);
  1165. up_write(&md->io_lock);
  1166. /* were we interrupted ? */
  1167. if (r < 0) {
  1168. down_write(&md->io_lock);
  1169. __flush_deferred_io(md);
  1170. up_write(&md->io_lock);
  1171. unlock_fs(md);
  1172. goto out; /* pushback list is already flushed, so skip flush */
  1173. }
  1174. dm_table_postsuspend_targets(map);
  1175. set_bit(DMF_SUSPENDED, &md->flags);
  1176. flush_and_out:
  1177. if (r && noflush) {
  1178. /*
  1179. * Because there may be already I/Os in the pushback list,
  1180. * flush them before return.
  1181. */
  1182. down_write(&md->io_lock);
  1183. __merge_pushback_list(md);
  1184. __flush_deferred_io(md);
  1185. up_write(&md->io_lock);
  1186. }
  1187. out:
  1188. if (r && md->suspended_bdev) {
  1189. bdput(md->suspended_bdev);
  1190. md->suspended_bdev = NULL;
  1191. }
  1192. dm_table_put(map);
  1193. out_unlock:
  1194. mutex_unlock(&md->suspend_lock);
  1195. return r;
  1196. }
  1197. int dm_resume(struct mapped_device *md)
  1198. {
  1199. int r = -EINVAL;
  1200. struct dm_table *map = NULL;
  1201. mutex_lock(&md->suspend_lock);
  1202. if (!dm_suspended(md))
  1203. goto out;
  1204. map = dm_get_table(md);
  1205. if (!map || !dm_table_get_size(map))
  1206. goto out;
  1207. r = dm_table_resume_targets(map);
  1208. if (r)
  1209. goto out;
  1210. down_write(&md->io_lock);
  1211. __flush_deferred_io(md);
  1212. up_write(&md->io_lock);
  1213. unlock_fs(md);
  1214. if (md->suspended_bdev) {
  1215. bdput(md->suspended_bdev);
  1216. md->suspended_bdev = NULL;
  1217. }
  1218. clear_bit(DMF_SUSPENDED, &md->flags);
  1219. dm_table_unplug_all(map);
  1220. dm_kobject_uevent(md);
  1221. r = 0;
  1222. out:
  1223. dm_table_put(map);
  1224. mutex_unlock(&md->suspend_lock);
  1225. return r;
  1226. }
  1227. /*-----------------------------------------------------------------
  1228. * Event notification.
  1229. *---------------------------------------------------------------*/
  1230. void dm_kobject_uevent(struct mapped_device *md)
  1231. {
  1232. kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE);
  1233. }
  1234. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  1235. {
  1236. return atomic_add_return(1, &md->uevent_seq);
  1237. }
  1238. uint32_t dm_get_event_nr(struct mapped_device *md)
  1239. {
  1240. return atomic_read(&md->event_nr);
  1241. }
  1242. int dm_wait_event(struct mapped_device *md, int event_nr)
  1243. {
  1244. return wait_event_interruptible(md->eventq,
  1245. (event_nr != atomic_read(&md->event_nr)));
  1246. }
  1247. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  1248. {
  1249. unsigned long flags;
  1250. spin_lock_irqsave(&md->uevent_lock, flags);
  1251. list_add(elist, &md->uevent_list);
  1252. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1253. }
  1254. /*
  1255. * The gendisk is only valid as long as you have a reference
  1256. * count on 'md'.
  1257. */
  1258. struct gendisk *dm_disk(struct mapped_device *md)
  1259. {
  1260. return md->disk;
  1261. }
  1262. int dm_suspended(struct mapped_device *md)
  1263. {
  1264. return test_bit(DMF_SUSPENDED, &md->flags);
  1265. }
  1266. int dm_noflush_suspending(struct dm_target *ti)
  1267. {
  1268. struct mapped_device *md = dm_table_get_md(ti->table);
  1269. int r = __noflush_suspending(md);
  1270. dm_put(md);
  1271. return r;
  1272. }
  1273. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  1274. static struct block_device_operations dm_blk_dops = {
  1275. .open = dm_blk_open,
  1276. .release = dm_blk_close,
  1277. .ioctl = dm_blk_ioctl,
  1278. .getgeo = dm_blk_getgeo,
  1279. .owner = THIS_MODULE
  1280. };
  1281. EXPORT_SYMBOL(dm_get_mapinfo);
  1282. /*
  1283. * module hooks
  1284. */
  1285. module_init(dm_init);
  1286. module_exit(dm_exit);
  1287. module_param(major, uint, 0);
  1288. MODULE_PARM_DESC(major, "The major number of the device mapper");
  1289. MODULE_DESCRIPTION(DM_NAME " driver");
  1290. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  1291. MODULE_LICENSE("GPL");