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