dm.c 74 KB

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