dm.c 76 KB

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