dm.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650
  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/mempool.h>
  16. #include <linux/slab.h>
  17. #include <linux/idr.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/delay.h>
  20. #include <linux/wait.h>
  21. #include <linux/kthread.h>
  22. #include <linux/ktime.h>
  23. #include <linux/elevator.h> /* for rq_end_sector() */
  24. #include <linux/blk-mq.h>
  25. #include <trace/events/block.h>
  26. #define DM_MSG_PREFIX "core"
  27. #ifdef CONFIG_PRINTK
  28. /*
  29. * ratelimit state to be used in DMXXX_LIMIT().
  30. */
  31. DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
  32. DEFAULT_RATELIMIT_INTERVAL,
  33. DEFAULT_RATELIMIT_BURST);
  34. EXPORT_SYMBOL(dm_ratelimit_state);
  35. #endif
  36. /*
  37. * Cookies are numeric values sent with CHANGE and REMOVE
  38. * uevents while resuming, removing or renaming the device.
  39. */
  40. #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
  41. #define DM_COOKIE_LENGTH 24
  42. static const char *_name = DM_NAME;
  43. static unsigned int major = 0;
  44. static unsigned int _major = 0;
  45. static DEFINE_IDR(_minor_idr);
  46. static DEFINE_SPINLOCK(_minor_lock);
  47. static void do_deferred_remove(struct work_struct *w);
  48. static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
  49. static struct workqueue_struct *deferred_remove_workqueue;
  50. /*
  51. * For bio-based dm.
  52. * One of these is allocated per bio.
  53. */
  54. struct dm_io {
  55. struct mapped_device *md;
  56. int error;
  57. atomic_t io_count;
  58. struct bio *bio;
  59. unsigned long start_time;
  60. spinlock_t endio_lock;
  61. struct dm_stats_aux stats_aux;
  62. };
  63. /*
  64. * For request-based dm.
  65. * One of these is allocated per request.
  66. */
  67. struct dm_rq_target_io {
  68. struct mapped_device *md;
  69. struct dm_target *ti;
  70. struct request *orig, *clone;
  71. struct kthread_work work;
  72. int error;
  73. union map_info info;
  74. };
  75. /*
  76. * For request-based dm - the bio clones we allocate are embedded in these
  77. * structs.
  78. *
  79. * We allocate these with bio_alloc_bioset, using the front_pad parameter when
  80. * the bioset is created - this means the bio has to come at the end of the
  81. * struct.
  82. */
  83. struct dm_rq_clone_bio_info {
  84. struct bio *orig;
  85. struct dm_rq_target_io *tio;
  86. struct bio clone;
  87. };
  88. union map_info *dm_get_rq_mapinfo(struct request *rq)
  89. {
  90. if (rq && rq->end_io_data)
  91. return &((struct dm_rq_target_io *)rq->end_io_data)->info;
  92. return NULL;
  93. }
  94. EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
  95. #define MINOR_ALLOCED ((void *)-1)
  96. /*
  97. * Bits for the md->flags field.
  98. */
  99. #define DMF_BLOCK_IO_FOR_SUSPEND 0
  100. #define DMF_SUSPENDED 1
  101. #define DMF_FROZEN 2
  102. #define DMF_FREEING 3
  103. #define DMF_DELETING 4
  104. #define DMF_NOFLUSH_SUSPENDING 5
  105. #define DMF_MERGE_IS_OPTIONAL 6
  106. #define DMF_DEFERRED_REMOVE 7
  107. #define DMF_SUSPENDED_INTERNALLY 8
  108. /*
  109. * A dummy definition to make RCU happy.
  110. * struct dm_table should never be dereferenced in this file.
  111. */
  112. struct dm_table {
  113. int undefined__;
  114. };
  115. /*
  116. * Work processed by per-device workqueue.
  117. */
  118. struct mapped_device {
  119. struct srcu_struct io_barrier;
  120. struct mutex suspend_lock;
  121. atomic_t holders;
  122. atomic_t open_count;
  123. /*
  124. * The current mapping.
  125. * Use dm_get_live_table{_fast} or take suspend_lock for
  126. * dereference.
  127. */
  128. struct dm_table __rcu *map;
  129. struct list_head table_devices;
  130. struct mutex table_devices_lock;
  131. unsigned long flags;
  132. struct request_queue *queue;
  133. unsigned type;
  134. /* Protect queue and type against concurrent access. */
  135. struct mutex type_lock;
  136. struct target_type *immutable_target_type;
  137. struct gendisk *disk;
  138. char name[16];
  139. void *interface_ptr;
  140. /*
  141. * A list of ios that arrived while we were suspended.
  142. */
  143. atomic_t pending[2];
  144. wait_queue_head_t wait;
  145. struct work_struct work;
  146. struct bio_list deferred;
  147. spinlock_t deferred_lock;
  148. /*
  149. * Processing queue (flush)
  150. */
  151. struct workqueue_struct *wq;
  152. /*
  153. * io objects are allocated from here.
  154. */
  155. mempool_t *io_pool;
  156. mempool_t *rq_pool;
  157. struct bio_set *bs;
  158. /*
  159. * Event handling.
  160. */
  161. atomic_t event_nr;
  162. wait_queue_head_t eventq;
  163. atomic_t uevent_seq;
  164. struct list_head uevent_list;
  165. spinlock_t uevent_lock; /* Protect access to uevent_list */
  166. /*
  167. * freeze/thaw support require holding onto a super block
  168. */
  169. struct super_block *frozen_sb;
  170. struct block_device *bdev;
  171. /* forced geometry settings */
  172. struct hd_geometry geometry;
  173. /* kobject and completion */
  174. struct dm_kobject_holder kobj_holder;
  175. /* zero-length flush that will be cloned and submitted to targets */
  176. struct bio flush_bio;
  177. /* the number of internal suspends */
  178. unsigned internal_suspend_count;
  179. struct dm_stats stats;
  180. struct kthread_worker kworker;
  181. struct task_struct *kworker_task;
  182. /* for request-based merge heuristic in dm_request_fn() */
  183. unsigned seq_rq_merge_deadline_usecs;
  184. int last_rq_rw;
  185. sector_t last_rq_pos;
  186. ktime_t last_rq_start_time;
  187. /* for blk-mq request-based DM support */
  188. struct blk_mq_tag_set tag_set;
  189. bool use_blk_mq;
  190. };
  191. #ifdef CONFIG_DM_MQ_DEFAULT
  192. static bool use_blk_mq = true;
  193. #else
  194. static bool use_blk_mq = false;
  195. #endif
  196. bool dm_use_blk_mq(struct mapped_device *md)
  197. {
  198. return md->use_blk_mq;
  199. }
  200. /*
  201. * For mempools pre-allocation at the table loading time.
  202. */
  203. struct dm_md_mempools {
  204. mempool_t *io_pool;
  205. mempool_t *rq_pool;
  206. struct bio_set *bs;
  207. };
  208. struct table_device {
  209. struct list_head list;
  210. atomic_t count;
  211. struct dm_dev dm_dev;
  212. };
  213. #define RESERVED_BIO_BASED_IOS 16
  214. #define RESERVED_REQUEST_BASED_IOS 256
  215. #define RESERVED_MAX_IOS 1024
  216. static struct kmem_cache *_io_cache;
  217. static struct kmem_cache *_rq_tio_cache;
  218. static struct kmem_cache *_rq_cache;
  219. /*
  220. * Bio-based DM's mempools' reserved IOs set by the user.
  221. */
  222. static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
  223. /*
  224. * Request-based DM's mempools' reserved IOs set by the user.
  225. */
  226. static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
  227. static unsigned __dm_get_module_param(unsigned *module_param,
  228. unsigned def, unsigned max)
  229. {
  230. unsigned param = ACCESS_ONCE(*module_param);
  231. unsigned modified_param = 0;
  232. if (!param)
  233. modified_param = def;
  234. else if (param > max)
  235. modified_param = max;
  236. if (modified_param) {
  237. (void)cmpxchg(module_param, param, modified_param);
  238. param = modified_param;
  239. }
  240. return param;
  241. }
  242. unsigned dm_get_reserved_bio_based_ios(void)
  243. {
  244. return __dm_get_module_param(&reserved_bio_based_ios,
  245. RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
  246. }
  247. EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
  248. unsigned dm_get_reserved_rq_based_ios(void)
  249. {
  250. return __dm_get_module_param(&reserved_rq_based_ios,
  251. RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
  252. }
  253. EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
  254. static int __init local_init(void)
  255. {
  256. int r = -ENOMEM;
  257. /* allocate a slab for the dm_ios */
  258. _io_cache = KMEM_CACHE(dm_io, 0);
  259. if (!_io_cache)
  260. return r;
  261. _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
  262. if (!_rq_tio_cache)
  263. goto out_free_io_cache;
  264. _rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
  265. __alignof__(struct request), 0, NULL);
  266. if (!_rq_cache)
  267. goto out_free_rq_tio_cache;
  268. r = dm_uevent_init();
  269. if (r)
  270. goto out_free_rq_cache;
  271. deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
  272. if (!deferred_remove_workqueue) {
  273. r = -ENOMEM;
  274. goto out_uevent_exit;
  275. }
  276. _major = major;
  277. r = register_blkdev(_major, _name);
  278. if (r < 0)
  279. goto out_free_workqueue;
  280. if (!_major)
  281. _major = r;
  282. return 0;
  283. out_free_workqueue:
  284. destroy_workqueue(deferred_remove_workqueue);
  285. out_uevent_exit:
  286. dm_uevent_exit();
  287. out_free_rq_cache:
  288. kmem_cache_destroy(_rq_cache);
  289. out_free_rq_tio_cache:
  290. kmem_cache_destroy(_rq_tio_cache);
  291. out_free_io_cache:
  292. kmem_cache_destroy(_io_cache);
  293. return r;
  294. }
  295. static void local_exit(void)
  296. {
  297. flush_scheduled_work();
  298. destroy_workqueue(deferred_remove_workqueue);
  299. kmem_cache_destroy(_rq_cache);
  300. kmem_cache_destroy(_rq_tio_cache);
  301. kmem_cache_destroy(_io_cache);
  302. unregister_blkdev(_major, _name);
  303. dm_uevent_exit();
  304. _major = 0;
  305. DMINFO("cleaned up");
  306. }
  307. static int (*_inits[])(void) __initdata = {
  308. local_init,
  309. dm_target_init,
  310. dm_linear_init,
  311. dm_stripe_init,
  312. dm_io_init,
  313. dm_kcopyd_init,
  314. dm_interface_init,
  315. dm_statistics_init,
  316. };
  317. static void (*_exits[])(void) = {
  318. local_exit,
  319. dm_target_exit,
  320. dm_linear_exit,
  321. dm_stripe_exit,
  322. dm_io_exit,
  323. dm_kcopyd_exit,
  324. dm_interface_exit,
  325. dm_statistics_exit,
  326. };
  327. static int __init dm_init(void)
  328. {
  329. const int count = ARRAY_SIZE(_inits);
  330. int r, i;
  331. for (i = 0; i < count; i++) {
  332. r = _inits[i]();
  333. if (r)
  334. goto bad;
  335. }
  336. return 0;
  337. bad:
  338. while (i--)
  339. _exits[i]();
  340. return r;
  341. }
  342. static void __exit dm_exit(void)
  343. {
  344. int i = ARRAY_SIZE(_exits);
  345. while (i--)
  346. _exits[i]();
  347. /*
  348. * Should be empty by this point.
  349. */
  350. idr_destroy(&_minor_idr);
  351. }
  352. /*
  353. * Block device functions
  354. */
  355. int dm_deleting_md(struct mapped_device *md)
  356. {
  357. return test_bit(DMF_DELETING, &md->flags);
  358. }
  359. static int dm_blk_open(struct block_device *bdev, fmode_t mode)
  360. {
  361. struct mapped_device *md;
  362. spin_lock(&_minor_lock);
  363. md = bdev->bd_disk->private_data;
  364. if (!md)
  365. goto out;
  366. if (test_bit(DMF_FREEING, &md->flags) ||
  367. dm_deleting_md(md)) {
  368. md = NULL;
  369. goto out;
  370. }
  371. dm_get(md);
  372. atomic_inc(&md->open_count);
  373. out:
  374. spin_unlock(&_minor_lock);
  375. return md ? 0 : -ENXIO;
  376. }
  377. static void dm_blk_close(struct gendisk *disk, fmode_t mode)
  378. {
  379. struct mapped_device *md;
  380. spin_lock(&_minor_lock);
  381. md = disk->private_data;
  382. if (WARN_ON(!md))
  383. goto out;
  384. if (atomic_dec_and_test(&md->open_count) &&
  385. (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
  386. queue_work(deferred_remove_workqueue, &deferred_remove_work);
  387. dm_put(md);
  388. out:
  389. spin_unlock(&_minor_lock);
  390. }
  391. int dm_open_count(struct mapped_device *md)
  392. {
  393. return atomic_read(&md->open_count);
  394. }
  395. /*
  396. * Guarantees nothing is using the device before it's deleted.
  397. */
  398. int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
  399. {
  400. int r = 0;
  401. spin_lock(&_minor_lock);
  402. if (dm_open_count(md)) {
  403. r = -EBUSY;
  404. if (mark_deferred)
  405. set_bit(DMF_DEFERRED_REMOVE, &md->flags);
  406. } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
  407. r = -EEXIST;
  408. else
  409. set_bit(DMF_DELETING, &md->flags);
  410. spin_unlock(&_minor_lock);
  411. return r;
  412. }
  413. int dm_cancel_deferred_remove(struct mapped_device *md)
  414. {
  415. int r = 0;
  416. spin_lock(&_minor_lock);
  417. if (test_bit(DMF_DELETING, &md->flags))
  418. r = -EBUSY;
  419. else
  420. clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
  421. spin_unlock(&_minor_lock);
  422. return r;
  423. }
  424. static void do_deferred_remove(struct work_struct *w)
  425. {
  426. dm_deferred_remove();
  427. }
  428. sector_t dm_get_size(struct mapped_device *md)
  429. {
  430. return get_capacity(md->disk);
  431. }
  432. struct request_queue *dm_get_md_queue(struct mapped_device *md)
  433. {
  434. return md->queue;
  435. }
  436. struct dm_stats *dm_get_stats(struct mapped_device *md)
  437. {
  438. return &md->stats;
  439. }
  440. static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  441. {
  442. struct mapped_device *md = bdev->bd_disk->private_data;
  443. return dm_get_geometry(md, geo);
  444. }
  445. static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
  446. unsigned int cmd, unsigned long arg)
  447. {
  448. struct mapped_device *md = bdev->bd_disk->private_data;
  449. int srcu_idx;
  450. struct dm_table *map;
  451. struct dm_target *tgt;
  452. int r = -ENOTTY;
  453. retry:
  454. map = dm_get_live_table(md, &srcu_idx);
  455. if (!map || !dm_table_get_size(map))
  456. goto out;
  457. /* We only support devices that have a single target */
  458. if (dm_table_get_num_targets(map) != 1)
  459. goto out;
  460. tgt = dm_table_get_target(map, 0);
  461. if (!tgt->type->ioctl)
  462. goto out;
  463. if (dm_suspended_md(md)) {
  464. r = -EAGAIN;
  465. goto out;
  466. }
  467. r = tgt->type->ioctl(tgt, cmd, arg);
  468. out:
  469. dm_put_live_table(md, srcu_idx);
  470. if (r == -ENOTCONN) {
  471. msleep(10);
  472. goto retry;
  473. }
  474. return r;
  475. }
  476. static struct dm_io *alloc_io(struct mapped_device *md)
  477. {
  478. return mempool_alloc(md->io_pool, GFP_NOIO);
  479. }
  480. static void free_io(struct mapped_device *md, struct dm_io *io)
  481. {
  482. mempool_free(io, md->io_pool);
  483. }
  484. static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
  485. {
  486. bio_put(&tio->clone);
  487. }
  488. static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
  489. gfp_t gfp_mask)
  490. {
  491. return mempool_alloc(md->io_pool, gfp_mask);
  492. }
  493. static void free_rq_tio(struct dm_rq_target_io *tio)
  494. {
  495. mempool_free(tio, tio->md->io_pool);
  496. }
  497. static struct request *alloc_clone_request(struct mapped_device *md,
  498. gfp_t gfp_mask)
  499. {
  500. return mempool_alloc(md->rq_pool, gfp_mask);
  501. }
  502. static void free_clone_request(struct mapped_device *md, struct request *rq)
  503. {
  504. mempool_free(rq, md->rq_pool);
  505. }
  506. static int md_in_flight(struct mapped_device *md)
  507. {
  508. return atomic_read(&md->pending[READ]) +
  509. atomic_read(&md->pending[WRITE]);
  510. }
  511. static void start_io_acct(struct dm_io *io)
  512. {
  513. struct mapped_device *md = io->md;
  514. struct bio *bio = io->bio;
  515. int cpu;
  516. int rw = bio_data_dir(bio);
  517. io->start_time = jiffies;
  518. cpu = part_stat_lock();
  519. part_round_stats(cpu, &dm_disk(md)->part0);
  520. part_stat_unlock();
  521. atomic_set(&dm_disk(md)->part0.in_flight[rw],
  522. atomic_inc_return(&md->pending[rw]));
  523. if (unlikely(dm_stats_used(&md->stats)))
  524. dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
  525. bio_sectors(bio), false, 0, &io->stats_aux);
  526. }
  527. static void end_io_acct(struct dm_io *io)
  528. {
  529. struct mapped_device *md = io->md;
  530. struct bio *bio = io->bio;
  531. unsigned long duration = jiffies - io->start_time;
  532. int pending;
  533. int rw = bio_data_dir(bio);
  534. generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
  535. if (unlikely(dm_stats_used(&md->stats)))
  536. dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
  537. bio_sectors(bio), true, duration, &io->stats_aux);
  538. /*
  539. * After this is decremented the bio must not be touched if it is
  540. * a flush.
  541. */
  542. pending = atomic_dec_return(&md->pending[rw]);
  543. atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
  544. pending += atomic_read(&md->pending[rw^0x1]);
  545. /* nudge anyone waiting on suspend queue */
  546. if (!pending)
  547. wake_up(&md->wait);
  548. }
  549. /*
  550. * Add the bio to the list of deferred io.
  551. */
  552. static void queue_io(struct mapped_device *md, struct bio *bio)
  553. {
  554. unsigned long flags;
  555. spin_lock_irqsave(&md->deferred_lock, flags);
  556. bio_list_add(&md->deferred, bio);
  557. spin_unlock_irqrestore(&md->deferred_lock, flags);
  558. queue_work(md->wq, &md->work);
  559. }
  560. /*
  561. * Everyone (including functions in this file), should use this
  562. * function to access the md->map field, and make sure they call
  563. * dm_put_live_table() when finished.
  564. */
  565. struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
  566. {
  567. *srcu_idx = srcu_read_lock(&md->io_barrier);
  568. return srcu_dereference(md->map, &md->io_barrier);
  569. }
  570. void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
  571. {
  572. srcu_read_unlock(&md->io_barrier, srcu_idx);
  573. }
  574. void dm_sync_table(struct mapped_device *md)
  575. {
  576. synchronize_srcu(&md->io_barrier);
  577. synchronize_rcu_expedited();
  578. }
  579. /*
  580. * A fast alternative to dm_get_live_table/dm_put_live_table.
  581. * The caller must not block between these two functions.
  582. */
  583. static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
  584. {
  585. rcu_read_lock();
  586. return rcu_dereference(md->map);
  587. }
  588. static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
  589. {
  590. rcu_read_unlock();
  591. }
  592. /*
  593. * Open a table device so we can use it as a map destination.
  594. */
  595. static int open_table_device(struct table_device *td, dev_t dev,
  596. struct mapped_device *md)
  597. {
  598. static char *_claim_ptr = "I belong to device-mapper";
  599. struct block_device *bdev;
  600. int r;
  601. BUG_ON(td->dm_dev.bdev);
  602. bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
  603. if (IS_ERR(bdev))
  604. return PTR_ERR(bdev);
  605. r = bd_link_disk_holder(bdev, dm_disk(md));
  606. if (r) {
  607. blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
  608. return r;
  609. }
  610. td->dm_dev.bdev = bdev;
  611. return 0;
  612. }
  613. /*
  614. * Close a table device that we've been using.
  615. */
  616. static void close_table_device(struct table_device *td, struct mapped_device *md)
  617. {
  618. if (!td->dm_dev.bdev)
  619. return;
  620. bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
  621. blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
  622. td->dm_dev.bdev = NULL;
  623. }
  624. static struct table_device *find_table_device(struct list_head *l, dev_t dev,
  625. fmode_t mode) {
  626. struct table_device *td;
  627. list_for_each_entry(td, l, list)
  628. if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
  629. return td;
  630. return NULL;
  631. }
  632. int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
  633. struct dm_dev **result) {
  634. int r;
  635. struct table_device *td;
  636. mutex_lock(&md->table_devices_lock);
  637. td = find_table_device(&md->table_devices, dev, mode);
  638. if (!td) {
  639. td = kmalloc(sizeof(*td), GFP_KERNEL);
  640. if (!td) {
  641. mutex_unlock(&md->table_devices_lock);
  642. return -ENOMEM;
  643. }
  644. td->dm_dev.mode = mode;
  645. td->dm_dev.bdev = NULL;
  646. if ((r = open_table_device(td, dev, md))) {
  647. mutex_unlock(&md->table_devices_lock);
  648. kfree(td);
  649. return r;
  650. }
  651. format_dev_t(td->dm_dev.name, dev);
  652. atomic_set(&td->count, 0);
  653. list_add(&td->list, &md->table_devices);
  654. }
  655. atomic_inc(&td->count);
  656. mutex_unlock(&md->table_devices_lock);
  657. *result = &td->dm_dev;
  658. return 0;
  659. }
  660. EXPORT_SYMBOL_GPL(dm_get_table_device);
  661. void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
  662. {
  663. struct table_device *td = container_of(d, struct table_device, dm_dev);
  664. mutex_lock(&md->table_devices_lock);
  665. if (atomic_dec_and_test(&td->count)) {
  666. close_table_device(td, md);
  667. list_del(&td->list);
  668. kfree(td);
  669. }
  670. mutex_unlock(&md->table_devices_lock);
  671. }
  672. EXPORT_SYMBOL(dm_put_table_device);
  673. static void free_table_devices(struct list_head *devices)
  674. {
  675. struct list_head *tmp, *next;
  676. list_for_each_safe(tmp, next, devices) {
  677. struct table_device *td = list_entry(tmp, struct table_device, list);
  678. DMWARN("dm_destroy: %s still exists with %d references",
  679. td->dm_dev.name, atomic_read(&td->count));
  680. kfree(td);
  681. }
  682. }
  683. /*
  684. * Get the geometry associated with a dm device
  685. */
  686. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
  687. {
  688. *geo = md->geometry;
  689. return 0;
  690. }
  691. /*
  692. * Set the geometry of a device.
  693. */
  694. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
  695. {
  696. sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
  697. if (geo->start > sz) {
  698. DMWARN("Start sector is beyond the geometry limits.");
  699. return -EINVAL;
  700. }
  701. md->geometry = *geo;
  702. return 0;
  703. }
  704. /*-----------------------------------------------------------------
  705. * CRUD START:
  706. * A more elegant soln is in the works that uses the queue
  707. * merge fn, unfortunately there are a couple of changes to
  708. * the block layer that I want to make for this. So in the
  709. * interests of getting something for people to use I give
  710. * you this clearly demarcated crap.
  711. *---------------------------------------------------------------*/
  712. static int __noflush_suspending(struct mapped_device *md)
  713. {
  714. return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  715. }
  716. /*
  717. * Decrements the number of outstanding ios that a bio has been
  718. * cloned into, completing the original io if necc.
  719. */
  720. static void dec_pending(struct dm_io *io, int error)
  721. {
  722. unsigned long flags;
  723. int io_error;
  724. struct bio *bio;
  725. struct mapped_device *md = io->md;
  726. /* Push-back supersedes any I/O errors */
  727. if (unlikely(error)) {
  728. spin_lock_irqsave(&io->endio_lock, flags);
  729. if (!(io->error > 0 && __noflush_suspending(md)))
  730. io->error = error;
  731. spin_unlock_irqrestore(&io->endio_lock, flags);
  732. }
  733. if (atomic_dec_and_test(&io->io_count)) {
  734. if (io->error == DM_ENDIO_REQUEUE) {
  735. /*
  736. * Target requested pushing back the I/O.
  737. */
  738. spin_lock_irqsave(&md->deferred_lock, flags);
  739. if (__noflush_suspending(md))
  740. bio_list_add_head(&md->deferred, io->bio);
  741. else
  742. /* noflush suspend was interrupted. */
  743. io->error = -EIO;
  744. spin_unlock_irqrestore(&md->deferred_lock, flags);
  745. }
  746. io_error = io->error;
  747. bio = io->bio;
  748. end_io_acct(io);
  749. free_io(md, io);
  750. if (io_error == DM_ENDIO_REQUEUE)
  751. return;
  752. if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
  753. /*
  754. * Preflush done for flush with data, reissue
  755. * without REQ_FLUSH.
  756. */
  757. bio->bi_rw &= ~REQ_FLUSH;
  758. queue_io(md, bio);
  759. } else {
  760. /* done with normal IO or empty flush */
  761. trace_block_bio_complete(md->queue, bio, io_error);
  762. bio_endio(bio, io_error);
  763. }
  764. }
  765. }
  766. static void disable_write_same(struct mapped_device *md)
  767. {
  768. struct queue_limits *limits = dm_get_queue_limits(md);
  769. /* device doesn't really support WRITE SAME, disable it */
  770. limits->max_write_same_sectors = 0;
  771. }
  772. static void clone_endio(struct bio *bio, int error)
  773. {
  774. int r = error;
  775. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  776. struct dm_io *io = tio->io;
  777. struct mapped_device *md = tio->io->md;
  778. dm_endio_fn endio = tio->ti->type->end_io;
  779. if (!bio_flagged(bio, BIO_UPTODATE) && !error)
  780. error = -EIO;
  781. if (endio) {
  782. r = endio(tio->ti, bio, error);
  783. if (r < 0 || r == DM_ENDIO_REQUEUE)
  784. /*
  785. * error and requeue request are handled
  786. * in dec_pending().
  787. */
  788. error = r;
  789. else if (r == DM_ENDIO_INCOMPLETE)
  790. /* The target will handle the io */
  791. return;
  792. else if (r) {
  793. DMWARN("unimplemented target endio return value: %d", r);
  794. BUG();
  795. }
  796. }
  797. if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
  798. !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
  799. disable_write_same(md);
  800. free_tio(md, tio);
  801. dec_pending(io, error);
  802. }
  803. /*
  804. * Partial completion handling for request-based dm
  805. */
  806. static void end_clone_bio(struct bio *clone, int error)
  807. {
  808. struct dm_rq_clone_bio_info *info =
  809. container_of(clone, struct dm_rq_clone_bio_info, clone);
  810. struct dm_rq_target_io *tio = info->tio;
  811. struct bio *bio = info->orig;
  812. unsigned int nr_bytes = info->orig->bi_iter.bi_size;
  813. bio_put(clone);
  814. if (tio->error)
  815. /*
  816. * An error has already been detected on the request.
  817. * Once error occurred, just let clone->end_io() handle
  818. * the remainder.
  819. */
  820. return;
  821. else if (error) {
  822. /*
  823. * Don't notice the error to the upper layer yet.
  824. * The error handling decision is made by the target driver,
  825. * when the request is completed.
  826. */
  827. tio->error = error;
  828. return;
  829. }
  830. /*
  831. * I/O for the bio successfully completed.
  832. * Notice the data completion to the upper layer.
  833. */
  834. /*
  835. * bios are processed from the head of the list.
  836. * So the completing bio should always be rq->bio.
  837. * If it's not, something wrong is happening.
  838. */
  839. if (tio->orig->bio != bio)
  840. DMERR("bio completion is going in the middle of the request");
  841. /*
  842. * Update the original request.
  843. * Do not use blk_end_request() here, because it may complete
  844. * the original request before the clone, and break the ordering.
  845. */
  846. blk_update_request(tio->orig, 0, nr_bytes);
  847. }
  848. static struct dm_rq_target_io *tio_from_request(struct request *rq)
  849. {
  850. return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
  851. }
  852. /*
  853. * Don't touch any member of the md after calling this function because
  854. * the md may be freed in dm_put() at the end of this function.
  855. * Or do dm_get() before calling this function and dm_put() later.
  856. */
  857. static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
  858. {
  859. int nr_requests_pending;
  860. atomic_dec(&md->pending[rw]);
  861. /* nudge anyone waiting on suspend queue */
  862. nr_requests_pending = md_in_flight(md);
  863. if (!nr_requests_pending)
  864. wake_up(&md->wait);
  865. /*
  866. * Run this off this callpath, as drivers could invoke end_io while
  867. * inside their request_fn (and holding the queue lock). Calling
  868. * back into ->request_fn() could deadlock attempting to grab the
  869. * queue lock again.
  870. */
  871. if (run_queue) {
  872. if (md->queue->mq_ops)
  873. blk_mq_run_hw_queues(md->queue, true);
  874. else if (!nr_requests_pending ||
  875. (nr_requests_pending >= md->queue->nr_congestion_on))
  876. blk_run_queue_async(md->queue);
  877. }
  878. /*
  879. * dm_put() must be at the end of this function. See the comment above
  880. */
  881. dm_put(md);
  882. }
  883. static void free_rq_clone(struct request *clone)
  884. {
  885. struct dm_rq_target_io *tio = clone->end_io_data;
  886. struct mapped_device *md = tio->md;
  887. blk_rq_unprep_clone(clone);
  888. if (md->type == DM_TYPE_MQ_REQUEST_BASED)
  889. /* stacked on blk-mq queue(s) */
  890. tio->ti->type->release_clone_rq(clone);
  891. else if (!md->queue->mq_ops)
  892. /* request_fn queue stacked on request_fn queue(s) */
  893. free_clone_request(md, clone);
  894. /*
  895. * NOTE: for the blk-mq queue stacked on request_fn queue(s) case:
  896. * no need to call free_clone_request() because we leverage blk-mq by
  897. * allocating the clone at the end of the blk-mq pdu (see: clone_rq)
  898. */
  899. if (!md->queue->mq_ops)
  900. free_rq_tio(tio);
  901. }
  902. /*
  903. * Complete the clone and the original request.
  904. * Must be called without clone's queue lock held,
  905. * see end_clone_request() for more details.
  906. */
  907. static void dm_end_request(struct request *clone, int error)
  908. {
  909. int rw = rq_data_dir(clone);
  910. struct dm_rq_target_io *tio = clone->end_io_data;
  911. struct mapped_device *md = tio->md;
  912. struct request *rq = tio->orig;
  913. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  914. rq->errors = clone->errors;
  915. rq->resid_len = clone->resid_len;
  916. if (rq->sense)
  917. /*
  918. * We are using the sense buffer of the original
  919. * request.
  920. * So setting the length of the sense data is enough.
  921. */
  922. rq->sense_len = clone->sense_len;
  923. }
  924. free_rq_clone(clone);
  925. if (!rq->q->mq_ops)
  926. blk_end_request_all(rq, error);
  927. else
  928. blk_mq_end_request(rq, error);
  929. rq_completed(md, rw, true);
  930. }
  931. static void dm_unprep_request(struct request *rq)
  932. {
  933. struct dm_rq_target_io *tio = tio_from_request(rq);
  934. struct request *clone = tio->clone;
  935. if (!rq->q->mq_ops) {
  936. rq->special = NULL;
  937. rq->cmd_flags &= ~REQ_DONTPREP;
  938. }
  939. if (clone)
  940. free_rq_clone(clone);
  941. }
  942. /*
  943. * Requeue the original request of a clone.
  944. */
  945. static void old_requeue_request(struct request *rq)
  946. {
  947. struct request_queue *q = rq->q;
  948. unsigned long flags;
  949. spin_lock_irqsave(q->queue_lock, flags);
  950. blk_requeue_request(q, rq);
  951. blk_run_queue_async(q);
  952. spin_unlock_irqrestore(q->queue_lock, flags);
  953. }
  954. static void dm_requeue_unmapped_original_request(struct mapped_device *md,
  955. struct request *rq)
  956. {
  957. int rw = rq_data_dir(rq);
  958. dm_unprep_request(rq);
  959. if (!rq->q->mq_ops)
  960. old_requeue_request(rq);
  961. else {
  962. blk_mq_requeue_request(rq);
  963. blk_mq_kick_requeue_list(rq->q);
  964. }
  965. rq_completed(md, rw, false);
  966. }
  967. static void dm_requeue_unmapped_request(struct request *clone)
  968. {
  969. struct dm_rq_target_io *tio = clone->end_io_data;
  970. dm_requeue_unmapped_original_request(tio->md, tio->orig);
  971. }
  972. static void old_stop_queue(struct request_queue *q)
  973. {
  974. unsigned long flags;
  975. if (blk_queue_stopped(q))
  976. return;
  977. spin_lock_irqsave(q->queue_lock, flags);
  978. blk_stop_queue(q);
  979. spin_unlock_irqrestore(q->queue_lock, flags);
  980. }
  981. static void stop_queue(struct request_queue *q)
  982. {
  983. if (!q->mq_ops)
  984. old_stop_queue(q);
  985. else
  986. blk_mq_stop_hw_queues(q);
  987. }
  988. static void old_start_queue(struct request_queue *q)
  989. {
  990. unsigned long flags;
  991. spin_lock_irqsave(q->queue_lock, flags);
  992. if (blk_queue_stopped(q))
  993. blk_start_queue(q);
  994. spin_unlock_irqrestore(q->queue_lock, flags);
  995. }
  996. static void start_queue(struct request_queue *q)
  997. {
  998. if (!q->mq_ops)
  999. old_start_queue(q);
  1000. else
  1001. blk_mq_start_stopped_hw_queues(q, true);
  1002. }
  1003. static void dm_done(struct request *clone, int error, bool mapped)
  1004. {
  1005. int r = error;
  1006. struct dm_rq_target_io *tio = clone->end_io_data;
  1007. dm_request_endio_fn rq_end_io = NULL;
  1008. if (tio->ti) {
  1009. rq_end_io = tio->ti->type->rq_end_io;
  1010. if (mapped && rq_end_io)
  1011. r = rq_end_io(tio->ti, clone, error, &tio->info);
  1012. }
  1013. if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
  1014. !clone->q->limits.max_write_same_sectors))
  1015. disable_write_same(tio->md);
  1016. if (r <= 0)
  1017. /* The target wants to complete the I/O */
  1018. dm_end_request(clone, r);
  1019. else if (r == DM_ENDIO_INCOMPLETE)
  1020. /* The target will handle the I/O */
  1021. return;
  1022. else if (r == DM_ENDIO_REQUEUE)
  1023. /* The target wants to requeue the I/O */
  1024. dm_requeue_unmapped_request(clone);
  1025. else {
  1026. DMWARN("unimplemented target endio return value: %d", r);
  1027. BUG();
  1028. }
  1029. }
  1030. /*
  1031. * Request completion handler for request-based dm
  1032. */
  1033. static void dm_softirq_done(struct request *rq)
  1034. {
  1035. bool mapped = true;
  1036. struct dm_rq_target_io *tio = tio_from_request(rq);
  1037. struct request *clone = tio->clone;
  1038. int rw;
  1039. if (!clone) {
  1040. rw = rq_data_dir(rq);
  1041. if (!rq->q->mq_ops) {
  1042. blk_end_request_all(rq, tio->error);
  1043. rq_completed(tio->md, rw, false);
  1044. free_rq_tio(tio);
  1045. } else {
  1046. blk_mq_end_request(rq, tio->error);
  1047. rq_completed(tio->md, rw, false);
  1048. }
  1049. return;
  1050. }
  1051. if (rq->cmd_flags & REQ_FAILED)
  1052. mapped = false;
  1053. dm_done(clone, tio->error, mapped);
  1054. }
  1055. /*
  1056. * Complete the clone and the original request with the error status
  1057. * through softirq context.
  1058. */
  1059. static void dm_complete_request(struct request *rq, int error)
  1060. {
  1061. struct dm_rq_target_io *tio = tio_from_request(rq);
  1062. tio->error = error;
  1063. blk_complete_request(rq);
  1064. }
  1065. /*
  1066. * Complete the not-mapped clone and the original request with the error status
  1067. * through softirq context.
  1068. * Target's rq_end_io() function isn't called.
  1069. * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
  1070. */
  1071. static void dm_kill_unmapped_request(struct request *rq, int error)
  1072. {
  1073. rq->cmd_flags |= REQ_FAILED;
  1074. dm_complete_request(rq, error);
  1075. }
  1076. /*
  1077. * Called with the clone's queue lock held (for non-blk-mq)
  1078. */
  1079. static void end_clone_request(struct request *clone, int error)
  1080. {
  1081. struct dm_rq_target_io *tio = clone->end_io_data;
  1082. if (!clone->q->mq_ops) {
  1083. /*
  1084. * For just cleaning up the information of the queue in which
  1085. * the clone was dispatched.
  1086. * The clone is *NOT* freed actually here because it is alloced
  1087. * from dm own mempool (REQ_ALLOCED isn't set).
  1088. */
  1089. __blk_put_request(clone->q, clone);
  1090. }
  1091. /*
  1092. * Actual request completion is done in a softirq context which doesn't
  1093. * hold the clone's queue lock. Otherwise, deadlock could occur because:
  1094. * - another request may be submitted by the upper level driver
  1095. * of the stacking during the completion
  1096. * - the submission which requires queue lock may be done
  1097. * against this clone's queue
  1098. */
  1099. dm_complete_request(tio->orig, error);
  1100. }
  1101. /*
  1102. * Return maximum size of I/O possible at the supplied sector up to the current
  1103. * target boundary.
  1104. */
  1105. static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
  1106. {
  1107. sector_t target_offset = dm_target_offset(ti, sector);
  1108. return ti->len - target_offset;
  1109. }
  1110. static sector_t max_io_len(sector_t sector, struct dm_target *ti)
  1111. {
  1112. sector_t len = max_io_len_target_boundary(sector, ti);
  1113. sector_t offset, max_len;
  1114. /*
  1115. * Does the target need to split even further?
  1116. */
  1117. if (ti->max_io_len) {
  1118. offset = dm_target_offset(ti, sector);
  1119. if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
  1120. max_len = sector_div(offset, ti->max_io_len);
  1121. else
  1122. max_len = offset & (ti->max_io_len - 1);
  1123. max_len = ti->max_io_len - max_len;
  1124. if (len > max_len)
  1125. len = max_len;
  1126. }
  1127. return len;
  1128. }
  1129. int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
  1130. {
  1131. if (len > UINT_MAX) {
  1132. DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
  1133. (unsigned long long)len, UINT_MAX);
  1134. ti->error = "Maximum size of target IO is too large";
  1135. return -EINVAL;
  1136. }
  1137. ti->max_io_len = (uint32_t) len;
  1138. return 0;
  1139. }
  1140. EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
  1141. /*
  1142. * A target may call dm_accept_partial_bio only from the map routine. It is
  1143. * allowed for all bio types except REQ_FLUSH.
  1144. *
  1145. * dm_accept_partial_bio informs the dm that the target only wants to process
  1146. * additional n_sectors sectors of the bio and the rest of the data should be
  1147. * sent in a next bio.
  1148. *
  1149. * A diagram that explains the arithmetics:
  1150. * +--------------------+---------------+-------+
  1151. * | 1 | 2 | 3 |
  1152. * +--------------------+---------------+-------+
  1153. *
  1154. * <-------------- *tio->len_ptr --------------->
  1155. * <------- bi_size ------->
  1156. * <-- n_sectors -->
  1157. *
  1158. * Region 1 was already iterated over with bio_advance or similar function.
  1159. * (it may be empty if the target doesn't use bio_advance)
  1160. * Region 2 is the remaining bio size that the target wants to process.
  1161. * (it may be empty if region 1 is non-empty, although there is no reason
  1162. * to make it empty)
  1163. * The target requires that region 3 is to be sent in the next bio.
  1164. *
  1165. * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
  1166. * the partially processed part (the sum of regions 1+2) must be the same for all
  1167. * copies of the bio.
  1168. */
  1169. void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
  1170. {
  1171. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  1172. unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
  1173. BUG_ON(bio->bi_rw & REQ_FLUSH);
  1174. BUG_ON(bi_size > *tio->len_ptr);
  1175. BUG_ON(n_sectors > bi_size);
  1176. *tio->len_ptr -= bi_size - n_sectors;
  1177. bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
  1178. }
  1179. EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
  1180. static void __map_bio(struct dm_target_io *tio)
  1181. {
  1182. int r;
  1183. sector_t sector;
  1184. struct mapped_device *md;
  1185. struct bio *clone = &tio->clone;
  1186. struct dm_target *ti = tio->ti;
  1187. clone->bi_end_io = clone_endio;
  1188. /*
  1189. * Map the clone. If r == 0 we don't need to do
  1190. * anything, the target has assumed ownership of
  1191. * this io.
  1192. */
  1193. atomic_inc(&tio->io->io_count);
  1194. sector = clone->bi_iter.bi_sector;
  1195. r = ti->type->map(ti, clone);
  1196. if (r == DM_MAPIO_REMAPPED) {
  1197. /* the bio has been remapped so dispatch it */
  1198. trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
  1199. tio->io->bio->bi_bdev->bd_dev, sector);
  1200. generic_make_request(clone);
  1201. } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
  1202. /* error the io and bail out, or requeue it if needed */
  1203. md = tio->io->md;
  1204. dec_pending(tio->io, r);
  1205. free_tio(md, tio);
  1206. } else if (r) {
  1207. DMWARN("unimplemented target map return value: %d", r);
  1208. BUG();
  1209. }
  1210. }
  1211. struct clone_info {
  1212. struct mapped_device *md;
  1213. struct dm_table *map;
  1214. struct bio *bio;
  1215. struct dm_io *io;
  1216. sector_t sector;
  1217. unsigned sector_count;
  1218. };
  1219. static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
  1220. {
  1221. bio->bi_iter.bi_sector = sector;
  1222. bio->bi_iter.bi_size = to_bytes(len);
  1223. }
  1224. /*
  1225. * Creates a bio that consists of range of complete bvecs.
  1226. */
  1227. static void clone_bio(struct dm_target_io *tio, struct bio *bio,
  1228. sector_t sector, unsigned len)
  1229. {
  1230. struct bio *clone = &tio->clone;
  1231. __bio_clone_fast(clone, bio);
  1232. if (bio_integrity(bio))
  1233. bio_integrity_clone(clone, bio, GFP_NOIO);
  1234. bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
  1235. clone->bi_iter.bi_size = to_bytes(len);
  1236. if (bio_integrity(bio))
  1237. bio_integrity_trim(clone, 0, len);
  1238. }
  1239. static struct dm_target_io *alloc_tio(struct clone_info *ci,
  1240. struct dm_target *ti,
  1241. unsigned target_bio_nr)
  1242. {
  1243. struct dm_target_io *tio;
  1244. struct bio *clone;
  1245. clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
  1246. tio = container_of(clone, struct dm_target_io, clone);
  1247. tio->io = ci->io;
  1248. tio->ti = ti;
  1249. tio->target_bio_nr = target_bio_nr;
  1250. return tio;
  1251. }
  1252. static void __clone_and_map_simple_bio(struct clone_info *ci,
  1253. struct dm_target *ti,
  1254. unsigned target_bio_nr, unsigned *len)
  1255. {
  1256. struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
  1257. struct bio *clone = &tio->clone;
  1258. tio->len_ptr = len;
  1259. __bio_clone_fast(clone, ci->bio);
  1260. if (len)
  1261. bio_setup_sector(clone, ci->sector, *len);
  1262. __map_bio(tio);
  1263. }
  1264. static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
  1265. unsigned num_bios, unsigned *len)
  1266. {
  1267. unsigned target_bio_nr;
  1268. for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
  1269. __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
  1270. }
  1271. static int __send_empty_flush(struct clone_info *ci)
  1272. {
  1273. unsigned target_nr = 0;
  1274. struct dm_target *ti;
  1275. BUG_ON(bio_has_data(ci->bio));
  1276. while ((ti = dm_table_get_target(ci->map, target_nr++)))
  1277. __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
  1278. return 0;
  1279. }
  1280. static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
  1281. sector_t sector, unsigned *len)
  1282. {
  1283. struct bio *bio = ci->bio;
  1284. struct dm_target_io *tio;
  1285. unsigned target_bio_nr;
  1286. unsigned num_target_bios = 1;
  1287. /*
  1288. * Does the target want to receive duplicate copies of the bio?
  1289. */
  1290. if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
  1291. num_target_bios = ti->num_write_bios(ti, bio);
  1292. for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
  1293. tio = alloc_tio(ci, ti, target_bio_nr);
  1294. tio->len_ptr = len;
  1295. clone_bio(tio, bio, sector, *len);
  1296. __map_bio(tio);
  1297. }
  1298. }
  1299. typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
  1300. static unsigned get_num_discard_bios(struct dm_target *ti)
  1301. {
  1302. return ti->num_discard_bios;
  1303. }
  1304. static unsigned get_num_write_same_bios(struct dm_target *ti)
  1305. {
  1306. return ti->num_write_same_bios;
  1307. }
  1308. typedef bool (*is_split_required_fn)(struct dm_target *ti);
  1309. static bool is_split_required_for_discard(struct dm_target *ti)
  1310. {
  1311. return ti->split_discard_bios;
  1312. }
  1313. static int __send_changing_extent_only(struct clone_info *ci,
  1314. get_num_bios_fn get_num_bios,
  1315. is_split_required_fn is_split_required)
  1316. {
  1317. struct dm_target *ti;
  1318. unsigned len;
  1319. unsigned num_bios;
  1320. do {
  1321. ti = dm_table_find_target(ci->map, ci->sector);
  1322. if (!dm_target_is_valid(ti))
  1323. return -EIO;
  1324. /*
  1325. * Even though the device advertised support for this type of
  1326. * request, that does not mean every target supports it, and
  1327. * reconfiguration might also have changed that since the
  1328. * check was performed.
  1329. */
  1330. num_bios = get_num_bios ? get_num_bios(ti) : 0;
  1331. if (!num_bios)
  1332. return -EOPNOTSUPP;
  1333. if (is_split_required && !is_split_required(ti))
  1334. len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
  1335. else
  1336. len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
  1337. __send_duplicate_bios(ci, ti, num_bios, &len);
  1338. ci->sector += len;
  1339. } while (ci->sector_count -= len);
  1340. return 0;
  1341. }
  1342. static int __send_discard(struct clone_info *ci)
  1343. {
  1344. return __send_changing_extent_only(ci, get_num_discard_bios,
  1345. is_split_required_for_discard);
  1346. }
  1347. static int __send_write_same(struct clone_info *ci)
  1348. {
  1349. return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
  1350. }
  1351. /*
  1352. * Select the correct strategy for processing a non-flush bio.
  1353. */
  1354. static int __split_and_process_non_flush(struct clone_info *ci)
  1355. {
  1356. struct bio *bio = ci->bio;
  1357. struct dm_target *ti;
  1358. unsigned len;
  1359. if (unlikely(bio->bi_rw & REQ_DISCARD))
  1360. return __send_discard(ci);
  1361. else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
  1362. return __send_write_same(ci);
  1363. ti = dm_table_find_target(ci->map, ci->sector);
  1364. if (!dm_target_is_valid(ti))
  1365. return -EIO;
  1366. len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
  1367. __clone_and_map_data_bio(ci, ti, ci->sector, &len);
  1368. ci->sector += len;
  1369. ci->sector_count -= len;
  1370. return 0;
  1371. }
  1372. /*
  1373. * Entry point to split a bio into clones and submit them to the targets.
  1374. */
  1375. static void __split_and_process_bio(struct mapped_device *md,
  1376. struct dm_table *map, struct bio *bio)
  1377. {
  1378. struct clone_info ci;
  1379. int error = 0;
  1380. if (unlikely(!map)) {
  1381. bio_io_error(bio);
  1382. return;
  1383. }
  1384. ci.map = map;
  1385. ci.md = md;
  1386. ci.io = alloc_io(md);
  1387. ci.io->error = 0;
  1388. atomic_set(&ci.io->io_count, 1);
  1389. ci.io->bio = bio;
  1390. ci.io->md = md;
  1391. spin_lock_init(&ci.io->endio_lock);
  1392. ci.sector = bio->bi_iter.bi_sector;
  1393. start_io_acct(ci.io);
  1394. if (bio->bi_rw & REQ_FLUSH) {
  1395. ci.bio = &ci.md->flush_bio;
  1396. ci.sector_count = 0;
  1397. error = __send_empty_flush(&ci);
  1398. /* dec_pending submits any data associated with flush */
  1399. } else {
  1400. ci.bio = bio;
  1401. ci.sector_count = bio_sectors(bio);
  1402. while (ci.sector_count && !error)
  1403. error = __split_and_process_non_flush(&ci);
  1404. }
  1405. /* drop the extra reference count */
  1406. dec_pending(ci.io, error);
  1407. }
  1408. /*-----------------------------------------------------------------
  1409. * CRUD END
  1410. *---------------------------------------------------------------*/
  1411. static int dm_merge_bvec(struct request_queue *q,
  1412. struct bvec_merge_data *bvm,
  1413. struct bio_vec *biovec)
  1414. {
  1415. struct mapped_device *md = q->queuedata;
  1416. struct dm_table *map = dm_get_live_table_fast(md);
  1417. struct dm_target *ti;
  1418. sector_t max_sectors, max_size = 0;
  1419. if (unlikely(!map))
  1420. goto out;
  1421. ti = dm_table_find_target(map, bvm->bi_sector);
  1422. if (!dm_target_is_valid(ti))
  1423. goto out;
  1424. /*
  1425. * Find maximum amount of I/O that won't need splitting
  1426. */
  1427. max_sectors = min(max_io_len(bvm->bi_sector, ti),
  1428. (sector_t) queue_max_sectors(q));
  1429. max_size = (max_sectors << SECTOR_SHIFT) - bvm->bi_size;
  1430. /*
  1431. * FIXME: this stop-gap fix _must_ be cleaned up (by passing a sector_t
  1432. * to the targets' merge function since it holds sectors not bytes).
  1433. * Just doing this as an interim fix for stable@ because the more
  1434. * comprehensive cleanup of switching to sector_t will impact every
  1435. * DM target that implements a ->merge hook.
  1436. */
  1437. if (max_size > INT_MAX)
  1438. max_size = INT_MAX;
  1439. /*
  1440. * merge_bvec_fn() returns number of bytes
  1441. * it can accept at this offset
  1442. * max is precomputed maximal io size
  1443. */
  1444. if (max_size && ti->type->merge)
  1445. max_size = ti->type->merge(ti, bvm, biovec, (int) max_size);
  1446. /*
  1447. * If the target doesn't support merge method and some of the devices
  1448. * provided their merge_bvec method (we know this by looking for the
  1449. * max_hw_sectors that dm_set_device_limits may set), then we can't
  1450. * allow bios with multiple vector entries. So always set max_size
  1451. * to 0, and the code below allows just one page.
  1452. */
  1453. else if (queue_max_hw_sectors(q) <= PAGE_SIZE >> 9)
  1454. max_size = 0;
  1455. out:
  1456. dm_put_live_table_fast(md);
  1457. /*
  1458. * Always allow an entire first page
  1459. */
  1460. if (max_size <= biovec->bv_len && !(bvm->bi_size >> SECTOR_SHIFT))
  1461. max_size = biovec->bv_len;
  1462. return max_size;
  1463. }
  1464. /*
  1465. * The request function that just remaps the bio built up by
  1466. * dm_merge_bvec.
  1467. */
  1468. static void dm_make_request(struct request_queue *q, struct bio *bio)
  1469. {
  1470. int rw = bio_data_dir(bio);
  1471. struct mapped_device *md = q->queuedata;
  1472. int srcu_idx;
  1473. struct dm_table *map;
  1474. map = dm_get_live_table(md, &srcu_idx);
  1475. generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
  1476. /* if we're suspended, we have to queue this io for later */
  1477. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
  1478. dm_put_live_table(md, srcu_idx);
  1479. if (bio_rw(bio) != READA)
  1480. queue_io(md, bio);
  1481. else
  1482. bio_io_error(bio);
  1483. return;
  1484. }
  1485. __split_and_process_bio(md, map, bio);
  1486. dm_put_live_table(md, srcu_idx);
  1487. return;
  1488. }
  1489. int dm_request_based(struct mapped_device *md)
  1490. {
  1491. return blk_queue_stackable(md->queue);
  1492. }
  1493. static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
  1494. {
  1495. int r;
  1496. if (blk_queue_io_stat(clone->q))
  1497. clone->cmd_flags |= REQ_IO_STAT;
  1498. clone->start_time = jiffies;
  1499. r = blk_insert_cloned_request(clone->q, clone);
  1500. if (r)
  1501. /* must complete clone in terms of original request */
  1502. dm_complete_request(rq, r);
  1503. }
  1504. static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
  1505. void *data)
  1506. {
  1507. struct dm_rq_target_io *tio = data;
  1508. struct dm_rq_clone_bio_info *info =
  1509. container_of(bio, struct dm_rq_clone_bio_info, clone);
  1510. info->orig = bio_orig;
  1511. info->tio = tio;
  1512. bio->bi_end_io = end_clone_bio;
  1513. return 0;
  1514. }
  1515. static int setup_clone(struct request *clone, struct request *rq,
  1516. struct dm_rq_target_io *tio, gfp_t gfp_mask)
  1517. {
  1518. int r;
  1519. r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
  1520. dm_rq_bio_constructor, tio);
  1521. if (r)
  1522. return r;
  1523. clone->cmd = rq->cmd;
  1524. clone->cmd_len = rq->cmd_len;
  1525. clone->sense = rq->sense;
  1526. clone->end_io = end_clone_request;
  1527. clone->end_io_data = tio;
  1528. tio->clone = clone;
  1529. return 0;
  1530. }
  1531. static struct request *clone_rq(struct request *rq, struct mapped_device *md,
  1532. struct dm_rq_target_io *tio, gfp_t gfp_mask)
  1533. {
  1534. /*
  1535. * Do not allocate a clone if tio->clone was already set
  1536. * (see: dm_mq_queue_rq).
  1537. */
  1538. bool alloc_clone = !tio->clone;
  1539. struct request *clone;
  1540. if (alloc_clone) {
  1541. clone = alloc_clone_request(md, gfp_mask);
  1542. if (!clone)
  1543. return NULL;
  1544. } else
  1545. clone = tio->clone;
  1546. blk_rq_init(NULL, clone);
  1547. if (setup_clone(clone, rq, tio, gfp_mask)) {
  1548. /* -ENOMEM */
  1549. if (alloc_clone)
  1550. free_clone_request(md, clone);
  1551. return NULL;
  1552. }
  1553. return clone;
  1554. }
  1555. static void map_tio_request(struct kthread_work *work);
  1556. static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
  1557. struct mapped_device *md)
  1558. {
  1559. tio->md = md;
  1560. tio->ti = NULL;
  1561. tio->clone = NULL;
  1562. tio->orig = rq;
  1563. tio->error = 0;
  1564. memset(&tio->info, 0, sizeof(tio->info));
  1565. if (md->kworker_task)
  1566. init_kthread_work(&tio->work, map_tio_request);
  1567. }
  1568. static struct dm_rq_target_io *prep_tio(struct request *rq,
  1569. struct mapped_device *md, gfp_t gfp_mask)
  1570. {
  1571. struct dm_rq_target_io *tio;
  1572. int srcu_idx;
  1573. struct dm_table *table;
  1574. tio = alloc_rq_tio(md, gfp_mask);
  1575. if (!tio)
  1576. return NULL;
  1577. init_tio(tio, rq, md);
  1578. table = dm_get_live_table(md, &srcu_idx);
  1579. if (!dm_table_mq_request_based(table)) {
  1580. if (!clone_rq(rq, md, tio, gfp_mask)) {
  1581. dm_put_live_table(md, srcu_idx);
  1582. free_rq_tio(tio);
  1583. return NULL;
  1584. }
  1585. }
  1586. dm_put_live_table(md, srcu_idx);
  1587. return tio;
  1588. }
  1589. /*
  1590. * Called with the queue lock held.
  1591. */
  1592. static int dm_prep_fn(struct request_queue *q, struct request *rq)
  1593. {
  1594. struct mapped_device *md = q->queuedata;
  1595. struct dm_rq_target_io *tio;
  1596. if (unlikely(rq->special)) {
  1597. DMWARN("Already has something in rq->special.");
  1598. return BLKPREP_KILL;
  1599. }
  1600. tio = prep_tio(rq, md, GFP_ATOMIC);
  1601. if (!tio)
  1602. return BLKPREP_DEFER;
  1603. rq->special = tio;
  1604. rq->cmd_flags |= REQ_DONTPREP;
  1605. return BLKPREP_OK;
  1606. }
  1607. /*
  1608. * Returns:
  1609. * 0 : the request has been processed
  1610. * DM_MAPIO_REQUEUE : the original request needs to be requeued
  1611. * < 0 : the request was completed due to failure
  1612. */
  1613. static int map_request(struct dm_rq_target_io *tio, struct request *rq,
  1614. struct mapped_device *md)
  1615. {
  1616. int r;
  1617. struct dm_target *ti = tio->ti;
  1618. struct request *clone = NULL;
  1619. if (tio->clone) {
  1620. clone = tio->clone;
  1621. r = ti->type->map_rq(ti, clone, &tio->info);
  1622. } else {
  1623. r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
  1624. if (r < 0) {
  1625. /* The target wants to complete the I/O */
  1626. dm_kill_unmapped_request(rq, r);
  1627. return r;
  1628. }
  1629. if (r != DM_MAPIO_REMAPPED)
  1630. return r;
  1631. if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
  1632. /* -ENOMEM */
  1633. ti->type->release_clone_rq(clone);
  1634. return DM_MAPIO_REQUEUE;
  1635. }
  1636. }
  1637. switch (r) {
  1638. case DM_MAPIO_SUBMITTED:
  1639. /* The target has taken the I/O to submit by itself later */
  1640. break;
  1641. case DM_MAPIO_REMAPPED:
  1642. /* The target has remapped the I/O so dispatch it */
  1643. trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
  1644. blk_rq_pos(rq));
  1645. dm_dispatch_clone_request(clone, rq);
  1646. break;
  1647. case DM_MAPIO_REQUEUE:
  1648. /* The target wants to requeue the I/O */
  1649. dm_requeue_unmapped_request(clone);
  1650. break;
  1651. default:
  1652. if (r > 0) {
  1653. DMWARN("unimplemented target map return value: %d", r);
  1654. BUG();
  1655. }
  1656. /* The target wants to complete the I/O */
  1657. dm_kill_unmapped_request(rq, r);
  1658. return r;
  1659. }
  1660. return 0;
  1661. }
  1662. static void map_tio_request(struct kthread_work *work)
  1663. {
  1664. struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
  1665. struct request *rq = tio->orig;
  1666. struct mapped_device *md = tio->md;
  1667. if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
  1668. dm_requeue_unmapped_original_request(md, rq);
  1669. }
  1670. static void dm_start_request(struct mapped_device *md, struct request *orig)
  1671. {
  1672. if (!orig->q->mq_ops)
  1673. blk_start_request(orig);
  1674. else
  1675. blk_mq_start_request(orig);
  1676. atomic_inc(&md->pending[rq_data_dir(orig)]);
  1677. if (md->seq_rq_merge_deadline_usecs) {
  1678. md->last_rq_pos = rq_end_sector(orig);
  1679. md->last_rq_rw = rq_data_dir(orig);
  1680. md->last_rq_start_time = ktime_get();
  1681. }
  1682. /*
  1683. * Hold the md reference here for the in-flight I/O.
  1684. * We can't rely on the reference count by device opener,
  1685. * because the device may be closed during the request completion
  1686. * when all bios are completed.
  1687. * See the comment in rq_completed() too.
  1688. */
  1689. dm_get(md);
  1690. }
  1691. #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
  1692. ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
  1693. {
  1694. return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
  1695. }
  1696. ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
  1697. const char *buf, size_t count)
  1698. {
  1699. unsigned deadline;
  1700. if (!dm_request_based(md) || md->use_blk_mq)
  1701. return count;
  1702. if (kstrtouint(buf, 10, &deadline))
  1703. return -EINVAL;
  1704. if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
  1705. deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
  1706. md->seq_rq_merge_deadline_usecs = deadline;
  1707. return count;
  1708. }
  1709. static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
  1710. {
  1711. ktime_t kt_deadline;
  1712. if (!md->seq_rq_merge_deadline_usecs)
  1713. return false;
  1714. kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
  1715. kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
  1716. return !ktime_after(ktime_get(), kt_deadline);
  1717. }
  1718. /*
  1719. * q->request_fn for request-based dm.
  1720. * Called with the queue lock held.
  1721. */
  1722. static void dm_request_fn(struct request_queue *q)
  1723. {
  1724. struct mapped_device *md = q->queuedata;
  1725. int srcu_idx;
  1726. struct dm_table *map = dm_get_live_table(md, &srcu_idx);
  1727. struct dm_target *ti;
  1728. struct request *rq;
  1729. struct dm_rq_target_io *tio;
  1730. sector_t pos;
  1731. /*
  1732. * For suspend, check blk_queue_stopped() and increment
  1733. * ->pending within a single queue_lock not to increment the
  1734. * number of in-flight I/Os after the queue is stopped in
  1735. * dm_suspend().
  1736. */
  1737. while (!blk_queue_stopped(q)) {
  1738. rq = blk_peek_request(q);
  1739. if (!rq)
  1740. goto out;
  1741. /* always use block 0 to find the target for flushes for now */
  1742. pos = 0;
  1743. if (!(rq->cmd_flags & REQ_FLUSH))
  1744. pos = blk_rq_pos(rq);
  1745. ti = dm_table_find_target(map, pos);
  1746. if (!dm_target_is_valid(ti)) {
  1747. /*
  1748. * Must perform setup, that rq_completed() requires,
  1749. * before calling dm_kill_unmapped_request
  1750. */
  1751. DMERR_LIMIT("request attempted access beyond the end of device");
  1752. dm_start_request(md, rq);
  1753. dm_kill_unmapped_request(rq, -EIO);
  1754. continue;
  1755. }
  1756. if (dm_request_peeked_before_merge_deadline(md) &&
  1757. md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
  1758. md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq))
  1759. goto delay_and_out;
  1760. if (ti->type->busy && ti->type->busy(ti))
  1761. goto delay_and_out;
  1762. dm_start_request(md, rq);
  1763. tio = tio_from_request(rq);
  1764. /* Establish tio->ti before queuing work (map_tio_request) */
  1765. tio->ti = ti;
  1766. queue_kthread_work(&md->kworker, &tio->work);
  1767. BUG_ON(!irqs_disabled());
  1768. }
  1769. goto out;
  1770. delay_and_out:
  1771. blk_delay_queue(q, HZ / 100);
  1772. out:
  1773. dm_put_live_table(md, srcu_idx);
  1774. }
  1775. static int dm_any_congested(void *congested_data, int bdi_bits)
  1776. {
  1777. int r = bdi_bits;
  1778. struct mapped_device *md = congested_data;
  1779. struct dm_table *map;
  1780. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1781. map = dm_get_live_table_fast(md);
  1782. if (map) {
  1783. /*
  1784. * Request-based dm cares about only own queue for
  1785. * the query about congestion status of request_queue
  1786. */
  1787. if (dm_request_based(md))
  1788. r = md->queue->backing_dev_info.state &
  1789. bdi_bits;
  1790. else
  1791. r = dm_table_any_congested(map, bdi_bits);
  1792. }
  1793. dm_put_live_table_fast(md);
  1794. }
  1795. return r;
  1796. }
  1797. /*-----------------------------------------------------------------
  1798. * An IDR is used to keep track of allocated minor numbers.
  1799. *---------------------------------------------------------------*/
  1800. static void free_minor(int minor)
  1801. {
  1802. spin_lock(&_minor_lock);
  1803. idr_remove(&_minor_idr, minor);
  1804. spin_unlock(&_minor_lock);
  1805. }
  1806. /*
  1807. * See if the device with a specific minor # is free.
  1808. */
  1809. static int specific_minor(int minor)
  1810. {
  1811. int r;
  1812. if (minor >= (1 << MINORBITS))
  1813. return -EINVAL;
  1814. idr_preload(GFP_KERNEL);
  1815. spin_lock(&_minor_lock);
  1816. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
  1817. spin_unlock(&_minor_lock);
  1818. idr_preload_end();
  1819. if (r < 0)
  1820. return r == -ENOSPC ? -EBUSY : r;
  1821. return 0;
  1822. }
  1823. static int next_free_minor(int *minor)
  1824. {
  1825. int r;
  1826. idr_preload(GFP_KERNEL);
  1827. spin_lock(&_minor_lock);
  1828. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
  1829. spin_unlock(&_minor_lock);
  1830. idr_preload_end();
  1831. if (r < 0)
  1832. return r;
  1833. *minor = r;
  1834. return 0;
  1835. }
  1836. static const struct block_device_operations dm_blk_dops;
  1837. static void dm_wq_work(struct work_struct *work);
  1838. static void dm_init_md_queue(struct mapped_device *md)
  1839. {
  1840. /*
  1841. * Request-based dm devices cannot be stacked on top of bio-based dm
  1842. * devices. The type of this dm device may not have been decided yet.
  1843. * The type is decided at the first table loading time.
  1844. * To prevent problematic device stacking, clear the queue flag
  1845. * for request stacking support until then.
  1846. *
  1847. * This queue is new, so no concurrency on the queue_flags.
  1848. */
  1849. queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
  1850. }
  1851. static void dm_init_old_md_queue(struct mapped_device *md)
  1852. {
  1853. md->use_blk_mq = false;
  1854. dm_init_md_queue(md);
  1855. /*
  1856. * Initialize aspects of queue that aren't relevant for blk-mq
  1857. */
  1858. md->queue->queuedata = md;
  1859. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  1860. md->queue->backing_dev_info.congested_data = md;
  1861. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  1862. }
  1863. /*
  1864. * Allocate and initialise a blank device with a given minor.
  1865. */
  1866. static struct mapped_device *alloc_dev(int minor)
  1867. {
  1868. int r;
  1869. struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
  1870. void *old_md;
  1871. if (!md) {
  1872. DMWARN("unable to allocate device, out of memory.");
  1873. return NULL;
  1874. }
  1875. if (!try_module_get(THIS_MODULE))
  1876. goto bad_module_get;
  1877. /* get a minor number for the dev */
  1878. if (minor == DM_ANY_MINOR)
  1879. r = next_free_minor(&minor);
  1880. else
  1881. r = specific_minor(minor);
  1882. if (r < 0)
  1883. goto bad_minor;
  1884. r = init_srcu_struct(&md->io_barrier);
  1885. if (r < 0)
  1886. goto bad_io_barrier;
  1887. md->use_blk_mq = use_blk_mq;
  1888. md->type = DM_TYPE_NONE;
  1889. mutex_init(&md->suspend_lock);
  1890. mutex_init(&md->type_lock);
  1891. mutex_init(&md->table_devices_lock);
  1892. spin_lock_init(&md->deferred_lock);
  1893. atomic_set(&md->holders, 1);
  1894. atomic_set(&md->open_count, 0);
  1895. atomic_set(&md->event_nr, 0);
  1896. atomic_set(&md->uevent_seq, 0);
  1897. INIT_LIST_HEAD(&md->uevent_list);
  1898. INIT_LIST_HEAD(&md->table_devices);
  1899. spin_lock_init(&md->uevent_lock);
  1900. md->queue = blk_alloc_queue(GFP_KERNEL);
  1901. if (!md->queue)
  1902. goto bad_queue;
  1903. dm_init_md_queue(md);
  1904. md->disk = alloc_disk(1);
  1905. if (!md->disk)
  1906. goto bad_disk;
  1907. atomic_set(&md->pending[0], 0);
  1908. atomic_set(&md->pending[1], 0);
  1909. init_waitqueue_head(&md->wait);
  1910. INIT_WORK(&md->work, dm_wq_work);
  1911. init_waitqueue_head(&md->eventq);
  1912. init_completion(&md->kobj_holder.completion);
  1913. md->kworker_task = NULL;
  1914. md->disk->major = _major;
  1915. md->disk->first_minor = minor;
  1916. md->disk->fops = &dm_blk_dops;
  1917. md->disk->queue = md->queue;
  1918. md->disk->private_data = md;
  1919. sprintf(md->disk->disk_name, "dm-%d", minor);
  1920. add_disk(md->disk);
  1921. format_dev_t(md->name, MKDEV(_major, minor));
  1922. md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
  1923. if (!md->wq)
  1924. goto bad_thread;
  1925. md->bdev = bdget_disk(md->disk, 0);
  1926. if (!md->bdev)
  1927. goto bad_bdev;
  1928. bio_init(&md->flush_bio);
  1929. md->flush_bio.bi_bdev = md->bdev;
  1930. md->flush_bio.bi_rw = WRITE_FLUSH;
  1931. dm_stats_init(&md->stats);
  1932. /* Populate the mapping, nobody knows we exist yet */
  1933. spin_lock(&_minor_lock);
  1934. old_md = idr_replace(&_minor_idr, md, minor);
  1935. spin_unlock(&_minor_lock);
  1936. BUG_ON(old_md != MINOR_ALLOCED);
  1937. return md;
  1938. bad_bdev:
  1939. destroy_workqueue(md->wq);
  1940. bad_thread:
  1941. del_gendisk(md->disk);
  1942. put_disk(md->disk);
  1943. bad_disk:
  1944. blk_cleanup_queue(md->queue);
  1945. bad_queue:
  1946. cleanup_srcu_struct(&md->io_barrier);
  1947. bad_io_barrier:
  1948. free_minor(minor);
  1949. bad_minor:
  1950. module_put(THIS_MODULE);
  1951. bad_module_get:
  1952. kfree(md);
  1953. return NULL;
  1954. }
  1955. static void unlock_fs(struct mapped_device *md);
  1956. static void free_dev(struct mapped_device *md)
  1957. {
  1958. int minor = MINOR(disk_devt(md->disk));
  1959. unlock_fs(md);
  1960. destroy_workqueue(md->wq);
  1961. if (md->kworker_task)
  1962. kthread_stop(md->kworker_task);
  1963. if (md->io_pool)
  1964. mempool_destroy(md->io_pool);
  1965. if (md->rq_pool)
  1966. mempool_destroy(md->rq_pool);
  1967. if (md->bs)
  1968. bioset_free(md->bs);
  1969. cleanup_srcu_struct(&md->io_barrier);
  1970. free_table_devices(&md->table_devices);
  1971. dm_stats_cleanup(&md->stats);
  1972. spin_lock(&_minor_lock);
  1973. md->disk->private_data = NULL;
  1974. spin_unlock(&_minor_lock);
  1975. if (blk_get_integrity(md->disk))
  1976. blk_integrity_unregister(md->disk);
  1977. del_gendisk(md->disk);
  1978. put_disk(md->disk);
  1979. blk_cleanup_queue(md->queue);
  1980. if (md->use_blk_mq)
  1981. blk_mq_free_tag_set(&md->tag_set);
  1982. bdput(md->bdev);
  1983. free_minor(minor);
  1984. module_put(THIS_MODULE);
  1985. kfree(md);
  1986. }
  1987. static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
  1988. {
  1989. struct dm_md_mempools *p = dm_table_get_md_mempools(t);
  1990. if (md->bs) {
  1991. /* The md already has necessary mempools. */
  1992. if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
  1993. /*
  1994. * Reload bioset because front_pad may have changed
  1995. * because a different table was loaded.
  1996. */
  1997. bioset_free(md->bs);
  1998. md->bs = p->bs;
  1999. p->bs = NULL;
  2000. }
  2001. /*
  2002. * There's no need to reload with request-based dm
  2003. * because the size of front_pad doesn't change.
  2004. * Note for future: If you are to reload bioset,
  2005. * prep-ed requests in the queue may refer
  2006. * to bio from the old bioset, so you must walk
  2007. * through the queue to unprep.
  2008. */
  2009. goto out;
  2010. }
  2011. BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
  2012. md->io_pool = p->io_pool;
  2013. p->io_pool = NULL;
  2014. md->rq_pool = p->rq_pool;
  2015. p->rq_pool = NULL;
  2016. md->bs = p->bs;
  2017. p->bs = NULL;
  2018. out:
  2019. /* mempool bind completed, no longer need any mempools in the table */
  2020. dm_table_free_md_mempools(t);
  2021. }
  2022. /*
  2023. * Bind a table to the device.
  2024. */
  2025. static void event_callback(void *context)
  2026. {
  2027. unsigned long flags;
  2028. LIST_HEAD(uevents);
  2029. struct mapped_device *md = (struct mapped_device *) context;
  2030. spin_lock_irqsave(&md->uevent_lock, flags);
  2031. list_splice_init(&md->uevent_list, &uevents);
  2032. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2033. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  2034. atomic_inc(&md->event_nr);
  2035. wake_up(&md->eventq);
  2036. }
  2037. /*
  2038. * Protected by md->suspend_lock obtained by dm_swap_table().
  2039. */
  2040. static void __set_size(struct mapped_device *md, sector_t size)
  2041. {
  2042. set_capacity(md->disk, size);
  2043. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  2044. }
  2045. /*
  2046. * Return 1 if the queue has a compulsory merge_bvec_fn function.
  2047. *
  2048. * If this function returns 0, then the device is either a non-dm
  2049. * device without a merge_bvec_fn, or it is a dm device that is
  2050. * able to split any bios it receives that are too big.
  2051. */
  2052. int dm_queue_merge_is_compulsory(struct request_queue *q)
  2053. {
  2054. struct mapped_device *dev_md;
  2055. if (!q->merge_bvec_fn)
  2056. return 0;
  2057. if (q->make_request_fn == dm_make_request) {
  2058. dev_md = q->queuedata;
  2059. if (test_bit(DMF_MERGE_IS_OPTIONAL, &dev_md->flags))
  2060. return 0;
  2061. }
  2062. return 1;
  2063. }
  2064. static int dm_device_merge_is_compulsory(struct dm_target *ti,
  2065. struct dm_dev *dev, sector_t start,
  2066. sector_t len, void *data)
  2067. {
  2068. struct block_device *bdev = dev->bdev;
  2069. struct request_queue *q = bdev_get_queue(bdev);
  2070. return dm_queue_merge_is_compulsory(q);
  2071. }
  2072. /*
  2073. * Return 1 if it is acceptable to ignore merge_bvec_fn based
  2074. * on the properties of the underlying devices.
  2075. */
  2076. static int dm_table_merge_is_optional(struct dm_table *table)
  2077. {
  2078. unsigned i = 0;
  2079. struct dm_target *ti;
  2080. while (i < dm_table_get_num_targets(table)) {
  2081. ti = dm_table_get_target(table, i++);
  2082. if (ti->type->iterate_devices &&
  2083. ti->type->iterate_devices(ti, dm_device_merge_is_compulsory, NULL))
  2084. return 0;
  2085. }
  2086. return 1;
  2087. }
  2088. /*
  2089. * Returns old map, which caller must destroy.
  2090. */
  2091. static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
  2092. struct queue_limits *limits)
  2093. {
  2094. struct dm_table *old_map;
  2095. struct request_queue *q = md->queue;
  2096. sector_t size;
  2097. int merge_is_optional;
  2098. size = dm_table_get_size(t);
  2099. /*
  2100. * Wipe any geometry if the size of the table changed.
  2101. */
  2102. if (size != dm_get_size(md))
  2103. memset(&md->geometry, 0, sizeof(md->geometry));
  2104. __set_size(md, size);
  2105. dm_table_event_callback(t, event_callback, md);
  2106. /*
  2107. * The queue hasn't been stopped yet, if the old table type wasn't
  2108. * for request-based during suspension. So stop it to prevent
  2109. * I/O mapping before resume.
  2110. * This must be done before setting the queue restrictions,
  2111. * because request-based dm may be run just after the setting.
  2112. */
  2113. if (dm_table_request_based(t))
  2114. stop_queue(q);
  2115. __bind_mempools(md, t);
  2116. merge_is_optional = dm_table_merge_is_optional(t);
  2117. old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2118. rcu_assign_pointer(md->map, t);
  2119. md->immutable_target_type = dm_table_get_immutable_target_type(t);
  2120. dm_table_set_restrictions(t, q, limits);
  2121. if (merge_is_optional)
  2122. set_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
  2123. else
  2124. clear_bit(DMF_MERGE_IS_OPTIONAL, &md->flags);
  2125. if (old_map)
  2126. dm_sync_table(md);
  2127. return old_map;
  2128. }
  2129. /*
  2130. * Returns unbound table for the caller to free.
  2131. */
  2132. static struct dm_table *__unbind(struct mapped_device *md)
  2133. {
  2134. struct dm_table *map = rcu_dereference_protected(md->map, 1);
  2135. if (!map)
  2136. return NULL;
  2137. dm_table_event_callback(map, NULL, NULL);
  2138. RCU_INIT_POINTER(md->map, NULL);
  2139. dm_sync_table(md);
  2140. return map;
  2141. }
  2142. /*
  2143. * Constructor for a new device.
  2144. */
  2145. int dm_create(int minor, struct mapped_device **result)
  2146. {
  2147. struct mapped_device *md;
  2148. md = alloc_dev(minor);
  2149. if (!md)
  2150. return -ENXIO;
  2151. dm_sysfs_init(md);
  2152. *result = md;
  2153. return 0;
  2154. }
  2155. /*
  2156. * Functions to manage md->type.
  2157. * All are required to hold md->type_lock.
  2158. */
  2159. void dm_lock_md_type(struct mapped_device *md)
  2160. {
  2161. mutex_lock(&md->type_lock);
  2162. }
  2163. void dm_unlock_md_type(struct mapped_device *md)
  2164. {
  2165. mutex_unlock(&md->type_lock);
  2166. }
  2167. void dm_set_md_type(struct mapped_device *md, unsigned type)
  2168. {
  2169. BUG_ON(!mutex_is_locked(&md->type_lock));
  2170. md->type = type;
  2171. }
  2172. unsigned dm_get_md_type(struct mapped_device *md)
  2173. {
  2174. BUG_ON(!mutex_is_locked(&md->type_lock));
  2175. return md->type;
  2176. }
  2177. struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
  2178. {
  2179. return md->immutable_target_type;
  2180. }
  2181. /*
  2182. * The queue_limits are only valid as long as you have a reference
  2183. * count on 'md'.
  2184. */
  2185. struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
  2186. {
  2187. BUG_ON(!atomic_read(&md->holders));
  2188. return &md->queue->limits;
  2189. }
  2190. EXPORT_SYMBOL_GPL(dm_get_queue_limits);
  2191. static void init_rq_based_worker_thread(struct mapped_device *md)
  2192. {
  2193. /* Initialize the request-based DM worker thread */
  2194. init_kthread_worker(&md->kworker);
  2195. md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
  2196. "kdmwork-%s", dm_device_name(md));
  2197. }
  2198. /*
  2199. * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
  2200. */
  2201. static int dm_init_request_based_queue(struct mapped_device *md)
  2202. {
  2203. struct request_queue *q = NULL;
  2204. /* Fully initialize the queue */
  2205. q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
  2206. if (!q)
  2207. return -EINVAL;
  2208. /* disable dm_request_fn's merge heuristic by default */
  2209. md->seq_rq_merge_deadline_usecs = 0;
  2210. md->queue = q;
  2211. dm_init_old_md_queue(md);
  2212. blk_queue_softirq_done(md->queue, dm_softirq_done);
  2213. blk_queue_prep_rq(md->queue, dm_prep_fn);
  2214. init_rq_based_worker_thread(md);
  2215. elv_register_queue(md->queue);
  2216. return 0;
  2217. }
  2218. static int dm_mq_init_request(void *data, struct request *rq,
  2219. unsigned int hctx_idx, unsigned int request_idx,
  2220. unsigned int numa_node)
  2221. {
  2222. struct mapped_device *md = data;
  2223. struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
  2224. /*
  2225. * Must initialize md member of tio, otherwise it won't
  2226. * be available in dm_mq_queue_rq.
  2227. */
  2228. tio->md = md;
  2229. return 0;
  2230. }
  2231. static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
  2232. const struct blk_mq_queue_data *bd)
  2233. {
  2234. struct request *rq = bd->rq;
  2235. struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
  2236. struct mapped_device *md = tio->md;
  2237. int srcu_idx;
  2238. struct dm_table *map = dm_get_live_table(md, &srcu_idx);
  2239. struct dm_target *ti;
  2240. sector_t pos;
  2241. /* always use block 0 to find the target for flushes for now */
  2242. pos = 0;
  2243. if (!(rq->cmd_flags & REQ_FLUSH))
  2244. pos = blk_rq_pos(rq);
  2245. ti = dm_table_find_target(map, pos);
  2246. if (!dm_target_is_valid(ti)) {
  2247. dm_put_live_table(md, srcu_idx);
  2248. DMERR_LIMIT("request attempted access beyond the end of device");
  2249. /*
  2250. * Must perform setup, that rq_completed() requires,
  2251. * before returning BLK_MQ_RQ_QUEUE_ERROR
  2252. */
  2253. dm_start_request(md, rq);
  2254. return BLK_MQ_RQ_QUEUE_ERROR;
  2255. }
  2256. dm_put_live_table(md, srcu_idx);
  2257. if (ti->type->busy && ti->type->busy(ti))
  2258. return BLK_MQ_RQ_QUEUE_BUSY;
  2259. dm_start_request(md, rq);
  2260. /* Init tio using md established in .init_request */
  2261. init_tio(tio, rq, md);
  2262. /*
  2263. * Establish tio->ti before queuing work (map_tio_request)
  2264. * or making direct call to map_request().
  2265. */
  2266. tio->ti = ti;
  2267. /* Clone the request if underlying devices aren't blk-mq */
  2268. if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) {
  2269. /* clone request is allocated at the end of the pdu */
  2270. tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io);
  2271. (void) clone_rq(rq, md, tio, GFP_ATOMIC);
  2272. queue_kthread_work(&md->kworker, &tio->work);
  2273. } else {
  2274. /* Direct call is fine since .queue_rq allows allocations */
  2275. if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
  2276. /* Undo dm_start_request() before requeuing */
  2277. rq_completed(md, rq_data_dir(rq), false);
  2278. return BLK_MQ_RQ_QUEUE_BUSY;
  2279. }
  2280. }
  2281. return BLK_MQ_RQ_QUEUE_OK;
  2282. }
  2283. static struct blk_mq_ops dm_mq_ops = {
  2284. .queue_rq = dm_mq_queue_rq,
  2285. .map_queue = blk_mq_map_queue,
  2286. .complete = dm_softirq_done,
  2287. .init_request = dm_mq_init_request,
  2288. };
  2289. static int dm_init_request_based_blk_mq_queue(struct mapped_device *md)
  2290. {
  2291. unsigned md_type = dm_get_md_type(md);
  2292. struct request_queue *q;
  2293. int err;
  2294. memset(&md->tag_set, 0, sizeof(md->tag_set));
  2295. md->tag_set.ops = &dm_mq_ops;
  2296. md->tag_set.queue_depth = BLKDEV_MAX_RQ;
  2297. md->tag_set.numa_node = NUMA_NO_NODE;
  2298. md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
  2299. md->tag_set.nr_hw_queues = 1;
  2300. if (md_type == DM_TYPE_REQUEST_BASED) {
  2301. /* make the memory for non-blk-mq clone part of the pdu */
  2302. md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request);
  2303. } else
  2304. md->tag_set.cmd_size = sizeof(struct dm_rq_target_io);
  2305. md->tag_set.driver_data = md;
  2306. err = blk_mq_alloc_tag_set(&md->tag_set);
  2307. if (err)
  2308. return err;
  2309. q = blk_mq_init_allocated_queue(&md->tag_set, md->queue);
  2310. if (IS_ERR(q)) {
  2311. err = PTR_ERR(q);
  2312. goto out_tag_set;
  2313. }
  2314. md->queue = q;
  2315. dm_init_md_queue(md);
  2316. /* backfill 'mq' sysfs registration normally done in blk_register_queue */
  2317. blk_mq_register_disk(md->disk);
  2318. if (md_type == DM_TYPE_REQUEST_BASED)
  2319. init_rq_based_worker_thread(md);
  2320. return 0;
  2321. out_tag_set:
  2322. blk_mq_free_tag_set(&md->tag_set);
  2323. return err;
  2324. }
  2325. static unsigned filter_md_type(unsigned type, struct mapped_device *md)
  2326. {
  2327. if (type == DM_TYPE_BIO_BASED)
  2328. return type;
  2329. return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
  2330. }
  2331. /*
  2332. * Setup the DM device's queue based on md's type
  2333. */
  2334. int dm_setup_md_queue(struct mapped_device *md)
  2335. {
  2336. int r;
  2337. unsigned md_type = filter_md_type(dm_get_md_type(md), md);
  2338. switch (md_type) {
  2339. case DM_TYPE_REQUEST_BASED:
  2340. r = dm_init_request_based_queue(md);
  2341. if (r) {
  2342. DMWARN("Cannot initialize queue for request-based mapped device");
  2343. return r;
  2344. }
  2345. break;
  2346. case DM_TYPE_MQ_REQUEST_BASED:
  2347. r = dm_init_request_based_blk_mq_queue(md);
  2348. if (r) {
  2349. DMWARN("Cannot initialize queue for request-based blk-mq mapped device");
  2350. return r;
  2351. }
  2352. break;
  2353. case DM_TYPE_BIO_BASED:
  2354. dm_init_old_md_queue(md);
  2355. blk_queue_make_request(md->queue, dm_make_request);
  2356. blk_queue_merge_bvec(md->queue, dm_merge_bvec);
  2357. break;
  2358. }
  2359. return 0;
  2360. }
  2361. struct mapped_device *dm_get_md(dev_t dev)
  2362. {
  2363. struct mapped_device *md;
  2364. unsigned minor = MINOR(dev);
  2365. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  2366. return NULL;
  2367. spin_lock(&_minor_lock);
  2368. md = idr_find(&_minor_idr, minor);
  2369. if (md) {
  2370. if ((md == MINOR_ALLOCED ||
  2371. (MINOR(disk_devt(dm_disk(md))) != minor) ||
  2372. dm_deleting_md(md) ||
  2373. test_bit(DMF_FREEING, &md->flags))) {
  2374. md = NULL;
  2375. goto out;
  2376. }
  2377. dm_get(md);
  2378. }
  2379. out:
  2380. spin_unlock(&_minor_lock);
  2381. return md;
  2382. }
  2383. EXPORT_SYMBOL_GPL(dm_get_md);
  2384. void *dm_get_mdptr(struct mapped_device *md)
  2385. {
  2386. return md->interface_ptr;
  2387. }
  2388. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  2389. {
  2390. md->interface_ptr = ptr;
  2391. }
  2392. void dm_get(struct mapped_device *md)
  2393. {
  2394. atomic_inc(&md->holders);
  2395. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  2396. }
  2397. int dm_hold(struct mapped_device *md)
  2398. {
  2399. spin_lock(&_minor_lock);
  2400. if (test_bit(DMF_FREEING, &md->flags)) {
  2401. spin_unlock(&_minor_lock);
  2402. return -EBUSY;
  2403. }
  2404. dm_get(md);
  2405. spin_unlock(&_minor_lock);
  2406. return 0;
  2407. }
  2408. EXPORT_SYMBOL_GPL(dm_hold);
  2409. const char *dm_device_name(struct mapped_device *md)
  2410. {
  2411. return md->name;
  2412. }
  2413. EXPORT_SYMBOL_GPL(dm_device_name);
  2414. static void __dm_destroy(struct mapped_device *md, bool wait)
  2415. {
  2416. struct dm_table *map;
  2417. int srcu_idx;
  2418. might_sleep();
  2419. map = dm_get_live_table(md, &srcu_idx);
  2420. spin_lock(&_minor_lock);
  2421. idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
  2422. set_bit(DMF_FREEING, &md->flags);
  2423. spin_unlock(&_minor_lock);
  2424. if (dm_request_based(md) && md->kworker_task)
  2425. flush_kthread_worker(&md->kworker);
  2426. /*
  2427. * Take suspend_lock so that presuspend and postsuspend methods
  2428. * do not race with internal suspend.
  2429. */
  2430. mutex_lock(&md->suspend_lock);
  2431. if (!dm_suspended_md(md)) {
  2432. dm_table_presuspend_targets(map);
  2433. dm_table_postsuspend_targets(map);
  2434. }
  2435. mutex_unlock(&md->suspend_lock);
  2436. /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
  2437. dm_put_live_table(md, srcu_idx);
  2438. /*
  2439. * Rare, but there may be I/O requests still going to complete,
  2440. * for example. Wait for all references to disappear.
  2441. * No one should increment the reference count of the mapped_device,
  2442. * after the mapped_device state becomes DMF_FREEING.
  2443. */
  2444. if (wait)
  2445. while (atomic_read(&md->holders))
  2446. msleep(1);
  2447. else if (atomic_read(&md->holders))
  2448. DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
  2449. dm_device_name(md), atomic_read(&md->holders));
  2450. dm_sysfs_exit(md);
  2451. dm_table_destroy(__unbind(md));
  2452. free_dev(md);
  2453. }
  2454. void dm_destroy(struct mapped_device *md)
  2455. {
  2456. __dm_destroy(md, true);
  2457. }
  2458. void dm_destroy_immediate(struct mapped_device *md)
  2459. {
  2460. __dm_destroy(md, false);
  2461. }
  2462. void dm_put(struct mapped_device *md)
  2463. {
  2464. atomic_dec(&md->holders);
  2465. }
  2466. EXPORT_SYMBOL_GPL(dm_put);
  2467. static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
  2468. {
  2469. int r = 0;
  2470. DECLARE_WAITQUEUE(wait, current);
  2471. add_wait_queue(&md->wait, &wait);
  2472. while (1) {
  2473. set_current_state(interruptible);
  2474. if (!md_in_flight(md))
  2475. break;
  2476. if (interruptible == TASK_INTERRUPTIBLE &&
  2477. signal_pending(current)) {
  2478. r = -EINTR;
  2479. break;
  2480. }
  2481. io_schedule();
  2482. }
  2483. set_current_state(TASK_RUNNING);
  2484. remove_wait_queue(&md->wait, &wait);
  2485. return r;
  2486. }
  2487. /*
  2488. * Process the deferred bios
  2489. */
  2490. static void dm_wq_work(struct work_struct *work)
  2491. {
  2492. struct mapped_device *md = container_of(work, struct mapped_device,
  2493. work);
  2494. struct bio *c;
  2495. int srcu_idx;
  2496. struct dm_table *map;
  2497. map = dm_get_live_table(md, &srcu_idx);
  2498. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  2499. spin_lock_irq(&md->deferred_lock);
  2500. c = bio_list_pop(&md->deferred);
  2501. spin_unlock_irq(&md->deferred_lock);
  2502. if (!c)
  2503. break;
  2504. if (dm_request_based(md))
  2505. generic_make_request(c);
  2506. else
  2507. __split_and_process_bio(md, map, c);
  2508. }
  2509. dm_put_live_table(md, srcu_idx);
  2510. }
  2511. static void dm_queue_flush(struct mapped_device *md)
  2512. {
  2513. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2514. smp_mb__after_atomic();
  2515. queue_work(md->wq, &md->work);
  2516. }
  2517. /*
  2518. * Swap in a new table, returning the old one for the caller to destroy.
  2519. */
  2520. struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
  2521. {
  2522. struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
  2523. struct queue_limits limits;
  2524. int r;
  2525. mutex_lock(&md->suspend_lock);
  2526. /* device must be suspended */
  2527. if (!dm_suspended_md(md))
  2528. goto out;
  2529. /*
  2530. * If the new table has no data devices, retain the existing limits.
  2531. * This helps multipath with queue_if_no_path if all paths disappear,
  2532. * then new I/O is queued based on these limits, and then some paths
  2533. * reappear.
  2534. */
  2535. if (dm_table_has_no_data_devices(table)) {
  2536. live_map = dm_get_live_table_fast(md);
  2537. if (live_map)
  2538. limits = md->queue->limits;
  2539. dm_put_live_table_fast(md);
  2540. }
  2541. if (!live_map) {
  2542. r = dm_calculate_queue_limits(table, &limits);
  2543. if (r) {
  2544. map = ERR_PTR(r);
  2545. goto out;
  2546. }
  2547. }
  2548. map = __bind(md, table, &limits);
  2549. out:
  2550. mutex_unlock(&md->suspend_lock);
  2551. return map;
  2552. }
  2553. /*
  2554. * Functions to lock and unlock any filesystem running on the
  2555. * device.
  2556. */
  2557. static int lock_fs(struct mapped_device *md)
  2558. {
  2559. int r;
  2560. WARN_ON(md->frozen_sb);
  2561. md->frozen_sb = freeze_bdev(md->bdev);
  2562. if (IS_ERR(md->frozen_sb)) {
  2563. r = PTR_ERR(md->frozen_sb);
  2564. md->frozen_sb = NULL;
  2565. return r;
  2566. }
  2567. set_bit(DMF_FROZEN, &md->flags);
  2568. return 0;
  2569. }
  2570. static void unlock_fs(struct mapped_device *md)
  2571. {
  2572. if (!test_bit(DMF_FROZEN, &md->flags))
  2573. return;
  2574. thaw_bdev(md->bdev, md->frozen_sb);
  2575. md->frozen_sb = NULL;
  2576. clear_bit(DMF_FROZEN, &md->flags);
  2577. }
  2578. /*
  2579. * If __dm_suspend returns 0, the device is completely quiescent
  2580. * now. There is no request-processing activity. All new requests
  2581. * are being added to md->deferred list.
  2582. *
  2583. * Caller must hold md->suspend_lock
  2584. */
  2585. static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
  2586. unsigned suspend_flags, int interruptible)
  2587. {
  2588. bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
  2589. bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
  2590. int r;
  2591. /*
  2592. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  2593. * This flag is cleared before dm_suspend returns.
  2594. */
  2595. if (noflush)
  2596. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2597. /*
  2598. * This gets reverted if there's an error later and the targets
  2599. * provide the .presuspend_undo hook.
  2600. */
  2601. dm_table_presuspend_targets(map);
  2602. /*
  2603. * Flush I/O to the device.
  2604. * Any I/O submitted after lock_fs() may not be flushed.
  2605. * noflush takes precedence over do_lockfs.
  2606. * (lock_fs() flushes I/Os and waits for them to complete.)
  2607. */
  2608. if (!noflush && do_lockfs) {
  2609. r = lock_fs(md);
  2610. if (r) {
  2611. dm_table_presuspend_undo_targets(map);
  2612. return r;
  2613. }
  2614. }
  2615. /*
  2616. * Here we must make sure that no processes are submitting requests
  2617. * to target drivers i.e. no one may be executing
  2618. * __split_and_process_bio. This is called from dm_request and
  2619. * dm_wq_work.
  2620. *
  2621. * To get all processes out of __split_and_process_bio in dm_request,
  2622. * we take the write lock. To prevent any process from reentering
  2623. * __split_and_process_bio from dm_request and quiesce the thread
  2624. * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
  2625. * flush_workqueue(md->wq).
  2626. */
  2627. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2628. if (map)
  2629. synchronize_srcu(&md->io_barrier);
  2630. /*
  2631. * Stop md->queue before flushing md->wq in case request-based
  2632. * dm defers requests to md->wq from md->queue.
  2633. */
  2634. if (dm_request_based(md)) {
  2635. stop_queue(md->queue);
  2636. if (md->kworker_task)
  2637. flush_kthread_worker(&md->kworker);
  2638. }
  2639. flush_workqueue(md->wq);
  2640. /*
  2641. * At this point no more requests are entering target request routines.
  2642. * We call dm_wait_for_completion to wait for all existing requests
  2643. * to finish.
  2644. */
  2645. r = dm_wait_for_completion(md, interruptible);
  2646. if (noflush)
  2647. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2648. if (map)
  2649. synchronize_srcu(&md->io_barrier);
  2650. /* were we interrupted ? */
  2651. if (r < 0) {
  2652. dm_queue_flush(md);
  2653. if (dm_request_based(md))
  2654. start_queue(md->queue);
  2655. unlock_fs(md);
  2656. dm_table_presuspend_undo_targets(map);
  2657. /* pushback list is already flushed, so skip flush */
  2658. }
  2659. return r;
  2660. }
  2661. /*
  2662. * We need to be able to change a mapping table under a mounted
  2663. * filesystem. For example we might want to move some data in
  2664. * the background. Before the table can be swapped with
  2665. * dm_bind_table, dm_suspend must be called to flush any in
  2666. * flight bios and ensure that any further io gets deferred.
  2667. */
  2668. /*
  2669. * Suspend mechanism in request-based dm.
  2670. *
  2671. * 1. Flush all I/Os by lock_fs() if needed.
  2672. * 2. Stop dispatching any I/O by stopping the request_queue.
  2673. * 3. Wait for all in-flight I/Os to be completed or requeued.
  2674. *
  2675. * To abort suspend, start the request_queue.
  2676. */
  2677. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  2678. {
  2679. struct dm_table *map = NULL;
  2680. int r = 0;
  2681. retry:
  2682. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  2683. if (dm_suspended_md(md)) {
  2684. r = -EINVAL;
  2685. goto out_unlock;
  2686. }
  2687. if (dm_suspended_internally_md(md)) {
  2688. /* already internally suspended, wait for internal resume */
  2689. mutex_unlock(&md->suspend_lock);
  2690. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  2691. if (r)
  2692. return r;
  2693. goto retry;
  2694. }
  2695. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2696. r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE);
  2697. if (r)
  2698. goto out_unlock;
  2699. set_bit(DMF_SUSPENDED, &md->flags);
  2700. dm_table_postsuspend_targets(map);
  2701. out_unlock:
  2702. mutex_unlock(&md->suspend_lock);
  2703. return r;
  2704. }
  2705. static int __dm_resume(struct mapped_device *md, struct dm_table *map)
  2706. {
  2707. if (map) {
  2708. int r = dm_table_resume_targets(map);
  2709. if (r)
  2710. return r;
  2711. }
  2712. dm_queue_flush(md);
  2713. /*
  2714. * Flushing deferred I/Os must be done after targets are resumed
  2715. * so that mapping of targets can work correctly.
  2716. * Request-based dm is queueing the deferred I/Os in its request_queue.
  2717. */
  2718. if (dm_request_based(md))
  2719. start_queue(md->queue);
  2720. unlock_fs(md);
  2721. return 0;
  2722. }
  2723. int dm_resume(struct mapped_device *md)
  2724. {
  2725. int r = -EINVAL;
  2726. struct dm_table *map = NULL;
  2727. retry:
  2728. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  2729. if (!dm_suspended_md(md))
  2730. goto out;
  2731. if (dm_suspended_internally_md(md)) {
  2732. /* already internally suspended, wait for internal resume */
  2733. mutex_unlock(&md->suspend_lock);
  2734. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  2735. if (r)
  2736. return r;
  2737. goto retry;
  2738. }
  2739. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2740. if (!map || !dm_table_get_size(map))
  2741. goto out;
  2742. r = __dm_resume(md, map);
  2743. if (r)
  2744. goto out;
  2745. clear_bit(DMF_SUSPENDED, &md->flags);
  2746. r = 0;
  2747. out:
  2748. mutex_unlock(&md->suspend_lock);
  2749. return r;
  2750. }
  2751. /*
  2752. * Internal suspend/resume works like userspace-driven suspend. It waits
  2753. * until all bios finish and prevents issuing new bios to the target drivers.
  2754. * It may be used only from the kernel.
  2755. */
  2756. static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
  2757. {
  2758. struct dm_table *map = NULL;
  2759. if (md->internal_suspend_count++)
  2760. return; /* nested internal suspend */
  2761. if (dm_suspended_md(md)) {
  2762. set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2763. return; /* nest suspend */
  2764. }
  2765. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2766. /*
  2767. * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
  2768. * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
  2769. * would require changing .presuspend to return an error -- avoid this
  2770. * until there is a need for more elaborate variants of internal suspend.
  2771. */
  2772. (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE);
  2773. set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2774. dm_table_postsuspend_targets(map);
  2775. }
  2776. static void __dm_internal_resume(struct mapped_device *md)
  2777. {
  2778. BUG_ON(!md->internal_suspend_count);
  2779. if (--md->internal_suspend_count)
  2780. return; /* resume from nested internal suspend */
  2781. if (dm_suspended_md(md))
  2782. goto done; /* resume from nested suspend */
  2783. /*
  2784. * NOTE: existing callers don't need to call dm_table_resume_targets
  2785. * (which may fail -- so best to avoid it for now by passing NULL map)
  2786. */
  2787. (void) __dm_resume(md, NULL);
  2788. done:
  2789. clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2790. smp_mb__after_atomic();
  2791. wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
  2792. }
  2793. void dm_internal_suspend_noflush(struct mapped_device *md)
  2794. {
  2795. mutex_lock(&md->suspend_lock);
  2796. __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
  2797. mutex_unlock(&md->suspend_lock);
  2798. }
  2799. EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
  2800. void dm_internal_resume(struct mapped_device *md)
  2801. {
  2802. mutex_lock(&md->suspend_lock);
  2803. __dm_internal_resume(md);
  2804. mutex_unlock(&md->suspend_lock);
  2805. }
  2806. EXPORT_SYMBOL_GPL(dm_internal_resume);
  2807. /*
  2808. * Fast variants of internal suspend/resume hold md->suspend_lock,
  2809. * which prevents interaction with userspace-driven suspend.
  2810. */
  2811. void dm_internal_suspend_fast(struct mapped_device *md)
  2812. {
  2813. mutex_lock(&md->suspend_lock);
  2814. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  2815. return;
  2816. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2817. synchronize_srcu(&md->io_barrier);
  2818. flush_workqueue(md->wq);
  2819. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  2820. }
  2821. EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
  2822. void dm_internal_resume_fast(struct mapped_device *md)
  2823. {
  2824. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  2825. goto done;
  2826. dm_queue_flush(md);
  2827. done:
  2828. mutex_unlock(&md->suspend_lock);
  2829. }
  2830. EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
  2831. /*-----------------------------------------------------------------
  2832. * Event notification.
  2833. *---------------------------------------------------------------*/
  2834. int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
  2835. unsigned cookie)
  2836. {
  2837. char udev_cookie[DM_COOKIE_LENGTH];
  2838. char *envp[] = { udev_cookie, NULL };
  2839. if (!cookie)
  2840. return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
  2841. else {
  2842. snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
  2843. DM_COOKIE_ENV_VAR_NAME, cookie);
  2844. return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
  2845. action, envp);
  2846. }
  2847. }
  2848. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  2849. {
  2850. return atomic_add_return(1, &md->uevent_seq);
  2851. }
  2852. uint32_t dm_get_event_nr(struct mapped_device *md)
  2853. {
  2854. return atomic_read(&md->event_nr);
  2855. }
  2856. int dm_wait_event(struct mapped_device *md, int event_nr)
  2857. {
  2858. return wait_event_interruptible(md->eventq,
  2859. (event_nr != atomic_read(&md->event_nr)));
  2860. }
  2861. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  2862. {
  2863. unsigned long flags;
  2864. spin_lock_irqsave(&md->uevent_lock, flags);
  2865. list_add(elist, &md->uevent_list);
  2866. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2867. }
  2868. /*
  2869. * The gendisk is only valid as long as you have a reference
  2870. * count on 'md'.
  2871. */
  2872. struct gendisk *dm_disk(struct mapped_device *md)
  2873. {
  2874. return md->disk;
  2875. }
  2876. EXPORT_SYMBOL_GPL(dm_disk);
  2877. struct kobject *dm_kobject(struct mapped_device *md)
  2878. {
  2879. return &md->kobj_holder.kobj;
  2880. }
  2881. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  2882. {
  2883. struct mapped_device *md;
  2884. md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
  2885. if (test_bit(DMF_FREEING, &md->flags) ||
  2886. dm_deleting_md(md))
  2887. return NULL;
  2888. dm_get(md);
  2889. return md;
  2890. }
  2891. int dm_suspended_md(struct mapped_device *md)
  2892. {
  2893. return test_bit(DMF_SUSPENDED, &md->flags);
  2894. }
  2895. int dm_suspended_internally_md(struct mapped_device *md)
  2896. {
  2897. return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2898. }
  2899. int dm_test_deferred_remove_flag(struct mapped_device *md)
  2900. {
  2901. return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
  2902. }
  2903. int dm_suspended(struct dm_target *ti)
  2904. {
  2905. return dm_suspended_md(dm_table_get_md(ti->table));
  2906. }
  2907. EXPORT_SYMBOL_GPL(dm_suspended);
  2908. int dm_noflush_suspending(struct dm_target *ti)
  2909. {
  2910. return __noflush_suspending(dm_table_get_md(ti->table));
  2911. }
  2912. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  2913. struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
  2914. unsigned integrity, unsigned per_bio_data_size)
  2915. {
  2916. struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
  2917. struct kmem_cache *cachep = NULL;
  2918. unsigned int pool_size = 0;
  2919. unsigned int front_pad;
  2920. if (!pools)
  2921. return NULL;
  2922. type = filter_md_type(type, md);
  2923. switch (type) {
  2924. case DM_TYPE_BIO_BASED:
  2925. cachep = _io_cache;
  2926. pool_size = dm_get_reserved_bio_based_ios();
  2927. front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
  2928. break;
  2929. case DM_TYPE_REQUEST_BASED:
  2930. cachep = _rq_tio_cache;
  2931. pool_size = dm_get_reserved_rq_based_ios();
  2932. pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
  2933. if (!pools->rq_pool)
  2934. goto out;
  2935. /* fall through to setup remaining rq-based pools */
  2936. case DM_TYPE_MQ_REQUEST_BASED:
  2937. if (!pool_size)
  2938. pool_size = dm_get_reserved_rq_based_ios();
  2939. front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
  2940. /* per_bio_data_size is not used. See __bind_mempools(). */
  2941. WARN_ON(per_bio_data_size != 0);
  2942. break;
  2943. default:
  2944. BUG();
  2945. }
  2946. if (cachep) {
  2947. pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
  2948. if (!pools->io_pool)
  2949. goto out;
  2950. }
  2951. pools->bs = bioset_create_nobvec(pool_size, front_pad);
  2952. if (!pools->bs)
  2953. goto out;
  2954. if (integrity && bioset_integrity_create(pools->bs, pool_size))
  2955. goto out;
  2956. return pools;
  2957. out:
  2958. dm_free_md_mempools(pools);
  2959. return NULL;
  2960. }
  2961. void dm_free_md_mempools(struct dm_md_mempools *pools)
  2962. {
  2963. if (!pools)
  2964. return;
  2965. if (pools->io_pool)
  2966. mempool_destroy(pools->io_pool);
  2967. if (pools->rq_pool)
  2968. mempool_destroy(pools->rq_pool);
  2969. if (pools->bs)
  2970. bioset_free(pools->bs);
  2971. kfree(pools);
  2972. }
  2973. static const struct block_device_operations dm_blk_dops = {
  2974. .open = dm_blk_open,
  2975. .release = dm_blk_close,
  2976. .ioctl = dm_blk_ioctl,
  2977. .getgeo = dm_blk_getgeo,
  2978. .owner = THIS_MODULE
  2979. };
  2980. /*
  2981. * module hooks
  2982. */
  2983. module_init(dm_init);
  2984. module_exit(dm_exit);
  2985. module_param(major, uint, 0);
  2986. MODULE_PARM_DESC(major, "The major number of the device mapper");
  2987. module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
  2988. MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
  2989. module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
  2990. MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
  2991. module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
  2992. MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
  2993. MODULE_DESCRIPTION(DM_NAME " driver");
  2994. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2995. MODULE_LICENSE("GPL");