dm.c 73 KB

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