dm-table.c 40 KB

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