dm.c 66 KB

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