dm-table.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  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 *array_size, char **old_argv)
  456. {
  457. char **argv;
  458. unsigned new_size;
  459. gfp_t gfp;
  460. if (*array_size) {
  461. new_size = *array_size * 2;
  462. gfp = GFP_KERNEL;
  463. } else {
  464. new_size = 8;
  465. gfp = GFP_NOIO;
  466. }
  467. argv = kmalloc(new_size * sizeof(*argv), gfp);
  468. if (argv) {
  469. memcpy(argv, old_argv, *array_size * sizeof(*argv));
  470. *array_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. struct request_queue *q = bdev_get_queue(dev->bdev);
  736. return q && blk_queue_dax(q);
  737. }
  738. static bool dm_table_supports_dax(struct dm_table *t)
  739. {
  740. struct dm_target *ti;
  741. unsigned i;
  742. /* Ensure that all targets support DAX. */
  743. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  744. ti = dm_table_get_target(t, i);
  745. if (!ti->type->direct_access)
  746. return false;
  747. if (!ti->type->iterate_devices ||
  748. !ti->type->iterate_devices(ti, device_supports_dax, NULL))
  749. return false;
  750. }
  751. return true;
  752. }
  753. static bool dm_table_does_not_support_partial_completion(struct dm_table *t);
  754. struct verify_rq_based_data {
  755. unsigned sq_count;
  756. unsigned mq_count;
  757. };
  758. static int device_is_rq_based(struct dm_target *ti, struct dm_dev *dev,
  759. sector_t start, sector_t len, void *data)
  760. {
  761. struct request_queue *q = bdev_get_queue(dev->bdev);
  762. struct verify_rq_based_data *v = data;
  763. if (q->mq_ops)
  764. v->mq_count++;
  765. else
  766. v->sq_count++;
  767. return queue_is_rq_based(q);
  768. }
  769. static int dm_table_determine_type(struct dm_table *t)
  770. {
  771. unsigned i;
  772. unsigned bio_based = 0, request_based = 0, hybrid = 0;
  773. struct verify_rq_based_data v = {.sq_count = 0, .mq_count = 0};
  774. struct dm_target *tgt;
  775. struct list_head *devices = dm_table_get_devices(t);
  776. enum dm_queue_mode live_md_type = dm_get_md_type(t->md);
  777. if (t->type != DM_TYPE_NONE) {
  778. /* target already set the table's type */
  779. if (t->type == DM_TYPE_BIO_BASED) {
  780. /* possibly upgrade to a variant of bio-based */
  781. goto verify_bio_based;
  782. }
  783. BUG_ON(t->type == DM_TYPE_DAX_BIO_BASED);
  784. BUG_ON(t->type == DM_TYPE_NVME_BIO_BASED);
  785. goto verify_rq_based;
  786. }
  787. for (i = 0; i < t->num_targets; i++) {
  788. tgt = t->targets + i;
  789. if (dm_target_hybrid(tgt))
  790. hybrid = 1;
  791. else if (dm_target_request_based(tgt))
  792. request_based = 1;
  793. else
  794. bio_based = 1;
  795. if (bio_based && request_based) {
  796. DMERR("Inconsistent table: different target types"
  797. " can't be mixed up");
  798. return -EINVAL;
  799. }
  800. }
  801. if (hybrid && !bio_based && !request_based) {
  802. /*
  803. * The targets can work either way.
  804. * Determine the type from the live device.
  805. * Default to bio-based if device is new.
  806. */
  807. if (__table_type_request_based(live_md_type))
  808. request_based = 1;
  809. else
  810. bio_based = 1;
  811. }
  812. if (bio_based) {
  813. verify_bio_based:
  814. /* We must use this table as bio-based */
  815. t->type = DM_TYPE_BIO_BASED;
  816. if (dm_table_supports_dax(t) ||
  817. (list_empty(devices) && live_md_type == DM_TYPE_DAX_BIO_BASED)) {
  818. t->type = DM_TYPE_DAX_BIO_BASED;
  819. } else {
  820. /* Check if upgrading to NVMe bio-based is valid or required */
  821. tgt = dm_table_get_immutable_target(t);
  822. if (tgt && !tgt->max_io_len && dm_table_does_not_support_partial_completion(t)) {
  823. t->type = DM_TYPE_NVME_BIO_BASED;
  824. goto verify_rq_based; /* must be stacked directly on NVMe (blk-mq) */
  825. } else if (list_empty(devices) && live_md_type == DM_TYPE_NVME_BIO_BASED) {
  826. t->type = DM_TYPE_NVME_BIO_BASED;
  827. }
  828. }
  829. return 0;
  830. }
  831. BUG_ON(!request_based); /* No targets in this table */
  832. /*
  833. * The only way to establish DM_TYPE_MQ_REQUEST_BASED is by
  834. * having a compatible target use dm_table_set_type.
  835. */
  836. t->type = DM_TYPE_REQUEST_BASED;
  837. verify_rq_based:
  838. /*
  839. * Request-based dm supports only tables that have a single target now.
  840. * To support multiple targets, request splitting support is needed,
  841. * and that needs lots of changes in the block-layer.
  842. * (e.g. request completion process for partial completion.)
  843. */
  844. if (t->num_targets > 1) {
  845. DMERR("%s DM doesn't support multiple targets",
  846. t->type == DM_TYPE_NVME_BIO_BASED ? "nvme bio-based" : "request-based");
  847. return -EINVAL;
  848. }
  849. if (list_empty(devices)) {
  850. int srcu_idx;
  851. struct dm_table *live_table = dm_get_live_table(t->md, &srcu_idx);
  852. /* inherit live table's type and all_blk_mq */
  853. if (live_table) {
  854. t->type = live_table->type;
  855. t->all_blk_mq = live_table->all_blk_mq;
  856. }
  857. dm_put_live_table(t->md, srcu_idx);
  858. return 0;
  859. }
  860. tgt = dm_table_get_immutable_target(t);
  861. if (!tgt) {
  862. DMERR("table load rejected: immutable target is required");
  863. return -EINVAL;
  864. } else if (tgt->max_io_len) {
  865. DMERR("table load rejected: immutable target that splits IO is not supported");
  866. return -EINVAL;
  867. }
  868. /* Non-request-stackable devices can't be used for request-based dm */
  869. if (!tgt->type->iterate_devices ||
  870. !tgt->type->iterate_devices(tgt, device_is_rq_based, &v)) {
  871. DMERR("table load rejected: including non-request-stackable devices");
  872. return -EINVAL;
  873. }
  874. if (v.sq_count && v.mq_count) {
  875. DMERR("table load rejected: not all devices are blk-mq request-stackable");
  876. return -EINVAL;
  877. }
  878. t->all_blk_mq = v.mq_count > 0;
  879. if (!t->all_blk_mq &&
  880. (t->type == DM_TYPE_MQ_REQUEST_BASED || t->type == DM_TYPE_NVME_BIO_BASED)) {
  881. DMERR("table load rejected: all devices are not blk-mq request-stackable");
  882. return -EINVAL;
  883. }
  884. return 0;
  885. }
  886. enum dm_queue_mode dm_table_get_type(struct dm_table *t)
  887. {
  888. return t->type;
  889. }
  890. struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
  891. {
  892. return t->immutable_target_type;
  893. }
  894. struct dm_target *dm_table_get_immutable_target(struct dm_table *t)
  895. {
  896. /* Immutable target is implicitly a singleton */
  897. if (t->num_targets > 1 ||
  898. !dm_target_is_immutable(t->targets[0].type))
  899. return NULL;
  900. return t->targets;
  901. }
  902. struct dm_target *dm_table_get_wildcard_target(struct dm_table *t)
  903. {
  904. struct dm_target *ti;
  905. unsigned i;
  906. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  907. ti = dm_table_get_target(t, i);
  908. if (dm_target_is_wildcard(ti->type))
  909. return ti;
  910. }
  911. return NULL;
  912. }
  913. bool dm_table_bio_based(struct dm_table *t)
  914. {
  915. return __table_type_bio_based(dm_table_get_type(t));
  916. }
  917. bool dm_table_request_based(struct dm_table *t)
  918. {
  919. return __table_type_request_based(dm_table_get_type(t));
  920. }
  921. bool dm_table_all_blk_mq_devices(struct dm_table *t)
  922. {
  923. return t->all_blk_mq;
  924. }
  925. static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *md)
  926. {
  927. enum dm_queue_mode type = dm_table_get_type(t);
  928. unsigned per_io_data_size = 0;
  929. unsigned min_pool_size = 0;
  930. struct dm_target *ti;
  931. unsigned i;
  932. if (unlikely(type == DM_TYPE_NONE)) {
  933. DMWARN("no table type is set, can't allocate mempools");
  934. return -EINVAL;
  935. }
  936. if (__table_type_bio_based(type))
  937. for (i = 0; i < t->num_targets; i++) {
  938. ti = t->targets + i;
  939. per_io_data_size = max(per_io_data_size, ti->per_io_data_size);
  940. min_pool_size = max(min_pool_size, ti->num_flush_bios);
  941. }
  942. t->mempools = dm_alloc_md_mempools(md, type, t->integrity_supported,
  943. per_io_data_size, min_pool_size);
  944. if (!t->mempools)
  945. return -ENOMEM;
  946. return 0;
  947. }
  948. void dm_table_free_md_mempools(struct dm_table *t)
  949. {
  950. dm_free_md_mempools(t->mempools);
  951. t->mempools = NULL;
  952. }
  953. struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
  954. {
  955. return t->mempools;
  956. }
  957. static int setup_indexes(struct dm_table *t)
  958. {
  959. int i;
  960. unsigned int total = 0;
  961. sector_t *indexes;
  962. /* allocate the space for *all* the indexes */
  963. for (i = t->depth - 2; i >= 0; i--) {
  964. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  965. total += t->counts[i];
  966. }
  967. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  968. if (!indexes)
  969. return -ENOMEM;
  970. /* set up internal nodes, bottom-up */
  971. for (i = t->depth - 2; i >= 0; i--) {
  972. t->index[i] = indexes;
  973. indexes += (KEYS_PER_NODE * t->counts[i]);
  974. setup_btree_index(i, t);
  975. }
  976. return 0;
  977. }
  978. /*
  979. * Builds the btree to index the map.
  980. */
  981. static int dm_table_build_index(struct dm_table *t)
  982. {
  983. int r = 0;
  984. unsigned int leaf_nodes;
  985. /* how many indexes will the btree have ? */
  986. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  987. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  988. /* leaf layer has already been set up */
  989. t->counts[t->depth - 1] = leaf_nodes;
  990. t->index[t->depth - 1] = t->highs;
  991. if (t->depth >= 2)
  992. r = setup_indexes(t);
  993. return r;
  994. }
  995. static bool integrity_profile_exists(struct gendisk *disk)
  996. {
  997. return !!blk_get_integrity(disk);
  998. }
  999. /*
  1000. * Get a disk whose integrity profile reflects the table's profile.
  1001. * Returns NULL if integrity support was inconsistent or unavailable.
  1002. */
  1003. static struct gendisk * dm_table_get_integrity_disk(struct dm_table *t)
  1004. {
  1005. struct list_head *devices = dm_table_get_devices(t);
  1006. struct dm_dev_internal *dd = NULL;
  1007. struct gendisk *prev_disk = NULL, *template_disk = NULL;
  1008. unsigned i;
  1009. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1010. struct dm_target *ti = dm_table_get_target(t, i);
  1011. if (!dm_target_passes_integrity(ti->type))
  1012. goto no_integrity;
  1013. }
  1014. list_for_each_entry(dd, devices, list) {
  1015. template_disk = dd->dm_dev->bdev->bd_disk;
  1016. if (!integrity_profile_exists(template_disk))
  1017. goto no_integrity;
  1018. else if (prev_disk &&
  1019. blk_integrity_compare(prev_disk, template_disk) < 0)
  1020. goto no_integrity;
  1021. prev_disk = template_disk;
  1022. }
  1023. return template_disk;
  1024. no_integrity:
  1025. if (prev_disk)
  1026. DMWARN("%s: integrity not set: %s and %s profile mismatch",
  1027. dm_device_name(t->md),
  1028. prev_disk->disk_name,
  1029. template_disk->disk_name);
  1030. return NULL;
  1031. }
  1032. /*
  1033. * Register the mapped device for blk_integrity support if the
  1034. * underlying devices have an integrity profile. But all devices may
  1035. * not have matching profiles (checking all devices isn't reliable
  1036. * during table load because this table may use other DM device(s) which
  1037. * must be resumed before they will have an initialized integity
  1038. * profile). Consequently, stacked DM devices force a 2 stage integrity
  1039. * profile validation: First pass during table load, final pass during
  1040. * resume.
  1041. */
  1042. static int dm_table_register_integrity(struct dm_table *t)
  1043. {
  1044. struct mapped_device *md = t->md;
  1045. struct gendisk *template_disk = NULL;
  1046. /* If target handles integrity itself do not register it here. */
  1047. if (t->integrity_added)
  1048. return 0;
  1049. template_disk = dm_table_get_integrity_disk(t);
  1050. if (!template_disk)
  1051. return 0;
  1052. if (!integrity_profile_exists(dm_disk(md))) {
  1053. t->integrity_supported = true;
  1054. /*
  1055. * Register integrity profile during table load; we can do
  1056. * this because the final profile must match during resume.
  1057. */
  1058. blk_integrity_register(dm_disk(md),
  1059. blk_get_integrity(template_disk));
  1060. return 0;
  1061. }
  1062. /*
  1063. * If DM device already has an initialized integrity
  1064. * profile the new profile should not conflict.
  1065. */
  1066. if (blk_integrity_compare(dm_disk(md), template_disk) < 0) {
  1067. DMWARN("%s: conflict with existing integrity profile: "
  1068. "%s profile mismatch",
  1069. dm_device_name(t->md),
  1070. template_disk->disk_name);
  1071. return 1;
  1072. }
  1073. /* Preserve existing integrity profile */
  1074. t->integrity_supported = true;
  1075. return 0;
  1076. }
  1077. /*
  1078. * Prepares the table for use by building the indices,
  1079. * setting the type, and allocating mempools.
  1080. */
  1081. int dm_table_complete(struct dm_table *t)
  1082. {
  1083. int r;
  1084. r = dm_table_determine_type(t);
  1085. if (r) {
  1086. DMERR("unable to determine table type");
  1087. return r;
  1088. }
  1089. r = dm_table_build_index(t);
  1090. if (r) {
  1091. DMERR("unable to build btrees");
  1092. return r;
  1093. }
  1094. r = dm_table_register_integrity(t);
  1095. if (r) {
  1096. DMERR("could not register integrity profile.");
  1097. return r;
  1098. }
  1099. r = dm_table_alloc_md_mempools(t, t->md);
  1100. if (r)
  1101. DMERR("unable to allocate mempools");
  1102. return r;
  1103. }
  1104. static DEFINE_MUTEX(_event_lock);
  1105. void dm_table_event_callback(struct dm_table *t,
  1106. void (*fn)(void *), void *context)
  1107. {
  1108. mutex_lock(&_event_lock);
  1109. t->event_fn = fn;
  1110. t->event_context = context;
  1111. mutex_unlock(&_event_lock);
  1112. }
  1113. void dm_table_event(struct dm_table *t)
  1114. {
  1115. /*
  1116. * You can no longer call dm_table_event() from interrupt
  1117. * context, use a bottom half instead.
  1118. */
  1119. BUG_ON(in_interrupt());
  1120. mutex_lock(&_event_lock);
  1121. if (t->event_fn)
  1122. t->event_fn(t->event_context);
  1123. mutex_unlock(&_event_lock);
  1124. }
  1125. EXPORT_SYMBOL(dm_table_event);
  1126. sector_t dm_table_get_size(struct dm_table *t)
  1127. {
  1128. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  1129. }
  1130. EXPORT_SYMBOL(dm_table_get_size);
  1131. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  1132. {
  1133. if (index >= t->num_targets)
  1134. return NULL;
  1135. return t->targets + index;
  1136. }
  1137. /*
  1138. * Search the btree for the correct target.
  1139. *
  1140. * Caller should check returned pointer with dm_target_is_valid()
  1141. * to trap I/O beyond end of device.
  1142. */
  1143. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  1144. {
  1145. unsigned int l, n = 0, k = 0;
  1146. sector_t *node;
  1147. for (l = 0; l < t->depth; l++) {
  1148. n = get_child(n, k);
  1149. node = get_node(t, l, n);
  1150. for (k = 0; k < KEYS_PER_NODE; k++)
  1151. if (node[k] >= sector)
  1152. break;
  1153. }
  1154. return &t->targets[(KEYS_PER_NODE * n) + k];
  1155. }
  1156. static int count_device(struct dm_target *ti, struct dm_dev *dev,
  1157. sector_t start, sector_t len, void *data)
  1158. {
  1159. unsigned *num_devices = data;
  1160. (*num_devices)++;
  1161. return 0;
  1162. }
  1163. /*
  1164. * Check whether a table has no data devices attached using each
  1165. * target's iterate_devices method.
  1166. * Returns false if the result is unknown because a target doesn't
  1167. * support iterate_devices.
  1168. */
  1169. bool dm_table_has_no_data_devices(struct dm_table *table)
  1170. {
  1171. struct dm_target *ti;
  1172. unsigned i, num_devices;
  1173. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  1174. ti = dm_table_get_target(table, i);
  1175. if (!ti->type->iterate_devices)
  1176. return false;
  1177. num_devices = 0;
  1178. ti->type->iterate_devices(ti, count_device, &num_devices);
  1179. if (num_devices)
  1180. return false;
  1181. }
  1182. return true;
  1183. }
  1184. static int device_is_zoned_model(struct dm_target *ti, struct dm_dev *dev,
  1185. sector_t start, sector_t len, void *data)
  1186. {
  1187. struct request_queue *q = bdev_get_queue(dev->bdev);
  1188. enum blk_zoned_model *zoned_model = data;
  1189. return q && blk_queue_zoned_model(q) == *zoned_model;
  1190. }
  1191. static bool dm_table_supports_zoned_model(struct dm_table *t,
  1192. enum blk_zoned_model zoned_model)
  1193. {
  1194. struct dm_target *ti;
  1195. unsigned i;
  1196. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1197. ti = dm_table_get_target(t, i);
  1198. if (zoned_model == BLK_ZONED_HM &&
  1199. !dm_target_supports_zoned_hm(ti->type))
  1200. return false;
  1201. if (!ti->type->iterate_devices ||
  1202. !ti->type->iterate_devices(ti, device_is_zoned_model, &zoned_model))
  1203. return false;
  1204. }
  1205. return true;
  1206. }
  1207. static int device_matches_zone_sectors(struct dm_target *ti, struct dm_dev *dev,
  1208. sector_t start, sector_t len, void *data)
  1209. {
  1210. struct request_queue *q = bdev_get_queue(dev->bdev);
  1211. unsigned int *zone_sectors = data;
  1212. return q && blk_queue_zone_sectors(q) == *zone_sectors;
  1213. }
  1214. static bool dm_table_matches_zone_sectors(struct dm_table *t,
  1215. unsigned int zone_sectors)
  1216. {
  1217. struct dm_target *ti;
  1218. unsigned i;
  1219. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1220. ti = dm_table_get_target(t, i);
  1221. if (!ti->type->iterate_devices ||
  1222. !ti->type->iterate_devices(ti, device_matches_zone_sectors, &zone_sectors))
  1223. return false;
  1224. }
  1225. return true;
  1226. }
  1227. static int validate_hardware_zoned_model(struct dm_table *table,
  1228. enum blk_zoned_model zoned_model,
  1229. unsigned int zone_sectors)
  1230. {
  1231. if (zoned_model == BLK_ZONED_NONE)
  1232. return 0;
  1233. if (!dm_table_supports_zoned_model(table, zoned_model)) {
  1234. DMERR("%s: zoned model is not consistent across all devices",
  1235. dm_device_name(table->md));
  1236. return -EINVAL;
  1237. }
  1238. /* Check zone size validity and compatibility */
  1239. if (!zone_sectors || !is_power_of_2(zone_sectors))
  1240. return -EINVAL;
  1241. if (!dm_table_matches_zone_sectors(table, zone_sectors)) {
  1242. DMERR("%s: zone sectors is not consistent across all devices",
  1243. dm_device_name(table->md));
  1244. return -EINVAL;
  1245. }
  1246. return 0;
  1247. }
  1248. /*
  1249. * Establish the new table's queue_limits and validate them.
  1250. */
  1251. int dm_calculate_queue_limits(struct dm_table *table,
  1252. struct queue_limits *limits)
  1253. {
  1254. struct dm_target *ti;
  1255. struct queue_limits ti_limits;
  1256. unsigned i;
  1257. enum blk_zoned_model zoned_model = BLK_ZONED_NONE;
  1258. unsigned int zone_sectors = 0;
  1259. blk_set_stacking_limits(limits);
  1260. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  1261. blk_set_stacking_limits(&ti_limits);
  1262. ti = dm_table_get_target(table, i);
  1263. if (!ti->type->iterate_devices)
  1264. goto combine_limits;
  1265. /*
  1266. * Combine queue limits of all the devices this target uses.
  1267. */
  1268. ti->type->iterate_devices(ti, dm_set_device_limits,
  1269. &ti_limits);
  1270. if (zoned_model == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
  1271. /*
  1272. * After stacking all limits, validate all devices
  1273. * in table support this zoned model and zone sectors.
  1274. */
  1275. zoned_model = ti_limits.zoned;
  1276. zone_sectors = ti_limits.chunk_sectors;
  1277. }
  1278. /* Set I/O hints portion of queue limits */
  1279. if (ti->type->io_hints)
  1280. ti->type->io_hints(ti, &ti_limits);
  1281. /*
  1282. * Check each device area is consistent with the target's
  1283. * overall queue limits.
  1284. */
  1285. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  1286. &ti_limits))
  1287. return -EINVAL;
  1288. combine_limits:
  1289. /*
  1290. * Merge this target's queue limits into the overall limits
  1291. * for the table.
  1292. */
  1293. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  1294. DMWARN("%s: adding target device "
  1295. "(start sect %llu len %llu) "
  1296. "caused an alignment inconsistency",
  1297. dm_device_name(table->md),
  1298. (unsigned long long) ti->begin,
  1299. (unsigned long long) ti->len);
  1300. /*
  1301. * FIXME: this should likely be moved to blk_stack_limits(), would
  1302. * also eliminate limits->zoned stacking hack in dm_set_device_limits()
  1303. */
  1304. if (limits->zoned == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
  1305. /*
  1306. * By default, the stacked limits zoned model is set to
  1307. * BLK_ZONED_NONE in blk_set_stacking_limits(). Update
  1308. * this model using the first target model reported
  1309. * that is not BLK_ZONED_NONE. This will be either the
  1310. * first target device zoned model or the model reported
  1311. * by the target .io_hints.
  1312. */
  1313. limits->zoned = ti_limits.zoned;
  1314. }
  1315. }
  1316. /*
  1317. * Verify that the zoned model and zone sectors, as determined before
  1318. * any .io_hints override, are the same across all devices in the table.
  1319. * - this is especially relevant if .io_hints is emulating a disk-managed
  1320. * zoned model (aka BLK_ZONED_NONE) on host-managed zoned block devices.
  1321. * BUT...
  1322. */
  1323. if (limits->zoned != BLK_ZONED_NONE) {
  1324. /*
  1325. * ...IF the above limits stacking determined a zoned model
  1326. * validate that all of the table's devices conform to it.
  1327. */
  1328. zoned_model = limits->zoned;
  1329. zone_sectors = limits->chunk_sectors;
  1330. }
  1331. if (validate_hardware_zoned_model(table, zoned_model, zone_sectors))
  1332. return -EINVAL;
  1333. return validate_hardware_logical_block_alignment(table, limits);
  1334. }
  1335. /*
  1336. * Verify that all devices have an integrity profile that matches the
  1337. * DM device's registered integrity profile. If the profiles don't
  1338. * match then unregister the DM device's integrity profile.
  1339. */
  1340. static void dm_table_verify_integrity(struct dm_table *t)
  1341. {
  1342. struct gendisk *template_disk = NULL;
  1343. if (t->integrity_added)
  1344. return;
  1345. if (t->integrity_supported) {
  1346. /*
  1347. * Verify that the original integrity profile
  1348. * matches all the devices in this table.
  1349. */
  1350. template_disk = dm_table_get_integrity_disk(t);
  1351. if (template_disk &&
  1352. blk_integrity_compare(dm_disk(t->md), template_disk) >= 0)
  1353. return;
  1354. }
  1355. if (integrity_profile_exists(dm_disk(t->md))) {
  1356. DMWARN("%s: unable to establish an integrity profile",
  1357. dm_device_name(t->md));
  1358. blk_integrity_unregister(dm_disk(t->md));
  1359. }
  1360. }
  1361. static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
  1362. sector_t start, sector_t len, void *data)
  1363. {
  1364. unsigned long flush = (unsigned long) data;
  1365. struct request_queue *q = bdev_get_queue(dev->bdev);
  1366. return q && (q->queue_flags & flush);
  1367. }
  1368. static bool dm_table_supports_flush(struct dm_table *t, unsigned long flush)
  1369. {
  1370. struct dm_target *ti;
  1371. unsigned i;
  1372. /*
  1373. * Require at least one underlying device to support flushes.
  1374. * t->devices includes internal dm devices such as mirror logs
  1375. * so we need to use iterate_devices here, which targets
  1376. * supporting flushes must provide.
  1377. */
  1378. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1379. ti = dm_table_get_target(t, i);
  1380. if (!ti->num_flush_bios)
  1381. continue;
  1382. if (ti->flush_supported)
  1383. return true;
  1384. if (ti->type->iterate_devices &&
  1385. ti->type->iterate_devices(ti, device_flush_capable, (void *) flush))
  1386. return true;
  1387. }
  1388. return false;
  1389. }
  1390. static int device_dax_write_cache_enabled(struct dm_target *ti,
  1391. struct dm_dev *dev, sector_t start,
  1392. sector_t len, void *data)
  1393. {
  1394. struct dax_device *dax_dev = dev->dax_dev;
  1395. if (!dax_dev)
  1396. return false;
  1397. if (dax_write_cache_enabled(dax_dev))
  1398. return true;
  1399. return false;
  1400. }
  1401. static int dm_table_supports_dax_write_cache(struct dm_table *t)
  1402. {
  1403. struct dm_target *ti;
  1404. unsigned i;
  1405. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1406. ti = dm_table_get_target(t, i);
  1407. if (ti->type->iterate_devices &&
  1408. ti->type->iterate_devices(ti,
  1409. device_dax_write_cache_enabled, NULL))
  1410. return true;
  1411. }
  1412. return false;
  1413. }
  1414. static int device_is_nonrot(struct dm_target *ti, struct dm_dev *dev,
  1415. sector_t start, sector_t len, void *data)
  1416. {
  1417. struct request_queue *q = bdev_get_queue(dev->bdev);
  1418. return q && blk_queue_nonrot(q);
  1419. }
  1420. static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
  1421. sector_t start, sector_t len, void *data)
  1422. {
  1423. struct request_queue *q = bdev_get_queue(dev->bdev);
  1424. return q && !blk_queue_add_random(q);
  1425. }
  1426. static int queue_supports_sg_merge(struct dm_target *ti, struct dm_dev *dev,
  1427. sector_t start, sector_t len, void *data)
  1428. {
  1429. struct request_queue *q = bdev_get_queue(dev->bdev);
  1430. return q && !test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
  1431. }
  1432. static bool dm_table_all_devices_attribute(struct dm_table *t,
  1433. iterate_devices_callout_fn func)
  1434. {
  1435. struct dm_target *ti;
  1436. unsigned i;
  1437. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1438. ti = dm_table_get_target(t, i);
  1439. if (!ti->type->iterate_devices ||
  1440. !ti->type->iterate_devices(ti, func, NULL))
  1441. return false;
  1442. }
  1443. return true;
  1444. }
  1445. static int device_no_partial_completion(struct dm_target *ti, struct dm_dev *dev,
  1446. sector_t start, sector_t len, void *data)
  1447. {
  1448. char b[BDEVNAME_SIZE];
  1449. /* For now, NVMe devices are the only devices of this class */
  1450. return (strncmp(bdevname(dev->bdev, b), "nvme", 4) == 0);
  1451. }
  1452. static bool dm_table_does_not_support_partial_completion(struct dm_table *t)
  1453. {
  1454. return dm_table_all_devices_attribute(t, device_no_partial_completion);
  1455. }
  1456. static int device_not_write_same_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_same_sectors;
  1461. }
  1462. static bool dm_table_supports_write_same(struct dm_table *t)
  1463. {
  1464. struct dm_target *ti;
  1465. unsigned i;
  1466. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1467. ti = dm_table_get_target(t, i);
  1468. if (!ti->num_write_same_bios)
  1469. return false;
  1470. if (!ti->type->iterate_devices ||
  1471. ti->type->iterate_devices(ti, device_not_write_same_capable, NULL))
  1472. return false;
  1473. }
  1474. return true;
  1475. }
  1476. static int device_not_write_zeroes_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 && !q->limits.max_write_zeroes_sectors;
  1481. }
  1482. static bool dm_table_supports_write_zeroes(struct dm_table *t)
  1483. {
  1484. struct dm_target *ti;
  1485. unsigned i = 0;
  1486. while (i < dm_table_get_num_targets(t)) {
  1487. ti = dm_table_get_target(t, i++);
  1488. if (!ti->num_write_zeroes_bios)
  1489. return false;
  1490. if (!ti->type->iterate_devices ||
  1491. ti->type->iterate_devices(ti, device_not_write_zeroes_capable, NULL))
  1492. return false;
  1493. }
  1494. return true;
  1495. }
  1496. static int device_not_discard_capable(struct dm_target *ti, struct dm_dev *dev,
  1497. sector_t start, sector_t len, void *data)
  1498. {
  1499. struct request_queue *q = bdev_get_queue(dev->bdev);
  1500. return q && !blk_queue_discard(q);
  1501. }
  1502. static bool dm_table_supports_discards(struct dm_table *t)
  1503. {
  1504. struct dm_target *ti;
  1505. unsigned i;
  1506. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1507. ti = dm_table_get_target(t, i);
  1508. if (!ti->num_discard_bios)
  1509. return false;
  1510. /*
  1511. * Either the target provides discard support (as implied by setting
  1512. * 'discards_supported') or it relies on _all_ data devices having
  1513. * discard support.
  1514. */
  1515. if (!ti->discards_supported &&
  1516. (!ti->type->iterate_devices ||
  1517. ti->type->iterate_devices(ti, device_not_discard_capable, NULL)))
  1518. return false;
  1519. }
  1520. return true;
  1521. }
  1522. static int device_not_secure_erase_capable(struct dm_target *ti,
  1523. struct dm_dev *dev, sector_t start,
  1524. sector_t len, void *data)
  1525. {
  1526. struct request_queue *q = bdev_get_queue(dev->bdev);
  1527. return q && !blk_queue_secure_erase(q);
  1528. }
  1529. static bool dm_table_supports_secure_erase(struct dm_table *t)
  1530. {
  1531. struct dm_target *ti;
  1532. unsigned int i;
  1533. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1534. ti = dm_table_get_target(t, i);
  1535. if (!ti->num_secure_erase_bios)
  1536. return false;
  1537. if (!ti->type->iterate_devices ||
  1538. ti->type->iterate_devices(ti, device_not_secure_erase_capable, NULL))
  1539. return false;
  1540. }
  1541. return true;
  1542. }
  1543. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  1544. struct queue_limits *limits)
  1545. {
  1546. bool wc = false, fua = false;
  1547. /*
  1548. * Copy table's limits to the DM device's request_queue
  1549. */
  1550. q->limits = *limits;
  1551. if (!dm_table_supports_discards(t)) {
  1552. blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
  1553. /* Must also clear discard limits... */
  1554. q->limits.max_discard_sectors = 0;
  1555. q->limits.max_hw_discard_sectors = 0;
  1556. q->limits.discard_granularity = 0;
  1557. q->limits.discard_alignment = 0;
  1558. q->limits.discard_misaligned = 0;
  1559. } else
  1560. blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
  1561. if (dm_table_supports_secure_erase(t))
  1562. blk_queue_flag_set(QUEUE_FLAG_SECERASE, q);
  1563. if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_WC))) {
  1564. wc = true;
  1565. if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_FUA)))
  1566. fua = true;
  1567. }
  1568. blk_queue_write_cache(q, wc, fua);
  1569. if (dm_table_supports_dax(t))
  1570. blk_queue_flag_set(QUEUE_FLAG_DAX, q);
  1571. if (dm_table_supports_dax_write_cache(t))
  1572. dax_write_cache(t->md->dax_dev, true);
  1573. /* Ensure that all underlying devices are non-rotational. */
  1574. if (dm_table_all_devices_attribute(t, device_is_nonrot))
  1575. blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
  1576. else
  1577. blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
  1578. if (!dm_table_supports_write_same(t))
  1579. q->limits.max_write_same_sectors = 0;
  1580. if (!dm_table_supports_write_zeroes(t))
  1581. q->limits.max_write_zeroes_sectors = 0;
  1582. if (dm_table_all_devices_attribute(t, queue_supports_sg_merge))
  1583. blk_queue_flag_clear(QUEUE_FLAG_NO_SG_MERGE, q);
  1584. else
  1585. blk_queue_flag_set(QUEUE_FLAG_NO_SG_MERGE, q);
  1586. dm_table_verify_integrity(t);
  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) && dm_table_all_devices_attribute(t, device_is_not_random))
  1594. blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q);
  1595. }
  1596. unsigned int dm_table_get_num_targets(struct dm_table *t)
  1597. {
  1598. return t->num_targets;
  1599. }
  1600. struct list_head *dm_table_get_devices(struct dm_table *t)
  1601. {
  1602. return &t->devices;
  1603. }
  1604. fmode_t dm_table_get_mode(struct dm_table *t)
  1605. {
  1606. return t->mode;
  1607. }
  1608. EXPORT_SYMBOL(dm_table_get_mode);
  1609. enum suspend_mode {
  1610. PRESUSPEND,
  1611. PRESUSPEND_UNDO,
  1612. POSTSUSPEND,
  1613. };
  1614. static void suspend_targets(struct dm_table *t, enum suspend_mode mode)
  1615. {
  1616. int i = t->num_targets;
  1617. struct dm_target *ti = t->targets;
  1618. lockdep_assert_held(&t->md->suspend_lock);
  1619. while (i--) {
  1620. switch (mode) {
  1621. case PRESUSPEND:
  1622. if (ti->type->presuspend)
  1623. ti->type->presuspend(ti);
  1624. break;
  1625. case PRESUSPEND_UNDO:
  1626. if (ti->type->presuspend_undo)
  1627. ti->type->presuspend_undo(ti);
  1628. break;
  1629. case POSTSUSPEND:
  1630. if (ti->type->postsuspend)
  1631. ti->type->postsuspend(ti);
  1632. break;
  1633. }
  1634. ti++;
  1635. }
  1636. }
  1637. void dm_table_presuspend_targets(struct dm_table *t)
  1638. {
  1639. if (!t)
  1640. return;
  1641. suspend_targets(t, PRESUSPEND);
  1642. }
  1643. void dm_table_presuspend_undo_targets(struct dm_table *t)
  1644. {
  1645. if (!t)
  1646. return;
  1647. suspend_targets(t, PRESUSPEND_UNDO);
  1648. }
  1649. void dm_table_postsuspend_targets(struct dm_table *t)
  1650. {
  1651. if (!t)
  1652. return;
  1653. suspend_targets(t, POSTSUSPEND);
  1654. }
  1655. int dm_table_resume_targets(struct dm_table *t)
  1656. {
  1657. int i, r = 0;
  1658. lockdep_assert_held(&t->md->suspend_lock);
  1659. for (i = 0; i < t->num_targets; i++) {
  1660. struct dm_target *ti = t->targets + i;
  1661. if (!ti->type->preresume)
  1662. continue;
  1663. r = ti->type->preresume(ti);
  1664. if (r) {
  1665. DMERR("%s: %s: preresume failed, error = %d",
  1666. dm_device_name(t->md), ti->type->name, r);
  1667. return r;
  1668. }
  1669. }
  1670. for (i = 0; i < t->num_targets; i++) {
  1671. struct dm_target *ti = t->targets + i;
  1672. if (ti->type->resume)
  1673. ti->type->resume(ti);
  1674. }
  1675. return 0;
  1676. }
  1677. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb)
  1678. {
  1679. list_add(&cb->list, &t->target_callbacks);
  1680. }
  1681. EXPORT_SYMBOL_GPL(dm_table_add_target_callbacks);
  1682. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  1683. {
  1684. struct dm_dev_internal *dd;
  1685. struct list_head *devices = dm_table_get_devices(t);
  1686. struct dm_target_callbacks *cb;
  1687. int r = 0;
  1688. list_for_each_entry(dd, devices, list) {
  1689. struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
  1690. char b[BDEVNAME_SIZE];
  1691. if (likely(q))
  1692. r |= bdi_congested(q->backing_dev_info, bdi_bits);
  1693. else
  1694. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  1695. dm_device_name(t->md),
  1696. bdevname(dd->dm_dev->bdev, b));
  1697. }
  1698. list_for_each_entry(cb, &t->target_callbacks, list)
  1699. if (cb->congested_fn)
  1700. r |= cb->congested_fn(cb, bdi_bits);
  1701. return r;
  1702. }
  1703. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1704. {
  1705. return t->md;
  1706. }
  1707. EXPORT_SYMBOL(dm_table_get_md);
  1708. void dm_table_run_md_queue_async(struct dm_table *t)
  1709. {
  1710. struct mapped_device *md;
  1711. struct request_queue *queue;
  1712. unsigned long flags;
  1713. if (!dm_table_request_based(t))
  1714. return;
  1715. md = dm_table_get_md(t);
  1716. queue = dm_get_md_queue(md);
  1717. if (queue) {
  1718. if (queue->mq_ops)
  1719. blk_mq_run_hw_queues(queue, true);
  1720. else {
  1721. spin_lock_irqsave(queue->queue_lock, flags);
  1722. blk_run_queue_async(queue);
  1723. spin_unlock_irqrestore(queue->queue_lock, flags);
  1724. }
  1725. }
  1726. }
  1727. EXPORT_SYMBOL(dm_table_run_md_queue_async);