dm.c 87 KB

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