dm.c 82 KB

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