dm.c 82 KB

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