dm.c 76 KB

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