dm-table.c 54 KB

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