linux/drivers/md/dm-raid.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2010-2011 Neil Brown
   3 * Copyright (C) 2010-2011 Red Hat, Inc. All rights reserved.
   4 *
   5 * This file is released under the GPL.
   6 */
   7
   8#include <linux/slab.h>
   9#include <linux/module.h>
  10
  11#include "md.h"
  12#include "raid1.h"
  13#include "raid5.h"
  14#include "raid10.h"
  15#include "bitmap.h"
  16
  17#include <linux/device-mapper.h>
  18
  19#define DM_MSG_PREFIX "raid"
  20
  21/*
  22 * The following flags are used by dm-raid.c to set up the array state.
  23 * They must be cleared before md_run is called.
  24 */
  25#define FirstUse 10             /* rdev flag */
  26
  27struct raid_dev {
  28        /*
  29         * Two DM devices, one to hold metadata and one to hold the
  30         * actual data/parity.  The reason for this is to not confuse
  31         * ti->len and give more flexibility in altering size and
  32         * characteristics.
  33         *
  34         * While it is possible for this device to be associated
  35         * with a different physical device than the data_dev, it
  36         * is intended for it to be the same.
  37         *    |--------- Physical Device ---------|
  38         *    |- meta_dev -|------ data_dev ------|
  39         */
  40        struct dm_dev *meta_dev;
  41        struct dm_dev *data_dev;
  42        struct md_rdev rdev;
  43};
  44
  45/*
  46 * Flags for rs->print_flags field.
  47 */
  48#define DMPF_SYNC              0x1
  49#define DMPF_NOSYNC            0x2
  50#define DMPF_REBUILD           0x4
  51#define DMPF_DAEMON_SLEEP      0x8
  52#define DMPF_MIN_RECOVERY_RATE 0x10
  53#define DMPF_MAX_RECOVERY_RATE 0x20
  54#define DMPF_MAX_WRITE_BEHIND  0x40
  55#define DMPF_STRIPE_CACHE      0x80
  56#define DMPF_REGION_SIZE       0x100
  57#define DMPF_RAID10_COPIES     0x200
  58#define DMPF_RAID10_FORMAT     0x400
  59
  60struct raid_set {
  61        struct dm_target *ti;
  62
  63        uint32_t bitmap_loaded;
  64        uint32_t print_flags;
  65
  66        struct mddev md;
  67        struct raid_type *raid_type;
  68        struct dm_target_callbacks callbacks;
  69
  70        struct raid_dev dev[0];
  71};
  72
  73/* Supported raid types and properties. */
  74static struct raid_type {
  75        const char *name;               /* RAID algorithm. */
  76        const char *descr;              /* Descriptor text for logging. */
  77        const unsigned parity_devs;     /* # of parity devices. */
  78        const unsigned minimal_devs;    /* minimal # of devices in set. */
  79        const unsigned level;           /* RAID level. */
  80        const unsigned algorithm;       /* RAID algorithm. */
  81} raid_types[] = {
  82        {"raid1",    "RAID1 (mirroring)",               0, 2, 1, 0 /* NONE */},
  83        {"raid10",   "RAID10 (striped mirrors)",        0, 2, 10, UINT_MAX /* Varies */},
  84        {"raid4",    "RAID4 (dedicated parity disk)",   1, 2, 5, ALGORITHM_PARITY_0},
  85        {"raid5_la", "RAID5 (left asymmetric)",         1, 2, 5, ALGORITHM_LEFT_ASYMMETRIC},
  86        {"raid5_ra", "RAID5 (right asymmetric)",        1, 2, 5, ALGORITHM_RIGHT_ASYMMETRIC},
  87        {"raid5_ls", "RAID5 (left symmetric)",          1, 2, 5, ALGORITHM_LEFT_SYMMETRIC},
  88        {"raid5_rs", "RAID5 (right symmetric)",         1, 2, 5, ALGORITHM_RIGHT_SYMMETRIC},
  89        {"raid6_zr", "RAID6 (zero restart)",            2, 4, 6, ALGORITHM_ROTATING_ZERO_RESTART},
  90        {"raid6_nr", "RAID6 (N restart)",               2, 4, 6, ALGORITHM_ROTATING_N_RESTART},
  91        {"raid6_nc", "RAID6 (N continue)",              2, 4, 6, ALGORITHM_ROTATING_N_CONTINUE}
  92};
  93
  94static unsigned raid10_md_layout_to_copies(int layout)
  95{
  96        return layout & 0xFF;
  97}
  98
  99static int raid10_format_to_md_layout(char *format, unsigned copies)
 100{
 101        /* 1 "far" copy, and 'copies' "near" copies */
 102        return (1 << 8) | (copies & 0xFF);
 103}
 104
 105static struct raid_type *get_raid_type(char *name)
 106{
 107        int i;
 108
 109        for (i = 0; i < ARRAY_SIZE(raid_types); i++)
 110                if (!strcmp(raid_types[i].name, name))
 111                        return &raid_types[i];
 112
 113        return NULL;
 114}
 115
 116static struct raid_set *context_alloc(struct dm_target *ti, struct raid_type *raid_type, unsigned raid_devs)
 117{
 118        unsigned i;
 119        struct raid_set *rs;
 120
 121        if (raid_devs <= raid_type->parity_devs) {
 122                ti->error = "Insufficient number of devices";
 123                return ERR_PTR(-EINVAL);
 124        }
 125
 126        rs = kzalloc(sizeof(*rs) + raid_devs * sizeof(rs->dev[0]), GFP_KERNEL);
 127        if (!rs) {
 128                ti->error = "Cannot allocate raid context";
 129                return ERR_PTR(-ENOMEM);
 130        }
 131
 132        mddev_init(&rs->md);
 133
 134        rs->ti = ti;
 135        rs->raid_type = raid_type;
 136        rs->md.raid_disks = raid_devs;
 137        rs->md.level = raid_type->level;
 138        rs->md.new_level = rs->md.level;
 139        rs->md.layout = raid_type->algorithm;
 140        rs->md.new_layout = rs->md.layout;
 141        rs->md.delta_disks = 0;
 142        rs->md.recovery_cp = 0;
 143
 144        for (i = 0; i < raid_devs; i++)
 145                md_rdev_init(&rs->dev[i].rdev);
 146
 147        /*
 148         * Remaining items to be initialized by further RAID params:
 149         *  rs->md.persistent
 150         *  rs->md.external
 151         *  rs->md.chunk_sectors
 152         *  rs->md.new_chunk_sectors
 153         *  rs->md.dev_sectors
 154         */
 155
 156        return rs;
 157}
 158
 159static void context_free(struct raid_set *rs)
 160{
 161        int i;
 162
 163        for (i = 0; i < rs->md.raid_disks; i++) {
 164                if (rs->dev[i].meta_dev)
 165                        dm_put_device(rs->ti, rs->dev[i].meta_dev);
 166                md_rdev_clear(&rs->dev[i].rdev);
 167                if (rs->dev[i].data_dev)
 168                        dm_put_device(rs->ti, rs->dev[i].data_dev);
 169        }
 170
 171        kfree(rs);
 172}
 173
 174/*
 175 * For every device we have two words
 176 *  <meta_dev>: meta device name or '-' if missing
 177 *  <data_dev>: data device name or '-' if missing
 178 *
 179 * The following are permitted:
 180 *    - -
 181 *    - <data_dev>
 182 *    <meta_dev> <data_dev>
 183 *
 184 * The following is not allowed:
 185 *    <meta_dev> -
 186 *
 187 * This code parses those words.  If there is a failure,
 188 * the caller must use context_free to unwind the operations.
 189 */
 190static int dev_parms(struct raid_set *rs, char **argv)
 191{
 192        int i;
 193        int rebuild = 0;
 194        int metadata_available = 0;
 195        int ret = 0;
 196
 197        for (i = 0; i < rs->md.raid_disks; i++, argv += 2) {
 198                rs->dev[i].rdev.raid_disk = i;
 199
 200                rs->dev[i].meta_dev = NULL;
 201                rs->dev[i].data_dev = NULL;
 202
 203                /*
 204                 * There are no offsets, since there is a separate device
 205                 * for data and metadata.
 206                 */
 207                rs->dev[i].rdev.data_offset = 0;
 208                rs->dev[i].rdev.mddev = &rs->md;
 209
 210                if (strcmp(argv[0], "-")) {
 211                        ret = dm_get_device(rs->ti, argv[0],
 212                                            dm_table_get_mode(rs->ti->table),
 213                                            &rs->dev[i].meta_dev);
 214                        rs->ti->error = "RAID metadata device lookup failure";
 215                        if (ret)
 216                                return ret;
 217
 218                        rs->dev[i].rdev.sb_page = alloc_page(GFP_KERNEL);
 219                        if (!rs->dev[i].rdev.sb_page)
 220                                return -ENOMEM;
 221                }
 222
 223                if (!strcmp(argv[1], "-")) {
 224                        if (!test_bit(In_sync, &rs->dev[i].rdev.flags) &&
 225                            (!rs->dev[i].rdev.recovery_offset)) {
 226                                rs->ti->error = "Drive designated for rebuild not specified";
 227                                return -EINVAL;
 228                        }
 229
 230                        rs->ti->error = "No data device supplied with metadata device";
 231                        if (rs->dev[i].meta_dev)
 232                                return -EINVAL;
 233
 234                        continue;
 235                }
 236
 237                ret = dm_get_device(rs->ti, argv[1],
 238                                    dm_table_get_mode(rs->ti->table),
 239                                    &rs->dev[i].data_dev);
 240                if (ret) {
 241                        rs->ti->error = "RAID device lookup failure";
 242                        return ret;
 243                }
 244
 245                if (rs->dev[i].meta_dev) {
 246                        metadata_available = 1;
 247                        rs->dev[i].rdev.meta_bdev = rs->dev[i].meta_dev->bdev;
 248                }
 249                rs->dev[i].rdev.bdev = rs->dev[i].data_dev->bdev;
 250                list_add(&rs->dev[i].rdev.same_set, &rs->md.disks);
 251                if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
 252                        rebuild++;
 253        }
 254
 255        if (metadata_available) {
 256                rs->md.external = 0;
 257                rs->md.persistent = 1;
 258                rs->md.major_version = 2;
 259        } else if (rebuild && !rs->md.recovery_cp) {
 260                /*
 261                 * Without metadata, we will not be able to tell if the array
 262                 * is in-sync or not - we must assume it is not.  Therefore,
 263                 * it is impossible to rebuild a drive.
 264                 *
 265                 * Even if there is metadata, the on-disk information may
 266                 * indicate that the array is not in-sync and it will then
 267                 * fail at that time.
 268                 *
 269                 * User could specify 'nosync' option if desperate.
 270                 */
 271                DMERR("Unable to rebuild drive while array is not in-sync");
 272                rs->ti->error = "RAID device lookup failure";
 273                return -EINVAL;
 274        }
 275
 276        return 0;
 277}
 278
 279/*
 280 * validate_region_size
 281 * @rs
 282 * @region_size:  region size in sectors.  If 0, pick a size (4MiB default).
 283 *
 284 * Set rs->md.bitmap_info.chunksize (which really refers to 'region size').
 285 * Ensure that (ti->len/region_size < 2^21) - required by MD bitmap.
 286 *
 287 * Returns: 0 on success, -EINVAL on failure.
 288 */
 289static int validate_region_size(struct raid_set *rs, unsigned long region_size)
 290{
 291        unsigned long min_region_size = rs->ti->len / (1 << 21);
 292
 293        if (!region_size) {
 294                /*
 295                 * Choose a reasonable default.  All figures in sectors.
 296                 */
 297                if (min_region_size > (1 << 13)) {
 298                        DMINFO("Choosing default region size of %lu sectors",
 299                               region_size);
 300                        region_size = min_region_size;
 301                } else {
 302                        DMINFO("Choosing default region size of 4MiB");
 303                        region_size = 1 << 13; /* sectors */
 304                }
 305        } else {
 306                /*
 307                 * Validate user-supplied value.
 308                 */
 309                if (region_size > rs->ti->len) {
 310                        rs->ti->error = "Supplied region size is too large";
 311                        return -EINVAL;
 312                }
 313
 314                if (region_size < min_region_size) {
 315                        DMERR("Supplied region_size (%lu sectors) below minimum (%lu)",
 316                              region_size, min_region_size);
 317                        rs->ti->error = "Supplied region size is too small";
 318                        return -EINVAL;
 319                }
 320
 321                if (!is_power_of_2(region_size)) {
 322                        rs->ti->error = "Region size is not a power of 2";
 323                        return -EINVAL;
 324                }
 325
 326                if (region_size < rs->md.chunk_sectors) {
 327                        rs->ti->error = "Region size is smaller than the chunk size";
 328                        return -EINVAL;
 329                }
 330        }
 331
 332        /*
 333         * Convert sectors to bytes.
 334         */
 335        rs->md.bitmap_info.chunksize = (region_size << 9);
 336
 337        return 0;
 338}
 339
 340/*
 341 * validate_raid_redundancy
 342 * @rs
 343 *
 344 * Determine if there are enough devices in the array that haven't
 345 * failed (or are being rebuilt) to form a usable array.
 346 *
 347 * Returns: 0 on success, -EINVAL on failure.
 348 */
 349static int validate_raid_redundancy(struct raid_set *rs)
 350{
 351        unsigned i, rebuild_cnt = 0;
 352        unsigned rebuilds_per_group, copies, d;
 353
 354        for (i = 0; i < rs->md.raid_disks; i++)
 355                if (!test_bit(In_sync, &rs->dev[i].rdev.flags) ||
 356                    !rs->dev[i].rdev.sb_page)
 357                        rebuild_cnt++;
 358
 359        switch (rs->raid_type->level) {
 360        case 1:
 361                if (rebuild_cnt >= rs->md.raid_disks)
 362                        goto too_many;
 363                break;
 364        case 4:
 365        case 5:
 366        case 6:
 367                if (rebuild_cnt > rs->raid_type->parity_devs)
 368                        goto too_many;
 369                break;
 370        case 10:
 371                copies = raid10_md_layout_to_copies(rs->md.layout);
 372                if (rebuild_cnt < copies)
 373                        break;
 374
 375                /*
 376                 * It is possible to have a higher rebuild count for RAID10,
 377                 * as long as the failed devices occur in different mirror
 378                 * groups (i.e. different stripes).
 379                 *
 380                 * Right now, we only allow for "near" copies.  When other
 381                 * formats are added, we will have to check those too.
 382                 *
 383                 * When checking "near" format, make sure no adjacent devices
 384                 * have failed beyond what can be handled.  In addition to the
 385                 * simple case where the number of devices is a multiple of the
 386                 * number of copies, we must also handle cases where the number
 387                 * of devices is not a multiple of the number of copies.
 388                 * E.g.    dev1 dev2 dev3 dev4 dev5
 389                 *          A    A    B    B    C
 390                 *          C    D    D    E    E
 391                 */
 392                for (i = 0; i < rs->md.raid_disks * copies; i++) {
 393                        if (!(i % copies))
 394                                rebuilds_per_group = 0;
 395                        d = i % rs->md.raid_disks;
 396                        if ((!rs->dev[d].rdev.sb_page ||
 397                             !test_bit(In_sync, &rs->dev[d].rdev.flags)) &&
 398                            (++rebuilds_per_group >= copies))
 399                                goto too_many;
 400                }
 401                break;
 402        default:
 403                if (rebuild_cnt)
 404                        return -EINVAL;
 405        }
 406
 407        return 0;
 408
 409too_many:
 410        return -EINVAL;
 411}
 412
 413/*
 414 * Possible arguments are...
 415 *      <chunk_size> [optional_args]
 416 *
 417 * Argument definitions
 418 *    <chunk_size>                      The number of sectors per disk that
 419 *                                      will form the "stripe"
 420 *    [[no]sync]                        Force or prevent recovery of the
 421 *                                      entire array
 422 *    [rebuild <idx>]                   Rebuild the drive indicated by the index
 423 *    [daemon_sleep <ms>]               Time between bitmap daemon work to
 424 *                                      clear bits
 425 *    [min_recovery_rate <kB/sec/disk>] Throttle RAID initialization
 426 *    [max_recovery_rate <kB/sec/disk>] Throttle RAID initialization
 427 *    [write_mostly <idx>]              Indicate a write mostly drive via index
 428 *    [max_write_behind <sectors>]      See '-write-behind=' (man mdadm)
 429 *    [stripe_cache <sectors>]          Stripe cache size for higher RAIDs
 430 *    [region_size <sectors>]           Defines granularity of bitmap
 431 *
 432 * RAID10-only options:
 433 *    [raid10_copies <# copies>]        Number of copies.  (Default: 2)
 434 *    [raid10_format <near>]            Layout algorithm.  (Default: near)
 435 */
 436static int parse_raid_params(struct raid_set *rs, char **argv,
 437                             unsigned num_raid_params)
 438{
 439        char *raid10_format = "near";
 440        unsigned raid10_copies = 2;
 441        unsigned i;
 442        unsigned long value, region_size = 0;
 443        sector_t sectors_per_dev = rs->ti->len;
 444        sector_t max_io_len;
 445        char *key;
 446
 447        /*
 448         * First, parse the in-order required arguments
 449         * "chunk_size" is the only argument of this type.
 450         */
 451        if ((strict_strtoul(argv[0], 10, &value) < 0)) {
 452                rs->ti->error = "Bad chunk size";
 453                return -EINVAL;
 454        } else if (rs->raid_type->level == 1) {
 455                if (value)
 456                        DMERR("Ignoring chunk size parameter for RAID 1");
 457                value = 0;
 458        } else if (!is_power_of_2(value)) {
 459                rs->ti->error = "Chunk size must be a power of 2";
 460                return -EINVAL;
 461        } else if (value < 8) {
 462                rs->ti->error = "Chunk size value is too small";
 463                return -EINVAL;
 464        }
 465
 466        rs->md.new_chunk_sectors = rs->md.chunk_sectors = value;
 467        argv++;
 468        num_raid_params--;
 469
 470        /*
 471         * We set each individual device as In_sync with a completed
 472         * 'recovery_offset'.  If there has been a device failure or
 473         * replacement then one of the following cases applies:
 474         *
 475         *   1) User specifies 'rebuild'.
 476         *      - Device is reset when param is read.
 477         *   2) A new device is supplied.
 478         *      - No matching superblock found, resets device.
 479         *   3) Device failure was transient and returns on reload.
 480         *      - Failure noticed, resets device for bitmap replay.
 481         *   4) Device hadn't completed recovery after previous failure.
 482         *      - Superblock is read and overrides recovery_offset.
 483         *
 484         * What is found in the superblocks of the devices is always
 485         * authoritative, unless 'rebuild' or '[no]sync' was specified.
 486         */
 487        for (i = 0; i < rs->md.raid_disks; i++) {
 488                set_bit(In_sync, &rs->dev[i].rdev.flags);
 489                rs->dev[i].rdev.recovery_offset = MaxSector;
 490        }
 491
 492        /*
 493         * Second, parse the unordered optional arguments
 494         */
 495        for (i = 0; i < num_raid_params; i++) {
 496                if (!strcasecmp(argv[i], "nosync")) {
 497                        rs->md.recovery_cp = MaxSector;
 498                        rs->print_flags |= DMPF_NOSYNC;
 499                        continue;
 500                }
 501                if (!strcasecmp(argv[i], "sync")) {
 502                        rs->md.recovery_cp = 0;
 503                        rs->print_flags |= DMPF_SYNC;
 504                        continue;
 505                }
 506
 507                /* The rest of the optional arguments come in key/value pairs */
 508                if ((i + 1) >= num_raid_params) {
 509                        rs->ti->error = "Wrong number of raid parameters given";
 510                        return -EINVAL;
 511                }
 512
 513                key = argv[i++];
 514
 515                /* Parameters that take a string value are checked here. */
 516                if (!strcasecmp(key, "raid10_format")) {
 517                        if (rs->raid_type->level != 10) {
 518                                rs->ti->error = "'raid10_format' is an invalid parameter for this RAID type";
 519                                return -EINVAL;
 520                        }
 521                        if (strcmp("near", argv[i])) {
 522                                rs->ti->error = "Invalid 'raid10_format' value given";
 523                                return -EINVAL;
 524                        }
 525                        raid10_format = argv[i];
 526                        rs->print_flags |= DMPF_RAID10_FORMAT;
 527                        continue;
 528                }
 529
 530                if (strict_strtoul(argv[i], 10, &value) < 0) {
 531                        rs->ti->error = "Bad numerical argument given in raid params";
 532                        return -EINVAL;
 533                }
 534
 535                /* Parameters that take a numeric value are checked here */
 536                if (!strcasecmp(key, "rebuild")) {
 537                        if (value >= rs->md.raid_disks) {
 538                                rs->ti->error = "Invalid rebuild index given";
 539                                return -EINVAL;
 540                        }
 541                        clear_bit(In_sync, &rs->dev[value].rdev.flags);
 542                        rs->dev[value].rdev.recovery_offset = 0;
 543                        rs->print_flags |= DMPF_REBUILD;
 544                } else if (!strcasecmp(key, "write_mostly")) {
 545                        if (rs->raid_type->level != 1) {
 546                                rs->ti->error = "write_mostly option is only valid for RAID1";
 547                                return -EINVAL;
 548                        }
 549                        if (value >= rs->md.raid_disks) {
 550                                rs->ti->error = "Invalid write_mostly drive index given";
 551                                return -EINVAL;
 552                        }
 553                        set_bit(WriteMostly, &rs->dev[value].rdev.flags);
 554                } else if (!strcasecmp(key, "max_write_behind")) {
 555                        if (rs->raid_type->level != 1) {
 556                                rs->ti->error = "max_write_behind option is only valid for RAID1";
 557                                return -EINVAL;
 558                        }
 559                        rs->print_flags |= DMPF_MAX_WRITE_BEHIND;
 560
 561                        /*
 562                         * In device-mapper, we specify things in sectors, but
 563                         * MD records this value in kB
 564                         */
 565                        value /= 2;
 566                        if (value > COUNTER_MAX) {
 567                                rs->ti->error = "Max write-behind limit out of range";
 568                                return -EINVAL;
 569                        }
 570                        rs->md.bitmap_info.max_write_behind = value;
 571                } else if (!strcasecmp(key, "daemon_sleep")) {
 572                        rs->print_flags |= DMPF_DAEMON_SLEEP;
 573                        if (!value || (value > MAX_SCHEDULE_TIMEOUT)) {
 574                                rs->ti->error = "daemon sleep period out of range";
 575                                return -EINVAL;
 576                        }
 577                        rs->md.bitmap_info.daemon_sleep = value;
 578                } else if (!strcasecmp(key, "stripe_cache")) {
 579                        rs->print_flags |= DMPF_STRIPE_CACHE;
 580
 581                        /*
 582                         * In device-mapper, we specify things in sectors, but
 583                         * MD records this value in kB
 584                         */
 585                        value /= 2;
 586
 587                        if ((rs->raid_type->level != 5) &&
 588                            (rs->raid_type->level != 6)) {
 589                                rs->ti->error = "Inappropriate argument: stripe_cache";
 590                                return -EINVAL;
 591                        }
 592                        if (raid5_set_cache_size(&rs->md, (int)value)) {
 593                                rs->ti->error = "Bad stripe_cache size";
 594                                return -EINVAL;
 595                        }
 596                } else if (!strcasecmp(key, "min_recovery_rate")) {
 597                        rs->print_flags |= DMPF_MIN_RECOVERY_RATE;
 598                        if (value > INT_MAX) {
 599                                rs->ti->error = "min_recovery_rate out of range";
 600                                return -EINVAL;
 601                        }
 602                        rs->md.sync_speed_min = (int)value;
 603                } else if (!strcasecmp(key, "max_recovery_rate")) {
 604                        rs->print_flags |= DMPF_MAX_RECOVERY_RATE;
 605                        if (value > INT_MAX) {
 606                                rs->ti->error = "max_recovery_rate out of range";
 607                                return -EINVAL;
 608                        }
 609                        rs->md.sync_speed_max = (int)value;
 610                } else if (!strcasecmp(key, "region_size")) {
 611                        rs->print_flags |= DMPF_REGION_SIZE;
 612                        region_size = value;
 613                } else if (!strcasecmp(key, "raid10_copies") &&
 614                           (rs->raid_type->level == 10)) {
 615                        if ((value < 2) || (value > 0xFF)) {
 616                                rs->ti->error = "Bad value for 'raid10_copies'";
 617                                return -EINVAL;
 618                        }
 619                        rs->print_flags |= DMPF_RAID10_COPIES;
 620                        raid10_copies = value;
 621                } else {
 622                        DMERR("Unable to parse RAID parameter: %s", key);
 623                        rs->ti->error = "Unable to parse RAID parameters";
 624                        return -EINVAL;
 625                }
 626        }
 627
 628        if (validate_region_size(rs, region_size))
 629                return -EINVAL;
 630
 631        if (rs->md.chunk_sectors)
 632                max_io_len = rs->md.chunk_sectors;
 633        else
 634                max_io_len = region_size;
 635
 636        if (dm_set_target_max_io_len(rs->ti, max_io_len))
 637                return -EINVAL;
 638
 639        if (rs->raid_type->level == 10) {
 640                if (raid10_copies > rs->md.raid_disks) {
 641                        rs->ti->error = "Not enough devices to satisfy specification";
 642                        return -EINVAL;
 643                }
 644
 645                /* (Len * #mirrors) / #devices */
 646                sectors_per_dev = rs->ti->len * raid10_copies;
 647                sector_div(sectors_per_dev, rs->md.raid_disks);
 648
 649                rs->md.layout = raid10_format_to_md_layout(raid10_format,
 650                                                           raid10_copies);
 651                rs->md.new_layout = rs->md.layout;
 652        } else if ((rs->raid_type->level > 1) &&
 653                   sector_div(sectors_per_dev,
 654                              (rs->md.raid_disks - rs->raid_type->parity_devs))) {
 655                rs->ti->error = "Target length not divisible by number of data devices";
 656                return -EINVAL;
 657        }
 658        rs->md.dev_sectors = sectors_per_dev;
 659
 660        /* Assume there are no metadata devices until the drives are parsed */
 661        rs->md.persistent = 0;
 662        rs->md.external = 1;
 663
 664        return 0;
 665}
 666
 667static void do_table_event(struct work_struct *ws)
 668{
 669        struct raid_set *rs = container_of(ws, struct raid_set, md.event_work);
 670
 671        dm_table_event(rs->ti->table);
 672}
 673
 674static int raid_is_congested(struct dm_target_callbacks *cb, int bits)
 675{
 676        struct raid_set *rs = container_of(cb, struct raid_set, callbacks);
 677
 678        if (rs->raid_type->level == 1)
 679                return md_raid1_congested(&rs->md, bits);
 680
 681        if (rs->raid_type->level == 10)
 682                return md_raid10_congested(&rs->md, bits);
 683
 684        return md_raid5_congested(&rs->md, bits);
 685}
 686
 687/*
 688 * This structure is never routinely used by userspace, unlike md superblocks.
 689 * Devices with this superblock should only ever be accessed via device-mapper.
 690 */
 691#define DM_RAID_MAGIC 0x64526D44
 692struct dm_raid_superblock {
 693        __le32 magic;           /* "DmRd" */
 694        __le32 features;        /* Used to indicate possible future changes */
 695
 696        __le32 num_devices;     /* Number of devices in this array. (Max 64) */
 697        __le32 array_position;  /* The position of this drive in the array */
 698
 699        __le64 events;          /* Incremented by md when superblock updated */
 700        __le64 failed_devices;  /* Bit field of devices to indicate failures */
 701
 702        /*
 703         * This offset tracks the progress of the repair or replacement of
 704         * an individual drive.
 705         */
 706        __le64 disk_recovery_offset;
 707
 708        /*
 709         * This offset tracks the progress of the initial array
 710         * synchronisation/parity calculation.
 711         */
 712        __le64 array_resync_offset;
 713
 714        /*
 715         * RAID characteristics
 716         */
 717        __le32 level;
 718        __le32 layout;
 719        __le32 stripe_sectors;
 720
 721        __u8 pad[452];          /* Round struct to 512 bytes. */
 722                                /* Always set to 0 when writing. */
 723} __packed;
 724
 725static int read_disk_sb(struct md_rdev *rdev, int size)
 726{
 727        BUG_ON(!rdev->sb_page);
 728
 729        if (rdev->sb_loaded)
 730                return 0;
 731
 732        if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, 1)) {
 733                DMERR("Failed to read superblock of device at position %d",
 734                      rdev->raid_disk);
 735                md_error(rdev->mddev, rdev);
 736                return -EINVAL;
 737        }
 738
 739        rdev->sb_loaded = 1;
 740
 741        return 0;
 742}
 743
 744static void super_sync(struct mddev *mddev, struct md_rdev *rdev)
 745{
 746        int i;
 747        uint64_t failed_devices;
 748        struct dm_raid_superblock *sb;
 749        struct raid_set *rs = container_of(mddev, struct raid_set, md);
 750
 751        sb = page_address(rdev->sb_page);
 752        failed_devices = le64_to_cpu(sb->failed_devices);
 753
 754        for (i = 0; i < mddev->raid_disks; i++)
 755                if (!rs->dev[i].data_dev ||
 756                    test_bit(Faulty, &(rs->dev[i].rdev.flags)))
 757                        failed_devices |= (1ULL << i);
 758
 759        memset(sb, 0, sizeof(*sb));
 760
 761        sb->magic = cpu_to_le32(DM_RAID_MAGIC);
 762        sb->features = cpu_to_le32(0);  /* No features yet */
 763
 764        sb->num_devices = cpu_to_le32(mddev->raid_disks);
 765        sb->array_position = cpu_to_le32(rdev->raid_disk);
 766
 767        sb->events = cpu_to_le64(mddev->events);
 768        sb->failed_devices = cpu_to_le64(failed_devices);
 769
 770        sb->disk_recovery_offset = cpu_to_le64(rdev->recovery_offset);
 771        sb->array_resync_offset = cpu_to_le64(mddev->recovery_cp);
 772
 773        sb->level = cpu_to_le32(mddev->level);
 774        sb->layout = cpu_to_le32(mddev->layout);
 775        sb->stripe_sectors = cpu_to_le32(mddev->chunk_sectors);
 776}
 777
 778/*
 779 * super_load
 780 *
 781 * This function creates a superblock if one is not found on the device
 782 * and will decide which superblock to use if there's a choice.
 783 *
 784 * Return: 1 if use rdev, 0 if use refdev, -Exxx otherwise
 785 */
 786static int super_load(struct md_rdev *rdev, struct md_rdev *refdev)
 787{
 788        int ret;
 789        struct dm_raid_superblock *sb;
 790        struct dm_raid_superblock *refsb;
 791        uint64_t events_sb, events_refsb;
 792
 793        rdev->sb_start = 0;
 794        rdev->sb_size = sizeof(*sb);
 795
 796        ret = read_disk_sb(rdev, rdev->sb_size);
 797        if (ret)
 798                return ret;
 799
 800        sb = page_address(rdev->sb_page);
 801
 802        /*
 803         * Two cases that we want to write new superblocks and rebuild:
 804         * 1) New device (no matching magic number)
 805         * 2) Device specified for rebuild (!In_sync w/ offset == 0)
 806         */
 807        if ((sb->magic != cpu_to_le32(DM_RAID_MAGIC)) ||
 808            (!test_bit(In_sync, &rdev->flags) && !rdev->recovery_offset)) {
 809                super_sync(rdev->mddev, rdev);
 810
 811                set_bit(FirstUse, &rdev->flags);
 812
 813                /* Force writing of superblocks to disk */
 814                set_bit(MD_CHANGE_DEVS, &rdev->mddev->flags);
 815
 816                /* Any superblock is better than none, choose that if given */
 817                return refdev ? 0 : 1;
 818        }
 819
 820        if (!refdev)
 821                return 1;
 822
 823        events_sb = le64_to_cpu(sb->events);
 824
 825        refsb = page_address(refdev->sb_page);
 826        events_refsb = le64_to_cpu(refsb->events);
 827
 828        return (events_sb > events_refsb) ? 1 : 0;
 829}
 830
 831static int super_init_validation(struct mddev *mddev, struct md_rdev *rdev)
 832{
 833        int role;
 834        struct raid_set *rs = container_of(mddev, struct raid_set, md);
 835        uint64_t events_sb;
 836        uint64_t failed_devices;
 837        struct dm_raid_superblock *sb;
 838        uint32_t new_devs = 0;
 839        uint32_t rebuilds = 0;
 840        struct md_rdev *r;
 841        struct dm_raid_superblock *sb2;
 842
 843        sb = page_address(rdev->sb_page);
 844        events_sb = le64_to_cpu(sb->events);
 845        failed_devices = le64_to_cpu(sb->failed_devices);
 846
 847        /*
 848         * Initialise to 1 if this is a new superblock.
 849         */
 850        mddev->events = events_sb ? : 1;
 851
 852        /*
 853         * Reshaping is not currently allowed
 854         */
 855        if ((le32_to_cpu(sb->level) != mddev->level) ||
 856            (le32_to_cpu(sb->layout) != mddev->layout) ||
 857            (le32_to_cpu(sb->stripe_sectors) != mddev->chunk_sectors)) {
 858                DMERR("Reshaping arrays not yet supported.");
 859                return -EINVAL;
 860        }
 861
 862        /* We can only change the number of devices in RAID1 right now */
 863        if ((rs->raid_type->level != 1) &&
 864            (le32_to_cpu(sb->num_devices) != mddev->raid_disks)) {
 865                DMERR("Reshaping arrays not yet supported.");
 866                return -EINVAL;
 867        }
 868
 869        if (!(rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC)))
 870                mddev->recovery_cp = le64_to_cpu(sb->array_resync_offset);
 871
 872        /*
 873         * During load, we set FirstUse if a new superblock was written.
 874         * There are two reasons we might not have a superblock:
 875         * 1) The array is brand new - in which case, all of the
 876         *    devices must have their In_sync bit set.  Also,
 877         *    recovery_cp must be 0, unless forced.
 878         * 2) This is a new device being added to an old array
 879         *    and the new device needs to be rebuilt - in which
 880         *    case the In_sync bit will /not/ be set and
 881         *    recovery_cp must be MaxSector.
 882         */
 883        rdev_for_each(r, mddev) {
 884                if (!test_bit(In_sync, &r->flags)) {
 885                        DMINFO("Device %d specified for rebuild: "
 886                               "Clearing superblock", r->raid_disk);
 887                        rebuilds++;
 888                } else if (test_bit(FirstUse, &r->flags))
 889                        new_devs++;
 890        }
 891
 892        if (!rebuilds) {
 893                if (new_devs == mddev->raid_disks) {
 894                        DMINFO("Superblocks created for new array");
 895                        set_bit(MD_ARRAY_FIRST_USE, &mddev->flags);
 896                } else if (new_devs) {
 897                        DMERR("New device injected "
 898                              "into existing array without 'rebuild' "
 899                              "parameter specified");
 900                        return -EINVAL;
 901                }
 902        } else if (new_devs) {
 903                DMERR("'rebuild' devices cannot be "
 904                      "injected into an array with other first-time devices");
 905                return -EINVAL;
 906        } else if (mddev->recovery_cp != MaxSector) {
 907                DMERR("'rebuild' specified while array is not in-sync");
 908                return -EINVAL;
 909        }
 910
 911        /*
 912         * Now we set the Faulty bit for those devices that are
 913         * recorded in the superblock as failed.
 914         */
 915        rdev_for_each(r, mddev) {
 916                if (!r->sb_page)
 917                        continue;
 918                sb2 = page_address(r->sb_page);
 919                sb2->failed_devices = 0;
 920
 921                /*
 922                 * Check for any device re-ordering.
 923                 */
 924                if (!test_bit(FirstUse, &r->flags) && (r->raid_disk >= 0)) {
 925                        role = le32_to_cpu(sb2->array_position);
 926                        if (role != r->raid_disk) {
 927                                if (rs->raid_type->level != 1) {
 928                                        rs->ti->error = "Cannot change device "
 929                                                "positions in RAID array";
 930                                        return -EINVAL;
 931                                }
 932                                DMINFO("RAID1 device #%d now at position #%d",
 933                                       role, r->raid_disk);
 934                        }
 935
 936                        /*
 937                         * Partial recovery is performed on
 938                         * returning failed devices.
 939                         */
 940                        if (failed_devices & (1 << role))
 941                                set_bit(Faulty, &r->flags);
 942                }
 943        }
 944
 945        return 0;
 946}
 947
 948static int super_validate(struct mddev *mddev, struct md_rdev *rdev)
 949{
 950        struct dm_raid_superblock *sb = page_address(rdev->sb_page);
 951
 952        /*
 953         * If mddev->events is not set, we know we have not yet initialized
 954         * the array.
 955         */
 956        if (!mddev->events && super_init_validation(mddev, rdev))
 957                return -EINVAL;
 958
 959        mddev->bitmap_info.offset = 4096 >> 9; /* Enable bitmap creation */
 960        rdev->mddev->bitmap_info.default_offset = 4096 >> 9;
 961        if (!test_bit(FirstUse, &rdev->flags)) {
 962                rdev->recovery_offset = le64_to_cpu(sb->disk_recovery_offset);
 963                if (rdev->recovery_offset != MaxSector)
 964                        clear_bit(In_sync, &rdev->flags);
 965        }
 966
 967        /*
 968         * If a device comes back, set it as not In_sync and no longer faulty.
 969         */
 970        if (test_bit(Faulty, &rdev->flags)) {
 971                clear_bit(Faulty, &rdev->flags);
 972                clear_bit(In_sync, &rdev->flags);
 973                rdev->saved_raid_disk = rdev->raid_disk;
 974                rdev->recovery_offset = 0;
 975        }
 976
 977        clear_bit(FirstUse, &rdev->flags);
 978
 979        return 0;
 980}
 981
 982/*
 983 * Analyse superblocks and select the freshest.
 984 */
 985static int analyse_superblocks(struct dm_target *ti, struct raid_set *rs)
 986{
 987        int ret;
 988        struct raid_dev *dev;
 989        struct md_rdev *rdev, *tmp, *freshest;
 990        struct mddev *mddev = &rs->md;
 991
 992        freshest = NULL;
 993        rdev_for_each_safe(rdev, tmp, mddev) {
 994                /*
 995                 * Skipping super_load due to DMPF_SYNC will cause
 996                 * the array to undergo initialization again as
 997                 * though it were new.  This is the intended effect
 998                 * of the "sync" directive.
 999                 *
1000                 * When reshaping capability is added, we must ensure
1001                 * that the "sync" directive is disallowed during the
1002                 * reshape.
1003                 */
1004                if (rs->print_flags & DMPF_SYNC)
1005                        continue;
1006
1007                if (!rdev->meta_bdev)
1008                        continue;
1009
1010                ret = super_load(rdev, freshest);
1011
1012                switch (ret) {
1013                case 1:
1014                        freshest = rdev;
1015                        break;
1016                case 0:
1017                        break;
1018                default:
1019                        dev = container_of(rdev, struct raid_dev, rdev);
1020                        if (dev->meta_dev)
1021                                dm_put_device(ti, dev->meta_dev);
1022
1023                        dev->meta_dev = NULL;
1024                        rdev->meta_bdev = NULL;
1025
1026                        if (rdev->sb_page)
1027                                put_page(rdev->sb_page);
1028
1029                        rdev->sb_page = NULL;
1030
1031                        rdev->sb_loaded = 0;
1032
1033                        /*
1034                         * We might be able to salvage the data device
1035                         * even though the meta device has failed.  For
1036                         * now, we behave as though '- -' had been
1037                         * set for this device in the table.
1038                         */
1039                        if (dev->data_dev)
1040                                dm_put_device(ti, dev->data_dev);
1041
1042                        dev->data_dev = NULL;
1043                        rdev->bdev = NULL;
1044
1045                        list_del(&rdev->same_set);
1046                }
1047        }
1048
1049        if (!freshest)
1050                return 0;
1051
1052        if (validate_raid_redundancy(rs)) {
1053                rs->ti->error = "Insufficient redundancy to activate array";
1054                return -EINVAL;
1055        }
1056
1057        /*
1058         * Validation of the freshest device provides the source of
1059         * validation for the remaining devices.
1060         */
1061        ti->error = "Unable to assemble array: Invalid superblocks";
1062        if (super_validate(mddev, freshest))
1063                return -EINVAL;
1064
1065        rdev_for_each(rdev, mddev)
1066                if ((rdev != freshest) && super_validate(mddev, rdev))
1067                        return -EINVAL;
1068
1069        return 0;
1070}
1071
1072/*
1073 * Construct a RAID4/5/6 mapping:
1074 * Args:
1075 *      <raid_type> <#raid_params> <raid_params>                \
1076 *      <#raid_devs> { <meta_dev1> <dev1> .. <meta_devN> <devN> }
1077 *
1078 * <raid_params> varies by <raid_type>.  See 'parse_raid_params' for
1079 * details on possible <raid_params>.
1080 */
1081static int raid_ctr(struct dm_target *ti, unsigned argc, char **argv)
1082{
1083        int ret;
1084        struct raid_type *rt;
1085        unsigned long num_raid_params, num_raid_devs;
1086        struct raid_set *rs = NULL;
1087
1088        /* Must have at least <raid_type> <#raid_params> */
1089        if (argc < 2) {
1090                ti->error = "Too few arguments";
1091                return -EINVAL;
1092        }
1093
1094        /* raid type */
1095        rt = get_raid_type(argv[0]);
1096        if (!rt) {
1097                ti->error = "Unrecognised raid_type";
1098                return -EINVAL;
1099        }
1100        argc--;
1101        argv++;
1102
1103        /* number of RAID parameters */
1104        if (strict_strtoul(argv[0], 10, &num_raid_params) < 0) {
1105                ti->error = "Cannot understand number of RAID parameters";
1106                return -EINVAL;
1107        }
1108        argc--;
1109        argv++;
1110
1111        /* Skip over RAID params for now and find out # of devices */
1112        if (num_raid_params + 1 > argc) {
1113                ti->error = "Arguments do not agree with counts given";
1114                return -EINVAL;
1115        }
1116
1117        if ((strict_strtoul(argv[num_raid_params], 10, &num_raid_devs) < 0) ||
1118            (num_raid_devs >= INT_MAX)) {
1119                ti->error = "Cannot understand number of raid devices";
1120                return -EINVAL;
1121        }
1122
1123        rs = context_alloc(ti, rt, (unsigned)num_raid_devs);
1124        if (IS_ERR(rs))
1125                return PTR_ERR(rs);
1126
1127        ret = parse_raid_params(rs, argv, (unsigned)num_raid_params);
1128        if (ret)
1129                goto bad;
1130
1131        ret = -EINVAL;
1132
1133        argc -= num_raid_params + 1; /* +1: we already have num_raid_devs */
1134        argv += num_raid_params + 1;
1135
1136        if (argc != (num_raid_devs * 2)) {
1137                ti->error = "Supplied RAID devices does not match the count given";
1138                goto bad;
1139        }
1140
1141        ret = dev_parms(rs, argv);
1142        if (ret)
1143                goto bad;
1144
1145        rs->md.sync_super = super_sync;
1146        ret = analyse_superblocks(ti, rs);
1147        if (ret)
1148                goto bad;
1149
1150        INIT_WORK(&rs->md.event_work, do_table_event);
1151        ti->private = rs;
1152        ti->num_flush_requests = 1;
1153
1154        mutex_lock(&rs->md.reconfig_mutex);
1155        ret = md_run(&rs->md);
1156        rs->md.in_sync = 0; /* Assume already marked dirty */
1157        mutex_unlock(&rs->md.reconfig_mutex);
1158
1159        if (ret) {
1160                ti->error = "Fail to run raid array";
1161                goto bad;
1162        }
1163
1164        if (ti->len != rs->md.array_sectors) {
1165                ti->error = "Array size does not match requested target length";
1166                ret = -EINVAL;
1167                goto size_mismatch;
1168        }
1169        rs->callbacks.congested_fn = raid_is_congested;
1170        dm_table_add_target_callbacks(ti->table, &rs->callbacks);
1171
1172        mddev_suspend(&rs->md);
1173        return 0;
1174
1175size_mismatch:
1176        md_stop(&rs->md);
1177bad:
1178        context_free(rs);
1179
1180        return ret;
1181}
1182
1183static void raid_dtr(struct dm_target *ti)
1184{
1185        struct raid_set *rs = ti->private;
1186
1187        list_del_init(&rs->callbacks.list);
1188        md_stop(&rs->md);
1189        context_free(rs);
1190}
1191
1192static int raid_map(struct dm_target *ti, struct bio *bio, union map_info *map_context)
1193{
1194        struct raid_set *rs = ti->private;
1195        struct mddev *mddev = &rs->md;
1196
1197        mddev->pers->make_request(mddev, bio);
1198
1199        return DM_MAPIO_SUBMITTED;
1200}
1201
1202static int raid_status(struct dm_target *ti, status_type_t type,
1203                       unsigned status_flags, char *result, unsigned maxlen)
1204{
1205        struct raid_set *rs = ti->private;
1206        unsigned raid_param_cnt = 1; /* at least 1 for chunksize */
1207        unsigned sz = 0;
1208        int i, array_in_sync = 0;
1209        sector_t sync;
1210
1211        switch (type) {
1212        case STATUSTYPE_INFO:
1213                DMEMIT("%s %d ", rs->raid_type->name, rs->md.raid_disks);
1214
1215                if (test_bit(MD_RECOVERY_RUNNING, &rs->md.recovery))
1216                        sync = rs->md.curr_resync_completed;
1217                else
1218                        sync = rs->md.recovery_cp;
1219
1220                if (sync >= rs->md.resync_max_sectors) {
1221                        array_in_sync = 1;
1222                        sync = rs->md.resync_max_sectors;
1223                } else {
1224                        /*
1225                         * The array may be doing an initial sync, or it may
1226                         * be rebuilding individual components.  If all the
1227                         * devices are In_sync, then it is the array that is
1228                         * being initialized.
1229                         */
1230                        for (i = 0; i < rs->md.raid_disks; i++)
1231                                if (!test_bit(In_sync, &rs->dev[i].rdev.flags))
1232                                        array_in_sync = 1;
1233                }
1234                /*
1235                 * Status characters:
1236                 *  'D' = Dead/Failed device
1237                 *  'a' = Alive but not in-sync
1238                 *  'A' = Alive and in-sync
1239                 */
1240                for (i = 0; i < rs->md.raid_disks; i++) {
1241                        if (test_bit(Faulty, &rs->dev[i].rdev.flags))
1242                                DMEMIT("D");
1243                        else if (!array_in_sync ||
1244                                 !test_bit(In_sync, &rs->dev[i].rdev.flags))
1245                                DMEMIT("a");
1246                        else
1247                                DMEMIT("A");
1248                }
1249
1250                /*
1251                 * In-sync ratio:
1252                 *  The in-sync ratio shows the progress of:
1253                 *   - Initializing the array
1254                 *   - Rebuilding a subset of devices of the array
1255                 *  The user can distinguish between the two by referring
1256                 *  to the status characters.
1257                 */
1258                DMEMIT(" %llu/%llu",
1259                       (unsigned long long) sync,
1260                       (unsigned long long) rs->md.resync_max_sectors);
1261
1262                break;
1263        case STATUSTYPE_TABLE:
1264                /* The string you would use to construct this array */
1265                for (i = 0; i < rs->md.raid_disks; i++) {
1266                        if ((rs->print_flags & DMPF_REBUILD) &&
1267                            rs->dev[i].data_dev &&
1268                            !test_bit(In_sync, &rs->dev[i].rdev.flags))
1269                                raid_param_cnt += 2; /* for rebuilds */
1270                        if (rs->dev[i].data_dev &&
1271                            test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1272                                raid_param_cnt += 2;
1273                }
1274
1275                raid_param_cnt += (hweight32(rs->print_flags & ~DMPF_REBUILD) * 2);
1276                if (rs->print_flags & (DMPF_SYNC | DMPF_NOSYNC))
1277                        raid_param_cnt--;
1278
1279                DMEMIT("%s %u %u", rs->raid_type->name,
1280                       raid_param_cnt, rs->md.chunk_sectors);
1281
1282                if ((rs->print_flags & DMPF_SYNC) &&
1283                    (rs->md.recovery_cp == MaxSector))
1284                        DMEMIT(" sync");
1285                if (rs->print_flags & DMPF_NOSYNC)
1286                        DMEMIT(" nosync");
1287
1288                for (i = 0; i < rs->md.raid_disks; i++)
1289                        if ((rs->print_flags & DMPF_REBUILD) &&
1290                            rs->dev[i].data_dev &&
1291                            !test_bit(In_sync, &rs->dev[i].rdev.flags))
1292                                DMEMIT(" rebuild %u", i);
1293
1294                if (rs->print_flags & DMPF_DAEMON_SLEEP)
1295                        DMEMIT(" daemon_sleep %lu",
1296                               rs->md.bitmap_info.daemon_sleep);
1297
1298                if (rs->print_flags & DMPF_MIN_RECOVERY_RATE)
1299                        DMEMIT(" min_recovery_rate %d", rs->md.sync_speed_min);
1300
1301                if (rs->print_flags & DMPF_MAX_RECOVERY_RATE)
1302                        DMEMIT(" max_recovery_rate %d", rs->md.sync_speed_max);
1303
1304                for (i = 0; i < rs->md.raid_disks; i++)
1305                        if (rs->dev[i].data_dev &&
1306                            test_bit(WriteMostly, &rs->dev[i].rdev.flags))
1307                                DMEMIT(" write_mostly %u", i);
1308
1309                if (rs->print_flags & DMPF_MAX_WRITE_BEHIND)
1310                        DMEMIT(" max_write_behind %lu",
1311                               rs->md.bitmap_info.max_write_behind);
1312
1313                if (rs->print_flags & DMPF_STRIPE_CACHE) {
1314                        struct r5conf *conf = rs->md.private;
1315
1316                        /* convert from kiB to sectors */
1317                        DMEMIT(" stripe_cache %d",
1318                               conf ? conf->max_nr_stripes * 2 : 0);
1319                }
1320
1321                if (rs->print_flags & DMPF_REGION_SIZE)
1322                        DMEMIT(" region_size %lu",
1323                               rs->md.bitmap_info.chunksize >> 9);
1324
1325                if (rs->print_flags & DMPF_RAID10_COPIES)
1326                        DMEMIT(" raid10_copies %u",
1327                               raid10_md_layout_to_copies(rs->md.layout));
1328
1329                if (rs->print_flags & DMPF_RAID10_FORMAT)
1330                        DMEMIT(" raid10_format near");
1331
1332                DMEMIT(" %d", rs->md.raid_disks);
1333                for (i = 0; i < rs->md.raid_disks; i++) {
1334                        if (rs->dev[i].meta_dev)
1335                                DMEMIT(" %s", rs->dev[i].meta_dev->name);
1336                        else
1337                                DMEMIT(" -");
1338
1339                        if (rs->dev[i].data_dev)
1340                                DMEMIT(" %s", rs->dev[i].data_dev->name);
1341                        else
1342                                DMEMIT(" -");
1343                }
1344        }
1345
1346        return 0;
1347}
1348
1349static int raid_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn, void *data)
1350{
1351        struct raid_set *rs = ti->private;
1352        unsigned i;
1353        int ret = 0;
1354
1355        for (i = 0; !ret && i < rs->md.raid_disks; i++)
1356                if (rs->dev[i].data_dev)
1357                        ret = fn(ti,
1358                                 rs->dev[i].data_dev,
1359                                 0, /* No offset on data devs */
1360                                 rs->md.dev_sectors,
1361                                 data);
1362
1363        return ret;
1364}
1365
1366static void raid_io_hints(struct dm_target *ti, struct queue_limits *limits)
1367{
1368        struct raid_set *rs = ti->private;
1369        unsigned chunk_size = rs->md.chunk_sectors << 9;
1370        struct r5conf *conf = rs->md.private;
1371
1372        blk_limits_io_min(limits, chunk_size);
1373        blk_limits_io_opt(limits, chunk_size * (conf->raid_disks - conf->max_degraded));
1374}
1375
1376static void raid_presuspend(struct dm_target *ti)
1377{
1378        struct raid_set *rs = ti->private;
1379
1380        md_stop_writes(&rs->md);
1381}
1382
1383static void raid_postsuspend(struct dm_target *ti)
1384{
1385        struct raid_set *rs = ti->private;
1386
1387        mddev_suspend(&rs->md);
1388}
1389
1390static void raid_resume(struct dm_target *ti)
1391{
1392        struct raid_set *rs = ti->private;
1393
1394        set_bit(MD_CHANGE_DEVS, &rs->md.flags);
1395        if (!rs->bitmap_loaded) {
1396                bitmap_load(&rs->md);
1397                rs->bitmap_loaded = 1;
1398        }
1399
1400        clear_bit(MD_RECOVERY_FROZEN, &rs->md.recovery);
1401        mddev_resume(&rs->md);
1402}
1403
1404static struct target_type raid_target = {
1405        .name = "raid",
1406        .version = {1, 3, 2},
1407        .module = THIS_MODULE,
1408        .ctr = raid_ctr,
1409        .dtr = raid_dtr,
1410        .map = raid_map,
1411        .status = raid_status,
1412        .iterate_devices = raid_iterate_devices,
1413        .io_hints = raid_io_hints,
1414        .presuspend = raid_presuspend,
1415        .postsuspend = raid_postsuspend,
1416        .resume = raid_resume,
1417};
1418
1419static int __init dm_raid_init(void)
1420{
1421        return dm_register_target(&raid_target);
1422}
1423
1424static void __exit dm_raid_exit(void)
1425{
1426        dm_unregister_target(&raid_target);
1427}
1428
1429module_init(dm_raid_init);
1430module_exit(dm_raid_exit);
1431
1432MODULE_DESCRIPTION(DM_NAME " raid4/5/6 target");
1433MODULE_ALIAS("dm-raid1");
1434MODULE_ALIAS("dm-raid10");
1435MODULE_ALIAS("dm-raid4");
1436MODULE_ALIAS("dm-raid5");
1437MODULE_ALIAS("dm-raid6");
1438MODULE_AUTHOR("Neil Brown <dm-devel@redhat.com>");
1439MODULE_LICENSE("GPL");
1440
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.