dm-table.c 41 KB

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