md/raid0: tidy up printk messages.
[safe/jmp/linux-2.6] / drivers / md / dm-table.c
index 267817e..9924ea2 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/blkdev.h>
 #include <linux/namei.h>
 #include <linux/ctype.h>
+#include <linux/string.h>
 #include <linux/slab.h>
 #include <linux/interrupt.h>
 #include <linux/mutex.h>
@@ -41,6 +42,7 @@
 struct dm_table {
        struct mapped_device *md;
        atomic_t holders;
+       unsigned type;
 
        /* btree table */
        unsigned int depth;
@@ -62,15 +64,11 @@ struct dm_table {
        /* a list of devices used by this table */
        struct list_head devices;
 
-       /*
-        * These are optimistic limits taken from all the
-        * targets, some targets will need smaller limits.
-        */
-       struct queue_limits limits;
-
        /* events get handed up using this callback */
        void (*event_fn)(void *);
        void *event_context;
+
+       struct dm_md_mempools *mempools;
 };
 
 /*
@@ -240,6 +238,9 @@ void dm_table_destroy(struct dm_table *t)
 {
        unsigned int i;
 
+       if (!t)
+               return;
+
        while (atomic_read(&t->holders))
                msleep(1);
        smp_mb();
@@ -264,6 +265,8 @@ void dm_table_destroy(struct dm_table *t)
        if (t->devices.next != &t->devices)
                free_devices(&t->devices);
 
+       dm_free_md_mempools(t->mempools);
+
        kfree(t);
 }
 
@@ -344,47 +347,54 @@ static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
 }
 
 /*
- * If possible, this checks an area of a destination device is valid.
+ * If possible, this checks an area of a destination device is invalid.
  */
-static int device_area_is_valid(struct dm_target *ti, struct block_device *bdev,
-                            sector_t start, sector_t len)
+static int device_area_is_invalid(struct dm_target *ti, struct dm_dev *dev,
+                                 sector_t start, sector_t len, void *data)
 {
-       sector_t dev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
+       struct queue_limits *limits = data;
+       struct block_device *bdev = dev->bdev;
+       sector_t dev_size =
+               i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
        unsigned short logical_block_size_sectors =
-               ti->limits.logical_block_size >> SECTOR_SHIFT;
+               limits->logical_block_size >> SECTOR_SHIFT;
        char b[BDEVNAME_SIZE];
 
        if (!dev_size)
-               return 1;
+               return 0;
 
        if ((start >= dev_size) || (start + len > dev_size)) {
-               DMWARN("%s: %s too small for target",
-                      dm_device_name(ti->table->md), bdevname(bdev, b));
-               return 0;
+               DMWARN("%s: %s too small for target: "
+                      "start=%llu, len=%llu, dev_size=%llu",
+                      dm_device_name(ti->table->md), bdevname(bdev, b),
+                      (unsigned long long)start,
+                      (unsigned long long)len,
+                      (unsigned long long)dev_size);
+               return 1;
        }
 
        if (logical_block_size_sectors <= 1)
-               return 1;
+               return 0;
 
        if (start & (logical_block_size_sectors - 1)) {
                DMWARN("%s: start=%llu not aligned to h/w "
-                      "logical block size %hu of %s",
+                      "logical block size %u of %s",
                       dm_device_name(ti->table->md),
                       (unsigned long long)start,
-                      ti->limits.logical_block_size, bdevname(bdev, b));
-               return 0;
+                      limits->logical_block_size, bdevname(bdev, b));
+               return 1;
        }
 
        if (len & (logical_block_size_sectors - 1)) {
                DMWARN("%s: len=%llu not aligned to h/w "
-                      "logical block size %hu of %s",
+                      "logical block size %u of %s",
                       dm_device_name(ti->table->md),
                       (unsigned long long)len,
-                      ti->limits.logical_block_size, bdevname(bdev, b));
-               return 0;
+                      limits->logical_block_size, bdevname(bdev, b));
+               return 1;
        }
 
