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