dm-table.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  1. /*
  2. * Copyright (C) 2001 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 <linux/module.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/blkdev.h>
  11. #include <linux/namei.h>
  12. #include <linux/ctype.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mutex.h>
  17. #include <linux/delay.h>
  18. #include <linux/atomic.h>
  19. #include <linux/blk-mq.h>
  20. #include <linux/mount.h>
  21. #include <linux/dax.h>
  22. #include <linux/bio.h>
  23. #include <linux/keyslot-manager.h>
  24. #define DM_MSG_PREFIX "table"
  25. #define MAX_DEPTH 16
  26. #define NODE_SIZE L1_CACHE_BYTES
  27. #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t))
  28. #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1)
  29. struct dm_table {
  30. struct mapped_device *md;
  31. enum dm_queue_mode type;
  32. /* btree table */
  33. unsigned int depth;
  34. unsigned int counts[MAX_DEPTH]; /* in nodes */
  35. sector_t *index[MAX_DEPTH];
  36. unsigned int num_targets;
  37. unsigned int num_allocated;
  38. sector_t *highs;
  39. struct dm_target *targets;
  40. struct target_type *immutable_target_type;
  41. bool integrity_supported:1;
  42. bool singleton:1;
  43. bool all_blk_mq:1;
  44. unsigned integrity_added:1;
  45. /*
  46. * Indicates the rw permissions for the new logical
  47. * device. This should be a combination of FMODE_READ
  48. * and FMODE_WRITE.
  49. */
  50. fmode_t mode;
  51. /* a list of devices used by this table */
  52. struct list_head devices;
  53. /* events get handed up using this callback */
  54. void (*event_fn)(void *);
  55. void *event_context;
  56. struct dm_md_mempools *mempools;
  57. struct list_head target_callbacks;
  58. };
  59. /*
  60. * Similar to ceiling(log_size(n))
  61. */
  62. static unsigned int int_log(unsigned int n, unsigned int base)
  63. {
  64. int result = 0;
  65. while (n > 1) {
  66. n = dm_div_up(n, base);
  67. result++;
  68. }
  69. return result;
  70. }
  71. /*
  72. * Calculate the index of the child node of the n'th node k'th key.
  73. */
  74. static inline unsigned int get_child(unsigned int n, unsigned int k)
  75. {
  76. return (n * CHILDREN_PER_NODE) + k;
  77. }
  78. /*
  79. * Return the n'th node of level l from table t.
  80. */
  81. static inline sector_t *get_node(struct dm_table *t,
  82. unsigned int l, unsigned int n)
  83. {
  84. return t->index[l] + (n * KEYS_PER_NODE);
  85. }
  86. /*
  87. * Return the highest key that you could lookup from the n'th
  88. * node on level l of the btree.
  89. */
  90. static sector_t high(struct dm_table *t, unsigned int l, unsigned int n)
  91. {
  92. for (; l < t->depth - 1; l++)
  93. n = get_child(n, CHILDREN_PER_NODE - 1);
  94. if (n >= t->counts[l])
  95. return (sector_t) - 1;
  96. return get_node(t, l, n)[KEYS_PER_NODE - 1];
  97. }
  98. /*
  99. * Fills in a level of the btree based on the highs of the level
  100. * below it.
  101. */
  102. static int setup_btree_index(unsigned int l, struct dm_table *t)
  103. {
  104. unsigned int n, k;
  105. sector_t *node;
  106. for (n = 0U; n < t->counts[l]; n++) {
  107. node = get_node(t, l, n);
  108. for (k = 0U; k < KEYS_PER_NODE; k++)
  109. node[k] = high(t, l + 1, get_child(n, k));
  110. }
  111. return 0;
  112. }
  113. void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size)
  114. {
  115. unsigned long size;
  116. void *addr;
  117. /*
  118. * Check that we're not going to overflow.
  119. */
  120. if (nmemb > (ULONG_MAX / elem_size))
  121. return NULL;
  122. size = nmemb * elem_size;
  123. addr = vzalloc(size);
  124. return addr;
  125. }
  126. EXPORT_SYMBOL(dm_vcalloc);
  127. /*
  128. * highs, and targets are managed as dynamic arrays during a
  129. * table load.
  130. */
  131. static int alloc_targets(struct dm_table *t, unsigned int num)
  132. {
  133. sector_t *n_highs;
  134. struct dm_target *n_targets;
  135. /*
  136. * Allocate both the target array and offset array at once.
  137. * Append an empty entry to catch sectors beyond the end of
  138. * the device.
  139. */
  140. n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) +
  141. sizeof(sector_t));
  142. if (!n_highs)
  143. return -ENOMEM;
  144. n_targets = (struct dm_target *) (n_highs + num);
  145. memset(n_highs, -1, sizeof(*n_highs) * num);
  146. vfree(t->highs);
  147. t->num_allocated = num;
  148. t->highs = n_highs;
  149. t->targets = n_targets;
  150. return 0;
  151. }
  152. int dm_table_create(struct dm_table **result, fmode_t mode,
  153. unsigned num_targets, struct mapped_device *md)
  154. {
  155. struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL);
  156. if (!t)
  157. return -ENOMEM;
  158. INIT_LIST_HEAD(&t->devices);
  159. INIT_LIST_HEAD(&t->target_callbacks);
  160. if (!num_targets)
  161. num_targets = KEYS_PER_NODE;
  162. num_targets = dm_round_up(num_targets, KEYS_PER_NODE);
  163. if (!num_targets) {
  164. kfree(t);
  165. return -ENOMEM;
  166. }
  167. if (alloc_targets(t, num_targets)) {
  168. kfree(t);
  169. return -ENOMEM;
  170. }
  171. t->type = DM_TYPE_NONE;
  172. t->mode = mode;
  173. t->md = md;
  174. *result = t;
  175. return 0;
  176. }
  177. static void free_devices(struct list_head *devices, struct mapped_device *md)
  178. {
  179. struct list_head *tmp, *next;
  180. list_for_each_safe(tmp, next, devices) {
  181. struct dm_dev_internal *dd =
  182. list_entry(tmp, struct dm_dev_internal, list);
  183. DMWARN("%s: dm_table_destroy: dm_put_device call missing for %s",
  184. dm_device_name(md), dd->dm_dev->name);
  185. dm_put_table_device(md, dd->dm_dev);
  186. kfree(dd);
  187. }
  188. }
  189. void dm_table_destroy(struct dm_table *t)
  190. {
  191. unsigned int i;
  192. if (!t)
  193. return;
  194. /* free the indexes */
  195. if (t->depth >= 2)
  196. vfree(t->index[t->depth - 2]);
  197. /* free the targets */
  198. for (i = 0; i < t->num_targets; i++) {
  199. struct dm_target *tgt = t->targets + i;
  200. if (tgt->type->dtr)
  201. tgt->type->dtr(tgt);
  202. dm_put_target_type(tgt->type);
  203. }
  204. vfree(t->highs);
  205. /* free the device list */
  206. free_devices(&t->devices, t->md);
  207. dm_free_md_mempools(t->mempools);
  208. kfree(t);
  209. }
  210. /*
  211. * See if we've already got a device in the list.
  212. */
  213. static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev)
  214. {
  215. struct dm_dev_internal *dd;
  216. list_for_each_entry (dd, l, list)
  217. if (dd->dm_dev->bdev->bd_dev == dev)
  218. return dd;
  219. return NULL;
  220. }
  221. /*
  222. * If possible, this checks an area of a destination device is invalid.
  223. */
  224. static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
  225. sector_t start, sector_t len, void *data)
  226. {
  227. struct request_queue *q;
  228. struct queue_limits *limits = data;
  229. struct block_device *bdev = dev->bdev;
  230. sector_t dev_size =
  231. i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  232. unsigned short logical_block_size_sectors =
  233. limits->logical_block_size >> SECTOR_SHIFT;
  234. char b[BDEVNAME_SIZE];
  235. /*
  236. * Some devices exist without request functions,
  237. * such as loop devices not yet bound to backing files.
  238. * Forbid the use of such devices.
  239. */
  240. q = bdev_get_queue(bdev);
  241. if (!q || !q->make_request_fn) {
  242. DMWARN("%s: %s is not yet initialised: "
  243. "start=%llu, len=%llu, dev_size=%llu",
  244. dm_device_name(ti->table->md), bdevname(bdev, b),
  245. (unsigned long long)start,
  246. (unsigned long long)len,
  247. (unsigned long long)dev_size);
  248. return 1;
  249. }
  250. if (!dev_size)
  251. return 0;
  252. if ((start >= dev_size) || (start + len > dev_size)) {
  253. DMWARN("%s: %s too small for target: "
  254. "start=%llu, len=%llu, dev_size=%llu",
  255. dm_device_name(ti->table->md), bdevname(bdev, b),
  256. (unsigned long long)start,
  257. (unsigned long long)len,
  258. (unsigned long long)dev_size);
  259. return 1;
  260. }
  261. /*
  262. * If the target is mapped to zoned block device(s), check
  263. * that the zones are not partially mapped.
  264. */
  265. if (bdev_zoned_model(bdev) != BLK_ZONED_NONE) {
  266. unsigned int zone_sectors = bdev_zone_sectors(bdev);
  267. if (start & (zone_sectors - 1)) {
  268. DMWARN("%s: start=%llu not aligned to h/w zone size %u of %s",
  269. dm_device_name(ti->table->md),
  270. (unsigned long long)start,
  271. zone_sectors, bdevname(bdev, b));
  272. return 1;
  273. }
  274. /*
  275. * Note: The last zone of a zoned block device may be smaller
  276. * than other zones. So for a target mapping the end of a
  277. * zoned block device with such a zone, len would not be zone
  278. * aligned. We do not allow such last smaller zone to be part
  279. * of the mapping here to ensure that mappings with multiple
  280. * devices do not end up with a smaller zone in the middle of
  281. * the sector range.
  282. */
  283. if (len & (zone_sectors - 1)) {
  284. DMWARN("%s: len=%llu not aligned to h/w zone size %u of %s",
  285. dm_device_name(ti->table->md),
  286. (unsigned long long)len,
  287. zone_sectors, bdevname(bdev, b));
  288. return 1;
  289. }
  290. }
  291. if (logical_block_size_sectors <= 1)
  292. return 0;
  293. if (start & (logical_block_size_sectors - 1)) {
  294. DMWARN("%s: start=%llu not aligned to h/w "
  295. "logical block size %u of %s",
  296. dm_device_name(ti->table->md),
  297. (unsigned long long)start,
  298. limits->logical_block_size, bdevname(bdev, b));
  299. return 1;
  300. }
  301. if (len & (logical_block_size_sectors - 1)) {
  302. DMWARN("%s: len=%llu not aligned to h/w "
  303. "logical block size %u of %s",
  304. dm_device_name(ti->table->md),
  305. (unsigned long long)len,
  306. limits->logical_block_size, bdevname(bdev, b));
  307. return 1;
  308. }
  309. return 0;
  310. }
  311. /*
  312. * This upgrades the mode on an already open dm_dev, being
  313. * careful to leave things as they were if we fail to reopen the
  314. * device and not to touch the existing bdev field in case
  315. * it is accessed concurrently inside dm_table_any_congested().
  316. */
  317. static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  318. struct mapped_device *md)
  319. {
  320. int r;
  321. struct dm_dev *old_dev, *new_dev;
  322. old_dev = dd->dm_dev;
  323. r = dm_get_table_device(md, dd->dm_dev->bdev->bd_dev,
  324. dd->dm_dev->mode | new_mode, &new_dev);
  325. if (r)
  326. return r;
  327. dd->dm_dev = new_dev;
  328. dm_put_table_device(md, old_dev);
  329. return 0;
  330. }
  331. /*
  332. * Convert the path to a device
  333. */
  334. dev_t dm_get_dev_t(const char *path)
  335. {
  336. dev_t dev;
  337. struct block_device *bdev;
  338. bdev = lookup_bdev(path);
  339. if (IS_ERR(bdev))
  340. dev = name_to_dev_t(path);
  341. else {
  342. dev = bdev->bd_dev;
  343. bdput(bdev);
  344. }
  345. if (!dev) {
  346. unsigned int wait_time_ms = 0;
  347. DMERR("%s: retry %s\n", __func__, path);
  348. while (driver_probe_done() != 0 || dev == 0) {
  349. msleep(100);
  350. wait_time_ms += 100;
  351. if (wait_time_ms > DM_WAIT_DEV_MAX_TIME) {
  352. DMERR("%s: retry timeout(%dms)\n", __func__,
  353. DM_WAIT_DEV_MAX_TIME);
  354. DMERR("no dev found for %s", path);
  355. return 0;
  356. }
  357. bdev = lookup_bdev(path);
  358. if (IS_ERR(bdev))
  359. dev = name_to_dev_t(path);
  360. else {
  361. dev = bdev->bd_dev;
  362. bdput(bdev);
  363. }
  364. }
  365. }
  366. return dev;
  367. }
  368. EXPORT_SYMBOL_GPL(dm_get_dev_t);
  369. /*
  370. * Add a device to the list, or just increment the usage count if
  371. * it's already present.
  372. */
  373. int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
  374. struct dm_dev **result)
  375. {
  376. int r;
  377. dev_t dev;
  378. unsigned int major, minor;
  379. char dummy;
  380. struct dm_dev_internal *dd;
  381. struct dm_table *t = ti->table;
  382. BUG_ON(!t);
  383. if (sscanf(path, "%u:%u%c", &major, &minor, &dummy) == 2) {
  384. /* Extract the major/minor numbers */
  385. dev = MKDEV(major, minor);
  386. if (MAJOR(dev) != major || MINOR(dev) != minor)
  387. return -EOVERFLOW;
  388. } else {
  389. dev = dm_get_dev_t(path);
  390. if (!dev)
  391. return -ENODEV;
  392. }
  393. dd = find_device(&t->devices, dev);
  394. if (!dd) {
  395. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  396. if (!dd)
  397. return -ENOMEM;
  398. if ((r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev))) {
  399. kfree(dd);
  400. return r;
  401. }
  402. atomic_set(&dd->count, 0);
  403. list_add(&dd->list, &t->devices);
  404. } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
  405. r = upgrade_mode(dd, mode, t->md);
  406. if (r)
  407. return r;
  408. }
  409. atomic_inc(&dd->count);
  410. *result = dd->dm_dev;
  411. return 0;
  412. }
  413. EXPORT_SYMBOL(dm_get_device);
  414. static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  415. sector_t start, sector_t len, void *data)
  416. {
  417. struct queue_limits *limits = data;
  418. struct block_device *bdev = dev->bdev;
  419. struct request_queue *q = bdev_get_queue(bdev);
  420. char b[BDEVNAME_SIZE];
  421. if (unlikely(!q)) {
  422. DMWARN("%s: Cannot set limits for nonexistent device %s",
  423. dm_device_name(ti->table->md), bdevname(bdev, b));
  424. return 0;
  425. }
  426. if (bdev_stack_limits(limits, bdev, start) < 0)
  427. DMWARN("%s: adding target device %s caused an alignment inconsistency: "
  428. "physical_block_size=%u, logical_block_size=%u, "
  429. "alignment_offset=%u, start=%llu",
  430. dm_device_name(ti->table->md), bdevname(bdev, b),
  431. q->limits.physical_block_size,
  432. q->limits.logical_block_size,
  433. q->limits.alignment_offset,
  434. (unsigned long long) start << SECTOR_SHIFT);
  435. limits->zoned = blk_queue_zoned_model(q);
  436. return 0;
  437. }
  438. /*
  439. * Decrement a device's use count and remove it if necessary.
  440. */
  441. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  442. {
  443. int found = 0;
  444. struct list_head *devices = &ti->table->devices;
  445. struct dm_dev_internal *dd;
  446. list_for_each_entry(dd, devices, list) {
  447. if (dd->dm_dev == d) {
  448. found = 1;
  449. break;
  450. }
  451. }
  452. if (!found) {
  453. DMWARN("%s: device %s not in table devices list",
  454. dm_device_name(ti->table->md), d->name);
  455. return;
  456. }
  457. if (atomic_dec_and_test(&dd->count)) {
  458. dm_put_table_device(ti->table->md, d);
  459. list_del(&dd->list);
  460. kfree(dd);
  461. }
  462. }
  463. EXPORT_SYMBOL(dm_put_device);
  464. /*
  465. * Checks to see if the target joins onto the end of the table.
  466. */
  467. static int adjoin(struct dm_table *table, struct dm_target *ti)
  468. {
  469. struct dm_target *prev;
  470. if (!table->num_targets)
  471. return !ti->begin;
  472. prev = &table->targets[table->num_targets - 1];
  473. return (ti->begin == (prev->begin + prev->len));
  474. }
  475. /*
  476. * Used to dynamically allocate the arg array.
  477. *
  478. * We do first allocation with GFP_NOIO because dm-mpath and dm-thin must
  479. * process messages even if some device is suspended. These messages have a
  480. * small fixed number of arguments.
  481. *
  482. * On the other hand, dm-switch needs to process bulk data using messages and
  483. * excessive use of GFP_NOIO could cause trouble.
  484. */
  485. static char **realloc_argv(unsigned *size, char **old_argv)
  486. {
  487. char **argv;
  488. unsigned new_size;
  489. gfp_t gfp;
  490. if (*size) {
  491. new_size = *size * 2;
  492. gfp = GFP_KERNEL;
  493. } else {
  494. new_size = 8;
  495. gfp = GFP_NOIO;
  496. }
  497. argv = kmalloc(new_size * sizeof(*argv), gfp);
  498. if (argv) {
  499. memcpy(argv, old_argv, *size * sizeof(*argv));
  500. *size = new_size;
  501. }
  502. kfree(old_argv);
  503. return argv;
  504. }
  505. /*
  506. * Destructively splits up the argument list to pass to ctr.
  507. */
  508. int dm_split_args(int *argc, char ***argvp, char *input)
  509. {
  510. char *start, *end = input, *out, **argv = NULL;
  511. unsigned array_size = 0;
  512. *argc = 0;
  513. if (!input) {
  514. *argvp = NULL;
  515. return 0;
  516. }
  517. argv = realloc_argv(&array_size, argv);
  518. if (!argv)
  519. return -ENOMEM;
  520. while (1) {
  521. /* Skip whitespace */
  522. start = skip_spaces(end);
  523. if (!*start)
  524. break; /* success, we hit the end */
  525. /* 'out' is used to remove any back-quotes */
  526. end = out = start;
  527. while (*end) {
  528. /* Everything apart from '\0' can be quoted */
  529. if (*end == '\\' && *(end + 1)) {
  530. *out++ = *(end + 1);
  531. end += 2;
  532. continue;
  533. }
  534. if (isspace(*end))
  535. break; /* end of token */
  536. *out++ = *end++;
  537. }
  538. /* have we already filled the array ? */
  539. if ((*argc + 1) > array_size) {
  540. argv = realloc_argv(&array_size, argv);
  541. if (!argv)
  542. return -ENOMEM;
  543. }
  544. /* we know this is whitespace */
  545. if (*end)
  546. end++;
  547. /* terminate the string and put it in the array */
  548. *out = '\0';
  549. argv[*argc] = start;
  550. (*argc)++;
  551. }
  552. *argvp = argv;
  553. return 0;
  554. }
  555. /*
  556. * Impose necessary and sufficient conditions on a devices's table such
  557. * that any incoming bio which respects its logical_block_size can be
  558. * processed successfully. If it falls across the boundary between
  559. * two or more targets, the size of each piece it gets split into must
  560. * be compatible with the logical_block_size of the target processing it.
  561. */
  562. static int validate_hardware_logical_block_alignment(struct dm_table *table,
  563. struct queue_limits *limits)
  564. {
  565. /*
  566. * This function uses arithmetic modulo the logical_block_size
  567. * (in units of 512-byte sectors).
  568. */
  569. unsigned short device_logical_block_size_sects =
  570. limits->logical_block_size >> SECTOR_SHIFT;
  571. /*
  572. * Offset of the start of the next table entry, mod logical_block_size.
  573. */
  574. unsigned short next_target_start = 0;
  575. /*
  576. * Given an aligned bio that extends beyond the end of a
  577. * target, how many sectors must the next target handle?
  578. */
  579. unsigned short remaining = 0;
  580. struct dm_target *uninitialized_var(ti);
  581. struct queue_limits ti_limits;
  582. unsigned i;
  583. /*
  584. * Check each entry in the table in turn.
  585. */
  586. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  587. ti = dm_table_get_target(table, i);
  588. blk_set_stacking_limits(&ti_limits);
  589. /* combine all target devices' limits */
  590. if (ti->type->iterate_devices)
  591. ti->type->iterate_devices(ti, dm_set_device_limits,
  592. &ti_limits);
  593. /*
  594. * If the remaining sectors fall entirely within this
  595. * table entry are they compatible with its logical_block_size?
  596. */
  597. if (remaining < ti->len &&
  598. remaining & ((ti_limits.logical_block_size >>
  599. SECTOR_SHIFT) - 1))
  600. break; /* Error */
  601. next_target_start =
  602. (unsigned short) ((next_target_start + ti->len) &
  603. (device_logical_block_size_sects - 1));
  604. remaining = next_target_start ?
  605. device_logical_block_size_sects - next_target_start : 0;
  606. }
  607. if (remaining) {
  608. DMWARN("%s: table line %u (start sect %llu len %llu) "
  609. "not aligned to h/w logical block size %u",
  610. dm_device_name(table->md), i,
  611. (unsigned long long) ti->begin,
  612. (unsigned long long) ti->len,
  613. limits->logical_block_size);
  614. return -EINVAL;
  615. }
  616. return 0;
  617. }
  618. int dm_table_add_target(struct dm_table *t, const char *type,
  619. sector_t start, sector_t len, char *params)
  620. {
  621. int r = -EINVAL, argc;
  622. char **argv;
  623. struct dm_target *tgt;
  624. if (t->singleton) {
  625. DMERR("%s: target type %s must appear alone in table",
  626. dm_device_name(t->md), t->targets->type->name);
  627. return -EINVAL;
  628. }
  629. BUG_ON(t->num_targets >= t->num_allocated);
  630. tgt = t->targets + t->num_targets;
  631. memset(tgt, 0, sizeof(*tgt));
  632. if (!len) {
  633. DMERR("%s: zero-length target", dm_device_name(t->md));
  634. return -EINVAL;
  635. }
  636. tgt->type = dm_get_target_type(type);
  637. if (!tgt->type) {
  638. DMERR("%s: %s: unknown target type", dm_device_name(t->md), type);
  639. return -EINVAL;
  640. }
  641. if (dm_target_needs_singleton(tgt->type)) {
  642. if (t->num_targets) {
  643. tgt->error = "singleton target type must appear alone in table";
  644. goto bad;
  645. }
  646. t->singleton = true;
  647. }
  648. if (dm_target_always_writeable(tgt->type) && !(t->mode & FMODE_WRITE)) {
  649. tgt->error = "target type may not be included in a read-only table";
  650. goto bad;
  651. }
  652. if (t->immutable_target_type) {
  653. if (t->immutable_target_type != tgt->type) {
  654. tgt->error = "immutable target type cannot be mixed with other target types";
  655. goto bad;
  656. }
  657. } else if (dm_target_is_immutable(tgt->type)) {
  658. if (t->num_targets) {
  659. tgt->error = "immutable target type cannot be mixed with other target types";
  660. goto bad;
  661. }
  662. t->immutable_target_type = tgt->type;
  663. }
  664. if (dm_target_has_integrity(tgt->type))
  665. t->integrity_added = 1;
  666. tgt->table = t;
  667. tgt->begin = start;
  668. tgt->len = len;
  669. tgt->error = "Unknown error";
  670. /*
  671. * Does this target adjoin the previous one ?
  672. */
  673. if (!adjoin(t, tgt)) {
  674. tgt->error = "Gap in table";
  675. goto bad;
  676. }
  677. r = dm_split_args(&argc, &argv, params);
  678. if (r) {
  679. tgt->error = "couldn't split parameters (insufficient memory)";
  680. goto bad;
  681. }
  682. r = tgt->type->ctr(tgt, argc, argv);
  683. kfree(argv);
  684. if (r)
  685. goto bad;
  686. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  687. if (!tgt->num_discard_bios && tgt->discards_supported)
  688. DMWARN("%s: %s: ignoring discards_supported because num_discard_bios is zero.",
  689. dm_device_name(t->md), type);
  690. return 0;
  691. bad:
  692. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  693. dm_put_target_type(tgt->type);
  694. return r;
  695. }
  696. /*
  697. * Target argument parsing helpers.
  698. */
  699. static int validate_next_arg(const struct dm_arg *arg,
  700. struct dm_arg_set *arg_set,
  701. unsigned *value, char **error, unsigned grouped)
  702. {
  703. const char *arg_str = dm_shift_arg(arg_set);
  704. char dummy;
  705. if (!arg_str ||
  706. (sscanf(arg_str, "%u%c", value, &dummy) != 1) ||
  707. (*value < arg->min) ||
  708. (*value > arg->max) ||
  709. (grouped && arg_set->argc < *value)) {
  710. *error = arg->error;
  711. return -EINVAL;
  712. }
  713. return 0;
  714. }
  715. int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
  716. unsigned *value, char **error)
  717. {
  718. return validate_next_arg(arg, arg_set, value, error, 0);
  719. }
  720. EXPORT_SYMBOL(dm_read_arg);
  721. int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
  722. unsigned *value, char **error)
  723. {
  724. return validate_next_arg(arg, arg_set, value, error, 1);
  725. }
  726. EXPORT_SYMBOL(dm_read_arg_group);
  727. const char *dm_shift_arg(struct dm_arg_set *as)
  728. {
  729. char *r;
  730. if (as->argc) {
  731. as->argc--;
  732. r = *as->argv;
  733. as->argv++;
  734. return r;
  735. }
  736. return NULL;
  737. }
  738. EXPORT_SYMBOL(dm_shift_arg);
  739. void dm_consume_args(struct dm_arg_set *as, unsigned num_args)
  740. {
  741. BUG_ON(as->argc < num_args);
  742. as->argc -= num_args;
  743. as->argv += num_args;
  744. }
  745. EXPORT_SYMBOL(dm_consume_args);
  746. static bool __table_type_bio_based(enum dm_queue_mode table_type)
  747. {
  748. return (table_type == DM_TYPE_BIO_BASED ||
  749. table_type == DM_TYPE_DAX_BIO_BASED);
  750. }
  751. static bool __table_type_request_based(enum dm_queue_mode table_type)
  752. {
  753. return (table_type == DM_TYPE_REQUEST_BASED ||
  754. table_type == DM_TYPE_MQ_REQUEST_BASED);
  755. }
  756. void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type)
  757. {
  758. t->type = type;
  759. }
  760. EXPORT_SYMBOL_GPL(dm_table_set_type);
  761. static int device_not_dax_capable(struct dm_target *ti, struct dm_dev *dev,
  762. sector_t start, sector_t len, void *data)
  763. {
  764. return !bdev_dax_supported(dev->bdev, PAGE_SIZE);
  765. }
  766. static bool dm_table_supports_dax(struct dm_table *t)
  767. {
  768. struct dm_target *ti;
  769. unsigned i;
  770. /* Ensure that all targets support DAX. */
  771. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  772. ti = dm_table_get_target(t, i);
  773. if (!ti->type->direct_access)
  774. return false;
  775. if (!ti->type->iterate_devices ||
  776. ti->type->iterate_devices(ti, device_not_dax_capable, NULL))
  777. return false;
  778. }
  779. return true;
  780. }
  781. static int dm_table_determine_type(struct dm_table *t)
  782. {
  783. unsigned i;
  784. unsigned bio_based = 0, request_based = 0, hybrid = 0;
  785. unsigned sq_count = 0, mq_count = 0;
  786. struct dm_target *tgt;
  787. struct dm_dev_internal *dd;
  788. struct list_head *devices = dm_table_get_devices(t);
  789. enum dm_queue_mode live_md_type = dm_get_md_type(t->md);
  790. if (t->type != DM_TYPE_NONE) {
  791. /* target already set the table's type */
  792. if (t->type == DM_TYPE_BIO_BASED)
  793. return 0;
  794. BUG_ON(t->type == DM_TYPE_DAX_BIO_BASED);
  795. goto verify_rq_based;
  796. }
  797. for (i = 0; i < t->num_targets; i++) {
  798. tgt = t->targets + i;
  799. if (dm_target_hybrid(tgt))
  800. hybrid = 1;
  801. else if (dm_target_request_based(tgt))
  802. request_based = 1;
  803. else
  804. bio_based = 1;
  805. if (bio_based && request_based) {
  806. DMWARN("Inconsistent table: different target types"
  807. " can't be mixed up");
  808. return -EINVAL;
  809. }
  810. }
  811. if (hybrid && !bio_based && !request_based) {
  812. /*
  813. * The targets can work either way.
  814. * Determine the type from the live device.
  815. * Default to bio-based if device is new.
  816. */
  817. if (__table_type_request_based(live_md_type))
  818. request_based = 1;
  819. else
  820. bio_based = 1;
  821. }
  822. if (bio_based) {
  823. /* We must use this table as bio-based */
  824. t->type = DM_TYPE_BIO_BASED;
  825. if (dm_table_supports_dax(t) ||
  826. (list_empty(devices) && live_md_type == DM_TYPE_DAX_BIO_BASED))
  827. t->type = DM_TYPE_DAX_BIO_BASED;
  828. return 0;
  829. }
  830. BUG_ON(!request_based); /* No targets in this table */
  831. /*
  832. * The only way to establish DM_TYPE_MQ_REQUEST_BASED is by
  833. * having a compatible target use dm_table_set_type.
  834. */
  835. t->type = DM_TYPE_REQUEST_BASED;
  836. verify_rq_based:
  837. /*
  838. * Request-based dm supports only tables that have a single target now.
  839. * To support multiple targets, request splitting support is needed,
  840. * and that needs lots of changes in the block-layer.
  841. * (e.g. request completion process for partial completion.)
  842. */
  843. if (t->num_targets > 1) {
  844. DMWARN("Request-based dm doesn't support multiple targets yet");
  845. return -EINVAL;
  846. }
  847. if (list_empty(devices)) {
  848. int srcu_idx;
  849. struct dm_table *live_table = dm_get_live_table(t->md, &srcu_idx);
  850. /* inherit live table's type and all_blk_mq */
  851. if (live_table) {
  852. t->type = live_table->type;
  853. t->all_blk_mq = live_table->all_blk_mq;
  854. }
  855. dm_put_live_table(t->md, srcu_idx);
  856. return 0;
  857. }
  858. /* Non-request-stackable devices can't be used for request-based dm */
  859. list_for_each_entry(dd, devices, list) {
  860. struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
  861. if (!blk_queue_stackable(q)) {
  862. DMERR("table load rejected: including"
  863. " non-request-stackable devices");
  864. return -EINVAL;
  865. }
  866. if (q->mq_ops)
  867. mq_count++;
  868. else
  869. sq_count++;
  870. }
  871. if (sq_count && mq_count) {
  872. DMERR("table load rejected: not all devices are blk-mq request-stackable");
  873. return -EINVAL;
  874. }
  875. t->all_blk_mq = mq_count > 0;
  876. if (t->type == DM_TYPE_MQ_REQUEST_BASED && !t->all_blk_mq) {
  877. DMERR("table load rejected: all devices are not blk-mq request-stackable");
  878. return -EINVAL;
  879. }
  880. return 0;
  881. }
  882. enum dm_queue_mode dm_table_get_type(struct dm_table *t)
  883. {
  884. return t->type;
  885. }
  886. struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
  887. {
  888. return t->immutable_target_type;
  889. }
  890. struct dm_target *dm_table_get_immutable_target(struct dm_table *t)
  891. {
  892. /* Immutable target is implicitly a singleton */
  893. if (t->num_targets > 1 ||
  894. !dm_target_is_immutable(t->targets[0].type))
  895. return NULL;
  896. return t->targets;
  897. }
  898. struct dm_target *dm_table_get_wildcard_target(struct dm_table *t)
  899. {
  900. struct dm_target *ti;
  901. unsigned i;
  902. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  903. ti = dm_table_get_target(t, i);
  904. if (dm_target_is_wildcard(ti->type))
  905. return ti;
  906. }
  907. return NULL;
  908. }
  909. bool dm_table_bio_based(struct dm_table *t)
  910. {
  911. return __table_type_bio_based(dm_table_get_type(t));
  912. }
  913. bool dm_table_request_based(struct dm_table *t)
  914. {
  915. return __table_type_request_based(dm_table_get_type(t));
  916. }
  917. bool dm_table_all_blk_mq_devices(struct dm_table *t)
  918. {
  919. return t->all_blk_mq;
  920. }
  921. static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *md)
  922. {
  923. enum dm_queue_mode type = dm_table_get_type(t);
  924. unsigned per_io_data_size = 0;
  925. struct dm_target *tgt;
  926. unsigned i;
  927. if (unlikely(type == DM_TYPE_NONE)) {
  928. DMWARN("no table type is set, can't allocate mempools");
  929. return -EINVAL;
  930. }
  931. if (__table_type_bio_based(type))
  932. for (i = 0; i < t->num_targets; i++) {
  933. tgt = t->targets + i;
  934. per_io_data_size = max(per_io_data_size, tgt->per_io_data_size);
  935. }
  936. t->mempools = dm_alloc_md_mempools(md, type, t->integrity_supported, per_io_data_size);
  937. if (!t->mempools)
  938. return -ENOMEM;
  939. return 0;
  940. }
  941. void dm_table_free_md_mempools(struct dm_table *t)
  942. {
  943. dm_free_md_mempools(t->mempools);
  944. t->mempools = NULL;
  945. }
  946. struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
  947. {
  948. return t->mempools;
  949. }
  950. static int setup_indexes(struct dm_table *t)
  951. {
  952. int i;
  953. unsigned int total = 0;
  954. sector_t *indexes;
  955. /* allocate the space for *all* the indexes */
  956. for (i = t->depth - 2; i >= 0; i--) {
  957. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  958. total += t->counts[i];
  959. }
  960. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  961. if (!indexes)
  962. return -ENOMEM;
  963. /* set up internal nodes, bottom-up */
  964. for (i = t->depth - 2; i >= 0; i--) {
  965. t->index[i] = indexes;
  966. indexes += (KEYS_PER_NODE * t->counts[i]);
  967. setup_btree_index(i, t);
  968. }
  969. return 0;
  970. }
  971. /*
  972. * Builds the btree to index the map.
  973. */
  974. static int dm_table_build_index(struct dm_table *t)
  975. {
  976. int r = 0;
  977. unsigned int leaf_nodes;
  978. /* how many indexes will the btree have ? */
  979. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  980. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  981. /* leaf layer has already been set up */
  982. t->counts[t->depth - 1] = leaf_nodes;
  983. t->index[t->depth - 1] = t->highs;
  984. if (t->depth >= 2)
  985. r = setup_indexes(t);
  986. return r;
  987. }
  988. static bool integrity_profile_exists(struct gendisk *disk)
  989. {
  990. return !!blk_get_integrity(disk);
  991. }
  992. /*
  993. * Get a disk whose integrity profile reflects the table's profile.
  994. * Returns NULL if integrity support was inconsistent or unavailable.
  995. */
  996. static struct gendisk * dm_table_get_integrity_disk(struct dm_table *t)
  997. {
  998. struct list_head *devices = dm_table_get_devices(t);
  999. struct dm_dev_internal *dd = NULL;
  1000. struct gendisk *prev_disk = NULL, *template_disk = NULL;
  1001. unsigned i;
  1002. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1003. struct dm_target *ti = dm_table_get_target(t, i);
  1004. if (!dm_target_passes_integrity(ti->type))
  1005. goto no_integrity;
  1006. }
  1007. list_for_each_entry(dd, devices, list) {
  1008. template_disk = dd->dm_dev->bdev->bd_disk;
  1009. if (!integrity_profile_exists(template_disk))
  1010. goto no_integrity;
  1011. else if (prev_disk &&
  1012. blk_integrity_compare(prev_disk, template_disk) < 0)
  1013. goto no_integrity;
  1014. prev_disk = template_disk;
  1015. }
  1016. return template_disk;
  1017. no_integrity:
  1018. if (prev_disk)
  1019. DMWARN("%s: integrity not set: %s and %s profile mismatch",
  1020. dm_device_name(t->md),
  1021. prev_disk->disk_name,
  1022. template_disk->disk_name);
  1023. return NULL;
  1024. }
  1025. /*
  1026. * Register the mapped device for blk_integrity support if the
  1027. * underlying devices have an integrity profile. But all devices may
  1028. * not have matching profiles (checking all devices isn't reliable
  1029. * during table load because this table may use other DM device(s) which
  1030. * must be resumed before they will have an initialized integity
  1031. * profile). Consequently, stacked DM devices force a 2 stage integrity
  1032. * profile validation: First pass during table load, final pass during
  1033. * resume.
  1034. */
  1035. static int dm_table_register_integrity(struct dm_table *t)
  1036. {
  1037. struct mapped_device *md = t->md;
  1038. struct gendisk *template_disk = NULL;
  1039. /* If target handles integrity itself do not register it here. */
  1040. if (t->integrity_added)
  1041. return 0;
  1042. template_disk = dm_table_get_integrity_disk(t);
  1043. if (!template_disk)
  1044. return 0;
  1045. if (!integrity_profile_exists(dm_disk(md))) {
  1046. t->integrity_supported = true;
  1047. /*
  1048. * Register integrity profile during table load; we can do
  1049. * this because the final profile must match during resume.
  1050. */
  1051. blk_integrity_register(dm_disk(md),
  1052. blk_get_integrity(template_disk));
  1053. return 0;
  1054. }
  1055. /*
  1056. * If DM device already has an initialized integrity
  1057. * profile the new profile should not conflict.
  1058. */
  1059. if (blk_integrity_compare(dm_disk(md), template_disk) < 0) {
  1060. DMWARN("%s: conflict with existing integrity profile: "
  1061. "%s profile mismatch",
  1062. dm_device_name(t->md),
  1063. template_disk->disk_name);
  1064. return 1;
  1065. }
  1066. /* Preserve existing integrity profile */
  1067. t->integrity_supported = true;
  1068. return 0;
  1069. }
  1070. /*
  1071. * Prepares the table for use by building the indices,
  1072. * setting the type, and allocating mempools.
  1073. */
  1074. int dm_table_complete(struct dm_table *t)
  1075. {
  1076. int r;
  1077. r = dm_table_determine_type(t);
  1078. if (r) {
  1079. DMERR("unable to determine table type");
  1080. return r;
  1081. }
  1082. r = dm_table_build_index(t);
  1083. if (r) {
  1084. DMERR("unable to build btrees");
  1085. return r;
  1086. }
  1087. r = dm_table_register_integrity(t);
  1088. if (r) {
  1089. DMERR("could not register integrity profile.");
  1090. return r;
  1091. }
  1092. r = dm_table_alloc_md_mempools(t, t->md);
  1093. if (r)
  1094. DMERR("unable to allocate mempools");
  1095. return r;
  1096. }
  1097. static DEFINE_MUTEX(_event_lock);
  1098. void dm_table_event_callback(struct dm_table *t,
  1099. void (*fn)(void *), void *context)
  1100. {
  1101. mutex_lock(&_event_lock);
  1102. t->event_fn = fn;
  1103. t->event_context = context;
  1104. mutex_unlock(&_event_lock);
  1105. }
  1106. void dm_table_event(struct dm_table *t)
  1107. {
  1108. mutex_lock(&_event_lock);
  1109. if (t->event_fn)
  1110. t->event_fn(t->event_context);
  1111. mutex_unlock(&_event_lock);
  1112. }
  1113. EXPORT_SYMBOL(dm_table_event);
  1114. inline sector_t dm_table_get_size(struct dm_table *t)
  1115. {
  1116. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  1117. }
  1118. EXPORT_SYMBOL(dm_table_get_size);
  1119. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  1120. {
  1121. if (index >= t->num_targets)
  1122. return NULL;
  1123. return t->targets + index;
  1124. }
  1125. /*
  1126. * Search the btree for the correct target.
  1127. *
  1128. * Caller should check returned pointer with dm_target_is_valid()
  1129. * to trap I/O beyond end of device.
  1130. */
  1131. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  1132. {
  1133. unsigned int l, n = 0, k = 0;
  1134. sector_t *node;
  1135. if (unlikely(sector >= dm_table_get_size(t)))
  1136. return &t->targets[t->num_targets];
  1137. for (l = 0; l < t->depth; l++) {
  1138. n = get_child(n, k);
  1139. node = get_node(t, l, n);
  1140. for (k = 0; k < KEYS_PER_NODE; k++)
  1141. if (node[k] >= sector)
  1142. break;
  1143. }
  1144. return &t->targets[(KEYS_PER_NODE * n) + k];
  1145. }
  1146. /*
  1147. * type->iterate_devices() should be called when the sanity check needs to
  1148. * iterate and check all underlying data devices. iterate_devices() will
  1149. * iterate all underlying data devices until it encounters a non-zero return
  1150. * code, returned by whether the input iterate_devices_callout_fn, or
  1151. * iterate_devices() itself internally.
  1152. *
  1153. * For some target type (e.g. dm-stripe), one call of iterate_devices() may
  1154. * iterate multiple underlying devices internally, in which case a non-zero
  1155. * return code returned by iterate_devices_callout_fn will stop the iteration
  1156. * in advance.
  1157. *
  1158. * Cases requiring _any_ underlying device supporting some kind of attribute,
  1159. * should use the iteration structure like dm_table_any_dev_attr(), or call
  1160. * it directly. @func should handle semantics of positive examples, e.g.
  1161. * capable of something.
  1162. *
  1163. * Cases requiring _all_ underlying devices supporting some kind of attribute,
  1164. * should use the iteration structure like dm_table_supports_nowait() or
  1165. * dm_table_supports_discards(). Or introduce dm_table_all_devs_attr() that
  1166. * uses an @anti_func that handle semantics of counter examples, e.g. not
  1167. * capable of something. So: return !dm_table_any_dev_attr(t, anti_func, data);
  1168. */
  1169. static bool dm_table_any_dev_attr(struct dm_table *t,
  1170. iterate_devices_callout_fn func, void *data)
  1171. {
  1172. struct dm_target *ti;
  1173. unsigned int i;
  1174. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1175. ti = dm_table_get_target(t, i);
  1176. if (ti->type->iterate_devices &&
  1177. ti->type->iterate_devices(ti, func, data))
  1178. return true;
  1179. }
  1180. return false;
  1181. }
  1182. static int count_device(struct dm_target *ti, struct dm_dev *dev,
  1183. sector_t start, sector_t len, void *data)
  1184. {
  1185. unsigned *num_devices = data;
  1186. (*num_devices)++;
  1187. return 0;
  1188. }
  1189. /*
  1190. * Check whether a table has no data devices attached using each
  1191. * target's iterate_devices method.
  1192. * Returns false if the result is unknown because a target doesn't
  1193. * support iterate_devices.
  1194. */
  1195. bool dm_table_has_no_data_devices(struct dm_table *table)
  1196. {
  1197. struct dm_target *ti;
  1198. unsigned i, num_devices;
  1199. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  1200. ti = dm_table_get_target(table, i);
  1201. if (!ti->type->iterate_devices)
  1202. return false;
  1203. num_devices = 0;
  1204. ti->type->iterate_devices(ti, count_device, &num_devices);
  1205. if (num_devices)
  1206. return false;
  1207. }
  1208. return true;
  1209. }
  1210. static int device_not_zoned_model(struct dm_target *ti, struct dm_dev *dev,
  1211. sector_t start, sector_t len, void *data)
  1212. {
  1213. struct request_queue *q = bdev_get_queue(dev->bdev);
  1214. enum blk_zoned_model *zoned_model = data;
  1215. return !q || blk_queue_zoned_model(q) != *zoned_model;
  1216. }
  1217. static bool dm_table_supports_zoned_model(struct dm_table *t,
  1218. enum blk_zoned_model zoned_model)
  1219. {
  1220. struct dm_target *ti;
  1221. unsigned i;
  1222. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1223. ti = dm_table_get_target(t, i);
  1224. if (zoned_model == BLK_ZONED_HM &&
  1225. !dm_target_supports_zoned_hm(ti->type))
  1226. return false;
  1227. if (!ti->type->iterate_devices ||
  1228. ti->type->iterate_devices(ti, device_not_zoned_model, &zoned_model))
  1229. return false;
  1230. }
  1231. return true;
  1232. }
  1233. static int device_not_matches_zone_sectors(struct dm_target *ti, struct dm_dev *dev,
  1234. sector_t start, sector_t len, void *data)
  1235. {
  1236. struct request_queue *q = bdev_get_queue(dev->bdev);
  1237. unsigned int *zone_sectors = data;
  1238. return !q || blk_queue_zone_sectors(q) != *zone_sectors;
  1239. }
  1240. static int validate_hardware_zoned_model(struct dm_table *table,
  1241. enum blk_zoned_model zoned_model,
  1242. unsigned int zone_sectors)
  1243. {
  1244. if (zoned_model == BLK_ZONED_NONE)
  1245. return 0;
  1246. if (!dm_table_supports_zoned_model(table, zoned_model)) {
  1247. DMERR("%s: zoned model is not consistent across all devices",
  1248. dm_device_name(table->md));
  1249. return -EINVAL;
  1250. }
  1251. /* Check zone size validity and compatibility */
  1252. if (!zone_sectors || !is_power_of_2(zone_sectors))
  1253. return -EINVAL;
  1254. if (dm_table_any_dev_attr(table, device_not_matches_zone_sectors, &zone_sectors)) {
  1255. DMERR("%s: zone sectors is not consistent across all devices",
  1256. dm_device_name(table->md));
  1257. return -EINVAL;
  1258. }
  1259. return 0;
  1260. }
  1261. /*
  1262. * Establish the new table's queue_limits and validate them.
  1263. */
  1264. int dm_calculate_queue_limits(struct dm_table *table,
  1265. struct queue_limits *limits)
  1266. {
  1267. struct dm_target *ti;
  1268. struct queue_limits ti_limits;
  1269. unsigned i;
  1270. enum blk_zoned_model zoned_model = BLK_ZONED_NONE;
  1271. unsigned int zone_sectors = 0;
  1272. blk_set_stacking_limits(limits);
  1273. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  1274. blk_set_stacking_limits(&ti_limits);
  1275. ti = dm_table_get_target(table, i);
  1276. if (!ti->type->iterate_devices)
  1277. goto combine_limits;
  1278. /*
  1279. * Combine queue limits of all the devices this target uses.
  1280. */
  1281. ti->type->iterate_devices(ti, dm_set_device_limits,
  1282. &ti_limits);
  1283. if (zoned_model == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
  1284. /*
  1285. * After stacking all limits, validate all devices
  1286. * in table support this zoned model and zone sectors.
  1287. */
  1288. zoned_model = ti_limits.zoned;
  1289. zone_sectors = ti_limits.chunk_sectors;
  1290. }
  1291. /* Set I/O hints portion of queue limits */
  1292. if (ti->type->io_hints)
  1293. ti->type->io_hints(ti, &ti_limits);
  1294. /*
  1295. * Check each device area is consistent with the target's
  1296. * overall queue limits.
  1297. */
  1298. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  1299. &ti_limits))
  1300. return -EINVAL;
  1301. combine_limits:
  1302. /*
  1303. * Merge this target's queue limits into the overall limits
  1304. * for the table.
  1305. */
  1306. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  1307. DMWARN("%s: adding target device "
  1308. "(start sect %llu len %llu) "
  1309. "caused an alignment inconsistency",
  1310. dm_device_name(table->md),
  1311. (unsigned long long) ti->begin,
  1312. (unsigned long long) ti->len);
  1313. /*
  1314. * FIXME: this should likely be moved to blk_stack_limits(), would
  1315. * also eliminate limits->zoned stacking hack in dm_set_device_limits()
  1316. */
  1317. if (limits->zoned == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
  1318. /*
  1319. * By default, the stacked limits zoned model is set to
  1320. * BLK_ZONED_NONE in blk_set_stacking_limits(). Update
  1321. * this model using the first target model reported
  1322. * that is not BLK_ZONED_NONE. This will be either the
  1323. * first target device zoned model or the model reported
  1324. * by the target .io_hints.
  1325. */
  1326. limits->zoned = ti_limits.zoned;
  1327. }
  1328. }
  1329. /*
  1330. * Verify that the zoned model and zone sectors, as determined before
  1331. * any .io_hints override, are the same across all devices in the table.
  1332. * - this is especially relevant if .io_hints is emulating a disk-managed
  1333. * zoned model (aka BLK_ZONED_NONE) on host-managed zoned block devices.
  1334. * BUT...
  1335. */
  1336. if (limits->zoned != BLK_ZONED_NONE) {
  1337. /*
  1338. * ...IF the above limits stacking determined a zoned model
  1339. * validate that all of the table's devices conform to it.
  1340. */
  1341. zoned_model = limits->zoned;
  1342. zone_sectors = limits->chunk_sectors;
  1343. }
  1344. if (validate_hardware_zoned_model(table, zoned_model, zone_sectors))
  1345. return -EINVAL;
  1346. return validate_hardware_logical_block_alignment(table, limits);
  1347. }
  1348. /*
  1349. * Verify that all devices have an integrity profile that matches the
  1350. * DM device's registered integrity profile. If the profiles don't
  1351. * match then unregister the DM device's integrity profile.
  1352. */
  1353. static void dm_table_verify_integrity(struct dm_table *t)
  1354. {
  1355. struct gendisk *template_disk = NULL;
  1356. if (t->integrity_added)
  1357. return;
  1358. if (t->integrity_supported) {
  1359. /*
  1360. * Verify that the original integrity profile
  1361. * matches all the devices in this table.
  1362. */
  1363. template_disk = dm_table_get_integrity_disk(t);
  1364. if (template_disk &&
  1365. blk_integrity_compare(dm_disk(t->md), template_disk) >= 0)
  1366. return;
  1367. }
  1368. if (integrity_profile_exists(dm_disk(t->md))) {
  1369. DMWARN("%s: unable to establish an integrity profile",
  1370. dm_device_name(t->md));
  1371. blk_integrity_unregister(dm_disk(t->md));
  1372. }
  1373. }
  1374. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  1375. static int device_intersect_crypto_modes(struct dm_target *ti,
  1376. struct dm_dev *dev, sector_t start,
  1377. sector_t len, void *data)
  1378. {
  1379. struct keyslot_manager *parent = data;
  1380. struct keyslot_manager *child = bdev_get_queue(dev->bdev)->ksm;
  1381. keyslot_manager_intersect_modes(parent, child);
  1382. return 0;
  1383. }
  1384. /*
  1385. * Update the inline crypto modes supported by 'q->ksm' to be the intersection
  1386. * of the modes supported by all targets in the table.
  1387. *
  1388. * For any mode to be supported at all, all targets must have explicitly
  1389. * declared that they can pass through inline crypto support. For a particular
  1390. * mode to be supported, all underlying devices must also support it.
  1391. *
  1392. * Assume that 'q->ksm' initially declares all modes to be supported.
  1393. */
  1394. static void dm_calculate_supported_crypto_modes(struct dm_table *t,
  1395. struct request_queue *q)
  1396. {
  1397. struct dm_target *ti;
  1398. unsigned int i;
  1399. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1400. ti = dm_table_get_target(t, i);
  1401. if (!ti->may_passthrough_inline_crypto) {
  1402. keyslot_manager_intersect_modes(q->ksm, NULL);
  1403. return;
  1404. }
  1405. if (!ti->type->iterate_devices)
  1406. continue;
  1407. ti->type->iterate_devices(ti, device_intersect_crypto_modes,
  1408. q->ksm);
  1409. }
  1410. }
  1411. #else /* CONFIG_BLK_INLINE_ENCRYPTION */
  1412. static inline void dm_calculate_supported_crypto_modes(struct dm_table *t,
  1413. struct request_queue *q)
  1414. {
  1415. }
  1416. #endif /* !CONFIG_BLK_INLINE_ENCRYPTION */
  1417. static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
  1418. sector_t start, sector_t len, void *data)
  1419. {
  1420. unsigned long flush = (unsigned long) data;
  1421. struct request_queue *q = bdev_get_queue(dev->bdev);
  1422. return q && (q->queue_flags & flush);
  1423. }
  1424. static bool dm_table_supports_flush(struct dm_table *t, unsigned long flush)
  1425. {
  1426. struct dm_target *ti;
  1427. unsigned i;
  1428. /*
  1429. * Require at least one underlying device to support flushes.
  1430. * t->devices includes internal dm devices such as mirror logs
  1431. * so we need to use iterate_devices here, which targets
  1432. * supporting flushes must provide.
  1433. */
  1434. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1435. ti = dm_table_get_target(t, i);
  1436. if (!ti->num_flush_bios)
  1437. continue;
  1438. if (ti->flush_supported)
  1439. return true;
  1440. if (ti->type->iterate_devices &&
  1441. ti->type->iterate_devices(ti, device_flush_capable, (void *) flush))
  1442. return true;
  1443. }
  1444. return false;
  1445. }
  1446. static int device_dax_write_cache_enabled(struct dm_target *ti,
  1447. struct dm_dev *dev, sector_t start,
  1448. sector_t len, void *data)
  1449. {
  1450. struct dax_device *dax_dev = dev->dax_dev;
  1451. if (!dax_dev)
  1452. return false;
  1453. if (dax_write_cache_enabled(dax_dev))
  1454. return true;
  1455. return false;
  1456. }
  1457. static int device_is_rotational(struct dm_target *ti, struct dm_dev *dev,
  1458. sector_t start, sector_t len, void *data)
  1459. {
  1460. struct request_queue *q = bdev_get_queue(dev->bdev);
  1461. return q && !blk_queue_nonrot(q);
  1462. }
  1463. static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
  1464. sector_t start, sector_t len, void *data)
  1465. {
  1466. struct request_queue *q = bdev_get_queue(dev->bdev);
  1467. return q && !blk_queue_add_random(q);
  1468. }
  1469. static int queue_no_sg_merge(struct dm_target *ti, struct dm_dev *dev,
  1470. sector_t start, sector_t len, void *data)
  1471. {
  1472. struct request_queue *q = bdev_get_queue(dev->bdev);
  1473. return q && test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
  1474. }
  1475. static int device_not_write_same_capable(struct dm_target *ti, struct dm_dev *dev,
  1476. sector_t start, sector_t len, void *data)
  1477. {
  1478. struct request_queue *q = bdev_get_queue(dev->bdev);
  1479. return q && !q->limits.max_write_same_sectors;
  1480. }
  1481. static bool dm_table_supports_write_same(struct dm_table *t)
  1482. {
  1483. struct dm_target *ti;
  1484. unsigned i;
  1485. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1486. ti = dm_table_get_target(t, i);
  1487. if (!ti->num_write_same_bios)
  1488. return false;
  1489. if (!ti->type->iterate_devices ||
  1490. ti->type->iterate_devices(ti, device_not_write_same_capable, NULL))
  1491. return false;
  1492. }
  1493. return true;
  1494. }
  1495. static int device_not_write_zeroes_capable(struct dm_target *ti, struct dm_dev *dev,
  1496. sector_t start, sector_t len, void *data)
  1497. {
  1498. struct request_queue *q = bdev_get_queue(dev->bdev);
  1499. return q && !q->limits.max_write_zeroes_sectors;
  1500. }
  1501. static bool dm_table_supports_write_zeroes(struct dm_table *t)
  1502. {
  1503. struct dm_target *ti;
  1504. unsigned i = 0;
  1505. while (i < dm_table_get_num_targets(t)) {
  1506. ti = dm_table_get_target(t, i++);
  1507. if (!ti->num_write_zeroes_bios)
  1508. return false;
  1509. if (!ti->type->iterate_devices ||
  1510. ti->type->iterate_devices(ti, device_not_write_zeroes_capable, NULL))
  1511. return false;
  1512. }
  1513. return true;
  1514. }
  1515. static int device_not_discard_capable(struct dm_target *ti, struct dm_dev *dev,
  1516. sector_t start, sector_t len, void *data)
  1517. {
  1518. struct request_queue *q = bdev_get_queue(dev->bdev);
  1519. return q && !blk_queue_discard(q);
  1520. }
  1521. static bool dm_table_supports_discards(struct dm_table *t)
  1522. {
  1523. struct dm_target *ti;
  1524. unsigned i;
  1525. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1526. ti = dm_table_get_target(t, i);
  1527. if (!ti->num_discard_bios)
  1528. return false;
  1529. /*
  1530. * Either the target provides discard support (as implied by setting
  1531. * 'discards_supported') or it relies on _all_ data devices having
  1532. * discard support.
  1533. */
  1534. if (!ti->discards_supported &&
  1535. (!ti->type->iterate_devices ||
  1536. ti->type->iterate_devices(ti, device_not_discard_capable, NULL)))
  1537. return false;
  1538. }
  1539. return true;
  1540. }
  1541. static int device_requires_stable_pages(struct dm_target *ti,
  1542. struct dm_dev *dev, sector_t start,
  1543. sector_t len, void *data)
  1544. {
  1545. struct request_queue *q = bdev_get_queue(dev->bdev);
  1546. return q && bdi_cap_stable_pages_required(q->backing_dev_info);
  1547. }
  1548. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  1549. struct queue_limits *limits)
  1550. {
  1551. bool wc = false, fua = false;
  1552. /*
  1553. * Copy table's limits to the DM device's request_queue
  1554. */
  1555. q->limits = *limits;
  1556. if (!dm_table_supports_discards(t))
  1557. queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
  1558. else
  1559. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  1560. if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_WC))) {
  1561. wc = true;
  1562. if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_FUA)))
  1563. fua = true;
  1564. }
  1565. blk_queue_write_cache(q, wc, fua);
  1566. /* Inherit inline-crypt capability of underlying devices. */
  1567. if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_INLINECRYPT)))
  1568. queue_flag_set_unlocked(QUEUE_FLAG_INLINECRYPT, q);
  1569. if (dm_table_supports_dax(t))
  1570. queue_flag_set_unlocked(QUEUE_FLAG_DAX, q);
  1571. else
  1572. queue_flag_clear_unlocked(QUEUE_FLAG_DAX, q);
  1573. if (dm_table_any_dev_attr(t, device_dax_write_cache_enabled, NULL))
  1574. dax_write_cache(t->md->dax_dev, true);
  1575. /* Ensure that all underlying devices are non-rotational. */
  1576. if (dm_table_any_dev_attr(t, device_is_rotational, NULL))
  1577. queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
  1578. else
  1579. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  1580. if (!dm_table_supports_write_same(t))
  1581. q->limits.max_write_same_sectors = 0;
  1582. if (!dm_table_supports_write_zeroes(t))
  1583. q->limits.max_write_zeroes_sectors = 0;
  1584. if (dm_table_any_dev_attr(t, queue_no_sg_merge, NULL))
  1585. queue_flag_set_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
  1586. else
  1587. queue_flag_clear_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
  1588. dm_table_verify_integrity(t);
  1589. dm_calculate_supported_crypto_modes(t, q);
  1590. /*
  1591. * Some devices don't use blk_integrity but still want stable pages
  1592. * because they do their own checksumming.
  1593. * If any underlying device requires stable pages, a table must require
  1594. * them as well. Only targets that support iterate_devices are considered:
  1595. * don't want error, zero, etc to require stable pages.
  1596. */
  1597. if (dm_table_any_dev_attr(t, device_requires_stable_pages, NULL))
  1598. q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
  1599. else
  1600. q->backing_dev_info->capabilities &= ~BDI_CAP_STABLE_WRITES;
  1601. /*
  1602. * Determine whether or not this queue's I/O timings contribute
  1603. * to the entropy pool, Only request-based targets use this.
  1604. * Clear QUEUE_FLAG_ADD_RANDOM if any underlying device does not
  1605. * have it set.
  1606. */
  1607. if (blk_queue_add_random(q) &&
  1608. dm_table_any_dev_attr(t, device_is_not_random, NULL))
  1609. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, q);
  1610. /*
  1611. * QUEUE_FLAG_STACKABLE must be set after all queue settings are
  1612. * visible to other CPUs because, once the flag is set, incoming bios
  1613. * are processed by request-based dm, which refers to the queue
  1614. * settings.
  1615. * Until the flag set, bios are passed to bio-based dm and queued to
  1616. * md->deferred where queue settings are not needed yet.
  1617. * Those bios are passed to request-based dm at the resume time.
  1618. */
  1619. smp_mb();
  1620. if (dm_table_request_based(t))
  1621. queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
  1622. /* io_pages is used for readahead */
  1623. q->backing_dev_info->io_pages = limits->max_sectors >> (PAGE_SHIFT - 9);
  1624. }
  1625. unsigned int dm_table_get_num_targets(struct dm_table *t)
  1626. {
  1627. return t->num_targets;
  1628. }
  1629. struct list_head *dm_table_get_devices(struct dm_table *t)
  1630. {
  1631. return &t->devices;
  1632. }
  1633. fmode_t dm_table_get_mode(struct dm_table *t)
  1634. {
  1635. return t->mode;
  1636. }
  1637. EXPORT_SYMBOL(dm_table_get_mode);
  1638. enum suspend_mode {
  1639. PRESUSPEND,
  1640. PRESUSPEND_UNDO,
  1641. POSTSUSPEND,
  1642. };
  1643. static void suspend_targets(struct dm_table *t, enum suspend_mode mode)
  1644. {
  1645. int i = t->num_targets;
  1646. struct dm_target *ti = t->targets;
  1647. lockdep_assert_held(&t->md->suspend_lock);
  1648. while (i--) {
  1649. switch (mode) {
  1650. case PRESUSPEND:
  1651. if (ti->type->presuspend)
  1652. ti->type->presuspend(ti);
  1653. break;
  1654. case PRESUSPEND_UNDO:
  1655. if (ti->type->presuspend_undo)
  1656. ti->type->presuspend_undo(ti);
  1657. break;
  1658. case POSTSUSPEND:
  1659. if (ti->type->postsuspend)
  1660. ti->type->postsuspend(ti);
  1661. break;
  1662. }
  1663. ti++;
  1664. }
  1665. }
  1666. void dm_table_presuspend_targets(struct dm_table *t)
  1667. {
  1668. if (!t)
  1669. return;
  1670. suspend_targets(t, PRESUSPEND);
  1671. }
  1672. void dm_table_presuspend_undo_targets(struct dm_table *t)
  1673. {
  1674. if (!t)
  1675. return;
  1676. suspend_targets(t, PRESUSPEND_UNDO);
  1677. }
  1678. void dm_table_postsuspend_targets(struct dm_table *t)
  1679. {
  1680. if (!t)
  1681. return;
  1682. suspend_targets(t, POSTSUSPEND);
  1683. }
  1684. int dm_table_resume_targets(struct dm_table *t)
  1685. {
  1686. int i, r = 0;
  1687. lockdep_assert_held(&t->md->suspend_lock);
  1688. for (i = 0; i < t->num_targets; i++) {
  1689. struct dm_target *ti = t->targets + i;
  1690. if (!ti->type->preresume)
  1691. continue;
  1692. r = ti->type->preresume(ti);
  1693. if (r) {
  1694. DMERR("%s: %s: preresume failed, error = %d",
  1695. dm_device_name(t->md), ti->type->name, r);
  1696. return r;
  1697. }
  1698. }
  1699. for (i = 0; i < t->num_targets; i++) {
  1700. struct dm_target *ti = t->targets + i;
  1701. if (ti->type->resume)
  1702. ti->type->resume(ti);
  1703. }
  1704. return 0;
  1705. }
  1706. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb)
  1707. {
  1708. list_add(&cb->list, &t->target_callbacks);
  1709. }
  1710. EXPORT_SYMBOL_GPL(dm_table_add_target_callbacks);
  1711. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  1712. {
  1713. struct dm_dev_internal *dd;
  1714. struct list_head *devices = dm_table_get_devices(t);
  1715. struct dm_target_callbacks *cb;
  1716. int r = 0;
  1717. list_for_each_entry(dd, devices, list) {
  1718. struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
  1719. char b[BDEVNAME_SIZE];
  1720. if (likely(q))
  1721. r |= bdi_congested(q->backing_dev_info, bdi_bits);
  1722. else
  1723. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  1724. dm_device_name(t->md),
  1725. bdevname(dd->dm_dev->bdev, b));
  1726. }
  1727. list_for_each_entry(cb, &t->target_callbacks, list)
  1728. if (cb->congested_fn)
  1729. r |= cb->congested_fn(cb, bdi_bits);
  1730. return r;
  1731. }
  1732. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1733. {
  1734. return t->md;
  1735. }
  1736. EXPORT_SYMBOL(dm_table_get_md);
  1737. void dm_table_run_md_queue_async(struct dm_table *t)
  1738. {
  1739. struct mapped_device *md;
  1740. struct request_queue *queue;
  1741. unsigned long flags;
  1742. if (!dm_table_request_based(t))
  1743. return;
  1744. md = dm_table_get_md(t);
  1745. queue = dm_get_md_queue(md);
  1746. if (queue) {
  1747. if (queue->mq_ops)
  1748. blk_mq_run_hw_queues(queue, true);
  1749. else {
  1750. spin_lock_irqsave(queue->queue_lock, flags);
  1751. blk_run_queue_async(queue);
  1752. spin_unlock_irqrestore(queue->queue_lock, flags);
  1753. }
  1754. }
  1755. }
  1756. EXPORT_SYMBOL(dm_table_run_md_queue_async);