dm.c 68 KB

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