dm-table.c 47 KB

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