dm.c 68 KB

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