dm.c 34 KB

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