dm.c 73 KB

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