dm-table.c 51 KB

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