dm-table.c 34 KB

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