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