dm.c 78 KB

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