dm.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  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 <trace/events/block.h>
  21. #define DM_MSG_PREFIX "core"
  22. /*
  23. * Cookies are numeric values sent with CHANGE and REMOVE
  24. * uevents while resuming, removing or renaming the device.
  25. */
  26. #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
  27. #define DM_COOKIE_LENGTH 24
  28. static const char *_name = DM_NAME;
  29. static unsigned int major = 0;
  30. static unsigned int _major = 0;
  31. static DEFINE_SPINLOCK(_minor_lock);
  32. /*
  33. * For bio-based dm.
  34. * One of these is allocated per bio.
  35. */
  36. struct dm_io {
  37. struct mapped_device *md;
  38. int error;
  39. atomic_t io_count;
  40. struct bio *bio;
  41. unsigned long start_time;
  42. };
  43. /*
  44. * For bio-based dm.
  45. * One of these is allocated per target within a bio. Hopefully
  46. * this will be simplified out one day.
  47. */
  48. struct dm_target_io {
  49. struct dm_io *io;
  50. struct dm_target *ti;
  51. union map_info info;
  52. };
  53. /*
  54. * For request-based dm.
  55. * One of these is allocated per request.
  56. */
  57. struct dm_rq_target_io {
  58. struct mapped_device *md;
  59. struct dm_target *ti;
  60. struct request *orig, clone;
  61. int error;
  62. union map_info info;
  63. };
  64. /*
  65. * For request-based dm.
  66. * One of these is allocated per bio.
  67. */
  68. struct dm_rq_clone_bio_info {
  69. struct bio *orig;
  70. struct dm_rq_target_io *tio;
  71. };
  72. union map_info *dm_get_mapinfo(struct bio *bio)
  73. {
  74. if (bio && bio->bi_private)
  75. return &((struct dm_target_io *)bio->bi_private)->info;
  76. return NULL;
  77. }
  78. union map_info *dm_get_rq_mapinfo(struct request *rq)
  79. {
  80. if (rq && rq->end_io_data)
  81. return &((struct dm_rq_target_io *)rq->end_io_data)->info;
  82. return NULL;
  83. }
  84. EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
  85. #define MINOR_ALLOCED ((void *)-1)
  86. /*
  87. * Bits for the md->flags field.
  88. */
  89. #define DMF_BLOCK_IO_FOR_SUSPEND 0
  90. #define DMF_SUSPENDED 1
  91. #define DMF_FROZEN 2
  92. #define DMF_FREEING 3
  93. #define DMF_DELETING 4
  94. #define DMF_NOFLUSH_SUSPENDING 5
  95. #define DMF_QUEUE_IO_TO_THREAD 6
  96. /*
  97. * Work processed by per-device workqueue.
  98. */
  99. struct mapped_device {
  100. struct rw_semaphore io_lock;
  101. struct mutex suspend_lock;
  102. rwlock_t map_lock;
  103. atomic_t holders;
  104. atomic_t open_count;
  105. unsigned long flags;
  106. struct request_queue *queue;
  107. struct gendisk *disk;
  108. char name[16];
  109. void *interface_ptr;
  110. /*
  111. * A list of ios that arrived while we were suspended.
  112. */
  113. atomic_t pending;
  114. wait_queue_head_t wait;
  115. struct work_struct work;
  116. struct bio_list deferred;
  117. spinlock_t deferred_lock;
  118. /*
  119. * An error from the barrier request currently being processed.
  120. */
  121. int barrier_error;
  122. /*
  123. * Processing queue (flush/barriers)
  124. */
  125. struct workqueue_struct *wq;
  126. /*
  127. * The current mapping.
  128. */
  129. struct dm_table *map;
  130. /*
  131. * io objects are allocated from here.
  132. */
  133. mempool_t *io_pool;
  134. mempool_t *tio_pool;
  135. struct bio_set *bs;
  136. /*
  137. * Event handling.
  138. */
  139. atomic_t event_nr;
  140. wait_queue_head_t eventq;
  141. atomic_t uevent_seq;
  142. struct list_head uevent_list;
  143. spinlock_t uevent_lock; /* Protect access to uevent_list */
  144. /*
  145. * freeze/thaw support require holding onto a super block
  146. */
  147. struct super_block *frozen_sb;
  148. struct block_device *bdev;
  149. /* forced geometry settings */
  150. struct hd_geometry geometry;
  151. /* marker of flush suspend for request-based dm */
  152. struct request suspend_rq;
  153. /* For saving the address of __make_request for request based dm */
  154. make_request_fn *saved_make_request_fn;
  155. /* sysfs handle */
  156. struct kobject kobj;
  157. /* zero-length barrier that will be cloned and submitted to targets */
  158. struct bio barrier_bio;
  159. };
  160. /*
  161. * For mempools pre-allocation at the table loading time.
  162. */
  163. struct dm_md_mempools {
  164. mempool_t *io_pool;
  165. mempool_t *tio_pool;
  166. struct bio_set *bs;
  167. };
  168. #define MIN_IOS 256
  169. static struct kmem_cache *_io_cache;
  170. static struct kmem_cache *_tio_cache;
  171. static struct kmem_cache *_rq_tio_cache;
  172. static struct kmem_cache *_rq_bio_info_cache;
  173. static int __init local_init(void)
  174. {
  175. int r = -ENOMEM;
  176. /* allocate a slab for the dm_ios */
  177. _io_cache = KMEM_CACHE(dm_io, 0);
  178. if (!_io_cache)
  179. return r;
  180. /* allocate a slab for the target ios */
  181. _tio_cache = KMEM_CACHE(dm_target_io, 0);
  182. if (!_tio_cache)
  183. goto out_free_io_cache;
  184. _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
  185. if (!_rq_tio_cache)
  186. goto out_free_tio_cache;
  187. _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
  188. if (!_rq_bio_info_cache)
  189. goto out_free_rq_tio_cache;
  190. r = dm_uevent_init();
  191. if (r)
  192. goto out_free_rq_bio_info_cache;
  193. _major = major;
  194. r = register_blkdev(_major, _name);
  195. if (r < 0)
  196. goto out_uevent_exit;
  197. if (!_major)
  198. _major = r;
  199. return 0;
  200. out_uevent_exit:
  201. dm_uevent_exit();
  202. out_free_rq_bio_info_cache:
  203. kmem_cache_destroy(_rq_bio_info_cache);
  204. out_free_rq_tio_cache:
  205. kmem_cache_destroy(_rq_tio_cache);
  206. out_free_tio_cache:
  207. kmem_cache_destroy(_tio_cache);
  208. out_free_io_cache:
  209. kmem_cache_destroy(_io_cache);
  210. return r;
  211. }
  212. static void local_exit(void)
  213. {
  214. kmem_cache_destroy(_rq_bio_info_cache);
  215. kmem_cache_destroy(_rq_tio_cache);
  216. kmem_cache_destroy(_tio_cache);
  217. kmem_cache_destroy(_io_cache);
  218. unregister_blkdev(_major, _name);
  219. dm_uevent_exit();
  220. _major = 0;
  221. DMINFO("cleaned up");
  222. }
  223. static int (*_inits[])(void) __initdata = {
  224. local_init,
  225. dm_target_init,
  226. dm_linear_init,
  227. dm_stripe_init,
  228. dm_kcopyd_init,
  229. dm_interface_init,
  230. };
  231. static void (*_exits[])(void) = {
  232. local_exit,
  233. dm_target_exit,
  234. dm_linear_exit,
  235. dm_stripe_exit,
  236. dm_kcopyd_exit,
  237. dm_interface_exit,
  238. };
  239. static int __init dm_init(void)
  240. {
  241. const int count = ARRAY_SIZE(_inits);
  242. int r, i;
  243. for (i = 0; i < count; i++) {
  244. r = _inits[i]();
  245. if (r)
  246. goto bad;
  247. }
  248. return 0;
  249. bad:
  250. while (i--)
  251. _exits[i]();
  252. return r;
  253. }
  254. static void __exit dm_exit(void)
  255. {
  256. int i = ARRAY_SIZE(_exits);
  257. while (i--)
  258. _exits[i]();
  259. }
  260. /*
  261. * Block device functions
  262. */
  263. static int dm_blk_open(struct block_device *bdev, fmode_t mode)
  264. {
  265. struct mapped_device *md;
  266. spin_lock(&_minor_lock);
  267. md = bdev->bd_disk->private_data;
  268. if (!md)
  269. goto out;
  270. if (test_bit(DMF_FREEING, &md->flags) ||
  271. test_bit(DMF_DELETING, &md->flags)) {
  272. md = NULL;
  273. goto out;
  274. }
  275. dm_get(md);
  276. atomic_inc(&md->open_count);
  277. out:
  278. spin_unlock(&_minor_lock);
  279. return md ? 0 : -ENXIO;
  280. }
  281. static int dm_blk_close(struct gendisk *disk, fmode_t mode)
  282. {
  283. struct mapped_device *md = disk->private_data;
  284. atomic_dec(&md->open_count);
  285. dm_put(md);
  286. return 0;
  287. }
  288. int dm_open_count(struct mapped_device *md)
  289. {
  290. return atomic_read(&md->open_count);
  291. }
  292. /*
  293. * Guarantees nothing is using the device before it's deleted.
  294. */
  295. int dm_lock_for_deletion(struct mapped_device *md)
  296. {
  297. int r = 0;
  298. spin_lock(&_minor_lock);
  299. if (dm_open_count(md))
  300. r = -EBUSY;
  301. else
  302. set_bit(DMF_DELETING, &md->flags);
  303. spin_unlock(&_minor_lock);
  304. return r;
  305. }
  306. static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  307. {
  308. struct mapped_device *md = bdev->bd_disk->private_data;
  309. return dm_get_geometry(md, geo);
  310. }
  311. static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
  312. unsigned int cmd, unsigned long arg)
  313. {
  314. struct mapped_device *md = bdev->bd_disk->private_data;
  315. struct dm_table *map = dm_get_table(md);
  316. struct dm_target *tgt;
  317. int r = -ENOTTY;
  318. if (!map || !dm_table_get_size(map))
  319. goto out;
  320. /* We only support devices that have a single target */
  321. if (dm_table_get_num_targets(map) != 1)
  322. goto out;
  323. tgt = dm_table_get_target(map, 0);
  324. if (dm_suspended(md)) {
  325. r = -EAGAIN;
  326. goto out;
  327. }
  328. if (tgt->type->ioctl)
  329. r = tgt->type->ioctl(tgt, cmd, arg);
  330. out:
  331. dm_table_put(map);
  332. return r;
  333. }
  334. static struct dm_io *alloc_io(struct mapped_device *md)
  335. {
  336. return mempool_alloc(md->io_pool, GFP_NOIO);
  337. }
  338. static void free_io(struct mapped_device *md, struct dm_io *io)
  339. {
  340. mempool_free(io, md->io_pool);
  341. }
  342. static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
  343. {
  344. mempool_free(tio, md->tio_pool);
  345. }
  346. static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md)
  347. {
  348. return mempool_alloc(md->tio_pool, GFP_ATOMIC);
  349. }
  350. static void free_rq_tio(struct dm_rq_target_io *tio)
  351. {
  352. mempool_free(tio, tio->md->tio_pool);
  353. }
  354. static struct dm_rq_clone_bio_info *alloc_bio_info(struct mapped_device *md)
  355. {
  356. return mempool_alloc(md->io_pool, GFP_ATOMIC);
  357. }
  358. static void free_bio_info(struct dm_rq_clone_bio_info *info)
  359. {
  360. mempool_free(info, info->tio->md->io_pool);
  361. }
  362. static void start_io_acct(struct dm_io *io)
  363. {
  364. struct mapped_device *md = io->md;
  365. int cpu;
  366. io->start_time = jiffies;
  367. cpu = part_stat_lock();
  368. part_round_stats(cpu, &dm_disk(md)->part0);
  369. part_stat_unlock();
  370. dm_disk(md)->part0.in_flight = atomic_inc_return(&md->pending);
  371. }
  372. static void end_io_acct(struct dm_io *io)
  373. {
  374. struct mapped_device *md = io->md;
  375. struct bio *bio = io->bio;
  376. unsigned long duration = jiffies - io->start_time;
  377. int pending, cpu;
  378. int rw = bio_data_dir(bio);
  379. cpu = part_stat_lock();
  380. part_round_stats(cpu, &dm_disk(md)->part0);
  381. part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
  382. part_stat_unlock();
  383. /*
  384. * After this is decremented the bio must not be touched if it is
  385. * a barrier.
  386. */
  387. dm_disk(md)->part0.in_flight = pending =
  388. atomic_dec_return(&md->pending);
  389. /* nudge anyone waiting on suspend queue */
  390. if (!pending)
  391. wake_up(&md->wait);
  392. }
  393. /*
  394. * Add the bio to the list of deferred io.
  395. */
  396. static void queue_io(struct mapped_device *md, struct bio *bio)
  397. {
  398. down_write(&md->io_lock);
  399. spin_lock_irq(&md->deferred_lock);
  400. bio_list_add(&md->deferred, bio);
  401. spin_unlock_irq(&md->deferred_lock);
  402. if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
  403. queue_work(md->wq, &md->work);
  404. up_write(&md->io_lock);
  405. }
  406. /*
  407. * Everyone (including functions in this file), should use this
  408. * function to access the md->map field, and make sure they call
  409. * dm_table_put() when finished.
  410. */
  411. struct dm_table *dm_get_table(struct mapped_device *md)
  412. {
  413. struct dm_table *t;
  414. read_lock(&md->map_lock);
  415. t = md->map;
  416. if (t)
  417. dm_table_get(t);
  418. read_unlock(&md->map_lock);
  419. return t;
  420. }
  421. /*
  422. * Get the geometry associated with a dm device
  423. */
  424. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
  425. {
  426. *geo = md->geometry;
  427. return 0;
  428. }
  429. /*
  430. * Set the geometry of a device.
  431. */
  432. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
  433. {
  434. sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
  435. if (geo->start > sz) {
  436. DMWARN("Start sector is beyond the geometry limits.");
  437. return -EINVAL;
  438. }
  439. md->geometry = *geo;
  440. return 0;
  441. }
  442. /*-----------------------------------------------------------------
  443. * CRUD START:
  444. * A more elegant soln is in the works that uses the queue
  445. * merge fn, unfortunately there are a couple of changes to
  446. * the block layer that I want to make for this. So in the
  447. * interests of getting something for people to use I give
  448. * you this clearly demarcated crap.
  449. *---------------------------------------------------------------*/
  450. static int __noflush_suspending(struct mapped_device *md)
  451. {
  452. return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  453. }
  454. /*
  455. * Decrements the number of outstanding ios that a bio has been
  456. * cloned into, completing the original io if necc.
  457. */
  458. static void dec_pending(struct dm_io *io, int error)
  459. {
  460. unsigned long flags;
  461. int io_error;
  462. struct bio *bio;
  463. struct mapped_device *md = io->md;
  464. /* Push-back supersedes any I/O errors */
  465. if (error && !(io->error > 0 && __noflush_suspending(md)))
  466. io->error = error;
  467. if (atomic_dec_and_test(&io->io_count)) {
  468. if (io->error == DM_ENDIO_REQUEUE) {
  469. /*
  470. * Target requested pushing back the I/O.
  471. */
  472. spin_lock_irqsave(&md->deferred_lock, flags);
  473. if (__noflush_suspending(md)) {
  474. if (!bio_barrier(io->bio))
  475. bio_list_add_head(&md->deferred,
  476. io->bio);
  477. } else
  478. /* noflush suspend was interrupted. */
  479. io->error = -EIO;
  480. spin_unlock_irqrestore(&md->deferred_lock, flags);
  481. }
  482. io_error = io->error;
  483. bio = io->bio;
  484. if (bio_barrier(bio)) {
  485. /*
  486. * There can be just one barrier request so we use
  487. * a per-device variable for error reporting.
  488. * Note that you can't touch the bio after end_io_acct
  489. */
  490. if (!md->barrier_error && io_error != -EOPNOTSUPP)
  491. md->barrier_error = io_error;
  492. end_io_acct(io);
  493. } else {
  494. end_io_acct(io);
  495. if (io_error != DM_ENDIO_REQUEUE) {
  496. trace_block_bio_complete(md->queue, bio);
  497. bio_endio(bio, io_error);
  498. }
  499. }
  500. free_io(md, io);
  501. }
  502. }
  503. static void clone_endio(struct bio *bio, int error)
  504. {
  505. int r = 0;
  506. struct dm_target_io *tio = bio->bi_private;
  507. struct dm_io *io = tio->io;
  508. struct mapped_device *md = tio->io->md;
  509. dm_endio_fn endio = tio->ti->type->end_io;
  510. if (!bio_flagged(bio, BIO_UPTODATE) && !error)
  511. error = -EIO;
  512. if (endio) {
  513. r = endio(tio->ti, bio, error, &tio->info);
  514. if (r < 0 || r == DM_ENDIO_REQUEUE)
  515. /*
  516. * error and requeue request are handled
  517. * in dec_pending().
  518. */
  519. error = r;
  520. else if (r == DM_ENDIO_INCOMPLETE)
  521. /* The target will handle the io */
  522. return;
  523. else if (r) {
  524. DMWARN("unimplemented target endio return value: %d", r);
  525. BUG();
  526. }
  527. }
  528. /*
  529. * Store md for cleanup instead of tio which is about to get freed.
  530. */
  531. bio->bi_private = md->bs;
  532. free_tio(md, tio);
  533. bio_put(bio);
  534. dec_pending(io, error);
  535. }
  536. /*
  537. * Partial completion handling for request-based dm
  538. */
  539. static void end_clone_bio(struct bio *clone, int error)
  540. {
  541. struct dm_rq_clone_bio_info *info = clone->bi_private;
  542. struct dm_rq_target_io *tio = info->tio;
  543. struct bio *bio = info->orig;
  544. unsigned int nr_bytes = info->orig->bi_size;
  545. bio_put(clone);
  546. if (tio->error)
  547. /*
  548. * An error has already been detected on the request.
  549. * Once error occurred, just let clone->end_io() handle
  550. * the remainder.
  551. */
  552. return;
  553. else if (error) {
  554. /*
  555. * Don't notice the error to the upper layer yet.
  556. * The error handling decision is made by the target driver,
  557. * when the request is completed.
  558. */
  559. tio->error = error;
  560. return;
  561. }
  562. /*
  563. * I/O for the bio successfully completed.
  564. * Notice the data completion to the upper layer.
  565. */
  566. /*
  567. * bios are processed from the head of the list.
  568. * So the completing bio should always be rq->bio.
  569. * If it's not, something wrong is happening.
  570. */
  571. if (tio->orig->bio != bio)
  572. DMERR("bio completion is going in the middle of the request");
  573. /*
  574. * Update the original request.
  575. * Do not use blk_end_request() here, because it may complete
  576. * the original request before the clone, and break the ordering.
  577. */
  578. blk_update_request(tio->orig, 0, nr_bytes);
  579. }
  580. /*
  581. * Don't touch any member of the md after calling this function because
  582. * the md may be freed in dm_put() at the end of this function.
  583. * Or do dm_get() before calling this function and dm_put() later.
  584. */
  585. static void rq_completed(struct mapped_device *md, int run_queue)
  586. {
  587. int wakeup_waiters = 0;
  588. struct request_queue *q = md->queue;
  589. unsigned long flags;
  590. spin_lock_irqsave(q->queue_lock, flags);
  591. if (!queue_in_flight(q))
  592. wakeup_waiters = 1;
  593. spin_unlock_irqrestore(q->queue_lock, flags);
  594. /* nudge anyone waiting on suspend queue */
  595. if (wakeup_waiters)
  596. wake_up(&md->wait);
  597. if (run_queue)
  598. blk_run_queue(q);
  599. /*
  600. * dm_put() must be at the end of this function. See the comment above
  601. */
  602. dm_put(md);
  603. }
  604. static void dm_unprep_request(struct request *rq)
  605. {
  606. struct request *clone = rq->special;
  607. struct dm_rq_target_io *tio = clone->end_io_data;
  608. rq->special = NULL;
  609. rq->cmd_flags &= ~REQ_DONTPREP;
  610. blk_rq_unprep_clone(clone);
  611. free_rq_tio(tio);
  612. }
  613. /*
  614. * Requeue the original request of a clone.
  615. */
  616. void dm_requeue_unmapped_request(struct request *clone)
  617. {
  618. struct dm_rq_target_io *tio = clone->end_io_data;
  619. struct mapped_device *md = tio->md;
  620. struct request *rq = tio->orig;
  621. struct request_queue *q = rq->q;
  622. unsigned long flags;
  623. dm_unprep_request(rq);
  624. spin_lock_irqsave(q->queue_lock, flags);
  625. if (elv_queue_empty(q))
  626. blk_plug_device(q);
  627. blk_requeue_request(q, rq);
  628. spin_unlock_irqrestore(q->queue_lock, flags);
  629. rq_completed(md, 0);
  630. }
  631. EXPORT_SYMBOL_GPL(dm_requeue_unmapped_request);
  632. static void __stop_queue(struct request_queue *q)
  633. {
  634. blk_stop_queue(q);
  635. }
  636. static void stop_queue(struct request_queue *q)
  637. {
  638. unsigned long flags;
  639. spin_lock_irqsave(q->queue_lock, flags);
  640. __stop_queue(q);
  641. spin_unlock_irqrestore(q->queue_lock, flags);
  642. }
  643. static void __start_queue(struct request_queue *q)
  644. {
  645. if (blk_queue_stopped(q))
  646. blk_start_queue(q);
  647. }
  648. static void start_queue(struct request_queue *q)
  649. {
  650. unsigned long flags;
  651. spin_lock_irqsave(q->queue_lock, flags);
  652. __start_queue(q);
  653. spin_unlock_irqrestore(q->queue_lock, flags);
  654. }
  655. /*
  656. * Complete the clone and the original request.
  657. * Must be called without queue lock.
  658. */
  659. static void dm_end_request(struct request *clone, int error)
  660. {
  661. struct dm_rq_target_io *tio = clone->end_io_data;
  662. struct mapped_device *md = tio->md;
  663. struct request *rq = tio->orig;
  664. if (blk_pc_request(rq)) {
  665. rq->errors = clone->errors;
  666. rq->resid_len = clone->resid_len;
  667. if (rq->sense)
  668. /*
  669. * We are using the sense buffer of the original
  670. * request.
  671. * So setting the length of the sense data is enough.
  672. */
  673. rq->sense_len = clone->sense_len;
  674. }
  675. BUG_ON(clone->bio);
  676. free_rq_tio(tio);
  677. blk_end_request_all(rq, error);
  678. rq_completed(md, 1);
  679. }
  680. /*
  681. * Request completion handler for request-based dm
  682. */
  683. static void dm_softirq_done(struct request *rq)
  684. {
  685. struct request *clone = rq->completion_data;
  686. struct dm_rq_target_io *tio = clone->end_io_data;
  687. dm_request_endio_fn rq_end_io = tio->ti->type->rq_end_io;
  688. int error = tio->error;
  689. if (!(rq->cmd_flags & REQ_FAILED) && rq_end_io)
  690. error = rq_end_io(tio->ti, clone, error, &tio->info);
  691. if (error <= 0)
  692. /* The target wants to complete the I/O */
  693. dm_end_request(clone, error);
  694. else if (error == DM_ENDIO_INCOMPLETE)
  695. /* The target will handle the I/O */
  696. return;
  697. else if (error == DM_ENDIO_REQUEUE)
  698. /* The target wants to requeue the I/O */
  699. dm_requeue_unmapped_request(clone);
  700. else {
  701. DMWARN("unimplemented target endio return value: %d", error);
  702. BUG();
  703. }
  704. }
  705. /*
  706. * Complete the clone and the original request with the error status
  707. * through softirq context.
  708. */
  709. static void dm_complete_request(struct request *clone, int error)
  710. {
  711. struct dm_rq_target_io *tio = clone->end_io_data;
  712. struct request *rq = tio->orig;
  713. tio->error = error;
  714. rq->completion_data = clone;
  715. blk_complete_request(rq);
  716. }
  717. /*
  718. * Complete the not-mapped clone and the original request with the error status
  719. * through softirq context.
  720. * Target's rq_end_io() function isn't called.
  721. * This may be used when the target's map_rq() function fails.
  722. */
  723. void dm_kill_unmapped_request(struct request *clone, int error)
  724. {
  725. struct dm_rq_target_io *tio = clone->end_io_data;
  726. struct request *rq = tio->orig;
  727. rq->cmd_flags |= REQ_FAILED;
  728. dm_complete_request(clone, error);
  729. }
  730. EXPORT_SYMBOL_GPL(dm_kill_unmapped_request);
  731. /*
  732. * Called with the queue lock held
  733. */
  734. static void end_clone_request(struct request *clone, int error)
  735. {
  736. /*
  737. * For just cleaning up the information of the queue in which
  738. * the clone was dispatched.
  739. * The clone is *NOT* freed actually here because it is alloced from
  740. * dm own mempool and REQ_ALLOCED isn't set in clone->cmd_flags.
  741. */
  742. __blk_put_request(clone->q, clone);
  743. /*
  744. * Actual request completion is done in a softirq context which doesn't
  745. * hold the queue lock. Otherwise, deadlock could occur because:
  746. * - another request may be submitted by the upper level driver
  747. * of the stacking during the completion
  748. * - the submission which requires queue lock may be done
  749. * against this queue
  750. */
  751. dm_complete_request(clone, error);
  752. }
  753. static sector_t max_io_len(struct mapped_device *md,
  754. sector_t sector, struct dm_target *ti)
  755. {
  756. sector_t offset = sector - ti->begin;
  757. sector_t len = ti->len - offset;
  758. /*
  759. * Does the target need to split even further ?
  760. */
  761. if (ti->split_io) {
  762. sector_t boundary;
  763. boundary = ((offset + ti->split_io) & ~(ti->split_io - 1))
  764. - offset;
  765. if (len > boundary)
  766. len = boundary;
  767. }
  768. return len;
  769. }
  770. static void __map_bio(struct dm_target *ti, struct bio *clone,
  771. struct dm_target_io *tio)
  772. {
  773. int r;
  774. sector_t sector;
  775. struct mapped_device *md;
  776. clone->bi_end_io = clone_endio;
  777. clone->bi_private = tio;
  778. /*
  779. * Map the clone. If r == 0 we don't need to do
  780. * anything, the target has assumed ownership of
  781. * this io.
  782. */
  783. atomic_inc(&tio->io->io_count);
  784. sector = clone->bi_sector;
  785. r = ti->type->map(ti, clone, &tio->info);
  786. if (r == DM_MAPIO_REMAPPED) {
  787. /* the bio has been remapped so dispatch it */
  788. trace_block_remap(bdev_get_queue(clone->bi_bdev), clone,
  789. tio->io->bio->bi_bdev->bd_dev, sector);
  790. generic_make_request(clone);
  791. } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
  792. /* error the io and bail out, or requeue it if needed */
  793. md = tio->io->md;
  794. dec_pending(tio->io, r);
  795. /*
  796. * Store bio_set for cleanup.
  797. */
  798. clone->bi_private = md->bs;
  799. bio_put(clone);
  800. free_tio(md, tio);
  801. } else if (r) {
  802. DMWARN("unimplemented target map return value: %d", r);
  803. BUG();
  804. }
  805. }
  806. struct clone_info {
  807. struct mapped_device *md;
  808. struct dm_table *map;
  809. struct bio *bio;
  810. struct dm_io *io;
  811. sector_t sector;
  812. sector_t sector_count;
  813. unsigned short idx;
  814. };
  815. static void dm_bio_destructor(struct bio *bio)
  816. {
  817. struct bio_set *bs = bio->bi_private;
  818. bio_free(bio, bs);
  819. }
  820. /*
  821. * Creates a little bio that is just does part of a bvec.
  822. */
  823. static struct bio *split_bvec(struct bio *bio, sector_t sector,
  824. unsigned short idx, unsigned int offset,
  825. unsigned int len, struct bio_set *bs)
  826. {
  827. struct bio *clone;
  828. struct bio_vec *bv = bio->bi_io_vec + idx;
  829. clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
  830. clone->bi_destructor = dm_bio_destructor;
  831. *clone->bi_io_vec = *bv;
  832. clone->bi_sector = sector;
  833. clone->bi_bdev = bio->bi_bdev;
  834. clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
  835. clone->bi_vcnt = 1;
  836. clone->bi_size = to_bytes(len);
  837. clone->bi_io_vec->bv_offset = offset;
  838. clone->bi_io_vec->bv_len = clone->bi_size;
  839. clone->bi_flags |= 1 << BIO_CLONED;
  840. if (bio_integrity(bio)) {
  841. bio_integrity_clone(clone, bio, GFP_NOIO);
  842. bio_integrity_trim(clone,
  843. bio_sector_offset(bio, idx, offset), len);
  844. }
  845. return clone;
  846. }
  847. /*
  848. * Creates a bio that consists of range of complete bvecs.
  849. */
  850. static struct bio *clone_bio(struct bio *bio, sector_t sector,
  851. unsigned short idx, unsigned short bv_count,
  852. unsigned int len, struct bio_set *bs)
  853. {
  854. struct bio *clone;
  855. clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
  856. __bio_clone(clone, bio);
  857. clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
  858. clone->bi_destructor = dm_bio_destructor;
  859. clone->bi_sector = sector;
  860. clone->bi_idx = idx;
  861. clone->bi_vcnt = idx + bv_count;
  862. clone->bi_size = to_bytes(len);
  863. clone->bi_flags &= ~(1 << BIO_SEG_VALID);
  864. if (bio_integrity(bio)) {
  865. bio_integrity_clone(clone, bio, GFP_NOIO);
  866. if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
  867. bio_integrity_trim(clone,
  868. bio_sector_offset(bio, idx, 0), len);
  869. }
  870. return clone;
  871. }
  872. static struct dm_target_io *alloc_tio(struct clone_info *ci,
  873. struct dm_target *ti)
  874. {
  875. struct dm_target_io *tio = mempool_alloc(ci->md->tio_pool, GFP_NOIO);
  876. tio->io = ci->io;
  877. tio->ti = ti;
  878. memset(&tio->info, 0, sizeof(tio->info));
  879. return tio;
  880. }
  881. static void __flush_target(struct clone_info *ci, struct dm_target *ti,
  882. unsigned flush_nr)
  883. {
  884. struct dm_target_io *tio = alloc_tio(ci, ti);
  885. struct bio *clone;
  886. tio->info.flush_request = flush_nr;
  887. clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
  888. __bio_clone(clone, ci->bio);
  889. clone->bi_destructor = dm_bio_destructor;
  890. __map_bio(ti, clone, tio);
  891. }
  892. static int __clone_and_map_empty_barrier(struct clone_info *ci)
  893. {
  894. unsigned target_nr = 0, flush_nr;
  895. struct dm_target *ti;
  896. while ((ti = dm_table_get_target(ci->map, target_nr++)))
  897. for (flush_nr = 0; flush_nr < ti->num_flush_requests;
  898. flush_nr++)
  899. __flush_target(ci, ti, flush_nr);
  900. ci->sector_count = 0;
  901. return 0;
  902. }
  903. static int __clone_and_map(struct clone_info *ci)
  904. {
  905. struct bio *clone, *bio = ci->bio;
  906. struct dm_target *ti;
  907. sector_t len = 0, max;
  908. struct dm_target_io *tio;
  909. if (unlikely(bio_empty_barrier(bio)))
  910. return __clone_and_map_empty_barrier(ci);
  911. ti = dm_table_find_target(ci->map, ci->sector);
  912. if (!dm_target_is_valid(ti))
  913. return -EIO;
  914. max = max_io_len(ci->md, ci->sector, ti);
  915. /*
  916. * Allocate a target io object.
  917. */
  918. tio = alloc_tio(ci, ti);
  919. if (ci->sector_count <= max) {
  920. /*
  921. * Optimise for the simple case where we can do all of
  922. * the remaining io with a single clone.
  923. */
  924. clone = clone_bio(bio, ci->sector, ci->idx,
  925. bio->bi_vcnt - ci->idx, ci->sector_count,
  926. ci->md->bs);
  927. __map_bio(ti, clone, tio);
  928. ci->sector_count = 0;
  929. } else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
  930. /*
  931. * There are some bvecs that don't span targets.
  932. * Do as many of these as possible.
  933. */
  934. int i;
  935. sector_t remaining = max;
  936. sector_t bv_len;
  937. for (i = ci->idx; remaining && (i < bio->bi_vcnt); i++) {
  938. bv_len = to_sector(bio->bi_io_vec[i].bv_len);
  939. if (bv_len > remaining)
  940. break;
  941. remaining -= bv_len;
  942. len += bv_len;
  943. }
  944. clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
  945. ci->md->bs);
  946. __map_bio(ti, clone, tio);
  947. ci->sector += len;
  948. ci->sector_count -= len;
  949. ci->idx = i;
  950. } else {
  951. /*
  952. * Handle a bvec that must be split between two or more targets.
  953. */
  954. struct bio_vec *bv = bio->bi_io_vec + ci->idx;
  955. sector_t remaining = to_sector(bv->bv_len);
  956. unsigned int offset = 0;
  957. do {
  958. if (offset) {
  959. ti = dm_table_find_target(ci->map, ci->sector);
  960. if (!dm_target_is_valid(ti))
  961. return -EIO;
  962. max = max_io_len(ci->md, ci->sector, ti);
  963. tio = alloc_tio(ci, ti);
  964. }
  965. len = min(remaining, max);
  966. clone = split_bvec(bio, ci->sector, ci->idx,
  967. bv->bv_offset + offset, len,
  968. ci->md->bs);
  969. __map_bio(ti, clone, tio);
  970. ci->sector += len;
  971. ci->sector_count -= len;
  972. offset += to_bytes(len);
  973. } while (remaining -= len);
  974. ci->idx++;
  975. }
  976. return 0;
  977. }
  978. /*
  979. * Split the bio into several clones and submit it to targets.
  980. */
  981. static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
  982. {
  983. struct clone_info ci;
  984. int error = 0;
  985. ci.map = dm_get_table(md);
  986. if (unlikely(!ci.map)) {
  987. if (!bio_barrier(bio))
  988. bio_io_error(bio);
  989. else
  990. if (!md->barrier_error)
  991. md->barrier_error = -EIO;
  992. return;
  993. }
  994. ci.md = md;
  995. ci.bio = bio;
  996. ci.io = alloc_io(md);
  997. ci.io->error = 0;
  998. atomic_set(&ci.io->io_count, 1);
  999. ci.io->bio = bio;
  1000. ci.io->md = md;
  1001. ci.sector = bio->bi_sector;
  1002. ci.sector_count = bio_sectors(bio);
  1003. if (unlikely(bio_empty_barrier(bio)))
  1004. ci.sector_count = 1;
  1005. ci.idx = bio->bi_idx;
  1006. start_io_acct(ci.io);
  1007. while (ci.sector_count && !error)
  1008. error = __clone_and_map(&ci);
  1009. /* drop the extra reference count */
  1010. dec_pending(ci.io, error);
  1011. dm_table_put(ci.map);
  1012. }
  1013. /*-----------------------------------------------------------------
  1014. * CRUD END
  1015. *---------------------------------------------------------------*/
  1016. static int dm_merge_bvec(struct request_queue *q,
  1017. struct bvec_merge_data *bvm,
  1018. struct bio_vec *biovec)
  1019. {
  1020. struct mapped_device *md = q->queuedata;
  1021. struct dm_table *map = dm_get_table(md);
  1022. struct dm_target *ti;
  1023. sector_t max_sectors;
  1024. int max_size = 0;
  1025. if (unlikely(!map))
  1026. goto out;
  1027. ti = dm_table_find_target(map, bvm->bi_sector);
  1028. if (!dm_target_is_valid(ti))
  1029. goto out_table;
  1030. /*
  1031. * Find maximum amount of I/O that won't need splitting
  1032. */
  1033. max_sectors = min(max_io_len(md, bvm->bi_sector, ti),
  1034. (sector_t) BIO_MAX_SECTORS);
  1035. max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
  1036. if (max_size < 0)
  1037. max_size = 0;
  1038. /*
  1039. * merge_bvec_fn() returns number of bytes
  1040. * it can accept at this offset
  1041. * max is precomputed maximal io size
  1042. */
  1043. if (max_size && ti->type->merge)
  1044. max_size = ti->type->merge(ti, bvm, biovec, max_size);
  1045. /*
  1046. * If the target doesn't support merge method and some of the devices
  1047. * provided their merge_bvec method (we know this by looking at
  1048. * queue_max_hw_sectors), then we can't allow bios with multiple vector
  1049. * entries. So always set max_size to 0, and the code below allows
  1050. * just one page.
  1051. */
  1052. else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
  1053. max_size = 0;
  1054. out_table:
  1055. dm_table_put(map);
  1056. out:
  1057. /*
  1058. * Always allow an entire first page
  1059. */
  1060. if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
  1061. max_size = biovec->bv_len;
  1062. return max_size;
  1063. }
  1064. /*
  1065. * The request function that just remaps the bio built up by
  1066. * dm_merge_bvec.
  1067. */
  1068. static int _dm_request(struct request_queue *q, struct bio *bio)
  1069. {
  1070. int rw = bio_data_dir(bio);
  1071. struct mapped_device *md = q->queuedata;
  1072. int cpu;
  1073. down_read(&md->io_lock);
  1074. cpu = part_stat_lock();
  1075. part_stat_inc(cpu, &dm_disk(md)->part0, ios[rw]);
  1076. part_stat_add(cpu, &dm_disk(md)->part0, sectors[rw], bio_sectors(bio));
  1077. part_stat_unlock();
  1078. /*
  1079. * If we're suspended or the thread is processing barriers
  1080. * we have to queue this io for later.
  1081. */
  1082. if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
  1083. unlikely(bio_barrier(bio))) {
  1084. up_read(&md->io_lock);
  1085. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
  1086. bio_rw(bio) == READA) {
  1087. bio_io_error(bio);
  1088. return 0;
  1089. }
  1090. queue_io(md, bio);
  1091. return 0;
  1092. }
  1093. __split_and_process_bio(md, bio);
  1094. up_read(&md->io_lock);
  1095. return 0;
  1096. }
  1097. static int dm_make_request(struct request_queue *q, struct bio *bio)
  1098. {
  1099. struct mapped_device *md = q->queuedata;
  1100. if (unlikely(bio_barrier(bio))) {
  1101. bio_endio(bio, -EOPNOTSUPP);
  1102. return 0;
  1103. }
  1104. return md->saved_make_request_fn(q, bio); /* call __make_request() */
  1105. }
  1106. static int dm_request_based(struct mapped_device *md)
  1107. {
  1108. return blk_queue_stackable(md->queue);
  1109. }
  1110. static int dm_request(struct request_queue *q, struct bio *bio)
  1111. {
  1112. struct mapped_device *md = q->queuedata;
  1113. if (dm_request_based(md))
  1114. return dm_make_request(q, bio);
  1115. return _dm_request(q, bio);
  1116. }
  1117. void dm_dispatch_request(struct request *rq)
  1118. {
  1119. int r;
  1120. if (blk_queue_io_stat(rq->q))
  1121. rq->cmd_flags |= REQ_IO_STAT;
  1122. rq->start_time = jiffies;
  1123. r = blk_insert_cloned_request(rq->q, rq);
  1124. if (r)
  1125. dm_complete_request(rq, r);
  1126. }
  1127. EXPORT_SYMBOL_GPL(dm_dispatch_request);
  1128. static void dm_rq_bio_destructor(struct bio *bio)
  1129. {
  1130. struct dm_rq_clone_bio_info *info = bio->bi_private;
  1131. struct mapped_device *md = info->tio->md;
  1132. free_bio_info(info);
  1133. bio_free(bio, md->bs);
  1134. }
  1135. static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
  1136. void *data)
  1137. {
  1138. struct dm_rq_target_io *tio = data;
  1139. struct mapped_device *md = tio->md;
  1140. struct dm_rq_clone_bio_info *info = alloc_bio_info(md);
  1141. if (!info)
  1142. return -ENOMEM;
  1143. info->orig = bio_orig;
  1144. info->tio = tio;
  1145. bio->bi_end_io = end_clone_bio;
  1146. bio->bi_private = info;
  1147. bio->bi_destructor = dm_rq_bio_destructor;
  1148. return 0;
  1149. }
  1150. static int setup_clone(struct request *clone, struct request *rq,
  1151. struct dm_rq_target_io *tio)
  1152. {
  1153. int r = blk_rq_prep_clone(clone, rq, tio->md->bs, GFP_ATOMIC,
  1154. dm_rq_bio_constructor, tio);
  1155. if (r)
  1156. return r;
  1157. clone->cmd = rq->cmd;
  1158. clone->cmd_len = rq->cmd_len;
  1159. clone->sense = rq->sense;
  1160. clone->buffer = rq->buffer;
  1161. clone->end_io = end_clone_request;
  1162. clone->end_io_data = tio;
  1163. return 0;
  1164. }
  1165. static int dm_rq_flush_suspending(struct mapped_device *md)
  1166. {
  1167. return !md->suspend_rq.special;
  1168. }
  1169. /*
  1170. * Called with the queue lock held.
  1171. */
  1172. static int dm_prep_fn(struct request_queue *q, struct request *rq)
  1173. {
  1174. struct mapped_device *md = q->queuedata;
  1175. struct dm_rq_target_io *tio;
  1176. struct request *clone;
  1177. if (unlikely(rq == &md->suspend_rq)) {
  1178. if (dm_rq_flush_suspending(md))
  1179. return BLKPREP_OK;
  1180. else
  1181. /* The flush suspend was interrupted */
  1182. return BLKPREP_KILL;
  1183. }
  1184. if (unlikely(rq->special)) {
  1185. DMWARN("Already has something in rq->special.");
  1186. return BLKPREP_KILL;
  1187. }
  1188. tio = alloc_rq_tio(md); /* Only one for each original request */
  1189. if (!tio)
  1190. /* -ENOMEM */
  1191. return BLKPREP_DEFER;
  1192. tio->md = md;
  1193. tio->ti = NULL;
  1194. tio->orig = rq;
  1195. tio->error = 0;
  1196. memset(&tio->info, 0, sizeof(tio->info));
  1197. clone = &tio->clone;
  1198. if (setup_clone(clone, rq, tio)) {
  1199. /* -ENOMEM */
  1200. free_rq_tio(tio);
  1201. return BLKPREP_DEFER;
  1202. }
  1203. rq->special = clone;
  1204. rq->cmd_flags |= REQ_DONTPREP;
  1205. return BLKPREP_OK;
  1206. }
  1207. static void map_request(struct dm_target *ti, struct request *rq,
  1208. struct mapped_device *md)
  1209. {
  1210. int r;
  1211. struct request *clone = rq->special;
  1212. struct dm_rq_target_io *tio = clone->end_io_data;
  1213. /*
  1214. * Hold the md reference here for the in-flight I/O.
  1215. * We can't rely on the reference count by device opener,
  1216. * because the device may be closed during the request completion
  1217. * when all bios are completed.
  1218. * See the comment in rq_completed() too.
  1219. */
  1220. dm_get(md);
  1221. tio->ti = ti;
  1222. r = ti->type->map_rq(ti, clone, &tio->info);
  1223. switch (r) {
  1224. case DM_MAPIO_SUBMITTED:
  1225. /* The target has taken the I/O to submit by itself later */
  1226. break;
  1227. case DM_MAPIO_REMAPPED:
  1228. /* The target has remapped the I/O so dispatch it */
  1229. dm_dispatch_request(clone);
  1230. break;
  1231. case DM_MAPIO_REQUEUE:
  1232. /* The target wants to requeue the I/O */
  1233. dm_requeue_unmapped_request(clone);
  1234. break;
  1235. default:
  1236. if (r > 0) {
  1237. DMWARN("unimplemented target map return value: %d", r);
  1238. BUG();
  1239. }
  1240. /* The target wants to complete the I/O */
  1241. dm_kill_unmapped_request(clone, r);
  1242. break;
  1243. }
  1244. }
  1245. /*
  1246. * q->request_fn for request-based dm.
  1247. * Called with the queue lock held.
  1248. */
  1249. static void dm_request_fn(struct request_queue *q)
  1250. {
  1251. struct mapped_device *md = q->queuedata;
  1252. struct dm_table *map = dm_get_table(md);
  1253. struct dm_target *ti;
  1254. struct request *rq;
  1255. /*
  1256. * For noflush suspend, check blk_queue_stopped() to immediately
  1257. * quit I/O dispatching.
  1258. */
  1259. while (!blk_queue_plugged(q) && !blk_queue_stopped(q)) {
  1260. rq = blk_peek_request(q);
  1261. if (!rq)
  1262. goto plug_and_out;
  1263. if (unlikely(rq == &md->suspend_rq)) { /* Flush suspend maker */
  1264. if (queue_in_flight(q))
  1265. /* Not quiet yet. Wait more */
  1266. goto plug_and_out;
  1267. /* This device should be quiet now */
  1268. __stop_queue(q);
  1269. blk_start_request(rq);
  1270. __blk_end_request_all(rq, 0);
  1271. wake_up(&md->wait);
  1272. goto out;
  1273. }
  1274. ti = dm_table_find_target(map, blk_rq_pos(rq));
  1275. if (ti->type->busy && ti->type->busy(ti))
  1276. goto plug_and_out;
  1277. blk_start_request(rq);
  1278. spin_unlock(q->queue_lock);
  1279. map_request(ti, rq, md);
  1280. spin_lock_irq(q->queue_lock);
  1281. }
  1282. goto out;
  1283. plug_and_out:
  1284. if (!elv_queue_empty(q))
  1285. /* Some requests still remain, retry later */
  1286. blk_plug_device(q);
  1287. out:
  1288. dm_table_put(map);
  1289. return;
  1290. }
  1291. int dm_underlying_device_busy(struct request_queue *q)
  1292. {
  1293. return blk_lld_busy(q);
  1294. }
  1295. EXPORT_SYMBOL_GPL(dm_underlying_device_busy);
  1296. static int dm_lld_busy(struct request_queue *q)
  1297. {
  1298. int r;
  1299. struct mapped_device *md = q->queuedata;
  1300. struct dm_table *map = dm_get_table(md);
  1301. if (!map || test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))
  1302. r = 1;
  1303. else
  1304. r = dm_table_any_busy_target(map);
  1305. dm_table_put(map);
  1306. return r;
  1307. }
  1308. static void dm_unplug_all(struct request_queue *q)
  1309. {
  1310. struct mapped_device *md = q->queuedata;
  1311. struct dm_table *map = dm_get_table(md);
  1312. if (map) {
  1313. if (dm_request_based(md))
  1314. generic_unplug_device(q);
  1315. dm_table_unplug_all(map);
  1316. dm_table_put(map);
  1317. }
  1318. }
  1319. static int dm_any_congested(void *congested_data, int bdi_bits)
  1320. {
  1321. int r = bdi_bits;
  1322. struct mapped_device *md = congested_data;
  1323. struct dm_table *map;
  1324. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1325. map = dm_get_table(md);
  1326. if (map) {
  1327. /*
  1328. * Request-based dm cares about only own queue for
  1329. * the query about congestion status of request_queue
  1330. */
  1331. if (dm_request_based(md))
  1332. r = md->queue->backing_dev_info.state &
  1333. bdi_bits;
  1334. else
  1335. r = dm_table_any_congested(map, bdi_bits);
  1336. dm_table_put(map);
  1337. }
  1338. }
  1339. return r;
  1340. }
  1341. /*-----------------------------------------------------------------
  1342. * An IDR is used to keep track of allocated minor numbers.
  1343. *---------------------------------------------------------------*/
  1344. static DEFINE_IDR(_minor_idr);
  1345. static void free_minor(int minor)
  1346. {
  1347. spin_lock(&_minor_lock);
  1348. idr_remove(&_minor_idr, minor);
  1349. spin_unlock(&_minor_lock);
  1350. }
  1351. /*
  1352. * See if the device with a specific minor # is free.
  1353. */
  1354. static int specific_minor(int minor)
  1355. {
  1356. int r, m;
  1357. if (minor >= (1 << MINORBITS))
  1358. return -EINVAL;
  1359. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  1360. if (!r)
  1361. return -ENOMEM;
  1362. spin_lock(&_minor_lock);
  1363. if (idr_find(&_minor_idr, minor)) {
  1364. r = -EBUSY;
  1365. goto out;
  1366. }
  1367. r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
  1368. if (r)
  1369. goto out;
  1370. if (m != minor) {
  1371. idr_remove(&_minor_idr, m);
  1372. r = -EBUSY;
  1373. goto out;
  1374. }
  1375. out:
  1376. spin_unlock(&_minor_lock);
  1377. return r;
  1378. }
  1379. static int next_free_minor(int *minor)
  1380. {
  1381. int r, m;
  1382. r = idr_pre_get(&_minor_idr, GFP_KERNEL);
  1383. if (!r)
  1384. return -ENOMEM;
  1385. spin_lock(&_minor_lock);
  1386. r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
  1387. if (r)
  1388. goto out;
  1389. if (m >= (1 << MINORBITS)) {
  1390. idr_remove(&_minor_idr, m);
  1391. r = -ENOSPC;
  1392. goto out;
  1393. }
  1394. *minor = m;
  1395. out:
  1396. spin_unlock(&_minor_lock);
  1397. return r;
  1398. }
  1399. static struct block_device_operations dm_blk_dops;
  1400. static void dm_wq_work(struct work_struct *work);
  1401. /*
  1402. * Allocate and initialise a blank device with a given minor.
  1403. */
  1404. static struct mapped_device *alloc_dev(int minor)
  1405. {
  1406. int r;
  1407. struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
  1408. void *old_md;
  1409. if (!md) {
  1410. DMWARN("unable to allocate device, out of memory.");
  1411. return NULL;
  1412. }
  1413. if (!try_module_get(THIS_MODULE))
  1414. goto bad_module_get;
  1415. /* get a minor number for the dev */
  1416. if (minor == DM_ANY_MINOR)
  1417. r = next_free_minor(&minor);
  1418. else
  1419. r = specific_minor(minor);
  1420. if (r < 0)
  1421. goto bad_minor;
  1422. init_rwsem(&md->io_lock);
  1423. mutex_init(&md->suspend_lock);
  1424. spin_lock_init(&md->deferred_lock);
  1425. rwlock_init(&md->map_lock);
  1426. atomic_set(&md->holders, 1);
  1427. atomic_set(&md->open_count, 0);
  1428. atomic_set(&md->event_nr, 0);
  1429. atomic_set(&md->uevent_seq, 0);
  1430. INIT_LIST_HEAD(&md->uevent_list);
  1431. spin_lock_init(&md->uevent_lock);
  1432. md->queue = blk_init_queue(dm_request_fn, NULL);
  1433. if (!md->queue)
  1434. goto bad_queue;
  1435. /*
  1436. * Request-based dm devices cannot be stacked on top of bio-based dm
  1437. * devices. The type of this dm device has not been decided yet,
  1438. * although we initialized the queue using blk_init_queue().
  1439. * The type is decided at the first table loading time.
  1440. * To prevent problematic device stacking, clear the queue flag
  1441. * for request stacking support until then.
  1442. *
  1443. * This queue is new, so no concurrency on the queue_flags.
  1444. */
  1445. queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
  1446. md->saved_make_request_fn = md->queue->make_request_fn;
  1447. md->queue->queuedata = md;
  1448. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  1449. md->queue->backing_dev_info.congested_data = md;
  1450. blk_queue_make_request(md->queue, dm_request);
  1451. blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN, NULL);
  1452. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  1453. md->queue->unplug_fn = dm_unplug_all;
  1454. blk_queue_merge_bvec(md->queue, dm_merge_bvec);
  1455. blk_queue_softirq_done(md->queue, dm_softirq_done);
  1456. blk_queue_prep_rq(md->queue, dm_prep_fn);
  1457. blk_queue_lld_busy(md->queue, dm_lld_busy);
  1458. md->disk = alloc_disk(1);
  1459. if (!md->disk)
  1460. goto bad_disk;
  1461. atomic_set(&md->pending, 0);
  1462. init_waitqueue_head(&md->wait);
  1463. INIT_WORK(&md->work, dm_wq_work);
  1464. init_waitqueue_head(&md->eventq);
  1465. md->disk->major = _major;
  1466. md->disk->first_minor = minor;
  1467. md->disk->fops = &dm_blk_dops;
  1468. md->disk->queue = md->queue;
  1469. md->disk->private_data = md;
  1470. sprintf(md->disk->disk_name, "dm-%d", minor);
  1471. add_disk(md->disk);
  1472. format_dev_t(md->name, MKDEV(_major, minor));
  1473. md->wq = create_singlethread_workqueue("kdmflush");
  1474. if (!md->wq)
  1475. goto bad_thread;
  1476. md->bdev = bdget_disk(md->disk, 0);
  1477. if (!md->bdev)
  1478. goto bad_bdev;
  1479. /* Populate the mapping, nobody knows we exist yet */
  1480. spin_lock(&_minor_lock);
  1481. old_md = idr_replace(&_minor_idr, md, minor);
  1482. spin_unlock(&_minor_lock);
  1483. BUG_ON(old_md != MINOR_ALLOCED);
  1484. return md;
  1485. bad_bdev:
  1486. destroy_workqueue(md->wq);
  1487. bad_thread:
  1488. put_disk(md->disk);
  1489. bad_disk:
  1490. blk_cleanup_queue(md->queue);
  1491. bad_queue:
  1492. free_minor(minor);
  1493. bad_minor:
  1494. module_put(THIS_MODULE);
  1495. bad_module_get:
  1496. kfree(md);
  1497. return NULL;
  1498. }
  1499. static void unlock_fs(struct mapped_device *md);
  1500. static void free_dev(struct mapped_device *md)
  1501. {
  1502. int minor = MINOR(disk_devt(md->disk));
  1503. unlock_fs(md);
  1504. bdput(md->bdev);
  1505. destroy_workqueue(md->wq);
  1506. if (md->tio_pool)
  1507. mempool_destroy(md->tio_pool);
  1508. if (md->io_pool)
  1509. mempool_destroy(md->io_pool);
  1510. if (md->bs)
  1511. bioset_free(md->bs);
  1512. blk_integrity_unregister(md->disk);
  1513. del_gendisk(md->disk);
  1514. free_minor(minor);
  1515. spin_lock(&_minor_lock);
  1516. md->disk->private_data = NULL;
  1517. spin_unlock(&_minor_lock);
  1518. put_disk(md->disk);
  1519. blk_cleanup_queue(md->queue);
  1520. module_put(THIS_MODULE);
  1521. kfree(md);
  1522. }
  1523. static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
  1524. {
  1525. struct dm_md_mempools *p;
  1526. if (md->io_pool && md->tio_pool && md->bs)
  1527. /* the md already has necessary mempools */
  1528. goto out;
  1529. p = dm_table_get_md_mempools(t);
  1530. BUG_ON(!p || md->io_pool || md->tio_pool || md->bs);
  1531. md->io_pool = p->io_pool;
  1532. p->io_pool = NULL;
  1533. md->tio_pool = p->tio_pool;
  1534. p->tio_pool = NULL;
  1535. md->bs = p->bs;
  1536. p->bs = NULL;
  1537. out:
  1538. /* mempool bind completed, now no need any mempools in the table */
  1539. dm_table_free_md_mempools(t);
  1540. }
  1541. /*
  1542. * Bind a table to the device.
  1543. */
  1544. static void event_callback(void *context)
  1545. {
  1546. unsigned long flags;
  1547. LIST_HEAD(uevents);
  1548. struct mapped_device *md = (struct mapped_device *) context;
  1549. spin_lock_irqsave(&md->uevent_lock, flags);
  1550. list_splice_init(&md->uevent_list, &uevents);
  1551. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1552. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  1553. atomic_inc(&md->event_nr);
  1554. wake_up(&md->eventq);
  1555. }
  1556. static void __set_size(struct mapped_device *md, sector_t size)
  1557. {
  1558. set_capacity(md->disk, size);
  1559. mutex_lock(&md->bdev->bd_inode->i_mutex);
  1560. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  1561. mutex_unlock(&md->bdev->bd_inode->i_mutex);
  1562. }
  1563. static int __bind(struct mapped_device *md, struct dm_table *t,
  1564. struct queue_limits *limits)
  1565. {
  1566. struct request_queue *q = md->queue;
  1567. sector_t size;
  1568. size = dm_table_get_size(t);
  1569. /*
  1570. * Wipe any geometry if the size of the table changed.
  1571. */
  1572. if (size != get_capacity(md->disk))
  1573. memset(&md->geometry, 0, sizeof(md->geometry));
  1574. __set_size(md, size);
  1575. if (!size) {
  1576. dm_table_destroy(t);
  1577. return 0;
  1578. }
  1579. dm_table_event_callback(t, event_callback, md);
  1580. /*
  1581. * The queue hasn't been stopped yet, if the old table type wasn't
  1582. * for request-based during suspension. So stop it to prevent
  1583. * I/O mapping before resume.
  1584. * This must be done before setting the queue restrictions,
  1585. * because request-based dm may be run just after the setting.
  1586. */
  1587. if (dm_table_request_based(t) && !blk_queue_stopped(q))
  1588. stop_queue(q);
  1589. __bind_mempools(md, t);
  1590. write_lock(&md->map_lock);
  1591. md->map = t;
  1592. dm_table_set_restrictions(t, q, limits);
  1593. write_unlock(&md->map_lock);
  1594. return 0;
  1595. }
  1596. static void __unbind(struct mapped_device *md)
  1597. {
  1598. struct dm_table *map = md->map;
  1599. if (!map)
  1600. return;
  1601. dm_table_event_callback(map, NULL, NULL);
  1602. write_lock(&md->map_lock);
  1603. md->map = NULL;
  1604. write_unlock(&md->map_lock);
  1605. dm_table_destroy(map);
  1606. }
  1607. /*
  1608. * Constructor for a new device.
  1609. */
  1610. int dm_create(int minor, struct mapped_device **result)
  1611. {
  1612. struct mapped_device *md;
  1613. md = alloc_dev(minor);
  1614. if (!md)
  1615. return -ENXIO;
  1616. dm_sysfs_init(md);
  1617. *result = md;
  1618. return 0;
  1619. }
  1620. static struct mapped_device *dm_find_md(dev_t dev)
  1621. {
  1622. struct mapped_device *md;
  1623. unsigned minor = MINOR(dev);
  1624. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  1625. return NULL;
  1626. spin_lock(&_minor_lock);
  1627. md = idr_find(&_minor_idr, minor);
  1628. if (md && (md == MINOR_ALLOCED ||
  1629. (MINOR(disk_devt(dm_disk(md))) != minor) ||
  1630. test_bit(DMF_FREEING, &md->flags))) {
  1631. md = NULL;
  1632. goto out;
  1633. }
  1634. out:
  1635. spin_unlock(&_minor_lock);
  1636. return md;
  1637. }
  1638. struct mapped_device *dm_get_md(dev_t dev)
  1639. {
  1640. struct mapped_device *md = dm_find_md(dev);
  1641. if (md)
  1642. dm_get(md);
  1643. return md;
  1644. }
  1645. void *dm_get_mdptr(struct mapped_device *md)
  1646. {
  1647. return md->interface_ptr;
  1648. }
  1649. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  1650. {
  1651. md->interface_ptr = ptr;
  1652. }
  1653. void dm_get(struct mapped_device *md)
  1654. {
  1655. atomic_inc(&md->holders);
  1656. }
  1657. const char *dm_device_name(struct mapped_device *md)
  1658. {
  1659. return md->name;
  1660. }
  1661. EXPORT_SYMBOL_GPL(dm_device_name);
  1662. void dm_put(struct mapped_device *md)
  1663. {
  1664. struct dm_table *map;
  1665. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  1666. if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
  1667. map = dm_get_table(md);
  1668. idr_replace(&_minor_idr, MINOR_ALLOCED,
  1669. MINOR(disk_devt(dm_disk(md))));
  1670. set_bit(DMF_FREEING, &md->flags);
  1671. spin_unlock(&_minor_lock);
  1672. if (!dm_suspended(md)) {
  1673. dm_table_presuspend_targets(map);
  1674. dm_table_postsuspend_targets(map);
  1675. }
  1676. dm_sysfs_exit(md);
  1677. dm_table_put(map);
  1678. __unbind(md);
  1679. free_dev(md);
  1680. }
  1681. }
  1682. EXPORT_SYMBOL_GPL(dm_put);
  1683. static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
  1684. {
  1685. int r = 0;
  1686. DECLARE_WAITQUEUE(wait, current);
  1687. struct request_queue *q = md->queue;
  1688. unsigned long flags;
  1689. dm_unplug_all(md->queue);
  1690. add_wait_queue(&md->wait, &wait);
  1691. while (1) {
  1692. set_current_state(interruptible);
  1693. smp_mb();
  1694. if (dm_request_based(md)) {
  1695. spin_lock_irqsave(q->queue_lock, flags);
  1696. if (!queue_in_flight(q) && blk_queue_stopped(q)) {
  1697. spin_unlock_irqrestore(q->queue_lock, flags);
  1698. break;
  1699. }
  1700. spin_unlock_irqrestore(q->queue_lock, flags);
  1701. } else if (!atomic_read(&md->pending))
  1702. break;
  1703. if (interruptible == TASK_INTERRUPTIBLE &&
  1704. signal_pending(current)) {
  1705. r = -EINTR;
  1706. break;
  1707. }
  1708. io_schedule();
  1709. }
  1710. set_current_state(TASK_RUNNING);
  1711. remove_wait_queue(&md->wait, &wait);
  1712. return r;
  1713. }
  1714. static void dm_flush(struct mapped_device *md)
  1715. {
  1716. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  1717. bio_init(&md->barrier_bio);
  1718. md->barrier_bio.bi_bdev = md->bdev;
  1719. md->barrier_bio.bi_rw = WRITE_BARRIER;
  1720. __split_and_process_bio(md, &md->barrier_bio);
  1721. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  1722. }
  1723. static void process_barrier(struct mapped_device *md, struct bio *bio)
  1724. {
  1725. md->barrier_error = 0;
  1726. dm_flush(md);
  1727. if (!bio_empty_barrier(bio)) {
  1728. __split_and_process_bio(md, bio);
  1729. dm_flush(md);
  1730. }
  1731. if (md->barrier_error != DM_ENDIO_REQUEUE)
  1732. bio_endio(bio, md->barrier_error);
  1733. else {
  1734. spin_lock_irq(&md->deferred_lock);
  1735. bio_list_add_head(&md->deferred, bio);
  1736. spin_unlock_irq(&md->deferred_lock);
  1737. }
  1738. }
  1739. /*
  1740. * Process the deferred bios
  1741. */
  1742. static void dm_wq_work(struct work_struct *work)
  1743. {
  1744. struct mapped_device *md = container_of(work, struct mapped_device,
  1745. work);
  1746. struct bio *c;
  1747. down_write(&md->io_lock);
  1748. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1749. spin_lock_irq(&md->deferred_lock);
  1750. c = bio_list_pop(&md->deferred);
  1751. spin_unlock_irq(&md->deferred_lock);
  1752. if (!c) {
  1753. clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
  1754. break;
  1755. }
  1756. up_write(&md->io_lock);
  1757. if (dm_request_based(md))
  1758. generic_make_request(c);
  1759. else {
  1760. if (bio_barrier(c))
  1761. process_barrier(md, c);
  1762. else
  1763. __split_and_process_bio(md, c);
  1764. }
  1765. down_write(&md->io_lock);
  1766. }
  1767. up_write(&md->io_lock);
  1768. }
  1769. static void dm_queue_flush(struct mapped_device *md)
  1770. {
  1771. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1772. smp_mb__after_clear_bit();
  1773. queue_work(md->wq, &md->work);
  1774. }
  1775. /*
  1776. * Swap in a new table (destroying old one).
  1777. */
  1778. int dm_swap_table(struct mapped_device *md, struct dm_table *table)
  1779. {
  1780. struct queue_limits limits;
  1781. int r = -EINVAL;
  1782. mutex_lock(&md->suspend_lock);
  1783. /* device must be suspended */
  1784. if (!dm_suspended(md))
  1785. goto out;
  1786. r = dm_calculate_queue_limits(table, &limits);
  1787. if (r)
  1788. goto out;
  1789. /* cannot change the device type, once a table is bound */
  1790. if (md->map &&
  1791. (dm_table_get_type(md->map) != dm_table_get_type(table))) {
  1792. DMWARN("can't change the device type after a table is bound");
  1793. goto out;
  1794. }
  1795. __unbind(md);
  1796. r = __bind(md, table, &limits);
  1797. out:
  1798. mutex_unlock(&md->suspend_lock);
  1799. return r;
  1800. }
  1801. static void dm_rq_invalidate_suspend_marker(struct mapped_device *md)
  1802. {
  1803. md->suspend_rq.special = (void *)0x1;
  1804. }
  1805. static void dm_rq_abort_suspend(struct mapped_device *md, int noflush)
  1806. {
  1807. struct request_queue *q = md->queue;
  1808. unsigned long flags;
  1809. spin_lock_irqsave(q->queue_lock, flags);
  1810. if (!noflush)
  1811. dm_rq_invalidate_suspend_marker(md);
  1812. __start_queue(q);
  1813. spin_unlock_irqrestore(q->queue_lock, flags);
  1814. }
  1815. static void dm_rq_start_suspend(struct mapped_device *md, int noflush)
  1816. {
  1817. struct request *rq = &md->suspend_rq;
  1818. struct request_queue *q = md->queue;
  1819. if (noflush)
  1820. stop_queue(q);
  1821. else {
  1822. blk_rq_init(q, rq);
  1823. blk_insert_request(q, rq, 0, NULL);
  1824. }
  1825. }
  1826. static int dm_rq_suspend_available(struct mapped_device *md, int noflush)
  1827. {
  1828. int r = 1;
  1829. struct request *rq = &md->suspend_rq;
  1830. struct request_queue *q = md->queue;
  1831. unsigned long flags;
  1832. if (noflush)
  1833. return r;
  1834. /* The marker must be protected by queue lock if it is in use */
  1835. spin_lock_irqsave(q->queue_lock, flags);
  1836. if (unlikely(rq->ref_count)) {
  1837. /*
  1838. * This can happen, when the previous flush suspend was
  1839. * interrupted, the marker is still in the queue and
  1840. * this flush suspend has been invoked, because we don't
  1841. * remove the marker at the time of suspend interruption.
  1842. * We have only one marker per mapped_device, so we can't
  1843. * start another flush suspend while it is in use.
  1844. */
  1845. BUG_ON(!rq->special); /* The marker should be invalidated */
  1846. DMWARN("Invalidating the previous flush suspend is still in"
  1847. " progress. Please retry later.");
  1848. r = 0;
  1849. }
  1850. spin_unlock_irqrestore(q->queue_lock, flags);
  1851. return r;
  1852. }
  1853. /*
  1854. * Functions to lock and unlock any filesystem running on the
  1855. * device.
  1856. */
  1857. static int lock_fs(struct mapped_device *md)
  1858. {
  1859. int r;
  1860. WARN_ON(md->frozen_sb);
  1861. md->frozen_sb = freeze_bdev(md->bdev);
  1862. if (IS_ERR(md->frozen_sb)) {
  1863. r = PTR_ERR(md->frozen_sb);
  1864. md->frozen_sb = NULL;
  1865. return r;
  1866. }
  1867. set_bit(DMF_FROZEN, &md->flags);
  1868. return 0;
  1869. }
  1870. static void unlock_fs(struct mapped_device *md)
  1871. {
  1872. if (!test_bit(DMF_FROZEN, &md->flags))
  1873. return;
  1874. thaw_bdev(md->bdev, md->frozen_sb);
  1875. md->frozen_sb = NULL;
  1876. clear_bit(DMF_FROZEN, &md->flags);
  1877. }
  1878. /*
  1879. * We need to be able to change a mapping table under a mounted
  1880. * filesystem. For example we might want to move some data in
  1881. * the background. Before the table can be swapped with
  1882. * dm_bind_table, dm_suspend must be called to flush any in
  1883. * flight bios and ensure that any further io gets deferred.
  1884. */
  1885. /*
  1886. * Suspend mechanism in request-based dm.
  1887. *
  1888. * After the suspend starts, further incoming requests are kept in
  1889. * the request_queue and deferred.
  1890. * Remaining requests in the request_queue at the start of suspend are flushed
  1891. * if it is flush suspend.
  1892. * The suspend completes when the following conditions have been satisfied,
  1893. * so wait for it:
  1894. * 1. q->in_flight is 0 (which means no in_flight request)
  1895. * 2. queue has been stopped (which means no request dispatching)
  1896. *
  1897. *
  1898. * Noflush suspend
  1899. * ---------------
  1900. * Noflush suspend doesn't need to dispatch remaining requests.
  1901. * So stop the queue immediately. Then, wait for all in_flight requests
  1902. * to be completed or requeued.
  1903. *
  1904. * To abort noflush suspend, start the queue.
  1905. *
  1906. *
  1907. * Flush suspend
  1908. * -------------
  1909. * Flush suspend needs to dispatch remaining requests. So stop the queue
  1910. * after the remaining requests are completed. (Requeued request must be also
  1911. * re-dispatched and completed. Until then, we can't stop the queue.)
  1912. *
  1913. * During flushing the remaining requests, further incoming requests are also
  1914. * inserted to the same queue. To distinguish which requests are to be
  1915. * flushed, we insert a marker request to the queue at the time of starting
  1916. * flush suspend, like a barrier.
  1917. * The dispatching is blocked when the marker is found on the top of the queue.
  1918. * And the queue is stopped when all in_flight requests are completed, since
  1919. * that means the remaining requests are completely flushed.
  1920. * Then, the marker is removed from the queue.
  1921. *
  1922. * To abort flush suspend, we also need to take care of the marker, not only
  1923. * starting the queue.
  1924. * We don't remove the marker forcibly from the queue since it's against
  1925. * the block-layer manner. Instead, we put a invalidated mark on the marker.
  1926. * When the invalidated marker is found on the top of the queue, it is
  1927. * immediately removed from the queue, so it doesn't block dispatching.
  1928. * Because we have only one marker per mapped_device, we can't start another
  1929. * flush suspend until the invalidated marker is removed from the queue.
  1930. * So fail and return with -EBUSY in such a case.
  1931. */
  1932. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  1933. {
  1934. struct dm_table *map = NULL;
  1935. int r = 0;
  1936. int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
  1937. int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
  1938. mutex_lock(&md->suspend_lock);
  1939. if (dm_suspended(md)) {
  1940. r = -EINVAL;
  1941. goto out_unlock;
  1942. }
  1943. if (dm_request_based(md) && !dm_rq_suspend_available(md, noflush)) {
  1944. r = -EBUSY;
  1945. goto out_unlock;
  1946. }
  1947. map = dm_get_table(md);
  1948. /*
  1949. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  1950. * This flag is cleared before dm_suspend returns.
  1951. */
  1952. if (noflush)
  1953. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1954. /* This does not get reverted if there's an error later. */
  1955. dm_table_presuspend_targets(map);
  1956. /*
  1957. * Flush I/O to the device. noflush supersedes do_lockfs,
  1958. * because lock_fs() needs to flush I/Os.
  1959. */
  1960. if (!noflush && do_lockfs) {
  1961. r = lock_fs(md);
  1962. if (r)
  1963. goto out;
  1964. }
  1965. /*
  1966. * Here we must make sure that no processes are submitting requests
  1967. * to target drivers i.e. no one may be executing
  1968. * __split_and_process_bio. This is called from dm_request and
  1969. * dm_wq_work.
  1970. *
  1971. * To get all processes out of __split_and_process_bio in dm_request,
  1972. * we take the write lock. To prevent any process from reentering
  1973. * __split_and_process_bio from dm_request, we set
  1974. * DMF_QUEUE_IO_TO_THREAD.
  1975. *
  1976. * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
  1977. * and call flush_workqueue(md->wq). flush_workqueue will wait until
  1978. * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
  1979. * further calls to __split_and_process_bio from dm_wq_work.
  1980. */
  1981. down_write(&md->io_lock);
  1982. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1983. set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
  1984. up_write(&md->io_lock);
  1985. flush_workqueue(md->wq);
  1986. if (dm_request_based(md))
  1987. dm_rq_start_suspend(md, noflush);
  1988. /*
  1989. * At this point no more requests are entering target request routines.
  1990. * We call dm_wait_for_completion to wait for all existing requests
  1991. * to finish.
  1992. */
  1993. r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
  1994. down_write(&md->io_lock);
  1995. if (noflush)
  1996. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  1997. up_write(&md->io_lock);
  1998. /* were we interrupted ? */
  1999. if (r < 0) {
  2000. dm_queue_flush(md);
  2001. if (dm_request_based(md))
  2002. dm_rq_abort_suspend(md, noflush);
  2003. unlock_fs(md);
  2004. goto out; /* pushback list is already flushed, so skip flush */
  2005. }
  2006. /*
  2007. * If dm_wait_for_completion returned 0, the device is completely
  2008. * quiescent now. There is no request-processing activity. All new
  2009. * requests are being added to md->deferred list.
  2010. */
  2011. dm_table_postsuspend_targets(map);
  2012. set_bit(DMF_SUSPENDED, &md->flags);
  2013. out:
  2014. dm_table_put(map);
  2015. out_unlock:
  2016. mutex_unlock(&md->suspend_lock);
  2017. return r;
  2018. }
  2019. int dm_resume(struct mapped_device *md)
  2020. {
  2021. int r = -EINVAL;
  2022. struct dm_table *map = NULL;
  2023. mutex_lock(&md->suspend_lock);
  2024. if (!dm_suspended(md))
  2025. goto out;
  2026. map = dm_get_table(md);
  2027. if (!map || !dm_table_get_size(map))
  2028. goto out;
  2029. r = dm_table_resume_targets(map);
  2030. if (r)
  2031. goto out;
  2032. dm_queue_flush(md);
  2033. /*
  2034. * Flushing deferred I/Os must be done after targets are resumed
  2035. * so that mapping of targets can work correctly.
  2036. * Request-based dm is queueing the deferred I/Os in its request_queue.
  2037. */
  2038. if (dm_request_based(md))
  2039. start_queue(md->queue);
  2040. unlock_fs(md);
  2041. clear_bit(DMF_SUSPENDED, &md->flags);
  2042. dm_table_unplug_all(map);
  2043. r = 0;
  2044. out:
  2045. dm_table_put(map);
  2046. mutex_unlock(&md->suspend_lock);
  2047. return r;
  2048. }
  2049. /*-----------------------------------------------------------------
  2050. * Event notification.
  2051. *---------------------------------------------------------------*/
  2052. void dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
  2053. unsigned cookie)
  2054. {
  2055. char udev_cookie[DM_COOKIE_LENGTH];
  2056. char *envp[] = { udev_cookie, NULL };
  2057. if (!cookie)
  2058. kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
  2059. else {
  2060. snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
  2061. DM_COOKIE_ENV_VAR_NAME, cookie);
  2062. kobject_uevent_env(&disk_to_dev(md->disk)->kobj, action, envp);
  2063. }
  2064. }
  2065. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  2066. {
  2067. return atomic_add_return(1, &md->uevent_seq);
  2068. }
  2069. uint32_t dm_get_event_nr(struct mapped_device *md)
  2070. {
  2071. return atomic_read(&md->event_nr);
  2072. }
  2073. int dm_wait_event(struct mapped_device *md, int event_nr)
  2074. {
  2075. return wait_event_interruptible(md->eventq,
  2076. (event_nr != atomic_read(&md->event_nr)));
  2077. }
  2078. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  2079. {
  2080. unsigned long flags;
  2081. spin_lock_irqsave(&md->uevent_lock, flags);
  2082. list_add(elist, &md->uevent_list);
  2083. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2084. }
  2085. /*
  2086. * The gendisk is only valid as long as you have a reference
  2087. * count on 'md'.
  2088. */
  2089. struct gendisk *dm_disk(struct mapped_device *md)
  2090. {
  2091. return md->disk;
  2092. }
  2093. struct kobject *dm_kobject(struct mapped_device *md)
  2094. {
  2095. return &md->kobj;
  2096. }
  2097. /*
  2098. * struct mapped_device should not be exported outside of dm.c
  2099. * so use this check to verify that kobj is part of md structure
  2100. */
  2101. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  2102. {
  2103. struct mapped_device *md;
  2104. md = container_of(kobj, struct mapped_device, kobj);
  2105. if (&md->kobj != kobj)
  2106. return NULL;
  2107. if (test_bit(DMF_FREEING, &md->flags) ||
  2108. test_bit(DMF_DELETING, &md->flags))
  2109. return NULL;
  2110. dm_get(md);
  2111. return md;
  2112. }
  2113. int dm_suspended(struct mapped_device *md)
  2114. {
  2115. return test_bit(DMF_SUSPENDED, &md->flags);
  2116. }
  2117. int dm_noflush_suspending(struct dm_target *ti)
  2118. {
  2119. struct mapped_device *md = dm_table_get_md(ti->table);
  2120. int r = __noflush_suspending(md);
  2121. dm_put(md);
  2122. return r;
  2123. }
  2124. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  2125. struct dm_md_mempools *dm_alloc_md_mempools(unsigned type)
  2126. {
  2127. struct dm_md_mempools *pools = kmalloc(sizeof(*pools), GFP_KERNEL);
  2128. if (!pools)
  2129. return NULL;
  2130. pools->io_pool = (type == DM_TYPE_BIO_BASED) ?
  2131. mempool_create_slab_pool(MIN_IOS, _io_cache) :
  2132. mempool_create_slab_pool(MIN_IOS, _rq_bio_info_cache);
  2133. if (!pools->io_pool)
  2134. goto free_pools_and_out;
  2135. pools->tio_pool = (type == DM_TYPE_BIO_BASED) ?
  2136. mempool_create_slab_pool(MIN_IOS, _tio_cache) :
  2137. mempool_create_slab_pool(MIN_IOS, _rq_tio_cache);
  2138. if (!pools->tio_pool)
  2139. goto free_io_pool_and_out;
  2140. pools->bs = (type == DM_TYPE_BIO_BASED) ?
  2141. bioset_create(16, 0) : bioset_create(MIN_IOS, 0);
  2142. if (!pools->bs)
  2143. goto free_tio_pool_and_out;
  2144. return pools;
  2145. free_tio_pool_and_out:
  2146. mempool_destroy(pools->tio_pool);
  2147. free_io_pool_and_out:
  2148. mempool_destroy(pools->io_pool);
  2149. free_pools_and_out:
  2150. kfree(pools);
  2151. return NULL;
  2152. }
  2153. void dm_free_md_mempools(struct dm_md_mempools *pools)
  2154. {
  2155. if (!pools)
  2156. return;
  2157. if (pools->io_pool)
  2158. mempool_destroy(pools->io_pool);
  2159. if (pools->tio_pool)
  2160. mempool_destroy(pools->tio_pool);
  2161. if (pools->bs)
  2162. bioset_free(pools->bs);
  2163. kfree(pools);
  2164. }
  2165. static struct block_device_operations dm_blk_dops = {
  2166. .open = dm_blk_open,
  2167. .release = dm_blk_close,
  2168. .ioctl = dm_blk_ioctl,
  2169. .getgeo = dm_blk_getgeo,
  2170. .owner = THIS_MODULE
  2171. };
  2172. EXPORT_SYMBOL(dm_get_mapinfo);
  2173. /*
  2174. * module hooks
  2175. */
  2176. module_init(dm_init);
  2177. module_exit(dm_exit);
  2178. module_param(major, uint, 0);
  2179. MODULE_PARM_DESC(major, "The major number of the device mapper");
  2180. MODULE_DESCRIPTION(DM_NAME " driver");
  2181. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2182. MODULE_LICENSE("GPL");