dm-table.c 49 KB

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