]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2001 Sistina Software (UK) Limited. | |
3 | * Copyright (C) 2004 Red Hat, Inc. All rights reserved. | |
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> |
1da177e4 LT |
18 | #include <asm/atomic.h> |
19 | ||
72d94861 AK |
20 | #define DM_MSG_PREFIX "table" |
21 | ||
1da177e4 LT |
22 | #define MAX_DEPTH 16 |
23 | #define NODE_SIZE L1_CACHE_BYTES | |
24 | #define KEYS_PER_NODE (NODE_SIZE / sizeof(sector_t)) | |
25 | #define CHILDREN_PER_NODE (KEYS_PER_NODE + 1) | |
26 | ||
27 | struct dm_table { | |
1134e5ae | 28 | struct mapped_device *md; |
1da177e4 LT |
29 | atomic_t holders; |
30 | ||
31 | /* btree table */ | |
32 | unsigned int depth; | |
33 | unsigned int counts[MAX_DEPTH]; /* in nodes */ | |
34 | sector_t *index[MAX_DEPTH]; | |
35 | ||
36 | unsigned int num_targets; | |
37 | unsigned int num_allocated; | |
38 | sector_t *highs; | |
39 | struct dm_target *targets; | |
40 | ||
41 | /* | |
42 | * Indicates the rw permissions for the new logical | |
43 | * device. This should be a combination of FMODE_READ | |
44 | * and FMODE_WRITE. | |
45 | */ | |
46 | int mode; | |
47 | ||
48 | /* a list of devices used by this table */ | |
49 | struct list_head devices; | |
50 | ||
51 | /* | |
52 | * These are optimistic limits taken from all the | |
53 | * targets, some targets will need smaller limits. | |
54 | */ | |
55 | struct io_restrictions limits; | |
56 | ||
57 | /* events get handed up using this callback */ | |
58 | void (*event_fn)(void *); | |
59 | void *event_context; | |
60 | }; | |
61 | ||
62 | /* | |
63 | * Similar to ceiling(log_size(n)) | |
64 | */ | |
65 | static unsigned int int_log(unsigned int n, unsigned int base) | |
66 | { | |
67 | int result = 0; | |
68 | ||
69 | while (n > 1) { | |
70 | n = dm_div_up(n, base); | |
71 | result++; | |
72 | } | |
73 | ||
74 | return result; | |
75 | } | |
76 | ||
77 | /* | |
78 | * Returns the minimum that is _not_ zero, unless both are zero. | |
79 | */ | |
80 | #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r)) | |
81 | ||
82 | /* | |
83 | * Combine two io_restrictions, always taking the lower value. | |
84 | */ | |
85 | static void combine_restrictions_low(struct io_restrictions *lhs, | |
86 | struct io_restrictions *rhs) | |
87 | { | |
88 | lhs->max_sectors = | |
89 | min_not_zero(lhs->max_sectors, rhs->max_sectors); | |
90 | ||
91 | lhs->max_phys_segments = | |
92 | min_not_zero(lhs->max_phys_segments, rhs->max_phys_segments); | |
93 | ||
94 | lhs->max_hw_segments = | |
95 | min_not_zero(lhs->max_hw_segments, rhs->max_hw_segments); | |
96 | ||
97 | lhs->hardsect_size = max(lhs->hardsect_size, rhs->hardsect_size); | |
98 | ||
99 | lhs->max_segment_size = | |
100 | min_not_zero(lhs->max_segment_size, rhs->max_segment_size); | |
101 | ||
102 | lhs->seg_boundary_mask = | |
103 | min_not_zero(lhs->seg_boundary_mask, rhs->seg_boundary_mask); | |
969429b5 N |
104 | |
105 | lhs->no_cluster |= rhs->no_cluster; | |
1da177e4 LT |
106 | } |
107 | ||
108 | /* | |
109 | * Calculate the index of the child node of the n'th node k'th key. | |
110 | */ | |
111 | static inline unsigned int get_child(unsigned int n, unsigned int k) | |
112 | { | |
113 | return (n * CHILDREN_PER_NODE) + k; | |
114 | } | |
115 | ||
116 | /* | |
117 | * Return the n'th node of level l from table t. | |
118 | */ | |
119 | static inline sector_t *get_node(struct dm_table *t, | |
120 | unsigned int l, unsigned int n) | |
121 | { | |
122 | return t->index[l] + (n * KEYS_PER_NODE); | |
123 | } | |
124 | ||
125 | /* | |
126 | * Return the highest key that you could lookup from the n'th | |
127 | * node on level l of the btree. | |
128 | */ | |
129 | static sector_t high(struct dm_table *t, unsigned int l, unsigned int n) | |
130 | { | |
131 | for (; l < t->depth - 1; l++) | |
132 | n = get_child(n, CHILDREN_PER_NODE - 1); | |
133 | ||
134 | if (n >= t->counts[l]) | |
135 | return (sector_t) - 1; | |
136 | ||
137 | return get_node(t, l, n)[KEYS_PER_NODE - 1]; | |
138 | } | |
139 | ||
140 | /* | |
141 | * Fills in a level of the btree based on the highs of the level | |
142 | * below it. | |
143 | */ | |
144 | static int setup_btree_index(unsigned int l, struct dm_table *t) | |
145 | { | |
146 | unsigned int n, k; | |
147 | sector_t *node; | |
148 | ||
149 | for (n = 0U; n < t->counts[l]; n++) { | |
150 | node = get_node(t, l, n); | |
151 | ||
152 | for (k = 0U; k < KEYS_PER_NODE; k++) | |
153 | node[k] = high(t, l + 1, get_child(n, k)); | |
154 | } | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | void *dm_vcalloc(unsigned long nmemb, unsigned long elem_size) | |
160 | { | |
161 | unsigned long size; | |
162 | void *addr; | |
163 | ||
164 | /* | |
165 | * Check that we're not going to overflow. | |
166 | */ | |
167 | if (nmemb > (ULONG_MAX / elem_size)) | |
168 | return NULL; | |
169 | ||
170 | size = nmemb * elem_size; | |
171 | addr = vmalloc(size); | |
172 | if (addr) | |
173 | memset(addr, 0, size); | |
174 | ||
175 | return addr; | |
176 | } | |
177 | ||
178 | /* | |
179 | * highs, and targets are managed as dynamic arrays during a | |
180 | * table load. | |
181 | */ | |
182 | static int alloc_targets(struct dm_table *t, unsigned int num) | |
183 | { | |
184 | sector_t *n_highs; | |
185 | struct dm_target *n_targets; | |
186 | int n = t->num_targets; | |
187 | ||
188 | /* | |
189 | * Allocate both the target array and offset array at once. | |
190 | */ | |
191 | n_highs = (sector_t *) dm_vcalloc(num, sizeof(struct dm_target) + | |
192 | sizeof(sector_t)); | |
193 | if (!n_highs) | |
194 | return -ENOMEM; | |
195 | ||
196 | n_targets = (struct dm_target *) (n_highs + num); | |
197 | ||
198 | if (n) { | |
199 | memcpy(n_highs, t->highs, sizeof(*n_highs) * n); | |
200 | memcpy(n_targets, t->targets, sizeof(*n_targets) * n); | |
201 | } | |
202 | ||
203 | memset(n_highs + n, -1, sizeof(*n_highs) * (num - n)); | |
204 | vfree(t->highs); | |
205 | ||
206 | t->num_allocated = num; | |
207 | t->highs = n_highs; | |
208 | t->targets = n_targets; | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
1134e5ae MA |
213 | int dm_table_create(struct dm_table **result, int mode, |
214 | unsigned num_targets, struct mapped_device *md) | |
1da177e4 | 215 | { |
094262db | 216 | struct dm_table *t = kzalloc(sizeof(*t), GFP_KERNEL); |
1da177e4 LT |
217 | |
218 | if (!t) | |
219 | return -ENOMEM; | |
220 | ||
1da177e4 LT |
221 | INIT_LIST_HEAD(&t->devices); |
222 | atomic_set(&t->holders, 1); | |
223 | ||
224 | if (!num_targets) | |
225 | num_targets = KEYS_PER_NODE; | |
226 | ||
227 | num_targets = dm_round_up(num_targets, KEYS_PER_NODE); | |
228 | ||
229 | if (alloc_targets(t, num_targets)) { | |
230 | kfree(t); | |
231 | t = NULL; | |
232 | return -ENOMEM; | |
233 | } | |
234 | ||
235 | t->mode = mode; | |
1134e5ae | 236 | t->md = md; |
1da177e4 LT |
237 | *result = t; |
238 | return 0; | |
239 | } | |
240 | ||
c2ade42d DT |
241 | int dm_create_error_table(struct dm_table **result, struct mapped_device *md) |
242 | { | |
243 | struct dm_table *t; | |
244 | sector_t dev_size = 1; | |
245 | int r; | |
246 | ||
247 | /* | |
248 | * Find current size of device. | |
249 | * Default to 1 sector if inactive. | |
250 | */ | |
251 | t = dm_get_table(md); | |
252 | if (t) { | |
253 | dev_size = dm_table_get_size(t); | |
254 | dm_table_put(t); | |
255 | } | |
256 | ||
257 | r = dm_table_create(&t, FMODE_READ, 1, md); | |
258 | if (r) | |
259 | return r; | |
260 | ||
261 | r = dm_table_add_target(t, "error", 0, dev_size, NULL); | |
262 | if (r) | |
263 | goto out; | |
264 | ||
265 | r = dm_table_complete(t); | |
266 | if (r) | |
267 | goto out; | |
268 | ||
269 | *result = t; | |
270 | ||
271 | out: | |
272 | if (r) | |
273 | dm_table_put(t); | |
274 | ||
275 | return r; | |
276 | } | |
277 | EXPORT_SYMBOL_GPL(dm_create_error_table); | |
278 | ||
1da177e4 LT |
279 | static void free_devices(struct list_head *devices) |
280 | { | |
281 | struct list_head *tmp, *next; | |
282 | ||
283 | for (tmp = devices->next; tmp != devices; tmp = next) { | |
284 | struct dm_dev *dd = list_entry(tmp, struct dm_dev, list); | |
285 | next = tmp->next; | |
286 | kfree(dd); | |
287 | } | |
288 | } | |
289 | ||
5e198d94 | 290 | static void table_destroy(struct dm_table *t) |
1da177e4 LT |
291 | { |
292 | unsigned int i; | |
293 | ||
294 | /* free the indexes (see dm_table_complete) */ | |
295 | if (t->depth >= 2) | |
296 | vfree(t->index[t->depth - 2]); | |
297 | ||
298 | /* free the targets */ | |
299 | for (i = 0; i < t->num_targets; i++) { | |
300 | struct dm_target *tgt = t->targets + i; | |
301 | ||
302 | if (tgt->type->dtr) | |
303 | tgt->type->dtr(tgt); | |
304 | ||
305 | dm_put_target_type(tgt->type); | |
306 | } | |
307 | ||
308 | vfree(t->highs); | |
309 | ||
310 | /* free the device list */ | |
311 | if (t->devices.next != &t->devices) { | |
312 | DMWARN("devices still present during destroy: " | |
313 | "dm_table_remove_device calls missing"); | |
314 | ||
315 | free_devices(&t->devices); | |
316 | } | |
317 | ||
318 | kfree(t); | |
319 | } | |
320 | ||
321 | void dm_table_get(struct dm_table *t) | |
322 | { | |
323 | atomic_inc(&t->holders); | |
324 | } | |
325 | ||
326 | void dm_table_put(struct dm_table *t) | |
327 | { | |
328 | if (!t) | |
329 | return; | |
330 | ||
331 | if (atomic_dec_and_test(&t->holders)) | |
332 | table_destroy(t); | |
333 | } | |
334 | ||
335 | /* | |
336 | * Checks to see if we need to extend highs or targets. | |
337 | */ | |
338 | static inline int check_space(struct dm_table *t) | |
339 | { | |
340 | if (t->num_targets >= t->num_allocated) | |
341 | return alloc_targets(t, t->num_allocated * 2); | |
342 | ||
343 | return 0; | |
344 | } | |
345 | ||
346 | /* | |
347 | * Convert a device path to a dev_t. | |
348 | */ | |
349 | static int lookup_device(const char *path, dev_t *dev) | |
350 | { | |
351 | int r; | |
352 | struct nameidata nd; | |
353 | struct inode *inode; | |
354 | ||
355 | if ((r = path_lookup(path, LOOKUP_FOLLOW, &nd))) | |
356 | return r; | |
357 | ||
358 | inode = nd.dentry->d_inode; | |
359 | if (!inode) { | |
360 | r = -ENOENT; | |
361 | goto out; | |
362 | } | |
363 | ||
364 | if (!S_ISBLK(inode->i_mode)) { | |
365 | r = -ENOTBLK; | |
366 | goto out; | |
367 | } | |
368 | ||
369 | *dev = inode->i_rdev; | |
370 | ||
371 | out: | |
372 | path_release(&nd); | |
373 | return r; | |
374 | } | |
375 | ||
376 | /* | |
377 | * See if we've already got a device in the list. | |
378 | */ | |
379 | static struct dm_dev *find_device(struct list_head *l, dev_t dev) | |
380 | { | |
381 | struct dm_dev *dd; | |
382 | ||
383 | list_for_each_entry (dd, l, list) | |
384 | if (dd->bdev->bd_dev == dev) | |
385 | return dd; | |
386 | ||
387 | return NULL; | |
388 | } | |
389 | ||
390 | /* | |
391 | * Open a device so we can use it as a map destination. | |
392 | */ | |
f165921d | 393 | static int open_dev(struct dm_dev *d, dev_t dev, struct mapped_device *md) |
1da177e4 LT |
394 | { |
395 | static char *_claim_ptr = "I belong to device-mapper"; | |
396 | struct block_device *bdev; | |
397 | ||
398 | int r; | |
399 | ||
547bc926 | 400 | BUG_ON(d->bdev); |
1da177e4 LT |
401 | |
402 | bdev = open_by_devnum(dev, d->mode); | |
403 | if (IS_ERR(bdev)) | |
404 | return PTR_ERR(bdev); | |
f165921d | 405 | r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md)); |
1da177e4 LT |
406 | if (r) |
407 | blkdev_put(bdev); | |
408 | else | |
409 | d->bdev = bdev; | |
410 | return r; | |
411 | } | |
412 | ||
413 | /* | |
414 | * Close a device that we've been using. | |
415 | */ | |
f165921d | 416 | static void close_dev(struct dm_dev *d, struct mapped_device *md) |
1da177e4 LT |
417 | { |
418 | if (!d->bdev) | |
419 | return; | |
420 | ||
f165921d | 421 | bd_release_from_disk(d->bdev, dm_disk(md)); |
1da177e4 LT |
422 | blkdev_put(d->bdev); |
423 | d->bdev = NULL; | |
424 | } | |
425 | ||
426 | /* | |
2cd54d9b | 427 | * If possible, this checks an area of a destination device is valid. |
1da177e4 LT |
428 | */ |
429 | static int check_device_area(struct dm_dev *dd, sector_t start, sector_t len) | |
430 | { | |
2cd54d9b MA |
431 | sector_t dev_size = dd->bdev->bd_inode->i_size >> SECTOR_SHIFT; |
432 | ||
433 | if (!dev_size) | |
434 | return 1; | |
435 | ||
1da177e4 LT |
436 | return ((start < dev_size) && (len <= (dev_size - start))); |
437 | } | |
438 | ||
439 | /* | |
440 | * This upgrades the mode on an already open dm_dev. Being | |
441 | * careful to leave things as they were if we fail to reopen the | |
442 | * device. | |
443 | */ | |
f165921d | 444 | static int upgrade_mode(struct dm_dev *dd, int new_mode, struct mapped_device *md) |
1da177e4 LT |
445 | { |
446 | int r; | |
447 | struct dm_dev dd_copy; | |
448 | dev_t dev = dd->bdev->bd_dev; | |
449 | ||
450 | dd_copy = *dd; | |
451 | ||
452 | dd->mode |= new_mode; | |
453 | dd->bdev = NULL; | |
f165921d | 454 | r = open_dev(dd, dev, md); |
1da177e4 | 455 | if (!r) |
f165921d | 456 | close_dev(&dd_copy, md); |
1da177e4 LT |
457 | else |
458 | *dd = dd_copy; | |
459 | ||
460 | return r; | |
461 | } | |
462 | ||
463 | /* | |
464 | * Add a device to the list, or just increment the usage count if | |
465 | * it's already present. | |
466 | */ | |
467 | static int __table_get_device(struct dm_table *t, struct dm_target *ti, | |
468 | const char *path, sector_t start, sector_t len, | |
469 | int mode, struct dm_dev **result) | |
470 | { | |
471 | int r; | |
472 | dev_t dev; | |
473 | struct dm_dev *dd; | |
474 | unsigned int major, minor; | |
475 | ||
547bc926 | 476 | BUG_ON(!t); |
1da177e4 LT |
477 | |
478 | if (sscanf(path, "%u:%u", &major, &minor) == 2) { | |
479 | /* Extract the major/minor numbers */ | |
480 | dev = MKDEV(major, minor); | |
481 | if (MAJOR(dev) != major || MINOR(dev) != minor) | |
482 | return -EOVERFLOW; | |
483 | } else { | |
484 | /* convert the path to a device */ | |
485 | if ((r = lookup_device(path, &dev))) | |
486 | return r; | |
487 | } | |
488 | ||
489 | dd = find_device(&t->devices, dev); | |
490 | if (!dd) { | |
491 | dd = kmalloc(sizeof(*dd), GFP_KERNEL); | |
492 | if (!dd) | |
493 | return -ENOMEM; | |
494 | ||
495 | dd->mode = mode; | |
496 | dd->bdev = NULL; | |
497 | ||
f165921d | 498 | if ((r = open_dev(dd, dev, t->md))) { |
1da177e4 LT |
499 | kfree(dd); |
500 | return r; | |
501 | } | |
502 | ||
503 | format_dev_t(dd->name, dev); | |
504 | ||
505 | atomic_set(&dd->count, 0); | |
506 | list_add(&dd->list, &t->devices); | |
507 | ||
508 | } else if (dd->mode != (mode | dd->mode)) { | |
f165921d | 509 | r = upgrade_mode(dd, mode, t->md); |
1da177e4 LT |
510 | if (r) |
511 | return r; | |
512 | } | |
513 | atomic_inc(&dd->count); | |
514 | ||
515 | if (!check_device_area(dd, start, len)) { | |
516 | DMWARN("device %s too small for target", path); | |
517 | dm_put_device(ti, dd); | |
518 | return -EINVAL; | |
519 | } | |
520 | ||
521 | *result = dd; | |
522 | ||
523 | return 0; | |
524 | } | |
525 | ||
3cb40214 | 526 | void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev) |
1da177e4 | 527 | { |
165125e1 | 528 | struct request_queue *q = bdev_get_queue(bdev); |
3cb40214 BR |
529 | struct io_restrictions *rs = &ti->limits; |
530 | ||
531 | /* | |
532 | * Combine the device limits low. | |
533 | * | |
534 | * FIXME: if we move an io_restriction struct | |
535 | * into q this would just be a call to | |
536 | * combine_restrictions_low() | |
537 | */ | |
538 | rs->max_sectors = | |
539 | min_not_zero(rs->max_sectors, q->max_sectors); | |
540 | ||
541 | /* FIXME: Device-Mapper on top of RAID-0 breaks because DM | |
542 | * currently doesn't honor MD's merge_bvec_fn routine. | |
543 | * In this case, we'll force DM to use PAGE_SIZE or | |
544 | * smaller I/O, just to be safe. A better fix is in the | |
545 | * works, but add this for the time being so it will at | |
546 | * least operate correctly. | |
547 | */ | |
548 | if (q->merge_bvec_fn) | |
1da177e4 | 549 | rs->max_sectors = |
3cb40214 BR |
550 | min_not_zero(rs->max_sectors, |
551 | (unsigned int) (PAGE_SIZE >> 9)); | |
1da177e4 | 552 | |
3cb40214 BR |
553 | rs->max_phys_segments = |
554 | min_not_zero(rs->max_phys_segments, | |
555 | q->max_phys_segments); | |
1da177e4 | 556 | |
3cb40214 BR |
557 | rs->max_hw_segments = |
558 | min_not_zero(rs->max_hw_segments, q->max_hw_segments); | |
1da177e4 | 559 | |
3cb40214 | 560 | rs->hardsect_size = max(rs->hardsect_size, q->hardsect_size); |
1da177e4 | 561 | |
3cb40214 BR |
562 | rs->max_segment_size = |
563 | min_not_zero(rs->max_segment_size, q->max_segment_size); | |
1da177e4 | 564 | |
3cb40214 BR |
565 | rs->seg_boundary_mask = |
566 | min_not_zero(rs->seg_boundary_mask, | |
567 | q->seg_boundary_mask); | |
1da177e4 | 568 | |
3cb40214 BR |
569 | rs->no_cluster |= !test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags); |
570 | } | |
571 | EXPORT_SYMBOL_GPL(dm_set_device_limits); | |
969429b5 | 572 | |
3cb40214 BR |
573 | int dm_get_device(struct dm_target *ti, const char *path, sector_t start, |
574 | sector_t len, int mode, struct dm_dev **result) | |
575 | { | |
576 | int r = __table_get_device(ti->table, ti, path, | |
577 | start, len, mode, result); | |
578 | ||
579 | if (!r) | |
580 | dm_set_device_limits(ti, (*result)->bdev); | |
1da177e4 LT |
581 | |
582 | return r; | |
583 | } | |
584 | ||
585 | /* | |
586 | * Decrement a devices use count and remove it if necessary. | |
587 | */ | |
588 | void dm_put_device(struct dm_target *ti, struct dm_dev *dd) | |
589 | { | |
590 | if (atomic_dec_and_test(&dd->count)) { | |
f165921d | 591 | close_dev(dd, ti->table->md); |
1da177e4 LT |
592 | list_del(&dd->list); |
593 | kfree(dd); | |
594 | } | |
595 | } | |
596 | ||
597 | /* | |
598 | * Checks to see if the target joins onto the end of the table. | |
599 | */ | |
600 | static int adjoin(struct dm_table *table, struct dm_target *ti) | |
601 | { | |
602 | struct dm_target *prev; | |
603 | ||
604 | if (!table->num_targets) | |
605 | return !ti->begin; | |
606 | ||
607 | prev = &table->targets[table->num_targets - 1]; | |
608 | return (ti->begin == (prev->begin + prev->len)); | |
609 | } | |
610 | ||
611 | /* | |
612 | * Used to dynamically allocate the arg array. | |
613 | */ | |
614 | static char **realloc_argv(unsigned *array_size, char **old_argv) | |
615 | { | |
616 | char **argv; | |
617 | unsigned new_size; | |
618 | ||
619 | new_size = *array_size ? *array_size * 2 : 64; | |
620 | argv = kmalloc(new_size * sizeof(*argv), GFP_KERNEL); | |
621 | if (argv) { | |
622 | memcpy(argv, old_argv, *array_size * sizeof(*argv)); | |
623 | *array_size = new_size; | |
624 | } | |
625 | ||
626 | kfree(old_argv); | |
627 | return argv; | |
628 | } | |
629 | ||
630 | /* | |
631 | * Destructively splits up the argument list to pass to ctr. | |
632 | */ | |
633 | int dm_split_args(int *argc, char ***argvp, char *input) | |
634 | { | |
635 | char *start, *end = input, *out, **argv = NULL; | |
636 | unsigned array_size = 0; | |
637 | ||
638 | *argc = 0; | |
814d6862 DT |
639 | |
640 | if (!input) { | |
641 | *argvp = NULL; | |
642 | return 0; | |
643 | } | |
644 | ||
1da177e4 LT |
645 | argv = realloc_argv(&array_size, argv); |
646 | if (!argv) | |
647 | return -ENOMEM; | |
648 | ||
649 | while (1) { | |
650 | start = end; | |
651 | ||
652 | /* Skip whitespace */ | |
653 | while (*start && isspace(*start)) | |
654 | start++; | |
655 | ||
656 | if (!*start) | |
657 | break; /* success, we hit the end */ | |
658 | ||
659 | /* 'out' is used to remove any back-quotes */ | |
660 | end = out = start; | |
661 | while (*end) { | |
662 | /* Everything apart from '\0' can be quoted */ | |
663 | if (*end == '\\' && *(end + 1)) { | |
664 | *out++ = *(end + 1); | |
665 | end += 2; | |
666 | continue; | |
667 | } | |
668 | ||
669 | if (isspace(*end)) | |
670 | break; /* end of token */ | |
671 | ||
672 | *out++ = *end++; | |
673 | } | |
674 | ||
675 | /* have we already filled the array ? */ | |
676 | if ((*argc + 1) > array_size) { | |
677 | argv = realloc_argv(&array_size, argv); | |
678 | if (!argv) | |
679 | return -ENOMEM; | |
680 | } | |
681 | ||
682 | /* we know this is whitespace */ | |
683 | if (*end) | |
684 | end++; | |
685 | ||
686 | /* terminate the string and put it in the array */ | |
687 | *out = '\0'; | |
688 | argv[*argc] = start; | |
689 | (*argc)++; | |
690 | } | |
691 | ||
692 | *argvp = argv; | |
693 | return 0; | |
694 | } | |
695 | ||
696 | static void check_for_valid_limits(struct io_restrictions *rs) | |
697 | { | |
698 | if (!rs->max_sectors) | |
defd94b7 | 699 | rs->max_sectors = SAFE_MAX_SECTORS; |
1da177e4 LT |
700 | if (!rs->max_phys_segments) |
701 | rs->max_phys_segments = MAX_PHYS_SEGMENTS; | |
702 | if (!rs->max_hw_segments) | |
703 | rs->max_hw_segments = MAX_HW_SEGMENTS; | |
704 | if (!rs->hardsect_size) | |
705 | rs->hardsect_size = 1 << SECTOR_SHIFT; | |
706 | if (!rs->max_segment_size) | |
707 | rs->max_segment_size = MAX_SEGMENT_SIZE; | |
708 | if (!rs->seg_boundary_mask) | |
709 | rs->seg_boundary_mask = -1; | |
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 ((r = check_space(t))) | |
720 | return r; | |
721 | ||
722 | tgt = t->targets + t->num_targets; | |
723 | memset(tgt, 0, sizeof(*tgt)); | |
724 | ||
725 | if (!len) { | |
72d94861 | 726 | DMERR("%s: zero-length target", dm_device_name(t->md)); |
1da177e4 LT |
727 | return -EINVAL; |
728 | } | |
729 | ||
730 | tgt->type = dm_get_target_type(type); | |
731 | if (!tgt->type) { | |
72d94861 AK |
732 | DMERR("%s: %s: unknown target type", dm_device_name(t->md), |
733 | type); | |
1da177e4 LT |
734 | return -EINVAL; |
735 | } | |
736 | ||
737 | tgt->table = t; | |
738 | tgt->begin = start; | |
739 | tgt->len = len; | |
740 | tgt->error = "Unknown error"; | |
741 | ||
742 | /* | |
743 | * Does this target adjoin the previous one ? | |
744 | */ | |
745 | if (!adjoin(t, tgt)) { | |
746 | tgt->error = "Gap in table"; | |
747 | r = -EINVAL; | |
748 | goto bad; | |
749 | } | |
750 | ||
751 | r = dm_split_args(&argc, &argv, params); | |
752 | if (r) { | |
753 | tgt->error = "couldn't split parameters (insufficient memory)"; | |
754 | goto bad; | |
755 | } | |
756 | ||
757 | r = tgt->type->ctr(tgt, argc, argv); | |
758 | kfree(argv); | |
759 | if (r) | |
760 | goto bad; | |
761 | ||
762 | t->highs[t->num_targets++] = tgt->begin + tgt->len - 1; | |
763 | ||
764 | /* FIXME: the plan is to combine high here and then have | |
765 | * the merge fn apply the target level restrictions. */ | |
766 | combine_restrictions_low(&t->limits, &tgt->limits); | |
767 | return 0; | |
768 | ||
769 | bad: | |
72d94861 | 770 | DMERR("%s: %s: %s", dm_device_name(t->md), type, tgt->error); |
1da177e4 LT |
771 | dm_put_target_type(tgt->type); |
772 | return r; | |
773 | } | |
774 | ||
775 | static int setup_indexes(struct dm_table *t) | |
776 | { | |
777 | int i; | |
778 | unsigned int total = 0; | |
779 | sector_t *indexes; | |
780 | ||
781 | /* allocate the space for *all* the indexes */ | |
782 | for (i = t->depth - 2; i >= 0; i--) { | |
783 | t->counts[i] = dm_div_up(t->counts[i + 1], CHILDREN_PER_NODE); | |
784 | total += t->counts[i]; | |
785 | } | |
786 | ||
787 | indexes = (sector_t *) dm_vcalloc(total, (unsigned long) NODE_SIZE); | |
788 | if (!indexes) | |
789 | return -ENOMEM; | |
790 | ||
791 | /* set up internal nodes, bottom-up */ | |
792 | for (i = t->depth - 2, total = 0; i >= 0; i--) { | |
793 | t->index[i] = indexes; | |
794 | indexes += (KEYS_PER_NODE * t->counts[i]); | |
795 | setup_btree_index(i, t); | |
796 | } | |
797 | ||
798 | return 0; | |
799 | } | |
800 | ||
801 | /* | |
802 | * Builds the btree to index the map. | |
803 | */ | |
804 | int dm_table_complete(struct dm_table *t) | |
805 | { | |
806 | int r = 0; | |
807 | unsigned int leaf_nodes; | |
808 | ||
809 | check_for_valid_limits(&t->limits); | |
810 | ||
811 | /* how many indexes will the btree have ? */ | |
812 | leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE); | |
813 | t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE); | |
814 | ||
815 | /* leaf layer has already been set up */ | |
816 | t->counts[t->depth - 1] = leaf_nodes; | |
817 | t->index[t->depth - 1] = t->highs; | |
818 | ||
819 | if (t->depth >= 2) | |
820 | r = setup_indexes(t); | |
821 | ||
822 | return r; | |
823 | } | |
824 | ||
48c9c27b | 825 | static DEFINE_MUTEX(_event_lock); |
1da177e4 LT |
826 | void dm_table_event_callback(struct dm_table *t, |
827 | void (*fn)(void *), void *context) | |
828 | { | |
48c9c27b | 829 | mutex_lock(&_event_lock); |
1da177e4 LT |
830 | t->event_fn = fn; |
831 | t->event_context = context; | |
48c9c27b | 832 | mutex_unlock(&_event_lock); |
1da177e4 LT |
833 | } |
834 | ||
835 | void dm_table_event(struct dm_table *t) | |
836 | { | |
837 | /* | |
838 | * You can no longer call dm_table_event() from interrupt | |
839 | * context, use a bottom half instead. | |
840 | */ | |
841 | BUG_ON(in_interrupt()); | |
842 | ||
48c9c27b | 843 | mutex_lock(&_event_lock); |
1da177e4 LT |
844 | if (t->event_fn) |
845 | t->event_fn(t->event_context); | |
48c9c27b | 846 | mutex_unlock(&_event_lock); |
1da177e4 LT |
847 | } |
848 | ||
849 | sector_t dm_table_get_size(struct dm_table *t) | |
850 | { | |
851 | return t->num_targets ? (t->highs[t->num_targets - 1] + 1) : 0; | |
852 | } | |
853 | ||
854 | struct dm_target *dm_table_get_target(struct dm_table *t, unsigned int index) | |
855 | { | |
14353539 | 856 | if (index >= t->num_targets) |
1da177e4 LT |
857 | return NULL; |
858 | ||
859 | return t->targets + index; | |
860 | } | |
861 | ||
862 | /* | |
863 | * Search the btree for the correct target. | |
864 | */ | |
865 | struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector) | |
866 | { | |
867 | unsigned int l, n = 0, k = 0; | |
868 | sector_t *node; | |
869 | ||
870 | for (l = 0; l < t->depth; l++) { | |
871 | n = get_child(n, k); | |
872 | node = get_node(t, l, n); | |
873 | ||
874 | for (k = 0; k < KEYS_PER_NODE; k++) | |
875 | if (node[k] >= sector) | |
876 | break; | |
877 | } | |
878 | ||
879 | return &t->targets[(KEYS_PER_NODE * n) + k]; | |
880 | } | |
881 | ||
882 | void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q) | |
883 | { | |
884 | /* | |
885 | * Make sure we obey the optimistic sub devices | |
886 | * restrictions. | |
887 | */ | |
888 | blk_queue_max_sectors(q, t->limits.max_sectors); | |
889 | q->max_phys_segments = t->limits.max_phys_segments; | |
890 | q->max_hw_segments = t->limits.max_hw_segments; | |
891 | q->hardsect_size = t->limits.hardsect_size; | |
892 | q->max_segment_size = t->limits.max_segment_size; | |
893 | q->seg_boundary_mask = t->limits.seg_boundary_mask; | |
969429b5 N |
894 | if (t->limits.no_cluster) |
895 | q->queue_flags &= ~(1 << QUEUE_FLAG_CLUSTER); | |
896 | else | |
897 | q->queue_flags |= (1 << QUEUE_FLAG_CLUSTER); | |
898 | ||
1da177e4 LT |
899 | } |
900 | ||
901 | unsigned int dm_table_get_num_targets(struct dm_table *t) | |
902 | { | |
903 | return t->num_targets; | |
904 | } | |
905 | ||
906 | struct list_head *dm_table_get_devices(struct dm_table *t) | |
907 | { | |
908 | return &t->devices; | |
909 | } | |
910 | ||
911 | int dm_table_get_mode(struct dm_table *t) | |
912 | { | |
913 | return t->mode; | |
914 | } | |
915 | ||
916 | static void suspend_targets(struct dm_table *t, unsigned postsuspend) | |
917 | { | |
918 | int i = t->num_targets; | |
919 | struct dm_target *ti = t->targets; | |
920 | ||
921 | while (i--) { | |
922 | if (postsuspend) { | |
923 | if (ti->type->postsuspend) | |
924 | ti->type->postsuspend(ti); | |
925 | } else if (ti->type->presuspend) | |
926 | ti->type->presuspend(ti); | |
927 | ||
928 | ti++; | |
929 | } | |
930 | } | |
931 | ||
932 | void dm_table_presuspend_targets(struct dm_table *t) | |
933 | { | |
cf222b37 AK |
934 | if (!t) |
935 | return; | |
936 | ||
1da177e4 LT |
937 | return suspend_targets(t, 0); |
938 | } | |
939 | ||
940 | void dm_table_postsuspend_targets(struct dm_table *t) | |
941 | { | |
cf222b37 AK |
942 | if (!t) |
943 | return; | |
944 | ||
1da177e4 LT |
945 | return suspend_targets(t, 1); |
946 | } | |
947 | ||
8757b776 | 948 | int dm_table_resume_targets(struct dm_table *t) |
1da177e4 | 949 | { |
8757b776 MB |
950 | int i, r = 0; |
951 | ||
952 | for (i = 0; i < t->num_targets; i++) { | |
953 | struct dm_target *ti = t->targets + i; | |
954 | ||
955 | if (!ti->type->preresume) | |
956 | continue; | |
957 | ||
958 | r = ti->type->preresume(ti); | |
959 | if (r) | |
960 | return r; | |
961 | } | |
1da177e4 LT |
962 | |
963 | for (i = 0; i < t->num_targets; i++) { | |
964 | struct dm_target *ti = t->targets + i; | |
965 | ||
966 | if (ti->type->resume) | |
967 | ti->type->resume(ti); | |
968 | } | |
8757b776 MB |
969 | |
970 | return 0; | |
1da177e4 LT |
971 | } |
972 | ||
973 | int dm_table_any_congested(struct dm_table *t, int bdi_bits) | |
974 | { | |
975 | struct list_head *d, *devices; | |
976 | int r = 0; | |
977 | ||
978 | devices = dm_table_get_devices(t); | |
979 | for (d = devices->next; d != devices; d = d->next) { | |
980 | struct dm_dev *dd = list_entry(d, struct dm_dev, list); | |
165125e1 | 981 | struct request_queue *q = bdev_get_queue(dd->bdev); |
1da177e4 LT |
982 | r |= bdi_congested(&q->backing_dev_info, bdi_bits); |
983 | } | |
984 | ||
985 | return r; | |
986 | } | |
987 | ||
988 | void dm_table_unplug_all(struct dm_table *t) | |
989 | { | |
990 | struct list_head *d, *devices = dm_table_get_devices(t); | |
991 | ||
992 | for (d = devices->next; d != devices; d = d->next) { | |
993 | struct dm_dev *dd = list_entry(d, struct dm_dev, list); | |
165125e1 | 994 | struct request_queue *q = bdev_get_queue(dd->bdev); |
1da177e4 LT |
995 | |
996 | if (q->unplug_fn) | |
997 | q->unplug_fn(q); | |
998 | } | |
999 | } | |
1000 | ||
1134e5ae MA |
1001 | struct mapped_device *dm_table_get_md(struct dm_table *t) |
1002 | { | |
1003 | dm_get(t->md); | |
1004 | ||
1005 | return t->md; | |
1006 | } | |
1007 | ||
1da177e4 LT |
1008 | EXPORT_SYMBOL(dm_vcalloc); |
1009 | EXPORT_SYMBOL(dm_get_device); | |
1010 | EXPORT_SYMBOL(dm_put_device); | |
1011 | EXPORT_SYMBOL(dm_table_event); | |
d5e404c1 | 1012 | EXPORT_SYMBOL(dm_table_get_size); |
1da177e4 | 1013 | EXPORT_SYMBOL(dm_table_get_mode); |
1134e5ae | 1014 | EXPORT_SYMBOL(dm_table_get_md); |
1da177e4 LT |
1015 | EXPORT_SYMBOL(dm_table_put); |
1016 | EXPORT_SYMBOL(dm_table_get); | |
1017 | EXPORT_SYMBOL(dm_table_unplug_all); |