dm.c 72 KB

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