-       return 1;
+       return 0;
 }
 
 /*
@@ -419,8 +429,7 @@ static int upgrade_mode(struct dm_dev_internal *dd, fmode_t new_mode,
  * it's already present.
  */
 static int __table_get_device(struct dm_table *t, struct dm_target *ti,
-                             const char *path, sector_t start, sector_t len,
-                             fmode_t mode, struct dm_dev **result)
+                     const char *path, fmode_t mode, struct dm_dev **result)
 {
        int r;
        dev_t uninitialized_var(dev);
@@ -479,20 +488,29 @@ static int __table_get_device(struct dm_table *t, struct dm_target *ti,
  */
 #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
 
-void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
+int dm_set_device_limits(struct dm_target *ti, struct dm_dev *dev,
+                        sector_t start, sector_t len, void *data)
 {
+       struct queue_limits *limits = data;
+       struct block_device *bdev = dev->bdev;
        struct request_queue *q = bdev_get_queue(bdev);
        char b[BDEVNAME_SIZE];
 
        if (unlikely(!q)) {
                DMWARN("%s: Cannot set limits for nonexistent device %s",
                       dm_device_name(ti->table->md), bdevname(bdev, b));
-               return;
+               return 0;
        }
 
-       if (blk_stack_limits(&ti->limits, &q->limits, 0) < 0)
-               DMWARN("%s: target device %s is misaligned",
-                      dm_device_name(ti->table->md), bdevname(bdev, b));
+       if (bdev_stack_limits(limits, bdev, start) < 0)
+               DMWARN("%s: adding target device %s caused an alignment inconsistency: "
+                      "physical_block_size=%u, logical_block_size=%u, "
+                      "alignment_offset=%u, start=%llu",
+                      dm_device_name(ti->table->md), bdevname(bdev, b),
+                      q->limits.physical_block_size,
+                      q->limits.logical_block_size,
+                      q->limits.alignment_offset,
+                      (unsigned long long) start << SECTOR_SHIFT);
 
        /*
         * Check if merge fn is supported.
@@ -501,32 +519,20 @@ void dm_set_device_limits(struct dm_target *ti, struct block_device *bdev)
         */
 
        if (q->merge_bvec_fn && !ti->type->merge)
-               ti->limits.max_sectors =
-                       min_not_zero(ti->limits.max_sectors,
+               limits->max_sectors =
+                       min_not_zero(limits->max_sectors,
                                     (unsigned int) (PAGE_SIZE >> 9));
+       return 0;
 }
 EXPORT_SYMBOL_GPL(dm_set_device_limits);
 
-int dm_get_device(struct dm_target *ti, const char *path, sector_t start,
-                 sector_t len, fmode_t mode, struct dm_dev **result)
+int dm_get_device(struct dm_target *ti, const char *path, fmode_t mode,
+                 struct dm_dev **result)
 {
-       int r = __table_get_device(ti->table, ti, path,
-                                  start, len, mode, result);
-
-       if (r)
-               return r;
-
-       dm_set_device_limits(ti, (*result)->bdev);
-
-       if (!device_area_is_valid(ti, (*result)->bdev, start, len)) {
-               dm_put_device(ti, *result);
-               *result = NULL;
-               return -EINVAL;
-       }
-
-       return r;
+       return __table_get_device(ti->table, ti, path, mode, result);
 }
 
+
 /*
  * Decrement a devices use count and remove it if necessary.
  */
@@ -595,11 +601,8 @@ int dm_split_args(int *argc, char ***argvp, char *input)
                return -ENOMEM;
 
        while (1) {
-               start = end;
-
                /* Skip whitespace */
-               while (*start && isspace(*start))
-                       start++;
+               start = skip_spaces(end);
 
                if (!*start)
                        break;  /* success, we hit the end */
@@ -641,34 +644,6 @@ int dm_split_args(int *argc, char ***argvp, char *input)
        return 0;
 }
 
-static void init_valid_queue_limits(struct queue_limits *limits)
-{
-       if (!limits->max_sectors)
-               limits->max_sectors = SAFE_MAX_SECTORS;
-       if (!limits->max_hw_sectors)
-               limits->max_hw_sectors = SAFE_MAX_SECTORS;
-       if (!limits->max_phys_segments)
-               limits->max_phys_segments = MAX_PHYS_SEGMENTS;
-       if (!limits->max_hw_segments)
-               limits->max_hw_segments = MAX_HW_SEGMENTS;
-       if (!limits->logical_block_size)
-               limits->logical_block_size = 1 << SECTOR_SHIFT;
-       if (!limits->physical_block_size)
-               limits->physical_block_size = 1 << SECTOR_SHIFT;
-       if (!limits->io_min)
-               limits->io_min = 1 << SECTOR_SHIFT;
-       if (!limits->max_segment_size)
-               limits->max_segment_size = MAX_SEGMENT_SIZE;
-       if (!limits->seg_boundary_mask)
-               limits->seg_boundary_mask = BLK_SEG_BOUNDARY_MASK;
-       if (!limits->bounce_pfn)
-               limits->bounce_pfn = -1;
-       /*
-        * The other fields (alignment_offset, io_opt, misaligned)
-        * hold 0 from the kzalloc().
-        */
-}
-
 /*
  * Impose necessary and sufficient conditions on a devices's table such
  * that any incoming bio which respects its logical_block_size can be
@@ -676,14 +651,15 @@ static void init_valid_queue_limits(struct queue_limits *limits)
  * two or more targets, the size of each piece it gets split into must
  * be compatible with the logical_block_size of the target processing it.
  */
-static int validate_hardware_logical_block_alignment(struct dm_table *table)
+static int validate_hardware_logical_block_alignment(struct dm_table *table,
+                                                struct queue_limits *limits)
 {
        /*
         * This function uses arithmetic modulo the logical_block_size
         * (in units of 512-byte sectors).
         */
        unsigned short device_logical_block_size_sects =
-               table->limits.logical_block_size >> SECTOR_SHIFT;
+               limits->logical_block_size >> SECTOR_SHIFT;
 
        /*
         * Offset of the start of the next table entry, mod logical_block_size.
@@ -697,6 +673,7 @@ static int validate_hardware_logical_block_alignment(struct dm_table *table)
        unsigned short remaining = 0;
 
        struct dm_target *uninitialized_var(ti);
+       struct queue_limits ti_limits;
        unsigned i = 0;
 
        /*
@@ -705,12 +682,19 @@ static int validate_hardware_logical_block_alignment(struct dm_table *table)
        while (i < dm_table_get_num_targets(table)) {
                ti = dm_table_get_target(table, i++);
 
+               blk_set_default_limits(&ti_limits);
+
+               /* combine all target devices' limits */
+               if (ti->type->iterate_devices)
+                       ti->type->iterate_devices(ti, dm_set_device_limits,
+                                                 &ti_limits);
+
                /*
                 * If the remaining sectors fall entirely within this
                 * table entry are they compatible with its logical_block_size?
                 */
                if (remaining < ti->len &&
-                   remaining & ((ti->limits.logical_block_size >>
+                   remaining & ((ti_limits.logical_block_size >>
                                  SECTOR_SHIFT) - 1))
                        break;  /* Error */
 
@@ -723,11 +707,11 @@ static int validate_hardware_logical_block_alignment(struct dm_table *table)
 
        if (remaining) {
                DMWARN("%s: table line %u (start sect %llu len %llu) "
-                      "not aligned to hardware logical block size %hu",
+                      "not aligned to h/w logical block size %u",
                       dm_device_name(table->md), i,
                       (unsigned long long) ti->begin,
                       (unsigned long long) ti->len,
-                      table->limits.logical_block_size);
+                      limits->logical_block_size);
                return -EINVAL;
        }
 
@@ -786,12 +770,6 @@ int dm_table_add_target(struct dm_table *t, const char *type,
 
        t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
 
-       if (blk_stack_limits(&t->limits, &tgt->limits, 0) < 0)
-               DMWARN("%s: target device (start sect %llu len %llu) "
-                      "is misaligned",
-                      dm_device_name(t->md),
-                      (unsigned long long) tgt->begin,
-                      (unsigned long long) tgt->len);
        return 0;
 
  bad:
@@ -800,6 +778,99 @@ int dm_table_add_target(struct dm_table *t, const char *type,
        return r;
 }
 
+int dm_table_set_type(struct dm_table *t)
+{
+       unsigned i;
+       unsigned bio_based = 0, request_based = 0;
+       struct dm_target *tgt;
+       struct dm_dev_internal *dd;
+       struct list_head *devices;
+
+       for (i = 0; i < t->num_targets; i++) {
+               tgt = t->targets + i;
+               if (dm_target_request_based(tgt))
+                       request_based = 1;
+               else
+                       bio_based = 1;
+
+               if (bio_based && request_based) {
+                       DMWARN("Inconsistent table: different target types"
+                              " can't be mixed up");
+                       return -EINVAL;
+               }
+       }
+
+       if (bio_based) {
+               /* We must use this table as bio-based */
+               t->type = DM_TYPE_BIO_BASED;
+               return 0;
+       }
+
+       BUG_ON(!request_based); /* No targets in this table */
+
+       /* Non-request-stackable devices can't be used for request-based dm */
+       devices = dm_table_get_devices(t);
+       list_for_each_entry(dd, devices, list) {
+               if (!blk_queue_stackable(bdev_get_queue(dd->dm_dev.bdev))) {
+                       DMWARN("table load rejected: including"
+                              " non-request-stackable devices");
+                       return -EINVAL;
+               }
+       }
+
+       /*
+        * Request-based dm supports only tables that have a single target now.
+        * To support multiple targets, request splitting support is needed,
+        * and that needs lots of changes in the block-layer.
+        * (e.g. request completion process for partial completion.)
+        */
+       if (t->num_targets > 1) {
+               DMWARN("Request-based dm doesn't support multiple targets yet");
+               return -EINVAL;
+       }
+
+       t->type = DM_TYPE_REQUEST_BASED;
+
+       return 0;
+}
+
+unsigned dm_table_get_type(struct dm_table *t)
+{
+       return t->type;
+}
+
+bool dm_table_request_based(struct dm_table *t)
+{
+       return dm_table_get_type(t) == DM_TYPE_REQUEST_BASED;
+}
+
+int dm_table_alloc_md_mempools(struct dm_table *t)
+{
+       unsigned type = dm_table_get_type(t);
+
+       if (unlikely(type == DM_TYPE_NONE)) {
+               DMWARN("no table type is set, can't allocate mempools");
+               return -EINVAL;
+       }
+
+       t->mempools = dm_alloc_md_mempools(type);
+       if (!t->mempools)
+               return -ENOMEM;
+
+       return 0;
+}
+
+void dm_table_free_md_mempools(struct dm_table *t)
+{
+       dm_free_md_mempools(t->mempools);
+       t->mempools = NULL;
+}
+
+struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t)
+{
+       return t->mempools;
+}
+
 static int setup_indexes(struct dm_table *t)
 {
        int i;
@@ -834,12 +905,6 @@ int dm_table_complete(struct dm_table *t)
        int r = 0;
        unsigned int leaf_nodes;
 
-       init_valid_queue_limits(&t->limits);
-
-       r = validate_hardware_logical_block_alignment(t);
-       if (r)
-               return r;
-
        /* how many indexes will the btree have ? */
        leaf_nodes = dm_div_up(t->num_targets, KEYS_PER_NODE);
        t->depth = 1 + int_log(leaf_nodes, CHILDREN_PER_NODE);
@@ -915,6 +980,61 @@ struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
 }
 
 /*
+ * Establish the new table's queue_limits and validate them.
+ */
+int dm_calculate_queue_limits(struct dm_table *table,
+                             struct queue_limits *limits)
+{
+       struct dm_target *uninitialized_var(ti);
+       struct queue_limits ti_limits;
+       unsigned i = 0;
+
+       blk_set_default_limits(limits);
+
+       while (i < dm_table_get_num_targets(table)) {
+               blk_set_default_limits(&ti_limits);
+
+               ti = dm_table_get_target(table, i++);
+
+               if (!ti->type->iterate_devices)
+                       goto combine_limits;
+
+               /*
+                * Combine queue limits of all the devices this target uses.
+                */
+               ti->type->iterate_devices(ti, dm_set_device_limits,
+                                         &ti_limits);
+
+               /* Set I/O hints portion of queue limits */
+               if (ti->type->io_hints)
+                       ti->type->io_hints(ti, &ti_limits);
+
+               /*
+                * Check each device area is consistent with the target's
+                * overall queue limits.
+                */
+               if (ti->type->iterate_devices(ti, device_area_is_invalid,
+                                             &ti_limits))
+                       return -EINVAL;
+
+combine_limits:
+               /*
+                * Merge this target's queue limits into the overall limits
+                * for the table.
+                */
+               if (blk_stack_limits(limits, &ti_limits, 0) < 0)
+                       DMWARN("%s: adding target device "
+                              "(start sect %llu len %llu) "
+                              "caused an alignment inconsistency",
+                              dm_device_name(table->md),
+                              (unsigned long long) ti->begin,
+                              (unsigned long long) ti->len);
+       }
+
+       return validate_hardware_logical_block_alignment(table, limits);
+}
+
+/*
  * Set the integrity profile for this device if all devices used have
  * matching profiles.
  */
@@ -953,19 +1073,33 @@ no_integrity:
        return;
 }
 
-void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q)
+void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
+                              struct queue_limits *limits)
 {
        /*
         * Copy table's limits to the DM device's request_queue
         */
-       q->limits = t->limits;
+       q->limits = *limits;
 
-       if (t->limits.no_cluster)
+       if (limits->no_cluster)
                queue_flag_clear_unlocked(QUEUE_FLAG_CLUSTER, q);
        else
                queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
 
        dm_table_set_integrity(t);
+
+       /*
+        * QUEUE_FLAG_STACKABLE must be set after all queue settings are
+        * visible to other CPUs because, once the flag is set, incoming bios
+        * are processed by request-based dm, which refers to the queue
+        * settings.
+        * Until the flag set, bios are passed to bio-based dm and queued to
+        * md->deferred where queue settings are not needed yet.
+        * Those bios are passed to request-based dm at the resume time.
+        */
+       smp_mb();
+       if (dm_table_request_based(t))
+               queue_flag_set_unlocked(QUEUE_FLAG_STACKABLE, q);
 }
 
 unsigned int dm_table_get_num_targets(struct dm_table *t)
@@ -1061,6 +1195,20 @@ int dm_table_any_congested(struct dm_table *t, int bdi_bits)
        return r;
 }
 
+int dm_table_any_busy_target(struct dm_table *t)
+{
+       unsigned i;
+       struct dm_target *ti;
+
+       for (i = 0; i < t->num_targets; i++) {
+               ti = t->targets + i;
+               if (ti->type->busy && ti->type->busy(ti))
+                       return 1;
+       }
+
+       return 0;
+}
+
 void dm_table_unplug_all(struct dm_table *t)
 {
        struct dm_dev_internal *dd;
@@ -1081,8 +1229,6 @@ void dm_table_unplug_all(struct dm_table *t)
 
 struct mapped_device *dm_table_get_md(struct dm_table *t)
 {
-       dm_get(t->md);
-
        return t->md;
 }