dm.c 65 KB

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