dm.c 35 KB

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