dm-table.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  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. unsigned int major, minor;
  356. char dummy;
  357. struct dm_dev_internal *dd;
  358. struct dm_table *t = ti->table;
  359. BUG_ON(!t);
  360. if (sscanf(path, "%u:%u%c", &major, &minor, &dummy) == 2) {
  361. /* Extract the major/minor numbers */
  362. dev = MKDEV(major, minor);
  363. if (MAJOR(dev) != major || MINOR(dev) != minor)
  364. return -EOVERFLOW;
  365. } else {
  366. dev = dm_get_dev_t(path);
  367. if (!dev)
  368. return -ENODEV;
  369. }
  370. dd = find_device(&t->devices, dev);
  371. if (!dd) {
  372. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  373. if (!dd)
  374. return -ENOMEM;
  375. if ((r = dm_get_table_device(t->md, dev, mode, &dd->dm_dev))) {
  376. kfree(dd);
  377. return r;
  378. }
  379. refcount_set(&dd->count, 1);
  380. list_add(&dd->list, &t->devices);
  381. goto out;
  382. } else if (dd->dm_dev->mode != (mode | dd->dm_dev->mode)) {
  383. r = upgrade_mode(dd, mode, t->md);
  384. if (r)
  385. return r;
  386. }
  387. refcount_inc(&dd->count);
  388. out:
  389. *result = dd->dm_dev;
  390. return 0;
  391. }
  392. EXPORT_SYMBOL(dm_get_device);
  393. static int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
  394. sector_t start, sector_t len, void *data)
  395. {
  396. struct queue_limits *limits = data;
  397. struct block_device *bdev = dev->bdev;
  398. struct request_queue *q = bdev_get_queue(bdev);
  399. char b[BDEVNAME_SIZE];
  400. if (unlikely(!q)) {
  401. DMWARN("%s: Cannot set limits for nonexistent device %s",
  402. dm_device_name(ti->table->md), bdevname(bdev, b));
  403. return 0;
  404. }
  405. if (bdev_stack_limits(limits, bdev, start) < 0)
  406. DMWARN("%s: adding target device %s caused an alignment inconsistency: "
  407. "physical_block_size=%u, logical_block_size=%u, "
  408. "alignment_offset=%u, start=%llu",
  409. dm_device_name(ti->table->md), bdevname(bdev, b),
  410. q->limits.physical_block_size,
  411. q->limits.logical_block_size,
  412. q->limits.alignment_offset,
  413. (unsigned long long) start << SECTOR_SHIFT);
  414. limits->zoned = blk_queue_zoned_model(q);
  415. return 0;
  416. }
  417. /*
  418. * Decrement a device's use count and remove it if necessary.
  419. */
  420. void dm_put_device(struct dm_target *ti, struct dm_dev *d)
  421. {
  422. int found = 0;
  423. struct list_head *devices = &ti->table->devices;
  424. struct dm_dev_internal *dd;
  425. list_for_each_entry(dd, devices, list) {
  426. if (dd->dm_dev == d) {
  427. found = 1;
  428. break;
  429. }
  430. }
  431. if (!found) {
  432. DMWARN("%s: device %s not in table devices list",
  433. dm_device_name(ti->table->md), d->name);
  434. return;
  435. }
  436. if (refcount_dec_and_test(&dd->count)) {
  437. dm_put_table_device(ti->table->md, d);
  438. list_del(&dd->list);
  439. kfree(dd);
  440. }
  441. }
  442. EXPORT_SYMBOL(dm_put_device);
  443. /*
  444. * Checks to see if the target joins onto the end of the table.
  445. */
  446. static int adjoin(struct dm_table *table, struct dm_target *ti)
  447. {
  448. struct dm_target *prev;
  449. if (!table->num_targets)
  450. return !ti->begin;
  451. prev = &table->targets[table->num_targets - 1];
  452. return (ti->begin == (prev->begin + prev->len));
  453. }
  454. /*
  455. * Used to dynamically allocate the arg array.
  456. *
  457. * We do first allocation with GFP_NOIO because dm-mpath and dm-thin must
  458. * process messages even if some device is suspended. These messages have a
  459. * small fixed number of arguments.
  460. *
  461. * On the other hand, dm-switch needs to process bulk data using messages and
  462. * excessive use of GFP_NOIO could cause trouble.
  463. */
  464. static char **realloc_argv(unsigned *size, char **old_argv)
  465. {
  466. char **argv;
  467. unsigned new_size;
  468. gfp_t gfp;
  469. if (*size) {
  470. new_size = *size * 2;
  471. gfp = GFP_KERNEL;
  472. } else {
  473. new_size = 8;
  474. gfp = GFP_NOIO;
  475. }
  476. argv = kmalloc_array(new_size, sizeof(*argv), gfp);
  477. if (argv && old_argv) {
  478. memcpy(argv, old_argv, *size * sizeof(*argv));
  479. *size = new_size;
  480. }
  481. kfree(old_argv);
  482. return argv;
  483. }
  484. /*
  485. * Destructively splits up the argument list to pass to ctr.
  486. */
  487. int dm_split_args(int *argc, char ***argvp, char *input)
  488. {
  489. char *start, *end = input, *out, **argv = NULL;
  490. unsigned array_size = 0;
  491. *argc = 0;
  492. if (!input) {
  493. *argvp = NULL;
  494. return 0;
  495. }
  496. argv = realloc_argv(&array_size, argv);
  497. if (!argv)
  498. return -ENOMEM;
  499. while (1) {
  500. /* Skip whitespace */
  501. start = skip_spaces(end);
  502. if (!*start)
  503. break; /* success, we hit the end */
  504. /* 'out' is used to remove any back-quotes */
  505. end = out = start;
  506. while (*end) {
  507. /* Everything apart from '\0' can be quoted */
  508. if (*end == '\\' && *(end + 1)) {
  509. *out++ = *(end + 1);
  510. end += 2;
  511. continue;
  512. }
  513. if (isspace(*end))
  514. break; /* end of token */
  515. *out++ = *end++;
  516. }
  517. /* have we already filled the array ? */
  518. if ((*argc + 1) > array_size) {
  519. argv = realloc_argv(&array_size, argv);
  520. if (!argv)
  521. return -ENOMEM;
  522. }
  523. /* we know this is whitespace */
  524. if (*end)
  525. end++;
  526. /* terminate the string and put it in the array */
  527. *out = '\0';
  528. argv[*argc] = start;
  529. (*argc)++;
  530. }
  531. *argvp = argv;
  532. return 0;
  533. }
  534. /*
  535. * Impose necessary and sufficient conditions on a devices's table such
  536. * that any incoming bio which respects its logical_block_size can be
  537. * processed successfully. If it falls across the boundary between
  538. * two or more targets, the size of each piece it gets split into must
  539. * be compatible with the logical_block_size of the target processing it.
  540. */
  541. static int validate_hardware_logical_block_alignment(struct dm_table *table,
  542. struct queue_limits *limits)
  543. {
  544. /*
  545. * This function uses arithmetic modulo the logical_block_size
  546. * (in units of 512-byte sectors).
  547. */
  548. unsigned short device_logical_block_size_sects =
  549. limits->logical_block_size >> SECTOR_SHIFT;
  550. /*
  551. * Offset of the start of the next table entry, mod logical_block_size.
  552. */
  553. unsigned short next_target_start = 0;
  554. /*
  555. * Given an aligned bio that extends beyond the end of a
  556. * target, how many sectors must the next target handle?
  557. */
  558. unsigned short remaining = 0;
  559. struct dm_target *uninitialized_var(ti);
  560. struct queue_limits ti_limits;
  561. unsigned i;
  562. /*
  563. * Check each entry in the table in turn.
  564. */
  565. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  566. ti = dm_table_get_target(table, i);
  567. blk_set_stacking_limits(&ti_limits);
  568. /* combine all target devices' limits */
  569. if (ti->type->iterate_devices)
  570. ti->type->iterate_devices(ti, dm_set_device_limits,
  571. &ti_limits);
  572. /*
  573. * If the remaining sectors fall entirely within this
  574. * table entry are they compatible with its logical_block_size?
  575. */
  576. if (remaining < ti->len &&
  577. remaining & ((ti_limits.logical_block_size >>
  578. SECTOR_SHIFT) - 1))
  579. break; /* Error */
  580. next_target_start =
  581. (unsigned short) ((next_target_start + ti->len) &
  582. (device_logical_block_size_sects - 1));
  583. remaining = next_target_start ?
  584. device_logical_block_size_sects - next_target_start : 0;
  585. }
  586. if (remaining) {
  587. DMWARN("%s: table line %u (start sect %llu len %llu) "
  588. "not aligned to h/w logical block size %u",
  589. dm_device_name(table->md), i,
  590. (unsigned long long) ti->begin,
  591. (unsigned long long) ti->len,
  592. limits->logical_block_size);
  593. return -EINVAL;
  594. }
  595. return 0;
  596. }
  597. int dm_table_add_target(struct dm_table *t, const char *type,
  598. sector_t start, sector_t len, char *params)
  599. {
  600. int r = -EINVAL, argc;
  601. char **argv;
  602. struct dm_target *tgt;
  603. if (t->singleton) {
  604. DMERR("%s: target type %s must appear alone in table",
  605. dm_device_name(t->md), t->targets->type->name);
  606. return -EINVAL;
  607. }
  608. BUG_ON(t->num_targets >= t->num_allocated);
  609. tgt = t->targets + t->num_targets;
  610. memset(tgt, 0, sizeof(*tgt));
  611. if (!len) {
  612. DMERR("%s: zero-length target", dm_device_name(t->md));
  613. return -EINVAL;
  614. }
  615. tgt->type = dm_get_target_type(type);
  616. if (!tgt->type) {
  617. DMERR("%s: %s: unknown target type", dm_device_name(t->md), type);
  618. return -EINVAL;
  619. }
  620. if (dm_target_needs_singleton(tgt->type)) {
  621. if (t->num_targets) {
  622. tgt->error = "singleton target type must appear alone in table";
  623. goto bad;
  624. }
  625. t->singleton = true;
  626. }
  627. if (dm_target_always_writeable(tgt->type) && !(t->mode & FMODE_WRITE)) {
  628. tgt->error = "target type may not be included in a read-only table";
  629. goto bad;
  630. }
  631. if (t->immutable_target_type) {
  632. if (t->immutable_target_type != tgt->type) {
  633. tgt->error = "immutable target type cannot be mixed with other target types";
  634. goto bad;
  635. }
  636. } else if (dm_target_is_immutable(tgt->type)) {
  637. if (t->num_targets) {
  638. tgt->error = "immutable target type cannot be mixed with other target types";
  639. goto bad;
  640. }
  641. t->immutable_target_type = tgt->type;
  642. }
  643. if (dm_target_has_integrity(tgt->type))
  644. t->integrity_added = 1;
  645. tgt->table = t;
  646. tgt->begin = start;
  647. tgt->len = len;
  648. tgt->error = "Unknown error";
  649. /*
  650. * Does this target adjoin the previous one ?
  651. */
  652. if (!adjoin(t, tgt)) {
  653. tgt->error = "Gap in table";
  654. goto bad;
  655. }
  656. r = dm_split_args(&argc, &argv, params);
  657. if (r) {
  658. tgt->error = "couldn't split parameters (insufficient memory)";
  659. goto bad;
  660. }
  661. r = tgt->type->ctr(tgt, argc, argv);
  662. kfree(argv);
  663. if (r)
  664. goto bad;
  665. t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
  666. if (!tgt->num_discard_bios && tgt->discards_supported)
  667. DMWARN("%s: %s: ignoring discards_supported because num_discard_bios is zero.",
  668. dm_device_name(t->md), type);
  669. return 0;
  670. bad:
  671. DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error);
  672. dm_put_target_type(tgt->type);
  673. return r;
  674. }
  675. /*
  676. * Target argument parsing helpers.
  677. */
  678. static int validate_next_arg(const struct dm_arg *arg,
  679. struct dm_arg_set *arg_set,
  680. unsigned *value, char **error, unsigned grouped)
  681. {
  682. const char *arg_str = dm_shift_arg(arg_set);
  683. char dummy;
  684. if (!arg_str ||
  685. (sscanf(arg_str, "%u%c", value, &dummy) != 1) ||
  686. (*value < arg->min) ||
  687. (*value > arg->max) ||
  688. (grouped && arg_set->argc < *value)) {
  689. *error = arg->error;
  690. return -EINVAL;
  691. }
  692. return 0;
  693. }
  694. int dm_read_arg(const struct dm_arg *arg, struct dm_arg_set *arg_set,
  695. unsigned *value, char **error)
  696. {
  697. return validate_next_arg(arg, arg_set, value, error, 0);
  698. }
  699. EXPORT_SYMBOL(dm_read_arg);
  700. int dm_read_arg_group(const struct dm_arg *arg, struct dm_arg_set *arg_set,
  701. unsigned *value, char **error)
  702. {
  703. return validate_next_arg(arg, arg_set, value, error, 1);
  704. }
  705. EXPORT_SYMBOL(dm_read_arg_group);
  706. const char *dm_shift_arg(struct dm_arg_set *as)
  707. {
  708. char *r;
  709. if (as->argc) {
  710. as->argc--;
  711. r = *as->argv;
  712. as->argv++;
  713. return r;
  714. }
  715. return NULL;
  716. }
  717. EXPORT_SYMBOL(dm_shift_arg);
  718. void dm_consume_args(struct dm_arg_set *as, unsigned num_args)
  719. {
  720. BUG_ON(as->argc < num_args);
  721. as->argc -= num_args;
  722. as->argv += num_args;
  723. }
  724. EXPORT_SYMBOL(dm_consume_args);
  725. static bool __table_type_bio_based(enum dm_queue_mode table_type)
  726. {
  727. return (table_type == DM_TYPE_BIO_BASED ||
  728. table_type == DM_TYPE_DAX_BIO_BASED ||
  729. table_type == DM_TYPE_NVME_BIO_BASED);
  730. }
  731. static bool __table_type_request_based(enum dm_queue_mode table_type)
  732. {
  733. return (table_type == DM_TYPE_REQUEST_BASED ||
  734. table_type == DM_TYPE_MQ_REQUEST_BASED);
  735. }
  736. void dm_table_set_type(struct dm_table *t, enum dm_queue_mode type)
  737. {
  738. t->type = type;
  739. }
  740. EXPORT_SYMBOL_GPL(dm_table_set_type);
  741. static int device_not_dax_capable(struct dm_target *ti, struct dm_dev *dev,
  742. sector_t start, sector_t len, void *data)
  743. {
  744. return !bdev_dax_supported(dev->bdev, PAGE_SIZE);
  745. }
  746. static bool dm_table_supports_dax(struct dm_table *t)
  747. {
  748. struct dm_target *ti;
  749. unsigned i;
  750. /* Ensure that all targets support DAX. */
  751. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  752. ti = dm_table_get_target(t, i);
  753. if (!ti->type->direct_access)
  754. return false;
  755. if (!ti->type->iterate_devices ||
  756. ti->type->iterate_devices(ti, device_not_dax_capable, NULL))
  757. return false;
  758. }
  759. return true;
  760. }
  761. static bool dm_table_does_not_support_partial_completion(struct dm_table *t);
  762. struct verify_rq_based_data {
  763. unsigned sq_count;
  764. unsigned mq_count;
  765. };
  766. static int device_is_rq_based(struct dm_target *ti, struct dm_dev *dev,
  767. sector_t start, sector_t len, void *data)
  768. {
  769. struct request_queue *q = bdev_get_queue(dev->bdev);
  770. struct verify_rq_based_data *v = data;
  771. if (q->mq_ops)
  772. v->mq_count++;
  773. else
  774. v->sq_count++;
  775. return queue_is_rq_based(q);
  776. }
  777. static int dm_table_determine_type(struct dm_table *t)
  778. {
  779. unsigned i;
  780. unsigned bio_based = 0, request_based = 0, hybrid = 0;
  781. struct verify_rq_based_data v = {.sq_count = 0, .mq_count = 0};
  782. struct dm_target *tgt;
  783. struct list_head *devices = dm_table_get_devices(t);
  784. enum dm_queue_mode live_md_type = dm_get_md_type(t->md);
  785. if (t->type != DM_TYPE_NONE) {
  786. /* target already set the table's type */
  787. if (t->type == DM_TYPE_BIO_BASED) {
  788. /* possibly upgrade to a variant of bio-based */
  789. goto verify_bio_based;
  790. }
  791. BUG_ON(t->type == DM_TYPE_DAX_BIO_BASED);
  792. BUG_ON(t->type == DM_TYPE_NVME_BIO_BASED);
  793. goto verify_rq_based;
  794. }
  795. for (i = 0; i < t->num_targets; i++) {
  796. tgt = t->targets + i;
  797. if (dm_target_hybrid(tgt))
  798. hybrid = 1;
  799. else if (dm_target_request_based(tgt))
  800. request_based = 1;
  801. else
  802. bio_based = 1;
  803. if (bio_based && request_based) {
  804. DMERR("Inconsistent table: different target types"
  805. " can't be mixed up");
  806. return -EINVAL;
  807. }
  808. }
  809. if (hybrid && !bio_based && !request_based) {
  810. /*
  811. * The targets can work either way.
  812. * Determine the type from the live device.
  813. * Default to bio-based if device is new.
  814. */
  815. if (__table_type_request_based(live_md_type))
  816. request_based = 1;
  817. else
  818. bio_based = 1;
  819. }
  820. if (bio_based) {
  821. verify_bio_based:
  822. /* We must use this table as bio-based */
  823. t->type = DM_TYPE_BIO_BASED;
  824. if (dm_table_supports_dax(t) ||
  825. (list_empty(devices) && live_md_type == DM_TYPE_DAX_BIO_BASED)) {
  826. t->type = DM_TYPE_DAX_BIO_BASED;
  827. } else {
  828. /* Check if upgrading to NVMe bio-based is valid or required */
  829. tgt = dm_table_get_immutable_target(t);
  830. if (tgt && !tgt->max_io_len && dm_table_does_not_support_partial_completion(t)) {
  831. t->type = DM_TYPE_NVME_BIO_BASED;
  832. goto verify_rq_based; /* must be stacked directly on NVMe (blk-mq) */
  833. } else if (list_empty(devices) && live_md_type == DM_TYPE_NVME_BIO_BASED) {
  834. t->type = DM_TYPE_NVME_BIO_BASED;
  835. }
  836. }
  837. return 0;
  838. }
  839. BUG_ON(!request_based); /* No targets in this table */
  840. /*
  841. * The only way to establish DM_TYPE_MQ_REQUEST_BASED is by
  842. * having a compatible target use dm_table_set_type.
  843. */
  844. t->type = DM_TYPE_REQUEST_BASED;
  845. verify_rq_based:
  846. /*
  847. * Request-based dm supports only tables that have a single target now.
  848. * To support multiple targets, request splitting support is needed,
  849. * and that needs lots of changes in the block-layer.
  850. * (e.g. request completion process for partial completion.)
  851. */
  852. if (t->num_targets > 1) {
  853. DMERR("%s DM doesn't support multiple targets",
  854. t->type == DM_TYPE_NVME_BIO_BASED ? "nvme bio-based" : "request-based");
  855. return -EINVAL;
  856. }
  857. if (list_empty(devices)) {
  858. int srcu_idx;
  859. struct dm_table *live_table = dm_get_live_table(t->md, &srcu_idx);
  860. /* inherit live table's type and all_blk_mq */
  861. if (live_table) {
  862. t->type = live_table->type;
  863. t->all_blk_mq = live_table->all_blk_mq;
  864. }
  865. dm_put_live_table(t->md, srcu_idx);
  866. return 0;
  867. }
  868. tgt = dm_table_get_immutable_target(t);
  869. if (!tgt) {
  870. DMERR("table load rejected: immutable target is required");
  871. return -EINVAL;
  872. } else if (tgt->max_io_len) {
  873. DMERR("table load rejected: immutable target that splits IO is not supported");
  874. return -EINVAL;
  875. }
  876. /* Non-request-stackable devices can't be used for request-based dm */
  877. if (!tgt->type->iterate_devices ||
  878. !tgt->type->iterate_devices(tgt, device_is_rq_based, &v)) {
  879. DMERR("table load rejected: including non-request-stackable devices");
  880. return -EINVAL;
  881. }
  882. if (v.sq_count && v.mq_count) {
  883. DMERR("table load rejected: not all devices are blk-mq request-stackable");
  884. return -EINVAL;
  885. }
  886. t->all_blk_mq = v.mq_count > 0;
  887. if (!t->all_blk_mq &&
  888. (t->type == DM_TYPE_MQ_REQUEST_BASED || t->type == DM_TYPE_NVME_BIO_BASED)) {
  889. DMERR("table load rejected: all devices are not blk-mq request-stackable");
  890. return -EINVAL;
  891. }
  892. return 0;
  893. }
  894. enum dm_queue_mode dm_table_get_type(struct dm_table *t)
  895. {
  896. return t->type;
  897. }
  898. struct target_type *dm_table_get_immutable_target_type(struct dm_table *t)
  899. {
  900. return t->immutable_target_type;
  901. }
  902. struct dm_target *dm_table_get_immutable_target(struct dm_table *t)
  903. {
  904. /* Immutable target is implicitly a singleton */
  905. if (t->num_targets > 1 ||
  906. !dm_target_is_immutable(t->targets[0].type))
  907. return NULL;
  908. return t->targets;
  909. }
  910. struct dm_target *dm_table_get_wildcard_target(struct dm_table *t)
  911. {
  912. struct dm_target *ti;
  913. unsigned i;
  914. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  915. ti = dm_table_get_target(t, i);
  916. if (dm_target_is_wildcard(ti->type))
  917. return ti;
  918. }
  919. return NULL;
  920. }
  921. bool dm_table_bio_based(struct dm_table *t)
  922. {
  923. return __table_type_bio_based(dm_table_get_type(t));
  924. }
  925. bool dm_table_request_based(struct dm_table *t)
  926. {
  927. return __table_type_request_based(dm_table_get_type(t));
  928. }
  929. bool dm_table_all_blk_mq_devices(struct dm_table *t)
  930. {
  931. return t->all_blk_mq;
  932. }
  933. static int dm_table_alloc_md_mempools(struct dm_table *t, struct mapped_device *md)
  934. {
  935. enum dm_queue_mode type = dm_table_get_type(t);
  936. unsigned per_io_data_size = 0;
  937. unsigned min_pool_size = 0;
  938. struct dm_target *ti;
  939. unsigned i;
  940. if (unlikely(type == DM_TYPE_NONE)) {
  941. DMWARN("no table type is set, can't allocate mempools");
  942. return -EINVAL;
  943. }
  944. if (__table_type_bio_based(type))
  945. for (i = 0; i < t->num_targets; i++) {
  946. ti = t->targets + i;
  947. per_io_data_size = max(per_io_data_size, ti->per_io_data_size);
  948. min_pool_size = max(min_pool_size, ti->num_flush_bios);
  949. }
  950. t->mempools = dm_alloc_md_mempools(md, type, t->integrity_supported,
  951. per_io_data_size, min_pool_size);
  952. if (!t->mempools)
  953. return -ENOMEM;
  954. return 0;
  955. }
  956. void dm_table_free_md_mempools(struct dm_table *t)
  957. {
  958. dm_free_md_mempools(t->mempools);
  959. t->mempools = NULL;
  960. }
  961. struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
  962. {
  963. return t->mempools;
  964. }
  965. static int setup_indexes(struct dm_table *t)
  966. {
  967. int i;
  968. unsigned int total = 0;
  969. sector_t *indexes;
  970. /* allocate the space for *all* the indexes */
  971. for (i = t->depth - 2; i >= 0; i--) {
  972. t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE);
  973. total += t->counts[i];
  974. }
  975. indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE);
  976. if (!indexes)
  977. return -ENOMEM;
  978. /* set up internal nodes, bottom-up */
  979. for (i = t->depth - 2; i >= 0; i--) {
  980. t->index[i] = indexes;
  981. indexes += (KEYS_PER_NODE * t->counts[i]);
  982. setup_btree_index(i, t);
  983. }
  984. return 0;
  985. }
  986. /*
  987. * Builds the btree to index the map.
  988. */
  989. static int dm_table_build_index(struct dm_table *t)
  990. {
  991. int r = 0;
  992. unsigned int leaf_nodes;
  993. /* how many indexes will the btree have ? */
  994. leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
  995. t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
  996. /* leaf layer has already been set up */
  997. t->counts[t->depth - 1] = leaf_nodes;
  998. t->index[t->depth - 1] = t->highs;
  999. if (t->depth >= 2)
  1000. r = setup_indexes(t);
  1001. return r;
  1002. }
  1003. static bool integrity_profile_exists(struct gendisk *disk)
  1004. {
  1005. return !!blk_get_integrity(disk);
  1006. }
  1007. /*
  1008. * Get a disk whose integrity profile reflects the table's profile.
  1009. * Returns NULL if integrity support was inconsistent or unavailable.
  1010. */
  1011. static struct gendisk * dm_table_get_integrity_disk(struct dm_table *t)
  1012. {
  1013. struct list_head *devices = dm_table_get_devices(t);
  1014. struct dm_dev_internal *dd = NULL;
  1015. struct gendisk *prev_disk = NULL, *template_disk = NULL;
  1016. unsigned i;
  1017. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1018. struct dm_target *ti = dm_table_get_target(t, i);
  1019. if (!dm_target_passes_integrity(ti->type))
  1020. goto no_integrity;
  1021. }
  1022. list_for_each_entry(dd, devices, list) {
  1023. template_disk = dd->dm_dev->bdev->bd_disk;
  1024. if (!integrity_profile_exists(template_disk))
  1025. goto no_integrity;
  1026. else if (prev_disk &&
  1027. blk_integrity_compare(prev_disk, template_disk) < 0)
  1028. goto no_integrity;
  1029. prev_disk = template_disk;
  1030. }
  1031. return template_disk;
  1032. no_integrity:
  1033. if (prev_disk)
  1034. DMWARN("%s: integrity not set: %s and %s profile mismatch",
  1035. dm_device_name(t->md),
  1036. prev_disk->disk_name,
  1037. template_disk->disk_name);
  1038. return NULL;
  1039. }
  1040. /*
  1041. * Register the mapped device for blk_integrity support if the
  1042. * underlying devices have an integrity profile. But all devices may
  1043. * not have matching profiles (checking all devices isn't reliable
  1044. * during table load because this table may use other DM device(s) which
  1045. * must be resumed before they will have an initialized integity
  1046. * profile). Consequently, stacked DM devices force a 2 stage integrity
  1047. * profile validation: First pass during table load, final pass during
  1048. * resume.
  1049. */
  1050. static int dm_table_register_integrity(struct dm_table *t)
  1051. {
  1052. struct mapped_device *md = t->md;
  1053. struct gendisk *template_disk = NULL;
  1054. /* If target handles integrity itself do not register it here. */
  1055. if (t->integrity_added)
  1056. return 0;
  1057. template_disk = dm_table_get_integrity_disk(t);
  1058. if (!template_disk)
  1059. return 0;
  1060. if (!integrity_profile_exists(dm_disk(md))) {
  1061. t->integrity_supported = true;
  1062. /*
  1063. * Register integrity profile during table load; we can do
  1064. * this because the final profile must match during resume.
  1065. */
  1066. blk_integrity_register(dm_disk(md),
  1067. blk_get_integrity(template_disk));
  1068. return 0;
  1069. }
  1070. /*
  1071. * If DM device already has an initialized integrity
  1072. * profile the new profile should not conflict.
  1073. */
  1074. if (blk_integrity_compare(dm_disk(md), template_disk) < 0) {
  1075. DMWARN("%s: conflict with existing integrity profile: "
  1076. "%s profile mismatch",
  1077. dm_device_name(t->md),
  1078. template_disk->disk_name);
  1079. return 1;
  1080. }
  1081. /* Preserve existing integrity profile */
  1082. t->integrity_supported = true;
  1083. return 0;
  1084. }
  1085. /*
  1086. * Prepares the table for use by building the indices,
  1087. * setting the type, and allocating mempools.
  1088. */
  1089. int dm_table_complete(struct dm_table *t)
  1090. {
  1091. int r;
  1092. r = dm_table_determine_type(t);
  1093. if (r) {
  1094. DMERR("unable to determine table type");
  1095. return r;
  1096. }
  1097. r = dm_table_build_index(t);
  1098. if (r) {
  1099. DMERR("unable to build btrees");
  1100. return r;
  1101. }
  1102. r = dm_table_register_integrity(t);
  1103. if (r) {
  1104. DMERR("could not register integrity profile.");
  1105. return r;
  1106. }
  1107. r = dm_table_alloc_md_mempools(t, t->md);
  1108. if (r)
  1109. DMERR("unable to allocate mempools");
  1110. return r;
  1111. }
  1112. static DEFINE_MUTEX(_event_lock);
  1113. void dm_table_event_callback(struct dm_table *t,
  1114. void (*fn)(void *), void *context)
  1115. {
  1116. mutex_lock(&_event_lock);
  1117. t->event_fn = fn;
  1118. t->event_context = context;
  1119. mutex_unlock(&_event_lock);
  1120. }
  1121. void dm_table_event(struct dm_table *t)
  1122. {
  1123. mutex_lock(&_event_lock);
  1124. if (t->event_fn)
  1125. t->event_fn(t->event_context);
  1126. mutex_unlock(&_event_lock);
  1127. }
  1128. EXPORT_SYMBOL(dm_table_event);
  1129. inline sector_t dm_table_get_size(struct dm_table *t)
  1130. {
  1131. return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0;
  1132. }
  1133. EXPORT_SYMBOL(dm_table_get_size);
  1134. struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index)
  1135. {
  1136. if (index >= t->num_targets)
  1137. return NULL;
  1138. return t->targets + index;
  1139. }
  1140. /*
  1141. * Search the btree for the correct target.
  1142. *
  1143. * Caller should check returned pointer with dm_target_is_valid()
  1144. * to trap I/O beyond end of device.
  1145. */
  1146. struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
  1147. {
  1148. unsigned int l, n = 0, k = 0;
  1149. sector_t *node;
  1150. if (unlikely(sector >= dm_table_get_size(t)))
  1151. return &t->targets[t->num_targets];
  1152. for (l = 0; l < t->depth; l++) {
  1153. n = get_child(n, k);
  1154. node = get_node(t, l, n);
  1155. for (k = 0; k < KEYS_PER_NODE; k++)
  1156. if (node[k] >= sector)
  1157. break;
  1158. }
  1159. return &t->targets[(KEYS_PER_NODE * n) + k];
  1160. }
  1161. /*
  1162. * type->iterate_devices() should be called when the sanity check needs to
  1163. * iterate and check all underlying data devices. iterate_devices() will
  1164. * iterate all underlying data devices until it encounters a non-zero return
  1165. * code, returned by whether the input iterate_devices_callout_fn, or
  1166. * iterate_devices() itself internally.
  1167. *
  1168. * For some target type (e.g. dm-stripe), one call of iterate_devices() may
  1169. * iterate multiple underlying devices internally, in which case a non-zero
  1170. * return code returned by iterate_devices_callout_fn will stop the iteration
  1171. * in advance.
  1172. *
  1173. * Cases requiring _any_ underlying device supporting some kind of attribute,
  1174. * should use the iteration structure like dm_table_any_dev_attr(), or call
  1175. * it directly. @func should handle semantics of positive examples, e.g.
  1176. * capable of something.
  1177. *
  1178. * Cases requiring _all_ underlying devices supporting some kind of attribute,
  1179. * should use the iteration structure like dm_table_supports_nowait() or
  1180. * dm_table_supports_discards(). Or introduce dm_table_all_devs_attr() that
  1181. * uses an @anti_func that handle semantics of counter examples, e.g. not
  1182. * capable of something. So: return !dm_table_any_dev_attr(t, anti_func, data);
  1183. */
  1184. static bool dm_table_any_dev_attr(struct dm_table *t,
  1185. iterate_devices_callout_fn func, void *data)
  1186. {
  1187. struct dm_target *ti;
  1188. unsigned int i;
  1189. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1190. ti = dm_table_get_target(t, i);
  1191. if (ti->type->iterate_devices &&
  1192. ti->type->iterate_devices(ti, func, data))
  1193. return true;
  1194. }
  1195. return false;
  1196. }
  1197. static int count_device(struct dm_target *ti, struct dm_dev *dev,
  1198. sector_t start, sector_t len, void *data)
  1199. {
  1200. unsigned *num_devices = data;
  1201. (*num_devices)++;
  1202. return 0;
  1203. }
  1204. /*
  1205. * Check whether a table has no data devices attached using each
  1206. * target's iterate_devices method.
  1207. * Returns false if the result is unknown because a target doesn't
  1208. * support iterate_devices.
  1209. */
  1210. bool dm_table_has_no_data_devices(struct dm_table *table)
  1211. {
  1212. struct dm_target *ti;
  1213. unsigned i, num_devices;
  1214. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  1215. ti = dm_table_get_target(table, i);
  1216. if (!ti->type->iterate_devices)
  1217. return false;
  1218. num_devices = 0;
  1219. ti->type->iterate_devices(ti, count_device, &num_devices);
  1220. if (num_devices)
  1221. return false;
  1222. }
  1223. return true;
  1224. }
  1225. static int device_not_zoned_model(struct dm_target *ti, struct dm_dev *dev,
  1226. sector_t start, sector_t len, void *data)
  1227. {
  1228. struct request_queue *q = bdev_get_queue(dev->bdev);
  1229. enum blk_zoned_model *zoned_model = data;
  1230. return !q || blk_queue_zoned_model(q) != *zoned_model;
  1231. }
  1232. static bool dm_table_supports_zoned_model(struct dm_table *t,
  1233. enum blk_zoned_model zoned_model)
  1234. {
  1235. struct dm_target *ti;
  1236. unsigned i;
  1237. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1238. ti = dm_table_get_target(t, i);
  1239. if (zoned_model == BLK_ZONED_HM &&
  1240. !dm_target_supports_zoned_hm(ti->type))
  1241. return false;
  1242. if (!ti->type->iterate_devices ||
  1243. ti->type->iterate_devices(ti, device_not_zoned_model, &zoned_model))
  1244. return false;
  1245. }
  1246. return true;
  1247. }
  1248. static int device_not_matches_zone_sectors(struct dm_target *ti, struct dm_dev *dev,
  1249. sector_t start, sector_t len, void *data)
  1250. {
  1251. struct request_queue *q = bdev_get_queue(dev->bdev);
  1252. unsigned int *zone_sectors = data;
  1253. return !q || blk_queue_zone_sectors(q) != *zone_sectors;
  1254. }
  1255. static int validate_hardware_zoned_model(struct dm_table *table,
  1256. enum blk_zoned_model zoned_model,
  1257. unsigned int zone_sectors)
  1258. {
  1259. if (zoned_model == BLK_ZONED_NONE)
  1260. return 0;
  1261. if (!dm_table_supports_zoned_model(table, zoned_model)) {
  1262. DMERR("%s: zoned model is not consistent across all devices",
  1263. dm_device_name(table->md));
  1264. return -EINVAL;
  1265. }
  1266. /* Check zone size validity and compatibility */
  1267. if (!zone_sectors || !is_power_of_2(zone_sectors))
  1268. return -EINVAL;
  1269. if (dm_table_any_dev_attr(table, device_not_matches_zone_sectors, &zone_sectors)) {
  1270. DMERR("%s: zone sectors is not consistent across all devices",
  1271. dm_device_name(table->md));
  1272. return -EINVAL;
  1273. }
  1274. return 0;
  1275. }
  1276. /*
  1277. * Establish the new table's queue_limits and validate them.
  1278. */
  1279. int dm_calculate_queue_limits(struct dm_table *table,
  1280. struct queue_limits *limits)
  1281. {
  1282. struct dm_target *ti;
  1283. struct queue_limits ti_limits;
  1284. unsigned i;
  1285. enum blk_zoned_model zoned_model = BLK_ZONED_NONE;
  1286. unsigned int zone_sectors = 0;
  1287. blk_set_stacking_limits(limits);
  1288. for (i = 0; i < dm_table_get_num_targets(table); i++) {
  1289. blk_set_stacking_limits(&ti_limits);
  1290. ti = dm_table_get_target(table, i);
  1291. if (!ti->type->iterate_devices)
  1292. goto combine_limits;
  1293. /*
  1294. * Combine queue limits of all the devices this target uses.
  1295. */
  1296. ti->type->iterate_devices(ti, dm_set_device_limits,
  1297. &ti_limits);
  1298. if (zoned_model == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
  1299. /*
  1300. * After stacking all limits, validate all devices
  1301. * in table support this zoned model and zone sectors.
  1302. */
  1303. zoned_model = ti_limits.zoned;
  1304. zone_sectors = ti_limits.chunk_sectors;
  1305. }
  1306. /* Set I/O hints portion of queue limits */
  1307. if (ti->type->io_hints)
  1308. ti->type->io_hints(ti, &ti_limits);
  1309. /*
  1310. * Check each device area is consistent with the target's
  1311. * overall queue limits.
  1312. */
  1313. if (ti->type->iterate_devices(ti, device_area_is_invalid,
  1314. &ti_limits))
  1315. return -EINVAL;
  1316. combine_limits:
  1317. /*
  1318. * Merge this target's queue limits into the overall limits
  1319. * for the table.
  1320. */
  1321. if (blk_stack_limits(limits, &ti_limits, 0) < 0)
  1322. DMWARN("%s: adding target device "
  1323. "(start sect %llu len %llu) "
  1324. "caused an alignment inconsistency",
  1325. dm_device_name(table->md),
  1326. (unsigned long long) ti->begin,
  1327. (unsigned long long) ti->len);
  1328. /*
  1329. * FIXME: this should likely be moved to blk_stack_limits(), would
  1330. * also eliminate limits->zoned stacking hack in dm_set_device_limits()
  1331. */
  1332. if (limits->zoned == BLK_ZONED_NONE && ti_limits.zoned != BLK_ZONED_NONE) {
  1333. /*
  1334. * By default, the stacked limits zoned model is set to
  1335. * BLK_ZONED_NONE in blk_set_stacking_limits(). Update
  1336. * this model using the first target model reported
  1337. * that is not BLK_ZONED_NONE. This will be either the
  1338. * first target device zoned model or the model reported
  1339. * by the target .io_hints.
  1340. */
  1341. limits->zoned = ti_limits.zoned;
  1342. }
  1343. }
  1344. /*
  1345. * Verify that the zoned model and zone sectors, as determined before
  1346. * any .io_hints override, are the same across all devices in the table.
  1347. * - this is especially relevant if .io_hints is emulating a disk-managed
  1348. * zoned model (aka BLK_ZONED_NONE) on host-managed zoned block devices.
  1349. * BUT...
  1350. */
  1351. if (limits->zoned != BLK_ZONED_NONE) {
  1352. /*
  1353. * ...IF the above limits stacking determined a zoned model
  1354. * validate that all of the table's devices conform to it.
  1355. */
  1356. zoned_model = limits->zoned;
  1357. zone_sectors = limits->chunk_sectors;
  1358. }
  1359. if (validate_hardware_zoned_model(table, zoned_model, zone_sectors))
  1360. return -EINVAL;
  1361. return validate_hardware_logical_block_alignment(table, limits);
  1362. }
  1363. /*
  1364. * Verify that all devices have an integrity profile that matches the
  1365. * DM device's registered integrity profile. If the profiles don't
  1366. * match then unregister the DM device's integrity profile.
  1367. */
  1368. static void dm_table_verify_integrity(struct dm_table *t)
  1369. {
  1370. struct gendisk *template_disk = NULL;
  1371. if (t->integrity_added)
  1372. return;
  1373. if (t->integrity_supported) {
  1374. /*
  1375. * Verify that the original integrity profile
  1376. * matches all the devices in this table.
  1377. */
  1378. template_disk = dm_table_get_integrity_disk(t);
  1379. if (template_disk &&
  1380. blk_integrity_compare(dm_disk(t->md), template_disk) >= 0)
  1381. return;
  1382. }
  1383. if (integrity_profile_exists(dm_disk(t->md))) {
  1384. DMWARN("%s: unable to establish an integrity profile",
  1385. dm_device_name(t->md));
  1386. blk_integrity_unregister(dm_disk(t->md));
  1387. }
  1388. }
  1389. static int device_flush_capable(struct dm_target *ti, struct dm_dev *dev,
  1390. sector_t start, sector_t len, void *data)
  1391. {
  1392. unsigned long flush = (unsigned long) data;
  1393. struct request_queue *q = bdev_get_queue(dev->bdev);
  1394. return q && (q->queue_flags & flush);
  1395. }
  1396. static bool dm_table_supports_flush(struct dm_table *t, unsigned long flush)
  1397. {
  1398. struct dm_target *ti;
  1399. unsigned i;
  1400. /*
  1401. * Require at least one underlying device to support flushes.
  1402. * t->devices includes internal dm devices such as mirror logs
  1403. * so we need to use iterate_devices here, which targets
  1404. * supporting flushes must provide.
  1405. */
  1406. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1407. ti = dm_table_get_target(t, i);
  1408. if (!ti->num_flush_bios)
  1409. continue;
  1410. if (ti->flush_supported)
  1411. return true;
  1412. if (ti->type->iterate_devices &&
  1413. ti->type->iterate_devices(ti, device_flush_capable, (void *) flush))
  1414. return true;
  1415. }
  1416. return false;
  1417. }
  1418. static int device_dax_write_cache_enabled(struct dm_target *ti,
  1419. struct dm_dev *dev, sector_t start,
  1420. sector_t len, void *data)
  1421. {
  1422. struct dax_device *dax_dev = dev->dax_dev;
  1423. if (!dax_dev)
  1424. return false;
  1425. if (dax_write_cache_enabled(dax_dev))
  1426. return true;
  1427. return false;
  1428. }
  1429. static int device_is_rotational(struct dm_target *ti, struct dm_dev *dev,
  1430. sector_t start, sector_t len, void *data)
  1431. {
  1432. struct request_queue *q = bdev_get_queue(dev->bdev);
  1433. return q && !blk_queue_nonrot(q);
  1434. }
  1435. static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
  1436. sector_t start, sector_t len, void *data)
  1437. {
  1438. struct request_queue *q = bdev_get_queue(dev->bdev);
  1439. return q && !blk_queue_add_random(q);
  1440. }
  1441. static int queue_no_sg_merge(struct dm_target *ti, struct dm_dev *dev,
  1442. sector_t start, sector_t len, void *data)
  1443. {
  1444. struct request_queue *q = bdev_get_queue(dev->bdev);
  1445. return q && test_bit(QUEUE_FLAG_NO_SG_MERGE, &q->queue_flags);
  1446. }
  1447. static int device_is_partial_completion(struct dm_target *ti, struct dm_dev *dev,
  1448. sector_t start, sector_t len, void *data)
  1449. {
  1450. char b[BDEVNAME_SIZE];
  1451. /* For now, NVMe devices are the only devices of this class */
  1452. return (strncmp(bdevname(dev->bdev, b), "nvme", 4) != 0);
  1453. }
  1454. static bool dm_table_does_not_support_partial_completion(struct dm_table *t)
  1455. {
  1456. return !dm_table_any_dev_attr(t, device_is_partial_completion, NULL);
  1457. }
  1458. static int device_not_write_same_capable(struct dm_target *ti, struct dm_dev *dev,
  1459. sector_t start, sector_t len, void *data)
  1460. {
  1461. struct request_queue *q = bdev_get_queue(dev->bdev);
  1462. return q && !q->limits.max_write_same_sectors;
  1463. }
  1464. static bool dm_table_supports_write_same(struct dm_table *t)
  1465. {
  1466. struct dm_target *ti;
  1467. unsigned i;
  1468. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1469. ti = dm_table_get_target(t, i);
  1470. if (!ti->num_write_same_bios)
  1471. return false;
  1472. if (!ti->type->iterate_devices ||
  1473. ti->type->iterate_devices(ti, device_not_write_same_capable, NULL))
  1474. return false;
  1475. }
  1476. return true;
  1477. }
  1478. static int device_not_write_zeroes_capable(struct dm_target *ti, struct dm_dev *dev,
  1479. sector_t start, sector_t len, void *data)
  1480. {
  1481. struct request_queue *q = bdev_get_queue(dev->bdev);
  1482. return q && !q->limits.max_write_zeroes_sectors;
  1483. }
  1484. static bool dm_table_supports_write_zeroes(struct dm_table *t)
  1485. {
  1486. struct dm_target *ti;
  1487. unsigned i = 0;
  1488. while (i < dm_table_get_num_targets(t)) {
  1489. ti = dm_table_get_target(t, i++);
  1490. if (!ti->num_write_zeroes_bios)
  1491. return false;
  1492. if (!ti->type->iterate_devices ||
  1493. ti->type->iterate_devices(ti, device_not_write_zeroes_capable, NULL))
  1494. return false;
  1495. }
  1496. return true;
  1497. }
  1498. static int device_not_discard_capable(struct dm_target *ti, struct dm_dev *dev,
  1499. sector_t start, sector_t len, void *data)
  1500. {
  1501. struct request_queue *q = bdev_get_queue(dev->bdev);
  1502. return q && !blk_queue_discard(q);
  1503. }
  1504. static bool dm_table_supports_discards(struct dm_table *t)
  1505. {
  1506. struct dm_target *ti;
  1507. unsigned i;
  1508. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1509. ti = dm_table_get_target(t, i);
  1510. if (!ti->num_discard_bios)
  1511. return false;
  1512. /*
  1513. * Either the target provides discard support (as implied by setting
  1514. * 'discards_supported') or it relies on _all_ data devices having
  1515. * discard support.
  1516. */
  1517. if (!ti->discards_supported &&
  1518. (!ti->type->iterate_devices ||
  1519. ti->type->iterate_devices(ti, device_not_discard_capable, NULL)))
  1520. return false;
  1521. }
  1522. return true;
  1523. }
  1524. static int device_not_secure_erase_capable(struct dm_target *ti,
  1525. struct dm_dev *dev, sector_t start,
  1526. sector_t len, void *data)
  1527. {
  1528. struct request_queue *q = bdev_get_queue(dev->bdev);
  1529. return q && !blk_queue_secure_erase(q);
  1530. }
  1531. static bool dm_table_supports_secure_erase(struct dm_table *t)
  1532. {
  1533. struct dm_target *ti;
  1534. unsigned int i;
  1535. for (i = 0; i < dm_table_get_num_targets(t); i++) {
  1536. ti = dm_table_get_target(t, i);
  1537. if (!ti->num_secure_erase_bios)
  1538. return false;
  1539. if (!ti->type->iterate_devices ||
  1540. ti->type->iterate_devices(ti, device_not_secure_erase_capable, NULL))
  1541. return false;
  1542. }
  1543. return true;
  1544. }
  1545. static int device_requires_stable_pages(struct dm_target *ti,
  1546. struct dm_dev *dev, sector_t start,
  1547. sector_t len, void *data)
  1548. {
  1549. struct request_queue *q = bdev_get_queue(dev->bdev);
  1550. return q && bdi_cap_stable_pages_required(q->backing_dev_info);
  1551. }
  1552. void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
  1553. struct queue_limits *limits)
  1554. {
  1555. bool wc = false, fua = false;
  1556. /*
  1557. * Copy table's limits to the DM device's request_queue
  1558. */
  1559. q->limits = *limits;
  1560. if (!dm_table_supports_discards(t)) {
  1561. blk_queue_flag_clear(QUEUE_FLAG_DISCARD, q);
  1562. /* Must also clear discard limits... */
  1563. q->limits.max_discard_sectors = 0;
  1564. q->limits.max_hw_discard_sectors = 0;
  1565. q->limits.discard_granularity = 0;
  1566. q->limits.discard_alignment = 0;
  1567. q->limits.discard_misaligned = 0;
  1568. } else
  1569. blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
  1570. if (dm_table_supports_secure_erase(t))
  1571. blk_queue_flag_set(QUEUE_FLAG_SECERASE, q);
  1572. if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_WC))) {
  1573. wc = true;
  1574. if (dm_table_supports_flush(t, (1UL << QUEUE_FLAG_FUA)))
  1575. fua = true;
  1576. }
  1577. blk_queue_write_cache(q, wc, fua);
  1578. if (dm_table_supports_dax(t))
  1579. blk_queue_flag_set(QUEUE_FLAG_DAX, q);
  1580. else
  1581. blk_queue_flag_clear(QUEUE_FLAG_DAX, q);
  1582. if (dm_table_any_dev_attr(t, device_dax_write_cache_enabled, NULL))
  1583. dax_write_cache(t->md->dax_dev, true);
  1584. /* Ensure that all underlying devices are non-rotational. */
  1585. if (dm_table_any_dev_attr(t, device_is_rotational, NULL))
  1586. blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
  1587. else
  1588. blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
  1589. if (!dm_table_supports_write_same(t))
  1590. q->limits.max_write_same_sectors = 0;
  1591. if (!dm_table_supports_write_zeroes(t))
  1592. q->limits.max_write_zeroes_sectors = 0;
  1593. if (dm_table_any_dev_attr(t, queue_no_sg_merge, NULL))
  1594. blk_queue_flag_set(QUEUE_FLAG_NO_SG_MERGE, q);
  1595. else
  1596. blk_queue_flag_clear(QUEUE_FLAG_NO_SG_MERGE, q);
  1597. dm_table_verify_integrity(t);
  1598. /*
  1599. * Some devices don't use blk_integrity but still want stable pages
  1600. * because they do their own checksumming.
  1601. * If any underlying device requires stable pages, a table must require
  1602. * them as well. Only targets that support iterate_devices are considered:
  1603. * don't want error, zero, etc to require stable pages.
  1604. */
  1605. if (dm_table_any_dev_attr(t, device_requires_stable_pages, NULL))
  1606. q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
  1607. else
  1608. q->backing_dev_info->capabilities &= ~BDI_CAP_STABLE_WRITES;
  1609. /*
  1610. * Determine whether or not this queue's I/O timings contribute
  1611. * to the entropy pool, Only request-based targets use this.
  1612. * Clear QUEUE_FLAG_ADD_RANDOM if any underlying device does not
  1613. * have it set.
  1614. */
  1615. if (blk_queue_add_random(q) &&
  1616. dm_table_any_dev_attr(t, device_is_not_random, NULL))
  1617. blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q);
  1618. }
  1619. unsigned int dm_table_get_num_targets(struct dm_table *t)
  1620. {
  1621. return t->num_targets;
  1622. }
  1623. struct list_head *dm_table_get_devices(struct dm_table *t)
  1624. {
  1625. return &t->devices;
  1626. }
  1627. fmode_t dm_table_get_mode(struct dm_table *t)
  1628. {
  1629. return t->mode;
  1630. }
  1631. EXPORT_SYMBOL(dm_table_get_mode);
  1632. enum suspend_mode {
  1633. PRESUSPEND,
  1634. PRESUSPEND_UNDO,
  1635. POSTSUSPEND,
  1636. };
  1637. static void suspend_targets(struct dm_table *t, enum suspend_mode mode)
  1638. {
  1639. int i = t->num_targets;
  1640. struct dm_target *ti = t->targets;
  1641. lockdep_assert_held(&t->md->suspend_lock);
  1642. while (i--) {
  1643. switch (mode) {
  1644. case PRESUSPEND:
  1645. if (ti->type->presuspend)
  1646. ti->type->presuspend(ti);
  1647. break;
  1648. case PRESUSPEND_UNDO:
  1649. if (ti->type->presuspend_undo)
  1650. ti->type->presuspend_undo(ti);
  1651. break;
  1652. case POSTSUSPEND:
  1653. if (ti->type->postsuspend)
  1654. ti->type->postsuspend(ti);
  1655. break;
  1656. }
  1657. ti++;
  1658. }
  1659. }
  1660. void dm_table_presuspend_targets(struct dm_table *t)
  1661. {
  1662. if (!t)
  1663. return;
  1664. suspend_targets(t, PRESUSPEND);
  1665. }
  1666. void dm_table_presuspend_undo_targets(struct dm_table *t)
  1667. {
  1668. if (!t)
  1669. return;
  1670. suspend_targets(t, PRESUSPEND_UNDO);
  1671. }
  1672. void dm_table_postsuspend_targets(struct dm_table *t)
  1673. {
  1674. if (!t)
  1675. return;
  1676. suspend_targets(t, POSTSUSPEND);
  1677. }
  1678. int dm_table_resume_targets(struct dm_table *t)
  1679. {
  1680. int i, r = 0;
  1681. lockdep_assert_held(&t->md->suspend_lock);
  1682. for (i = 0; i < t->num_targets; i++) {
  1683. struct dm_target *ti = t->targets + i;
  1684. if (!ti->type->preresume)
  1685. continue;
  1686. r = ti->type->preresume(ti);
  1687. if (r) {
  1688. DMERR("%s: %s: preresume failed, error = %d",
  1689. dm_device_name(t->md), ti->type->name, r);
  1690. return r;
  1691. }
  1692. }
  1693. for (i = 0; i < t->num_targets; i++) {
  1694. struct dm_target *ti = t->targets + i;
  1695. if (ti->type->resume)
  1696. ti->type->resume(ti);
  1697. }
  1698. return 0;
  1699. }
  1700. void dm_table_add_target_callbacks(struct dm_table *t, struct dm_target_callbacks *cb)
  1701. {
  1702. list_add(&cb->list, &t->target_callbacks);
  1703. }
  1704. EXPORT_SYMBOL_GPL(dm_table_add_target_callbacks);
  1705. int dm_table_any_congested(struct dm_table *t, int bdi_bits)
  1706. {
  1707. struct dm_dev_internal *dd;
  1708. struct list_head *devices = dm_table_get_devices(t);
  1709. struct dm_target_callbacks *cb;
  1710. int r = 0;
  1711. list_for_each_entry(dd, devices, list) {
  1712. struct request_queue *q = bdev_get_queue(dd->dm_dev->bdev);
  1713. char b[BDEVNAME_SIZE];
  1714. if (likely(q))
  1715. r |= bdi_congested(q->backing_dev_info, bdi_bits);
  1716. else
  1717. DMWARN_LIMIT("%s: any_congested: nonexistent device %s",
  1718. dm_device_name(t->md),
  1719. bdevname(dd->dm_dev->bdev, b));
  1720. }
  1721. list_for_each_entry(cb, &t->target_callbacks, list)
  1722. if (cb->congested_fn)
  1723. r |= cb->congested_fn(cb, bdi_bits);
  1724. return r;
  1725. }
  1726. struct mapped_device *dm_table_get_md(struct dm_table *t)
  1727. {
  1728. return t->md;
  1729. }
  1730. EXPORT_SYMBOL(dm_table_get_md);
  1731. void dm_table_run_md_queue_async(struct dm_table *t)
  1732. {
  1733. struct mapped_device *md;
  1734. struct request_queue *queue;
  1735. unsigned long flags;
  1736. if (!dm_table_request_based(t))
  1737. return;
  1738. md = dm_table_get_md(t);
  1739. queue = dm_get_md_queue(md);
  1740. if (queue) {
  1741. if (queue->mq_ops)
  1742. blk_mq_run_hw_queues(queue, true);
  1743. else {
  1744. spin_lock_irqsave(queue->queue_lock, flags);
  1745. blk_run_queue_async(queue);
  1746. spin_unlock_irqrestore(queue->queue_lock, flags);
  1747. }
  1748. }
  1749. }
  1750. EXPORT_SYMBOL(dm_table_run_md_queue_async);