dm.c 86 KB

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