dm.c 34 KB

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