dm.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203
  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-core.h"
  8. #include "dm-rq.h"
  9. #include "dm-uevent.h"
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/blkpg.h>
  15. #include <linux/bio.h>
  16. #include <linux/mempool.h>
  17. #include <linux/dax.h>
  18. #include <linux/slab.h>
  19. #include <linux/idr.h>
  20. #include <linux/uio.h>
  21. #include <linux/hdreg.h>
  22. #include <linux/delay.h>
  23. #include <linux/wait.h>
  24. #include <linux/pr.h>
  25. #include <linux/refcount.h>
  26. #define DM_MSG_PREFIX "core"
  27. /*
  28. * Cookies are numeric values sent with CHANGE and REMOVE
  29. * uevents while resuming, removing or renaming the device.
  30. */
  31. #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
  32. #define DM_COOKIE_LENGTH 24
  33. static const char *_name = DM_NAME;
  34. static unsigned int major = 0;
  35. static unsigned int _major = 0;
  36. static DEFINE_IDR(_minor_idr);
  37. static DEFINE_SPINLOCK(_minor_lock);
  38. static void do_deferred_remove(struct work_struct *w);
  39. static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
  40. static struct workqueue_struct *deferred_remove_workqueue;
  41. atomic_t dm_global_event_nr = ATOMIC_INIT(0);
  42. DECLARE_WAIT_QUEUE_HEAD(dm_global_eventq);
  43. void dm_issue_global_event(void)
  44. {
  45. atomic_inc(&dm_global_event_nr);
  46. wake_up(&dm_global_eventq);
  47. }
  48. /*
  49. * One of these is allocated (on-stack) per original bio.
  50. */
  51. struct clone_info {
  52. struct dm_table *map;
  53. struct bio *bio;
  54. struct dm_io *io;
  55. sector_t sector;
  56. unsigned sector_count;
  57. };
  58. /*
  59. * One of these is allocated per clone bio.
  60. */
  61. #define DM_TIO_MAGIC 7282014
  62. struct dm_target_io {
  63. unsigned magic;
  64. struct dm_io *io;
  65. struct dm_target *ti;
  66. unsigned target_bio_nr;
  67. unsigned *len_ptr;
  68. bool inside_dm_io;
  69. struct bio clone;
  70. };
  71. /*
  72. * One of these is allocated per original bio.
  73. * It contains the first clone used for that original.
  74. */
  75. #define DM_IO_MAGIC 5191977
  76. struct dm_io {
  77. unsigned magic;
  78. struct mapped_device *md;
  79. blk_status_t status;
  80. atomic_t io_count;
  81. struct bio *orig_bio;
  82. unsigned long start_time;
  83. spinlock_t endio_lock;
  84. struct dm_stats_aux stats_aux;
  85. /* last member of dm_target_io is 'struct bio' */
  86. struct dm_target_io tio;
  87. };
  88. void *dm_per_bio_data(struct bio *bio, size_t data_size)
  89. {
  90. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  91. if (!tio->inside_dm_io)
  92. return (char *)bio - offsetof(struct dm_target_io, clone) - data_size;
  93. return (char *)bio - offsetof(struct dm_target_io, clone) - offsetof(struct dm_io, tio) - data_size;
  94. }
  95. EXPORT_SYMBOL_GPL(dm_per_bio_data);
  96. struct bio *dm_bio_from_per_bio_data(void *data, size_t data_size)
  97. {
  98. struct dm_io *io = (struct dm_io *)((char *)data + data_size);
  99. if (io->magic == DM_IO_MAGIC)
  100. return (struct bio *)((char *)io + offsetof(struct dm_io, tio) + offsetof(struct dm_target_io, clone));
  101. BUG_ON(io->magic != DM_TIO_MAGIC);
  102. return (struct bio *)((char *)io + offsetof(struct dm_target_io, clone));
  103. }
  104. EXPORT_SYMBOL_GPL(dm_bio_from_per_bio_data);
  105. unsigned dm_bio_get_target_bio_nr(const struct bio *bio)
  106. {
  107. return container_of(bio, struct dm_target_io, clone)->target_bio_nr;
  108. }
  109. EXPORT_SYMBOL_GPL(dm_bio_get_target_bio_nr);
  110. #define MINOR_ALLOCED ((void *)-1)
  111. /*
  112. * Bits for the md->flags field.
  113. */
  114. #define DMF_BLOCK_IO_FOR_SUSPEND 0
  115. #define DMF_SUSPENDED 1
  116. #define DMF_FROZEN 2
  117. #define DMF_FREEING 3
  118. #define DMF_DELETING 4
  119. #define DMF_NOFLUSH_SUSPENDING 5
  120. #define DMF_DEFERRED_REMOVE 6
  121. #define DMF_SUSPENDED_INTERNALLY 7
  122. #define DM_NUMA_NODE NUMA_NO_NODE
  123. static int dm_numa_node = DM_NUMA_NODE;
  124. /*
  125. * For mempools pre-allocation at the table loading time.
  126. */
  127. struct dm_md_mempools {
  128. struct bio_set bs;
  129. struct bio_set io_bs;
  130. };
  131. struct table_device {
  132. struct list_head list;
  133. refcount_t count;
  134. struct dm_dev dm_dev;
  135. };
  136. static struct kmem_cache *_rq_tio_cache;
  137. static struct kmem_cache *_rq_cache;
  138. /*
  139. * Bio-based DM's mempools' reserved IOs set by the user.
  140. */
  141. #define RESERVED_BIO_BASED_IOS 16
  142. static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
  143. static int __dm_get_module_param_int(int *module_param, int min, int max)
  144. {
  145. int param = READ_ONCE(*module_param);
  146. int modified_param = 0;
  147. bool modified = true;
  148. if (param < min)
  149. modified_param = min;
  150. else if (param > max)
  151. modified_param = max;
  152. else
  153. modified = false;
  154. if (modified) {
  155. (void)cmpxchg(module_param, param, modified_param);
  156. param = modified_param;
  157. }
  158. return param;
  159. }
  160. unsigned __dm_get_module_param(unsigned *module_param,
  161. unsigned def, unsigned max)
  162. {
  163. unsigned param = READ_ONCE(*module_param);
  164. unsigned modified_param = 0;
  165. if (!param)
  166. modified_param = def;
  167. else if (param > max)
  168. modified_param = max;
  169. if (modified_param) {
  170. (void)cmpxchg(module_param, param, modified_param);
  171. param = modified_param;
  172. }
  173. return param;
  174. }
  175. unsigned dm_get_reserved_bio_based_ios(void)
  176. {
  177. return __dm_get_module_param(&reserved_bio_based_ios,
  178. RESERVED_BIO_BASED_IOS, DM_RESERVED_MAX_IOS);
  179. }
  180. EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
  181. static unsigned dm_get_numa_node(void)
  182. {
  183. return __dm_get_module_param_int(&dm_numa_node,
  184. DM_NUMA_NODE, num_online_nodes() - 1);
  185. }
  186. static int __init local_init(void)
  187. {
  188. int r = -ENOMEM;
  189. _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
  190. if (!_rq_tio_cache)
  191. return r;
  192. _rq_cache = kmem_cache_create("dm_old_clone_request", sizeof(struct request),
  193. __alignof__(struct request), 0, NULL);
  194. if (!_rq_cache)
  195. goto out_free_rq_tio_cache;
  196. r = dm_uevent_init();
  197. if (r)
  198. goto out_free_rq_cache;
  199. deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
  200. if (!deferred_remove_workqueue) {
  201. r = -ENOMEM;
  202. goto out_uevent_exit;
  203. }
  204. _major = major;
  205. r = register_blkdev(_major, _name);
  206. if (r < 0)
  207. goto out_free_workqueue;
  208. if (!_major)
  209. _major = r;
  210. return 0;
  211. out_free_workqueue:
  212. destroy_workqueue(deferred_remove_workqueue);
  213. out_uevent_exit:
  214. dm_uevent_exit();
  215. out_free_rq_cache:
  216. kmem_cache_destroy(_rq_cache);
  217. out_free_rq_tio_cache:
  218. kmem_cache_destroy(_rq_tio_cache);
  219. return r;
  220. }
  221. static void local_exit(void)
  222. {
  223. flush_scheduled_work();
  224. destroy_workqueue(deferred_remove_workqueue);
  225. kmem_cache_destroy(_rq_cache);
  226. kmem_cache_destroy(_rq_tio_cache);
  227. unregister_blkdev(_major, _name);
  228. dm_uevent_exit();
  229. _major = 0;
  230. DMINFO("cleaned up");
  231. }
  232. static int (*_inits[])(void) __initdata = {
  233. local_init,
  234. dm_target_init,
  235. dm_linear_init,
  236. dm_stripe_init,
  237. dm_io_init,
  238. dm_kcopyd_init,
  239. dm_interface_init,
  240. dm_statistics_init,
  241. };
  242. static void (*_exits[])(void) = {
  243. local_exit,
  244. dm_target_exit,
  245. dm_linear_exit,
  246. dm_stripe_exit,
  247. dm_io_exit,
  248. dm_kcopyd_exit,
  249. dm_interface_exit,
  250. dm_statistics_exit,
  251. };
  252. static int __init dm_init(void)
  253. {
  254. const int count = ARRAY_SIZE(_inits);
  255. int r, i;
  256. for (i = 0; i < count; i++) {
  257. r = _inits[i]();
  258. if (r)
  259. goto bad;
  260. }
  261. return 0;
  262. bad:
  263. while (i--)
  264. _exits[i]();
  265. return r;
  266. }
  267. static void __exit dm_exit(void)
  268. {
  269. int i = ARRAY_SIZE(_exits);
  270. while (i--)
  271. _exits[i]();
  272. /*
  273. * Should be empty by this point.
  274. */
  275. idr_destroy(&_minor_idr);
  276. }
  277. /*
  278. * Block device functions
  279. */
  280. int dm_deleting_md(struct mapped_device *md)
  281. {
  282. return test_bit(DMF_DELETING, &md->flags);
  283. }
  284. static int dm_blk_open(struct block_device *bdev, fmode_t mode)
  285. {
  286. struct mapped_device *md;
  287. spin_lock(&_minor_lock);
  288. md = bdev->bd_disk->private_data;
  289. if (!md)
  290. goto out;
  291. if (test_bit(DMF_FREEING, &md->flags) ||
  292. dm_deleting_md(md)) {
  293. md = NULL;
  294. goto out;
  295. }
  296. dm_get(md);
  297. atomic_inc(&md->open_count);
  298. out:
  299. spin_unlock(&_minor_lock);
  300. return md ? 0 : -ENXIO;
  301. }
  302. static void dm_blk_close(struct gendisk *disk, fmode_t mode)
  303. {
  304. struct mapped_device *md;
  305. spin_lock(&_minor_lock);
  306. md = disk->private_data;
  307. if (WARN_ON(!md))
  308. goto out;
  309. if (atomic_dec_and_test(&md->open_count) &&
  310. (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
  311. queue_work(deferred_remove_workqueue, &deferred_remove_work);
  312. dm_put(md);
  313. out:
  314. spin_unlock(&_minor_lock);
  315. }
  316. int dm_open_count(struct mapped_device *md)
  317. {
  318. return atomic_read(&md->open_count);
  319. }
  320. /*
  321. * Guarantees nothing is using the device before it's deleted.
  322. */
  323. int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
  324. {
  325. int r = 0;
  326. spin_lock(&_minor_lock);
  327. if (dm_open_count(md)) {
  328. r = -EBUSY;
  329. if (mark_deferred)
  330. set_bit(DMF_DEFERRED_REMOVE, &md->flags);
  331. } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
  332. r = -EEXIST;
  333. else
  334. set_bit(DMF_DELETING, &md->flags);
  335. spin_unlock(&_minor_lock);
  336. return r;
  337. }
  338. int dm_cancel_deferred_remove(struct mapped_device *md)
  339. {
  340. int r = 0;
  341. spin_lock(&_minor_lock);
  342. if (test_bit(DMF_DELETING, &md->flags))
  343. r = -EBUSY;
  344. else
  345. clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
  346. spin_unlock(&_minor_lock);
  347. return r;
  348. }
  349. static void do_deferred_remove(struct work_struct *w)
  350. {
  351. dm_deferred_remove();
  352. }
  353. sector_t dm_get_size(struct mapped_device *md)
  354. {
  355. return get_capacity(md->disk);
  356. }
  357. struct request_queue *dm_get_md_queue(struct mapped_device *md)
  358. {
  359. return md->queue;
  360. }
  361. struct dm_stats *dm_get_stats(struct mapped_device *md)
  362. {
  363. return &md->stats;
  364. }
  365. static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  366. {
  367. struct mapped_device *md = bdev->bd_disk->private_data;
  368. return dm_get_geometry(md, geo);
  369. }
  370. static int dm_prepare_ioctl(struct mapped_device *md, int *srcu_idx,
  371. struct block_device **bdev)
  372. __acquires(md->io_barrier)
  373. {
  374. struct dm_target *tgt;
  375. struct dm_table *map;
  376. int r;
  377. retry:
  378. r = -ENOTTY;
  379. map = dm_get_live_table(md, srcu_idx);
  380. if (!map || !dm_table_get_size(map))
  381. return r;
  382. /* We only support devices that have a single target */
  383. if (dm_table_get_num_targets(map) != 1)
  384. return r;
  385. tgt = dm_table_get_target(map, 0);
  386. if (!tgt->type->prepare_ioctl)
  387. return r;
  388. if (dm_suspended_md(md))
  389. return -EAGAIN;
  390. r = tgt->type->prepare_ioctl(tgt, bdev);
  391. if (r == -ENOTCONN && !fatal_signal_pending(current)) {
  392. dm_put_live_table(md, *srcu_idx);
  393. msleep(10);
  394. goto retry;
  395. }
  396. return r;
  397. }
  398. static void dm_unprepare_ioctl(struct mapped_device *md, int srcu_idx)
  399. __releases(md->io_barrier)
  400. {
  401. dm_put_live_table(md, srcu_idx);
  402. }
  403. static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
  404. unsigned int cmd, unsigned long arg)
  405. {
  406. struct mapped_device *md = bdev->bd_disk->private_data;
  407. int r, srcu_idx;
  408. r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
  409. if (r < 0)
  410. goto out;
  411. if (r > 0) {
  412. /*
  413. * Target determined this ioctl is being issued against a
  414. * subset of the parent bdev; require extra privileges.
  415. */
  416. if (!capable(CAP_SYS_RAWIO)) {
  417. DMWARN_LIMIT(
  418. "%s: sending ioctl %x to DM device without required privilege.",
  419. current->comm, cmd);
  420. r = -ENOIOCTLCMD;
  421. goto out;
  422. }
  423. }
  424. r = __blkdev_driver_ioctl(bdev, mode, cmd, arg);
  425. out:
  426. dm_unprepare_ioctl(md, srcu_idx);
  427. return r;
  428. }
  429. static void start_io_acct(struct dm_io *io);
  430. static struct dm_io *alloc_io(struct mapped_device *md, struct bio *bio)
  431. {
  432. struct dm_io *io;
  433. struct dm_target_io *tio;
  434. struct bio *clone;
  435. clone = bio_alloc_bioset(GFP_NOIO, 0, &md->io_bs);
  436. if (!clone)
  437. return NULL;
  438. tio = container_of(clone, struct dm_target_io, clone);
  439. tio->inside_dm_io = true;
  440. tio->io = NULL;
  441. io = container_of(tio, struct dm_io, tio);
  442. io->magic = DM_IO_MAGIC;
  443. io->status = 0;
  444. atomic_set(&io->io_count, 1);
  445. io->orig_bio = bio;
  446. io->md = md;
  447. spin_lock_init(&io->endio_lock);
  448. start_io_acct(io);
  449. return io;
  450. }
  451. static void free_io(struct mapped_device *md, struct dm_io *io)
  452. {
  453. bio_put(&io->tio.clone);
  454. }
  455. static struct dm_target_io *alloc_tio(struct clone_info *ci, struct dm_target *ti,
  456. unsigned target_bio_nr, gfp_t gfp_mask)
  457. {
  458. struct dm_target_io *tio;
  459. if (!ci->io->tio.io) {
  460. /* the dm_target_io embedded in ci->io is available */
  461. tio = &ci->io->tio;
  462. } else {
  463. struct bio *clone = bio_alloc_bioset(gfp_mask, 0, &ci->io->md->bs);
  464. if (!clone)
  465. return NULL;
  466. tio = container_of(clone, struct dm_target_io, clone);
  467. tio->inside_dm_io = false;
  468. }
  469. tio->magic = DM_TIO_MAGIC;
  470. tio->io = ci->io;
  471. tio->ti = ti;
  472. tio->target_bio_nr = target_bio_nr;
  473. return tio;
  474. }
  475. static void free_tio(struct dm_target_io *tio)
  476. {
  477. if (tio->inside_dm_io)
  478. return;
  479. bio_put(&tio->clone);
  480. }
  481. int md_in_flight(struct mapped_device *md)
  482. {
  483. return atomic_read(&md->pending[READ]) +
  484. atomic_read(&md->pending[WRITE]);
  485. }
  486. static void start_io_acct(struct dm_io *io)
  487. {
  488. struct mapped_device *md = io->md;
  489. struct bio *bio = io->orig_bio;
  490. int rw = bio_data_dir(bio);
  491. io->start_time = jiffies;
  492. generic_start_io_acct(md->queue, bio_op(bio), bio_sectors(bio),
  493. &dm_disk(md)->part0);
  494. atomic_set(&dm_disk(md)->part0.in_flight[rw],
  495. atomic_inc_return(&md->pending[rw]));
  496. if (unlikely(dm_stats_used(&md->stats)))
  497. dm_stats_account_io(&md->stats, bio_data_dir(bio),
  498. bio->bi_iter.bi_sector, bio_sectors(bio),
  499. false, 0, &io->stats_aux);
  500. }
  501. static void end_io_acct(struct dm_io *io)
  502. {
  503. struct mapped_device *md = io->md;
  504. struct bio *bio = io->orig_bio;
  505. unsigned long duration = jiffies - io->start_time;
  506. int pending;
  507. int rw = bio_data_dir(bio);
  508. generic_end_io_acct(md->queue, bio_op(bio), &dm_disk(md)->part0,
  509. io->start_time);
  510. if (unlikely(dm_stats_used(&md->stats)))
  511. dm_stats_account_io(&md->stats, bio_data_dir(bio),
  512. bio->bi_iter.bi_sector, bio_sectors(bio),
  513. true, duration, &io->stats_aux);
  514. /*
  515. * After this is decremented the bio must not be touched if it is
  516. * a flush.
  517. */
  518. pending = atomic_dec_return(&md->pending[rw]);
  519. atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
  520. pending += atomic_read(&md->pending[rw^0x1]);
  521. /* nudge anyone waiting on suspend queue */
  522. if (!pending)
  523. wake_up(&md->wait);
  524. }
  525. /*
  526. * Add the bio to the list of deferred io.
  527. */
  528. static void queue_io(struct mapped_device *md, struct bio *bio)
  529. {
  530. unsigned long flags;
  531. spin_lock_irqsave(&md->deferred_lock, flags);
  532. bio_list_add(&md->deferred, bio);
  533. spin_unlock_irqrestore(&md->deferred_lock, flags);
  534. queue_work(md->wq, &md->work);
  535. }
  536. /*
  537. * Everyone (including functions in this file), should use this
  538. * function to access the md->map field, and make sure they call
  539. * dm_put_live_table() when finished.
  540. */
  541. struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
  542. {
  543. *srcu_idx = srcu_read_lock(&md->io_barrier);
  544. return srcu_dereference(md->map, &md->io_barrier);
  545. }
  546. void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
  547. {
  548. srcu_read_unlock(&md->io_barrier, srcu_idx);
  549. }
  550. void dm_sync_table(struct mapped_device *md)
  551. {
  552. synchronize_srcu(&md->io_barrier);
  553. synchronize_rcu_expedited();
  554. }
  555. /*
  556. * A fast alternative to dm_get_live_table/dm_put_live_table.
  557. * The caller must not block between these two functions.
  558. */
  559. static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
  560. {
  561. rcu_read_lock();
  562. return rcu_dereference(md->map);
  563. }
  564. static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
  565. {
  566. rcu_read_unlock();
  567. }
  568. static char *_dm_claim_ptr = "I belong to device-mapper";
  569. /*
  570. * Open a table device so we can use it as a map destination.
  571. */
  572. static int open_table_device(struct table_device *td, dev_t dev,
  573. struct mapped_device *md)
  574. {
  575. struct block_device *bdev;
  576. int r;
  577. BUG_ON(td->dm_dev.bdev);
  578. bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _dm_claim_ptr);
  579. if (IS_ERR(bdev))
  580. return PTR_ERR(bdev);
  581. r = bd_link_disk_holder(bdev, dm_disk(md));
  582. if (r) {
  583. blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
  584. return r;
  585. }
  586. td->dm_dev.bdev = bdev;
  587. td->dm_dev.dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
  588. return 0;
  589. }
  590. /*
  591. * Close a table device that we've been using.
  592. */
  593. static void close_table_device(struct table_device *td, struct mapped_device *md)
  594. {
  595. if (!td->dm_dev.bdev)
  596. return;
  597. bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
  598. blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
  599. put_dax(td->dm_dev.dax_dev);
  600. td->dm_dev.bdev = NULL;
  601. td->dm_dev.dax_dev = NULL;
  602. }
  603. static struct table_device *find_table_device(struct list_head *l, dev_t dev,
  604. fmode_t mode) {
  605. struct table_device *td;
  606. list_for_each_entry(td, l, list)
  607. if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
  608. return td;
  609. return NULL;
  610. }
  611. int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
  612. struct dm_dev **result) {
  613. int r;
  614. struct table_device *td;
  615. mutex_lock(&md->table_devices_lock);
  616. td = find_table_device(&md->table_devices, dev, mode);
  617. if (!td) {
  618. td = kmalloc_node(sizeof(*td), GFP_KERNEL, md->numa_node_id);
  619. if (!td) {
  620. mutex_unlock(&md->table_devices_lock);
  621. return -ENOMEM;
  622. }
  623. td->dm_dev.mode = mode;
  624. td->dm_dev.bdev = NULL;
  625. if ((r = open_table_device(td, dev, md))) {
  626. mutex_unlock(&md->table_devices_lock);
  627. kfree(td);
  628. return r;
  629. }
  630. format_dev_t(td->dm_dev.name, dev);
  631. refcount_set(&td->count, 1);
  632. list_add(&td->list, &md->table_devices);
  633. } else {
  634. refcount_inc(&td->count);
  635. }
  636. mutex_unlock(&md->table_devices_lock);
  637. *result = &td->dm_dev;
  638. return 0;
  639. }
  640. EXPORT_SYMBOL_GPL(dm_get_table_device);
  641. void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
  642. {
  643. struct table_device *td = container_of(d, struct table_device, dm_dev);
  644. mutex_lock(&md->table_devices_lock);
  645. if (refcount_dec_and_test(&td->count)) {
  646. close_table_device(td, md);
  647. list_del(&td->list);
  648. kfree(td);
  649. }
  650. mutex_unlock(&md->table_devices_lock);
  651. }
  652. EXPORT_SYMBOL(dm_put_table_device);
  653. static void free_table_devices(struct list_head *devices)
  654. {
  655. struct list_head *tmp, *next;
  656. list_for_each_safe(tmp, next, devices) {
  657. struct table_device *td = list_entry(tmp, struct table_device, list);
  658. DMWARN("dm_destroy: %s still exists with %d references",
  659. td->dm_dev.name, refcount_read(&td->count));
  660. kfree(td);
  661. }
  662. }
  663. /*
  664. * Get the geometry associated with a dm device
  665. */
  666. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
  667. {
  668. *geo = md->geometry;
  669. return 0;
  670. }
  671. /*
  672. * Set the geometry of a device.
  673. */
  674. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
  675. {
  676. sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
  677. if (geo->start > sz) {
  678. DMWARN("Start sector is beyond the geometry limits.");
  679. return -EINVAL;
  680. }
  681. md->geometry = *geo;
  682. return 0;
  683. }
  684. static int __noflush_suspending(struct mapped_device *md)
  685. {
  686. return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  687. }
  688. /*
  689. * Decrements the number of outstanding ios that a bio has been
  690. * cloned into, completing the original io if necc.
  691. */
  692. static void dec_pending(struct dm_io *io, blk_status_t error)
  693. {
  694. unsigned long flags;
  695. blk_status_t io_error;
  696. struct bio *bio;
  697. struct mapped_device *md = io->md;
  698. /* Push-back supersedes any I/O errors */
  699. if (unlikely(error)) {
  700. spin_lock_irqsave(&io->endio_lock, flags);
  701. if (!(io->status == BLK_STS_DM_REQUEUE && __noflush_suspending(md)))
  702. io->status = error;
  703. spin_unlock_irqrestore(&io->endio_lock, flags);
  704. }
  705. if (atomic_dec_and_test(&io->io_count)) {
  706. if (io->status == BLK_STS_DM_REQUEUE) {
  707. /*
  708. * Target requested pushing back the I/O.
  709. */
  710. spin_lock_irqsave(&md->deferred_lock, flags);
  711. if (__noflush_suspending(md))
  712. /* NOTE early return due to BLK_STS_DM_REQUEUE below */
  713. bio_list_add_head(&md->deferred, io->orig_bio);
  714. else
  715. /* noflush suspend was interrupted. */
  716. io->status = BLK_STS_IOERR;
  717. spin_unlock_irqrestore(&md->deferred_lock, flags);
  718. }
  719. io_error = io->status;
  720. bio = io->orig_bio;
  721. end_io_acct(io);
  722. free_io(md, io);
  723. if (io_error == BLK_STS_DM_REQUEUE)
  724. return;
  725. if ((bio->bi_opf & REQ_PREFLUSH) && bio->bi_iter.bi_size) {
  726. /*
  727. * Preflush done for flush with data, reissue
  728. * without REQ_PREFLUSH.
  729. */
  730. bio->bi_opf &= ~REQ_PREFLUSH;
  731. queue_io(md, bio);
  732. } else {
  733. /* done with normal IO or empty flush */
  734. if (io_error)
  735. bio->bi_status = io_error;
  736. bio_endio(bio);
  737. }
  738. }
  739. }
  740. void disable_discard(struct mapped_device *md)
  741. {
  742. struct queue_limits *limits = dm_get_queue_limits(md);
  743. /* device doesn't really support DISCARD, disable it */
  744. limits->max_discard_sectors = 0;
  745. blk_queue_flag_clear(QUEUE_FLAG_DISCARD, md->queue);
  746. }
  747. void disable_write_same(struct mapped_device *md)
  748. {
  749. struct queue_limits *limits = dm_get_queue_limits(md);
  750. /* device doesn't really support WRITE SAME, disable it */
  751. limits->max_write_same_sectors = 0;
  752. }
  753. void disable_write_zeroes(struct mapped_device *md)
  754. {
  755. struct queue_limits *limits = dm_get_queue_limits(md);
  756. /* device doesn't really support WRITE ZEROES, disable it */
  757. limits->max_write_zeroes_sectors = 0;
  758. }
  759. static void clone_endio(struct bio *bio)
  760. {
  761. blk_status_t error = bio->bi_status;
  762. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  763. struct dm_io *io = tio->io;
  764. struct mapped_device *md = tio->io->md;
  765. dm_endio_fn endio = tio->ti->type->end_io;
  766. if (unlikely(error == BLK_STS_TARGET) && md->type != DM_TYPE_NVME_BIO_BASED) {
  767. if (bio_op(bio) == REQ_OP_DISCARD &&
  768. !bio->bi_disk->queue->limits.max_discard_sectors)
  769. disable_discard(md);
  770. else if (bio_op(bio) == REQ_OP_WRITE_SAME &&
  771. !bio->bi_disk->queue->limits.max_write_same_sectors)
  772. disable_write_same(md);
  773. else if (bio_op(bio) == REQ_OP_WRITE_ZEROES &&
  774. !bio->bi_disk->queue->limits.max_write_zeroes_sectors)
  775. disable_write_zeroes(md);
  776. }
  777. if (endio) {
  778. int r = endio(tio->ti, bio, &error);
  779. switch (r) {
  780. case DM_ENDIO_REQUEUE:
  781. error = BLK_STS_DM_REQUEUE;
  782. /*FALLTHRU*/
  783. case DM_ENDIO_DONE:
  784. break;
  785. case DM_ENDIO_INCOMPLETE:
  786. /* The target will handle the io */
  787. return;
  788. default:
  789. DMWARN("unimplemented target endio return value: %d", r);
  790. BUG();
  791. }
  792. }
  793. free_tio(tio);
  794. dec_pending(io, error);
  795. }
  796. /*
  797. * Return maximum size of I/O possible at the supplied sector up to the current
  798. * target boundary.
  799. */
  800. static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
  801. {
  802. sector_t target_offset = dm_target_offset(ti, sector);
  803. return ti->len - target_offset;
  804. }
  805. static sector_t max_io_len(sector_t sector, struct dm_target *ti)
  806. {
  807. sector_t len = max_io_len_target_boundary(sector, ti);
  808. sector_t offset, max_len;
  809. /*
  810. * Does the target need to split even further?
  811. */
  812. if (ti->max_io_len) {
  813. offset = dm_target_offset(ti, sector);
  814. if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
  815. max_len = sector_div(offset, ti->max_io_len);
  816. else
  817. max_len = offset & (ti->max_io_len - 1);
  818. max_len = ti->max_io_len - max_len;
  819. if (len > max_len)
  820. len = max_len;
  821. }
  822. return len;
  823. }
  824. int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
  825. {
  826. if (len > UINT_MAX) {
  827. DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
  828. (unsigned long long)len, UINT_MAX);
  829. ti->error = "Maximum size of target IO is too large";
  830. return -EINVAL;
  831. }
  832. ti->max_io_len = (uint32_t) len;
  833. return 0;
  834. }
  835. EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
  836. static struct dm_target *dm_dax_get_live_target(struct mapped_device *md,
  837. sector_t sector, int *srcu_idx)
  838. __acquires(md->io_barrier)
  839. {
  840. struct dm_table *map;
  841. struct dm_target *ti;
  842. map = dm_get_live_table(md, srcu_idx);
  843. if (!map)
  844. return NULL;
  845. ti = dm_table_find_target(map, sector);
  846. if (!dm_target_is_valid(ti))
  847. return NULL;
  848. return ti;
  849. }
  850. static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  851. long nr_pages, void **kaddr, pfn_t *pfn)
  852. {
  853. struct mapped_device *md = dax_get_private(dax_dev);
  854. sector_t sector = pgoff * PAGE_SECTORS;
  855. struct dm_target *ti;
  856. long len, ret = -EIO;
  857. int srcu_idx;
  858. ti = dm_dax_get_live_target(md, sector, &srcu_idx);
  859. if (!ti)
  860. goto out;
  861. if (!ti->type->direct_access)
  862. goto out;
  863. len = max_io_len(sector, ti) / PAGE_SECTORS;
  864. if (len < 1)
  865. goto out;
  866. nr_pages = min(len, nr_pages);
  867. ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
  868. out:
  869. dm_put_live_table(md, srcu_idx);
  870. return ret;
  871. }
  872. static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
  873. void *addr, size_t bytes, struct iov_iter *i)
  874. {
  875. struct mapped_device *md = dax_get_private(dax_dev);
  876. sector_t sector = pgoff * PAGE_SECTORS;
  877. struct dm_target *ti;
  878. long ret = 0;
  879. int srcu_idx;
  880. ti = dm_dax_get_live_target(md, sector, &srcu_idx);
  881. if (!ti)
  882. goto out;
  883. if (!ti->type->dax_copy_from_iter) {
  884. ret = copy_from_iter(addr, bytes, i);
  885. goto out;
  886. }
  887. ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
  888. out:
  889. dm_put_live_table(md, srcu_idx);
  890. return ret;
  891. }
  892. static size_t dm_dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff,
  893. void *addr, size_t bytes, struct iov_iter *i)
  894. {
  895. struct mapped_device *md = dax_get_private(dax_dev);
  896. sector_t sector = pgoff * PAGE_SECTORS;
  897. struct dm_target *ti;
  898. long ret = 0;
  899. int srcu_idx;
  900. ti = dm_dax_get_live_target(md, sector, &srcu_idx);
  901. if (!ti)
  902. goto out;
  903. if (!ti->type->dax_copy_to_iter) {
  904. ret = copy_to_iter(addr, bytes, i);
  905. goto out;
  906. }
  907. ret = ti->type->dax_copy_to_iter(ti, pgoff, addr, bytes, i);
  908. out:
  909. dm_put_live_table(md, srcu_idx);
  910. return ret;
  911. }
  912. /*
  913. * A target may call dm_accept_partial_bio only from the map routine. It is
  914. * allowed for all bio types except REQ_PREFLUSH and REQ_OP_ZONE_RESET.
  915. *
  916. * dm_accept_partial_bio informs the dm that the target only wants to process
  917. * additional n_sectors sectors of the bio and the rest of the data should be
  918. * sent in a next bio.
  919. *
  920. * A diagram that explains the arithmetics:
  921. * +--------------------+---------------+-------+
  922. * | 1 | 2 | 3 |
  923. * +--------------------+---------------+-------+
  924. *
  925. * <-------------- *tio->len_ptr --------------->
  926. * <------- bi_size ------->
  927. * <-- n_sectors -->
  928. *
  929. * Region 1 was already iterated over with bio_advance or similar function.
  930. * (it may be empty if the target doesn't use bio_advance)
  931. * Region 2 is the remaining bio size that the target wants to process.
  932. * (it may be empty if region 1 is non-empty, although there is no reason
  933. * to make it empty)
  934. * The target requires that region 3 is to be sent in the next bio.
  935. *
  936. * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
  937. * the partially processed part (the sum of regions 1+2) must be the same for all
  938. * copies of the bio.
  939. */
  940. void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
  941. {
  942. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  943. unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
  944. BUG_ON(bio->bi_opf & REQ_PREFLUSH);
  945. BUG_ON(bi_size > *tio->len_ptr);
  946. BUG_ON(n_sectors > bi_size);
  947. *tio->len_ptr -= bi_size - n_sectors;
  948. bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
  949. }
  950. EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
  951. /*
  952. * The zone descriptors obtained with a zone report indicate zone positions
  953. * within the target backing device, regardless of that device is a partition
  954. * and regardless of the target mapping start sector on the device or partition.
  955. * The zone descriptors start sector and write pointer position must be adjusted
  956. * to match their relative position within the dm device.
  957. * A target may call dm_remap_zone_report() after completion of a
  958. * REQ_OP_ZONE_REPORT bio to remap the zone descriptors obtained from the
  959. * backing device.
  960. */
  961. void dm_remap_zone_report(struct dm_target *ti, struct bio *bio, sector_t start)
  962. {
  963. #ifdef CONFIG_BLK_DEV_ZONED
  964. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  965. struct bio *report_bio = tio->io->orig_bio;
  966. struct blk_zone_report_hdr *hdr = NULL;
  967. struct blk_zone *zone;
  968. unsigned int nr_rep = 0;
  969. unsigned int ofst;
  970. sector_t part_offset;
  971. struct bio_vec bvec;
  972. struct bvec_iter iter;
  973. void *addr;
  974. if (bio->bi_status)
  975. return;
  976. /*
  977. * bio sector was incremented by the request size on completion. Taking
  978. * into account the original request sector, the target start offset on
  979. * the backing device and the target mapping offset (ti->begin), the
  980. * start sector of the backing device. The partition offset is always 0
  981. * if the target uses a whole device.
  982. */
  983. part_offset = bio->bi_iter.bi_sector + ti->begin - (start + bio_end_sector(report_bio));
  984. /*
  985. * Remap the start sector of the reported zones. For sequential zones,
  986. * also remap the write pointer position.
  987. */
  988. bio_for_each_segment(bvec, report_bio, iter) {
  989. addr = kmap_atomic(bvec.bv_page);
  990. /* Remember the report header in the first page */
  991. if (!hdr) {
  992. hdr = addr;
  993. ofst = sizeof(struct blk_zone_report_hdr);
  994. } else
  995. ofst = 0;
  996. /* Set zones start sector */
  997. while (hdr->nr_zones && ofst < bvec.bv_len) {
  998. zone = addr + ofst;
  999. zone->start -= part_offset;
  1000. if (zone->start >= start + ti->len) {
  1001. hdr->nr_zones = 0;
  1002. break;
  1003. }
  1004. zone->start = zone->start + ti->begin - start;
  1005. if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
  1006. if (zone->cond == BLK_ZONE_COND_FULL)
  1007. zone->wp = zone->start + zone->len;
  1008. else if (zone->cond == BLK_ZONE_COND_EMPTY)
  1009. zone->wp = zone->start;
  1010. else
  1011. zone->wp = zone->wp + ti->begin - start - part_offset;
  1012. }
  1013. ofst += sizeof(struct blk_zone);
  1014. hdr->nr_zones--;
  1015. nr_rep++;
  1016. }
  1017. if (addr != hdr)
  1018. kunmap_atomic(addr);
  1019. if (!hdr->nr_zones)
  1020. break;
  1021. }
  1022. if (hdr) {
  1023. hdr->nr_zones = nr_rep;
  1024. kunmap_atomic(hdr);
  1025. }
  1026. bio_advance(report_bio, report_bio->bi_iter.bi_size);
  1027. #else /* !CONFIG_BLK_DEV_ZONED */
  1028. bio->bi_status = BLK_STS_NOTSUPP;
  1029. #endif
  1030. }
  1031. EXPORT_SYMBOL_GPL(dm_remap_zone_report);
  1032. static blk_qc_t __map_bio(struct dm_target_io *tio)
  1033. {
  1034. int r;
  1035. sector_t sector;
  1036. struct bio *clone = &tio->clone;
  1037. struct dm_io *io = tio->io;
  1038. struct mapped_device *md = io->md;
  1039. struct dm_target *ti = tio->ti;
  1040. blk_qc_t ret = BLK_QC_T_NONE;
  1041. clone->bi_end_io = clone_endio;
  1042. /*
  1043. * Map the clone. If r == 0 we don't need to do
  1044. * anything, the target has assumed ownership of
  1045. * this io.
  1046. */
  1047. atomic_inc(&io->io_count);
  1048. sector = clone->bi_iter.bi_sector;
  1049. r = ti->type->map(ti, clone);
  1050. switch (r) {
  1051. case DM_MAPIO_SUBMITTED:
  1052. break;
  1053. case DM_MAPIO_REMAPPED:
  1054. /* the bio has been remapped so dispatch it */
  1055. trace_block_bio_remap(clone->bi_disk->queue, clone,
  1056. bio_dev(io->orig_bio), sector);
  1057. if (md->type == DM_TYPE_NVME_BIO_BASED)
  1058. ret = direct_make_request(clone);
  1059. else
  1060. ret = generic_make_request(clone);
  1061. break;
  1062. case DM_MAPIO_KILL:
  1063. free_tio(tio);
  1064. dec_pending(io, BLK_STS_IOERR);
  1065. break;
  1066. case DM_MAPIO_REQUEUE:
  1067. free_tio(tio);
  1068. dec_pending(io, BLK_STS_DM_REQUEUE);
  1069. break;
  1070. default:
  1071. DMWARN("unimplemented target map return value: %d", r);
  1072. BUG();
  1073. }
  1074. return ret;
  1075. }
  1076. static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
  1077. {
  1078. bio->bi_iter.bi_sector = sector;
  1079. bio->bi_iter.bi_size = to_bytes(len);
  1080. }
  1081. /*
  1082. * Creates a bio that consists of range of complete bvecs.
  1083. */
  1084. static int clone_bio(struct dm_target_io *tio, struct bio *bio,
  1085. sector_t sector, unsigned len)
  1086. {
  1087. struct bio *clone = &tio->clone;
  1088. __bio_clone_fast(clone, bio);
  1089. if (unlikely(bio_integrity(bio) != NULL)) {
  1090. int r;
  1091. if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
  1092. !dm_target_passes_integrity(tio->ti->type))) {
  1093. DMWARN("%s: the target %s doesn't support integrity data.",
  1094. dm_device_name(tio->io->md),
  1095. tio->ti->type->name);
  1096. return -EIO;
  1097. }
  1098. r = bio_integrity_clone(clone, bio, GFP_NOIO);
  1099. if (r < 0)
  1100. return r;
  1101. }
  1102. if (bio_op(bio) != REQ_OP_ZONE_REPORT)
  1103. bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
  1104. clone->bi_iter.bi_size = to_bytes(len);
  1105. if (unlikely(bio_integrity(bio) != NULL))
  1106. bio_integrity_trim(clone);
  1107. return 0;
  1108. }
  1109. static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
  1110. struct dm_target *ti, unsigned num_bios)
  1111. {
  1112. struct dm_target_io *tio;
  1113. int try;
  1114. if (!num_bios)
  1115. return;
  1116. if (num_bios == 1) {
  1117. tio = alloc_tio(ci, ti, 0, GFP_NOIO);
  1118. bio_list_add(blist, &tio->clone);
  1119. return;
  1120. }
  1121. for (try = 0; try < 2; try++) {
  1122. int bio_nr;
  1123. struct bio *bio;
  1124. if (try)
  1125. mutex_lock(&ci->io->md->table_devices_lock);
  1126. for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
  1127. tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
  1128. if (!tio)
  1129. break;
  1130. bio_list_add(blist, &tio->clone);
  1131. }
  1132. if (try)
  1133. mutex_unlock(&ci->io->md->table_devices_lock);
  1134. if (bio_nr == num_bios)
  1135. return;
  1136. while ((bio = bio_list_pop(blist))) {
  1137. tio = container_of(bio, struct dm_target_io, clone);
  1138. free_tio(tio);
  1139. }
  1140. }
  1141. }
  1142. static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci,
  1143. struct dm_target_io *tio, unsigned *len)
  1144. {
  1145. struct bio *clone = &tio->clone;
  1146. tio->len_ptr = len;
  1147. __bio_clone_fast(clone, ci->bio);
  1148. if (len)
  1149. bio_setup_sector(clone, ci->sector, *len);
  1150. return __map_bio(tio);
  1151. }
  1152. static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
  1153. unsigned num_bios, unsigned *len)
  1154. {
  1155. struct bio_list blist = BIO_EMPTY_LIST;
  1156. struct bio *bio;
  1157. struct dm_target_io *tio;
  1158. alloc_multiple_bios(&blist, ci, ti, num_bios);
  1159. while ((bio = bio_list_pop(&blist))) {
  1160. tio = container_of(bio, struct dm_target_io, clone);
  1161. (void) __clone_and_map_simple_bio(ci, tio, len);
  1162. }
  1163. }
  1164. static int __send_empty_flush(struct clone_info *ci)
  1165. {
  1166. unsigned target_nr = 0;
  1167. struct dm_target *ti;
  1168. BUG_ON(bio_has_data(ci->bio));
  1169. while ((ti = dm_table_get_target(ci->map, target_nr++)))
  1170. __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
  1171. return 0;
  1172. }
  1173. static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
  1174. sector_t sector, unsigned *len)
  1175. {
  1176. struct bio *bio = ci->bio;
  1177. struct dm_target_io *tio;
  1178. int r;
  1179. tio = alloc_tio(ci, ti, 0, GFP_NOIO);
  1180. tio->len_ptr = len;
  1181. r = clone_bio(tio, bio, sector, *len);
  1182. if (r < 0) {
  1183. free_tio(tio);
  1184. return r;
  1185. }
  1186. (void) __map_bio(tio);
  1187. return 0;
  1188. }
  1189. typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
  1190. static unsigned get_num_discard_bios(struct dm_target *ti)
  1191. {
  1192. return ti->num_discard_bios;
  1193. }
  1194. static unsigned get_num_secure_erase_bios(struct dm_target *ti)
  1195. {
  1196. return ti->num_secure_erase_bios;
  1197. }
  1198. static unsigned get_num_write_same_bios(struct dm_target *ti)
  1199. {
  1200. return ti->num_write_same_bios;
  1201. }
  1202. static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
  1203. {
  1204. return ti->num_write_zeroes_bios;
  1205. }
  1206. typedef bool (*is_split_required_fn)(struct dm_target *ti);
  1207. static bool is_split_required_for_discard(struct dm_target *ti)
  1208. {
  1209. return ti->split_discard_bios;
  1210. }
  1211. static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
  1212. get_num_bios_fn get_num_bios,
  1213. is_split_required_fn is_split_required)
  1214. {
  1215. unsigned len;
  1216. unsigned num_bios;
  1217. /*
  1218. * Even though the device advertised support for this type of
  1219. * request, that does not mean every target supports it, and
  1220. * reconfiguration might also have changed that since the
  1221. * check was performed.
  1222. */
  1223. num_bios = get_num_bios ? get_num_bios(ti) : 0;
  1224. if (!num_bios)
  1225. return -EOPNOTSUPP;
  1226. if (is_split_required && !is_split_required(ti))
  1227. len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
  1228. else
  1229. len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
  1230. __send_duplicate_bios(ci, ti, num_bios, &len);
  1231. ci->sector += len;
  1232. ci->sector_count -= len;
  1233. return 0;
  1234. }
  1235. static int __send_discard(struct clone_info *ci, struct dm_target *ti)
  1236. {
  1237. return __send_changing_extent_only(ci, ti, get_num_discard_bios,
  1238. is_split_required_for_discard);
  1239. }
  1240. static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti)
  1241. {
  1242. return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios, NULL);
  1243. }
  1244. static int __send_write_same(struct clone_info *ci, struct dm_target *ti)
  1245. {
  1246. return __send_changing_extent_only(ci, ti, get_num_write_same_bios, NULL);
  1247. }
  1248. static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti)
  1249. {
  1250. return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios, NULL);
  1251. }
  1252. static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
  1253. int *result)
  1254. {
  1255. struct bio *bio = ci->bio;
  1256. if (bio_op(bio) == REQ_OP_DISCARD)
  1257. *result = __send_discard(ci, ti);
  1258. else if (bio_op(bio) == REQ_OP_SECURE_ERASE)
  1259. *result = __send_secure_erase(ci, ti);
  1260. else if (bio_op(bio) == REQ_OP_WRITE_SAME)
  1261. *result = __send_write_same(ci, ti);
  1262. else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
  1263. *result = __send_write_zeroes(ci, ti);
  1264. else
  1265. return false;
  1266. return true;
  1267. }
  1268. /*
  1269. * Select the correct strategy for processing a non-flush bio.
  1270. */
  1271. static int __split_and_process_non_flush(struct clone_info *ci)
  1272. {
  1273. struct bio *bio = ci->bio;
  1274. struct dm_target *ti;
  1275. unsigned len;
  1276. int r;
  1277. ti = dm_table_find_target(ci->map, ci->sector);
  1278. if (!dm_target_is_valid(ti))
  1279. return -EIO;
  1280. if (unlikely(__process_abnormal_io(ci, ti, &r)))
  1281. return r;
  1282. if (bio_op(bio) == REQ_OP_ZONE_REPORT)
  1283. len = ci->sector_count;
  1284. else
  1285. len = min_t(sector_t, max_io_len(ci->sector, ti),
  1286. ci->sector_count);
  1287. r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
  1288. if (r < 0)
  1289. return r;
  1290. ci->sector += len;
  1291. ci->sector_count -= len;
  1292. return 0;
  1293. }
  1294. static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
  1295. struct dm_table *map, struct bio *bio)
  1296. {
  1297. ci->map = map;
  1298. ci->io = alloc_io(md, bio);
  1299. ci->sector = bio->bi_iter.bi_sector;
  1300. }
  1301. /*
  1302. * Entry point to split a bio into clones and submit them to the targets.
  1303. */
  1304. static blk_qc_t __split_and_process_bio(struct mapped_device *md,
  1305. struct dm_table *map, struct bio *bio)
  1306. {
  1307. struct clone_info ci;
  1308. blk_qc_t ret = BLK_QC_T_NONE;
  1309. int error = 0;
  1310. if (unlikely(!map)) {
  1311. bio_io_error(bio);
  1312. return ret;
  1313. }
  1314. blk_queue_split(md->queue, &bio);
  1315. init_clone_info(&ci, md, map, bio);
  1316. if (bio->bi_opf & REQ_PREFLUSH) {
  1317. ci.bio = &ci.io->md->flush_bio;
  1318. ci.sector_count = 0;
  1319. error = __send_empty_flush(&ci);
  1320. /* dec_pending submits any data associated with flush */
  1321. } else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
  1322. ci.bio = bio;
  1323. ci.sector_count = 0;
  1324. error = __split_and_process_non_flush(&ci);
  1325. } else {
  1326. ci.bio = bio;
  1327. ci.sector_count = bio_sectors(bio);
  1328. while (ci.sector_count && !error) {
  1329. error = __split_and_process_non_flush(&ci);
  1330. if (current->bio_list && ci.sector_count && !error) {
  1331. /*
  1332. * Remainder must be passed to generic_make_request()
  1333. * so that it gets handled *after* bios already submitted
  1334. * have been completely processed.
  1335. * We take a clone of the original to store in
  1336. * ci.io->orig_bio to be used by end_io_acct() and
  1337. * for dec_pending to use for completion handling.
  1338. * As this path is not used for REQ_OP_ZONE_REPORT,
  1339. * the usage of io->orig_bio in dm_remap_zone_report()
  1340. * won't be affected by this reassignment.
  1341. */
  1342. struct bio *b = bio_split(bio, bio_sectors(bio) - ci.sector_count,
  1343. GFP_NOIO, &md->queue->bio_split);
  1344. ci.io->orig_bio = b;
  1345. bio_chain(b, bio);
  1346. ret = generic_make_request(bio);
  1347. break;
  1348. }
  1349. }
  1350. }
  1351. /* drop the extra reference count */
  1352. dec_pending(ci.io, errno_to_blk_status(error));
  1353. return ret;
  1354. }
  1355. /*
  1356. * Optimized variant of __split_and_process_bio that leverages the
  1357. * fact that targets that use it do _not_ have a need to split bios.
  1358. */
  1359. static blk_qc_t __process_bio(struct mapped_device *md,
  1360. struct dm_table *map, struct bio *bio)
  1361. {
  1362. struct clone_info ci;
  1363. blk_qc_t ret = BLK_QC_T_NONE;
  1364. int error = 0;
  1365. if (unlikely(!map)) {
  1366. bio_io_error(bio);
  1367. return ret;
  1368. }
  1369. init_clone_info(&ci, md, map, bio);
  1370. if (bio->bi_opf & REQ_PREFLUSH) {
  1371. ci.bio = &ci.io->md->flush_bio;
  1372. ci.sector_count = 0;
  1373. error = __send_empty_flush(&ci);
  1374. /* dec_pending submits any data associated with flush */
  1375. } else {
  1376. struct dm_target *ti = md->immutable_target;
  1377. struct dm_target_io *tio;
  1378. /*
  1379. * Defend against IO still getting in during teardown
  1380. * - as was seen for a time with nvme-fcloop
  1381. */
  1382. if (unlikely(WARN_ON_ONCE(!ti || !dm_target_is_valid(ti)))) {
  1383. error = -EIO;
  1384. goto out;
  1385. }
  1386. ci.bio = bio;
  1387. ci.sector_count = bio_sectors(bio);
  1388. if (unlikely(__process_abnormal_io(&ci, ti, &error)))
  1389. goto out;
  1390. tio = alloc_tio(&ci, ti, 0, GFP_NOIO);
  1391. ret = __clone_and_map_simple_bio(&ci, tio, NULL);
  1392. }
  1393. out:
  1394. /* drop the extra reference count */
  1395. dec_pending(ci.io, errno_to_blk_status(error));
  1396. return ret;
  1397. }
  1398. typedef blk_qc_t (process_bio_fn)(struct mapped_device *, struct dm_table *, struct bio *);
  1399. static blk_qc_t __dm_make_request(struct request_queue *q, struct bio *bio,
  1400. process_bio_fn process_bio)
  1401. {
  1402. struct mapped_device *md = q->queuedata;
  1403. blk_qc_t ret = BLK_QC_T_NONE;
  1404. int srcu_idx;
  1405. struct dm_table *map;
  1406. map = dm_get_live_table(md, &srcu_idx);
  1407. /* if we're suspended, we have to queue this io for later */
  1408. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
  1409. dm_put_live_table(md, srcu_idx);
  1410. if (!(bio->bi_opf & REQ_RAHEAD))
  1411. queue_io(md, bio);
  1412. else
  1413. bio_io_error(bio);
  1414. return ret;
  1415. }
  1416. ret = process_bio(md, map, bio);
  1417. dm_put_live_table(md, srcu_idx);
  1418. return ret;
  1419. }
  1420. /*
  1421. * The request function that remaps the bio to one target and
  1422. * splits off any remainder.
  1423. */
  1424. static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
  1425. {
  1426. return __dm_make_request(q, bio, __split_and_process_bio);
  1427. }
  1428. static blk_qc_t dm_make_request_nvme(struct request_queue *q, struct bio *bio)
  1429. {
  1430. return __dm_make_request(q, bio, __process_bio);
  1431. }
  1432. static int dm_any_congested(void *congested_data, int bdi_bits)
  1433. {
  1434. int r = bdi_bits;
  1435. struct mapped_device *md = congested_data;
  1436. struct dm_table *map;
  1437. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1438. if (dm_request_based(md)) {
  1439. /*
  1440. * With request-based DM we only need to check the
  1441. * top-level queue for congestion.
  1442. */
  1443. r = md->queue->backing_dev_info->wb.state & bdi_bits;
  1444. } else {
  1445. map = dm_get_live_table_fast(md);
  1446. if (map)
  1447. r = dm_table_any_congested(map, bdi_bits);
  1448. dm_put_live_table_fast(md);
  1449. }
  1450. }
  1451. return r;
  1452. }
  1453. /*-----------------------------------------------------------------
  1454. * An IDR is used to keep track of allocated minor numbers.
  1455. *---------------------------------------------------------------*/
  1456. static void free_minor(int minor)
  1457. {
  1458. spin_lock(&_minor_lock);
  1459. idr_remove(&_minor_idr, minor);
  1460. spin_unlock(&_minor_lock);
  1461. }
  1462. /*
  1463. * See if the device with a specific minor # is free.
  1464. */
  1465. static int specific_minor(int minor)
  1466. {
  1467. int r;
  1468. if (minor >= (1 << MINORBITS))
  1469. return -EINVAL;
  1470. idr_preload(GFP_KERNEL);
  1471. spin_lock(&_minor_lock);
  1472. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
  1473. spin_unlock(&_minor_lock);
  1474. idr_preload_end();
  1475. if (r < 0)
  1476. return r == -ENOSPC ? -EBUSY : r;
  1477. return 0;
  1478. }
  1479. static int next_free_minor(int *minor)
  1480. {
  1481. int r;
  1482. idr_preload(GFP_KERNEL);
  1483. spin_lock(&_minor_lock);
  1484. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
  1485. spin_unlock(&_minor_lock);
  1486. idr_preload_end();
  1487. if (r < 0)
  1488. return r;
  1489. *minor = r;
  1490. return 0;
  1491. }
  1492. static const struct block_device_operations dm_blk_dops;
  1493. static const struct dax_operations dm_dax_ops;
  1494. static void dm_wq_work(struct work_struct *work);
  1495. static void dm_init_normal_md_queue(struct mapped_device *md)
  1496. {
  1497. md->use_blk_mq = false;
  1498. /*
  1499. * Initialize aspects of queue that aren't relevant for blk-mq
  1500. */
  1501. md->queue->backing_dev_info->congested_fn = dm_any_congested;
  1502. }
  1503. static void cleanup_mapped_device(struct mapped_device *md)
  1504. {
  1505. if (md->wq)
  1506. destroy_workqueue(md->wq);
  1507. if (md->kworker_task)
  1508. kthread_stop(md->kworker_task);
  1509. bioset_exit(&md->bs);
  1510. bioset_exit(&md->io_bs);
  1511. if (md->dax_dev) {
  1512. kill_dax(md->dax_dev);
  1513. put_dax(md->dax_dev);
  1514. md->dax_dev = NULL;
  1515. }
  1516. if (md->disk) {
  1517. spin_lock(&_minor_lock);
  1518. md->disk->private_data = NULL;
  1519. spin_unlock(&_minor_lock);
  1520. del_gendisk(md->disk);
  1521. put_disk(md->disk);
  1522. }
  1523. if (md->queue)
  1524. blk_cleanup_queue(md->queue);
  1525. cleanup_srcu_struct(&md->io_barrier);
  1526. if (md->bdev) {
  1527. bdput(md->bdev);
  1528. md->bdev = NULL;
  1529. }
  1530. mutex_destroy(&md->suspend_lock);
  1531. mutex_destroy(&md->type_lock);
  1532. mutex_destroy(&md->table_devices_lock);
  1533. dm_mq_cleanup_mapped_device(md);
  1534. }
  1535. /*
  1536. * Allocate and initialise a blank device with a given minor.
  1537. */
  1538. static struct mapped_device *alloc_dev(int minor)
  1539. {
  1540. int r, numa_node_id = dm_get_numa_node();
  1541. struct dax_device *dax_dev = NULL;
  1542. struct mapped_device *md;
  1543. void *old_md;
  1544. md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
  1545. if (!md) {
  1546. DMWARN("unable to allocate device, out of memory.");
  1547. return NULL;
  1548. }
  1549. if (!try_module_get(THIS_MODULE))
  1550. goto bad_module_get;
  1551. /* get a minor number for the dev */
  1552. if (minor == DM_ANY_MINOR)
  1553. r = next_free_minor(&minor);
  1554. else
  1555. r = specific_minor(minor);
  1556. if (r < 0)
  1557. goto bad_minor;
  1558. r = init_srcu_struct(&md->io_barrier);
  1559. if (r < 0)
  1560. goto bad_io_barrier;
  1561. md->numa_node_id = numa_node_id;
  1562. md->use_blk_mq = dm_use_blk_mq_default();
  1563. md->init_tio_pdu = false;
  1564. md->type = DM_TYPE_NONE;
  1565. mutex_init(&md->suspend_lock);
  1566. mutex_init(&md->type_lock);
  1567. mutex_init(&md->table_devices_lock);
  1568. spin_lock_init(&md->deferred_lock);
  1569. atomic_set(&md->holders, 1);
  1570. atomic_set(&md->open_count, 0);
  1571. atomic_set(&md->event_nr, 0);
  1572. atomic_set(&md->uevent_seq, 0);
  1573. INIT_LIST_HEAD(&md->uevent_list);
  1574. INIT_LIST_HEAD(&md->table_devices);
  1575. spin_lock_init(&md->uevent_lock);
  1576. md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id, NULL);
  1577. if (!md->queue)
  1578. goto bad;
  1579. md->queue->queuedata = md;
  1580. md->queue->backing_dev_info->congested_data = md;
  1581. md->disk = alloc_disk_node(1, md->numa_node_id);
  1582. if (!md->disk)
  1583. goto bad;
  1584. atomic_set(&md->pending[0], 0);
  1585. atomic_set(&md->pending[1], 0);
  1586. init_waitqueue_head(&md->wait);
  1587. INIT_WORK(&md->work, dm_wq_work);
  1588. init_waitqueue_head(&md->eventq);
  1589. init_completion(&md->kobj_holder.completion);
  1590. md->kworker_task = NULL;
  1591. md->disk->major = _major;
  1592. md->disk->first_minor = minor;
  1593. md->disk->fops = &dm_blk_dops;
  1594. md->disk->queue = md->queue;
  1595. md->disk->private_data = md;
  1596. sprintf(md->disk->disk_name, "dm-%d", minor);
  1597. if (IS_ENABLED(CONFIG_DAX_DRIVER)) {
  1598. dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
  1599. if (!dax_dev)
  1600. goto bad;
  1601. }
  1602. md->dax_dev = dax_dev;
  1603. add_disk_no_queue_reg(md->disk);
  1604. format_dev_t(md->name, MKDEV(_major, minor));
  1605. md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
  1606. if (!md->wq)
  1607. goto bad;
  1608. md->bdev = bdget_disk(md->disk, 0);
  1609. if (!md->bdev)
  1610. goto bad;
  1611. bio_init(&md->flush_bio, NULL, 0);
  1612. bio_set_dev(&md->flush_bio, md->bdev);
  1613. md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
  1614. dm_stats_init(&md->stats);
  1615. /* Populate the mapping, nobody knows we exist yet */
  1616. spin_lock(&_minor_lock);
  1617. old_md = idr_replace(&_minor_idr, md, minor);
  1618. spin_unlock(&_minor_lock);
  1619. BUG_ON(old_md != MINOR_ALLOCED);
  1620. return md;
  1621. bad:
  1622. cleanup_mapped_device(md);
  1623. bad_io_barrier:
  1624. free_minor(minor);
  1625. bad_minor:
  1626. module_put(THIS_MODULE);
  1627. bad_module_get:
  1628. kvfree(md);
  1629. return NULL;
  1630. }
  1631. static void unlock_fs(struct mapped_device *md);
  1632. static void free_dev(struct mapped_device *md)
  1633. {
  1634. int minor = MINOR(disk_devt(md->disk));
  1635. unlock_fs(md);
  1636. cleanup_mapped_device(md);
  1637. free_table_devices(&md->table_devices);
  1638. dm_stats_cleanup(&md->stats);
  1639. free_minor(minor);
  1640. module_put(THIS_MODULE);
  1641. kvfree(md);
  1642. }
  1643. static int __bind_mempools(struct mapped_device *md, struct dm_table *t)
  1644. {
  1645. struct dm_md_mempools *p = dm_table_get_md_mempools(t);
  1646. int ret = 0;
  1647. if (dm_table_bio_based(t)) {
  1648. /*
  1649. * The md may already have mempools that need changing.
  1650. * If so, reload bioset because front_pad may have changed
  1651. * because a different table was loaded.
  1652. */
  1653. bioset_exit(&md->bs);
  1654. bioset_exit(&md->io_bs);
  1655. } else if (bioset_initialized(&md->bs)) {
  1656. /*
  1657. * There's no need to reload with request-based dm
  1658. * because the size of front_pad doesn't change.
  1659. * Note for future: If you are to reload bioset,
  1660. * prep-ed requests in the queue may refer
  1661. * to bio from the old bioset, so you must walk
  1662. * through the queue to unprep.
  1663. */
  1664. goto out;
  1665. }
  1666. BUG_ON(!p ||
  1667. bioset_initialized(&md->bs) ||
  1668. bioset_initialized(&md->io_bs));
  1669. ret = bioset_init_from_src(&md->bs, &p->bs);
  1670. if (ret)
  1671. goto out;
  1672. ret = bioset_init_from_src(&md->io_bs, &p->io_bs);
  1673. if (ret)
  1674. bioset_exit(&md->bs);
  1675. out:
  1676. /* mempool bind completed, no longer need any mempools in the table */
  1677. dm_table_free_md_mempools(t);
  1678. return ret;
  1679. }
  1680. /*
  1681. * Bind a table to the device.
  1682. */
  1683. static void event_callback(void *context)
  1684. {
  1685. unsigned long flags;
  1686. LIST_HEAD(uevents);
  1687. struct mapped_device *md = (struct mapped_device *) context;
  1688. spin_lock_irqsave(&md->uevent_lock, flags);
  1689. list_splice_init(&md->uevent_list, &uevents);
  1690. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1691. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  1692. atomic_inc(&md->event_nr);
  1693. wake_up(&md->eventq);
  1694. dm_issue_global_event();
  1695. }
  1696. /*
  1697. * Protected by md->suspend_lock obtained by dm_swap_table().
  1698. */
  1699. static void __set_size(struct mapped_device *md, sector_t size)
  1700. {
  1701. lockdep_assert_held(&md->suspend_lock);
  1702. set_capacity(md->disk, size);
  1703. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  1704. }
  1705. /*
  1706. * Returns old map, which caller must destroy.
  1707. */
  1708. static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
  1709. struct queue_limits *limits)
  1710. {
  1711. struct dm_table *old_map;
  1712. struct request_queue *q = md->queue;
  1713. bool request_based = dm_table_request_based(t);
  1714. sector_t size;
  1715. int ret;
  1716. lockdep_assert_held(&md->suspend_lock);
  1717. size = dm_table_get_size(t);
  1718. /*
  1719. * Wipe any geometry if the size of the table changed.
  1720. */
  1721. if (size != dm_get_size(md))
  1722. memset(&md->geometry, 0, sizeof(md->geometry));
  1723. __set_size(md, size);
  1724. dm_table_event_callback(t, event_callback, md);
  1725. /*
  1726. * The queue hasn't been stopped yet, if the old table type wasn't
  1727. * for request-based during suspension. So stop it to prevent
  1728. * I/O mapping before resume.
  1729. * This must be done before setting the queue restrictions,
  1730. * because request-based dm may be run just after the setting.
  1731. */
  1732. if (request_based)
  1733. dm_stop_queue(q);
  1734. if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) {
  1735. /*
  1736. * Leverage the fact that request-based DM targets and
  1737. * NVMe bio based targets are immutable singletons
  1738. * - used to optimize both dm_request_fn and dm_mq_queue_rq;
  1739. * and __process_bio.
  1740. */
  1741. md->immutable_target = dm_table_get_immutable_target(t);
  1742. }
  1743. ret = __bind_mempools(md, t);
  1744. if (ret) {
  1745. old_map = ERR_PTR(ret);
  1746. goto out;
  1747. }
  1748. old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  1749. rcu_assign_pointer(md->map, (void *)t);
  1750. md->immutable_target_type = dm_table_get_immutable_target_type(t);
  1751. dm_table_set_restrictions(t, q, limits);
  1752. if (old_map)
  1753. dm_sync_table(md);
  1754. out:
  1755. return old_map;
  1756. }
  1757. /*
  1758. * Returns unbound table for the caller to free.
  1759. */
  1760. static struct dm_table *__unbind(struct mapped_device *md)
  1761. {
  1762. struct dm_table *map = rcu_dereference_protected(md->map, 1);
  1763. if (!map)
  1764. return NULL;
  1765. dm_table_event_callback(map, NULL, NULL);
  1766. RCU_INIT_POINTER(md->map, NULL);
  1767. dm_sync_table(md);
  1768. return map;
  1769. }
  1770. /*
  1771. * Constructor for a new device.
  1772. */
  1773. int dm_create(int minor, struct mapped_device **result)
  1774. {
  1775. int r;
  1776. struct mapped_device *md;
  1777. md = alloc_dev(minor);
  1778. if (!md)
  1779. return -ENXIO;
  1780. r = dm_sysfs_init(md);
  1781. if (r) {
  1782. free_dev(md);
  1783. return r;
  1784. }
  1785. *result = md;
  1786. return 0;
  1787. }
  1788. /*
  1789. * Functions to manage md->type.
  1790. * All are required to hold md->type_lock.
  1791. */
  1792. void dm_lock_md_type(struct mapped_device *md)
  1793. {
  1794. mutex_lock(&md->type_lock);
  1795. }
  1796. void dm_unlock_md_type(struct mapped_device *md)
  1797. {
  1798. mutex_unlock(&md->type_lock);
  1799. }
  1800. void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
  1801. {
  1802. BUG_ON(!mutex_is_locked(&md->type_lock));
  1803. md->type = type;
  1804. }
  1805. enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
  1806. {
  1807. return md->type;
  1808. }
  1809. struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
  1810. {
  1811. return md->immutable_target_type;
  1812. }
  1813. /*
  1814. * The queue_limits are only valid as long as you have a reference
  1815. * count on 'md'.
  1816. */
  1817. struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
  1818. {
  1819. BUG_ON(!atomic_read(&md->holders));
  1820. return &md->queue->limits;
  1821. }
  1822. EXPORT_SYMBOL_GPL(dm_get_queue_limits);
  1823. /*
  1824. * Setup the DM device's queue based on md's type
  1825. */
  1826. int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
  1827. {
  1828. int r;
  1829. struct queue_limits limits;
  1830. enum dm_queue_mode type = dm_get_md_type(md);
  1831. switch (type) {
  1832. case DM_TYPE_REQUEST_BASED:
  1833. dm_init_normal_md_queue(md);
  1834. r = dm_old_init_request_queue(md, t);
  1835. if (r) {
  1836. DMERR("Cannot initialize queue for request-based mapped device");
  1837. return r;
  1838. }
  1839. break;
  1840. case DM_TYPE_MQ_REQUEST_BASED:
  1841. r = dm_mq_init_request_queue(md, t);
  1842. if (r) {
  1843. DMERR("Cannot initialize queue for request-based dm-mq mapped device");
  1844. return r;
  1845. }
  1846. break;
  1847. case DM_TYPE_BIO_BASED:
  1848. case DM_TYPE_DAX_BIO_BASED:
  1849. dm_init_normal_md_queue(md);
  1850. blk_queue_make_request(md->queue, dm_make_request);
  1851. break;
  1852. case DM_TYPE_NVME_BIO_BASED:
  1853. dm_init_normal_md_queue(md);
  1854. blk_queue_make_request(md->queue, dm_make_request_nvme);
  1855. break;
  1856. case DM_TYPE_NONE:
  1857. WARN_ON_ONCE(true);
  1858. break;
  1859. }
  1860. r = dm_calculate_queue_limits(t, &limits);
  1861. if (r) {
  1862. DMERR("Cannot calculate initial queue limits");
  1863. return r;
  1864. }
  1865. dm_table_set_restrictions(t, md->queue, &limits);
  1866. blk_register_queue(md->disk);
  1867. return 0;
  1868. }
  1869. struct mapped_device *dm_get_md(dev_t dev)
  1870. {
  1871. struct mapped_device *md;
  1872. unsigned minor = MINOR(dev);
  1873. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  1874. return NULL;
  1875. spin_lock(&_minor_lock);
  1876. md = idr_find(&_minor_idr, minor);
  1877. if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
  1878. test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
  1879. md = NULL;
  1880. goto out;
  1881. }
  1882. dm_get(md);
  1883. out:
  1884. spin_unlock(&_minor_lock);
  1885. return md;
  1886. }
  1887. EXPORT_SYMBOL_GPL(dm_get_md);
  1888. void *dm_get_mdptr(struct mapped_device *md)
  1889. {
  1890. return md->interface_ptr;
  1891. }
  1892. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  1893. {
  1894. md->interface_ptr = ptr;
  1895. }
  1896. void dm_get(struct mapped_device *md)
  1897. {
  1898. atomic_inc(&md->holders);
  1899. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  1900. }
  1901. int dm_hold(struct mapped_device *md)
  1902. {
  1903. spin_lock(&_minor_lock);
  1904. if (test_bit(DMF_FREEING, &md->flags)) {
  1905. spin_unlock(&_minor_lock);
  1906. return -EBUSY;
  1907. }
  1908. dm_get(md);
  1909. spin_unlock(&_minor_lock);
  1910. return 0;
  1911. }
  1912. EXPORT_SYMBOL_GPL(dm_hold);
  1913. const char *dm_device_name(struct mapped_device *md)
  1914. {
  1915. return md->name;
  1916. }
  1917. EXPORT_SYMBOL_GPL(dm_device_name);
  1918. static void __dm_destroy(struct mapped_device *md, bool wait)
  1919. {
  1920. struct dm_table *map;
  1921. int srcu_idx;
  1922. might_sleep();
  1923. spin_lock(&_minor_lock);
  1924. idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
  1925. set_bit(DMF_FREEING, &md->flags);
  1926. spin_unlock(&_minor_lock);
  1927. blk_set_queue_dying(md->queue);
  1928. if (dm_request_based(md) && md->kworker_task)
  1929. kthread_flush_worker(&md->kworker);
  1930. /*
  1931. * Take suspend_lock so that presuspend and postsuspend methods
  1932. * do not race with internal suspend.
  1933. */
  1934. mutex_lock(&md->suspend_lock);
  1935. map = dm_get_live_table(md, &srcu_idx);
  1936. if (!dm_suspended_md(md)) {
  1937. dm_table_presuspend_targets(map);
  1938. dm_table_postsuspend_targets(map);
  1939. }
  1940. /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
  1941. dm_put_live_table(md, srcu_idx);
  1942. mutex_unlock(&md->suspend_lock);
  1943. /*
  1944. * Rare, but there may be I/O requests still going to complete,
  1945. * for example. Wait for all references to disappear.
  1946. * No one should increment the reference count of the mapped_device,
  1947. * after the mapped_device state becomes DMF_FREEING.
  1948. */
  1949. if (wait)
  1950. while (atomic_read(&md->holders))
  1951. msleep(1);
  1952. else if (atomic_read(&md->holders))
  1953. DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
  1954. dm_device_name(md), atomic_read(&md->holders));
  1955. dm_sysfs_exit(md);
  1956. dm_table_destroy(__unbind(md));
  1957. free_dev(md);
  1958. }
  1959. void dm_destroy(struct mapped_device *md)
  1960. {
  1961. __dm_destroy(md, true);
  1962. }
  1963. void dm_destroy_immediate(struct mapped_device *md)
  1964. {
  1965. __dm_destroy(md, false);
  1966. }
  1967. void dm_put(struct mapped_device *md)
  1968. {
  1969. atomic_dec(&md->holders);
  1970. }
  1971. EXPORT_SYMBOL_GPL(dm_put);
  1972. static int dm_wait_for_completion(struct mapped_device *md, long task_state)
  1973. {
  1974. int r = 0;
  1975. DEFINE_WAIT(wait);
  1976. while (1) {
  1977. prepare_to_wait(&md->wait, &wait, task_state);
  1978. if (!md_in_flight(md))
  1979. break;
  1980. if (signal_pending_state(task_state, current)) {
  1981. r = -EINTR;
  1982. break;
  1983. }
  1984. io_schedule();
  1985. }
  1986. finish_wait(&md->wait, &wait);
  1987. return r;
  1988. }
  1989. /*
  1990. * Process the deferred bios
  1991. */
  1992. static void dm_wq_work(struct work_struct *work)
  1993. {
  1994. struct mapped_device *md = container_of(work, struct mapped_device,
  1995. work);
  1996. struct bio *c;
  1997. int srcu_idx;
  1998. struct dm_table *map;
  1999. map = dm_get_live_table(md, &srcu_idx);
  2000. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  2001. spin_lock_irq(&md->deferred_lock);
  2002. c = bio_list_pop(&md->deferred);
  2003. spin_unlock_irq(&md->deferred_lock);
  2004. if (!c)
  2005. break;
  2006. if (dm_request_based(md))
  2007. generic_make_request(c);
  2008. else
  2009. __split_and_process_bio(md, map, c);
  2010. }
  2011. dm_put_live_table(md, srcu_idx);
  2012. }
  2013. static void dm_queue_flush(struct mapped_device *md)
  2014. {
  2015. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2016. smp_mb__after_atomic();
  2017. queue_work(md->wq, &md->work);
  2018. }
  2019. /*
  2020. * Swap in a new table, returning the old one for the caller to destroy.
  2021. */
  2022. struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
  2023. {
  2024. struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
  2025. struct queue_limits limits;
  2026. int r;
  2027. mutex_lock(&md->suspend_lock);
  2028. /* device must be suspended */
  2029. if (!dm_suspended_md(md))
  2030. goto out;
  2031. /*
  2032. * If the new table has no data devices, retain the existing limits.
  2033. * This helps multipath with queue_if_no_path if all paths disappear,
  2034. * then new I/O is queued based on these limits, and then some paths
  2035. * reappear.
  2036. */
  2037. if (dm_table_has_no_data_devices(table)) {
  2038. live_map = dm_get_live_table_fast(md);
  2039. if (live_map)
  2040. limits = md->queue->limits;
  2041. dm_put_live_table_fast(md);
  2042. }
  2043. if (!live_map) {
  2044. r = dm_calculate_queue_limits(table, &limits);
  2045. if (r) {
  2046. map = ERR_PTR(r);
  2047. goto out;
  2048. }
  2049. }
  2050. map = __bind(md, table, &limits);
  2051. dm_issue_global_event();
  2052. out:
  2053. mutex_unlock(&md->suspend_lock);
  2054. return map;
  2055. }
  2056. /*
  2057. * Functions to lock and unlock any filesystem running on the
  2058. * device.
  2059. */
  2060. static int lock_fs(struct mapped_device *md)
  2061. {
  2062. int r;
  2063. WARN_ON(md->frozen_sb);
  2064. md->frozen_sb = freeze_bdev(md->bdev);
  2065. if (IS_ERR(md->frozen_sb)) {
  2066. r = PTR_ERR(md->frozen_sb);
  2067. md->frozen_sb = NULL;
  2068. return r;
  2069. }
  2070. set_bit(DMF_FROZEN, &md->flags);
  2071. return 0;
  2072. }
  2073. static void unlock_fs(struct mapped_device *md)
  2074. {
  2075. if (!test_bit(DMF_FROZEN, &md->flags))
  2076. return;
  2077. thaw_bdev(md->bdev, md->frozen_sb);
  2078. md->frozen_sb = NULL;
  2079. clear_bit(DMF_FROZEN, &md->flags);
  2080. }
  2081. /*
  2082. * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
  2083. * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
  2084. * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
  2085. *
  2086. * If __dm_suspend returns 0, the device is completely quiescent
  2087. * now. There is no request-processing activity. All new requests
  2088. * are being added to md->deferred list.
  2089. */
  2090. static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
  2091. unsigned suspend_flags, long task_state,
  2092. int dmf_suspended_flag)
  2093. {
  2094. bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
  2095. bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
  2096. int r;
  2097. lockdep_assert_held(&md->suspend_lock);
  2098. /*
  2099. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  2100. * This flag is cleared before dm_suspend returns.
  2101. */
  2102. if (noflush)
  2103. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2104. else
  2105. pr_debug("%s: suspending with flush\n", dm_device_name(md));
  2106. /*
  2107. * This gets reverted if there's an error later and the targets
  2108. * provide the .presuspend_undo hook.
  2109. */
  2110. dm_table_presuspend_targets(map);
  2111. /*
  2112. * Flush I/O to the device.
  2113. * Any I/O submitted after lock_fs() may not be flushed.
  2114. * noflush takes precedence over do_lockfs.
  2115. * (lock_fs() flushes I/Os and waits for them to complete.)
  2116. */
  2117. if (!noflush && do_lockfs) {
  2118. r = lock_fs(md);
  2119. if (r) {
  2120. dm_table_presuspend_undo_targets(map);
  2121. return r;
  2122. }
  2123. }
  2124. /*
  2125. * Here we must make sure that no processes are submitting requests
  2126. * to target drivers i.e. no one may be executing
  2127. * __split_and_process_bio. This is called from dm_request and
  2128. * dm_wq_work.
  2129. *
  2130. * To get all processes out of __split_and_process_bio in dm_request,
  2131. * we take the write lock. To prevent any process from reentering
  2132. * __split_and_process_bio from dm_request and quiesce the thread
  2133. * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
  2134. * flush_workqueue(md->wq).
  2135. */
  2136. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2137. if (map)
  2138. synchronize_srcu(&md->io_barrier);
  2139. /*
  2140. * Stop md->queue before flushing md->wq in case request-based
  2141. * dm defers requests to md->wq from md->queue.
  2142. */
  2143. if (dm_request_based(md)) {
  2144. dm_stop_queue(md->queue);
  2145. if (md->kworker_task)
  2146. kthread_flush_worker(&md->kworker);
  2147. }
  2148. flush_workqueue(md->wq);
  2149. /*
  2150. * At this point no more requests are entering target request routines.
  2151. * We call dm_wait_for_completion to wait for all existing requests
  2152. * to finish.
  2153. */
  2154. r = dm_wait_for_completion(md, task_state);
  2155. if (!r)
  2156. set_bit(dmf_suspended_flag, &md->flags);
  2157. if (noflush)
  2158. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2159. if (map)
  2160. synchronize_srcu(&md->io_barrier);
  2161. /* were we interrupted ? */
  2162. if (r < 0) {
  2163. dm_queue_flush(md);
  2164. if (dm_request_based(md))
  2165. dm_start_queue(md->queue);
  2166. unlock_fs(md);
  2167. dm_table_presuspend_undo_targets(map);
  2168. /* pushback list is already flushed, so skip flush */
  2169. }
  2170. return r;
  2171. }
  2172. /*
  2173. * We need to be able to change a mapping table under a mounted
  2174. * filesystem. For example we might want to move some data in
  2175. * the background. Before the table can be swapped with
  2176. * dm_bind_table, dm_suspend must be called to flush any in
  2177. * flight bios and ensure that any further io gets deferred.
  2178. */
  2179. /*
  2180. * Suspend mechanism in request-based dm.
  2181. *
  2182. * 1. Flush all I/Os by lock_fs() if needed.
  2183. * 2. Stop dispatching any I/O by stopping the request_queue.
  2184. * 3. Wait for all in-flight I/Os to be completed or requeued.
  2185. *
  2186. * To abort suspend, start the request_queue.
  2187. */
  2188. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  2189. {
  2190. struct dm_table *map = NULL;
  2191. int r = 0;
  2192. retry:
  2193. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  2194. if (dm_suspended_md(md)) {
  2195. r = -EINVAL;
  2196. goto out_unlock;
  2197. }
  2198. if (dm_suspended_internally_md(md)) {
  2199. /* already internally suspended, wait for internal resume */
  2200. mutex_unlock(&md->suspend_lock);
  2201. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  2202. if (r)
  2203. return r;
  2204. goto retry;
  2205. }
  2206. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2207. r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
  2208. if (r)
  2209. goto out_unlock;
  2210. dm_table_postsuspend_targets(map);
  2211. out_unlock:
  2212. mutex_unlock(&md->suspend_lock);
  2213. return r;
  2214. }
  2215. static int __dm_resume(struct mapped_device *md, struct dm_table *map)
  2216. {
  2217. if (map) {
  2218. int r = dm_table_resume_targets(map);
  2219. if (r)
  2220. return r;
  2221. }
  2222. dm_queue_flush(md);
  2223. /*
  2224. * Flushing deferred I/Os must be done after targets are resumed
  2225. * so that mapping of targets can work correctly.
  2226. * Request-based dm is queueing the deferred I/Os in its request_queue.
  2227. */
  2228. if (dm_request_based(md))
  2229. dm_start_queue(md->queue);
  2230. unlock_fs(md);
  2231. return 0;
  2232. }
  2233. int dm_resume(struct mapped_device *md)
  2234. {
  2235. int r;
  2236. struct dm_table *map = NULL;
  2237. retry:
  2238. r = -EINVAL;
  2239. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  2240. if (!dm_suspended_md(md))
  2241. goto out;
  2242. if (dm_suspended_internally_md(md)) {
  2243. /* already internally suspended, wait for internal resume */
  2244. mutex_unlock(&md->suspend_lock);
  2245. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  2246. if (r)
  2247. return r;
  2248. goto retry;
  2249. }
  2250. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2251. if (!map || !dm_table_get_size(map))
  2252. goto out;
  2253. r = __dm_resume(md, map);
  2254. if (r)
  2255. goto out;
  2256. clear_bit(DMF_SUSPENDED, &md->flags);
  2257. out:
  2258. mutex_unlock(&md->suspend_lock);
  2259. return r;
  2260. }
  2261. /*
  2262. * Internal suspend/resume works like userspace-driven suspend. It waits
  2263. * until all bios finish and prevents issuing new bios to the target drivers.
  2264. * It may be used only from the kernel.
  2265. */
  2266. static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
  2267. {
  2268. struct dm_table *map = NULL;
  2269. lockdep_assert_held(&md->suspend_lock);
  2270. if (md->internal_suspend_count++)
  2271. return; /* nested internal suspend */
  2272. if (dm_suspended_md(md)) {
  2273. set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2274. return; /* nest suspend */
  2275. }
  2276. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2277. /*
  2278. * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
  2279. * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
  2280. * would require changing .presuspend to return an error -- avoid this
  2281. * until there is a need for more elaborate variants of internal suspend.
  2282. */
  2283. (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
  2284. DMF_SUSPENDED_INTERNALLY);
  2285. dm_table_postsuspend_targets(map);
  2286. }
  2287. static void __dm_internal_resume(struct mapped_device *md)
  2288. {
  2289. BUG_ON(!md->internal_suspend_count);
  2290. if (--md->internal_suspend_count)
  2291. return; /* resume from nested internal suspend */
  2292. if (dm_suspended_md(md))
  2293. goto done; /* resume from nested suspend */
  2294. /*
  2295. * NOTE: existing callers don't need to call dm_table_resume_targets
  2296. * (which may fail -- so best to avoid it for now by passing NULL map)
  2297. */
  2298. (void) __dm_resume(md, NULL);
  2299. done:
  2300. clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2301. smp_mb__after_atomic();
  2302. wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
  2303. }
  2304. void dm_internal_suspend_noflush(struct mapped_device *md)
  2305. {
  2306. mutex_lock(&md->suspend_lock);
  2307. __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
  2308. mutex_unlock(&md->suspend_lock);
  2309. }
  2310. EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
  2311. void dm_internal_resume(struct mapped_device *md)
  2312. {
  2313. mutex_lock(&md->suspend_lock);
  2314. __dm_internal_resume(md);
  2315. mutex_unlock(&md->suspend_lock);
  2316. }
  2317. EXPORT_SYMBOL_GPL(dm_internal_resume);
  2318. /*
  2319. * Fast variants of internal suspend/resume hold md->suspend_lock,
  2320. * which prevents interaction with userspace-driven suspend.
  2321. */
  2322. void dm_internal_suspend_fast(struct mapped_device *md)
  2323. {
  2324. mutex_lock(&md->suspend_lock);
  2325. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  2326. return;
  2327. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2328. synchronize_srcu(&md->io_barrier);
  2329. flush_workqueue(md->wq);
  2330. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  2331. }
  2332. EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
  2333. void dm_internal_resume_fast(struct mapped_device *md)
  2334. {
  2335. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  2336. goto done;
  2337. dm_queue_flush(md);
  2338. done:
  2339. mutex_unlock(&md->suspend_lock);
  2340. }
  2341. EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
  2342. /*-----------------------------------------------------------------
  2343. * Event notification.
  2344. *---------------------------------------------------------------*/
  2345. int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
  2346. unsigned cookie)
  2347. {
  2348. char udev_cookie[DM_COOKIE_LENGTH];
  2349. char *envp[] = { udev_cookie, NULL };
  2350. if (!cookie)
  2351. return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
  2352. else {
  2353. snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
  2354. DM_COOKIE_ENV_VAR_NAME, cookie);
  2355. return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
  2356. action, envp);
  2357. }
  2358. }
  2359. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  2360. {
  2361. return atomic_add_return(1, &md->uevent_seq);
  2362. }
  2363. uint32_t dm_get_event_nr(struct mapped_device *md)
  2364. {
  2365. return atomic_read(&md->event_nr);
  2366. }
  2367. int dm_wait_event(struct mapped_device *md, int event_nr)
  2368. {
  2369. return wait_event_interruptible(md->eventq,
  2370. (event_nr != atomic_read(&md->event_nr)));
  2371. }
  2372. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  2373. {
  2374. unsigned long flags;
  2375. spin_lock_irqsave(&md->uevent_lock, flags);
  2376. list_add(elist, &md->uevent_list);
  2377. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2378. }
  2379. /*
  2380. * The gendisk is only valid as long as you have a reference
  2381. * count on 'md'.
  2382. */
  2383. struct gendisk *dm_disk(struct mapped_device *md)
  2384. {
  2385. return md->disk;
  2386. }
  2387. EXPORT_SYMBOL_GPL(dm_disk);
  2388. struct kobject *dm_kobject(struct mapped_device *md)
  2389. {
  2390. return &md->kobj_holder.kobj;
  2391. }
  2392. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  2393. {
  2394. struct mapped_device *md;
  2395. md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
  2396. spin_lock(&_minor_lock);
  2397. if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
  2398. md = NULL;
  2399. goto out;
  2400. }
  2401. dm_get(md);
  2402. out:
  2403. spin_unlock(&_minor_lock);
  2404. return md;
  2405. }
  2406. int dm_suspended_md(struct mapped_device *md)
  2407. {
  2408. return test_bit(DMF_SUSPENDED, &md->flags);
  2409. }
  2410. int dm_suspended_internally_md(struct mapped_device *md)
  2411. {
  2412. return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2413. }
  2414. int dm_test_deferred_remove_flag(struct mapped_device *md)
  2415. {
  2416. return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
  2417. }
  2418. int dm_suspended(struct dm_target *ti)
  2419. {
  2420. return dm_suspended_md(dm_table_get_md(ti->table));
  2421. }
  2422. EXPORT_SYMBOL_GPL(dm_suspended);
  2423. int dm_noflush_suspending(struct dm_target *ti)
  2424. {
  2425. return __noflush_suspending(dm_table_get_md(ti->table));
  2426. }
  2427. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  2428. struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
  2429. unsigned integrity, unsigned per_io_data_size,
  2430. unsigned min_pool_size)
  2431. {
  2432. struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
  2433. unsigned int pool_size = 0;
  2434. unsigned int front_pad, io_front_pad;
  2435. int ret;
  2436. if (!pools)
  2437. return NULL;
  2438. switch (type) {
  2439. case DM_TYPE_BIO_BASED:
  2440. case DM_TYPE_DAX_BIO_BASED:
  2441. case DM_TYPE_NVME_BIO_BASED:
  2442. pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
  2443. front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
  2444. io_front_pad = roundup(front_pad, __alignof__(struct dm_io)) + offsetof(struct dm_io, tio);
  2445. ret = bioset_init(&pools->io_bs, pool_size, io_front_pad, 0);
  2446. if (ret)
  2447. goto out;
  2448. if (integrity && bioset_integrity_create(&pools->io_bs, pool_size))
  2449. goto out;
  2450. break;
  2451. case DM_TYPE_REQUEST_BASED:
  2452. case DM_TYPE_MQ_REQUEST_BASED:
  2453. pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
  2454. front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
  2455. /* per_io_data_size is used for blk-mq pdu at queue allocation */
  2456. break;
  2457. default:
  2458. BUG();
  2459. }
  2460. ret = bioset_init(&pools->bs, pool_size, front_pad, 0);
  2461. if (ret)
  2462. goto out;
  2463. if (integrity && bioset_integrity_create(&pools->bs, pool_size))
  2464. goto out;
  2465. return pools;
  2466. out:
  2467. dm_free_md_mempools(pools);
  2468. return NULL;
  2469. }
  2470. void dm_free_md_mempools(struct dm_md_mempools *pools)
  2471. {
  2472. if (!pools)
  2473. return;
  2474. bioset_exit(&pools->bs);
  2475. bioset_exit(&pools->io_bs);
  2476. kfree(pools);
  2477. }
  2478. struct dm_pr {
  2479. u64 old_key;
  2480. u64 new_key;
  2481. u32 flags;
  2482. bool fail_early;
  2483. };
  2484. static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
  2485. void *data)
  2486. {
  2487. struct mapped_device *md = bdev->bd_disk->private_data;
  2488. struct dm_table *table;
  2489. struct dm_target *ti;
  2490. int ret = -ENOTTY, srcu_idx;
  2491. table = dm_get_live_table(md, &srcu_idx);
  2492. if (!table || !dm_table_get_size(table))
  2493. goto out;
  2494. /* We only support devices that have a single target */
  2495. if (dm_table_get_num_targets(table) != 1)
  2496. goto out;
  2497. ti = dm_table_get_target(table, 0);
  2498. ret = -EINVAL;
  2499. if (!ti->type->iterate_devices)
  2500. goto out;
  2501. ret = ti->type->iterate_devices(ti, fn, data);
  2502. out:
  2503. dm_put_live_table(md, srcu_idx);
  2504. return ret;
  2505. }
  2506. /*
  2507. * For register / unregister we need to manually call out to every path.
  2508. */
  2509. static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
  2510. sector_t start, sector_t len, void *data)
  2511. {
  2512. struct dm_pr *pr = data;
  2513. const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
  2514. if (!ops || !ops->pr_register)
  2515. return -EOPNOTSUPP;
  2516. return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
  2517. }
  2518. static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
  2519. u32 flags)
  2520. {
  2521. struct dm_pr pr = {
  2522. .old_key = old_key,
  2523. .new_key = new_key,
  2524. .flags = flags,
  2525. .fail_early = true,
  2526. };
  2527. int ret;
  2528. ret = dm_call_pr(bdev, __dm_pr_register, &pr);
  2529. if (ret && new_key) {
  2530. /* unregister all paths if we failed to register any path */
  2531. pr.old_key = new_key;
  2532. pr.new_key = 0;
  2533. pr.flags = 0;
  2534. pr.fail_early = false;
  2535. dm_call_pr(bdev, __dm_pr_register, &pr);
  2536. }
  2537. return ret;
  2538. }
  2539. static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
  2540. u32 flags)
  2541. {
  2542. struct mapped_device *md = bdev->bd_disk->private_data;
  2543. const struct pr_ops *ops;
  2544. int r, srcu_idx;
  2545. r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
  2546. if (r < 0)
  2547. goto out;
  2548. ops = bdev->bd_disk->fops->pr_ops;
  2549. if (ops && ops->pr_reserve)
  2550. r = ops->pr_reserve(bdev, key, type, flags);
  2551. else
  2552. r = -EOPNOTSUPP;
  2553. out:
  2554. dm_unprepare_ioctl(md, srcu_idx);
  2555. return r;
  2556. }
  2557. static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  2558. {
  2559. struct mapped_device *md = bdev->bd_disk->private_data;
  2560. const struct pr_ops *ops;
  2561. int r, srcu_idx;
  2562. r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
  2563. if (r < 0)
  2564. goto out;
  2565. ops = bdev->bd_disk->fops->pr_ops;
  2566. if (ops && ops->pr_release)
  2567. r = ops->pr_release(bdev, key, type);
  2568. else
  2569. r = -EOPNOTSUPP;
  2570. out:
  2571. dm_unprepare_ioctl(md, srcu_idx);
  2572. return r;
  2573. }
  2574. static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
  2575. enum pr_type type, bool abort)
  2576. {
  2577. struct mapped_device *md = bdev->bd_disk->private_data;
  2578. const struct pr_ops *ops;
  2579. int r, srcu_idx;
  2580. r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
  2581. if (r < 0)
  2582. goto out;
  2583. ops = bdev->bd_disk->fops->pr_ops;
  2584. if (ops && ops->pr_preempt)
  2585. r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
  2586. else
  2587. r = -EOPNOTSUPP;
  2588. out:
  2589. dm_unprepare_ioctl(md, srcu_idx);
  2590. return r;
  2591. }
  2592. static int dm_pr_clear(struct block_device *bdev, u64 key)
  2593. {
  2594. struct mapped_device *md = bdev->bd_disk->private_data;
  2595. const struct pr_ops *ops;
  2596. int r, srcu_idx;
  2597. r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
  2598. if (r < 0)
  2599. goto out;
  2600. ops = bdev->bd_disk->fops->pr_ops;
  2601. if (ops && ops->pr_clear)
  2602. r = ops->pr_clear(bdev, key);
  2603. else
  2604. r = -EOPNOTSUPP;
  2605. out:
  2606. dm_unprepare_ioctl(md, srcu_idx);
  2607. return r;
  2608. }
  2609. static const struct pr_ops dm_pr_ops = {
  2610. .pr_register = dm_pr_register,
  2611. .pr_reserve = dm_pr_reserve,
  2612. .pr_release = dm_pr_release,
  2613. .pr_preempt = dm_pr_preempt,
  2614. .pr_clear = dm_pr_clear,
  2615. };
  2616. static const struct block_device_operations dm_blk_dops = {
  2617. .open = dm_blk_open,
  2618. .release = dm_blk_close,
  2619. .ioctl = dm_blk_ioctl,
  2620. .getgeo = dm_blk_getgeo,
  2621. .pr_ops = &dm_pr_ops,
  2622. .owner = THIS_MODULE
  2623. };
  2624. static const struct dax_operations dm_dax_ops = {
  2625. .direct_access = dm_dax_direct_access,
  2626. .copy_from_iter = dm_dax_copy_from_iter,
  2627. .copy_to_iter = dm_dax_copy_to_iter,
  2628. };
  2629. /*
  2630. * module hooks
  2631. */
  2632. module_init(dm_init);
  2633. module_exit(dm_exit);
  2634. module_param(major, uint, 0);
  2635. MODULE_PARM_DESC(major, "The major number of the device mapper");
  2636. module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
  2637. MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
  2638. module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
  2639. MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
  2640. MODULE_DESCRIPTION(DM_NAME " driver");
  2641. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2642. MODULE_LICENSE("GPL");