dm.c 37 KB

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