dm.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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 void __clone_and_map(struct clone_info *ci)
  547. {
  548. struct bio *clone, *bio = ci->bio;
  549. struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
  550. sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
  551. struct dm_target_io *tio;
  552. /*
  553. * Allocate a target io object.
  554. */
  555. tio = alloc_tio(ci->md);
  556. tio->io = ci->io;
  557. tio->ti = ti;
  558. memset(&tio->info, 0, sizeof(tio->info));
  559. if (ci->sector_count <= max) {
  560. /*
  561. * Optimise for the simple case where we can do all of
  562. * the remaining io with a single clone.
  563. */
  564. clone = clone_bio(bio, ci->sector, ci->idx,
  565. bio->bi_vcnt - ci->idx, ci->sector_count,
  566. ci->md->bs);
  567. __map_bio(ti, clone, tio);
  568. ci->sector_count = 0;
  569. } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
  570. /*
  571. * There are some bvecs that don't span targets.
  572. * Do as many of these as possible.
  573. */
  574. int i;
  575. sector_t remaining = max;
  576. sector_t bv_len;
  577. for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
  578. bv_len = to_sector(bio->bi_io_vec[i].bv_len);
  579. if (bv_len > remaining)
  580. break;
  581. remaining -= bv_len;
  582. len += bv_len;
  583. }
  584. clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
  585. ci->md->bs);
  586. __map_bio(ti, clone, tio);
  587. ci->sector += len;
  588. ci->sector_count -= len;
  589. ci->idx = i;
  590. } else {
  591. /*
  592. * Handle a bvec that must be split between two or more targets.
  593. */
  594. struct bio_vec *bv = bio->bi_io_vec + ci->idx;
  595. sector_t remaining = to_sector(bv->bv_len);
  596. unsigned int offset = 0;
  597. do {
  598. if (offset) {
  599. ti = dm_table_find_target(ci->map, ci->sector);
  600. max = max_io_len(ci->md, ci->sector, ti);
  601. tio = alloc_tio(ci->md);
  602. tio->io = ci->io;
  603. tio->ti = ti;
  604. memset(&tio->info, 0, sizeof(tio->info));
  605. }
  606. len = min(remaining, max);
  607. clone = split_bvec(bio, ci->sector, ci->idx,
  608. bv->bv_offset + offset, len,
  609. ci->md->bs);
  610. __map_bio(ti, clone, tio);
  611. ci->sector += len;
  612. ci->sector_count -= len;
  613. offset += to_bytes(len);
  614. } while (remaining -= len);
  615. ci->idx++;
  616. }
  617. }
  618. /*
  619. * Split the bio into several clones.
  620. */
  621. static int __split_bio(struct mapped_device *md, struct bio *bio)
  622. {
  623. struct clone_info ci;
  624. ci.map = dm_get_table(md);
  625. if (unlikely(!ci.map))
  626. return -EIO;
  627. ci.md = md;
  628. ci.bio = bio;
  629. ci.io = alloc_io(md);
  630. ci.io->error = 0;
  631. atomic_set(&ci.io->io_count, 1);
  632. ci.io->bio = bio;
  633. ci.io->md = md;
  634. ci.sector = bio->bi_sector;
  635. ci.sector_count = bio_sectors(bio);
  636. ci.idx = bio->bi_idx;
  637. start_io_acct(ci.io);
  638. while (ci.sector_count)
  639. __clone_and_map(&ci);
  640. /* drop the extra reference count */
  641. dec_pending(ci.io, 0);
  642. dm_table_put(ci.map);
  643. return 0;
  644. }
  645. /*-----------------------------------------------------------------
  646. * CRUD END
  647. *---------------------------------------------------------------*/
  648. /*
  649. * The request function that just remaps the bio built up by
  650. * dm_merge_bvec.
  651. */
  652. static int dm_request(struct request_queue *q, struct bio *bio)
  653. {
  654. int r = -EIO;
  655. int rw = bio_data_dir(bio);
  656. struct mapped_device *md = q->queuedata;
  657. /*
  658. * There is no use in forwarding any barrier request since we can't
  659. * guarantee it is (or can be) handled by the targets correctly.
  660. */
  661. if (unlikely(bio_barrier(bio))) {
  662. bio_endio(bio, -EOPNOTSUPP);
  663. return 0;
  664. }
  665. down_read(&md->io_lock);
  666. disk_stat_inc(dm_disk(md), ios[rw]);
  667. disk_stat_add(dm_disk(md), sectors[rw], bio_sectors(bio));
  668. /*
  669. * If we're suspended we have to queue
  670. * this io for later.
  671. */
  672. while (test_bit(DMF_BLOCK_IO, &md->flags)) {
  673. up_read(&md->io_lock);
  674. if (bio_rw(bio) != READA)
  675. r = queue_io(md, bio);
  676. if (r <= 0)
  677. goto out_req;
  678. /*
  679. * We're in a while loop, because someone could suspend
  680. * before we get to the following read lock.
  681. */
  682. down_read(&md->io_lock);
  683. }
  684. r = __split_bio(md, bio);
  685. up_read(&md->io_lock);
  686. out_req:
  687. if (r < 0)
  688. bio_io_error(bio);
  689. return 0;
  690. }
  691. static void dm_unplug_all(struct request_queue *q)
  692. {
  693. struct mapped_device *md = q->queuedata;
  694. struct dm_table *map = dm_get_table(md);
  695. if (map) {
  696. dm_table_unplug_all(map);
  697. dm_table_put(map);
  698. }
  699. }
  700. static int dm_any_congested(void *congested_data, int bdi_bits)
  701. {
  702. int r;
  703. struct mapped_device *md = (struct mapped_device *) congested_data;
  704. struct dm_table *map = dm_get_table(md);
  705. if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
  706. r = bdi_bits;
  707. else
  708. r = dm_table_any_congested(map, bdi_bits);
  709. dm_table_put(map);
  710. return r;
  711. }
  712. /*-----------------------------------------------------------------
  713. * An IDR is used to keep track of allocated minor numbers.
  714. *---------------------------------------------------------------*/
  715. static DEFINE_IDR(_minor_idr);
  716. static void free_minor(int minor)
  717. {
  718. spin_lock(&_minor_lock);
  719. idr_remove(&_minor_idr, minor);
  720. spin_unlock(&_minor_lock);
  721. }
  722. /*
  723. * See if the device with a specific minor # is free.
  724. */
  725. static int specific_minor(struct mapped_device *md, int minor)
  726. {
  727. int r, m;
  728. if (minor >= (1 << MINORBITS))
  729. return -EINVAL;
  730. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  731. if (!r)
  732. return -ENOMEM;
  733. spin_lock(&_minor_lock);
  734. if (idr_find(&_minor_idr, minor)) {
  735. r = -EBUSY;
  736. goto out;
  737. }
  738. r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
  739. if (r)
  740. goto out;
  741. if (m != minor) {
  742. idr_remove(&_minor_idr, m);
  743. r = -EBUSY;
  744. goto out;
  745. }
  746. out:
  747. spin_unlock(&_minor_lock);
  748. return r;
  749. }
  750. static int next_free_minor(struct mapped_device *md, int *minor)
  751. {
  752. int r, m;
  753. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  754. if (!r)
  755. return -ENOMEM;
  756. spin_lock(&_minor_lock);
  757. r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
  758. if (r) {
  759. goto out;
  760. }
  761. if (m >= (1 << MINORBITS)) {
  762. idr_remove(&_minor_idr, m);
  763. r = -ENOSPC;
  764. goto out;
  765. }
  766. *minor = m;
  767. out:
  768. spin_unlock(&_minor_lock);
  769. return r;
  770. }
  771. static struct block_device_operations dm_blk_dops;
  772. /*
  773. * Allocate and initialise a blank device with a given minor.
  774. */
  775. static struct mapped_device *alloc_dev(int minor)
  776. {
  777. int r;
  778. struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
  779. void *old_md;
  780. if (!md) {
  781. DMWARN("unable to allocate device, out of memory.");
  782. return NULL;
  783. }
  784. if (!try_module_get(THIS_MODULE))
  785. goto bad0;
  786. /* get a minor number for the dev */
  787. if (minor == DM_ANY_MINOR)
  788. r = next_free_minor(md, &minor);
  789. else
  790. r = specific_minor(md, minor);
  791. if (r < 0)
  792. goto bad1;
  793. memset(md, 0, sizeof(*md));
  794. init_rwsem(&md->io_lock);
  795. init_MUTEX(&md->suspend_lock);
  796. spin_lock_init(&md->pushback_lock);
  797. rwlock_init(&md->map_lock);
  798. atomic_set(&md->holders, 1);
  799. atomic_set(&md->open_count, 0);
  800. atomic_set(&md->event_nr, 0);
  801. atomic_set(&md->uevent_seq, 0);
  802. INIT_LIST_HEAD(&md->uevent_list);
  803. spin_lock_init(&md->uevent_lock);
  804. md->queue = blk_alloc_queue(GFP_KERNEL);
  805. if (!md->queue)
  806. goto bad1_free_minor;
  807. md->queue->queuedata = md;
  808. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  809. md->queue->backing_dev_info.congested_data = md;
  810. blk_queue_make_request(md->queue, dm_request);
  811. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  812. md->queue->unplug_fn = dm_unplug_all;
  813. md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
  814. if (!md->io_pool)
  815. goto bad2;
  816. md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
  817. if (!md->tio_pool)
  818. goto bad3;
  819. md->bs = bioset_create(16, 16);
  820. if (!md->bs)
  821. goto bad_no_bioset;
  822. md->disk = alloc_disk(1);
  823. if (!md->disk)
  824. goto bad4;
  825. atomic_set(&md->pending, 0);
  826. init_waitqueue_head(&md->wait);
  827. init_waitqueue_head(&md->eventq);
  828. md->disk->major = _major;
  829. md->disk->first_minor = minor;
  830. md->disk->fops = &dm_blk_dops;
  831. md->disk->queue = md->queue;
  832. md->disk->private_data = md;
  833. sprintf(md->disk->disk_name, "dm-%d", minor);
  834. add_disk(md->disk);
  835. format_dev_t(md->name, MKDEV(_major, minor));
  836. /* Populate the mapping, nobody knows we exist yet */
  837. spin_lock(&_minor_lock);
  838. old_md = idr_replace(&_minor_idr, md, minor);
  839. spin_unlock(&_minor_lock);
  840. BUG_ON(old_md != MINOR_ALLOCED);
  841. return md;
  842. bad4:
  843. bioset_free(md->bs);
  844. bad_no_bioset:
  845. mempool_destroy(md->tio_pool);
  846. bad3:
  847. mempool_destroy(md->io_pool);
  848. bad2:
  849. blk_cleanup_queue(md->queue);
  850. bad1_free_minor:
  851. free_minor(minor);
  852. bad1:
  853. module_put(THIS_MODULE);
  854. bad0:
  855. kfree(md);
  856. return NULL;
  857. }
  858. static void unlock_fs(struct mapped_device *md);
  859. static void free_dev(struct mapped_device *md)
  860. {
  861. int minor = md->disk->first_minor;
  862. if (md->suspended_bdev) {
  863. unlock_fs(md);
  864. bdput(md->suspended_bdev);
  865. }
  866. mempool_destroy(md->tio_pool);
  867. mempool_destroy(md->io_pool);
  868. bioset_free(md->bs);
  869. del_gendisk(md->disk);
  870. free_minor(minor);
  871. spin_lock(&_minor_lock);
  872. md->disk->private_data = NULL;
  873. spin_unlock(&_minor_lock);
  874. put_disk(md->disk);
  875. blk_cleanup_queue(md->queue);
  876. module_put(THIS_MODULE);
  877. kfree(md);
  878. }
  879. /*
  880. * Bind a table to the device.
  881. */
  882. static void event_callback(void *context)
  883. {
  884. unsigned long flags;
  885. LIST_HEAD(uevents);
  886. struct mapped_device *md = (struct mapped_device *) context;
  887. spin_lock_irqsave(&md->uevent_lock, flags);
  888. list_splice_init(&md->uevent_list, &uevents);
  889. spin_unlock_irqrestore(&md->uevent_lock, flags);
  890. dm_send_uevents(&uevents, &md->disk->kobj);
  891. atomic_inc(&md->event_nr);
  892. wake_up(&md->eventq);
  893. }
  894. static void __set_size(struct mapped_device *md, sector_t size)
  895. {
  896. set_capacity(md->disk, size);
  897. mutex_lock(&md->suspended_bdev->bd_inode->i_mutex);
  898. i_size_write(md->suspended_bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  899. mutex_unlock(&md->suspended_bdev->bd_inode->i_mutex);
  900. }
  901. static int __bind(struct mapped_device *md, struct dm_table *t)
  902. {
  903. struct request_queue *q = md->queue;
  904. sector_t size;
  905. size = dm_table_get_size(t);
  906. /*
  907. * Wipe any geometry if the size of the table changed.
  908. */
  909. if (size != get_capacity(md->disk))
  910. memset(&md->geometry, 0, sizeof(md->geometry));
  911. if (md->suspended_bdev)
  912. __set_size(md, size);
  913. if (size == 0)
  914. return 0;
  915. dm_table_get(t);
  916. dm_table_event_callback(t, event_callback, md);
  917. write_lock(&md->map_lock);
  918. md->map = t;
  919. dm_table_set_restrictions(t, q);
  920. write_unlock(&md->map_lock);
  921. return 0;
  922. }
  923. static void __unbind(struct mapped_device *md)
  924. {
  925. struct dm_table *map = md->map;
  926. if (!map)
  927. return;
  928. dm_table_event_callback(map, NULL, NULL);
  929. write_lock(&md->map_lock);
  930. md->map = NULL;
  931. write_unlock(&md->map_lock);
  932. dm_table_put(map);
  933. }
  934. /*
  935. * Constructor for a new device.
  936. */
  937. int dm_create(int minor, struct mapped_device **result)
  938. {
  939. struct mapped_device *md;
  940. md = alloc_dev(minor);
  941. if (!md)
  942. return -ENXIO;
  943. *result = md;
  944. return 0;
  945. }
  946. static struct mapped_device *dm_find_md(dev_t dev)
  947. {
  948. struct mapped_device *md;
  949. unsigned minor = MINOR(dev);
  950. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  951. return NULL;
  952. spin_lock(&_minor_lock);
  953. md = idr_find(&_minor_idr, minor);
  954. if (md && (md == MINOR_ALLOCED ||
  955. (dm_disk(md)->first_minor != minor) ||
  956. test_bit(DMF_FREEING, &md->flags))) {
  957. md = NULL;
  958. goto out;
  959. }
  960. out:
  961. spin_unlock(&_minor_lock);
  962. return md;
  963. }
  964. struct mapped_device *dm_get_md(dev_t dev)
  965. {
  966. struct mapped_device *md = dm_find_md(dev);
  967. if (md)
  968. dm_get(md);
  969. return md;
  970. }
  971. void *dm_get_mdptr(struct mapped_device *md)
  972. {
  973. return md->interface_ptr;
  974. }
  975. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  976. {
  977. md->interface_ptr = ptr;
  978. }
  979. void dm_get(struct mapped_device *md)
  980. {
  981. atomic_inc(&md->holders);
  982. }
  983. const char *dm_device_name(struct mapped_device *md)
  984. {
  985. return md->name;
  986. }
  987. EXPORT_SYMBOL_GPL(dm_device_name);
  988. void dm_put(struct mapped_device *md)
  989. {
  990. struct dm_table *map;
  991. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  992. if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
  993. map = dm_get_table(md);
  994. idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
  995. set_bit(DMF_FREEING, &md->flags);
  996. spin_unlock(&_minor_lock);
  997. if (!dm_suspended(md)) {
  998. dm_table_presuspend_targets(map);
  999. dm_table_postsuspend_targets(map);
  1000. }
  1001. __unbind(md);
  1002. dm_table_put(map);
  1003. free_dev(md);
  1004. }
  1005. }
  1006. EXPORT_SYMBOL_GPL(dm_put);
  1007. /*
  1008. * Process the deferred bios
  1009. */
  1010. static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
  1011. {
  1012. struct bio *n;
  1013. while (c) {
  1014. n = c->bi_next;
  1015. c->bi_next = NULL;
  1016. if (__split_bio(md, c))
  1017. bio_io_error(c);
  1018. c = n;
  1019. }
  1020. }
  1021. /*
  1022. * Swap in a new table (destroying old one).
  1023. */
  1024. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  1025. {
  1026. int r = -EINVAL;
  1027. down(&md->suspend_lock);
  1028. /* device must be suspended */
  1029. if (!dm_suspended(md))
  1030. goto out;
  1031. /* without bdev, the device size cannot be changed */
  1032. if (!md->suspended_bdev)
  1033. if (get_capacity(md->disk) != dm_table_get_size(table))
  1034. goto out;
  1035. __unbind(md);
  1036. r = __bind(md, table);
  1037. out:
  1038. up(&md->suspend_lock);
  1039. return r;
  1040. }
  1041. /*
  1042. * Functions to lock and unlock any filesystem running on the
  1043. * device.
  1044. */
  1045. static int lock_fs(struct mapped_device *md)
  1046. {
  1047. int r;
  1048. WARN_ON(md->frozen_sb);
  1049. md->frozen_sb = freeze_bdev(md->suspended_bdev);
  1050. if (IS_ERR(md->frozen_sb)) {
  1051. r = PTR_ERR(md->frozen_sb);
  1052. md->frozen_sb = NULL;
  1053. return r;
  1054. }
  1055. set_bit(DMF_FROZEN, &md->flags);
  1056. /* don't bdput right now, we don't want the bdev
  1057. * to go away while it is locked.
  1058. */
  1059. return 0;
  1060. }
  1061. static void unlock_fs(struct mapped_device *md)
  1062. {
  1063. if (!test_bit(DMF_FROZEN, &md->flags))
  1064. return;
  1065. thaw_bdev(md->suspended_bdev, md->frozen_sb);
  1066. md->frozen_sb = NULL;
  1067. clear_bit(DMF_FROZEN, &md->flags);
  1068. }
  1069. /*
  1070. * We need to be able to change a mapping table under a mounted
  1071. * filesystem. For example we might want to move some data in
  1072. * the background. Before the table can be swapped with
  1073. * dm_bind_table, dm_suspend must be called to flush any in
  1074. * flight bios and ensure that any further io gets deferred.
  1075. */
  1076. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  1077. {
  1078. struct dm_table *map = NULL;
  1079. unsigned long flags;
  1080. DECLARE_WAITQUEUE(wait, current);
  1081. struct bio *def;
  1082. int r = -EINVAL;
  1083. int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
  1084. int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
  1085. down(&md->suspend_lock);
  1086. if (dm_suspended(md))
  1087. goto out_unlock;
  1088. map = dm_get_table(md);
  1089. /*
  1090. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  1091. * This flag is cleared before dm_suspend returns.
  1092. */
  1093. if (noflush)
  1094. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1095. /* This does not get reverted if there's an error later. */
  1096. dm_table_presuspend_targets(map);
  1097. /* bdget() can stall if the pending I/Os are not flushed */
  1098. if (!noflush) {
  1099. md->suspended_bdev = bdget_disk(md->disk, 0);
  1100. if (!md->suspended_bdev) {
  1101. DMWARN("bdget failed in dm_suspend");
  1102. r = -ENOMEM;
  1103. goto flush_and_out;
  1104. }
  1105. }
  1106. /*
  1107. * Flush I/O to the device.
  1108. * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
  1109. */
  1110. if (do_lockfs && !noflush) {
  1111. r = lock_fs(md);
  1112. if (r)
  1113. goto out;
  1114. }
  1115. /*
  1116. * First we set the BLOCK_IO flag so no more ios will be mapped.
  1117. */
  1118. down_write(&md->io_lock);
  1119. set_bit(DMF_BLOCK_IO, &md->flags);
  1120. add_wait_queue(&md->wait, &wait);
  1121. up_write(&md->io_lock);
  1122. /* unplug */
  1123. if (map)
  1124. dm_table_unplug_all(map);
  1125. /*
  1126. * Then we wait for the already mapped ios to
  1127. * complete.
  1128. */
  1129. while (1) {
  1130. set_current_state(TASK_INTERRUPTIBLE);
  1131. if (!atomic_read(&md->pending) || signal_pending(current))
  1132. break;
  1133. io_schedule();
  1134. }
  1135. set_current_state(TASK_RUNNING);
  1136. down_write(&md->io_lock);
  1137. remove_wait_queue(&md->wait, &wait);
  1138. if (noflush) {
  1139. spin_lock_irqsave(&md->pushback_lock, flags);
  1140. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1141. bio_list_merge_head(&md->deferred, &md->pushback);
  1142. bio_list_init(&md->pushback);
  1143. spin_unlock_irqrestore(&md->pushback_lock, flags);
  1144. }
  1145. /* were we interrupted ? */
  1146. r = -EINTR;
  1147. if (atomic_read(&md->pending)) {
  1148. clear_bit(DMF_BLOCK_IO, &md->flags);
  1149. def = bio_list_get(&md->deferred);
  1150. __flush_deferred_io(md, def);
  1151. up_write(&md->io_lock);
  1152. unlock_fs(md);
  1153. goto out; /* pushback list is already flushed, so skip flush */
  1154. }
  1155. up_write(&md->io_lock);
  1156. dm_table_postsuspend_targets(map);
  1157. set_bit(DMF_SUSPENDED, &md->flags);
  1158. r = 0;
  1159. flush_and_out:
  1160. if (r && noflush) {
  1161. /*
  1162. * Because there may be already I/Os in the pushback list,
  1163. * flush them before return.
  1164. */
  1165. down_write(&md->io_lock);
  1166. spin_lock_irqsave(&md->pushback_lock, flags);
  1167. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1168. bio_list_merge_head(&md->deferred, &md->pushback);
  1169. bio_list_init(&md->pushback);
  1170. spin_unlock_irqrestore(&md->pushback_lock, flags);
  1171. def = bio_list_get(&md->deferred);
  1172. __flush_deferred_io(md, def);
  1173. up_write(&md->io_lock);
  1174. }
  1175. out:
  1176. if (r && md->suspended_bdev) {
  1177. bdput(md->suspended_bdev);
  1178. md->suspended_bdev = NULL;
  1179. }
  1180. dm_table_put(map);
  1181. out_unlock:
  1182. up(&md->suspend_lock);
  1183. return r;
  1184. }
  1185. int dm_resume(struct mapped_device *md)
  1186. {
  1187. int r = -EINVAL;
  1188. struct bio *def;
  1189. struct dm_table *map = NULL;
  1190. down(&md->suspend_lock);
  1191. if (!dm_suspended(md))
  1192. goto out;
  1193. map = dm_get_table(md);
  1194. if (!map || !dm_table_get_size(map))
  1195. goto out;
  1196. r = dm_table_resume_targets(map);
  1197. if (r)
  1198. goto out;
  1199. down_write(&md->io_lock);
  1200. clear_bit(DMF_BLOCK_IO, &md->flags);
  1201. def = bio_list_get(&md->deferred);
  1202. __flush_deferred_io(md, def);
  1203. up_write(&md->io_lock);
  1204. unlock_fs(md);
  1205. if (md->suspended_bdev) {
  1206. bdput(md->suspended_bdev);
  1207. md->suspended_bdev = NULL;
  1208. }
  1209. clear_bit(DMF_SUSPENDED, &md->flags);
  1210. dm_table_unplug_all(map);
  1211. kobject_uevent(&md->disk->kobj, KOBJ_CHANGE);
  1212. r = 0;
  1213. out:
  1214. dm_table_put(map);
  1215. up(&md->suspend_lock);
  1216. return r;
  1217. }
  1218. /*-----------------------------------------------------------------
  1219. * Event notification.
  1220. *---------------------------------------------------------------*/
  1221. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  1222. {
  1223. return atomic_add_return(1, &md->uevent_seq);
  1224. }
  1225. uint32_t dm_get_event_nr(struct mapped_device *md)
  1226. {
  1227. return atomic_read(&md->event_nr);
  1228. }
  1229. int dm_wait_event(struct mapped_device *md, int event_nr)
  1230. {
  1231. return wait_event_interruptible(md->eventq,
  1232. (event_nr != atomic_read(&md->event_nr)));
  1233. }
  1234. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  1235. {
  1236. unsigned long flags;
  1237. spin_lock_irqsave(&md->uevent_lock, flags);
  1238. list_add(elist, &md->uevent_list);
  1239. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1240. }
  1241. /*
  1242. * The gendisk is only valid as long as you have a reference
  1243. * count on 'md'.
  1244. */
  1245. struct gendisk *dm_disk(struct mapped_device *md)
  1246. {
  1247. return md->disk;
  1248. }
  1249. int dm_suspended(struct mapped_device *md)
  1250. {
  1251. return test_bit(DMF_SUSPENDED, &md->flags);
  1252. }
  1253. int dm_noflush_suspending(struct dm_target *ti)
  1254. {
  1255. struct mapped_device *md = dm_table_get_md(ti->table);
  1256. int r = __noflush_suspending(md);
  1257. dm_put(md);
  1258. return r;
  1259. }
  1260. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  1261. static struct block_device_operations dm_blk_dops = {
  1262. .open = dm_blk_open,
  1263. .release = dm_blk_close,
  1264. .ioctl = dm_blk_ioctl,
  1265. .getgeo = dm_blk_getgeo,
  1266. .owner = THIS_MODULE
  1267. };
  1268. EXPORT_SYMBOL(dm_get_mapinfo);
  1269. /*
  1270. * module hooks
  1271. */
  1272. module_init(dm_init);
  1273. module_exit(dm_exit);
  1274. module_param(major, uint, 0);
  1275. MODULE_PARM_DESC(major, "The major number of the device mapper");
  1276. MODULE_DESCRIPTION(DM_NAME " driver");
  1277. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  1278. MODULE_LICENSE("GPL");