Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 Sistina Software (UK) Limited. |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
| 8 | #include "dm.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/ctype.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/interrupt.h> |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 18 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/atomic.h> |
| 20 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 21 | #define DM_MSG_PREFIX "table" |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #define MAX_DEPTH 16 |
| 24 | #define NODE_SIZE L1_CACHE_BYTES |
| 25 | #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t)) |
| 26 | #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1) |
| 27 | |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 28 | /* |
| 29 | * The table has always exactly one reference from either mapped_device->map |
| 30 | * or hash_cell->new_map. This reference is not counted in table->holders. |
| 31 | * A pair of dm_create_table/dm_destroy_table functions is used for table |
| 32 | * creation/destruction. |
| 33 | * |
| 34 | * Temporary references from the other code increase table->holders. A pair |
| 35 | * of dm_table_get/dm_table_put functions is used to manipulate it. |
| 36 | * |
| 37 | * When the table is about to be destroyed, we wait for table->holders to |
| 38 | * drop to zero. |
| 39 | */ |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct dm_table { |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 42 | struct mapped_device *md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | atomic_t holders; |
| 44 | |
| 45 | /* btree table */ |
| 46 | unsigned int depth; |
| 47 | unsigned int counts[MAX_DEPTH]; /* in nodes */ |
| 48 | sector_t *index[MAX_DEPTH]; |
| 49 | |
| 50 | unsigned int num_targets; |
| 51 | unsigned int num_allocated; |
| 52 | sector_t *highs; |
| 53 | struct dm_target *targets; |
| 54 | |
| 55 | /* |
| 56 | * Indicates the rw permissions for the new logical |
| 57 | * device. This should be a combination of FMODE_READ |
| 58 | * and FMODE_WRITE. |
| 59 | */ |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 60 | fmode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | /* a list of devices used by this table */ |
| 63 | struct list_head devices; |
| 64 | |
| 65 | /* |
| 66 | * These are optimistic limits taken from all the |
| 67 | * targets, some targets will need smaller limits. |
| 68 | */ |
Mike Snitzer | 5ab9758 | 2009-06-22 10:12:32 +0100 | [diff] [blame] | 69 | struct queue_limits limits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | /* events get handed up using this callback */ |
| 72 | void (*event_fn)(void *); |
| 73 | void *event_context; |
| 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * Similar to ceiling(log_size(n)) |
| 78 | */ |
| 79 | static unsigned int int_log(unsigned int n, unsigned int base) |
| 80 | { |
| 81 | int result = 0; |
| 82 | |
| 83 | while (n > 1) { |
| 84 | n = dm_div_up(n, base); |
| 85 | result++; |
| 86 | } |
| 87 | |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | * Calculate the index of the child node of the n'th node k'th key. |
| 93 | */ |
| 94 | static inline unsigned int get_child(unsigned int n, unsigned int k) |
| 95 | { |
| 96 | return (n * CHILDREN_PER_NODE) + k; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Return the n'th node of level l from table t. |
| 101 | */ |
| 102 | static inline sector_t *get_node(struct dm_table *t, |
| 103 | unsigned int l, unsigned int n) |
| 104 | { |
| 105 | return t->index[l] + (n * KEYS_PER_NODE); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Return the highest key that you could lookup from the n'th |
| 110 | * node on level l of the btree. |
| 111 | */ |
| 112 | static sector_t high(struct dm_table *t, unsigned int l, unsigned int n) |
| 113 | { |
| 114 | for (; l < t->depth - 1; l++) |
| 115 | n = get_child(n, CHILDREN_PER_NODE - 1); |
| 116 | |
| 117 | if (n >= t->counts[l]) |
| 118 | return (sector_t) - 1; |
| 119 | |
| 120 | return get_node(t, l, n)[KEYS_PER_NODE - 1]; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Fills in a level of the btree based on the highs of the level |
| 125 | * below it. |
| 126 | */ |
| 127 | static int setup_btree_index(unsigned int l, struct dm_table *t) |
| 128 | { |
| 129 | unsigned int n, k; |
| 130 | sector_t *node; |
| 131 | |
| 132 | for (n = 0U; n < t->counts[l]; n++) { |
| 133 | node = get_node(t, l, n); |
| 134 | |
| 135 | for (k = 0U; k < KEYS_PER_NODE; k++) |
| 136 | node[k] = high(t, l + 1, get_child(n, k)); |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size) |
| 143 | { |
| 144 | unsigned long size; |
| 145 | void *addr; |
| 146 | |
| 147 | /* |
| 148 | * Check that we're not going to overflow. |
| 149 | */ |
| 150 | if (nmemb > (ULONG_MAX / elem_size)) |
| 151 | return NULL; |
| 152 | |
| 153 | size = nmemb * elem_size; |
| 154 | addr = vmalloc(size); |
| 155 | if (addr) |
| 156 | memset(addr, 0, size); |
| 157 | |
| 158 | return addr; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * highs, and targets are managed as dynamic arrays during a |
| 163 | * table load. |
| 164 | */ |
| 165 | static int alloc_targets(struct dm_table *t, unsigned int num) |
| 166 | { |
| 167 | sector_t *n_highs; |
| 168 | struct dm_target *n_targets; |
| 169 | int n = t->num_targets; |
| 170 | |
| 171 | /* |
| 172 | * Allocate both the target array and offset array at once. |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 173 | * Append an empty entry to catch sectors beyond the end of |
| 174 | * the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | */ |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 176 | n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | sizeof(sector_t)); |
| 178 | if (!n_highs) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | n_targets = (struct dm_target *) (n_highs + num); |
| 182 | |
| 183 | if (n) { |
| 184 | memcpy(n_highs, t->highs, sizeof(*n_highs) * n); |
| 185 | memcpy(n_targets, t->targets, sizeof(*n_targets) * n); |
| 186 | } |
| 187 | |
| 188 | memset(n_highs + n, -1, sizeof(*n_highs) * (num - n)); |
| 189 | vfree(t->highs); |
| 190 | |
| 191 | t->num_allocated = num; |
| 192 | t->highs = n_highs; |
| 193 | t->targets = n_targets; |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 198 | int dm_table_create(struct dm_table **result, fmode_t mode, |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 199 | unsigned num_targets, struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Dmitry Monakhov | 094262d | 2007-10-19 22:38:51 +0100 | [diff] [blame] | 201 | struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | if (!t) |
| 204 | return -ENOMEM; |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | INIT_LIST_HEAD(&t->devices); |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 207 | atomic_set(&t->holders, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | if (!num_targets) |
| 210 | num_targets = KEYS_PER_NODE; |
| 211 | |
| 212 | num_targets = dm_round_up(num_targets, KEYS_PER_NODE); |
| 213 | |
| 214 | if (alloc_targets(t, num_targets)) { |
| 215 | kfree(t); |
| 216 | t = NULL; |
| 217 | return -ENOMEM; |
| 218 | } |
| 219 | |
| 220 | t->mode = mode; |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 221 | t->md = md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | *result = t; |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static void free_devices(struct list_head *devices) |
| 227 | { |
| 228 | struct list_head *tmp, *next; |
| 229 | |
Paul Jimenez | afb2452 | 2008-02-08 02:09:59 +0000 | [diff] [blame] | 230 | list_for_each_safe(tmp, next, devices) { |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 231 | struct dm_dev_internal *dd = |
| 232 | list_entry(tmp, struct dm_dev_internal, list); |
Jonthan Brassow | 1b6da75 | 2009-06-22 10:12:29 +0100 | [diff] [blame] | 233 | DMWARN("dm_table_destroy: dm_put_device call missing for %s", |
| 234 | dd->dm_dev.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | kfree(dd); |
| 236 | } |
| 237 | } |
| 238 | |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 239 | void dm_table_destroy(struct dm_table *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
| 241 | unsigned int i; |
| 242 | |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 243 | while (atomic_read(&t->holders)) |
| 244 | msleep(1); |
| 245 | smp_mb(); |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | /* free the indexes (see dm_table_complete) */ |
| 248 | if (t->depth >= 2) |
| 249 | vfree(t->index[t->depth - 2]); |
| 250 | |
| 251 | /* free the targets */ |
| 252 | for (i = 0; i < t->num_targets; i++) { |
| 253 | struct dm_target *tgt = t->targets + i; |
| 254 | |
| 255 | if (tgt->type->dtr) |
| 256 | tgt->type->dtr(tgt); |
| 257 | |
| 258 | dm_put_target_type(tgt->type); |
| 259 | } |
| 260 | |
| 261 | vfree(t->highs); |
| 262 | |
| 263 | /* free the device list */ |
Jonthan Brassow | 1b6da75 | 2009-06-22 10:12:29 +0100 | [diff] [blame] | 264 | if (t->devices.next != &t->devices) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | free_devices(&t->devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | kfree(t); |
| 268 | } |
| 269 | |
| 270 | void dm_table_get(struct dm_table *t) |
| 271 | { |
| 272 | atomic_inc(&t->holders); |
| 273 | } |
| 274 | |
| 275 | void dm_table_put(struct dm_table *t) |
| 276 | { |
| 277 | if (!t) |
| 278 | return; |
| 279 | |
Mikulas Patocka | d581687 | 2009-01-06 03:05:10 +0000 | [diff] [blame] | 280 | smp_mb__before_atomic_dec(); |
| 281 | atomic_dec(&t->holders); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Checks to see if we need to extend highs or targets. |
| 286 | */ |
| 287 | static inline int check_space(struct dm_table *t) |
| 288 | { |
| 289 | if (t->num_targets >= t->num_allocated) |
| 290 | return alloc_targets(t, t->num_allocated * 2); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | * See if we've already got a device in the list. |
| 297 | */ |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 298 | static struct dm_dev_internal *find_device(struct list_head *l, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 300 | struct dm_dev_internal *dd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | list_for_each_entry (dd, l, list) |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 303 | if (dd->dm_dev.bdev->bd_dev == dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | return dd; |
| 305 | |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Open a device so we can use it as a map destination. |
| 311 | */ |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 312 | static int open_dev(struct dm_dev_internal *d, dev_t dev, |
| 313 | struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | static char *_claim_ptr = "I belong to device-mapper"; |
| 316 | struct block_device *bdev; |
| 317 | |
| 318 | int r; |
| 319 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 320 | BUG_ON(d->dm_dev.bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 322 | bdev = open_by_devnum(dev, d->dm_dev.mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | if (IS_ERR(bdev)) |
| 324 | return PTR_ERR(bdev); |
Jun'ichi Nomura | f165921 | 2006-03-27 01:17:59 -0800 | [diff] [blame] | 325 | r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | if (r) |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 327 | blkdev_put(bdev, d->dm_dev.mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | else |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 329 | d->dm_dev.bdev = bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return r; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Close a device that we've been using. |
| 335 | */ |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 336 | static void close_dev(struct dm_dev_internal *d, struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 338 | if (!d->dm_dev.bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | return; |
| 340 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 341 | bd_release_from_disk(d->dm_dev.bdev, dm_disk(md)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 342 | blkdev_put(d->dm_dev.bdev, d->dm_dev.mode); |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 343 | d->dm_dev.bdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* |
Mike Anderson | 2cd54d9 | 2007-05-09 02:32:57 -0700 | [diff] [blame] | 347 | * If possible, this checks an area of a destination device is valid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | */ |
Mike Snitzer | 02acc3a | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 349 | static int device_area_is_valid(struct dm_target *ti, struct block_device *bdev, |
| 350 | sector_t start, sector_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Mike Snitzer | 02acc3a | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 352 | sector_t dev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT; |
| 353 | unsigned short logical_block_size_sectors = |
| 354 | ti->limits.logical_block_size >> SECTOR_SHIFT; |
| 355 | char b[BDEVNAME_SIZE]; |
Mike Anderson | 2cd54d9 | 2007-05-09 02:32:57 -0700 | [diff] [blame] | 356 | |
| 357 | if (!dev_size) |
| 358 | return 1; |
| 359 | |
Mike Snitzer | 02acc3a | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 360 | if ((start >= dev_size) || (start + len > dev_size)) { |
| 361 | DMWARN("%s: %s too small for target", |
| 362 | dm_device_name(ti->table->md), bdevname(bdev, b)); |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | if (logical_block_size_sectors <= 1) |
| 367 | return 1; |
| 368 | |
| 369 | if (start & (logical_block_size_sectors - 1)) { |
| 370 | DMWARN("%s: start=%llu not aligned to h/w " |
| 371 | "logical block size %hu of %s", |
| 372 | dm_device_name(ti->table->md), |
| 373 | (unsigned long long)start, |
| 374 | ti->limits.logical_block_size, bdevname(bdev, b)); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | if (len & (logical_block_size_sectors - 1)) { |
| 379 | DMWARN("%s: len=%llu not aligned to h/w " |
| 380 | "logical block size %hu of %s", |
| 381 | dm_device_name(ti->table->md), |
| 382 | (unsigned long long)len, |
| 383 | ti->limits.logical_block_size, bdevname(bdev, b)); |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
Alasdair G Kergon | 570b9d9 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 391 | * This upgrades the mode on an already open dm_dev, being |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | * careful to leave things as they were if we fail to reopen the |
Alasdair G Kergon | 570b9d9 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 393 | * device and not to touch the existing bdev field in case |
| 394 | * it is accessed concurrently inside dm_table_any_congested(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | */ |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 396 | static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode, |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 397 | struct mapped_device *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
| 399 | int r; |
Alasdair G Kergon | 570b9d9 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 400 | struct dm_dev_internal dd_new, dd_old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Alasdair G Kergon | 570b9d9 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 402 | dd_new = dd_old = *dd; |
| 403 | |
| 404 | dd_new.dm_dev.mode |= new_mode; |
| 405 | dd_new.dm_dev.bdev = NULL; |
| 406 | |
| 407 | r = open_dev(&dd_new, dd->dm_dev.bdev->bd_dev, md); |
| 408 | if (r) |
| 409 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 411 | dd->dm_dev.mode |= new_mode; |
Alasdair G Kergon | 570b9d9 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 412 | close_dev(&dd_old, md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Alasdair G Kergon | 570b9d9 | 2009-04-02 19:55:28 +0100 | [diff] [blame] | 414 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Add a device to the list, or just increment the usage count if |
| 419 | * it's already present. |
| 420 | */ |
| 421 | static int __table_get_device(struct dm_table *t, struct dm_target *ti, |
| 422 | const char *path, sector_t start, sector_t len, |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 423 | fmode_t mode, struct dm_dev **result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
| 425 | int r; |
Andrew Morton | 69a2ce7 | 2008-02-08 02:10:14 +0000 | [diff] [blame] | 426 | dev_t uninitialized_var(dev); |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 427 | struct dm_dev_internal *dd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | unsigned int major, minor; |
| 429 | |
Eric Sesterhenn | 547bc92 | 2006-03-26 18:22:50 +0200 | [diff] [blame] | 430 | BUG_ON(!t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | if (sscanf(path, "%u:%u", &major, &minor) == 2) { |
| 433 | /* Extract the major/minor numbers */ |
| 434 | dev = MKDEV(major, minor); |
| 435 | if (MAJOR(dev) != major || MINOR(dev) != minor) |
| 436 | return -EOVERFLOW; |
| 437 | } else { |
| 438 | /* convert the path to a device */ |
Christoph Hellwig | 72e8264 | 2008-08-11 00:24:08 +0200 | [diff] [blame] | 439 | struct block_device *bdev = lookup_bdev(path); |
| 440 | |
| 441 | if (IS_ERR(bdev)) |
| 442 | return PTR_ERR(bdev); |
| 443 | dev = bdev->bd_dev; |
| 444 | bdput(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | dd = find_device(&t->devices, dev); |
| 448 | if (!dd) { |
| 449 | dd = kmalloc(sizeof(*dd), GFP_KERNEL); |
| 450 | if (!dd) |
| 451 | return -ENOMEM; |
| 452 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 453 | dd->dm_dev.mode = mode; |
| 454 | dd->dm_dev.bdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Jun'ichi Nomura | f165921 | 2006-03-27 01:17:59 -0800 | [diff] [blame] | 456 | if ((r = open_dev(dd, dev, t->md))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | kfree(dd); |
| 458 | return r; |
| 459 | } |
| 460 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 461 | format_dev_t(dd->dm_dev.name, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | atomic_set(&dd->count, 0); |
| 464 | list_add(&dd->list, &t->devices); |
| 465 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 466 | } else if (dd->dm_dev.mode != (mode | dd->dm_dev.mode)) { |
Jun'ichi Nomura | f165921 | 2006-03-27 01:17:59 -0800 | [diff] [blame] | 467 | r = upgrade_mode(dd, mode, t->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | if (r) |
| 469 | return r; |
| 470 | } |
| 471 | atomic_inc(&dd->count); |
| 472 | |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 473 | *result = &dd->dm_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
Mike Snitzer | 5ab9758 | 2009-06-22 10:12:32 +0100 | [diff] [blame] | 477 | /* |
| 478 | * Returns the minimum that is _not_ zero, unless both are zero. |
| 479 | */ |
| 480 | #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r)) |
| 481 | |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 482 | void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev) |
| 483 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 484 | struct request_queue *q = bdev_get_queue(bdev); |
Alasdair G Kergon | 0c2322e | 2008-10-10 13:37:13 +0100 | [diff] [blame] | 485 | char b[BDEVNAME_SIZE]; |
| 486 | |
| 487 | if (unlikely(!q)) { |
| 488 | DMWARN("%s: Cannot set limits for nonexistent device %s", |
| 489 | dm_device_name(ti->table->md), bdevname(bdev, b)); |
| 490 | return; |
| 491 | } |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 492 | |
Mike Snitzer | 5ab9758 | 2009-06-22 10:12:32 +0100 | [diff] [blame] | 493 | if (blk_stack_limits(&ti->limits, &q->limits, 0) < 0) |
| 494 | DMWARN("%s: target device %s is misaligned", |
| 495 | dm_device_name(ti->table->md), bdevname(bdev, b)); |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 496 | |
Milan Broz | 9980c63 | 2008-07-21 12:00:39 +0100 | [diff] [blame] | 497 | /* |
| 498 | * Check if merge fn is supported. |
| 499 | * If not we'll force DM to use PAGE_SIZE or |
| 500 | * smaller I/O, just to be safe. |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 501 | */ |
Milan Broz | 9980c63 | 2008-07-21 12:00:39 +0100 | [diff] [blame] | 502 | |
| 503 | if (q->merge_bvec_fn && !ti->type->merge) |
Mike Snitzer | 5ab9758 | 2009-06-22 10:12:32 +0100 | [diff] [blame] | 504 | ti->limits.max_sectors = |
| 505 | min_not_zero(ti->limits.max_sectors, |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 506 | (unsigned int) (PAGE_SIZE >> 9)); |
Bryn Reeves | 3cb4021 | 2006-10-03 01:15:42 -0700 | [diff] [blame] | 507 | } |
| 508 | EXPORT_SYMBOL_GPL(dm_set_device_limits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
| 510 | int dm_get_device(struct dm_target *ti, const char *path, sector_t start, |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 511 | sector_t len, fmode_t mode, struct dm_dev **result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
| 513 | int r = __table_get_device(ti->table, ti, path, |
| 514 | start, len, mode, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Mike Snitzer | 02acc3a | 2009-06-22 10:12:30 +0100 | [diff] [blame] | 516 | if (r) |
| 517 | return r; |
| 518 | |
| 519 | dm_set_device_limits(ti, (*result)->bdev); |
| 520 | |
| 521 | if (!device_area_is_valid(ti, (*result)->bdev, start, len)) { |
| 522 | dm_put_device(ti, *result); |
| 523 | *result = NULL; |
| 524 | return -EINVAL; |
| 525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | return r; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Decrement a devices use count and remove it if necessary. |
| 532 | */ |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 533 | void dm_put_device(struct dm_target *ti, struct dm_dev *d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 535 | struct dm_dev_internal *dd = container_of(d, struct dm_dev_internal, |
| 536 | dm_dev); |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | if (atomic_dec_and_test(&dd->count)) { |
Jun'ichi Nomura | f165921 | 2006-03-27 01:17:59 -0800 | [diff] [blame] | 539 | close_dev(dd, ti->table->md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | list_del(&dd->list); |
| 541 | kfree(dd); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Checks to see if the target joins onto the end of the table. |
| 547 | */ |
| 548 | static int adjoin(struct dm_table *table, struct dm_target *ti) |
| 549 | { |
| 550 | struct dm_target *prev; |
| 551 | |
| 552 | if (!table->num_targets) |
| 553 | return !ti->begin; |
| 554 | |
| 555 | prev = &table->targets[table->num_targets - 1]; |
| 556 | return (ti->begin == (prev->begin + prev->len)); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Used to dynamically allocate the arg array. |
| 561 | */ |
| 562 | static char **realloc_argv(unsigned *array_size, char **old_argv) |
| 563 | { |
| 564 | char **argv; |
| 565 | unsigned new_size; |
| 566 | |
| 567 | new_size = *array_size ? *array_size * 2 : 64; |
| 568 | argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL); |
| 569 | if (argv) { |
| 570 | memcpy(argv, old_argv, *array_size * sizeof(*argv)); |
| 571 | *array_size = new_size; |
| 572 | } |
| 573 | |
| 574 | kfree(old_argv); |
| 575 | return argv; |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Destructively splits up the argument list to pass to ctr. |
| 580 | */ |
| 581 | int dm_split_args(int *argc, char ***argvp, char *input) |
| 582 | { |
| 583 | char *start, *end = input, *out, **argv = NULL; |
| 584 | unsigned array_size = 0; |
| 585 | |
| 586 | *argc = 0; |
David Teigland | 814d686 | 2006-06-26 00:27:31 -0700 | [diff] [blame] | 587 | |
| 588 | if (!input) { |
| 589 | *argvp = NULL; |
| 590 | return 0; |
| 591 | } |
| 592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | argv = realloc_argv(&array_size, argv); |
| 594 | if (!argv) |
| 595 | return -ENOMEM; |
| 596 | |
| 597 | while (1) { |
| 598 | start = end; |
| 599 | |
| 600 | /* Skip whitespace */ |
| 601 | while (*start && isspace(*start)) |
| 602 | start++; |
| 603 | |
| 604 | if (!*start) |
| 605 | break; /* success, we hit the end */ |
| 606 | |
| 607 | /* 'out' is used to remove any back-quotes */ |
| 608 | end = out = start; |
| 609 | while (*end) { |
| 610 | /* Everything apart from '\0' can be quoted */ |
| 611 | if (*end == '\\' && *(end + 1)) { |
| 612 | *out++ = *(end + 1); |
| 613 | end += 2; |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | if (isspace(*end)) |
| 618 | break; /* end of token */ |
| 619 | |
| 620 | *out++ = *end++; |
| 621 | } |
| 622 | |
| 623 | /* have we already filled the array ? */ |
| 624 | if ((*argc + 1) > array_size) { |
| 625 | argv = realloc_argv(&array_size, argv); |
| 626 | if (!argv) |
| 627 | return -ENOMEM; |
| 628 | } |
| 629 | |
| 630 | /* we know this is whitespace */ |
| 631 | if (*end) |
| 632 | end++; |
| 633 | |
| 634 | /* terminate the string and put it in the array */ |
| 635 | *out = '\0'; |
| 636 | argv[*argc] = start; |
| 637 | (*argc)++; |
| 638 | } |
| 639 | |
| 640 | *argvp = argv; |
| 641 | return 0; |
| 642 | } |
| 643 | |
Mike Snitzer | 5ab9758 | 2009-06-22 10:12:32 +0100 | [diff] [blame] | 644 | static void init_valid_queue_limits(struct queue_limits *limits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
Mike Snitzer | 5ab9758 | 2009-06-22 10:12:32 +0100 | [diff] [blame] | 646 | if (!limits->max_sectors) |
| 647 | limits->max_sectors = SAFE_MAX_SECTORS; |
| 648 | if (!limits->max_hw_sectors) |
| 649 | limits->max_hw_sectors = SAFE_MAX_SECTORS; |
| 650 | if (!limits->max_phys_segments) |
| 651 | limits->max_phys_segments = MAX_PHYS_SEGMENTS; |
| 652 | if (!limits->max_hw_segments) |
| 653 | limits->max_hw_segments = MAX_HW_SEGMENTS; |
| 654 | if (!limits->logical_block_size) |
| 655 | limits->logical_block_size = 1 << SECTOR_SHIFT; |
| 656 | if (!limits->physical_block_size) |
| 657 | limits->physical_block_size = 1 << SECTOR_SHIFT; |
| 658 | if (!limits->io_min) |
| 659 | limits->io_min = 1 << SECTOR_SHIFT; |
| 660 | if (!limits->max_segment_size) |
| 661 | limits->max_segment_size = MAX_SEGMENT_SIZE; |
| 662 | if (!limits->seg_boundary_mask) |
| 663 | limits->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK; |
| 664 | if (!limits->bounce_pfn) |
| 665 | limits->bounce_pfn = -1; |
| 666 | /* |
| 667 | * The other fields (alignment_offset, io_opt, misaligned) |
| 668 | * hold 0 from the kzalloc(). |
| 669 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Mike Snitzer | be6d430 | 2009-06-22 10:12:31 +0100 | [diff] [blame] | 672 | /* |
| 673 | * Impose necessary and sufficient conditions on a devices's table such |
| 674 | * that any incoming bio which respects its logical_block_size can be |
| 675 | * processed successfully. If it falls across the boundary between |
| 676 | * two or more targets, the size of each piece it gets split into must |
| 677 | * be compatible with the logical_block_size of the target processing it. |
| 678 | */ |
| 679 | static int validate_hardware_logical_block_alignment(struct dm_table *table) |
| 680 | { |
| 681 | /* |
| 682 | * This function uses arithmetic modulo the logical_block_size |
| 683 | * (in units of 512-byte sectors). |
| 684 | */ |
| 685 | unsigned short device_logical_block_size_sects = |
| 686 | table->limits.logical_block_size >> SECTOR_SHIFT; |
| 687 | |
| 688 | /* |
| 689 | * Offset of the start of the next table entry, mod logical_block_size. |
| 690 | */ |
| 691 | unsigned short next_target_start = 0; |
| 692 | |
| 693 | /* |
| 694 | * Given an aligned bio that extends beyond the end of a |
| 695 | * target, how many sectors must the next target handle? |
| 696 | */ |
| 697 | unsigned short remaining = 0; |
| 698 | |
| 699 | struct dm_target *uninitialized_var(ti); |
| 700 | unsigned i = 0; |
| 701 | |
| 702 | /* |
| 703 | * Check each entry in the table in turn. |
| 704 | */ |
| 705 | while (i < dm_table_get_num_targets(table)) { |
| 706 | ti = dm_table_get_target(table, i++); |
| 707 | |
| 708 | /* |
| 709 | * If the remaining sectors fall entirely within this |
| 710 | * table entry are they compatible with its logical_block_size? |
| 711 | */ |
| 712 | if (remaining < ti->len && |
| 713 | remaining & ((ti->limits.logical_block_size >> |
| 714 | SECTOR_SHIFT) - 1)) |
| 715 | break; /* Error */ |
| 716 | |
| 717 | next_target_start = |
| 718 | (unsigned short) ((next_target_start + ti->len) & |
| 719 | (device_logical_block_size_sects - 1)); |
| 720 | remaining = next_target_start ? |
| 721 | device_logical_block_size_sects - next_target_start : 0; |
| 722 | } |
| 723 | |
| 724 | if (remaining) { |
| 725 | DMWARN("%s: table line %u (start sect %llu len %llu) " |
| 726 | "not aligned to hardware logical block size %hu", |
| 727 | dm_device_name(table->md), i, |
| 728 | (unsigned long long) ti->begin, |
| 729 | (unsigned long long) ti->len, |
| 730 | table->limits.logical_block_size); |
| 731 | return -EINVAL; |
| 732 | } |
| 733 | |
| 734 | return 0; |
| 735 | } |
| 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | int dm_table_add_target(struct dm_table *t, const char *type, |
| 738 | sector_t start, sector_t len, char *params) |
| 739 | { |
| 740 | int r = -EINVAL, argc; |
| 741 | char **argv; |
| 742 | struct dm_target *tgt; |
| 743 | |
| 744 | if ((r = check_space(t))) |
| 745 | return r; |
| 746 | |
| 747 | tgt = t->targets + t->num_targets; |
| 748 | memset(tgt, 0, sizeof(*tgt)); |
| 749 | |
| 750 | if (!len) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 751 | DMERR("%s: zero-length target", dm_device_name(t->md)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | return -EINVAL; |
| 753 | } |
| 754 | |
| 755 | tgt->type = dm_get_target_type(type); |
| 756 | if (!tgt->type) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 757 | DMERR("%s: %s: unknown target type", dm_device_name(t->md), |
| 758 | type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | return -EINVAL; |
| 760 | } |
| 761 | |
| 762 | tgt->table = t; |
| 763 | tgt->begin = start; |
| 764 | tgt->len = len; |
| 765 | tgt->error = "Unknown error"; |
| 766 | |
| 767 | /* |
| 768 | * Does this target adjoin the previous one ? |
| 769 | */ |
| 770 | if (!adjoin(t, tgt)) { |
| 771 | tgt->error = "Gap in table"; |
| 772 | r = -EINVAL; |
| 773 | goto bad; |
| 774 | } |
| 775 | |
| 776 | r = dm_split_args(&argc, &argv, params); |
| 777 | if (r) { |
| 778 | tgt->error = "couldn't split parameters (insufficient memory)"; |
| 779 | goto bad; |
| 780 | } |
| 781 | |
| 782 | r = tgt->type->ctr(tgt, argc, argv); |
| 783 | kfree(argv); |
| 784 | if (r) |
| 785 | goto bad; |
| 786 | |
| 787 | t->highs[t->num_targets++] = tgt->begin + tgt->len - 1; |
| 788 | |
Mike Snitzer | 5ab9758 | 2009-06-22 10:12:32 +0100 | [diff] [blame] | 789 | if (blk_stack_limits(&t->limits, &tgt->limits, 0) < 0) |
| 790 | DMWARN("%s: target device (start sect %llu len %llu) " |
| 791 | "is misaligned", |
| 792 | dm_device_name(t->md), |
| 793 | (unsigned long long) tgt->begin, |
| 794 | (unsigned long long) tgt->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | return 0; |
| 796 | |
| 797 | bad: |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 798 | DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | dm_put_target_type(tgt->type); |
| 800 | return r; |
| 801 | } |
| 802 | |
| 803 | static int setup_indexes(struct dm_table *t) |
| 804 | { |
| 805 | int i; |
| 806 | unsigned int total = 0; |
| 807 | sector_t *indexes; |
| 808 | |
| 809 | /* allocate the space for *all* the indexes */ |
| 810 | for (i = t->depth - 2; i >= 0; i--) { |
| 811 | t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE); |
| 812 | total += t->counts[i]; |
| 813 | } |
| 814 | |
| 815 | indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE); |
| 816 | if (!indexes) |
| 817 | return -ENOMEM; |
| 818 | |
| 819 | /* set up internal nodes, bottom-up */ |
Jun'ichi Nomura | 82d601dc | 2008-02-08 02:10:04 +0000 | [diff] [blame] | 820 | for (i = t->depth - 2; i >= 0; i--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | t->index[i] = indexes; |
| 822 | indexes += (KEYS_PER_NODE * t->counts[i]); |
| 823 | setup_btree_index(i, t); |
| 824 | } |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | /* |
| 830 | * Builds the btree to index the map. |
| 831 | */ |
| 832 | int dm_table_complete(struct dm_table *t) |
| 833 | { |
| 834 | int r = 0; |
| 835 | unsigned int leaf_nodes; |
| 836 | |
Mike Snitzer | 5ab9758 | 2009-06-22 10:12:32 +0100 | [diff] [blame] | 837 | init_valid_queue_limits(&t->limits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
Mike Snitzer | be6d430 | 2009-06-22 10:12:31 +0100 | [diff] [blame] | 839 | r = validate_hardware_logical_block_alignment(t); |
| 840 | if (r) |
| 841 | return r; |
| 842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | /* how many indexes will the btree have ? */ |
| 844 | leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE); |
| 845 | t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE); |
| 846 | |
| 847 | /* leaf layer has already been set up */ |
| 848 | t->counts[t->depth - 1] = leaf_nodes; |
| 849 | t->index[t->depth - 1] = t->highs; |
| 850 | |
| 851 | if (t->depth >= 2) |
| 852 | r = setup_indexes(t); |
| 853 | |
| 854 | return r; |
| 855 | } |
| 856 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 857 | static DEFINE_MUTEX(_event_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | void dm_table_event_callback(struct dm_table *t, |
| 859 | void (*fn)(void *), void *context) |
| 860 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 861 | mutex_lock(&_event_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | t->event_fn = fn; |
| 863 | t->event_context = context; |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 864 | mutex_unlock(&_event_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | void dm_table_event(struct dm_table *t) |
| 868 | { |
| 869 | /* |
| 870 | * You can no longer call dm_table_event() from interrupt |
| 871 | * context, use a bottom half instead. |
| 872 | */ |
| 873 | BUG_ON(in_interrupt()); |
| 874 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 875 | mutex_lock(&_event_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | if (t->event_fn) |
| 877 | t->event_fn(t->event_context); |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 878 | mutex_unlock(&_event_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | sector_t dm_table_get_size(struct dm_table *t) |
| 882 | { |
| 883 | return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0; |
| 884 | } |
| 885 | |
| 886 | struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index) |
| 887 | { |
Milan Broz | 1435353 | 2006-06-26 00:27:27 -0700 | [diff] [blame] | 888 | if (index >= t->num_targets) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return NULL; |
| 890 | |
| 891 | return t->targets + index; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * Search the btree for the correct target. |
Jun'ichi Nomura | 512875b | 2007-12-13 14:15:25 +0000 | [diff] [blame] | 896 | * |
| 897 | * Caller should check returned pointer with dm_target_is_valid() |
| 898 | * to trap I/O beyond end of device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | */ |
| 900 | struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector) |
| 901 | { |
| 902 | unsigned int l, n = 0, k = 0; |
| 903 | sector_t *node; |
| 904 | |
| 905 | for (l = 0; l < t->depth; l++) { |
| 906 | n = get_child(n, k); |
| 907 | node = get_node(t, l, n); |
| 908 | |
| 909 | for (k = 0; k < KEYS_PER_NODE; k++) |
| 910 | if (node[k] >= sector) |
| 911 | break; |
| 912 | } |
| 913 | |
| 914 | return &t->targets[(KEYS_PER_NODE * n) + k]; |
| 915 | } |
| 916 | |
Martin K. Petersen | 9c47008 | 2009-04-09 00:27:12 +0100 | [diff] [blame] | 917 | /* |
| 918 | * Set the integrity profile for this device if all devices used have |
| 919 | * matching profiles. |
| 920 | */ |
| 921 | static void dm_table_set_integrity(struct dm_table *t) |
| 922 | { |
| 923 | struct list_head *devices = dm_table_get_devices(t); |
| 924 | struct dm_dev_internal *prev = NULL, *dd = NULL; |
| 925 | |
| 926 | if (!blk_get_integrity(dm_disk(t->md))) |
| 927 | return; |
| 928 | |
| 929 | list_for_each_entry(dd, devices, list) { |
| 930 | if (prev && |
| 931 | blk_integrity_compare(prev->dm_dev.bdev->bd_disk, |
| 932 | dd->dm_dev.bdev->bd_disk) < 0) { |
| 933 | DMWARN("%s: integrity not set: %s and %s mismatch", |
| 934 | dm_device_name(t->md), |
| 935 | prev->dm_dev.bdev->bd_disk->disk_name, |
| 936 | dd->dm_dev.bdev->bd_disk->disk_name); |
| 937 | goto no_integrity; |
| 938 | } |
| 939 | prev = dd; |
| 940 | } |
| 941 | |
| 942 | if (!prev || !bdev_get_integrity(prev->dm_dev.bdev)) |
| 943 | goto no_integrity; |
| 944 | |
| 945 | blk_integrity_register(dm_disk(t->md), |
| 946 | bdev_get_integrity(prev->dm_dev.bdev)); |
| 947 | |
| 948 | return; |
| 949 | |
| 950 | no_integrity: |
| 951 | blk_integrity_register(dm_disk(t->md), NULL); |
| 952 | |
| 953 | return; |
| 954 | } |
| 955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q) |
| 957 | { |
| 958 | /* |
Mike Snitzer | 1197764 | 2009-06-22 10:12:32 +0100 | [diff] [blame^] | 959 | * Copy table's limits to the DM device's request_queue |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | */ |
Mike Snitzer | 1197764 | 2009-06-22 10:12:32 +0100 | [diff] [blame^] | 961 | q->limits = t->limits; |
Jens Axboe | c9a3f6d | 2008-04-29 19:12:35 +0200 | [diff] [blame] | 962 | |
NeilBrown | 969429b | 2006-03-27 01:17:49 -0800 | [diff] [blame] | 963 | if (t->limits.no_cluster) |
Jens Axboe | c9a3f6d | 2008-04-29 19:12:35 +0200 | [diff] [blame] | 964 | queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q); |
NeilBrown | 969429b | 2006-03-27 01:17:49 -0800 | [diff] [blame] | 965 | else |
Jens Axboe | c9a3f6d | 2008-04-29 19:12:35 +0200 | [diff] [blame] | 966 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q); |
NeilBrown | 969429b | 2006-03-27 01:17:49 -0800 | [diff] [blame] | 967 | |
Martin K. Petersen | 9c47008 | 2009-04-09 00:27:12 +0100 | [diff] [blame] | 968 | dm_table_set_integrity(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | unsigned int dm_table_get_num_targets(struct dm_table *t) |
| 972 | { |
| 973 | return t->num_targets; |
| 974 | } |
| 975 | |
| 976 | struct list_head *dm_table_get_devices(struct dm_table *t) |
| 977 | { |
| 978 | return &t->devices; |
| 979 | } |
| 980 | |
Al Viro | aeb5d72 | 2008-09-02 15:28:45 -0400 | [diff] [blame] | 981 | fmode_t dm_table_get_mode(struct dm_table *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | { |
| 983 | return t->mode; |
| 984 | } |
| 985 | |
| 986 | static void suspend_targets(struct dm_table *t, unsigned postsuspend) |
| 987 | { |
| 988 | int i = t->num_targets; |
| 989 | struct dm_target *ti = t->targets; |
| 990 | |
| 991 | while (i--) { |
| 992 | if (postsuspend) { |
| 993 | if (ti->type->postsuspend) |
| 994 | ti->type->postsuspend(ti); |
| 995 | } else if (ti->type->presuspend) |
| 996 | ti->type->presuspend(ti); |
| 997 | |
| 998 | ti++; |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | void dm_table_presuspend_targets(struct dm_table *t) |
| 1003 | { |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1004 | if (!t) |
| 1005 | return; |
| 1006 | |
Adrian Bunk | e8488d0 | 2008-04-24 22:10:51 +0100 | [diff] [blame] | 1007 | suspend_targets(t, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | void dm_table_postsuspend_targets(struct dm_table *t) |
| 1011 | { |
Alasdair G Kergon | cf222b3 | 2005-07-28 21:15:57 -0700 | [diff] [blame] | 1012 | if (!t) |
| 1013 | return; |
| 1014 | |
Adrian Bunk | e8488d0 | 2008-04-24 22:10:51 +0100 | [diff] [blame] | 1015 | suspend_targets(t, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 1018 | int dm_table_resume_targets(struct dm_table *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | { |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 1020 | int i, r = 0; |
| 1021 | |
| 1022 | for (i = 0; i < t->num_targets; i++) { |
| 1023 | struct dm_target *ti = t->targets + i; |
| 1024 | |
| 1025 | if (!ti->type->preresume) |
| 1026 | continue; |
| 1027 | |
| 1028 | r = ti->type->preresume(ti); |
| 1029 | if (r) |
| 1030 | return r; |
| 1031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
| 1033 | for (i = 0; i < t->num_targets; i++) { |
| 1034 | struct dm_target *ti = t->targets + i; |
| 1035 | |
| 1036 | if (ti->type->resume) |
| 1037 | ti->type->resume(ti); |
| 1038 | } |
Milan Broz | 8757b77 | 2006-10-03 01:15:36 -0700 | [diff] [blame] | 1039 | |
| 1040 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | int dm_table_any_congested(struct dm_table *t, int bdi_bits) |
| 1044 | { |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 1045 | struct dm_dev_internal *dd; |
Paul Jimenez | afb2452 | 2008-02-08 02:09:59 +0000 | [diff] [blame] | 1046 | struct list_head *devices = dm_table_get_devices(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | int r = 0; |
| 1048 | |
Paul Jimenez | afb2452 | 2008-02-08 02:09:59 +0000 | [diff] [blame] | 1049 | list_for_each_entry(dd, devices, list) { |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 1050 | struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev); |
Alasdair G Kergon | 0c2322e | 2008-10-10 13:37:13 +0100 | [diff] [blame] | 1051 | char b[BDEVNAME_SIZE]; |
| 1052 | |
| 1053 | if (likely(q)) |
| 1054 | r |= bdi_congested(&q->backing_dev_info, bdi_bits); |
| 1055 | else |
| 1056 | DMWARN_LIMIT("%s: any_congested: nonexistent device %s", |
| 1057 | dm_device_name(t->md), |
| 1058 | bdevname(dd->dm_dev.bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | return r; |
| 1062 | } |
| 1063 | |
| 1064 | void dm_table_unplug_all(struct dm_table *t) |
| 1065 | { |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 1066 | struct dm_dev_internal *dd; |
Paul Jimenez | afb2452 | 2008-02-08 02:09:59 +0000 | [diff] [blame] | 1067 | struct list_head *devices = dm_table_get_devices(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
Paul Jimenez | afb2452 | 2008-02-08 02:09:59 +0000 | [diff] [blame] | 1069 | list_for_each_entry(dd, devices, list) { |
Mikulas Patocka | 82b1519 | 2008-10-10 13:37:09 +0100 | [diff] [blame] | 1070 | struct request_queue *q = bdev_get_queue(dd->dm_dev.bdev); |
Alasdair G Kergon | 0c2322e | 2008-10-10 13:37:13 +0100 | [diff] [blame] | 1071 | char b[BDEVNAME_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Alasdair G Kergon | 0c2322e | 2008-10-10 13:37:13 +0100 | [diff] [blame] | 1073 | if (likely(q)) |
| 1074 | blk_unplug(q); |
| 1075 | else |
| 1076 | DMWARN_LIMIT("%s: Cannot unplug nonexistent device %s", |
| 1077 | dm_device_name(t->md), |
| 1078 | bdevname(dd->dm_dev.bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1082 | struct mapped_device *dm_table_get_md(struct dm_table *t) |
| 1083 | { |
| 1084 | dm_get(t->md); |
| 1085 | |
| 1086 | return t->md; |
| 1087 | } |
| 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | EXPORT_SYMBOL(dm_vcalloc); |
| 1090 | EXPORT_SYMBOL(dm_get_device); |
| 1091 | EXPORT_SYMBOL(dm_put_device); |
| 1092 | EXPORT_SYMBOL(dm_table_event); |
Alasdair G Kergon | d5e404c | 2005-07-12 15:53:05 -0700 | [diff] [blame] | 1093 | EXPORT_SYMBOL(dm_table_get_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | EXPORT_SYMBOL(dm_table_get_mode); |
Mike Anderson | 1134e5a | 2006-03-27 01:17:54 -0800 | [diff] [blame] | 1095 | EXPORT_SYMBOL(dm_table_get_md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | EXPORT_SYMBOL(dm_table_put); |
| 1097 | EXPORT_SYMBOL(dm_table_get); |
| 1098 | EXPORT_SYMBOL(dm_table_unplug_all); |