dm.c 35 KB

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