dm.c 85 KB

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