dm-table.c 55 KB

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