dm.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772
  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.h"
  8. #include "dm-uevent.h"
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/blkpg.h>
  14. #include <linux/bio.h>
  15. #include <linux/mempool.h>
  16. #include <linux/slab.h>
  17. #include <linux/idr.h>
  18. #include <linux/hdreg.h>
  19. #include <linux/delay.h>
  20. #include <linux/wait.h>
  21. #include <linux/kthread.h>
  22. #include <linux/ktime.h>
  23. #include <linux/elevator.h> /* for rq_end_sector() */
  24. #include <linux/blk-mq.h>
  25. #include <linux/pr.h>
  26. #include <trace/events/block.h>
  27. #define DM_MSG_PREFIX "core"
  28. #ifdef CONFIG_PRINTK
  29. /*
  30. * ratelimit state to be used in DMXXX_LIMIT().
  31. */
  32. DEFINE_RATELIMIT_STATE(dm_ratelimit_state,
  33. DEFAULT_RATELIMIT_INTERVAL,
  34. DEFAULT_RATELIMIT_BURST);
  35. EXPORT_SYMBOL(dm_ratelimit_state);
  36. #endif
  37. /*
  38. * Cookies are numeric values sent with CHANGE and REMOVE
  39. * uevents while resuming, removing or renaming the device.
  40. */
  41. #define DM_COOKIE_ENV_VAR_NAME "DM_COOKIE"
  42. #define DM_COOKIE_LENGTH 24
  43. static const char *_name = DM_NAME;
  44. static unsigned int major = 0;
  45. static unsigned int _major = 0;
  46. static DEFINE_IDR(_minor_idr);
  47. static DEFINE_SPINLOCK(_minor_lock);
  48. static void do_deferred_remove(struct work_struct *w);
  49. static DECLARE_WORK(deferred_remove_work, do_deferred_remove);
  50. static struct workqueue_struct *deferred_remove_workqueue;
  51. /*
  52. * For bio-based dm.
  53. * One of these is allocated per bio.
  54. */
  55. struct dm_io {
  56. struct mapped_device *md;
  57. int error;
  58. atomic_t io_count;
  59. struct bio *bio;
  60. unsigned long start_time;
  61. spinlock_t endio_lock;
  62. struct dm_stats_aux stats_aux;
  63. };
  64. /*
  65. * For request-based dm.
  66. * One of these is allocated per request.
  67. */
  68. struct dm_rq_target_io {
  69. struct mapped_device *md;
  70. struct dm_target *ti;
  71. struct request *orig, *clone;
  72. struct kthread_work work;
  73. int error;
  74. union map_info info;
  75. struct dm_stats_aux stats_aux;
  76. unsigned long duration_jiffies;
  77. unsigned n_sectors;
  78. };
  79. /*
  80. * For request-based dm - the bio clones we allocate are embedded in these
  81. * structs.
  82. *
  83. * We allocate these with bio_alloc_bioset, using the front_pad parameter when
  84. * the bioset is created - this means the bio has to come at the end of the
  85. * struct.
  86. */
  87. struct dm_rq_clone_bio_info {
  88. struct bio *orig;
  89. struct dm_rq_target_io *tio;
  90. struct bio clone;
  91. };
  92. union map_info *dm_get_rq_mapinfo(struct request *rq)
  93. {
  94. if (rq && rq->end_io_data)
  95. return &((struct dm_rq_target_io *)rq->end_io_data)->info;
  96. return NULL;
  97. }
  98. EXPORT_SYMBOL_GPL(dm_get_rq_mapinfo);
  99. #define MINOR_ALLOCED ((void *)-1)
  100. /*
  101. * Bits for the md->flags field.
  102. */
  103. #define DMF_BLOCK_IO_FOR_SUSPEND 0
  104. #define DMF_SUSPENDED 1
  105. #define DMF_FROZEN 2
  106. #define DMF_FREEING 3
  107. #define DMF_DELETING 4
  108. #define DMF_NOFLUSH_SUSPENDING 5
  109. #define DMF_DEFERRED_REMOVE 6
  110. #define DMF_SUSPENDED_INTERNALLY 7
  111. /*
  112. * A dummy definition to make RCU happy.
  113. * struct dm_table should never be dereferenced in this file.
  114. */
  115. struct dm_table {
  116. int undefined__;
  117. };
  118. /*
  119. * Work processed by per-device workqueue.
  120. */
  121. struct mapped_device {
  122. struct srcu_struct io_barrier;
  123. struct mutex suspend_lock;
  124. atomic_t holders;
  125. atomic_t open_count;
  126. /*
  127. * The current mapping.
  128. * Use dm_get_live_table{_fast} or take suspend_lock for
  129. * dereference.
  130. */
  131. struct dm_table __rcu *map;
  132. struct list_head table_devices;
  133. struct mutex table_devices_lock;
  134. unsigned long flags;
  135. struct request_queue *queue;
  136. unsigned type;
  137. /* Protect queue and type against concurrent access. */
  138. struct mutex type_lock;
  139. struct target_type *immutable_target_type;
  140. struct gendisk *disk;
  141. char name[16];
  142. void *interface_ptr;
  143. /*
  144. * A list of ios that arrived while we were suspended.
  145. */
  146. atomic_t pending[2];
  147. wait_queue_head_t wait;
  148. struct work_struct work;
  149. struct bio_list deferred;
  150. spinlock_t deferred_lock;
  151. /*
  152. * Processing queue (flush)
  153. */
  154. struct workqueue_struct *wq;
  155. /*
  156. * io objects are allocated from here.
  157. */
  158. mempool_t *io_pool;
  159. mempool_t *rq_pool;
  160. struct bio_set *bs;
  161. /*
  162. * Event handling.
  163. */
  164. atomic_t event_nr;
  165. wait_queue_head_t eventq;
  166. atomic_t uevent_seq;
  167. struct list_head uevent_list;
  168. spinlock_t uevent_lock; /* Protect access to uevent_list */
  169. /*
  170. * freeze/thaw support require holding onto a super block
  171. */
  172. struct super_block *frozen_sb;
  173. struct block_device *bdev;
  174. /* forced geometry settings */
  175. struct hd_geometry geometry;
  176. /* kobject and completion */
  177. struct dm_kobject_holder kobj_holder;
  178. /* zero-length flush that will be cloned and submitted to targets */
  179. struct bio flush_bio;
  180. /* the number of internal suspends */
  181. unsigned internal_suspend_count;
  182. struct dm_stats stats;
  183. struct kthread_worker kworker;
  184. struct task_struct *kworker_task;
  185. /* for request-based merge heuristic in dm_request_fn() */
  186. unsigned seq_rq_merge_deadline_usecs;
  187. int last_rq_rw;
  188. sector_t last_rq_pos;
  189. ktime_t last_rq_start_time;
  190. /* for blk-mq request-based DM support */
  191. struct blk_mq_tag_set tag_set;
  192. bool use_blk_mq;
  193. };
  194. #ifdef CONFIG_DM_MQ_DEFAULT
  195. static bool use_blk_mq = true;
  196. #else
  197. static bool use_blk_mq = false;
  198. #endif
  199. bool dm_use_blk_mq(struct mapped_device *md)
  200. {
  201. return md->use_blk_mq;
  202. }
  203. /*
  204. * For mempools pre-allocation at the table loading time.
  205. */
  206. struct dm_md_mempools {
  207. mempool_t *io_pool;
  208. mempool_t *rq_pool;
  209. struct bio_set *bs;
  210. };
  211. struct table_device {
  212. struct list_head list;
  213. atomic_t count;
  214. struct dm_dev dm_dev;
  215. };
  216. #define RESERVED_BIO_BASED_IOS 16
  217. #define RESERVED_REQUEST_BASED_IOS 256
  218. #define RESERVED_MAX_IOS 1024
  219. static struct kmem_cache *_io_cache;
  220. static struct kmem_cache *_rq_tio_cache;
  221. static struct kmem_cache *_rq_cache;
  222. /*
  223. * Bio-based DM's mempools' reserved IOs set by the user.
  224. */
  225. static unsigned reserved_bio_based_ios = RESERVED_BIO_BASED_IOS;
  226. /*
  227. * Request-based DM's mempools' reserved IOs set by the user.
  228. */
  229. static unsigned reserved_rq_based_ios = RESERVED_REQUEST_BASED_IOS;
  230. static unsigned __dm_get_module_param(unsigned *module_param,
  231. unsigned def, unsigned max)
  232. {
  233. unsigned param = ACCESS_ONCE(*module_param);
  234. unsigned modified_param = 0;
  235. if (!param)
  236. modified_param = def;
  237. else if (param > max)
  238. modified_param = max;
  239. if (modified_param) {
  240. (void)cmpxchg(module_param, param, modified_param);
  241. param = modified_param;
  242. }
  243. return param;
  244. }
  245. unsigned dm_get_reserved_bio_based_ios(void)
  246. {
  247. return __dm_get_module_param(&reserved_bio_based_ios,
  248. RESERVED_BIO_BASED_IOS, RESERVED_MAX_IOS);
  249. }
  250. EXPORT_SYMBOL_GPL(dm_get_reserved_bio_based_ios);
  251. unsigned dm_get_reserved_rq_based_ios(void)
  252. {
  253. return __dm_get_module_param(&reserved_rq_based_ios,
  254. RESERVED_REQUEST_BASED_IOS, RESERVED_MAX_IOS);
  255. }
  256. EXPORT_SYMBOL_GPL(dm_get_reserved_rq_based_ios);
  257. static int __init local_init(void)
  258. {
  259. int r = -ENOMEM;
  260. /* allocate a slab for the dm_ios */
  261. _io_cache = KMEM_CACHE(dm_io, 0);
  262. if (!_io_cache)
  263. return r;
  264. _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
  265. if (!_rq_tio_cache)
  266. goto out_free_io_cache;
  267. _rq_cache = kmem_cache_create("dm_clone_request", sizeof(struct request),
  268. __alignof__(struct request), 0, NULL);
  269. if (!_rq_cache)
  270. goto out_free_rq_tio_cache;
  271. r = dm_uevent_init();
  272. if (r)
  273. goto out_free_rq_cache;
  274. deferred_remove_workqueue = alloc_workqueue("kdmremove", WQ_UNBOUND, 1);
  275. if (!deferred_remove_workqueue) {
  276. r = -ENOMEM;
  277. goto out_uevent_exit;
  278. }
  279. _major = major;
  280. r = register_blkdev(_major, _name);
  281. if (r < 0)
  282. goto out_free_workqueue;
  283. if (!_major)
  284. _major = r;
  285. return 0;
  286. out_free_workqueue:
  287. destroy_workqueue(deferred_remove_workqueue);
  288. out_uevent_exit:
  289. dm_uevent_exit();
  290. out_free_rq_cache:
  291. kmem_cache_destroy(_rq_cache);
  292. out_free_rq_tio_cache:
  293. kmem_cache_destroy(_rq_tio_cache);
  294. out_free_io_cache:
  295. kmem_cache_destroy(_io_cache);
  296. return r;
  297. }
  298. static void local_exit(void)
  299. {
  300. flush_scheduled_work();
  301. destroy_workqueue(deferred_remove_workqueue);
  302. kmem_cache_destroy(_rq_cache);
  303. kmem_cache_destroy(_rq_tio_cache);
  304. kmem_cache_destroy(_io_cache);
  305. unregister_blkdev(_major, _name);
  306. dm_uevent_exit();
  307. _major = 0;
  308. DMINFO("cleaned up");
  309. }
  310. static int (*_inits[])(void) __initdata = {
  311. local_init,
  312. dm_target_init,
  313. dm_linear_init,
  314. dm_stripe_init,
  315. dm_io_init,
  316. dm_kcopyd_init,
  317. dm_interface_init,
  318. dm_statistics_init,
  319. };
  320. static void (*_exits[])(void) = {
  321. local_exit,
  322. dm_target_exit,
  323. dm_linear_exit,
  324. dm_stripe_exit,
  325. dm_io_exit,
  326. dm_kcopyd_exit,
  327. dm_interface_exit,
  328. dm_statistics_exit,
  329. };
  330. static int __init dm_init(void)
  331. {
  332. const int count = ARRAY_SIZE(_inits);
  333. int r, i;
  334. for (i = 0; i < count; i++) {
  335. r = _inits[i]();
  336. if (r)
  337. goto bad;
  338. }
  339. return 0;
  340. bad:
  341. while (i--)
  342. _exits[i]();
  343. return r;
  344. }
  345. static void __exit dm_exit(void)
  346. {
  347. int i = ARRAY_SIZE(_exits);
  348. while (i--)
  349. _exits[i]();
  350. /*
  351. * Should be empty by this point.
  352. */
  353. idr_destroy(&_minor_idr);
  354. }
  355. /*
  356. * Block device functions
  357. */
  358. int dm_deleting_md(struct mapped_device *md)
  359. {
  360. return test_bit(DMF_DELETING, &md->flags);
  361. }
  362. static int dm_blk_open(struct block_device *bdev, fmode_t mode)
  363. {
  364. struct mapped_device *md;
  365. spin_lock(&_minor_lock);
  366. md = bdev->bd_disk->private_data;
  367. if (!md)
  368. goto out;
  369. if (test_bit(DMF_FREEING, &md->flags) ||
  370. dm_deleting_md(md)) {
  371. md = NULL;
  372. goto out;
  373. }
  374. dm_get(md);
  375. atomic_inc(&md->open_count);
  376. out:
  377. spin_unlock(&_minor_lock);
  378. return md ? 0 : -ENXIO;
  379. }
  380. static void dm_blk_close(struct gendisk *disk, fmode_t mode)
  381. {
  382. struct mapped_device *md;
  383. spin_lock(&_minor_lock);
  384. md = disk->private_data;
  385. if (WARN_ON(!md))
  386. goto out;
  387. if (atomic_dec_and_test(&md->open_count) &&
  388. (test_bit(DMF_DEFERRED_REMOVE, &md->flags)))
  389. queue_work(deferred_remove_workqueue, &deferred_remove_work);
  390. dm_put(md);
  391. out:
  392. spin_unlock(&_minor_lock);
  393. }
  394. int dm_open_count(struct mapped_device *md)
  395. {
  396. return atomic_read(&md->open_count);
  397. }
  398. /*
  399. * Guarantees nothing is using the device before it's deleted.
  400. */
  401. int dm_lock_for_deletion(struct mapped_device *md, bool mark_deferred, bool only_deferred)
  402. {
  403. int r = 0;
  404. spin_lock(&_minor_lock);
  405. if (dm_open_count(md)) {
  406. r = -EBUSY;
  407. if (mark_deferred)
  408. set_bit(DMF_DEFERRED_REMOVE, &md->flags);
  409. } else if (only_deferred && !test_bit(DMF_DEFERRED_REMOVE, &md->flags))
  410. r = -EEXIST;
  411. else
  412. set_bit(DMF_DELETING, &md->flags);
  413. spin_unlock(&_minor_lock);
  414. return r;
  415. }
  416. int dm_cancel_deferred_remove(struct mapped_device *md)
  417. {
  418. int r = 0;
  419. spin_lock(&_minor_lock);
  420. if (test_bit(DMF_DELETING, &md->flags))
  421. r = -EBUSY;
  422. else
  423. clear_bit(DMF_DEFERRED_REMOVE, &md->flags);
  424. spin_unlock(&_minor_lock);
  425. return r;
  426. }
  427. static void do_deferred_remove(struct work_struct *w)
  428. {
  429. dm_deferred_remove();
  430. }
  431. sector_t dm_get_size(struct mapped_device *md)
  432. {
  433. return get_capacity(md->disk);
  434. }
  435. struct request_queue *dm_get_md_queue(struct mapped_device *md)
  436. {
  437. return md->queue;
  438. }
  439. struct dm_stats *dm_get_stats(struct mapped_device *md)
  440. {
  441. return &md->stats;
  442. }
  443. static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  444. {
  445. struct mapped_device *md = bdev->bd_disk->private_data;
  446. return dm_get_geometry(md, geo);
  447. }
  448. static int dm_get_live_table_for_ioctl(struct mapped_device *md,
  449. struct dm_target **tgt, struct block_device **bdev,
  450. fmode_t *mode, int *srcu_idx)
  451. {
  452. struct dm_table *map;
  453. int r;
  454. retry:
  455. r = -ENOTTY;
  456. map = dm_get_live_table(md, srcu_idx);
  457. if (!map || !dm_table_get_size(map))
  458. goto out;
  459. /* We only support devices that have a single target */
  460. if (dm_table_get_num_targets(map) != 1)
  461. goto out;
  462. *tgt = dm_table_get_target(map, 0);
  463. if (!(*tgt)->type->prepare_ioctl)
  464. goto out;
  465. if (dm_suspended_md(md)) {
  466. r = -EAGAIN;
  467. goto out;
  468. }
  469. r = (*tgt)->type->prepare_ioctl(*tgt, bdev, mode);
  470. if (r < 0)
  471. goto out;
  472. return r;
  473. out:
  474. dm_put_live_table(md, *srcu_idx);
  475. if (r == -ENOTCONN && !fatal_signal_pending(current)) {
  476. msleep(10);
  477. goto retry;
  478. }
  479. return r;
  480. }
  481. static int dm_blk_ioctl(struct block_device *bdev, fmode_t mode,
  482. unsigned int cmd, unsigned long arg)
  483. {
  484. struct mapped_device *md = bdev->bd_disk->private_data;
  485. struct dm_target *tgt;
  486. struct block_device *tgt_bdev = NULL;
  487. int srcu_idx, r;
  488. r = dm_get_live_table_for_ioctl(md, &tgt, &tgt_bdev, &mode, &srcu_idx);
  489. if (r < 0)
  490. return r;
  491. if (r > 0) {
  492. /*
  493. * Target determined this ioctl is being issued against
  494. * a logical partition of the parent bdev; so extra
  495. * validation is needed.
  496. */
  497. r = scsi_verify_blk_ioctl(NULL, cmd);
  498. if (r)
  499. goto out;
  500. }
  501. r = __blkdev_driver_ioctl(tgt_bdev, mode, cmd, arg);
  502. out:
  503. dm_put_live_table(md, srcu_idx);
  504. return r;
  505. }
  506. static struct dm_io *alloc_io(struct mapped_device *md)
  507. {
  508. return mempool_alloc(md->io_pool, GFP_NOIO);
  509. }
  510. static void free_io(struct mapped_device *md, struct dm_io *io)
  511. {
  512. mempool_free(io, md->io_pool);
  513. }
  514. static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
  515. {
  516. bio_put(&tio->clone);
  517. }
  518. static struct dm_rq_target_io *alloc_rq_tio(struct mapped_device *md,
  519. gfp_t gfp_mask)
  520. {
  521. return mempool_alloc(md->io_pool, gfp_mask);
  522. }
  523. static void free_rq_tio(struct dm_rq_target_io *tio)
  524. {
  525. mempool_free(tio, tio->md->io_pool);
  526. }
  527. static struct request *alloc_clone_request(struct mapped_device *md,
  528. gfp_t gfp_mask)
  529. {
  530. return mempool_alloc(md->rq_pool, gfp_mask);
  531. }
  532. static void free_clone_request(struct mapped_device *md, struct request *rq)
  533. {
  534. mempool_free(rq, md->rq_pool);
  535. }
  536. static int md_in_flight(struct mapped_device *md)
  537. {
  538. return atomic_read(&md->pending[READ]) +
  539. atomic_read(&md->pending[WRITE]);
  540. }
  541. static void start_io_acct(struct dm_io *io)
  542. {
  543. struct mapped_device *md = io->md;
  544. struct bio *bio = io->bio;
  545. int cpu;
  546. int rw = bio_data_dir(bio);
  547. io->start_time = jiffies;
  548. cpu = part_stat_lock();
  549. part_round_stats(cpu, &dm_disk(md)->part0);
  550. part_stat_unlock();
  551. atomic_set(&dm_disk(md)->part0.in_flight[rw],
  552. atomic_inc_return(&md->pending[rw]));
  553. if (unlikely(dm_stats_used(&md->stats)))
  554. dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
  555. bio_sectors(bio), false, 0, &io->stats_aux);
  556. }
  557. static void end_io_acct(struct dm_io *io)
  558. {
  559. struct mapped_device *md = io->md;
  560. struct bio *bio = io->bio;
  561. unsigned long duration = jiffies - io->start_time;
  562. int pending;
  563. int rw = bio_data_dir(bio);
  564. generic_end_io_acct(rw, &dm_disk(md)->part0, io->start_time);
  565. if (unlikely(dm_stats_used(&md->stats)))
  566. dm_stats_account_io(&md->stats, bio->bi_rw, bio->bi_iter.bi_sector,
  567. bio_sectors(bio), true, duration, &io->stats_aux);
  568. /*
  569. * After this is decremented the bio must not be touched if it is
  570. * a flush.
  571. */
  572. pending = atomic_dec_return(&md->pending[rw]);
  573. atomic_set(&dm_disk(md)->part0.in_flight[rw], pending);
  574. pending += atomic_read(&md->pending[rw^0x1]);
  575. /* nudge anyone waiting on suspend queue */
  576. if (!pending)
  577. wake_up(&md->wait);
  578. }
  579. /*
  580. * Add the bio to the list of deferred io.
  581. */
  582. static void queue_io(struct mapped_device *md, struct bio *bio)
  583. {
  584. unsigned long flags;
  585. spin_lock_irqsave(&md->deferred_lock, flags);
  586. bio_list_add(&md->deferred, bio);
  587. spin_unlock_irqrestore(&md->deferred_lock, flags);
  588. queue_work(md->wq, &md->work);
  589. }
  590. /*
  591. * Everyone (including functions in this file), should use this
  592. * function to access the md->map field, and make sure they call
  593. * dm_put_live_table() when finished.
  594. */
  595. struct dm_table *dm_get_live_table(struct mapped_device *md, int *srcu_idx) __acquires(md->io_barrier)
  596. {
  597. *srcu_idx = srcu_read_lock(&md->io_barrier);
  598. return srcu_dereference(md->map, &md->io_barrier);
  599. }
  600. void dm_put_live_table(struct mapped_device *md, int srcu_idx) __releases(md->io_barrier)
  601. {
  602. srcu_read_unlock(&md->io_barrier, srcu_idx);
  603. }
  604. void dm_sync_table(struct mapped_device *md)
  605. {
  606. synchronize_srcu(&md->io_barrier);
  607. synchronize_rcu_expedited();
  608. }
  609. /*
  610. * A fast alternative to dm_get_live_table/dm_put_live_table.
  611. * The caller must not block between these two functions.
  612. */
  613. static struct dm_table *dm_get_live_table_fast(struct mapped_device *md) __acquires(RCU)
  614. {
  615. rcu_read_lock();
  616. return rcu_dereference(md->map);
  617. }
  618. static void dm_put_live_table_fast(struct mapped_device *md) __releases(RCU)
  619. {
  620. rcu_read_unlock();
  621. }
  622. /*
  623. * Open a table device so we can use it as a map destination.
  624. */
  625. static int open_table_device(struct table_device *td, dev_t dev,
  626. struct mapped_device *md)
  627. {
  628. static char *_claim_ptr = "I belong to device-mapper";
  629. struct block_device *bdev;
  630. int r;
  631. BUG_ON(td->dm_dev.bdev);
  632. bdev = blkdev_get_by_dev(dev, td->dm_dev.mode | FMODE_EXCL, _claim_ptr);
  633. if (IS_ERR(bdev))
  634. return PTR_ERR(bdev);
  635. r = bd_link_disk_holder(bdev, dm_disk(md));
  636. if (r) {
  637. blkdev_put(bdev, td->dm_dev.mode | FMODE_EXCL);
  638. return r;
  639. }
  640. td->dm_dev.bdev = bdev;
  641. return 0;
  642. }
  643. /*
  644. * Close a table device that we've been using.
  645. */
  646. static void close_table_device(struct table_device *td, struct mapped_device *md)
  647. {
  648. if (!td->dm_dev.bdev)
  649. return;
  650. bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md));
  651. blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
  652. td->dm_dev.bdev = NULL;
  653. }
  654. static struct table_device *find_table_device(struct list_head *l, dev_t dev,
  655. fmode_t mode) {
  656. struct table_device *td;
  657. list_for_each_entry(td, l, list)
  658. if (td->dm_dev.bdev->bd_dev == dev && td->dm_dev.mode == mode)
  659. return td;
  660. return NULL;
  661. }
  662. int dm_get_table_device(struct mapped_device *md, dev_t dev, fmode_t mode,
  663. struct dm_dev **result) {
  664. int r;
  665. struct table_device *td;
  666. mutex_lock(&md->table_devices_lock);
  667. td = find_table_device(&md->table_devices, dev, mode);
  668. if (!td) {
  669. td = kmalloc(sizeof(*td), GFP_KERNEL);
  670. if (!td) {
  671. mutex_unlock(&md->table_devices_lock);
  672. return -ENOMEM;
  673. }
  674. td->dm_dev.mode = mode;
  675. td->dm_dev.bdev = NULL;
  676. if ((r = open_table_device(td, dev, md))) {
  677. mutex_unlock(&md->table_devices_lock);
  678. kfree(td);
  679. return r;
  680. }
  681. format_dev_t(td->dm_dev.name, dev);
  682. atomic_set(&td->count, 0);
  683. list_add(&td->list, &md->table_devices);
  684. }
  685. atomic_inc(&td->count);
  686. mutex_unlock(&md->table_devices_lock);
  687. *result = &td->dm_dev;
  688. return 0;
  689. }
  690. EXPORT_SYMBOL_GPL(dm_get_table_device);
  691. void dm_put_table_device(struct mapped_device *md, struct dm_dev *d)
  692. {
  693. struct table_device *td = container_of(d, struct table_device, dm_dev);
  694. mutex_lock(&md->table_devices_lock);
  695. if (atomic_dec_and_test(&td->count)) {
  696. close_table_device(td, md);
  697. list_del(&td->list);
  698. kfree(td);
  699. }
  700. mutex_unlock(&md->table_devices_lock);
  701. }
  702. EXPORT_SYMBOL(dm_put_table_device);
  703. static void free_table_devices(struct list_head *devices)
  704. {
  705. struct list_head *tmp, *next;
  706. list_for_each_safe(tmp, next, devices) {
  707. struct table_device *td = list_entry(tmp, struct table_device, list);
  708. DMWARN("dm_destroy: %s still exists with %d references",
  709. td->dm_dev.name, atomic_read(&td->count));
  710. kfree(td);
  711. }
  712. }
  713. /*
  714. * Get the geometry associated with a dm device
  715. */
  716. int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
  717. {
  718. *geo = md->geometry;
  719. return 0;
  720. }
  721. /*
  722. * Set the geometry of a device.
  723. */
  724. int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
  725. {
  726. sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
  727. if (geo->start > sz) {
  728. DMWARN("Start sector is beyond the geometry limits.");
  729. return -EINVAL;
  730. }
  731. md->geometry = *geo;
  732. return 0;
  733. }
  734. /*-----------------------------------------------------------------
  735. * CRUD START:
  736. * A more elegant soln is in the works that uses the queue
  737. * merge fn, unfortunately there are a couple of changes to
  738. * the block layer that I want to make for this. So in the
  739. * interests of getting something for people to use I give
  740. * you this clearly demarcated crap.
  741. *---------------------------------------------------------------*/
  742. static int __noflush_suspending(struct mapped_device *md)
  743. {
  744. return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  745. }
  746. /*
  747. * Decrements the number of outstanding ios that a bio has been
  748. * cloned into, completing the original io if necc.
  749. */
  750. static void dec_pending(struct dm_io *io, int error)
  751. {
  752. unsigned long flags;
  753. int io_error;
  754. struct bio *bio;
  755. struct mapped_device *md = io->md;
  756. /* Push-back supersedes any I/O errors */
  757. if (unlikely(error)) {
  758. spin_lock_irqsave(&io->endio_lock, flags);
  759. if (!(io->error > 0 && __noflush_suspending(md)))
  760. io->error = error;
  761. spin_unlock_irqrestore(&io->endio_lock, flags);
  762. }
  763. if (atomic_dec_and_test(&io->io_count)) {
  764. if (io->error == DM_ENDIO_REQUEUE) {
  765. /*
  766. * Target requested pushing back the I/O.
  767. */
  768. spin_lock_irqsave(&md->deferred_lock, flags);
  769. if (__noflush_suspending(md))
  770. bio_list_add_head(&md->deferred, io->bio);
  771. else
  772. /* noflush suspend was interrupted. */
  773. io->error = -EIO;
  774. spin_unlock_irqrestore(&md->deferred_lock, flags);
  775. }
  776. io_error = io->error;
  777. bio = io->bio;
  778. end_io_acct(io);
  779. free_io(md, io);
  780. if (io_error == DM_ENDIO_REQUEUE)
  781. return;
  782. if ((bio->bi_rw & REQ_FLUSH) && bio->bi_iter.bi_size) {
  783. /*
  784. * Preflush done for flush with data, reissue
  785. * without REQ_FLUSH.
  786. */
  787. bio->bi_rw &= ~REQ_FLUSH;
  788. queue_io(md, bio);
  789. } else {
  790. /* done with normal IO or empty flush */
  791. trace_block_bio_complete(md->queue, bio, io_error);
  792. if (io_error)
  793. bio->bi_error = io_error;
  794. bio_endio(bio);
  795. }
  796. }
  797. }
  798. static void disable_write_same(struct mapped_device *md)
  799. {
  800. struct queue_limits *limits = dm_get_queue_limits(md);
  801. /* device doesn't really support WRITE SAME, disable it */
  802. limits->max_write_same_sectors = 0;
  803. }
  804. static void clone_endio(struct bio *bio)
  805. {
  806. int error = bio->bi_error;
  807. int r = error;
  808. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  809. struct dm_io *io = tio->io;
  810. struct mapped_device *md = tio->io->md;
  811. dm_endio_fn endio = tio->ti->type->end_io;
  812. if (endio) {
  813. r = endio(tio->ti, bio, error);
  814. if (r < 0 || r == DM_ENDIO_REQUEUE)
  815. /*
  816. * error and requeue request are handled
  817. * in dec_pending().
  818. */
  819. error = r;
  820. else if (r == DM_ENDIO_INCOMPLETE)
  821. /* The target will handle the io */
  822. return;
  823. else if (r) {
  824. DMWARN("unimplemented target endio return value: %d", r);
  825. BUG();
  826. }
  827. }
  828. if (unlikely(r == -EREMOTEIO && (bio->bi_rw & REQ_WRITE_SAME) &&
  829. !bdev_get_queue(bio->bi_bdev)->limits.max_write_same_sectors))
  830. disable_write_same(md);
  831. free_tio(md, tio);
  832. dec_pending(io, error);
  833. }
  834. /*
  835. * Partial completion handling for request-based dm
  836. */
  837. static void end_clone_bio(struct bio *clone)
  838. {
  839. struct dm_rq_clone_bio_info *info =
  840. container_of(clone, struct dm_rq_clone_bio_info, clone);
  841. struct dm_rq_target_io *tio = info->tio;
  842. struct bio *bio = info->orig;
  843. unsigned int nr_bytes = info->orig->bi_iter.bi_size;
  844. int error = clone->bi_error;
  845. bio_put(clone);
  846. if (tio->error)
  847. /*
  848. * An error has already been detected on the request.
  849. * Once error occurred, just let clone->end_io() handle
  850. * the remainder.
  851. */
  852. return;
  853. else if (error) {
  854. /*
  855. * Don't notice the error to the upper layer yet.
  856. * The error handling decision is made by the target driver,
  857. * when the request is completed.
  858. */
  859. tio->error = error;
  860. return;
  861. }
  862. /*
  863. * I/O for the bio successfully completed.
  864. * Notice the data completion to the upper layer.
  865. */
  866. /*
  867. * bios are processed from the head of the list.
  868. * So the completing bio should always be rq->bio.
  869. * If it's not, something wrong is happening.
  870. */
  871. if (tio->orig->bio != bio)
  872. DMERR("bio completion is going in the middle of the request");
  873. /*
  874. * Update the original request.
  875. * Do not use blk_end_request() here, because it may complete
  876. * the original request before the clone, and break the ordering.
  877. */
  878. blk_update_request(tio->orig, 0, nr_bytes);
  879. }
  880. static struct dm_rq_target_io *tio_from_request(struct request *rq)
  881. {
  882. return (rq->q->mq_ops ? blk_mq_rq_to_pdu(rq) : rq->special);
  883. }
  884. static void rq_end_stats(struct mapped_device *md, struct request *orig)
  885. {
  886. if (unlikely(dm_stats_used(&md->stats))) {
  887. struct dm_rq_target_io *tio = tio_from_request(orig);
  888. tio->duration_jiffies = jiffies - tio->duration_jiffies;
  889. dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
  890. tio->n_sectors, true, tio->duration_jiffies,
  891. &tio->stats_aux);
  892. }
  893. }
  894. /*
  895. * Don't touch any member of the md after calling this function because
  896. * the md may be freed in dm_put() at the end of this function.
  897. * Or do dm_get() before calling this function and dm_put() later.
  898. */
  899. static void rq_completed(struct mapped_device *md, int rw, bool run_queue)
  900. {
  901. atomic_dec(&md->pending[rw]);
  902. /* nudge anyone waiting on suspend queue */
  903. if (!md_in_flight(md))
  904. wake_up(&md->wait);
  905. /*
  906. * Run this off this callpath, as drivers could invoke end_io while
  907. * inside their request_fn (and holding the queue lock). Calling
  908. * back into ->request_fn() could deadlock attempting to grab the
  909. * queue lock again.
  910. */
  911. if (!md->queue->mq_ops && run_queue)
  912. blk_run_queue_async(md->queue);
  913. /*
  914. * dm_put() must be at the end of this function. See the comment above
  915. */
  916. dm_put(md);
  917. }
  918. static void free_rq_clone(struct request *clone)
  919. {
  920. struct dm_rq_target_io *tio = clone->end_io_data;
  921. struct mapped_device *md = tio->md;
  922. blk_rq_unprep_clone(clone);
  923. if (md->type == DM_TYPE_MQ_REQUEST_BASED)
  924. /* stacked on blk-mq queue(s) */
  925. tio->ti->type->release_clone_rq(clone);
  926. else if (!md->queue->mq_ops)
  927. /* request_fn queue stacked on request_fn queue(s) */
  928. free_clone_request(md, clone);
  929. /*
  930. * NOTE: for the blk-mq queue stacked on request_fn queue(s) case:
  931. * no need to call free_clone_request() because we leverage blk-mq by
  932. * allocating the clone at the end of the blk-mq pdu (see: clone_rq)
  933. */
  934. if (!md->queue->mq_ops)
  935. free_rq_tio(tio);
  936. }
  937. /*
  938. * Complete the clone and the original request.
  939. * Must be called without clone's queue lock held,
  940. * see end_clone_request() for more details.
  941. */
  942. static void dm_end_request(struct request *clone, int error)
  943. {
  944. int rw = rq_data_dir(clone);
  945. struct dm_rq_target_io *tio = clone->end_io_data;
  946. struct mapped_device *md = tio->md;
  947. struct request *rq = tio->orig;
  948. if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
  949. rq->errors = clone->errors;
  950. rq->resid_len = clone->resid_len;
  951. if (rq->sense)
  952. /*
  953. * We are using the sense buffer of the original
  954. * request.
  955. * So setting the length of the sense data is enough.
  956. */
  957. rq->sense_len = clone->sense_len;
  958. }
  959. free_rq_clone(clone);
  960. rq_end_stats(md, rq);
  961. if (!rq->q->mq_ops)
  962. blk_end_request_all(rq, error);
  963. else
  964. blk_mq_end_request(rq, error);
  965. rq_completed(md, rw, true);
  966. }
  967. static void dm_unprep_request(struct request *rq)
  968. {
  969. struct dm_rq_target_io *tio = tio_from_request(rq);
  970. struct request *clone = tio->clone;
  971. if (!rq->q->mq_ops) {
  972. rq->special = NULL;
  973. rq->cmd_flags &= ~REQ_DONTPREP;
  974. }
  975. if (clone)
  976. free_rq_clone(clone);
  977. else if (!tio->md->queue->mq_ops)
  978. free_rq_tio(tio);
  979. }
  980. /*
  981. * Requeue the original request of a clone.
  982. */
  983. static void old_requeue_request(struct request *rq)
  984. {
  985. struct request_queue *q = rq->q;
  986. unsigned long flags;
  987. spin_lock_irqsave(q->queue_lock, flags);
  988. blk_requeue_request(q, rq);
  989. blk_run_queue_async(q);
  990. spin_unlock_irqrestore(q->queue_lock, flags);
  991. }
  992. static void dm_requeue_original_request(struct mapped_device *md,
  993. struct request *rq)
  994. {
  995. int rw = rq_data_dir(rq);
  996. rq_end_stats(md, rq);
  997. dm_unprep_request(rq);
  998. if (!rq->q->mq_ops)
  999. old_requeue_request(rq);
  1000. else {
  1001. blk_mq_requeue_request(rq);
  1002. blk_mq_kick_requeue_list(rq->q);
  1003. }
  1004. rq_completed(md, rw, false);
  1005. }
  1006. static void old_stop_queue(struct request_queue *q)
  1007. {
  1008. unsigned long flags;
  1009. if (blk_queue_stopped(q))
  1010. return;
  1011. spin_lock_irqsave(q->queue_lock, flags);
  1012. blk_stop_queue(q);
  1013. spin_unlock_irqrestore(q->queue_lock, flags);
  1014. }
  1015. static void stop_queue(struct request_queue *q)
  1016. {
  1017. if (!q->mq_ops)
  1018. old_stop_queue(q);
  1019. else
  1020. blk_mq_stop_hw_queues(q);
  1021. }
  1022. static void old_start_queue(struct request_queue *q)
  1023. {
  1024. unsigned long flags;
  1025. spin_lock_irqsave(q->queue_lock, flags);
  1026. if (blk_queue_stopped(q))
  1027. blk_start_queue(q);
  1028. spin_unlock_irqrestore(q->queue_lock, flags);
  1029. }
  1030. static void start_queue(struct request_queue *q)
  1031. {
  1032. if (!q->mq_ops)
  1033. old_start_queue(q);
  1034. else
  1035. blk_mq_start_stopped_hw_queues(q, true);
  1036. }
  1037. static void dm_done(struct request *clone, int error, bool mapped)
  1038. {
  1039. int r = error;
  1040. struct dm_rq_target_io *tio = clone->end_io_data;
  1041. dm_request_endio_fn rq_end_io = NULL;
  1042. if (tio->ti) {
  1043. rq_end_io = tio->ti->type->rq_end_io;
  1044. if (mapped && rq_end_io)
  1045. r = rq_end_io(tio->ti, clone, error, &tio->info);
  1046. }
  1047. if (unlikely(r == -EREMOTEIO && (clone->cmd_flags & REQ_WRITE_SAME) &&
  1048. !clone->q->limits.max_write_same_sectors))
  1049. disable_write_same(tio->md);
  1050. if (r <= 0)
  1051. /* The target wants to complete the I/O */
  1052. dm_end_request(clone, r);
  1053. else if (r == DM_ENDIO_INCOMPLETE)
  1054. /* The target will handle the I/O */
  1055. return;
  1056. else if (r == DM_ENDIO_REQUEUE)
  1057. /* The target wants to requeue the I/O */
  1058. dm_requeue_original_request(tio->md, tio->orig);
  1059. else {
  1060. DMWARN("unimplemented target endio return value: %d", r);
  1061. BUG();
  1062. }
  1063. }
  1064. /*
  1065. * Request completion handler for request-based dm
  1066. */
  1067. static void dm_softirq_done(struct request *rq)
  1068. {
  1069. bool mapped = true;
  1070. struct dm_rq_target_io *tio = tio_from_request(rq);
  1071. struct request *clone = tio->clone;
  1072. int rw;
  1073. if (!clone) {
  1074. rq_end_stats(tio->md, rq);
  1075. rw = rq_data_dir(rq);
  1076. if (!rq->q->mq_ops) {
  1077. blk_end_request_all(rq, tio->error);
  1078. rq_completed(tio->md, rw, false);
  1079. free_rq_tio(tio);
  1080. } else {
  1081. blk_mq_end_request(rq, tio->error);
  1082. rq_completed(tio->md, rw, false);
  1083. }
  1084. return;
  1085. }
  1086. if (rq->cmd_flags & REQ_FAILED)
  1087. mapped = false;
  1088. dm_done(clone, tio->error, mapped);
  1089. }
  1090. /*
  1091. * Complete the clone and the original request with the error status
  1092. * through softirq context.
  1093. */
  1094. static void dm_complete_request(struct request *rq, int error)
  1095. {
  1096. struct dm_rq_target_io *tio = tio_from_request(rq);
  1097. tio->error = error;
  1098. if (!rq->q->mq_ops)
  1099. blk_complete_request(rq);
  1100. else
  1101. blk_mq_complete_request(rq, error);
  1102. }
  1103. /*
  1104. * Complete the not-mapped clone and the original request with the error status
  1105. * through softirq context.
  1106. * Target's rq_end_io() function isn't called.
  1107. * This may be used when the target's map_rq() or clone_and_map_rq() functions fail.
  1108. */
  1109. static void dm_kill_unmapped_request(struct request *rq, int error)
  1110. {
  1111. rq->cmd_flags |= REQ_FAILED;
  1112. dm_complete_request(rq, error);
  1113. }
  1114. /*
  1115. * Called with the clone's queue lock held (for non-blk-mq)
  1116. */
  1117. static void end_clone_request(struct request *clone, int error)
  1118. {
  1119. struct dm_rq_target_io *tio = clone->end_io_data;
  1120. if (!clone->q->mq_ops) {
  1121. /*
  1122. * For just cleaning up the information of the queue in which
  1123. * the clone was dispatched.
  1124. * The clone is *NOT* freed actually here because it is alloced
  1125. * from dm own mempool (REQ_ALLOCED isn't set).
  1126. */
  1127. __blk_put_request(clone->q, clone);
  1128. }
  1129. /*
  1130. * Actual request completion is done in a softirq context which doesn't
  1131. * hold the clone's queue lock. Otherwise, deadlock could occur because:
  1132. * - another request may be submitted by the upper level driver
  1133. * of the stacking during the completion
  1134. * - the submission which requires queue lock may be done
  1135. * against this clone's queue
  1136. */
  1137. dm_complete_request(tio->orig, error);
  1138. }
  1139. /*
  1140. * Return maximum size of I/O possible at the supplied sector up to the current
  1141. * target boundary.
  1142. */
  1143. static sector_t max_io_len_target_boundary(sector_t sector, struct dm_target *ti)
  1144. {
  1145. sector_t target_offset = dm_target_offset(ti, sector);
  1146. return ti->len - target_offset;
  1147. }
  1148. static sector_t max_io_len(sector_t sector, struct dm_target *ti)
  1149. {
  1150. sector_t len = max_io_len_target_boundary(sector, ti);
  1151. sector_t offset, max_len;
  1152. /*
  1153. * Does the target need to split even further?
  1154. */
  1155. if (ti->max_io_len) {
  1156. offset = dm_target_offset(ti, sector);
  1157. if (unlikely(ti->max_io_len & (ti->max_io_len - 1)))
  1158. max_len = sector_div(offset, ti->max_io_len);
  1159. else
  1160. max_len = offset & (ti->max_io_len - 1);
  1161. max_len = ti->max_io_len - max_len;
  1162. if (len > max_len)
  1163. len = max_len;
  1164. }
  1165. return len;
  1166. }
  1167. int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
  1168. {
  1169. if (len > UINT_MAX) {
  1170. DMERR("Specified maximum size of target IO (%llu) exceeds limit (%u)",
  1171. (unsigned long long)len, UINT_MAX);
  1172. ti->error = "Maximum size of target IO is too large";
  1173. return -EINVAL;
  1174. }
  1175. ti->max_io_len = (uint32_t) len;
  1176. return 0;
  1177. }
  1178. EXPORT_SYMBOL_GPL(dm_set_target_max_io_len);
  1179. /*
  1180. * A target may call dm_accept_partial_bio only from the map routine. It is
  1181. * allowed for all bio types except REQ_FLUSH.
  1182. *
  1183. * dm_accept_partial_bio informs the dm that the target only wants to process
  1184. * additional n_sectors sectors of the bio and the rest of the data should be
  1185. * sent in a next bio.
  1186. *
  1187. * A diagram that explains the arithmetics:
  1188. * +--------------------+---------------+-------+
  1189. * | 1 | 2 | 3 |
  1190. * +--------------------+---------------+-------+
  1191. *
  1192. * <-------------- *tio->len_ptr --------------->
  1193. * <------- bi_size ------->
  1194. * <-- n_sectors -->
  1195. *
  1196. * Region 1 was already iterated over with bio_advance or similar function.
  1197. * (it may be empty if the target doesn't use bio_advance)
  1198. * Region 2 is the remaining bio size that the target wants to process.
  1199. * (it may be empty if region 1 is non-empty, although there is no reason
  1200. * to make it empty)
  1201. * The target requires that region 3 is to be sent in the next bio.
  1202. *
  1203. * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
  1204. * the partially processed part (the sum of regions 1+2) must be the same for all
  1205. * copies of the bio.
  1206. */
  1207. void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
  1208. {
  1209. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  1210. unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
  1211. BUG_ON(bio->bi_rw & REQ_FLUSH);
  1212. BUG_ON(bi_size > *tio->len_ptr);
  1213. BUG_ON(n_sectors > bi_size);
  1214. *tio->len_ptr -= bi_size - n_sectors;
  1215. bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
  1216. }
  1217. EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
  1218. /*
  1219. * Flush current->bio_list when the target map method blocks.
  1220. * This fixes deadlocks in snapshot and possibly in other targets.
  1221. */
  1222. struct dm_offload {
  1223. struct blk_plug plug;
  1224. struct blk_plug_cb cb;
  1225. };
  1226. static void flush_current_bio_list(struct blk_plug_cb *cb, bool from_schedule)
  1227. {
  1228. struct dm_offload *o = container_of(cb, struct dm_offload, cb);
  1229. struct bio_list list;
  1230. struct bio *bio;
  1231. int i;
  1232. INIT_LIST_HEAD(&o->cb.list);
  1233. if (unlikely(!current->bio_list))
  1234. return;
  1235. for (i = 0; i < 2; i++) {
  1236. list = current->bio_list[i];
  1237. bio_list_init(&current->bio_list[i]);
  1238. while ((bio = bio_list_pop(&list))) {
  1239. struct bio_set *bs = bio->bi_pool;
  1240. if (unlikely(!bs) || bs == fs_bio_set) {
  1241. bio_list_add(&current->bio_list[i], bio);
  1242. continue;
  1243. }
  1244. spin_lock(&bs->rescue_lock);
  1245. bio_list_add(&bs->rescue_list, bio);
  1246. queue_work(bs->rescue_workqueue, &bs->rescue_work);
  1247. spin_unlock(&bs->rescue_lock);
  1248. }
  1249. }
  1250. }
  1251. static void dm_offload_start(struct dm_offload *o)
  1252. {
  1253. blk_start_plug(&o->plug);
  1254. o->cb.callback = flush_current_bio_list;
  1255. list_add(&o->cb.list, &current->plug->cb_list);
  1256. }
  1257. static void dm_offload_end(struct dm_offload *o)
  1258. {
  1259. list_del(&o->cb.list);
  1260. blk_finish_plug(&o->plug);
  1261. }
  1262. static void __map_bio(struct dm_target_io *tio)
  1263. {
  1264. int r;
  1265. sector_t sector;
  1266. struct mapped_device *md;
  1267. struct dm_offload o;
  1268. struct bio *clone = &tio->clone;
  1269. struct dm_target *ti = tio->ti;
  1270. clone->bi_end_io = clone_endio;
  1271. /*
  1272. * Map the clone. If r == 0 we don't need to do
  1273. * anything, the target has assumed ownership of
  1274. * this io.
  1275. */
  1276. atomic_inc(&tio->io->io_count);
  1277. sector = clone->bi_iter.bi_sector;
  1278. dm_offload_start(&o);
  1279. r = ti->type->map(ti, clone);
  1280. dm_offload_end(&o);
  1281. if (r == DM_MAPIO_REMAPPED) {
  1282. /* the bio has been remapped so dispatch it */
  1283. trace_block_bio_remap(bdev_get_queue(clone->bi_bdev), clone,
  1284. tio->io->bio->bi_bdev->bd_dev, sector);
  1285. generic_make_request(clone);
  1286. } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
  1287. /* error the io and bail out, or requeue it if needed */
  1288. md = tio->io->md;
  1289. dec_pending(tio->io, r);
  1290. free_tio(md, tio);
  1291. } else if (r != DM_MAPIO_SUBMITTED) {
  1292. DMWARN("unimplemented target map return value: %d", r);
  1293. BUG();
  1294. }
  1295. }
  1296. struct clone_info {
  1297. struct mapped_device *md;
  1298. struct dm_table *map;
  1299. struct bio *bio;
  1300. struct dm_io *io;
  1301. sector_t sector;
  1302. unsigned sector_count;
  1303. };
  1304. static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
  1305. {
  1306. bio->bi_iter.bi_sector = sector;
  1307. bio->bi_iter.bi_size = to_bytes(len);
  1308. }
  1309. /*
  1310. * Creates a bio that consists of range of complete bvecs.
  1311. */
  1312. static void clone_bio(struct dm_target_io *tio, struct bio *bio,
  1313. sector_t sector, unsigned len)
  1314. {
  1315. struct bio *clone = &tio->clone;
  1316. __bio_clone_fast(clone, bio);
  1317. if (bio_integrity(bio))
  1318. bio_integrity_clone(clone, bio, GFP_NOIO);
  1319. bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
  1320. clone->bi_iter.bi_size = to_bytes(len);
  1321. if (bio_integrity(bio))
  1322. bio_integrity_trim(clone, 0, len);
  1323. }
  1324. static struct dm_target_io *alloc_tio(struct clone_info *ci,
  1325. struct dm_target *ti,
  1326. unsigned target_bio_nr)
  1327. {
  1328. struct dm_target_io *tio;
  1329. struct bio *clone;
  1330. clone = bio_alloc_bioset(GFP_NOIO, 0, ci->md->bs);
  1331. tio = container_of(clone, struct dm_target_io, clone);
  1332. tio->io = ci->io;
  1333. tio->ti = ti;
  1334. tio->target_bio_nr = target_bio_nr;
  1335. return tio;
  1336. }
  1337. static void __clone_and_map_simple_bio(struct clone_info *ci,
  1338. struct dm_target *ti,
  1339. unsigned target_bio_nr, unsigned *len)
  1340. {
  1341. struct dm_target_io *tio = alloc_tio(ci, ti, target_bio_nr);
  1342. struct bio *clone = &tio->clone;
  1343. tio->len_ptr = len;
  1344. __bio_clone_fast(clone, ci->bio);
  1345. if (len)
  1346. bio_setup_sector(clone, ci->sector, *len);
  1347. __map_bio(tio);
  1348. }
  1349. static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
  1350. unsigned num_bios, unsigned *len)
  1351. {
  1352. unsigned target_bio_nr;
  1353. for (target_bio_nr = 0; target_bio_nr < num_bios; target_bio_nr++)
  1354. __clone_and_map_simple_bio(ci, ti, target_bio_nr, len);
  1355. }
  1356. static int __send_empty_flush(struct clone_info *ci)
  1357. {
  1358. unsigned target_nr = 0;
  1359. struct dm_target *ti;
  1360. BUG_ON(bio_has_data(ci->bio));
  1361. while ((ti = dm_table_get_target(ci->map, target_nr++)))
  1362. __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
  1363. return 0;
  1364. }
  1365. static void __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
  1366. sector_t sector, unsigned *len)
  1367. {
  1368. struct bio *bio = ci->bio;
  1369. struct dm_target_io *tio;
  1370. unsigned target_bio_nr;
  1371. unsigned num_target_bios = 1;
  1372. /*
  1373. * Does the target want to receive duplicate copies of the bio?
  1374. */
  1375. if (bio_data_dir(bio) == WRITE && ti->num_write_bios)
  1376. num_target_bios = ti->num_write_bios(ti, bio);
  1377. for (target_bio_nr = 0; target_bio_nr < num_target_bios; target_bio_nr++) {
  1378. tio = alloc_tio(ci, ti, target_bio_nr);
  1379. tio->len_ptr = len;
  1380. clone_bio(tio, bio, sector, *len);
  1381. __map_bio(tio);
  1382. }
  1383. }
  1384. typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
  1385. static unsigned get_num_discard_bios(struct dm_target *ti)
  1386. {
  1387. return ti->num_discard_bios;
  1388. }
  1389. static unsigned get_num_write_same_bios(struct dm_target *ti)
  1390. {
  1391. return ti->num_write_same_bios;
  1392. }
  1393. typedef bool (*is_split_required_fn)(struct dm_target *ti);
  1394. static bool is_split_required_for_discard(struct dm_target *ti)
  1395. {
  1396. return ti->split_discard_bios;
  1397. }
  1398. static int __send_changing_extent_only(struct clone_info *ci,
  1399. get_num_bios_fn get_num_bios,
  1400. is_split_required_fn is_split_required)
  1401. {
  1402. struct dm_target *ti;
  1403. unsigned len;
  1404. unsigned num_bios;
  1405. do {
  1406. ti = dm_table_find_target(ci->map, ci->sector);
  1407. if (!dm_target_is_valid(ti))
  1408. return -EIO;
  1409. /*
  1410. * Even though the device advertised support for this type of
  1411. * request, that does not mean every target supports it, and
  1412. * reconfiguration might also have changed that since the
  1413. * check was performed.
  1414. */
  1415. num_bios = get_num_bios ? get_num_bios(ti) : 0;
  1416. if (!num_bios)
  1417. return -EOPNOTSUPP;
  1418. if (is_split_required && !is_split_required(ti))
  1419. len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
  1420. else
  1421. len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
  1422. __send_duplicate_bios(ci, ti, num_bios, &len);
  1423. ci->sector += len;
  1424. } while (ci->sector_count -= len);
  1425. return 0;
  1426. }
  1427. static int __send_discard(struct clone_info *ci)
  1428. {
  1429. return __send_changing_extent_only(ci, get_num_discard_bios,
  1430. is_split_required_for_discard);
  1431. }
  1432. static int __send_write_same(struct clone_info *ci)
  1433. {
  1434. return __send_changing_extent_only(ci, get_num_write_same_bios, NULL);
  1435. }
  1436. /*
  1437. * Select the correct strategy for processing a non-flush bio.
  1438. */
  1439. static int __split_and_process_non_flush(struct clone_info *ci)
  1440. {
  1441. struct bio *bio = ci->bio;
  1442. struct dm_target *ti;
  1443. unsigned len;
  1444. if (unlikely(bio->bi_rw & REQ_DISCARD))
  1445. return __send_discard(ci);
  1446. else if (unlikely(bio->bi_rw & REQ_WRITE_SAME))
  1447. return __send_write_same(ci);
  1448. ti = dm_table_find_target(ci->map, ci->sector);
  1449. if (!dm_target_is_valid(ti))
  1450. return -EIO;
  1451. len = min_t(sector_t, max_io_len(ci->sector, ti), ci->sector_count);
  1452. __clone_and_map_data_bio(ci, ti, ci->sector, &len);
  1453. ci->sector += len;
  1454. ci->sector_count -= len;
  1455. return 0;
  1456. }
  1457. /*
  1458. * Entry point to split a bio into clones and submit them to the targets.
  1459. */
  1460. static void __split_and_process_bio(struct mapped_device *md,
  1461. struct dm_table *map, struct bio *bio)
  1462. {
  1463. struct clone_info ci;
  1464. int error = 0;
  1465. if (unlikely(!map)) {
  1466. bio_io_error(bio);
  1467. return;
  1468. }
  1469. ci.map = map;
  1470. ci.md = md;
  1471. ci.io = alloc_io(md);
  1472. ci.io->error = 0;
  1473. atomic_set(&ci.io->io_count, 1);
  1474. ci.io->bio = bio;
  1475. ci.io->md = md;
  1476. spin_lock_init(&ci.io->endio_lock);
  1477. ci.sector = bio->bi_iter.bi_sector;
  1478. start_io_acct(ci.io);
  1479. if (bio->bi_rw & REQ_FLUSH) {
  1480. ci.bio = &ci.md->flush_bio;
  1481. ci.sector_count = 0;
  1482. error = __send_empty_flush(&ci);
  1483. /* dec_pending submits any data associated with flush */
  1484. } else {
  1485. ci.bio = bio;
  1486. ci.sector_count = bio_sectors(bio);
  1487. while (ci.sector_count && !error)
  1488. error = __split_and_process_non_flush(&ci);
  1489. }
  1490. /* drop the extra reference count */
  1491. dec_pending(ci.io, error);
  1492. }
  1493. /*-----------------------------------------------------------------
  1494. * CRUD END
  1495. *---------------------------------------------------------------*/
  1496. /*
  1497. * The request function that just remaps the bio built up by
  1498. * dm_merge_bvec.
  1499. */
  1500. static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
  1501. {
  1502. int rw = bio_data_dir(bio);
  1503. struct mapped_device *md = q->queuedata;
  1504. int srcu_idx;
  1505. struct dm_table *map;
  1506. map = dm_get_live_table(md, &srcu_idx);
  1507. generic_start_io_acct(rw, bio_sectors(bio), &dm_disk(md)->part0);
  1508. /* if we're suspended, we have to queue this io for later */
  1509. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
  1510. dm_put_live_table(md, srcu_idx);
  1511. if (bio_rw(bio) != READA)
  1512. queue_io(md, bio);
  1513. else
  1514. bio_io_error(bio);
  1515. return BLK_QC_T_NONE;
  1516. }
  1517. __split_and_process_bio(md, map, bio);
  1518. dm_put_live_table(md, srcu_idx);
  1519. return BLK_QC_T_NONE;
  1520. }
  1521. int dm_request_based(struct mapped_device *md)
  1522. {
  1523. return blk_queue_stackable(md->queue);
  1524. }
  1525. static void dm_dispatch_clone_request(struct request *clone, struct request *rq)
  1526. {
  1527. int r;
  1528. if (blk_queue_io_stat(clone->q))
  1529. clone->cmd_flags |= REQ_IO_STAT;
  1530. clone->start_time = jiffies;
  1531. r = blk_insert_cloned_request(clone->q, clone);
  1532. if (r)
  1533. /* must complete clone in terms of original request */
  1534. dm_complete_request(rq, r);
  1535. }
  1536. static int dm_rq_bio_constructor(struct bio *bio, struct bio *bio_orig,
  1537. void *data)
  1538. {
  1539. struct dm_rq_target_io *tio = data;
  1540. struct dm_rq_clone_bio_info *info =
  1541. container_of(bio, struct dm_rq_clone_bio_info, clone);
  1542. info->orig = bio_orig;
  1543. info->tio = tio;
  1544. bio->bi_end_io = end_clone_bio;
  1545. return 0;
  1546. }
  1547. static int setup_clone(struct request *clone, struct request *rq,
  1548. struct dm_rq_target_io *tio, gfp_t gfp_mask)
  1549. {
  1550. int r;
  1551. r = blk_rq_prep_clone(clone, rq, tio->md->bs, gfp_mask,
  1552. dm_rq_bio_constructor, tio);
  1553. if (r)
  1554. return r;
  1555. clone->cmd = rq->cmd;
  1556. clone->cmd_len = rq->cmd_len;
  1557. clone->sense = rq->sense;
  1558. clone->end_io = end_clone_request;
  1559. clone->end_io_data = tio;
  1560. tio->clone = clone;
  1561. return 0;
  1562. }
  1563. static struct request *clone_rq(struct request *rq, struct mapped_device *md,
  1564. struct dm_rq_target_io *tio, gfp_t gfp_mask)
  1565. {
  1566. /*
  1567. * Do not allocate a clone if tio->clone was already set
  1568. * (see: dm_mq_queue_rq).
  1569. */
  1570. bool alloc_clone = !tio->clone;
  1571. struct request *clone;
  1572. if (alloc_clone) {
  1573. clone = alloc_clone_request(md, gfp_mask);
  1574. if (!clone)
  1575. return NULL;
  1576. } else
  1577. clone = tio->clone;
  1578. blk_rq_init(NULL, clone);
  1579. if (setup_clone(clone, rq, tio, gfp_mask)) {
  1580. /* -ENOMEM */
  1581. if (alloc_clone)
  1582. free_clone_request(md, clone);
  1583. return NULL;
  1584. }
  1585. return clone;
  1586. }
  1587. static void map_tio_request(struct kthread_work *work);
  1588. static void init_tio(struct dm_rq_target_io *tio, struct request *rq,
  1589. struct mapped_device *md)
  1590. {
  1591. tio->md = md;
  1592. tio->ti = NULL;
  1593. tio->clone = NULL;
  1594. tio->orig = rq;
  1595. tio->error = 0;
  1596. memset(&tio->info, 0, sizeof(tio->info));
  1597. if (md->kworker_task)
  1598. init_kthread_work(&tio->work, map_tio_request);
  1599. }
  1600. static struct dm_rq_target_io *prep_tio(struct request *rq,
  1601. struct mapped_device *md, gfp_t gfp_mask)
  1602. {
  1603. struct dm_rq_target_io *tio;
  1604. int srcu_idx;
  1605. struct dm_table *table;
  1606. tio = alloc_rq_tio(md, gfp_mask);
  1607. if (!tio)
  1608. return NULL;
  1609. init_tio(tio, rq, md);
  1610. table = dm_get_live_table(md, &srcu_idx);
  1611. if (!dm_table_mq_request_based(table)) {
  1612. if (!clone_rq(rq, md, tio, gfp_mask)) {
  1613. dm_put_live_table(md, srcu_idx);
  1614. free_rq_tio(tio);
  1615. return NULL;
  1616. }
  1617. }
  1618. dm_put_live_table(md, srcu_idx);
  1619. return tio;
  1620. }
  1621. /*
  1622. * Called with the queue lock held.
  1623. */
  1624. static int dm_prep_fn(struct request_queue *q, struct request *rq)
  1625. {
  1626. struct mapped_device *md = q->queuedata;
  1627. struct dm_rq_target_io *tio;
  1628. if (unlikely(rq->special)) {
  1629. DMWARN("Already has something in rq->special.");
  1630. return BLKPREP_KILL;
  1631. }
  1632. tio = prep_tio(rq, md, GFP_ATOMIC);
  1633. if (!tio)
  1634. return BLKPREP_DEFER;
  1635. rq->special = tio;
  1636. rq->cmd_flags |= REQ_DONTPREP;
  1637. return BLKPREP_OK;
  1638. }
  1639. /*
  1640. * Returns:
  1641. * 0 : the request has been processed
  1642. * DM_MAPIO_REQUEUE : the original request needs to be requeued
  1643. * < 0 : the request was completed due to failure
  1644. */
  1645. static int map_request(struct dm_rq_target_io *tio, struct request *rq,
  1646. struct mapped_device *md)
  1647. {
  1648. int r;
  1649. struct dm_target *ti = tio->ti;
  1650. struct request *clone = NULL;
  1651. if (tio->clone) {
  1652. clone = tio->clone;
  1653. r = ti->type->map_rq(ti, clone, &tio->info);
  1654. } else {
  1655. r = ti->type->clone_and_map_rq(ti, rq, &tio->info, &clone);
  1656. if (r < 0) {
  1657. /* The target wants to complete the I/O */
  1658. dm_kill_unmapped_request(rq, r);
  1659. return r;
  1660. }
  1661. if (r != DM_MAPIO_REMAPPED)
  1662. return r;
  1663. if (setup_clone(clone, rq, tio, GFP_ATOMIC)) {
  1664. /* -ENOMEM */
  1665. ti->type->release_clone_rq(clone);
  1666. return DM_MAPIO_REQUEUE;
  1667. }
  1668. }
  1669. switch (r) {
  1670. case DM_MAPIO_SUBMITTED:
  1671. /* The target has taken the I/O to submit by itself later */
  1672. break;
  1673. case DM_MAPIO_REMAPPED:
  1674. /* The target has remapped the I/O so dispatch it */
  1675. trace_block_rq_remap(clone->q, clone, disk_devt(dm_disk(md)),
  1676. blk_rq_pos(rq));
  1677. dm_dispatch_clone_request(clone, rq);
  1678. break;
  1679. case DM_MAPIO_REQUEUE:
  1680. /* The target wants to requeue the I/O */
  1681. dm_requeue_original_request(md, tio->orig);
  1682. break;
  1683. default:
  1684. if (r > 0) {
  1685. DMWARN("unimplemented target map return value: %d", r);
  1686. BUG();
  1687. }
  1688. /* The target wants to complete the I/O */
  1689. dm_kill_unmapped_request(rq, r);
  1690. return r;
  1691. }
  1692. return 0;
  1693. }
  1694. static void map_tio_request(struct kthread_work *work)
  1695. {
  1696. struct dm_rq_target_io *tio = container_of(work, struct dm_rq_target_io, work);
  1697. struct request *rq = tio->orig;
  1698. struct mapped_device *md = tio->md;
  1699. if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE)
  1700. dm_requeue_original_request(md, rq);
  1701. }
  1702. static void dm_start_request(struct mapped_device *md, struct request *orig)
  1703. {
  1704. if (!orig->q->mq_ops)
  1705. blk_start_request(orig);
  1706. else
  1707. blk_mq_start_request(orig);
  1708. atomic_inc(&md->pending[rq_data_dir(orig)]);
  1709. if (md->seq_rq_merge_deadline_usecs) {
  1710. md->last_rq_pos = rq_end_sector(orig);
  1711. md->last_rq_rw = rq_data_dir(orig);
  1712. md->last_rq_start_time = ktime_get();
  1713. }
  1714. if (unlikely(dm_stats_used(&md->stats))) {
  1715. struct dm_rq_target_io *tio = tio_from_request(orig);
  1716. tio->duration_jiffies = jiffies;
  1717. tio->n_sectors = blk_rq_sectors(orig);
  1718. dm_stats_account_io(&md->stats, orig->cmd_flags, blk_rq_pos(orig),
  1719. tio->n_sectors, false, 0, &tio->stats_aux);
  1720. }
  1721. /*
  1722. * Hold the md reference here for the in-flight I/O.
  1723. * We can't rely on the reference count by device opener,
  1724. * because the device may be closed during the request completion
  1725. * when all bios are completed.
  1726. * See the comment in rq_completed() too.
  1727. */
  1728. dm_get(md);
  1729. }
  1730. #define MAX_SEQ_RQ_MERGE_DEADLINE_USECS 100000
  1731. ssize_t dm_attr_rq_based_seq_io_merge_deadline_show(struct mapped_device *md, char *buf)
  1732. {
  1733. return sprintf(buf, "%u\n", md->seq_rq_merge_deadline_usecs);
  1734. }
  1735. ssize_t dm_attr_rq_based_seq_io_merge_deadline_store(struct mapped_device *md,
  1736. const char *buf, size_t count)
  1737. {
  1738. unsigned deadline;
  1739. if (!dm_request_based(md) || md->use_blk_mq)
  1740. return count;
  1741. if (kstrtouint(buf, 10, &deadline))
  1742. return -EINVAL;
  1743. if (deadline > MAX_SEQ_RQ_MERGE_DEADLINE_USECS)
  1744. deadline = MAX_SEQ_RQ_MERGE_DEADLINE_USECS;
  1745. md->seq_rq_merge_deadline_usecs = deadline;
  1746. return count;
  1747. }
  1748. static bool dm_request_peeked_before_merge_deadline(struct mapped_device *md)
  1749. {
  1750. ktime_t kt_deadline;
  1751. if (!md->seq_rq_merge_deadline_usecs)
  1752. return false;
  1753. kt_deadline = ns_to_ktime((u64)md->seq_rq_merge_deadline_usecs * NSEC_PER_USEC);
  1754. kt_deadline = ktime_add_safe(md->last_rq_start_time, kt_deadline);
  1755. return !ktime_after(ktime_get(), kt_deadline);
  1756. }
  1757. /*
  1758. * q->request_fn for request-based dm.
  1759. * Called with the queue lock held.
  1760. */
  1761. static void dm_request_fn(struct request_queue *q)
  1762. {
  1763. struct mapped_device *md = q->queuedata;
  1764. int srcu_idx;
  1765. struct dm_table *map = dm_get_live_table(md, &srcu_idx);
  1766. struct dm_target *ti;
  1767. struct request *rq;
  1768. struct dm_rq_target_io *tio;
  1769. sector_t pos;
  1770. /*
  1771. * For suspend, check blk_queue_stopped() and increment
  1772. * ->pending within a single queue_lock not to increment the
  1773. * number of in-flight I/Os after the queue is stopped in
  1774. * dm_suspend().
  1775. */
  1776. while (!blk_queue_stopped(q)) {
  1777. rq = blk_peek_request(q);
  1778. if (!rq)
  1779. goto out;
  1780. /* always use block 0 to find the target for flushes for now */
  1781. pos = 0;
  1782. if (!(rq->cmd_flags & REQ_FLUSH))
  1783. pos = blk_rq_pos(rq);
  1784. ti = dm_table_find_target(map, pos);
  1785. if (!dm_target_is_valid(ti)) {
  1786. /*
  1787. * Must perform setup, that rq_completed() requires,
  1788. * before calling dm_kill_unmapped_request
  1789. */
  1790. DMERR_LIMIT("request attempted access beyond the end of device");
  1791. dm_start_request(md, rq);
  1792. dm_kill_unmapped_request(rq, -EIO);
  1793. continue;
  1794. }
  1795. if (dm_request_peeked_before_merge_deadline(md) &&
  1796. md_in_flight(md) && rq->bio && rq->bio->bi_vcnt == 1 &&
  1797. md->last_rq_pos == pos && md->last_rq_rw == rq_data_dir(rq))
  1798. goto delay_and_out;
  1799. if (ti->type->busy && ti->type->busy(ti))
  1800. goto delay_and_out;
  1801. dm_start_request(md, rq);
  1802. tio = tio_from_request(rq);
  1803. /* Establish tio->ti before queuing work (map_tio_request) */
  1804. tio->ti = ti;
  1805. queue_kthread_work(&md->kworker, &tio->work);
  1806. BUG_ON(!irqs_disabled());
  1807. }
  1808. goto out;
  1809. delay_and_out:
  1810. blk_delay_queue(q, HZ / 100);
  1811. out:
  1812. dm_put_live_table(md, srcu_idx);
  1813. }
  1814. static int dm_any_congested(void *congested_data, int bdi_bits)
  1815. {
  1816. int r = bdi_bits;
  1817. struct mapped_device *md = congested_data;
  1818. struct dm_table *map;
  1819. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1820. map = dm_get_live_table_fast(md);
  1821. if (map) {
  1822. /*
  1823. * Request-based dm cares about only own queue for
  1824. * the query about congestion status of request_queue
  1825. */
  1826. if (dm_request_based(md))
  1827. r = md->queue->backing_dev_info.wb.state &
  1828. bdi_bits;
  1829. else
  1830. r = dm_table_any_congested(map, bdi_bits);
  1831. }
  1832. dm_put_live_table_fast(md);
  1833. }
  1834. return r;
  1835. }
  1836. /*-----------------------------------------------------------------
  1837. * An IDR is used to keep track of allocated minor numbers.
  1838. *---------------------------------------------------------------*/
  1839. static void free_minor(int minor)
  1840. {
  1841. spin_lock(&_minor_lock);
  1842. idr_remove(&_minor_idr, minor);
  1843. spin_unlock(&_minor_lock);
  1844. }
  1845. /*
  1846. * See if the device with a specific minor # is free.
  1847. */
  1848. static int specific_minor(int minor)
  1849. {
  1850. int r;
  1851. if (minor >= (1 << MINORBITS))
  1852. return -EINVAL;
  1853. idr_preload(GFP_KERNEL);
  1854. spin_lock(&_minor_lock);
  1855. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
  1856. spin_unlock(&_minor_lock);
  1857. idr_preload_end();
  1858. if (r < 0)
  1859. return r == -ENOSPC ? -EBUSY : r;
  1860. return 0;
  1861. }
  1862. static int next_free_minor(int *minor)
  1863. {
  1864. int r;
  1865. idr_preload(GFP_KERNEL);
  1866. spin_lock(&_minor_lock);
  1867. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
  1868. spin_unlock(&_minor_lock);
  1869. idr_preload_end();
  1870. if (r < 0)
  1871. return r;
  1872. *minor = r;
  1873. return 0;
  1874. }
  1875. static const struct block_device_operations dm_blk_dops;
  1876. static void dm_wq_work(struct work_struct *work);
  1877. static void dm_init_md_queue(struct mapped_device *md)
  1878. {
  1879. /*
  1880. * Request-based dm devices cannot be stacked on top of bio-based dm
  1881. * devices. The type of this dm device may not have been decided yet.
  1882. * The type is decided at the first table loading time.
  1883. * To prevent problematic device stacking, clear the queue flag
  1884. * for request stacking support until then.
  1885. *
  1886. * This queue is new, so no concurrency on the queue_flags.
  1887. */
  1888. queue_flag_clear_unlocked(QUEUE_FLAG_STACKABLE, md->queue);
  1889. /*
  1890. * Initialize data that will only be used by a non-blk-mq DM queue
  1891. * - must do so here (in alloc_dev callchain) before queue is used
  1892. */
  1893. md->queue->queuedata = md;
  1894. md->queue->backing_dev_info.congested_data = md;
  1895. }
  1896. static void dm_init_old_md_queue(struct mapped_device *md)
  1897. {
  1898. md->use_blk_mq = false;
  1899. dm_init_md_queue(md);
  1900. /*
  1901. * Initialize aspects of queue that aren't relevant for blk-mq
  1902. */
  1903. md->queue->backing_dev_info.congested_fn = dm_any_congested;
  1904. blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
  1905. }
  1906. static void cleanup_mapped_device(struct mapped_device *md)
  1907. {
  1908. if (md->wq)
  1909. destroy_workqueue(md->wq);
  1910. if (md->kworker_task)
  1911. kthread_stop(md->kworker_task);
  1912. mempool_destroy(md->io_pool);
  1913. mempool_destroy(md->rq_pool);
  1914. if (md->bs)
  1915. bioset_free(md->bs);
  1916. if (md->disk) {
  1917. spin_lock(&_minor_lock);
  1918. md->disk->private_data = NULL;
  1919. spin_unlock(&_minor_lock);
  1920. del_gendisk(md->disk);
  1921. put_disk(md->disk);
  1922. }
  1923. if (md->queue)
  1924. blk_cleanup_queue(md->queue);
  1925. cleanup_srcu_struct(&md->io_barrier);
  1926. if (md->bdev) {
  1927. bdput(md->bdev);
  1928. md->bdev = NULL;
  1929. }
  1930. }
  1931. /*
  1932. * Allocate and initialise a blank device with a given minor.
  1933. */
  1934. static struct mapped_device *alloc_dev(int minor)
  1935. {
  1936. int r;
  1937. struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
  1938. void *old_md;
  1939. if (!md) {
  1940. DMWARN("unable to allocate device, out of memory.");
  1941. return NULL;
  1942. }
  1943. if (!try_module_get(THIS_MODULE))
  1944. goto bad_module_get;
  1945. /* get a minor number for the dev */
  1946. if (minor == DM_ANY_MINOR)
  1947. r = next_free_minor(&minor);
  1948. else
  1949. r = specific_minor(minor);
  1950. if (r < 0)
  1951. goto bad_minor;
  1952. r = init_srcu_struct(&md->io_barrier);
  1953. if (r < 0)
  1954. goto bad_io_barrier;
  1955. md->use_blk_mq = use_blk_mq;
  1956. md->type = DM_TYPE_NONE;
  1957. mutex_init(&md->suspend_lock);
  1958. mutex_init(&md->type_lock);
  1959. mutex_init(&md->table_devices_lock);
  1960. spin_lock_init(&md->deferred_lock);
  1961. atomic_set(&md->holders, 1);
  1962. atomic_set(&md->open_count, 0);
  1963. atomic_set(&md->event_nr, 0);
  1964. atomic_set(&md->uevent_seq, 0);
  1965. INIT_LIST_HEAD(&md->uevent_list);
  1966. INIT_LIST_HEAD(&md->table_devices);
  1967. spin_lock_init(&md->uevent_lock);
  1968. md->queue = blk_alloc_queue(GFP_KERNEL);
  1969. if (!md->queue)
  1970. goto bad;
  1971. dm_init_md_queue(md);
  1972. md->disk = alloc_disk(1);
  1973. if (!md->disk)
  1974. goto bad;
  1975. atomic_set(&md->pending[0], 0);
  1976. atomic_set(&md->pending[1], 0);
  1977. init_waitqueue_head(&md->wait);
  1978. INIT_WORK(&md->work, dm_wq_work);
  1979. init_waitqueue_head(&md->eventq);
  1980. init_completion(&md->kobj_holder.completion);
  1981. md->kworker_task = NULL;
  1982. md->disk->major = _major;
  1983. md->disk->first_minor = minor;
  1984. md->disk->fops = &dm_blk_dops;
  1985. md->disk->queue = md->queue;
  1986. md->disk->private_data = md;
  1987. sprintf(md->disk->disk_name, "dm-%d", minor);
  1988. add_disk(md->disk);
  1989. format_dev_t(md->name, MKDEV(_major, minor));
  1990. md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
  1991. if (!md->wq)
  1992. goto bad;
  1993. md->bdev = bdget_disk(md->disk, 0);
  1994. if (!md->bdev)
  1995. goto bad;
  1996. bio_init(&md->flush_bio);
  1997. md->flush_bio.bi_bdev = md->bdev;
  1998. md->flush_bio.bi_rw = WRITE_FLUSH;
  1999. dm_stats_init(&md->stats);
  2000. /* Populate the mapping, nobody knows we exist yet */
  2001. spin_lock(&_minor_lock);
  2002. old_md = idr_replace(&_minor_idr, md, minor);
  2003. spin_unlock(&_minor_lock);
  2004. BUG_ON(old_md != MINOR_ALLOCED);
  2005. return md;
  2006. bad:
  2007. cleanup_mapped_device(md);
  2008. bad_io_barrier:
  2009. free_minor(minor);
  2010. bad_minor:
  2011. module_put(THIS_MODULE);
  2012. bad_module_get:
  2013. kfree(md);
  2014. return NULL;
  2015. }
  2016. static void unlock_fs(struct mapped_device *md);
  2017. static void free_dev(struct mapped_device *md)
  2018. {
  2019. int minor = MINOR(disk_devt(md->disk));
  2020. unlock_fs(md);
  2021. cleanup_mapped_device(md);
  2022. if (md->use_blk_mq)
  2023. blk_mq_free_tag_set(&md->tag_set);
  2024. free_table_devices(&md->table_devices);
  2025. dm_stats_cleanup(&md->stats);
  2026. free_minor(minor);
  2027. module_put(THIS_MODULE);
  2028. kfree(md);
  2029. }
  2030. static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
  2031. {
  2032. struct dm_md_mempools *p = dm_table_get_md_mempools(t);
  2033. if (md->bs) {
  2034. /* The md already has necessary mempools. */
  2035. if (dm_table_get_type(t) == DM_TYPE_BIO_BASED) {
  2036. /*
  2037. * Reload bioset because front_pad may have changed
  2038. * because a different table was loaded.
  2039. */
  2040. bioset_free(md->bs);
  2041. md->bs = p->bs;
  2042. p->bs = NULL;
  2043. }
  2044. /*
  2045. * There's no need to reload with request-based dm
  2046. * because the size of front_pad doesn't change.
  2047. * Note for future: If you are to reload bioset,
  2048. * prep-ed requests in the queue may refer
  2049. * to bio from the old bioset, so you must walk
  2050. * through the queue to unprep.
  2051. */
  2052. goto out;
  2053. }
  2054. BUG_ON(!p || md->io_pool || md->rq_pool || md->bs);
  2055. md->io_pool = p->io_pool;
  2056. p->io_pool = NULL;
  2057. md->rq_pool = p->rq_pool;
  2058. p->rq_pool = NULL;
  2059. md->bs = p->bs;
  2060. p->bs = NULL;
  2061. out:
  2062. /* mempool bind completed, no longer need any mempools in the table */
  2063. dm_table_free_md_mempools(t);
  2064. }
  2065. /*
  2066. * Bind a table to the device.
  2067. */
  2068. static void event_callback(void *context)
  2069. {
  2070. unsigned long flags;
  2071. LIST_HEAD(uevents);
  2072. struct mapped_device *md = (struct mapped_device *) context;
  2073. spin_lock_irqsave(&md->uevent_lock, flags);
  2074. list_splice_init(&md->uevent_list, &uevents);
  2075. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2076. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  2077. atomic_inc(&md->event_nr);
  2078. wake_up(&md->eventq);
  2079. }
  2080. /*
  2081. * Protected by md->suspend_lock obtained by dm_swap_table().
  2082. */
  2083. static void __set_size(struct mapped_device *md, sector_t size)
  2084. {
  2085. set_capacity(md->disk, size);
  2086. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  2087. }
  2088. /*
  2089. * Returns old map, which caller must destroy.
  2090. */
  2091. static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
  2092. struct queue_limits *limits)
  2093. {
  2094. struct dm_table *old_map;
  2095. struct request_queue *q = md->queue;
  2096. sector_t size;
  2097. size = dm_table_get_size(t);
  2098. /*
  2099. * Wipe any geometry if the size of the table changed.
  2100. */
  2101. if (size != dm_get_size(md))
  2102. memset(&md->geometry, 0, sizeof(md->geometry));
  2103. __set_size(md, size);
  2104. dm_table_event_callback(t, event_callback, md);
  2105. /*
  2106. * The queue hasn't been stopped yet, if the old table type wasn't
  2107. * for request-based during suspension. So stop it to prevent
  2108. * I/O mapping before resume.
  2109. * This must be done before setting the queue restrictions,
  2110. * because request-based dm may be run just after the setting.
  2111. */
  2112. if (dm_table_request_based(t))
  2113. stop_queue(q);
  2114. __bind_mempools(md, t);
  2115. old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2116. rcu_assign_pointer(md->map, t);
  2117. md->immutable_target_type = dm_table_get_immutable_target_type(t);
  2118. dm_table_set_restrictions(t, q, limits);
  2119. if (old_map)
  2120. dm_sync_table(md);
  2121. return old_map;
  2122. }
  2123. /*
  2124. * Returns unbound table for the caller to free.
  2125. */
  2126. static struct dm_table *__unbind(struct mapped_device *md)
  2127. {
  2128. struct dm_table *map = rcu_dereference_protected(md->map, 1);
  2129. if (!map)
  2130. return NULL;
  2131. dm_table_event_callback(map, NULL, NULL);
  2132. RCU_INIT_POINTER(md->map, NULL);
  2133. dm_sync_table(md);
  2134. return map;
  2135. }
  2136. /*
  2137. * Constructor for a new device.
  2138. */
  2139. int dm_create(int minor, struct mapped_device **result)
  2140. {
  2141. struct mapped_device *md;
  2142. md = alloc_dev(minor);
  2143. if (!md)
  2144. return -ENXIO;
  2145. dm_sysfs_init(md);
  2146. *result = md;
  2147. return 0;
  2148. }
  2149. /*
  2150. * Functions to manage md->type.
  2151. * All are required to hold md->type_lock.
  2152. */
  2153. void dm_lock_md_type(struct mapped_device *md)
  2154. {
  2155. mutex_lock(&md->type_lock);
  2156. }
  2157. void dm_unlock_md_type(struct mapped_device *md)
  2158. {
  2159. mutex_unlock(&md->type_lock);
  2160. }
  2161. void dm_set_md_type(struct mapped_device *md, unsigned type)
  2162. {
  2163. BUG_ON(!mutex_is_locked(&md->type_lock));
  2164. md->type = type;
  2165. }
  2166. unsigned dm_get_md_type(struct mapped_device *md)
  2167. {
  2168. BUG_ON(!mutex_is_locked(&md->type_lock));
  2169. return md->type;
  2170. }
  2171. struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
  2172. {
  2173. return md->immutable_target_type;
  2174. }
  2175. /*
  2176. * The queue_limits are only valid as long as you have a reference
  2177. * count on 'md'.
  2178. */
  2179. struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
  2180. {
  2181. BUG_ON(!atomic_read(&md->holders));
  2182. return &md->queue->limits;
  2183. }
  2184. EXPORT_SYMBOL_GPL(dm_get_queue_limits);
  2185. static void init_rq_based_worker_thread(struct mapped_device *md)
  2186. {
  2187. /* Initialize the request-based DM worker thread */
  2188. init_kthread_worker(&md->kworker);
  2189. md->kworker_task = kthread_run(kthread_worker_fn, &md->kworker,
  2190. "kdmwork-%s", dm_device_name(md));
  2191. }
  2192. /*
  2193. * Fully initialize a request-based queue (->elevator, ->request_fn, etc).
  2194. */
  2195. static int dm_init_request_based_queue(struct mapped_device *md)
  2196. {
  2197. struct request_queue *q = NULL;
  2198. /* Fully initialize the queue */
  2199. q = blk_init_allocated_queue(md->queue, dm_request_fn, NULL);
  2200. if (!q)
  2201. return -EINVAL;
  2202. /* disable dm_request_fn's merge heuristic by default */
  2203. md->seq_rq_merge_deadline_usecs = 0;
  2204. md->queue = q;
  2205. dm_init_old_md_queue(md);
  2206. blk_queue_softirq_done(md->queue, dm_softirq_done);
  2207. blk_queue_prep_rq(md->queue, dm_prep_fn);
  2208. init_rq_based_worker_thread(md);
  2209. elv_register_queue(md->queue);
  2210. return 0;
  2211. }
  2212. static int dm_mq_init_request(void *data, struct request *rq,
  2213. unsigned int hctx_idx, unsigned int request_idx,
  2214. unsigned int numa_node)
  2215. {
  2216. struct mapped_device *md = data;
  2217. struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
  2218. /*
  2219. * Must initialize md member of tio, otherwise it won't
  2220. * be available in dm_mq_queue_rq.
  2221. */
  2222. tio->md = md;
  2223. return 0;
  2224. }
  2225. static int dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
  2226. const struct blk_mq_queue_data *bd)
  2227. {
  2228. struct request *rq = bd->rq;
  2229. struct dm_rq_target_io *tio = blk_mq_rq_to_pdu(rq);
  2230. struct mapped_device *md = tio->md;
  2231. int srcu_idx;
  2232. struct dm_table *map = dm_get_live_table(md, &srcu_idx);
  2233. struct dm_target *ti;
  2234. sector_t pos;
  2235. /* always use block 0 to find the target for flushes for now */
  2236. pos = 0;
  2237. if (!(rq->cmd_flags & REQ_FLUSH))
  2238. pos = blk_rq_pos(rq);
  2239. ti = dm_table_find_target(map, pos);
  2240. if (!dm_target_is_valid(ti)) {
  2241. dm_put_live_table(md, srcu_idx);
  2242. DMERR_LIMIT("request attempted access beyond the end of device");
  2243. /*
  2244. * Must perform setup, that rq_completed() requires,
  2245. * before returning BLK_MQ_RQ_QUEUE_ERROR
  2246. */
  2247. dm_start_request(md, rq);
  2248. return BLK_MQ_RQ_QUEUE_ERROR;
  2249. }
  2250. dm_put_live_table(md, srcu_idx);
  2251. if (ti->type->busy && ti->type->busy(ti))
  2252. return BLK_MQ_RQ_QUEUE_BUSY;
  2253. dm_start_request(md, rq);
  2254. /* Init tio using md established in .init_request */
  2255. init_tio(tio, rq, md);
  2256. /*
  2257. * Establish tio->ti before queuing work (map_tio_request)
  2258. * or making direct call to map_request().
  2259. */
  2260. tio->ti = ti;
  2261. /* Clone the request if underlying devices aren't blk-mq */
  2262. if (dm_table_get_type(map) == DM_TYPE_REQUEST_BASED) {
  2263. /* clone request is allocated at the end of the pdu */
  2264. tio->clone = (void *)blk_mq_rq_to_pdu(rq) + sizeof(struct dm_rq_target_io);
  2265. (void) clone_rq(rq, md, tio, GFP_ATOMIC);
  2266. queue_kthread_work(&md->kworker, &tio->work);
  2267. } else {
  2268. /* Direct call is fine since .queue_rq allows allocations */
  2269. if (map_request(tio, rq, md) == DM_MAPIO_REQUEUE) {
  2270. /* Undo dm_start_request() before requeuing */
  2271. rq_end_stats(md, rq);
  2272. rq_completed(md, rq_data_dir(rq), false);
  2273. return BLK_MQ_RQ_QUEUE_BUSY;
  2274. }
  2275. }
  2276. return BLK_MQ_RQ_QUEUE_OK;
  2277. }
  2278. static struct blk_mq_ops dm_mq_ops = {
  2279. .queue_rq = dm_mq_queue_rq,
  2280. .map_queue = blk_mq_map_queue,
  2281. .complete = dm_softirq_done,
  2282. .init_request = dm_mq_init_request,
  2283. };
  2284. static int dm_init_request_based_blk_mq_queue(struct mapped_device *md)
  2285. {
  2286. unsigned md_type = dm_get_md_type(md);
  2287. struct request_queue *q;
  2288. int err;
  2289. memset(&md->tag_set, 0, sizeof(md->tag_set));
  2290. md->tag_set.ops = &dm_mq_ops;
  2291. md->tag_set.queue_depth = BLKDEV_MAX_RQ;
  2292. md->tag_set.numa_node = NUMA_NO_NODE;
  2293. md->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
  2294. md->tag_set.nr_hw_queues = 1;
  2295. if (md_type == DM_TYPE_REQUEST_BASED) {
  2296. /* make the memory for non-blk-mq clone part of the pdu */
  2297. md->tag_set.cmd_size = sizeof(struct dm_rq_target_io) + sizeof(struct request);
  2298. } else
  2299. md->tag_set.cmd_size = sizeof(struct dm_rq_target_io);
  2300. md->tag_set.driver_data = md;
  2301. err = blk_mq_alloc_tag_set(&md->tag_set);
  2302. if (err)
  2303. return err;
  2304. q = blk_mq_init_allocated_queue(&md->tag_set, md->queue);
  2305. if (IS_ERR(q)) {
  2306. err = PTR_ERR(q);
  2307. goto out_tag_set;
  2308. }
  2309. md->queue = q;
  2310. dm_init_md_queue(md);
  2311. /* backfill 'mq' sysfs registration normally done in blk_register_queue */
  2312. blk_mq_register_disk(md->disk);
  2313. if (md_type == DM_TYPE_REQUEST_BASED)
  2314. init_rq_based_worker_thread(md);
  2315. return 0;
  2316. out_tag_set:
  2317. blk_mq_free_tag_set(&md->tag_set);
  2318. return err;
  2319. }
  2320. static unsigned filter_md_type(unsigned type, struct mapped_device *md)
  2321. {
  2322. if (type == DM_TYPE_BIO_BASED)
  2323. return type;
  2324. return !md->use_blk_mq ? DM_TYPE_REQUEST_BASED : DM_TYPE_MQ_REQUEST_BASED;
  2325. }
  2326. /*
  2327. * Setup the DM device's queue based on md's type
  2328. */
  2329. int dm_setup_md_queue(struct mapped_device *md)
  2330. {
  2331. int r;
  2332. unsigned md_type = filter_md_type(dm_get_md_type(md), md);
  2333. switch (md_type) {
  2334. case DM_TYPE_REQUEST_BASED:
  2335. r = dm_init_request_based_queue(md);
  2336. if (r) {
  2337. DMWARN("Cannot initialize queue for request-based mapped device");
  2338. return r;
  2339. }
  2340. break;
  2341. case DM_TYPE_MQ_REQUEST_BASED:
  2342. r = dm_init_request_based_blk_mq_queue(md);
  2343. if (r) {
  2344. DMWARN("Cannot initialize queue for request-based blk-mq mapped device");
  2345. return r;
  2346. }
  2347. break;
  2348. case DM_TYPE_BIO_BASED:
  2349. dm_init_old_md_queue(md);
  2350. blk_queue_make_request(md->queue, dm_make_request);
  2351. /*
  2352. * DM handles splitting bios as needed. Free the bio_split bioset
  2353. * since it won't be used (saves 1 process per bio-based DM device).
  2354. */
  2355. bioset_free(md->queue->bio_split);
  2356. md->queue->bio_split = NULL;
  2357. break;
  2358. }
  2359. return 0;
  2360. }
  2361. struct mapped_device *dm_get_md(dev_t dev)
  2362. {
  2363. struct mapped_device *md;
  2364. unsigned minor = MINOR(dev);
  2365. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  2366. return NULL;
  2367. spin_lock(&_minor_lock);
  2368. md = idr_find(&_minor_idr, minor);
  2369. if (md) {
  2370. if ((md == MINOR_ALLOCED ||
  2371. (MINOR(disk_devt(dm_disk(md))) != minor) ||
  2372. dm_deleting_md(md) ||
  2373. test_bit(DMF_FREEING, &md->flags))) {
  2374. md = NULL;
  2375. goto out;
  2376. }
  2377. dm_get(md);
  2378. }
  2379. out:
  2380. spin_unlock(&_minor_lock);
  2381. return md;
  2382. }
  2383. EXPORT_SYMBOL_GPL(dm_get_md);
  2384. void *dm_get_mdptr(struct mapped_device *md)
  2385. {
  2386. return md->interface_ptr;
  2387. }
  2388. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  2389. {
  2390. md->interface_ptr = ptr;
  2391. }
  2392. void dm_get(struct mapped_device *md)
  2393. {
  2394. atomic_inc(&md->holders);
  2395. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  2396. }
  2397. int dm_hold(struct mapped_device *md)
  2398. {
  2399. spin_lock(&_minor_lock);
  2400. if (test_bit(DMF_FREEING, &md->flags)) {
  2401. spin_unlock(&_minor_lock);
  2402. return -EBUSY;
  2403. }
  2404. dm_get(md);
  2405. spin_unlock(&_minor_lock);
  2406. return 0;
  2407. }
  2408. EXPORT_SYMBOL_GPL(dm_hold);
  2409. const char *dm_device_name(struct mapped_device *md)
  2410. {
  2411. return md->name;
  2412. }
  2413. EXPORT_SYMBOL_GPL(dm_device_name);
  2414. static void __dm_destroy(struct mapped_device *md, bool wait)
  2415. {
  2416. struct request_queue *q = dm_get_md_queue(md);
  2417. struct dm_table *map;
  2418. int srcu_idx;
  2419. might_sleep();
  2420. spin_lock(&_minor_lock);
  2421. idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
  2422. set_bit(DMF_FREEING, &md->flags);
  2423. spin_unlock(&_minor_lock);
  2424. spin_lock_irq(q->queue_lock);
  2425. queue_flag_set(QUEUE_FLAG_DYING, q);
  2426. spin_unlock_irq(q->queue_lock);
  2427. if (dm_request_based(md) && md->kworker_task)
  2428. flush_kthread_worker(&md->kworker);
  2429. /*
  2430. * Take suspend_lock so that presuspend and postsuspend methods
  2431. * do not race with internal suspend.
  2432. */
  2433. mutex_lock(&md->suspend_lock);
  2434. map = dm_get_live_table(md, &srcu_idx);
  2435. if (!dm_suspended_md(md)) {
  2436. dm_table_presuspend_targets(map);
  2437. dm_table_postsuspend_targets(map);
  2438. }
  2439. /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
  2440. dm_put_live_table(md, srcu_idx);
  2441. mutex_unlock(&md->suspend_lock);
  2442. /*
  2443. * Rare, but there may be I/O requests still going to complete,
  2444. * for example. Wait for all references to disappear.
  2445. * No one should increment the reference count of the mapped_device,
  2446. * after the mapped_device state becomes DMF_FREEING.
  2447. */
  2448. if (wait)
  2449. while (atomic_read(&md->holders))
  2450. msleep(1);
  2451. else if (atomic_read(&md->holders))
  2452. DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
  2453. dm_device_name(md), atomic_read(&md->holders));
  2454. dm_sysfs_exit(md);
  2455. dm_table_destroy(__unbind(md));
  2456. free_dev(md);
  2457. }
  2458. void dm_destroy(struct mapped_device *md)
  2459. {
  2460. __dm_destroy(md, true);
  2461. }
  2462. void dm_destroy_immediate(struct mapped_device *md)
  2463. {
  2464. __dm_destroy(md, false);
  2465. }
  2466. void dm_put(struct mapped_device *md)
  2467. {
  2468. atomic_dec(&md->holders);
  2469. }
  2470. EXPORT_SYMBOL_GPL(dm_put);
  2471. static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
  2472. {
  2473. int r = 0;
  2474. DECLARE_WAITQUEUE(wait, current);
  2475. add_wait_queue(&md->wait, &wait);
  2476. while (1) {
  2477. set_current_state(interruptible);
  2478. if (!md_in_flight(md))
  2479. break;
  2480. if (interruptible == TASK_INTERRUPTIBLE &&
  2481. signal_pending(current)) {
  2482. r = -EINTR;
  2483. break;
  2484. }
  2485. io_schedule();
  2486. }
  2487. set_current_state(TASK_RUNNING);
  2488. remove_wait_queue(&md->wait, &wait);
  2489. return r;
  2490. }
  2491. /*
  2492. * Process the deferred bios
  2493. */
  2494. static void dm_wq_work(struct work_struct *work)
  2495. {
  2496. struct mapped_device *md = container_of(work, struct mapped_device,
  2497. work);
  2498. struct bio *c;
  2499. int srcu_idx;
  2500. struct dm_table *map;
  2501. map = dm_get_live_table(md, &srcu_idx);
  2502. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  2503. spin_lock_irq(&md->deferred_lock);
  2504. c = bio_list_pop(&md->deferred);
  2505. spin_unlock_irq(&md->deferred_lock);
  2506. if (!c)
  2507. break;
  2508. if (dm_request_based(md))
  2509. generic_make_request(c);
  2510. else
  2511. __split_and_process_bio(md, map, c);
  2512. }
  2513. dm_put_live_table(md, srcu_idx);
  2514. }
  2515. static void dm_queue_flush(struct mapped_device *md)
  2516. {
  2517. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2518. smp_mb__after_atomic();
  2519. queue_work(md->wq, &md->work);
  2520. }
  2521. /*
  2522. * Swap in a new table, returning the old one for the caller to destroy.
  2523. */
  2524. struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
  2525. {
  2526. struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
  2527. struct queue_limits limits;
  2528. int r;
  2529. mutex_lock(&md->suspend_lock);
  2530. /* device must be suspended */
  2531. if (!dm_suspended_md(md))
  2532. goto out;
  2533. /*
  2534. * If the new table has no data devices, retain the existing limits.
  2535. * This helps multipath with queue_if_no_path if all paths disappear,
  2536. * then new I/O is queued based on these limits, and then some paths
  2537. * reappear.
  2538. */
  2539. if (dm_table_has_no_data_devices(table)) {
  2540. live_map = dm_get_live_table_fast(md);
  2541. if (live_map)
  2542. limits = md->queue->limits;
  2543. dm_put_live_table_fast(md);
  2544. }
  2545. if (!live_map) {
  2546. r = dm_calculate_queue_limits(table, &limits);
  2547. if (r) {
  2548. map = ERR_PTR(r);
  2549. goto out;
  2550. }
  2551. }
  2552. map = __bind(md, table, &limits);
  2553. out:
  2554. mutex_unlock(&md->suspend_lock);
  2555. return map;
  2556. }
  2557. /*
  2558. * Functions to lock and unlock any filesystem running on the
  2559. * device.
  2560. */
  2561. static int lock_fs(struct mapped_device *md)
  2562. {
  2563. int r;
  2564. WARN_ON(md->frozen_sb);
  2565. md->frozen_sb = freeze_bdev(md->bdev);
  2566. if (IS_ERR(md->frozen_sb)) {
  2567. r = PTR_ERR(md->frozen_sb);
  2568. md->frozen_sb = NULL;
  2569. return r;
  2570. }
  2571. set_bit(DMF_FROZEN, &md->flags);
  2572. return 0;
  2573. }
  2574. static void unlock_fs(struct mapped_device *md)
  2575. {
  2576. if (!test_bit(DMF_FROZEN, &md->flags))
  2577. return;
  2578. thaw_bdev(md->bdev, md->frozen_sb);
  2579. md->frozen_sb = NULL;
  2580. clear_bit(DMF_FROZEN, &md->flags);
  2581. }
  2582. /*
  2583. * If __dm_suspend returns 0, the device is completely quiescent
  2584. * now. There is no request-processing activity. All new requests
  2585. * are being added to md->deferred list.
  2586. *
  2587. * Caller must hold md->suspend_lock
  2588. */
  2589. static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
  2590. unsigned suspend_flags, int interruptible,
  2591. int dmf_suspended_flag)
  2592. {
  2593. bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
  2594. bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
  2595. int r;
  2596. /*
  2597. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  2598. * This flag is cleared before dm_suspend returns.
  2599. */
  2600. if (noflush)
  2601. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2602. /*
  2603. * This gets reverted if there's an error later and the targets
  2604. * provide the .presuspend_undo hook.
  2605. */
  2606. dm_table_presuspend_targets(map);
  2607. /*
  2608. * Flush I/O to the device.
  2609. * Any I/O submitted after lock_fs() may not be flushed.
  2610. * noflush takes precedence over do_lockfs.
  2611. * (lock_fs() flushes I/Os and waits for them to complete.)
  2612. */
  2613. if (!noflush && do_lockfs) {
  2614. r = lock_fs(md);
  2615. if (r) {
  2616. dm_table_presuspend_undo_targets(map);
  2617. return r;
  2618. }
  2619. }
  2620. /*
  2621. * Here we must make sure that no processes are submitting requests
  2622. * to target drivers i.e. no one may be executing
  2623. * __split_and_process_bio. This is called from dm_request and
  2624. * dm_wq_work.
  2625. *
  2626. * To get all processes out of __split_and_process_bio in dm_request,
  2627. * we take the write lock. To prevent any process from reentering
  2628. * __split_and_process_bio from dm_request and quiesce the thread
  2629. * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
  2630. * flush_workqueue(md->wq).
  2631. */
  2632. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2633. if (map)
  2634. synchronize_srcu(&md->io_barrier);
  2635. /*
  2636. * Stop md->queue before flushing md->wq in case request-based
  2637. * dm defers requests to md->wq from md->queue.
  2638. */
  2639. if (dm_request_based(md)) {
  2640. stop_queue(md->queue);
  2641. if (md->kworker_task)
  2642. flush_kthread_worker(&md->kworker);
  2643. }
  2644. flush_workqueue(md->wq);
  2645. /*
  2646. * At this point no more requests are entering target request routines.
  2647. * We call dm_wait_for_completion to wait for all existing requests
  2648. * to finish.
  2649. */
  2650. r = dm_wait_for_completion(md, interruptible);
  2651. if (!r)
  2652. set_bit(dmf_suspended_flag, &md->flags);
  2653. if (noflush)
  2654. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2655. if (map)
  2656. synchronize_srcu(&md->io_barrier);
  2657. /* were we interrupted ? */
  2658. if (r < 0) {
  2659. dm_queue_flush(md);
  2660. if (dm_request_based(md))
  2661. start_queue(md->queue);
  2662. unlock_fs(md);
  2663. dm_table_presuspend_undo_targets(map);
  2664. /* pushback list is already flushed, so skip flush */
  2665. }
  2666. return r;
  2667. }
  2668. /*
  2669. * We need to be able to change a mapping table under a mounted
  2670. * filesystem. For example we might want to move some data in
  2671. * the background. Before the table can be swapped with
  2672. * dm_bind_table, dm_suspend must be called to flush any in
  2673. * flight bios and ensure that any further io gets deferred.
  2674. */
  2675. /*
  2676. * Suspend mechanism in request-based dm.
  2677. *
  2678. * 1. Flush all I/Os by lock_fs() if needed.
  2679. * 2. Stop dispatching any I/O by stopping the request_queue.
  2680. * 3. Wait for all in-flight I/Os to be completed or requeued.
  2681. *
  2682. * To abort suspend, start the request_queue.
  2683. */
  2684. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  2685. {
  2686. struct dm_table *map = NULL;
  2687. int r = 0;
  2688. retry:
  2689. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  2690. if (dm_suspended_md(md)) {
  2691. r = -EINVAL;
  2692. goto out_unlock;
  2693. }
  2694. if (dm_suspended_internally_md(md)) {
  2695. /* already internally suspended, wait for internal resume */
  2696. mutex_unlock(&md->suspend_lock);
  2697. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  2698. if (r)
  2699. return r;
  2700. goto retry;
  2701. }
  2702. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2703. r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
  2704. if (r)
  2705. goto out_unlock;
  2706. dm_table_postsuspend_targets(map);
  2707. out_unlock:
  2708. mutex_unlock(&md->suspend_lock);
  2709. return r;
  2710. }
  2711. static int __dm_resume(struct mapped_device *md, struct dm_table *map)
  2712. {
  2713. if (map) {
  2714. int r = dm_table_resume_targets(map);
  2715. if (r)
  2716. return r;
  2717. }
  2718. dm_queue_flush(md);
  2719. /*
  2720. * Flushing deferred I/Os must be done after targets are resumed
  2721. * so that mapping of targets can work correctly.
  2722. * Request-based dm is queueing the deferred I/Os in its request_queue.
  2723. */
  2724. if (dm_request_based(md))
  2725. start_queue(md->queue);
  2726. unlock_fs(md);
  2727. return 0;
  2728. }
  2729. int dm_resume(struct mapped_device *md)
  2730. {
  2731. int r;
  2732. struct dm_table *map = NULL;
  2733. retry:
  2734. r = -EINVAL;
  2735. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  2736. if (!dm_suspended_md(md))
  2737. goto out;
  2738. if (dm_suspended_internally_md(md)) {
  2739. /* already internally suspended, wait for internal resume */
  2740. mutex_unlock(&md->suspend_lock);
  2741. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  2742. if (r)
  2743. return r;
  2744. goto retry;
  2745. }
  2746. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2747. if (!map || !dm_table_get_size(map))
  2748. goto out;
  2749. r = __dm_resume(md, map);
  2750. if (r)
  2751. goto out;
  2752. clear_bit(DMF_SUSPENDED, &md->flags);
  2753. out:
  2754. mutex_unlock(&md->suspend_lock);
  2755. return r;
  2756. }
  2757. /*
  2758. * Internal suspend/resume works like userspace-driven suspend. It waits
  2759. * until all bios finish and prevents issuing new bios to the target drivers.
  2760. * It may be used only from the kernel.
  2761. */
  2762. static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
  2763. {
  2764. struct dm_table *map = NULL;
  2765. if (md->internal_suspend_count++)
  2766. return; /* nested internal suspend */
  2767. if (dm_suspended_md(md)) {
  2768. set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2769. return; /* nest suspend */
  2770. }
  2771. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2772. /*
  2773. * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
  2774. * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
  2775. * would require changing .presuspend to return an error -- avoid this
  2776. * until there is a need for more elaborate variants of internal suspend.
  2777. */
  2778. (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
  2779. DMF_SUSPENDED_INTERNALLY);
  2780. dm_table_postsuspend_targets(map);
  2781. }
  2782. static void __dm_internal_resume(struct mapped_device *md)
  2783. {
  2784. BUG_ON(!md->internal_suspend_count);
  2785. if (--md->internal_suspend_count)
  2786. return; /* resume from nested internal suspend */
  2787. if (dm_suspended_md(md))
  2788. goto done; /* resume from nested suspend */
  2789. /*
  2790. * NOTE: existing callers don't need to call dm_table_resume_targets
  2791. * (which may fail -- so best to avoid it for now by passing NULL map)
  2792. */
  2793. (void) __dm_resume(md, NULL);
  2794. done:
  2795. clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2796. smp_mb__after_atomic();
  2797. wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
  2798. }
  2799. void dm_internal_suspend_noflush(struct mapped_device *md)
  2800. {
  2801. mutex_lock(&md->suspend_lock);
  2802. __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
  2803. mutex_unlock(&md->suspend_lock);
  2804. }
  2805. EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
  2806. void dm_internal_resume(struct mapped_device *md)
  2807. {
  2808. mutex_lock(&md->suspend_lock);
  2809. __dm_internal_resume(md);
  2810. mutex_unlock(&md->suspend_lock);
  2811. }
  2812. EXPORT_SYMBOL_GPL(dm_internal_resume);
  2813. /*
  2814. * Fast variants of internal suspend/resume hold md->suspend_lock,
  2815. * which prevents interaction with userspace-driven suspend.
  2816. */
  2817. void dm_internal_suspend_fast(struct mapped_device *md)
  2818. {
  2819. mutex_lock(&md->suspend_lock);
  2820. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  2821. return;
  2822. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2823. synchronize_srcu(&md->io_barrier);
  2824. flush_workqueue(md->wq);
  2825. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  2826. }
  2827. EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
  2828. void dm_internal_resume_fast(struct mapped_device *md)
  2829. {
  2830. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  2831. goto done;
  2832. dm_queue_flush(md);
  2833. done:
  2834. mutex_unlock(&md->suspend_lock);
  2835. }
  2836. EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
  2837. /*-----------------------------------------------------------------
  2838. * Event notification.
  2839. *---------------------------------------------------------------*/
  2840. int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
  2841. unsigned cookie)
  2842. {
  2843. char udev_cookie[DM_COOKIE_LENGTH];
  2844. char *envp[] = { udev_cookie, NULL };
  2845. if (!cookie)
  2846. return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
  2847. else {
  2848. snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
  2849. DM_COOKIE_ENV_VAR_NAME, cookie);
  2850. return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
  2851. action, envp);
  2852. }
  2853. }
  2854. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  2855. {
  2856. return atomic_add_return(1, &md->uevent_seq);
  2857. }
  2858. uint32_t dm_get_event_nr(struct mapped_device *md)
  2859. {
  2860. return atomic_read(&md->event_nr);
  2861. }
  2862. int dm_wait_event(struct mapped_device *md, int event_nr)
  2863. {
  2864. return wait_event_interruptible(md->eventq,
  2865. (event_nr != atomic_read(&md->event_nr)));
  2866. }
  2867. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  2868. {
  2869. unsigned long flags;
  2870. spin_lock_irqsave(&md->uevent_lock, flags);
  2871. list_add(elist, &md->uevent_list);
  2872. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2873. }
  2874. /*
  2875. * The gendisk is only valid as long as you have a reference
  2876. * count on 'md'.
  2877. */
  2878. struct gendisk *dm_disk(struct mapped_device *md)
  2879. {
  2880. return md->disk;
  2881. }
  2882. EXPORT_SYMBOL_GPL(dm_disk);
  2883. struct kobject *dm_kobject(struct mapped_device *md)
  2884. {
  2885. return &md->kobj_holder.kobj;
  2886. }
  2887. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  2888. {
  2889. struct mapped_device *md;
  2890. md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
  2891. spin_lock(&_minor_lock);
  2892. if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
  2893. md = NULL;
  2894. goto out;
  2895. }
  2896. dm_get(md);
  2897. out:
  2898. spin_unlock(&_minor_lock);
  2899. return md;
  2900. }
  2901. int dm_suspended_md(struct mapped_device *md)
  2902. {
  2903. return test_bit(DMF_SUSPENDED, &md->flags);
  2904. }
  2905. int dm_suspended_internally_md(struct mapped_device *md)
  2906. {
  2907. return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2908. }
  2909. int dm_test_deferred_remove_flag(struct mapped_device *md)
  2910. {
  2911. return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
  2912. }
  2913. int dm_suspended(struct dm_target *ti)
  2914. {
  2915. return dm_suspended_md(dm_table_get_md(ti->table));
  2916. }
  2917. EXPORT_SYMBOL_GPL(dm_suspended);
  2918. int dm_noflush_suspending(struct dm_target *ti)
  2919. {
  2920. return __noflush_suspending(dm_table_get_md(ti->table));
  2921. }
  2922. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  2923. struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, unsigned type,
  2924. unsigned integrity, unsigned per_bio_data_size)
  2925. {
  2926. struct dm_md_mempools *pools = kzalloc(sizeof(*pools), GFP_KERNEL);
  2927. struct kmem_cache *cachep = NULL;
  2928. unsigned int pool_size = 0;
  2929. unsigned int front_pad;
  2930. if (!pools)
  2931. return NULL;
  2932. type = filter_md_type(type, md);
  2933. switch (type) {
  2934. case DM_TYPE_BIO_BASED:
  2935. cachep = _io_cache;
  2936. pool_size = dm_get_reserved_bio_based_ios();
  2937. front_pad = roundup(per_bio_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
  2938. break;
  2939. case DM_TYPE_REQUEST_BASED:
  2940. cachep = _rq_tio_cache;
  2941. pool_size = dm_get_reserved_rq_based_ios();
  2942. pools->rq_pool = mempool_create_slab_pool(pool_size, _rq_cache);
  2943. if (!pools->rq_pool)
  2944. goto out;
  2945. /* fall through to setup remaining rq-based pools */
  2946. case DM_TYPE_MQ_REQUEST_BASED:
  2947. if (!pool_size)
  2948. pool_size = dm_get_reserved_rq_based_ios();
  2949. front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
  2950. /* per_bio_data_size is not used. See __bind_mempools(). */
  2951. WARN_ON(per_bio_data_size != 0);
  2952. break;
  2953. default:
  2954. BUG();
  2955. }
  2956. if (cachep) {
  2957. pools->io_pool = mempool_create_slab_pool(pool_size, cachep);
  2958. if (!pools->io_pool)
  2959. goto out;
  2960. }
  2961. pools->bs = bioset_create_nobvec(pool_size, front_pad);
  2962. if (!pools->bs)
  2963. goto out;
  2964. if (integrity && bioset_integrity_create(pools->bs, pool_size))
  2965. goto out;
  2966. return pools;
  2967. out:
  2968. dm_free_md_mempools(pools);
  2969. return NULL;
  2970. }
  2971. void dm_free_md_mempools(struct dm_md_mempools *pools)
  2972. {
  2973. if (!pools)
  2974. return;
  2975. mempool_destroy(pools->io_pool);
  2976. mempool_destroy(pools->rq_pool);
  2977. if (pools->bs)
  2978. bioset_free(pools->bs);
  2979. kfree(pools);
  2980. }
  2981. static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
  2982. u32 flags)
  2983. {
  2984. struct mapped_device *md = bdev->bd_disk->private_data;
  2985. const struct pr_ops *ops;
  2986. struct dm_target *tgt;
  2987. fmode_t mode;
  2988. int srcu_idx, r;
  2989. r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
  2990. if (r < 0)
  2991. return r;
  2992. ops = bdev->bd_disk->fops->pr_ops;
  2993. if (ops && ops->pr_register)
  2994. r = ops->pr_register(bdev, old_key, new_key, flags);
  2995. else
  2996. r = -EOPNOTSUPP;
  2997. dm_put_live_table(md, srcu_idx);
  2998. return r;
  2999. }
  3000. static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
  3001. u32 flags)
  3002. {
  3003. struct mapped_device *md = bdev->bd_disk->private_data;
  3004. const struct pr_ops *ops;
  3005. struct dm_target *tgt;
  3006. fmode_t mode;
  3007. int srcu_idx, r;
  3008. r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
  3009. if (r < 0)
  3010. return r;
  3011. ops = bdev->bd_disk->fops->pr_ops;
  3012. if (ops && ops->pr_reserve)
  3013. r = ops->pr_reserve(bdev, key, type, flags);
  3014. else
  3015. r = -EOPNOTSUPP;
  3016. dm_put_live_table(md, srcu_idx);
  3017. return r;
  3018. }
  3019. static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  3020. {
  3021. struct mapped_device *md = bdev->bd_disk->private_data;
  3022. const struct pr_ops *ops;
  3023. struct dm_target *tgt;
  3024. fmode_t mode;
  3025. int srcu_idx, r;
  3026. r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
  3027. if (r < 0)
  3028. return r;
  3029. ops = bdev->bd_disk->fops->pr_ops;
  3030. if (ops && ops->pr_release)
  3031. r = ops->pr_release(bdev, key, type);
  3032. else
  3033. r = -EOPNOTSUPP;
  3034. dm_put_live_table(md, srcu_idx);
  3035. return r;
  3036. }
  3037. static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
  3038. enum pr_type type, bool abort)
  3039. {
  3040. struct mapped_device *md = bdev->bd_disk->private_data;
  3041. const struct pr_ops *ops;
  3042. struct dm_target *tgt;
  3043. fmode_t mode;
  3044. int srcu_idx, r;
  3045. r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
  3046. if (r < 0)
  3047. return r;
  3048. ops = bdev->bd_disk->fops->pr_ops;
  3049. if (ops && ops->pr_preempt)
  3050. r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
  3051. else
  3052. r = -EOPNOTSUPP;
  3053. dm_put_live_table(md, srcu_idx);
  3054. return r;
  3055. }
  3056. static int dm_pr_clear(struct block_device *bdev, u64 key)
  3057. {
  3058. struct mapped_device *md = bdev->bd_disk->private_data;
  3059. const struct pr_ops *ops;
  3060. struct dm_target *tgt;
  3061. fmode_t mode;
  3062. int srcu_idx, r;
  3063. r = dm_get_live_table_for_ioctl(md, &tgt, &bdev, &mode, &srcu_idx);
  3064. if (r < 0)
  3065. return r;
  3066. ops = bdev->bd_disk->fops->pr_ops;
  3067. if (ops && ops->pr_clear)
  3068. r = ops->pr_clear(bdev, key);
  3069. else
  3070. r = -EOPNOTSUPP;
  3071. dm_put_live_table(md, srcu_idx);
  3072. return r;
  3073. }
  3074. static const struct pr_ops dm_pr_ops = {
  3075. .pr_register = dm_pr_register,
  3076. .pr_reserve = dm_pr_reserve,
  3077. .pr_release = dm_pr_release,
  3078. .pr_preempt = dm_pr_preempt,
  3079. .pr_clear = dm_pr_clear,
  3080. };
  3081. static const struct block_device_operations dm_blk_dops = {
  3082. .open = dm_blk_open,
  3083. .release = dm_blk_close,
  3084. .ioctl = dm_blk_ioctl,
  3085. .getgeo = dm_blk_getgeo,
  3086. .pr_ops = &dm_pr_ops,
  3087. .owner = THIS_MODULE
  3088. };
  3089. /*
  3090. * module hooks
  3091. */
  3092. module_init(dm_init);
  3093. module_exit(dm_exit);
  3094. module_param(major, uint, 0);
  3095. MODULE_PARM_DESC(major, "The major number of the device mapper");
  3096. module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
  3097. MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
  3098. module_param(reserved_rq_based_ios, uint, S_IRUGO | S_IWUSR);
  3099. MODULE_PARM_DESC(reserved_rq_based_ios, "Reserved IOs in request-based mempools");
  3100. module_param(use_blk_mq, bool, S_IRUGO | S_IWUSR);
  3101. MODULE_PARM_DESC(use_blk_mq, "Use block multiqueue for request-based DM devices");
  3102. MODULE_DESCRIPTION(DM_NAME " driver");
  3103. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  3104. MODULE_LICENSE("GPL");