dm.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153
  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. {
  835. struct dm_table *map;
  836. struct dm_target *ti;
  837. map = dm_get_live_table(md, srcu_idx);
  838. if (!map)
  839. return NULL;
  840. ti = dm_table_find_target(map, sector);
  841. if (!dm_target_is_valid(ti))
  842. return NULL;
  843. return ti;
  844. }
  845. static long dm_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  846. long nr_pages, void **kaddr, pfn_t *pfn)
  847. {
  848. struct mapped_device *md = dax_get_private(dax_dev);
  849. sector_t sector = pgoff * PAGE_SECTORS;
  850. struct dm_target *ti;
  851. long len, ret = -EIO;
  852. int srcu_idx;
  853. ti = dm_dax_get_live_target(md, sector, &srcu_idx);
  854. if (!ti)
  855. goto out;
  856. if (!ti->type->direct_access)
  857. goto out;
  858. len = max_io_len(sector, ti) / PAGE_SECTORS;
  859. if (len < 1)
  860. goto out;
  861. nr_pages = min(len, nr_pages);
  862. if (ti->type->direct_access)
  863. ret = ti->type->direct_access(ti, pgoff, nr_pages, kaddr, pfn);
  864. out:
  865. dm_put_live_table(md, srcu_idx);
  866. return ret;
  867. }
  868. static size_t dm_dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
  869. void *addr, size_t bytes, struct iov_iter *i)
  870. {
  871. struct mapped_device *md = dax_get_private(dax_dev);
  872. sector_t sector = pgoff * PAGE_SECTORS;
  873. struct dm_target *ti;
  874. long ret = 0;
  875. int srcu_idx;
  876. ti = dm_dax_get_live_target(md, sector, &srcu_idx);
  877. if (!ti)
  878. goto out;
  879. if (!ti->type->dax_copy_from_iter) {
  880. ret = copy_from_iter(addr, bytes, i);
  881. goto out;
  882. }
  883. ret = ti->type->dax_copy_from_iter(ti, pgoff, addr, bytes, i);
  884. out:
  885. dm_put_live_table(md, srcu_idx);
  886. return ret;
  887. }
  888. /*
  889. * A target may call dm_accept_partial_bio only from the map routine. It is
  890. * allowed for all bio types except REQ_PREFLUSH and REQ_OP_ZONE_RESET.
  891. *
  892. * dm_accept_partial_bio informs the dm that the target only wants to process
  893. * additional n_sectors sectors of the bio and the rest of the data should be
  894. * sent in a next bio.
  895. *
  896. * A diagram that explains the arithmetics:
  897. * +--------------------+---------------+-------+
  898. * | 1 | 2 | 3 |
  899. * +--------------------+---------------+-------+
  900. *
  901. * <-------------- *tio->len_ptr --------------->
  902. * <------- bi_size ------->
  903. * <-- n_sectors -->
  904. *
  905. * Region 1 was already iterated over with bio_advance or similar function.
  906. * (it may be empty if the target doesn't use bio_advance)
  907. * Region 2 is the remaining bio size that the target wants to process.
  908. * (it may be empty if region 1 is non-empty, although there is no reason
  909. * to make it empty)
  910. * The target requires that region 3 is to be sent in the next bio.
  911. *
  912. * If the target wants to receive multiple copies of the bio (via num_*bios, etc),
  913. * the partially processed part (the sum of regions 1+2) must be the same for all
  914. * copies of the bio.
  915. */
  916. void dm_accept_partial_bio(struct bio *bio, unsigned n_sectors)
  917. {
  918. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  919. unsigned bi_size = bio->bi_iter.bi_size >> SECTOR_SHIFT;
  920. BUG_ON(bio->bi_opf & REQ_PREFLUSH);
  921. BUG_ON(bi_size > *tio->len_ptr);
  922. BUG_ON(n_sectors > bi_size);
  923. *tio->len_ptr -= bi_size - n_sectors;
  924. bio->bi_iter.bi_size = n_sectors << SECTOR_SHIFT;
  925. }
  926. EXPORT_SYMBOL_GPL(dm_accept_partial_bio);
  927. /*
  928. * The zone descriptors obtained with a zone report indicate
  929. * zone positions within the target device. The zone descriptors
  930. * must be remapped to match their position within the dm device.
  931. * A target may call dm_remap_zone_report after completion of a
  932. * REQ_OP_ZONE_REPORT bio to remap the zone descriptors obtained
  933. * from the target device mapping to the dm device.
  934. */
  935. void dm_remap_zone_report(struct dm_target *ti, struct bio *bio, sector_t start)
  936. {
  937. #ifdef CONFIG_BLK_DEV_ZONED
  938. struct dm_target_io *tio = container_of(bio, struct dm_target_io, clone);
  939. struct bio *report_bio = tio->io->orig_bio;
  940. struct blk_zone_report_hdr *hdr = NULL;
  941. struct blk_zone *zone;
  942. unsigned int nr_rep = 0;
  943. unsigned int ofst;
  944. struct bio_vec bvec;
  945. struct bvec_iter iter;
  946. void *addr;
  947. if (bio->bi_status)
  948. return;
  949. /*
  950. * Remap the start sector of the reported zones. For sequential zones,
  951. * also remap the write pointer position.
  952. */
  953. bio_for_each_segment(bvec, report_bio, iter) {
  954. addr = kmap_atomic(bvec.bv_page);
  955. /* Remember the report header in the first page */
  956. if (!hdr) {
  957. hdr = addr;
  958. ofst = sizeof(struct blk_zone_report_hdr);
  959. } else
  960. ofst = 0;
  961. /* Set zones start sector */
  962. while (hdr->nr_zones && ofst < bvec.bv_len) {
  963. zone = addr + ofst;
  964. if (zone->start >= start + ti->len) {
  965. hdr->nr_zones = 0;
  966. break;
  967. }
  968. zone->start = zone->start + ti->begin - start;
  969. if (zone->type != BLK_ZONE_TYPE_CONVENTIONAL) {
  970. if (zone->cond == BLK_ZONE_COND_FULL)
  971. zone->wp = zone->start + zone->len;
  972. else if (zone->cond == BLK_ZONE_COND_EMPTY)
  973. zone->wp = zone->start;
  974. else
  975. zone->wp = zone->wp + ti->begin - start;
  976. }
  977. ofst += sizeof(struct blk_zone);
  978. hdr->nr_zones--;
  979. nr_rep++;
  980. }
  981. if (addr != hdr)
  982. kunmap_atomic(addr);
  983. if (!hdr->nr_zones)
  984. break;
  985. }
  986. if (hdr) {
  987. hdr->nr_zones = nr_rep;
  988. kunmap_atomic(hdr);
  989. }
  990. bio_advance(report_bio, report_bio->bi_iter.bi_size);
  991. #else /* !CONFIG_BLK_DEV_ZONED */
  992. bio->bi_status = BLK_STS_NOTSUPP;
  993. #endif
  994. }
  995. EXPORT_SYMBOL_GPL(dm_remap_zone_report);
  996. static blk_qc_t __map_bio(struct dm_target_io *tio)
  997. {
  998. int r;
  999. sector_t sector;
  1000. struct bio *clone = &tio->clone;
  1001. struct dm_io *io = tio->io;
  1002. struct mapped_device *md = io->md;
  1003. struct dm_target *ti = tio->ti;
  1004. blk_qc_t ret = BLK_QC_T_NONE;
  1005. clone->bi_end_io = clone_endio;
  1006. /*
  1007. * Map the clone. If r == 0 we don't need to do
  1008. * anything, the target has assumed ownership of
  1009. * this io.
  1010. */
  1011. atomic_inc(&io->io_count);
  1012. sector = clone->bi_iter.bi_sector;
  1013. r = ti->type->map(ti, clone);
  1014. switch (r) {
  1015. case DM_MAPIO_SUBMITTED:
  1016. break;
  1017. case DM_MAPIO_REMAPPED:
  1018. /* the bio has been remapped so dispatch it */
  1019. trace_block_bio_remap(clone->bi_disk->queue, clone,
  1020. bio_dev(io->orig_bio), sector);
  1021. if (md->type == DM_TYPE_NVME_BIO_BASED)
  1022. ret = direct_make_request(clone);
  1023. else
  1024. ret = generic_make_request(clone);
  1025. break;
  1026. case DM_MAPIO_KILL:
  1027. free_tio(tio);
  1028. dec_pending(io, BLK_STS_IOERR);
  1029. break;
  1030. case DM_MAPIO_REQUEUE:
  1031. free_tio(tio);
  1032. dec_pending(io, BLK_STS_DM_REQUEUE);
  1033. break;
  1034. default:
  1035. DMWARN("unimplemented target map return value: %d", r);
  1036. BUG();
  1037. }
  1038. return ret;
  1039. }
  1040. static void bio_setup_sector(struct bio *bio, sector_t sector, unsigned len)
  1041. {
  1042. bio->bi_iter.bi_sector = sector;
  1043. bio->bi_iter.bi_size = to_bytes(len);
  1044. }
  1045. /*
  1046. * Creates a bio that consists of range of complete bvecs.
  1047. */
  1048. static int clone_bio(struct dm_target_io *tio, struct bio *bio,
  1049. sector_t sector, unsigned len)
  1050. {
  1051. struct bio *clone = &tio->clone;
  1052. __bio_clone_fast(clone, bio);
  1053. if (unlikely(bio_integrity(bio) != NULL)) {
  1054. int r;
  1055. if (unlikely(!dm_target_has_integrity(tio->ti->type) &&
  1056. !dm_target_passes_integrity(tio->ti->type))) {
  1057. DMWARN("%s: the target %s doesn't support integrity data.",
  1058. dm_device_name(tio->io->md),
  1059. tio->ti->type->name);
  1060. return -EIO;
  1061. }
  1062. r = bio_integrity_clone(clone, bio, GFP_NOIO);
  1063. if (r < 0)
  1064. return r;
  1065. }
  1066. if (bio_op(bio) != REQ_OP_ZONE_REPORT)
  1067. bio_advance(clone, to_bytes(sector - clone->bi_iter.bi_sector));
  1068. clone->bi_iter.bi_size = to_bytes(len);
  1069. if (unlikely(bio_integrity(bio) != NULL))
  1070. bio_integrity_trim(clone);
  1071. return 0;
  1072. }
  1073. static void alloc_multiple_bios(struct bio_list *blist, struct clone_info *ci,
  1074. struct dm_target *ti, unsigned num_bios)
  1075. {
  1076. struct dm_target_io *tio;
  1077. int try;
  1078. if (!num_bios)
  1079. return;
  1080. if (num_bios == 1) {
  1081. tio = alloc_tio(ci, ti, 0, GFP_NOIO);
  1082. bio_list_add(blist, &tio->clone);
  1083. return;
  1084. }
  1085. for (try = 0; try < 2; try++) {
  1086. int bio_nr;
  1087. struct bio *bio;
  1088. if (try)
  1089. mutex_lock(&ci->io->md->table_devices_lock);
  1090. for (bio_nr = 0; bio_nr < num_bios; bio_nr++) {
  1091. tio = alloc_tio(ci, ti, bio_nr, try ? GFP_NOIO : GFP_NOWAIT);
  1092. if (!tio)
  1093. break;
  1094. bio_list_add(blist, &tio->clone);
  1095. }
  1096. if (try)
  1097. mutex_unlock(&ci->io->md->table_devices_lock);
  1098. if (bio_nr == num_bios)
  1099. return;
  1100. while ((bio = bio_list_pop(blist))) {
  1101. tio = container_of(bio, struct dm_target_io, clone);
  1102. free_tio(tio);
  1103. }
  1104. }
  1105. }
  1106. static blk_qc_t __clone_and_map_simple_bio(struct clone_info *ci,
  1107. struct dm_target_io *tio, unsigned *len)
  1108. {
  1109. struct bio *clone = &tio->clone;
  1110. tio->len_ptr = len;
  1111. __bio_clone_fast(clone, ci->bio);
  1112. if (len)
  1113. bio_setup_sector(clone, ci->sector, *len);
  1114. return __map_bio(tio);
  1115. }
  1116. static void __send_duplicate_bios(struct clone_info *ci, struct dm_target *ti,
  1117. unsigned num_bios, unsigned *len)
  1118. {
  1119. struct bio_list blist = BIO_EMPTY_LIST;
  1120. struct bio *bio;
  1121. struct dm_target_io *tio;
  1122. alloc_multiple_bios(&blist, ci, ti, num_bios);
  1123. while ((bio = bio_list_pop(&blist))) {
  1124. tio = container_of(bio, struct dm_target_io, clone);
  1125. (void) __clone_and_map_simple_bio(ci, tio, len);
  1126. }
  1127. }
  1128. static int __send_empty_flush(struct clone_info *ci)
  1129. {
  1130. unsigned target_nr = 0;
  1131. struct dm_target *ti;
  1132. BUG_ON(bio_has_data(ci->bio));
  1133. while ((ti = dm_table_get_target(ci->map, target_nr++)))
  1134. __send_duplicate_bios(ci, ti, ti->num_flush_bios, NULL);
  1135. return 0;
  1136. }
  1137. static int __clone_and_map_data_bio(struct clone_info *ci, struct dm_target *ti,
  1138. sector_t sector, unsigned *len)
  1139. {
  1140. struct bio *bio = ci->bio;
  1141. struct dm_target_io *tio;
  1142. int r;
  1143. tio = alloc_tio(ci, ti, 0, GFP_NOIO);
  1144. tio->len_ptr = len;
  1145. r = clone_bio(tio, bio, sector, *len);
  1146. if (r < 0) {
  1147. free_tio(tio);
  1148. return r;
  1149. }
  1150. (void) __map_bio(tio);
  1151. return 0;
  1152. }
  1153. typedef unsigned (*get_num_bios_fn)(struct dm_target *ti);
  1154. static unsigned get_num_discard_bios(struct dm_target *ti)
  1155. {
  1156. return ti->num_discard_bios;
  1157. }
  1158. static unsigned get_num_secure_erase_bios(struct dm_target *ti)
  1159. {
  1160. return ti->num_secure_erase_bios;
  1161. }
  1162. static unsigned get_num_write_same_bios(struct dm_target *ti)
  1163. {
  1164. return ti->num_write_same_bios;
  1165. }
  1166. static unsigned get_num_write_zeroes_bios(struct dm_target *ti)
  1167. {
  1168. return ti->num_write_zeroes_bios;
  1169. }
  1170. typedef bool (*is_split_required_fn)(struct dm_target *ti);
  1171. static bool is_split_required_for_discard(struct dm_target *ti)
  1172. {
  1173. return ti->split_discard_bios;
  1174. }
  1175. static int __send_changing_extent_only(struct clone_info *ci, struct dm_target *ti,
  1176. get_num_bios_fn get_num_bios,
  1177. is_split_required_fn is_split_required)
  1178. {
  1179. unsigned len;
  1180. unsigned num_bios;
  1181. /*
  1182. * Even though the device advertised support for this type of
  1183. * request, that does not mean every target supports it, and
  1184. * reconfiguration might also have changed that since the
  1185. * check was performed.
  1186. */
  1187. num_bios = get_num_bios ? get_num_bios(ti) : 0;
  1188. if (!num_bios)
  1189. return -EOPNOTSUPP;
  1190. if (is_split_required && !is_split_required(ti))
  1191. len = min((sector_t)ci->sector_count, max_io_len_target_boundary(ci->sector, ti));
  1192. else
  1193. len = min((sector_t)ci->sector_count, max_io_len(ci->sector, ti));
  1194. __send_duplicate_bios(ci, ti, num_bios, &len);
  1195. ci->sector += len;
  1196. ci->sector_count -= len;
  1197. return 0;
  1198. }
  1199. static int __send_discard(struct clone_info *ci, struct dm_target *ti)
  1200. {
  1201. return __send_changing_extent_only(ci, ti, get_num_discard_bios,
  1202. is_split_required_for_discard);
  1203. }
  1204. static int __send_secure_erase(struct clone_info *ci, struct dm_target *ti)
  1205. {
  1206. return __send_changing_extent_only(ci, ti, get_num_secure_erase_bios, NULL);
  1207. }
  1208. static int __send_write_same(struct clone_info *ci, struct dm_target *ti)
  1209. {
  1210. return __send_changing_extent_only(ci, ti, get_num_write_same_bios, NULL);
  1211. }
  1212. static int __send_write_zeroes(struct clone_info *ci, struct dm_target *ti)
  1213. {
  1214. return __send_changing_extent_only(ci, ti, get_num_write_zeroes_bios, NULL);
  1215. }
  1216. static bool __process_abnormal_io(struct clone_info *ci, struct dm_target *ti,
  1217. int *result)
  1218. {
  1219. struct bio *bio = ci->bio;
  1220. if (bio_op(bio) == REQ_OP_DISCARD)
  1221. *result = __send_discard(ci, ti);
  1222. else if (bio_op(bio) == REQ_OP_SECURE_ERASE)
  1223. *result = __send_secure_erase(ci, ti);
  1224. else if (bio_op(bio) == REQ_OP_WRITE_SAME)
  1225. *result = __send_write_same(ci, ti);
  1226. else if (bio_op(bio) == REQ_OP_WRITE_ZEROES)
  1227. *result = __send_write_zeroes(ci, ti);
  1228. else
  1229. return false;
  1230. return true;
  1231. }
  1232. /*
  1233. * Select the correct strategy for processing a non-flush bio.
  1234. */
  1235. static int __split_and_process_non_flush(struct clone_info *ci)
  1236. {
  1237. struct bio *bio = ci->bio;
  1238. struct dm_target *ti;
  1239. unsigned len;
  1240. int r;
  1241. ti = dm_table_find_target(ci->map, ci->sector);
  1242. if (!dm_target_is_valid(ti))
  1243. return -EIO;
  1244. if (unlikely(__process_abnormal_io(ci, ti, &r)))
  1245. return r;
  1246. if (bio_op(bio) == REQ_OP_ZONE_REPORT)
  1247. len = ci->sector_count;
  1248. else
  1249. len = min_t(sector_t, max_io_len(ci->sector, ti),
  1250. ci->sector_count);
  1251. r = __clone_and_map_data_bio(ci, ti, ci->sector, &len);
  1252. if (r < 0)
  1253. return r;
  1254. ci->sector += len;
  1255. ci->sector_count -= len;
  1256. return 0;
  1257. }
  1258. static void init_clone_info(struct clone_info *ci, struct mapped_device *md,
  1259. struct dm_table *map, struct bio *bio)
  1260. {
  1261. ci->map = map;
  1262. ci->io = alloc_io(md, bio);
  1263. ci->sector = bio->bi_iter.bi_sector;
  1264. }
  1265. /*
  1266. * Entry point to split a bio into clones and submit them to the targets.
  1267. */
  1268. static blk_qc_t __split_and_process_bio(struct mapped_device *md,
  1269. struct dm_table *map, struct bio *bio)
  1270. {
  1271. struct clone_info ci;
  1272. blk_qc_t ret = BLK_QC_T_NONE;
  1273. int error = 0;
  1274. if (unlikely(!map)) {
  1275. bio_io_error(bio);
  1276. return ret;
  1277. }
  1278. init_clone_info(&ci, md, map, bio);
  1279. if (bio->bi_opf & REQ_PREFLUSH) {
  1280. ci.bio = &ci.io->md->flush_bio;
  1281. ci.sector_count = 0;
  1282. error = __send_empty_flush(&ci);
  1283. /* dec_pending submits any data associated with flush */
  1284. } else if (bio_op(bio) == REQ_OP_ZONE_RESET) {
  1285. ci.bio = bio;
  1286. ci.sector_count = 0;
  1287. error = __split_and_process_non_flush(&ci);
  1288. } else {
  1289. ci.bio = bio;
  1290. ci.sector_count = bio_sectors(bio);
  1291. while (ci.sector_count && !error) {
  1292. error = __split_and_process_non_flush(&ci);
  1293. if (current->bio_list && ci.sector_count && !error) {
  1294. /*
  1295. * Remainder must be passed to generic_make_request()
  1296. * so that it gets handled *after* bios already submitted
  1297. * have been completely processed.
  1298. * We take a clone of the original to store in
  1299. * ci.io->orig_bio to be used by end_io_acct() and
  1300. * for dec_pending to use for completion handling.
  1301. * As this path is not used for REQ_OP_ZONE_REPORT,
  1302. * the usage of io->orig_bio in dm_remap_zone_report()
  1303. * won't be affected by this reassignment.
  1304. */
  1305. struct bio *b = bio_clone_bioset(bio, GFP_NOIO,
  1306. md->queue->bio_split);
  1307. ci.io->orig_bio = b;
  1308. bio_advance(bio, (bio_sectors(bio) - ci.sector_count) << 9);
  1309. bio_chain(b, bio);
  1310. ret = generic_make_request(bio);
  1311. break;
  1312. }
  1313. }
  1314. }
  1315. /* drop the extra reference count */
  1316. dec_pending(ci.io, errno_to_blk_status(error));
  1317. return ret;
  1318. }
  1319. /*
  1320. * Optimized variant of __split_and_process_bio that leverages the
  1321. * fact that targets that use it do _not_ have a need to split bios.
  1322. */
  1323. static blk_qc_t __process_bio(struct mapped_device *md,
  1324. struct dm_table *map, struct bio *bio)
  1325. {
  1326. struct clone_info ci;
  1327. blk_qc_t ret = BLK_QC_T_NONE;
  1328. int error = 0;
  1329. if (unlikely(!map)) {
  1330. bio_io_error(bio);
  1331. return ret;
  1332. }
  1333. init_clone_info(&ci, md, map, bio);
  1334. if (bio->bi_opf & REQ_PREFLUSH) {
  1335. ci.bio = &ci.io->md->flush_bio;
  1336. ci.sector_count = 0;
  1337. error = __send_empty_flush(&ci);
  1338. /* dec_pending submits any data associated with flush */
  1339. } else {
  1340. struct dm_target *ti = md->immutable_target;
  1341. struct dm_target_io *tio;
  1342. /*
  1343. * Defend against IO still getting in during teardown
  1344. * - as was seen for a time with nvme-fcloop
  1345. */
  1346. if (unlikely(WARN_ON_ONCE(!ti || !dm_target_is_valid(ti)))) {
  1347. error = -EIO;
  1348. goto out;
  1349. }
  1350. ci.bio = bio;
  1351. ci.sector_count = bio_sectors(bio);
  1352. if (unlikely(__process_abnormal_io(&ci, ti, &error)))
  1353. goto out;
  1354. tio = alloc_tio(&ci, ti, 0, GFP_NOIO);
  1355. ret = __clone_and_map_simple_bio(&ci, tio, NULL);
  1356. }
  1357. out:
  1358. /* drop the extra reference count */
  1359. dec_pending(ci.io, errno_to_blk_status(error));
  1360. return ret;
  1361. }
  1362. typedef blk_qc_t (process_bio_fn)(struct mapped_device *, struct dm_table *, struct bio *);
  1363. static blk_qc_t __dm_make_request(struct request_queue *q, struct bio *bio,
  1364. process_bio_fn process_bio)
  1365. {
  1366. struct mapped_device *md = q->queuedata;
  1367. blk_qc_t ret = BLK_QC_T_NONE;
  1368. int srcu_idx;
  1369. struct dm_table *map;
  1370. map = dm_get_live_table(md, &srcu_idx);
  1371. /* if we're suspended, we have to queue this io for later */
  1372. if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags))) {
  1373. dm_put_live_table(md, srcu_idx);
  1374. if (!(bio->bi_opf & REQ_RAHEAD))
  1375. queue_io(md, bio);
  1376. else
  1377. bio_io_error(bio);
  1378. return ret;
  1379. }
  1380. ret = process_bio(md, map, bio);
  1381. dm_put_live_table(md, srcu_idx);
  1382. return ret;
  1383. }
  1384. /*
  1385. * The request function that remaps the bio to one target and
  1386. * splits off any remainder.
  1387. */
  1388. static blk_qc_t dm_make_request(struct request_queue *q, struct bio *bio)
  1389. {
  1390. return __dm_make_request(q, bio, __split_and_process_bio);
  1391. }
  1392. static blk_qc_t dm_make_request_nvme(struct request_queue *q, struct bio *bio)
  1393. {
  1394. return __dm_make_request(q, bio, __process_bio);
  1395. }
  1396. static int dm_any_congested(void *congested_data, int bdi_bits)
  1397. {
  1398. int r = bdi_bits;
  1399. struct mapped_device *md = congested_data;
  1400. struct dm_table *map;
  1401. if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1402. if (dm_request_based(md)) {
  1403. /*
  1404. * With request-based DM we only need to check the
  1405. * top-level queue for congestion.
  1406. */
  1407. r = md->queue->backing_dev_info->wb.state & bdi_bits;
  1408. } else {
  1409. map = dm_get_live_table_fast(md);
  1410. if (map)
  1411. r = dm_table_any_congested(map, bdi_bits);
  1412. dm_put_live_table_fast(md);
  1413. }
  1414. }
  1415. return r;
  1416. }
  1417. /*-----------------------------------------------------------------
  1418. * An IDR is used to keep track of allocated minor numbers.
  1419. *---------------------------------------------------------------*/
  1420. static void free_minor(int minor)
  1421. {
  1422. spin_lock(&_minor_lock);
  1423. idr_remove(&_minor_idr, minor);
  1424. spin_unlock(&_minor_lock);
  1425. }
  1426. /*
  1427. * See if the device with a specific minor # is free.
  1428. */
  1429. static int specific_minor(int minor)
  1430. {
  1431. int r;
  1432. if (minor >= (1 << MINORBITS))
  1433. return -EINVAL;
  1434. idr_preload(GFP_KERNEL);
  1435. spin_lock(&_minor_lock);
  1436. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, minor, minor + 1, GFP_NOWAIT);
  1437. spin_unlock(&_minor_lock);
  1438. idr_preload_end();
  1439. if (r < 0)
  1440. return r == -ENOSPC ? -EBUSY : r;
  1441. return 0;
  1442. }
  1443. static int next_free_minor(int *minor)
  1444. {
  1445. int r;
  1446. idr_preload(GFP_KERNEL);
  1447. spin_lock(&_minor_lock);
  1448. r = idr_alloc(&_minor_idr, MINOR_ALLOCED, 0, 1 << MINORBITS, GFP_NOWAIT);
  1449. spin_unlock(&_minor_lock);
  1450. idr_preload_end();
  1451. if (r < 0)
  1452. return r;
  1453. *minor = r;
  1454. return 0;
  1455. }
  1456. static const struct block_device_operations dm_blk_dops;
  1457. static const struct dax_operations dm_dax_ops;
  1458. static void dm_wq_work(struct work_struct *work);
  1459. static void dm_init_normal_md_queue(struct mapped_device *md)
  1460. {
  1461. md->use_blk_mq = false;
  1462. /*
  1463. * Initialize aspects of queue that aren't relevant for blk-mq
  1464. */
  1465. md->queue->backing_dev_info->congested_fn = dm_any_congested;
  1466. }
  1467. static void cleanup_mapped_device(struct mapped_device *md)
  1468. {
  1469. if (md->wq)
  1470. destroy_workqueue(md->wq);
  1471. if (md->kworker_task)
  1472. kthread_stop(md->kworker_task);
  1473. if (md->bs)
  1474. bioset_free(md->bs);
  1475. if (md->io_bs)
  1476. bioset_free(md->io_bs);
  1477. if (md->dax_dev) {
  1478. kill_dax(md->dax_dev);
  1479. put_dax(md->dax_dev);
  1480. md->dax_dev = NULL;
  1481. }
  1482. if (md->disk) {
  1483. spin_lock(&_minor_lock);
  1484. md->disk->private_data = NULL;
  1485. spin_unlock(&_minor_lock);
  1486. del_gendisk(md->disk);
  1487. put_disk(md->disk);
  1488. }
  1489. if (md->queue)
  1490. blk_cleanup_queue(md->queue);
  1491. cleanup_srcu_struct(&md->io_barrier);
  1492. if (md->bdev) {
  1493. bdput(md->bdev);
  1494. md->bdev = NULL;
  1495. }
  1496. mutex_destroy(&md->suspend_lock);
  1497. mutex_destroy(&md->type_lock);
  1498. mutex_destroy(&md->table_devices_lock);
  1499. dm_mq_cleanup_mapped_device(md);
  1500. }
  1501. /*
  1502. * Allocate and initialise a blank device with a given minor.
  1503. */
  1504. static struct mapped_device *alloc_dev(int minor)
  1505. {
  1506. int r, numa_node_id = dm_get_numa_node();
  1507. struct dax_device *dax_dev;
  1508. struct mapped_device *md;
  1509. void *old_md;
  1510. md = kvzalloc_node(sizeof(*md), GFP_KERNEL, numa_node_id);
  1511. if (!md) {
  1512. DMWARN("unable to allocate device, out of memory.");
  1513. return NULL;
  1514. }
  1515. if (!try_module_get(THIS_MODULE))
  1516. goto bad_module_get;
  1517. /* get a minor number for the dev */
  1518. if (minor == DM_ANY_MINOR)
  1519. r = next_free_minor(&minor);
  1520. else
  1521. r = specific_minor(minor);
  1522. if (r < 0)
  1523. goto bad_minor;
  1524. r = init_srcu_struct(&md->io_barrier);
  1525. if (r < 0)
  1526. goto bad_io_barrier;
  1527. md->numa_node_id = numa_node_id;
  1528. md->use_blk_mq = dm_use_blk_mq_default();
  1529. md->init_tio_pdu = false;
  1530. md->type = DM_TYPE_NONE;
  1531. mutex_init(&md->suspend_lock);
  1532. mutex_init(&md->type_lock);
  1533. mutex_init(&md->table_devices_lock);
  1534. spin_lock_init(&md->deferred_lock);
  1535. atomic_set(&md->holders, 1);
  1536. atomic_set(&md->open_count, 0);
  1537. atomic_set(&md->event_nr, 0);
  1538. atomic_set(&md->uevent_seq, 0);
  1539. INIT_LIST_HEAD(&md->uevent_list);
  1540. INIT_LIST_HEAD(&md->table_devices);
  1541. spin_lock_init(&md->uevent_lock);
  1542. md->queue = blk_alloc_queue_node(GFP_KERNEL, numa_node_id, NULL);
  1543. if (!md->queue)
  1544. goto bad;
  1545. md->queue->queuedata = md;
  1546. md->queue->backing_dev_info->congested_data = md;
  1547. md->disk = alloc_disk_node(1, md->numa_node_id);
  1548. if (!md->disk)
  1549. goto bad;
  1550. atomic_set(&md->pending[0], 0);
  1551. atomic_set(&md->pending[1], 0);
  1552. init_waitqueue_head(&md->wait);
  1553. INIT_WORK(&md->work, dm_wq_work);
  1554. init_waitqueue_head(&md->eventq);
  1555. init_completion(&md->kobj_holder.completion);
  1556. md->kworker_task = NULL;
  1557. md->disk->major = _major;
  1558. md->disk->first_minor = minor;
  1559. md->disk->fops = &dm_blk_dops;
  1560. md->disk->queue = md->queue;
  1561. md->disk->private_data = md;
  1562. sprintf(md->disk->disk_name, "dm-%d", minor);
  1563. dax_dev = alloc_dax(md, md->disk->disk_name, &dm_dax_ops);
  1564. if (!dax_dev)
  1565. goto bad;
  1566. md->dax_dev = dax_dev;
  1567. add_disk_no_queue_reg(md->disk);
  1568. format_dev_t(md->name, MKDEV(_major, minor));
  1569. md->wq = alloc_workqueue("kdmflush", WQ_MEM_RECLAIM, 0);
  1570. if (!md->wq)
  1571. goto bad;
  1572. md->bdev = bdget_disk(md->disk, 0);
  1573. if (!md->bdev)
  1574. goto bad;
  1575. bio_init(&md->flush_bio, NULL, 0);
  1576. bio_set_dev(&md->flush_bio, md->bdev);
  1577. md->flush_bio.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH | REQ_SYNC;
  1578. dm_stats_init(&md->stats);
  1579. /* Populate the mapping, nobody knows we exist yet */
  1580. spin_lock(&_minor_lock);
  1581. old_md = idr_replace(&_minor_idr, md, minor);
  1582. spin_unlock(&_minor_lock);
  1583. BUG_ON(old_md != MINOR_ALLOCED);
  1584. return md;
  1585. bad:
  1586. cleanup_mapped_device(md);
  1587. bad_io_barrier:
  1588. free_minor(minor);
  1589. bad_minor:
  1590. module_put(THIS_MODULE);
  1591. bad_module_get:
  1592. kvfree(md);
  1593. return NULL;
  1594. }
  1595. static void unlock_fs(struct mapped_device *md);
  1596. static void free_dev(struct mapped_device *md)
  1597. {
  1598. int minor = MINOR(disk_devt(md->disk));
  1599. unlock_fs(md);
  1600. cleanup_mapped_device(md);
  1601. free_table_devices(&md->table_devices);
  1602. dm_stats_cleanup(&md->stats);
  1603. free_minor(minor);
  1604. module_put(THIS_MODULE);
  1605. kvfree(md);
  1606. }
  1607. static void __bind_mempools(struct mapped_device *md, struct dm_table *t)
  1608. {
  1609. struct dm_md_mempools *p = dm_table_get_md_mempools(t);
  1610. if (dm_table_bio_based(t)) {
  1611. /*
  1612. * The md may already have mempools that need changing.
  1613. * If so, reload bioset because front_pad may have changed
  1614. * because a different table was loaded.
  1615. */
  1616. if (md->bs) {
  1617. bioset_free(md->bs);
  1618. md->bs = NULL;
  1619. }
  1620. if (md->io_bs) {
  1621. bioset_free(md->io_bs);
  1622. md->io_bs = NULL;
  1623. }
  1624. } else if (md->bs) {
  1625. /*
  1626. * There's no need to reload with request-based dm
  1627. * because the size of front_pad doesn't change.
  1628. * Note for future: If you are to reload bioset,
  1629. * prep-ed requests in the queue may refer
  1630. * to bio from the old bioset, so you must walk
  1631. * through the queue to unprep.
  1632. */
  1633. goto out;
  1634. }
  1635. BUG_ON(!p || md->bs || md->io_bs);
  1636. md->bs = p->bs;
  1637. p->bs = NULL;
  1638. md->io_bs = p->io_bs;
  1639. p->io_bs = NULL;
  1640. out:
  1641. /* mempool bind completed, no longer need any mempools in the table */
  1642. dm_table_free_md_mempools(t);
  1643. }
  1644. /*
  1645. * Bind a table to the device.
  1646. */
  1647. static void event_callback(void *context)
  1648. {
  1649. unsigned long flags;
  1650. LIST_HEAD(uevents);
  1651. struct mapped_device *md = (struct mapped_device *) context;
  1652. spin_lock_irqsave(&md->uevent_lock, flags);
  1653. list_splice_init(&md->uevent_list, &uevents);
  1654. spin_unlock_irqrestore(&md->uevent_lock, flags);
  1655. dm_send_uevents(&uevents, &disk_to_dev(md->disk)->kobj);
  1656. atomic_inc(&md->event_nr);
  1657. wake_up(&md->eventq);
  1658. dm_issue_global_event();
  1659. }
  1660. /*
  1661. * Protected by md->suspend_lock obtained by dm_swap_table().
  1662. */
  1663. static void __set_size(struct mapped_device *md, sector_t size)
  1664. {
  1665. lockdep_assert_held(&md->suspend_lock);
  1666. set_capacity(md->disk, size);
  1667. i_size_write(md->bdev->bd_inode, (loff_t)size << SECTOR_SHIFT);
  1668. }
  1669. /*
  1670. * Returns old map, which caller must destroy.
  1671. */
  1672. static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,
  1673. struct queue_limits *limits)
  1674. {
  1675. struct dm_table *old_map;
  1676. struct request_queue *q = md->queue;
  1677. bool request_based = dm_table_request_based(t);
  1678. sector_t size;
  1679. lockdep_assert_held(&md->suspend_lock);
  1680. size = dm_table_get_size(t);
  1681. /*
  1682. * Wipe any geometry if the size of the table changed.
  1683. */
  1684. if (size != dm_get_size(md))
  1685. memset(&md->geometry, 0, sizeof(md->geometry));
  1686. __set_size(md, size);
  1687. dm_table_event_callback(t, event_callback, md);
  1688. /*
  1689. * The queue hasn't been stopped yet, if the old table type wasn't
  1690. * for request-based during suspension. So stop it to prevent
  1691. * I/O mapping before resume.
  1692. * This must be done before setting the queue restrictions,
  1693. * because request-based dm may be run just after the setting.
  1694. */
  1695. if (request_based)
  1696. dm_stop_queue(q);
  1697. if (request_based || md->type == DM_TYPE_NVME_BIO_BASED) {
  1698. /*
  1699. * Leverage the fact that request-based DM targets and
  1700. * NVMe bio based targets are immutable singletons
  1701. * - used to optimize both dm_request_fn and dm_mq_queue_rq;
  1702. * and __process_bio.
  1703. */
  1704. md->immutable_target = dm_table_get_immutable_target(t);
  1705. }
  1706. __bind_mempools(md, t);
  1707. old_map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  1708. rcu_assign_pointer(md->map, (void *)t);
  1709. md->immutable_target_type = dm_table_get_immutable_target_type(t);
  1710. dm_table_set_restrictions(t, q, limits);
  1711. if (old_map)
  1712. dm_sync_table(md);
  1713. return old_map;
  1714. }
  1715. /*
  1716. * Returns unbound table for the caller to free.
  1717. */
  1718. static struct dm_table *__unbind(struct mapped_device *md)
  1719. {
  1720. struct dm_table *map = rcu_dereference_protected(md->map, 1);
  1721. if (!map)
  1722. return NULL;
  1723. dm_table_event_callback(map, NULL, NULL);
  1724. RCU_INIT_POINTER(md->map, NULL);
  1725. dm_sync_table(md);
  1726. return map;
  1727. }
  1728. /*
  1729. * Constructor for a new device.
  1730. */
  1731. int dm_create(int minor, struct mapped_device **result)
  1732. {
  1733. int r;
  1734. struct mapped_device *md;
  1735. md = alloc_dev(minor);
  1736. if (!md)
  1737. return -ENXIO;
  1738. r = dm_sysfs_init(md);
  1739. if (r) {
  1740. free_dev(md);
  1741. return r;
  1742. }
  1743. *result = md;
  1744. return 0;
  1745. }
  1746. /*
  1747. * Functions to manage md->type.
  1748. * All are required to hold md->type_lock.
  1749. */
  1750. void dm_lock_md_type(struct mapped_device *md)
  1751. {
  1752. mutex_lock(&md->type_lock);
  1753. }
  1754. void dm_unlock_md_type(struct mapped_device *md)
  1755. {
  1756. mutex_unlock(&md->type_lock);
  1757. }
  1758. void dm_set_md_type(struct mapped_device *md, enum dm_queue_mode type)
  1759. {
  1760. BUG_ON(!mutex_is_locked(&md->type_lock));
  1761. md->type = type;
  1762. }
  1763. enum dm_queue_mode dm_get_md_type(struct mapped_device *md)
  1764. {
  1765. return md->type;
  1766. }
  1767. struct target_type *dm_get_immutable_target_type(struct mapped_device *md)
  1768. {
  1769. return md->immutable_target_type;
  1770. }
  1771. /*
  1772. * The queue_limits are only valid as long as you have a reference
  1773. * count on 'md'.
  1774. */
  1775. struct queue_limits *dm_get_queue_limits(struct mapped_device *md)
  1776. {
  1777. BUG_ON(!atomic_read(&md->holders));
  1778. return &md->queue->limits;
  1779. }
  1780. EXPORT_SYMBOL_GPL(dm_get_queue_limits);
  1781. /*
  1782. * Setup the DM device's queue based on md's type
  1783. */
  1784. int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
  1785. {
  1786. int r;
  1787. struct queue_limits limits;
  1788. enum dm_queue_mode type = dm_get_md_type(md);
  1789. switch (type) {
  1790. case DM_TYPE_REQUEST_BASED:
  1791. dm_init_normal_md_queue(md);
  1792. r = dm_old_init_request_queue(md, t);
  1793. if (r) {
  1794. DMERR("Cannot initialize queue for request-based mapped device");
  1795. return r;
  1796. }
  1797. break;
  1798. case DM_TYPE_MQ_REQUEST_BASED:
  1799. r = dm_mq_init_request_queue(md, t);
  1800. if (r) {
  1801. DMERR("Cannot initialize queue for request-based dm-mq mapped device");
  1802. return r;
  1803. }
  1804. break;
  1805. case DM_TYPE_BIO_BASED:
  1806. case DM_TYPE_DAX_BIO_BASED:
  1807. dm_init_normal_md_queue(md);
  1808. blk_queue_make_request(md->queue, dm_make_request);
  1809. break;
  1810. case DM_TYPE_NVME_BIO_BASED:
  1811. dm_init_normal_md_queue(md);
  1812. blk_queue_make_request(md->queue, dm_make_request_nvme);
  1813. break;
  1814. case DM_TYPE_NONE:
  1815. WARN_ON_ONCE(true);
  1816. break;
  1817. }
  1818. r = dm_calculate_queue_limits(t, &limits);
  1819. if (r) {
  1820. DMERR("Cannot calculate initial queue limits");
  1821. return r;
  1822. }
  1823. dm_table_set_restrictions(t, md->queue, &limits);
  1824. blk_register_queue(md->disk);
  1825. return 0;
  1826. }
  1827. struct mapped_device *dm_get_md(dev_t dev)
  1828. {
  1829. struct mapped_device *md;
  1830. unsigned minor = MINOR(dev);
  1831. if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
  1832. return NULL;
  1833. spin_lock(&_minor_lock);
  1834. md = idr_find(&_minor_idr, minor);
  1835. if (!md || md == MINOR_ALLOCED || (MINOR(disk_devt(dm_disk(md))) != minor) ||
  1836. test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
  1837. md = NULL;
  1838. goto out;
  1839. }
  1840. dm_get(md);
  1841. out:
  1842. spin_unlock(&_minor_lock);
  1843. return md;
  1844. }
  1845. EXPORT_SYMBOL_GPL(dm_get_md);
  1846. void *dm_get_mdptr(struct mapped_device *md)
  1847. {
  1848. return md->interface_ptr;
  1849. }
  1850. void dm_set_mdptr(struct mapped_device *md, void *ptr)
  1851. {
  1852. md->interface_ptr = ptr;
  1853. }
  1854. void dm_get(struct mapped_device *md)
  1855. {
  1856. atomic_inc(&md->holders);
  1857. BUG_ON(test_bit(DMF_FREEING, &md->flags));
  1858. }
  1859. int dm_hold(struct mapped_device *md)
  1860. {
  1861. spin_lock(&_minor_lock);
  1862. if (test_bit(DMF_FREEING, &md->flags)) {
  1863. spin_unlock(&_minor_lock);
  1864. return -EBUSY;
  1865. }
  1866. dm_get(md);
  1867. spin_unlock(&_minor_lock);
  1868. return 0;
  1869. }
  1870. EXPORT_SYMBOL_GPL(dm_hold);
  1871. const char *dm_device_name(struct mapped_device *md)
  1872. {
  1873. return md->name;
  1874. }
  1875. EXPORT_SYMBOL_GPL(dm_device_name);
  1876. static void __dm_destroy(struct mapped_device *md, bool wait)
  1877. {
  1878. struct dm_table *map;
  1879. int srcu_idx;
  1880. might_sleep();
  1881. spin_lock(&_minor_lock);
  1882. idr_replace(&_minor_idr, MINOR_ALLOCED, MINOR(disk_devt(dm_disk(md))));
  1883. set_bit(DMF_FREEING, &md->flags);
  1884. spin_unlock(&_minor_lock);
  1885. blk_set_queue_dying(md->queue);
  1886. if (dm_request_based(md) && md->kworker_task)
  1887. kthread_flush_worker(&md->kworker);
  1888. /*
  1889. * Take suspend_lock so that presuspend and postsuspend methods
  1890. * do not race with internal suspend.
  1891. */
  1892. mutex_lock(&md->suspend_lock);
  1893. map = dm_get_live_table(md, &srcu_idx);
  1894. if (!dm_suspended_md(md)) {
  1895. dm_table_presuspend_targets(map);
  1896. dm_table_postsuspend_targets(map);
  1897. }
  1898. /* dm_put_live_table must be before msleep, otherwise deadlock is possible */
  1899. dm_put_live_table(md, srcu_idx);
  1900. mutex_unlock(&md->suspend_lock);
  1901. /*
  1902. * Rare, but there may be I/O requests still going to complete,
  1903. * for example. Wait for all references to disappear.
  1904. * No one should increment the reference count of the mapped_device,
  1905. * after the mapped_device state becomes DMF_FREEING.
  1906. */
  1907. if (wait)
  1908. while (atomic_read(&md->holders))
  1909. msleep(1);
  1910. else if (atomic_read(&md->holders))
  1911. DMWARN("%s: Forcibly removing mapped_device still in use! (%d users)",
  1912. dm_device_name(md), atomic_read(&md->holders));
  1913. dm_sysfs_exit(md);
  1914. dm_table_destroy(__unbind(md));
  1915. free_dev(md);
  1916. }
  1917. void dm_destroy(struct mapped_device *md)
  1918. {
  1919. __dm_destroy(md, true);
  1920. }
  1921. void dm_destroy_immediate(struct mapped_device *md)
  1922. {
  1923. __dm_destroy(md, false);
  1924. }
  1925. void dm_put(struct mapped_device *md)
  1926. {
  1927. atomic_dec(&md->holders);
  1928. }
  1929. EXPORT_SYMBOL_GPL(dm_put);
  1930. static int dm_wait_for_completion(struct mapped_device *md, long task_state)
  1931. {
  1932. int r = 0;
  1933. DEFINE_WAIT(wait);
  1934. while (1) {
  1935. prepare_to_wait(&md->wait, &wait, task_state);
  1936. if (!md_in_flight(md))
  1937. break;
  1938. if (signal_pending_state(task_state, current)) {
  1939. r = -EINTR;
  1940. break;
  1941. }
  1942. io_schedule();
  1943. }
  1944. finish_wait(&md->wait, &wait);
  1945. return r;
  1946. }
  1947. /*
  1948. * Process the deferred bios
  1949. */
  1950. static void dm_wq_work(struct work_struct *work)
  1951. {
  1952. struct mapped_device *md = container_of(work, struct mapped_device,
  1953. work);
  1954. struct bio *c;
  1955. int srcu_idx;
  1956. struct dm_table *map;
  1957. map = dm_get_live_table(md, &srcu_idx);
  1958. while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
  1959. spin_lock_irq(&md->deferred_lock);
  1960. c = bio_list_pop(&md->deferred);
  1961. spin_unlock_irq(&md->deferred_lock);
  1962. if (!c)
  1963. break;
  1964. if (dm_request_based(md))
  1965. generic_make_request(c);
  1966. else
  1967. __split_and_process_bio(md, map, c);
  1968. }
  1969. dm_put_live_table(md, srcu_idx);
  1970. }
  1971. static void dm_queue_flush(struct mapped_device *md)
  1972. {
  1973. clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  1974. smp_mb__after_atomic();
  1975. queue_work(md->wq, &md->work);
  1976. }
  1977. /*
  1978. * Swap in a new table, returning the old one for the caller to destroy.
  1979. */
  1980. struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
  1981. {
  1982. struct dm_table *live_map = NULL, *map = ERR_PTR(-EINVAL);
  1983. struct queue_limits limits;
  1984. int r;
  1985. mutex_lock(&md->suspend_lock);
  1986. /* device must be suspended */
  1987. if (!dm_suspended_md(md))
  1988. goto out;
  1989. /*
  1990. * If the new table has no data devices, retain the existing limits.
  1991. * This helps multipath with queue_if_no_path if all paths disappear,
  1992. * then new I/O is queued based on these limits, and then some paths
  1993. * reappear.
  1994. */
  1995. if (dm_table_has_no_data_devices(table)) {
  1996. live_map = dm_get_live_table_fast(md);
  1997. if (live_map)
  1998. limits = md->queue->limits;
  1999. dm_put_live_table_fast(md);
  2000. }
  2001. if (!live_map) {
  2002. r = dm_calculate_queue_limits(table, &limits);
  2003. if (r) {
  2004. map = ERR_PTR(r);
  2005. goto out;
  2006. }
  2007. }
  2008. map = __bind(md, table, &limits);
  2009. dm_issue_global_event();
  2010. out:
  2011. mutex_unlock(&md->suspend_lock);
  2012. return map;
  2013. }
  2014. /*
  2015. * Functions to lock and unlock any filesystem running on the
  2016. * device.
  2017. */
  2018. static int lock_fs(struct mapped_device *md)
  2019. {
  2020. int r;
  2021. WARN_ON(md->frozen_sb);
  2022. md->frozen_sb = freeze_bdev(md->bdev);
  2023. if (IS_ERR(md->frozen_sb)) {
  2024. r = PTR_ERR(md->frozen_sb);
  2025. md->frozen_sb = NULL;
  2026. return r;
  2027. }
  2028. set_bit(DMF_FROZEN, &md->flags);
  2029. return 0;
  2030. }
  2031. static void unlock_fs(struct mapped_device *md)
  2032. {
  2033. if (!test_bit(DMF_FROZEN, &md->flags))
  2034. return;
  2035. thaw_bdev(md->bdev, md->frozen_sb);
  2036. md->frozen_sb = NULL;
  2037. clear_bit(DMF_FROZEN, &md->flags);
  2038. }
  2039. /*
  2040. * @suspend_flags: DM_SUSPEND_LOCKFS_FLAG and/or DM_SUSPEND_NOFLUSH_FLAG
  2041. * @task_state: e.g. TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
  2042. * @dmf_suspended_flag: DMF_SUSPENDED or DMF_SUSPENDED_INTERNALLY
  2043. *
  2044. * If __dm_suspend returns 0, the device is completely quiescent
  2045. * now. There is no request-processing activity. All new requests
  2046. * are being added to md->deferred list.
  2047. */
  2048. static int __dm_suspend(struct mapped_device *md, struct dm_table *map,
  2049. unsigned suspend_flags, long task_state,
  2050. int dmf_suspended_flag)
  2051. {
  2052. bool do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG;
  2053. bool noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG;
  2054. int r;
  2055. lockdep_assert_held(&md->suspend_lock);
  2056. /*
  2057. * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
  2058. * This flag is cleared before dm_suspend returns.
  2059. */
  2060. if (noflush)
  2061. set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2062. else
  2063. pr_debug("%s: suspending with flush\n", dm_device_name(md));
  2064. /*
  2065. * This gets reverted if there's an error later and the targets
  2066. * provide the .presuspend_undo hook.
  2067. */
  2068. dm_table_presuspend_targets(map);
  2069. /*
  2070. * Flush I/O to the device.
  2071. * Any I/O submitted after lock_fs() may not be flushed.
  2072. * noflush takes precedence over do_lockfs.
  2073. * (lock_fs() flushes I/Os and waits for them to complete.)
  2074. */
  2075. if (!noflush && do_lockfs) {
  2076. r = lock_fs(md);
  2077. if (r) {
  2078. dm_table_presuspend_undo_targets(map);
  2079. return r;
  2080. }
  2081. }
  2082. /*
  2083. * Here we must make sure that no processes are submitting requests
  2084. * to target drivers i.e. no one may be executing
  2085. * __split_and_process_bio. This is called from dm_request and
  2086. * dm_wq_work.
  2087. *
  2088. * To get all processes out of __split_and_process_bio in dm_request,
  2089. * we take the write lock. To prevent any process from reentering
  2090. * __split_and_process_bio from dm_request and quiesce the thread
  2091. * (dm_wq_work), we set BMF_BLOCK_IO_FOR_SUSPEND and call
  2092. * flush_workqueue(md->wq).
  2093. */
  2094. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2095. if (map)
  2096. synchronize_srcu(&md->io_barrier);
  2097. /*
  2098. * Stop md->queue before flushing md->wq in case request-based
  2099. * dm defers requests to md->wq from md->queue.
  2100. */
  2101. if (dm_request_based(md)) {
  2102. dm_stop_queue(md->queue);
  2103. if (md->kworker_task)
  2104. kthread_flush_worker(&md->kworker);
  2105. }
  2106. flush_workqueue(md->wq);
  2107. /*
  2108. * At this point no more requests are entering target request routines.
  2109. * We call dm_wait_for_completion to wait for all existing requests
  2110. * to finish.
  2111. */
  2112. r = dm_wait_for_completion(md, task_state);
  2113. if (!r)
  2114. set_bit(dmf_suspended_flag, &md->flags);
  2115. if (noflush)
  2116. clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
  2117. if (map)
  2118. synchronize_srcu(&md->io_barrier);
  2119. /* were we interrupted ? */
  2120. if (r < 0) {
  2121. dm_queue_flush(md);
  2122. if (dm_request_based(md))
  2123. dm_start_queue(md->queue);
  2124. unlock_fs(md);
  2125. dm_table_presuspend_undo_targets(map);
  2126. /* pushback list is already flushed, so skip flush */
  2127. }
  2128. return r;
  2129. }
  2130. /*
  2131. * We need to be able to change a mapping table under a mounted
  2132. * filesystem. For example we might want to move some data in
  2133. * the background. Before the table can be swapped with
  2134. * dm_bind_table, dm_suspend must be called to flush any in
  2135. * flight bios and ensure that any further io gets deferred.
  2136. */
  2137. /*
  2138. * Suspend mechanism in request-based dm.
  2139. *
  2140. * 1. Flush all I/Os by lock_fs() if needed.
  2141. * 2. Stop dispatching any I/O by stopping the request_queue.
  2142. * 3. Wait for all in-flight I/Os to be completed or requeued.
  2143. *
  2144. * To abort suspend, start the request_queue.
  2145. */
  2146. int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
  2147. {
  2148. struct dm_table *map = NULL;
  2149. int r = 0;
  2150. retry:
  2151. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  2152. if (dm_suspended_md(md)) {
  2153. r = -EINVAL;
  2154. goto out_unlock;
  2155. }
  2156. if (dm_suspended_internally_md(md)) {
  2157. /* already internally suspended, wait for internal resume */
  2158. mutex_unlock(&md->suspend_lock);
  2159. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  2160. if (r)
  2161. return r;
  2162. goto retry;
  2163. }
  2164. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2165. r = __dm_suspend(md, map, suspend_flags, TASK_INTERRUPTIBLE, DMF_SUSPENDED);
  2166. if (r)
  2167. goto out_unlock;
  2168. dm_table_postsuspend_targets(map);
  2169. out_unlock:
  2170. mutex_unlock(&md->suspend_lock);
  2171. return r;
  2172. }
  2173. static int __dm_resume(struct mapped_device *md, struct dm_table *map)
  2174. {
  2175. if (map) {
  2176. int r = dm_table_resume_targets(map);
  2177. if (r)
  2178. return r;
  2179. }
  2180. dm_queue_flush(md);
  2181. /*
  2182. * Flushing deferred I/Os must be done after targets are resumed
  2183. * so that mapping of targets can work correctly.
  2184. * Request-based dm is queueing the deferred I/Os in its request_queue.
  2185. */
  2186. if (dm_request_based(md))
  2187. dm_start_queue(md->queue);
  2188. unlock_fs(md);
  2189. return 0;
  2190. }
  2191. int dm_resume(struct mapped_device *md)
  2192. {
  2193. int r;
  2194. struct dm_table *map = NULL;
  2195. retry:
  2196. r = -EINVAL;
  2197. mutex_lock_nested(&md->suspend_lock, SINGLE_DEPTH_NESTING);
  2198. if (!dm_suspended_md(md))
  2199. goto out;
  2200. if (dm_suspended_internally_md(md)) {
  2201. /* already internally suspended, wait for internal resume */
  2202. mutex_unlock(&md->suspend_lock);
  2203. r = wait_on_bit(&md->flags, DMF_SUSPENDED_INTERNALLY, TASK_INTERRUPTIBLE);
  2204. if (r)
  2205. return r;
  2206. goto retry;
  2207. }
  2208. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2209. if (!map || !dm_table_get_size(map))
  2210. goto out;
  2211. r = __dm_resume(md, map);
  2212. if (r)
  2213. goto out;
  2214. clear_bit(DMF_SUSPENDED, &md->flags);
  2215. out:
  2216. mutex_unlock(&md->suspend_lock);
  2217. return r;
  2218. }
  2219. /*
  2220. * Internal suspend/resume works like userspace-driven suspend. It waits
  2221. * until all bios finish and prevents issuing new bios to the target drivers.
  2222. * It may be used only from the kernel.
  2223. */
  2224. static void __dm_internal_suspend(struct mapped_device *md, unsigned suspend_flags)
  2225. {
  2226. struct dm_table *map = NULL;
  2227. lockdep_assert_held(&md->suspend_lock);
  2228. if (md->internal_suspend_count++)
  2229. return; /* nested internal suspend */
  2230. if (dm_suspended_md(md)) {
  2231. set_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2232. return; /* nest suspend */
  2233. }
  2234. map = rcu_dereference_protected(md->map, lockdep_is_held(&md->suspend_lock));
  2235. /*
  2236. * Using TASK_UNINTERRUPTIBLE because only NOFLUSH internal suspend is
  2237. * supported. Properly supporting a TASK_INTERRUPTIBLE internal suspend
  2238. * would require changing .presuspend to return an error -- avoid this
  2239. * until there is a need for more elaborate variants of internal suspend.
  2240. */
  2241. (void) __dm_suspend(md, map, suspend_flags, TASK_UNINTERRUPTIBLE,
  2242. DMF_SUSPENDED_INTERNALLY);
  2243. dm_table_postsuspend_targets(map);
  2244. }
  2245. static void __dm_internal_resume(struct mapped_device *md)
  2246. {
  2247. BUG_ON(!md->internal_suspend_count);
  2248. if (--md->internal_suspend_count)
  2249. return; /* resume from nested internal suspend */
  2250. if (dm_suspended_md(md))
  2251. goto done; /* resume from nested suspend */
  2252. /*
  2253. * NOTE: existing callers don't need to call dm_table_resume_targets
  2254. * (which may fail -- so best to avoid it for now by passing NULL map)
  2255. */
  2256. (void) __dm_resume(md, NULL);
  2257. done:
  2258. clear_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2259. smp_mb__after_atomic();
  2260. wake_up_bit(&md->flags, DMF_SUSPENDED_INTERNALLY);
  2261. }
  2262. void dm_internal_suspend_noflush(struct mapped_device *md)
  2263. {
  2264. mutex_lock(&md->suspend_lock);
  2265. __dm_internal_suspend(md, DM_SUSPEND_NOFLUSH_FLAG);
  2266. mutex_unlock(&md->suspend_lock);
  2267. }
  2268. EXPORT_SYMBOL_GPL(dm_internal_suspend_noflush);
  2269. void dm_internal_resume(struct mapped_device *md)
  2270. {
  2271. mutex_lock(&md->suspend_lock);
  2272. __dm_internal_resume(md);
  2273. mutex_unlock(&md->suspend_lock);
  2274. }
  2275. EXPORT_SYMBOL_GPL(dm_internal_resume);
  2276. /*
  2277. * Fast variants of internal suspend/resume hold md->suspend_lock,
  2278. * which prevents interaction with userspace-driven suspend.
  2279. */
  2280. void dm_internal_suspend_fast(struct mapped_device *md)
  2281. {
  2282. mutex_lock(&md->suspend_lock);
  2283. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  2284. return;
  2285. set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
  2286. synchronize_srcu(&md->io_barrier);
  2287. flush_workqueue(md->wq);
  2288. dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
  2289. }
  2290. EXPORT_SYMBOL_GPL(dm_internal_suspend_fast);
  2291. void dm_internal_resume_fast(struct mapped_device *md)
  2292. {
  2293. if (dm_suspended_md(md) || dm_suspended_internally_md(md))
  2294. goto done;
  2295. dm_queue_flush(md);
  2296. done:
  2297. mutex_unlock(&md->suspend_lock);
  2298. }
  2299. EXPORT_SYMBOL_GPL(dm_internal_resume_fast);
  2300. /*-----------------------------------------------------------------
  2301. * Event notification.
  2302. *---------------------------------------------------------------*/
  2303. int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
  2304. unsigned cookie)
  2305. {
  2306. char udev_cookie[DM_COOKIE_LENGTH];
  2307. char *envp[] = { udev_cookie, NULL };
  2308. if (!cookie)
  2309. return kobject_uevent(&disk_to_dev(md->disk)->kobj, action);
  2310. else {
  2311. snprintf(udev_cookie, DM_COOKIE_LENGTH, "%s=%u",
  2312. DM_COOKIE_ENV_VAR_NAME, cookie);
  2313. return kobject_uevent_env(&disk_to_dev(md->disk)->kobj,
  2314. action, envp);
  2315. }
  2316. }
  2317. uint32_t dm_next_uevent_seq(struct mapped_device *md)
  2318. {
  2319. return atomic_add_return(1, &md->uevent_seq);
  2320. }
  2321. uint32_t dm_get_event_nr(struct mapped_device *md)
  2322. {
  2323. return atomic_read(&md->event_nr);
  2324. }
  2325. int dm_wait_event(struct mapped_device *md, int event_nr)
  2326. {
  2327. return wait_event_interruptible(md->eventq,
  2328. (event_nr != atomic_read(&md->event_nr)));
  2329. }
  2330. void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
  2331. {
  2332. unsigned long flags;
  2333. spin_lock_irqsave(&md->uevent_lock, flags);
  2334. list_add(elist, &md->uevent_list);
  2335. spin_unlock_irqrestore(&md->uevent_lock, flags);
  2336. }
  2337. /*
  2338. * The gendisk is only valid as long as you have a reference
  2339. * count on 'md'.
  2340. */
  2341. struct gendisk *dm_disk(struct mapped_device *md)
  2342. {
  2343. return md->disk;
  2344. }
  2345. EXPORT_SYMBOL_GPL(dm_disk);
  2346. struct kobject *dm_kobject(struct mapped_device *md)
  2347. {
  2348. return &md->kobj_holder.kobj;
  2349. }
  2350. struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
  2351. {
  2352. struct mapped_device *md;
  2353. md = container_of(kobj, struct mapped_device, kobj_holder.kobj);
  2354. spin_lock(&_minor_lock);
  2355. if (test_bit(DMF_FREEING, &md->flags) || dm_deleting_md(md)) {
  2356. md = NULL;
  2357. goto out;
  2358. }
  2359. dm_get(md);
  2360. out:
  2361. spin_unlock(&_minor_lock);
  2362. return md;
  2363. }
  2364. int dm_suspended_md(struct mapped_device *md)
  2365. {
  2366. return test_bit(DMF_SUSPENDED, &md->flags);
  2367. }
  2368. int dm_suspended_internally_md(struct mapped_device *md)
  2369. {
  2370. return test_bit(DMF_SUSPENDED_INTERNALLY, &md->flags);
  2371. }
  2372. int dm_test_deferred_remove_flag(struct mapped_device *md)
  2373. {
  2374. return test_bit(DMF_DEFERRED_REMOVE, &md->flags);
  2375. }
  2376. int dm_suspended(struct dm_target *ti)
  2377. {
  2378. return dm_suspended_md(dm_table_get_md(ti->table));
  2379. }
  2380. EXPORT_SYMBOL_GPL(dm_suspended);
  2381. int dm_noflush_suspending(struct dm_target *ti)
  2382. {
  2383. return __noflush_suspending(dm_table_get_md(ti->table));
  2384. }
  2385. EXPORT_SYMBOL_GPL(dm_noflush_suspending);
  2386. struct dm_md_mempools *dm_alloc_md_mempools(struct mapped_device *md, enum dm_queue_mode type,
  2387. unsigned integrity, unsigned per_io_data_size,
  2388. unsigned min_pool_size)
  2389. {
  2390. struct dm_md_mempools *pools = kzalloc_node(sizeof(*pools), GFP_KERNEL, md->numa_node_id);
  2391. unsigned int pool_size = 0;
  2392. unsigned int front_pad, io_front_pad;
  2393. if (!pools)
  2394. return NULL;
  2395. switch (type) {
  2396. case DM_TYPE_BIO_BASED:
  2397. case DM_TYPE_DAX_BIO_BASED:
  2398. case DM_TYPE_NVME_BIO_BASED:
  2399. pool_size = max(dm_get_reserved_bio_based_ios(), min_pool_size);
  2400. front_pad = roundup(per_io_data_size, __alignof__(struct dm_target_io)) + offsetof(struct dm_target_io, clone);
  2401. io_front_pad = roundup(front_pad, __alignof__(struct dm_io)) + offsetof(struct dm_io, tio);
  2402. pools->io_bs = bioset_create(pool_size, io_front_pad, 0);
  2403. if (!pools->io_bs)
  2404. goto out;
  2405. if (integrity && bioset_integrity_create(pools->io_bs, pool_size))
  2406. goto out;
  2407. break;
  2408. case DM_TYPE_REQUEST_BASED:
  2409. case DM_TYPE_MQ_REQUEST_BASED:
  2410. pool_size = max(dm_get_reserved_rq_based_ios(), min_pool_size);
  2411. front_pad = offsetof(struct dm_rq_clone_bio_info, clone);
  2412. /* per_io_data_size is used for blk-mq pdu at queue allocation */
  2413. break;
  2414. default:
  2415. BUG();
  2416. }
  2417. pools->bs = bioset_create(pool_size, front_pad, 0);
  2418. if (!pools->bs)
  2419. goto out;
  2420. if (integrity && bioset_integrity_create(pools->bs, pool_size))
  2421. goto out;
  2422. return pools;
  2423. out:
  2424. dm_free_md_mempools(pools);
  2425. return NULL;
  2426. }
  2427. void dm_free_md_mempools(struct dm_md_mempools *pools)
  2428. {
  2429. if (!pools)
  2430. return;
  2431. if (pools->bs)
  2432. bioset_free(pools->bs);
  2433. if (pools->io_bs)
  2434. bioset_free(pools->io_bs);
  2435. kfree(pools);
  2436. }
  2437. struct dm_pr {
  2438. u64 old_key;
  2439. u64 new_key;
  2440. u32 flags;
  2441. bool fail_early;
  2442. };
  2443. static int dm_call_pr(struct block_device *bdev, iterate_devices_callout_fn fn,
  2444. void *data)
  2445. {
  2446. struct mapped_device *md = bdev->bd_disk->private_data;
  2447. struct dm_table *table;
  2448. struct dm_target *ti;
  2449. int ret = -ENOTTY, srcu_idx;
  2450. table = dm_get_live_table(md, &srcu_idx);
  2451. if (!table || !dm_table_get_size(table))
  2452. goto out;
  2453. /* We only support devices that have a single target */
  2454. if (dm_table_get_num_targets(table) != 1)
  2455. goto out;
  2456. ti = dm_table_get_target(table, 0);
  2457. ret = -EINVAL;
  2458. if (!ti->type->iterate_devices)
  2459. goto out;
  2460. ret = ti->type->iterate_devices(ti, fn, data);
  2461. out:
  2462. dm_put_live_table(md, srcu_idx);
  2463. return ret;
  2464. }
  2465. /*
  2466. * For register / unregister we need to manually call out to every path.
  2467. */
  2468. static int __dm_pr_register(struct dm_target *ti, struct dm_dev *dev,
  2469. sector_t start, sector_t len, void *data)
  2470. {
  2471. struct dm_pr *pr = data;
  2472. const struct pr_ops *ops = dev->bdev->bd_disk->fops->pr_ops;
  2473. if (!ops || !ops->pr_register)
  2474. return -EOPNOTSUPP;
  2475. return ops->pr_register(dev->bdev, pr->old_key, pr->new_key, pr->flags);
  2476. }
  2477. static int dm_pr_register(struct block_device *bdev, u64 old_key, u64 new_key,
  2478. u32 flags)
  2479. {
  2480. struct dm_pr pr = {
  2481. .old_key = old_key,
  2482. .new_key = new_key,
  2483. .flags = flags,
  2484. .fail_early = true,
  2485. };
  2486. int ret;
  2487. ret = dm_call_pr(bdev, __dm_pr_register, &pr);
  2488. if (ret && new_key) {
  2489. /* unregister all paths if we failed to register any path */
  2490. pr.old_key = new_key;
  2491. pr.new_key = 0;
  2492. pr.flags = 0;
  2493. pr.fail_early = false;
  2494. dm_call_pr(bdev, __dm_pr_register, &pr);
  2495. }
  2496. return ret;
  2497. }
  2498. static int dm_pr_reserve(struct block_device *bdev, u64 key, enum pr_type type,
  2499. u32 flags)
  2500. {
  2501. struct mapped_device *md = bdev->bd_disk->private_data;
  2502. const struct pr_ops *ops;
  2503. int r, srcu_idx;
  2504. r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
  2505. if (r < 0)
  2506. goto out;
  2507. ops = bdev->bd_disk->fops->pr_ops;
  2508. if (ops && ops->pr_reserve)
  2509. r = ops->pr_reserve(bdev, key, type, flags);
  2510. else
  2511. r = -EOPNOTSUPP;
  2512. out:
  2513. dm_unprepare_ioctl(md, srcu_idx);
  2514. return r;
  2515. }
  2516. static int dm_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
  2517. {
  2518. struct mapped_device *md = bdev->bd_disk->private_data;
  2519. const struct pr_ops *ops;
  2520. int r, srcu_idx;
  2521. r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
  2522. if (r < 0)
  2523. goto out;
  2524. ops = bdev->bd_disk->fops->pr_ops;
  2525. if (ops && ops->pr_release)
  2526. r = ops->pr_release(bdev, key, type);
  2527. else
  2528. r = -EOPNOTSUPP;
  2529. out:
  2530. dm_unprepare_ioctl(md, srcu_idx);
  2531. return r;
  2532. }
  2533. static int dm_pr_preempt(struct block_device *bdev, u64 old_key, u64 new_key,
  2534. enum pr_type type, bool abort)
  2535. {
  2536. struct mapped_device *md = bdev->bd_disk->private_data;
  2537. const struct pr_ops *ops;
  2538. int r, srcu_idx;
  2539. r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
  2540. if (r < 0)
  2541. goto out;
  2542. ops = bdev->bd_disk->fops->pr_ops;
  2543. if (ops && ops->pr_preempt)
  2544. r = ops->pr_preempt(bdev, old_key, new_key, type, abort);
  2545. else
  2546. r = -EOPNOTSUPP;
  2547. out:
  2548. dm_unprepare_ioctl(md, srcu_idx);
  2549. return r;
  2550. }
  2551. static int dm_pr_clear(struct block_device *bdev, u64 key)
  2552. {
  2553. struct mapped_device *md = bdev->bd_disk->private_data;
  2554. const struct pr_ops *ops;
  2555. int r, srcu_idx;
  2556. r = dm_prepare_ioctl(md, &srcu_idx, &bdev);
  2557. if (r < 0)
  2558. goto out;
  2559. ops = bdev->bd_disk->fops->pr_ops;
  2560. if (ops && ops->pr_clear)
  2561. r = ops->pr_clear(bdev, key);
  2562. else
  2563. r = -EOPNOTSUPP;
  2564. out:
  2565. dm_unprepare_ioctl(md, srcu_idx);
  2566. return r;
  2567. }
  2568. static const struct pr_ops dm_pr_ops = {
  2569. .pr_register = dm_pr_register,
  2570. .pr_reserve = dm_pr_reserve,
  2571. .pr_release = dm_pr_release,
  2572. .pr_preempt = dm_pr_preempt,
  2573. .pr_clear = dm_pr_clear,
  2574. };
  2575. static const struct block_device_operations dm_blk_dops = {
  2576. .open = dm_blk_open,
  2577. .release = dm_blk_close,
  2578. .ioctl = dm_blk_ioctl,
  2579. .getgeo = dm_blk_getgeo,
  2580. .pr_ops = &dm_pr_ops,
  2581. .owner = THIS_MODULE
  2582. };
  2583. static const struct dax_operations dm_dax_ops = {
  2584. .direct_access = dm_dax_direct_access,
  2585. .copy_from_iter = dm_dax_copy_from_iter,
  2586. };
  2587. /*
  2588. * module hooks
  2589. */
  2590. module_init(dm_init);
  2591. module_exit(dm_exit);
  2592. module_param(major, uint, 0);
  2593. MODULE_PARM_DESC(major, "The major number of the device mapper");
  2594. module_param(reserved_bio_based_ios, uint, S_IRUGO | S_IWUSR);
  2595. MODULE_PARM_DESC(reserved_bio_based_ios, "Reserved IOs in bio-based mempools");
  2596. module_param(dm_numa_node, int, S_IRUGO | S_IWUSR);
  2597. MODULE_PARM_DESC(dm_numa_node, "NUMA node for DM device memory allocations");
  2598. MODULE_DESCRIPTION(DM_NAME " driver");
  2599. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2600. MODULE_LICENSE("GPL");