dm-table.c 51 KB

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