dm.c 73 KB

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