linux/drivers/md/md.c
<<
>>
Prefs
   1/*
   2   md.c : Multiple Devices driver for Linux
   3          Copyright (C) 1998, 1999, 2000 Ingo Molnar
   4
   5     completely rewritten, based on the MD driver code from Marc Zyngier
   6
   7   Changes:
   8
   9   - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
  10   - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
  11   - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
  12   - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
  13   - kmod support by: Cyrus Durgin
  14   - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
  15   - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
  16
  17   - lots of fixes and improvements to the RAID1/RAID5 and generic
  18     RAID code (such as request based resynchronization):
  19
  20     Neil Brown <neilb@cse.unsw.edu.au>.
  21
  22   - persistent bitmap code
  23     Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
  24
  25   This program is free software; you can redistribute it and/or modify
  26   it under the terms of the GNU General Public License as published by
  27   the Free Software Foundation; either version 2, or (at your option)
  28   any later version.
  29
  30   You should have received a copy of the GNU General Public License
  31   (for example /usr/src/linux/COPYING); if not, write to the Free
  32   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33*/
  34
  35#include <linux/kthread.h>
  36#include <linux/blkdev.h>
  37#include <linux/sysctl.h>
  38#include <linux/seq_file.h>
  39#include <linux/fs.h>
  40#include <linux/poll.h>
  41#include <linux/ctype.h>
  42#include <linux/string.h>
  43#include <linux/hdreg.h>
  44#include <linux/proc_fs.h>
  45#include <linux/random.h>
  46#include <linux/module.h>
  47#include <linux/reboot.h>
  48#include <linux/file.h>
  49#include <linux/compat.h>
  50#include <linux/delay.h>
  51#include <linux/raid/md_p.h>
  52#include <linux/raid/md_u.h>
  53#include <linux/slab.h>
  54#include "md.h"
  55#include "bitmap.h"
  56
  57#ifndef MODULE
  58static void autostart_arrays(int part);
  59#endif
  60
  61/* pers_list is a list of registered personalities protected
  62 * by pers_lock.
  63 * pers_lock does extra service to protect accesses to
  64 * mddev->thread when the mutex cannot be held.
  65 */
  66static LIST_HEAD(pers_list);
  67static DEFINE_SPINLOCK(pers_lock);
  68
  69static void md_print_devices(void);
  70
  71static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
  72static struct workqueue_struct *md_wq;
  73static struct workqueue_struct *md_misc_wq;
  74
  75#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
  76
  77/*
  78 * Default number of read corrections we'll attempt on an rdev
  79 * before ejecting it from the array. We divide the read error
  80 * count by 2 for every hour elapsed between read errors.
  81 */
  82#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
  83/*
  84 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
  85 * is 1000 KB/sec, so the extra system load does not show up that much.
  86 * Increase it if you want to have more _guaranteed_ speed. Note that
  87 * the RAID driver will use the maximum available bandwidth if the IO
  88 * subsystem is idle. There is also an 'absolute maximum' reconstruction
  89 * speed limit - in case reconstruction slows down your system despite
  90 * idle IO detection.
  91 *
  92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
  93 * or /sys/block/mdX/md/sync_speed_{min,max}
  94 */
  95
  96static int sysctl_speed_limit_min = 1000;
  97static int sysctl_speed_limit_max = 200000;
  98static inline int speed_min(struct mddev *mddev)
  99{
 100        return mddev->sync_speed_min ?
 101                mddev->sync_speed_min : sysctl_speed_limit_min;
 102}
 103
 104static inline int speed_max(struct mddev *mddev)
 105{
 106        return mddev->sync_speed_max ?
 107                mddev->sync_speed_max : sysctl_speed_limit_max;
 108}
 109
 110static struct ctl_table_header *raid_table_header;
 111
 112static ctl_table raid_table[] = {
 113        {
 114                .procname       = "speed_limit_min",
 115                .data           = &sysctl_speed_limit_min,
 116                .maxlen         = sizeof(int),
 117                .mode           = S_IRUGO|S_IWUSR,
 118                .proc_handler   = proc_dointvec,
 119        },
 120        {
 121                .procname       = "speed_limit_max",
 122                .data           = &sysctl_speed_limit_max,
 123                .maxlen         = sizeof(int),
 124                .mode           = S_IRUGO|S_IWUSR,
 125                .proc_handler   = proc_dointvec,
 126        },
 127        { }
 128};
 129
 130static ctl_table raid_dir_table[] = {
 131        {
 132                .procname       = "raid",
 133                .maxlen         = 0,
 134                .mode           = S_IRUGO|S_IXUGO,
 135                .child          = raid_table,
 136        },
 137        { }
 138};
 139
 140static ctl_table raid_root_table[] = {
 141        {
 142                .procname       = "dev",
 143                .maxlen         = 0,
 144                .mode           = 0555,
 145                .child          = raid_dir_table,
 146        },
 147        {  }
 148};
 149
 150static const struct block_device_operations md_fops;
 151
 152static int start_readonly;
 153
 154/* bio_clone_mddev
 155 * like bio_clone, but with a local bio set
 156 */
 157
 158struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
 159                            struct mddev *mddev)
 160{
 161        struct bio *b;
 162
 163        if (!mddev || !mddev->bio_set)
 164                return bio_alloc(gfp_mask, nr_iovecs);
 165
 166        b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
 167        if (!b)
 168                return NULL;
 169        return b;
 170}
 171EXPORT_SYMBOL_GPL(bio_alloc_mddev);
 172
 173struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
 174                            struct mddev *mddev)
 175{
 176        if (!mddev || !mddev->bio_set)
 177                return bio_clone(bio, gfp_mask);
 178
 179        return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
 180}
 181EXPORT_SYMBOL_GPL(bio_clone_mddev);
 182
 183void md_trim_bio(struct bio *bio, int offset, int size)
 184{
 185        /* 'bio' is a cloned bio which we need to trim to match
 186         * the given offset and size.
 187         * This requires adjusting bi_sector, bi_size, and bi_io_vec
 188         */
 189        int i;
 190        struct bio_vec *bvec;
 191        int sofar = 0;
 192
 193        size <<= 9;
 194        if (offset == 0 && size == bio->bi_size)
 195                return;
 196
 197        bio->bi_sector += offset;
 198        bio->bi_size = size;
 199        offset <<= 9;
 200        clear_bit(BIO_SEG_VALID, &bio->bi_flags);
 201
 202        while (bio->bi_idx < bio->bi_vcnt &&
 203               bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
 204                /* remove this whole bio_vec */
 205                offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
 206                bio->bi_idx++;
 207        }
 208        if (bio->bi_idx < bio->bi_vcnt) {
 209                bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
 210                bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
 211        }
 212        /* avoid any complications with bi_idx being non-zero*/
 213        if (bio->bi_idx) {
 214                memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
 215                        (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
 216                bio->bi_vcnt -= bio->bi_idx;
 217                bio->bi_idx = 0;
 218        }
 219        /* Make sure vcnt and last bv are not too big */
 220        bio_for_each_segment(bvec, bio, i) {
 221                if (sofar + bvec->bv_len > size)
 222                        bvec->bv_len = size - sofar;
 223                if (bvec->bv_len == 0) {
 224                        bio->bi_vcnt = i;
 225                        break;
 226                }
 227                sofar += bvec->bv_len;
 228        }
 229}
 230EXPORT_SYMBOL_GPL(md_trim_bio);
 231
 232/*
 233 * We have a system wide 'event count' that is incremented
 234 * on any 'interesting' event, and readers of /proc/mdstat
 235 * can use 'poll' or 'select' to find out when the event
 236 * count increases.
 237 *
 238 * Events are:
 239 *  start array, stop array, error, add device, remove device,
 240 *  start build, activate spare
 241 */
 242static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
 243static atomic_t md_event_count;
 244void md_new_event(struct mddev *mddev)
 245{
 246        atomic_inc(&md_event_count);
 247        wake_up(&md_event_waiters);
 248}
 249EXPORT_SYMBOL_GPL(md_new_event);
 250
 251/* Alternate version that can be called from interrupts
 252 * when calling sysfs_notify isn't needed.
 253 */
 254static void md_new_event_inintr(struct mddev *mddev)
 255{
 256        atomic_inc(&md_event_count);
 257        wake_up(&md_event_waiters);
 258}
 259
 260/*
 261 * Enables to iterate over all existing md arrays
 262 * all_mddevs_lock protects this list.
 263 */
 264static LIST_HEAD(all_mddevs);
 265static DEFINE_SPINLOCK(all_mddevs_lock);
 266
 267
 268/*
 269 * iterates through all used mddevs in the system.
 270 * We take care to grab the all_mddevs_lock whenever navigating
 271 * the list, and to always hold a refcount when unlocked.
 272 * Any code which breaks out of this loop while own
 273 * a reference to the current mddev and must mddev_put it.
 274 */
 275#define for_each_mddev(_mddev,_tmp)                                     \
 276                                                                        \
 277        for (({ spin_lock(&all_mddevs_lock);                            \
 278                _tmp = all_mddevs.next;                                 \
 279                _mddev = NULL;});                                       \
 280             ({ if (_tmp != &all_mddevs)                                \
 281                        mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
 282                spin_unlock(&all_mddevs_lock);                          \
 283                if (_mddev) mddev_put(_mddev);                          \
 284                _mddev = list_entry(_tmp, struct mddev, all_mddevs);    \
 285                _tmp != &all_mddevs;});                                 \
 286             ({ spin_lock(&all_mddevs_lock);                            \
 287                _tmp = _tmp->next;})                                    \
 288                )
 289
 290
 291/* Rather than calling directly into the personality make_request function,
 292 * IO requests come here first so that we can check if the device is
 293 * being suspended pending a reconfiguration.
 294 * We hold a refcount over the call to ->make_request.  By the time that
 295 * call has finished, the bio has been linked into some internal structure
 296 * and so is visible to ->quiesce(), so we don't need the refcount any more.
 297 */
 298static void md_make_request(struct request_queue *q, struct bio *bio)
 299{
 300        const int rw = bio_data_dir(bio);
 301        struct mddev *mddev = q->queuedata;
 302        int cpu;
 303        unsigned int sectors;
 304
 305        if (mddev == NULL || mddev->pers == NULL
 306            || !mddev->ready) {
 307                bio_io_error(bio);
 308                return;
 309        }
 310        smp_rmb(); /* Ensure implications of  'active' are visible */
 311        rcu_read_lock();
 312        if (mddev->suspended) {
 313                DEFINE_WAIT(__wait);
 314                for (;;) {
 315                        prepare_to_wait(&mddev->sb_wait, &__wait,
 316                                        TASK_UNINTERRUPTIBLE);
 317                        if (!mddev->suspended)
 318                                break;
 319                        rcu_read_unlock();
 320                        schedule();
 321                        rcu_read_lock();
 322                }
 323                finish_wait(&mddev->sb_wait, &__wait);
 324        }
 325        atomic_inc(&mddev->active_io);
 326        rcu_read_unlock();
 327
 328        /*
 329         * save the sectors now since our bio can
 330         * go away inside make_request
 331         */
 332        sectors = bio_sectors(bio);
 333        mddev->pers->make_request(mddev, bio);
 334
 335        cpu = part_stat_lock();
 336        part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
 337        part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
 338        part_stat_unlock();
 339
 340        if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
 341                wake_up(&mddev->sb_wait);
 342}
 343
 344/* mddev_suspend makes sure no new requests are submitted
 345 * to the device, and that any requests that have been submitted
 346 * are completely handled.
 347 * Once ->stop is called and completes, the module will be completely
 348 * unused.
 349 */
 350void mddev_suspend(struct mddev *mddev)
 351{
 352        BUG_ON(mddev->suspended);
 353        mddev->suspended = 1;
 354        synchronize_rcu();
 355        wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
 356        mddev->pers->quiesce(mddev, 1);
 357
 358        del_timer_sync(&mddev->safemode_timer);
 359}
 360EXPORT_SYMBOL_GPL(mddev_suspend);
 361
 362void mddev_resume(struct mddev *mddev)
 363{
 364        mddev->suspended = 0;
 365        wake_up(&mddev->sb_wait);
 366        mddev->pers->quiesce(mddev, 0);
 367
 368        set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
 369        md_wakeup_thread(mddev->thread);
 370        md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
 371}
 372EXPORT_SYMBOL_GPL(mddev_resume);
 373
 374int mddev_congested(struct mddev *mddev, int bits)
 375{
 376        return mddev->suspended;
 377}
 378EXPORT_SYMBOL(mddev_congested);
 379
 380/*
 381 * Generic flush handling for md
 382 */
 383
 384static void md_end_flush(struct bio *bio, int err)
 385{
 386        struct md_rdev *rdev = bio->bi_private;
 387        struct mddev *mddev = rdev->mddev;
 388
 389        rdev_dec_pending(rdev, mddev);
 390
 391        if (atomic_dec_and_test(&mddev->flush_pending)) {
 392                /* The pre-request flush has finished */
 393                queue_work(md_wq, &mddev->flush_work);
 394        }
 395        bio_put(bio);
 396}
 397
 398static void md_submit_flush_data(struct work_struct *ws);
 399
 400static void submit_flushes(struct work_struct *ws)
 401{
 402        struct mddev *mddev = container_of(ws, struct mddev, flush_work);
 403        struct md_rdev *rdev;
 404
 405        INIT_WORK(&mddev->flush_work, md_submit_flush_data);
 406        atomic_set(&mddev->flush_pending, 1);
 407        rcu_read_lock();
 408        rdev_for_each_rcu(rdev, mddev)
 409                if (rdev->raid_disk >= 0 &&
 410                    !test_bit(Faulty, &rdev->flags)) {
 411                        /* Take two references, one is dropped
 412                         * when request finishes, one after
 413                         * we reclaim rcu_read_lock
 414                         */
 415                        struct bio *bi;
 416                        atomic_inc(&rdev->nr_pending);
 417                        atomic_inc(&rdev->nr_pending);
 418                        rcu_read_unlock();
 419                        bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
 420                        bi->bi_end_io = md_end_flush;
 421                        bi->bi_private = rdev;
 422                        bi->bi_bdev = rdev->bdev;
 423                        atomic_inc(&mddev->flush_pending);
 424                        submit_bio(WRITE_FLUSH, bi);
 425                        rcu_read_lock();
 426                        rdev_dec_pending(rdev, mddev);
 427                }
 428        rcu_read_unlock();
 429        if (atomic_dec_and_test(&mddev->flush_pending))
 430                queue_work(md_wq, &mddev->flush_work);
 431}
 432
 433static void md_submit_flush_data(struct work_struct *ws)
 434{
 435        struct mddev *mddev = container_of(ws, struct mddev, flush_work);
 436        struct bio *bio = mddev->flush_bio;
 437
 438        if (bio->bi_size == 0)
 439                /* an empty barrier - all done */
 440                bio_endio(bio, 0);
 441        else {
 442                bio->bi_rw &= ~REQ_FLUSH;
 443                mddev->pers->make_request(mddev, bio);
 444        }
 445
 446        mddev->flush_bio = NULL;
 447        wake_up(&mddev->sb_wait);
 448}
 449
 450void md_flush_request(struct mddev *mddev, struct bio *bio)
 451{
 452        spin_lock_irq(&mddev->write_lock);
 453        wait_event_lock_irq(mddev->sb_wait,
 454                            !mddev->flush_bio,
 455                            mddev->write_lock);
 456        mddev->flush_bio = bio;
 457        spin_unlock_irq(&mddev->write_lock);
 458
 459        INIT_WORK(&mddev->flush_work, submit_flushes);
 460        queue_work(md_wq, &mddev->flush_work);
 461}
 462EXPORT_SYMBOL(md_flush_request);
 463
 464void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
 465{
 466        struct mddev *mddev = cb->data;
 467        md_wakeup_thread(mddev->thread);
 468        kfree(cb);
 469}
 470EXPORT_SYMBOL(md_unplug);
 471
 472static inline struct mddev *mddev_get(struct mddev *mddev)
 473{
 474        atomic_inc(&mddev->active);
 475        return mddev;
 476}
 477
 478static void mddev_delayed_delete(struct work_struct *ws);
 479
 480static void mddev_put(struct mddev *mddev)
 481{
 482        struct bio_set *bs = NULL;
 483
 484        if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
 485                return;
 486        if (!mddev->raid_disks && list_empty(&mddev->disks) &&
 487            mddev->ctime == 0 && !mddev->hold_active) {
 488                /* Array is not configured at all, and not held active,
 489                 * so destroy it */
 490                list_del_init(&mddev->all_mddevs);
 491                bs = mddev->bio_set;
 492                mddev->bio_set = NULL;
 493                if (mddev->gendisk) {
 494                        /* We did a probe so need to clean up.  Call
 495                         * queue_work inside the spinlock so that
 496                         * flush_workqueue() after mddev_find will
 497                         * succeed in waiting for the work to be done.
 498                         */
 499                        INIT_WORK(&mddev->del_work, mddev_delayed_delete);
 500                        queue_work(md_misc_wq, &mddev->del_work);
 501                } else
 502                        kfree(mddev);
 503        }
 504        spin_unlock(&all_mddevs_lock);
 505        if (bs)
 506                bioset_free(bs);
 507}
 508
 509void mddev_init(struct mddev *mddev)
 510{
 511        mutex_init(&mddev->open_mutex);
 512        mutex_init(&mddev->reconfig_mutex);
 513        mutex_init(&mddev->bitmap_info.mutex);
 514        INIT_LIST_HEAD(&mddev->disks);
 515        INIT_LIST_HEAD(&mddev->all_mddevs);
 516        init_timer(&mddev->safemode_timer);
 517        atomic_set(&mddev->active, 1);
 518        atomic_set(&mddev->openers, 0);
 519        atomic_set(&mddev->active_io, 0);
 520        spin_lock_init(&mddev->write_lock);
 521        atomic_set(&mddev->flush_pending, 0);
 522        init_waitqueue_head(&mddev->sb_wait);
 523        init_waitqueue_head(&mddev->recovery_wait);
 524        mddev->reshape_position = MaxSector;
 525        mddev->reshape_backwards = 0;
 526        mddev->resync_min = 0;
 527        mddev->resync_max = MaxSector;
 528        mddev->level = LEVEL_NONE;
 529}
 530EXPORT_SYMBOL_GPL(mddev_init);
 531
 532static struct mddev * mddev_find(dev_t unit)
 533{
 534        struct mddev *mddev, *new = NULL;
 535
 536        if (unit && MAJOR(unit) != MD_MAJOR)
 537                unit &= ~((1<<MdpMinorShift)-1);
 538
 539 retry:
 540        spin_lock(&all_mddevs_lock);
 541
 542        if (unit) {
 543                list_for_each_entry(mddev, &all_mddevs, all_mddevs)
 544                        if (mddev->unit == unit) {
 545                                mddev_get(mddev);
 546                                spin_unlock(&all_mddevs_lock);
 547                                kfree(new);
 548                                return mddev;
 549                        }
 550
 551                if (new) {
 552                        list_add(&new->all_mddevs, &all_mddevs);
 553                        spin_unlock(&all_mddevs_lock);
 554                        new->hold_active = UNTIL_IOCTL;
 555                        return new;
 556                }
 557        } else if (new) {
 558                /* find an unused unit number */
 559                static int next_minor = 512;
 560                int start = next_minor;
 561                int is_free = 0;
 562                int dev = 0;
 563                while (!is_free) {
 564                        dev = MKDEV(MD_MAJOR, next_minor);
 565                        next_minor++;
 566                        if (next_minor > MINORMASK)
 567                                next_minor = 0;
 568                        if (next_minor == start) {
 569                                /* Oh dear, all in use. */
 570                                spin_unlock(&all_mddevs_lock);
 571                                kfree(new);
 572                                return NULL;
 573                        }
 574                                
 575                        is_free = 1;
 576                        list_for_each_entry(mddev, &all_mddevs, all_mddevs)
 577                                if (mddev->unit == dev) {
 578                                        is_free = 0;
 579                                        break;
 580                                }
 581                }
 582                new->unit = dev;
 583                new->md_minor = MINOR(dev);
 584                new->hold_active = UNTIL_STOP;
 585                list_add(&new->all_mddevs, &all_mddevs);
 586                spin_unlock(&all_mddevs_lock);
 587                return new;
 588        }
 589        spin_unlock(&all_mddevs_lock);
 590
 591        new = kzalloc(sizeof(*new), GFP_KERNEL);
 592        if (!new)
 593                return NULL;
 594
 595        new->unit = unit;
 596        if (MAJOR(unit) == MD_MAJOR)
 597                new->md_minor = MINOR(unit);
 598        else
 599                new->md_minor = MINOR(unit) >> MdpMinorShift;
 600
 601        mddev_init(new);
 602
 603        goto retry;
 604}
 605
 606static inline int mddev_lock(struct mddev * mddev)
 607{
 608        return mutex_lock_interruptible(&mddev->reconfig_mutex);
 609}
 610
 611static inline int mddev_is_locked(struct mddev *mddev)
 612{
 613        return mutex_is_locked(&mddev->reconfig_mutex);
 614}
 615
 616static inline int mddev_trylock(struct mddev * mddev)
 617{
 618        return mutex_trylock(&mddev->reconfig_mutex);
 619}
 620
 621static struct attribute_group md_redundancy_group;
 622
 623static void mddev_unlock(struct mddev * mddev)
 624{
 625        if (mddev->to_remove) {
 626                /* These cannot be removed under reconfig_mutex as
 627                 * an access to the files will try to take reconfig_mutex
 628                 * while holding the file unremovable, which leads to
 629                 * a deadlock.
 630                 * So hold set sysfs_active while the remove in happeing,
 631                 * and anything else which might set ->to_remove or my
 632                 * otherwise change the sysfs namespace will fail with
 633                 * -EBUSY if sysfs_active is still set.
 634                 * We set sysfs_active under reconfig_mutex and elsewhere
 635                 * test it under the same mutex to ensure its correct value
 636                 * is seen.
 637                 */
 638                struct attribute_group *to_remove = mddev->to_remove;
 639                mddev->to_remove = NULL;
 640                mddev->sysfs_active = 1;
 641                mutex_unlock(&mddev->reconfig_mutex);
 642
 643                if (mddev->kobj.sd) {
 644                        if (to_remove != &md_redundancy_group)
 645                                sysfs_remove_group(&mddev->kobj, to_remove);
 646                        if (mddev->pers == NULL ||
 647                            mddev->pers->sync_request == NULL) {
 648                                sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
 649                                if (mddev->sysfs_action)
 650                                        sysfs_put(mddev->sysfs_action);
 651                                mddev->sysfs_action = NULL;
 652                        }
 653                }
 654                mddev->sysfs_active = 0;
 655        } else
 656                mutex_unlock(&mddev->reconfig_mutex);
 657
 658        /* As we've dropped the mutex we need a spinlock to
 659         * make sure the thread doesn't disappear
 660         */
 661        spin_lock(&pers_lock);
 662        md_wakeup_thread(mddev->thread);
 663        spin_unlock(&pers_lock);
 664}
 665
 666static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
 667{
 668        struct md_rdev *rdev;
 669
 670        rdev_for_each(rdev, mddev)
 671                if (rdev->desc_nr == nr)
 672                        return rdev;
 673
 674        return NULL;
 675}
 676
 677static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
 678{
 679        struct md_rdev *rdev;
 680
 681        rdev_for_each_rcu(rdev, mddev)
 682                if (rdev->desc_nr == nr)
 683                        return rdev;
 684
 685        return NULL;
 686}
 687
 688static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
 689{
 690        struct md_rdev *rdev;
 691
 692        rdev_for_each(rdev, mddev)
 693                if (rdev->bdev->bd_dev == dev)
 694                        return rdev;
 695
 696        return NULL;
 697}
 698
 699static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
 700{
 701        struct md_rdev *rdev;
 702
 703        rdev_for_each_rcu(rdev, mddev)
 704                if (rdev->bdev->bd_dev == dev)
 705                        return rdev;
 706
 707        return NULL;
 708}
 709
 710static struct md_personality *find_pers(int level, char *clevel)
 711{
 712        struct md_personality *pers;
 713        list_for_each_entry(pers, &pers_list, list) {
 714                if (level != LEVEL_NONE && pers->level == level)
 715                        return pers;
 716                if (strcmp(pers->name, clevel)==0)
 717                        return pers;
 718        }
 719        return NULL;
 720}
 721
 722/* return the offset of the super block in 512byte sectors */
 723static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
 724{
 725        sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
 726        return MD_NEW_SIZE_SECTORS(num_sectors);
 727}
 728
 729static int alloc_disk_sb(struct md_rdev * rdev)
 730{
 731        if (rdev->sb_page)
 732                MD_BUG();
 733
 734        rdev->sb_page = alloc_page(GFP_KERNEL);
 735        if (!rdev->sb_page) {
 736                printk(KERN_ALERT "md: out of memory.\n");
 737                return -ENOMEM;
 738        }
 739
 740        return 0;
 741}
 742
 743void md_rdev_clear(struct md_rdev *rdev)
 744{
 745        if (rdev->sb_page) {
 746                put_page(rdev->sb_page);
 747                rdev->sb_loaded = 0;
 748                rdev->sb_page = NULL;
 749                rdev->sb_start = 0;
 750                rdev->sectors = 0;
 751        }
 752        if (rdev->bb_page) {
 753                put_page(rdev->bb_page);
 754                rdev->bb_page = NULL;
 755        }
 756        kfree(rdev->badblocks.page);
 757        rdev->badblocks.page = NULL;
 758}
 759EXPORT_SYMBOL_GPL(md_rdev_clear);
 760
 761static void super_written(struct bio *bio, int error)
 762{
 763        struct md_rdev *rdev = bio->bi_private;
 764        struct mddev *mddev = rdev->mddev;
 765
 766        if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
 767                printk("md: super_written gets error=%d, uptodate=%d\n",
 768                       error, test_bit(BIO_UPTODATE, &bio->bi_flags));
 769                WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
 770                md_error(mddev, rdev);
 771        }
 772
 773        if (atomic_dec_and_test(&mddev->pending_writes))
 774                wake_up(&mddev->sb_wait);
 775        bio_put(bio);
 776}
 777
 778void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
 779                   sector_t sector, int size, struct page *page)
 780{
 781        /* write first size bytes of page to sector of rdev
 782         * Increment mddev->pending_writes before returning
 783         * and decrement it on completion, waking up sb_wait
 784         * if zero is reached.
 785         * If an error occurred, call md_error
 786         */
 787        struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
 788
 789        bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
 790        bio->bi_sector = sector;
 791        bio_add_page(bio, page, size, 0);
 792        bio->bi_private = rdev;
 793        bio->bi_end_io = super_written;
 794
 795        atomic_inc(&mddev->pending_writes);
 796        submit_bio(WRITE_FLUSH_FUA, bio);
 797}
 798
 799void md_super_wait(struct mddev *mddev)
 800{
 801        /* wait for all superblock writes that were scheduled to complete */
 802        DEFINE_WAIT(wq);
 803        for(;;) {
 804                prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
 805                if (atomic_read(&mddev->pending_writes)==0)
 806                        break;
 807                schedule();
 808        }
 809        finish_wait(&mddev->sb_wait, &wq);
 810}
 811
 812static void bi_complete(struct bio *bio, int error)
 813{
 814        complete((struct completion*)bio->bi_private);
 815}
 816
 817int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
 818                 struct page *page, int rw, bool metadata_op)
 819{
 820        struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
 821        struct completion event;
 822        int ret;
 823
 824        rw |= REQ_SYNC;
 825
 826        bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
 827                rdev->meta_bdev : rdev->bdev;
 828        if (metadata_op)
 829                bio->bi_sector = sector + rdev->sb_start;
 830        else if (rdev->mddev->reshape_position != MaxSector &&
 831                 (rdev->mddev->reshape_backwards ==
 832                  (sector >= rdev->mddev->reshape_position)))
 833                bio->bi_sector = sector + rdev->new_data_offset;
 834        else
 835                bio->bi_sector = sector + rdev->data_offset;
 836        bio_add_page(bio, page, size, 0);
 837        init_completion(&event);
 838        bio->bi_private = &event;
 839        bio->bi_end_io = bi_complete;
 840        submit_bio(rw, bio);
 841        wait_for_completion(&event);
 842
 843        ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
 844        bio_put(bio);
 845        return ret;
 846}
 847EXPORT_SYMBOL_GPL(sync_page_io);
 848
 849static int read_disk_sb(struct md_rdev * rdev, int size)
 850{
 851        char b[BDEVNAME_SIZE];
 852        if (!rdev->sb_page) {
 853                MD_BUG();
 854                return -EINVAL;
 855        }
 856        if (rdev->sb_loaded)
 857                return 0;
 858
 859
 860        if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
 861                goto fail;
 862        rdev->sb_loaded = 1;
 863        return 0;
 864
 865fail:
 866        printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
 867                bdevname(rdev->bdev,b));
 868        return -EINVAL;
 869}
 870
 871static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
 872{
 873        return  sb1->set_uuid0 == sb2->set_uuid0 &&
 874                sb1->set_uuid1 == sb2->set_uuid1 &&
 875                sb1->set_uuid2 == sb2->set_uuid2 &&
 876                sb1->set_uuid3 == sb2->set_uuid3;
 877}
 878
 879static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
 880{
 881        int ret;
 882        mdp_super_t *tmp1, *tmp2;
 883
 884        tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
 885        tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
 886
 887        if (!tmp1 || !tmp2) {
 888                ret = 0;
 889                printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
 890                goto abort;
 891        }
 892
 893        *tmp1 = *sb1;
 894        *tmp2 = *sb2;
 895
 896        /*
 897         * nr_disks is not constant
 898         */
 899        tmp1->nr_disks = 0;
 900        tmp2->nr_disks = 0;
 901
 902        ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
 903abort:
 904        kfree(tmp1);
 905        kfree(tmp2);
 906        return ret;
 907}
 908
 909
 910static u32 md_csum_fold(u32 csum)
 911{
 912        csum = (csum & 0xffff) + (csum >> 16);
 913        return (csum & 0xffff) + (csum >> 16);
 914}
 915
 916static unsigned int calc_sb_csum(mdp_super_t * sb)
 917{
 918        u64 newcsum = 0;
 919        u32 *sb32 = (u32*)sb;
 920        int i;
 921        unsigned int disk_csum, csum;
 922
 923        disk_csum = sb->sb_csum;
 924        sb->sb_csum = 0;
 925
 926        for (i = 0; i < MD_SB_BYTES/4 ; i++)
 927                newcsum += sb32[i];
 928        csum = (newcsum & 0xffffffff) + (newcsum>>32);
 929
 930
 931#ifdef CONFIG_ALPHA
 932        /* This used to use csum_partial, which was wrong for several
 933         * reasons including that different results are returned on
 934         * different architectures.  It isn't critical that we get exactly
 935         * the same return value as before (we always csum_fold before
 936         * testing, and that removes any differences).  However as we
 937         * know that csum_partial always returned a 16bit value on
 938         * alphas, do a fold to maximise conformity to previous behaviour.
 939         */
 940        sb->sb_csum = md_csum_fold(disk_csum);
 941#else
 942        sb->sb_csum = disk_csum;
 943#endif
 944        return csum;
 945}
 946
 947
 948/*
 949 * Handle superblock details.
 950 * We want to be able to handle multiple superblock formats
 951 * so we have a common interface to them all, and an array of
 952 * different handlers.
 953 * We rely on user-space to write the initial superblock, and support
 954 * reading and updating of superblocks.
 955 * Interface methods are:
 956 *   int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
 957 *      loads and validates a superblock on dev.
 958 *      if refdev != NULL, compare superblocks on both devices
 959 *    Return:
 960 *      0 - dev has a superblock that is compatible with refdev
 961 *      1 - dev has a superblock that is compatible and newer than refdev
 962 *          so dev should be used as the refdev in future
 963 *     -EINVAL superblock incompatible or invalid
 964 *     -othererror e.g. -EIO
 965 *
 966 *   int validate_super(struct mddev *mddev, struct md_rdev *dev)
 967 *      Verify that dev is acceptable into mddev.
 968 *       The first time, mddev->raid_disks will be 0, and data from
 969 *       dev should be merged in.  Subsequent calls check that dev
 970 *       is new enough.  Return 0 or -EINVAL
 971 *
 972 *   void sync_super(struct mddev *mddev, struct md_rdev *dev)
 973 *     Update the superblock for rdev with data in mddev
 974 *     This does not write to disc.
 975 *
 976 */
 977
 978struct super_type  {
 979        char                *name;
 980        struct module       *owner;
 981        int                 (*load_super)(struct md_rdev *rdev,
 982                                          struct md_rdev *refdev,
 983                                          int minor_version);
 984        int                 (*validate_super)(struct mddev *mddev,
 985                                              struct md_rdev *rdev);
 986        void                (*sync_super)(struct mddev *mddev,
 987                                          struct md_rdev *rdev);
 988        unsigned long long  (*rdev_size_change)(struct md_rdev *rdev,
 989                                                sector_t num_sectors);
 990        int                 (*allow_new_offset)(struct md_rdev *rdev,
 991                                                unsigned long long new_offset);
 992};
 993
 994/*
 995 * Check that the given mddev has no bitmap.
 996 *
 997 * This function is called from the run method of all personalities that do not
 998 * support bitmaps. It prints an error message and returns non-zero if mddev
 999 * has a bitmap. Otherwise, it returns 0.
1000 *
1001 */
1002int md_check_no_bitmap(struct mddev *mddev)
1003{
1004        if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
1005                return 0;
1006        printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1007                mdname(mddev), mddev->pers->name);
1008        return 1;
1009}
1010EXPORT_SYMBOL(md_check_no_bitmap);
1011
1012/*
1013 * load_super for 0.90.0 
1014 */
1015static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1016{
1017        char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1018        mdp_super_t *sb;
1019        int ret;
1020
1021        /*
1022         * Calculate the position of the superblock (512byte sectors),
1023         * it's at the end of the disk.
1024         *
1025         * It also happens to be a multiple of 4Kb.
1026         */
1027        rdev->sb_start = calc_dev_sboffset(rdev);
1028
1029        ret = read_disk_sb(rdev, MD_SB_BYTES);
1030        if (ret) return ret;
1031
1032        ret = -EINVAL;
1033
1034        bdevname(rdev->bdev, b);
1035        sb = page_address(rdev->sb_page);
1036
1037        if (sb->md_magic != MD_SB_MAGIC) {
1038                printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1039                       b);
1040                goto abort;
1041        }
1042
1043        if (sb->major_version != 0 ||
1044            sb->minor_version < 90 ||
1045            sb->minor_version > 91) {
1046                printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1047                        sb->major_version, sb->minor_version,
1048                        b);
1049                goto abort;
1050        }
1051
1052        if (sb->raid_disks <= 0)
1053                goto abort;
1054
1055        if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1056                printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1057                        b);
1058                goto abort;
1059        }
1060
1061        rdev->preferred_minor = sb->md_minor;
1062        rdev->data_offset = 0;
1063        rdev->new_data_offset = 0;
1064        rdev->sb_size = MD_SB_BYTES;
1065        rdev->badblocks.shift = -1;
1066
1067        if (sb->level == LEVEL_MULTIPATH)
1068                rdev->desc_nr = -1;
1069        else
1070                rdev->desc_nr = sb->this_disk.number;
1071
1072        if (!refdev) {
1073                ret = 1;
1074        } else {
1075                __u64 ev1, ev2;
1076                mdp_super_t *refsb = page_address(refdev->sb_page);
1077                if (!uuid_equal(refsb, sb)) {
1078                        printk(KERN_WARNING "md: %s has different UUID to %s\n",
1079                                b, bdevname(refdev->bdev,b2));
1080                        goto abort;
1081                }
1082                if (!sb_equal(refsb, sb)) {
1083                        printk(KERN_WARNING "md: %s has same UUID"
1084                               " but different superblock to %s\n",
1085                               b, bdevname(refdev->bdev, b2));
1086                        goto abort;
1087                }
1088                ev1 = md_event(sb);
1089                ev2 = md_event(refsb);
1090                if (ev1 > ev2)
1091                        ret = 1;
1092                else 
1093                        ret = 0;
1094        }
1095        rdev->sectors = rdev->sb_start;
1096        /* Limit to 4TB as metadata cannot record more than that.
1097         * (not needed for Linear and RAID0 as metadata doesn't
1098         * record this size)
1099         */
1100        if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1101                rdev->sectors = (2ULL << 32) - 2;
1102
1103        if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1104                /* "this cannot possibly happen" ... */
1105                ret = -EINVAL;
1106
1107 abort:
1108        return ret;
1109}
1110
1111/*
1112 * validate_super for 0.90.0
1113 */
1114static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1115{
1116        mdp_disk_t *desc;
1117        mdp_super_t *sb = page_address(rdev->sb_page);
1118        __u64 ev1 = md_event(sb);
1119
1120        rdev->raid_disk = -1;
1121        clear_bit(Faulty, &rdev->flags);
1122        clear_bit(In_sync, &rdev->flags);
1123        clear_bit(WriteMostly, &rdev->flags);
1124
1125        if (mddev->raid_disks == 0) {
1126                mddev->major_version = 0;
1127                mddev->minor_version = sb->minor_version;
1128                mddev->patch_version = sb->patch_version;
1129                mddev->external = 0;
1130                mddev->chunk_sectors = sb->chunk_size >> 9;
1131                mddev->ctime = sb->ctime;
1132                mddev->utime = sb->utime;
1133                mddev->level = sb->level;
1134                mddev->clevel[0] = 0;
1135                mddev->layout = sb->layout;
1136                mddev->raid_disks = sb->raid_disks;
1137                mddev->dev_sectors = ((sector_t)sb->size) * 2;
1138                mddev->events = ev1;
1139                mddev->bitmap_info.offset = 0;
1140                mddev->bitmap_info.space = 0;
1141                /* bitmap can use 60 K after the 4K superblocks */
1142                mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1143                mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1144                mddev->reshape_backwards = 0;
1145
1146                if (mddev->minor_version >= 91) {
1147                        mddev->reshape_position = sb->reshape_position;
1148                        mddev->delta_disks = sb->delta_disks;
1149                        mddev->new_level = sb->new_level;
1150                        mddev->new_layout = sb->new_layout;
1151                        mddev->new_chunk_sectors = sb->new_chunk >> 9;
1152                        if (mddev->delta_disks < 0)
1153                                mddev->reshape_backwards = 1;
1154                } else {
1155                        mddev->reshape_position = MaxSector;
1156                        mddev->delta_disks = 0;
1157                        mddev->new_level = mddev->level;
1158                        mddev->new_layout = mddev->layout;
1159                        mddev->new_chunk_sectors = mddev->chunk_sectors;
1160                }
1161
1162                if (sb->state & (1<<MD_SB_CLEAN))
1163                        mddev->recovery_cp = MaxSector;
1164                else {
1165                        if (sb->events_hi == sb->cp_events_hi && 
1166                                sb->events_lo == sb->cp_events_lo) {
1167                                mddev->recovery_cp = sb->recovery_cp;
1168                        } else
1169                                mddev->recovery_cp = 0;
1170                }
1171
1172                memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1173                memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1174                memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1175                memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1176
1177                mddev->max_disks = MD_SB_DISKS;
1178
1179                if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1180                    mddev->bitmap_info.file == NULL) {
1181                        mddev->bitmap_info.offset =
1182                                mddev->bitmap_info.default_offset;
1183                        mddev->bitmap_info.space =
1184                                mddev->bitmap_info.space;
1185                }
1186
1187        } else if (mddev->pers == NULL) {
1188                /* Insist on good event counter while assembling, except
1189                 * for spares (which don't need an event count) */
1190                ++ev1;
1191                if (sb->disks[rdev->desc_nr].state & (
1192                            (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1193                        if (ev1 < mddev->events) 
1194                                return -EINVAL;
1195        } else if (mddev->bitmap) {
1196                /* if adding to array with a bitmap, then we can accept an
1197                 * older device ... but not too old.
1198                 */
1199                if (ev1 < mddev->bitmap->events_cleared)
1200                        return 0;
1201        } else {
1202                if (ev1 < mddev->events)
1203                        /* just a hot-add of a new device, leave raid_disk at -1 */
1204                        return 0;
1205        }
1206
1207        if (mddev->level != LEVEL_MULTIPATH) {
1208                desc = sb->disks + rdev->desc_nr;
1209
1210                if (desc->state & (1<<MD_DISK_FAULTY))
1211                        set_bit(Faulty, &rdev->flags);
1212                else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1213                            desc->raid_disk < mddev->raid_disks */) {
1214                        set_bit(In_sync, &rdev->flags);
1215                        rdev->raid_disk = desc->raid_disk;
1216                } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1217                        /* active but not in sync implies recovery up to
1218                         * reshape position.  We don't know exactly where
1219                         * that is, so set to zero for now */
1220                        if (mddev->minor_version >= 91) {
1221                                rdev->recovery_offset = 0;
1222                                rdev->raid_disk = desc->raid_disk;
1223                        }
1224                }
1225                if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1226                        set_bit(WriteMostly, &rdev->flags);
1227        } else /* MULTIPATH are always insync */
1228                set_bit(In_sync, &rdev->flags);
1229        return 0;
1230}
1231
1232/*
1233 * sync_super for 0.90.0
1234 */
1235static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1236{
1237        mdp_super_t *sb;
1238        struct md_rdev *rdev2;
1239        int next_spare = mddev->raid_disks;
1240
1241
1242        /* make rdev->sb match mddev data..
1243         *
1244         * 1/ zero out disks
1245         * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1246         * 3/ any empty disks < next_spare become removed
1247         *
1248         * disks[0] gets initialised to REMOVED because
1249         * we cannot be sure from other fields if it has
1250         * been initialised or not.
1251         */
1252        int i;
1253        int active=0, working=0,failed=0,spare=0,nr_disks=0;
1254
1255        rdev->sb_size = MD_SB_BYTES;
1256
1257        sb = page_address(rdev->sb_page);
1258
1259        memset(sb, 0, sizeof(*sb));
1260
1261        sb->md_magic = MD_SB_MAGIC;
1262        sb->major_version = mddev->major_version;
1263        sb->patch_version = mddev->patch_version;
1264        sb->gvalid_words  = 0; /* ignored */
1265        memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1266        memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1267        memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1268        memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1269
1270        sb->ctime = mddev->ctime;
1271        sb->level = mddev->level;
1272        sb->size = mddev->dev_sectors / 2;
1273        sb->raid_disks = mddev->raid_disks;
1274        sb->md_minor = mddev->md_minor;
1275        sb->not_persistent = 0;
1276        sb->utime = mddev->utime;
1277        sb->state = 0;
1278        sb->events_hi = (mddev->events>>32);
1279        sb->events_lo = (u32)mddev->events;
1280
1281        if (mddev->reshape_position == MaxSector)
1282                sb->minor_version = 90;
1283        else {
1284                sb->minor_version = 91;
1285                sb->reshape_position = mddev->reshape_position;
1286                sb->new_level = mddev->new_level;
1287                sb->delta_disks = mddev->delta_disks;
1288                sb->new_layout = mddev->new_layout;
1289                sb->new_chunk = mddev->new_chunk_sectors << 9;
1290        }
1291        mddev->minor_version = sb->minor_version;
1292        if (mddev->in_sync)
1293        {
1294                sb->recovery_cp = mddev->recovery_cp;
1295                sb->cp_events_hi = (mddev->events>>32);
1296                sb->cp_events_lo = (u32)mddev->events;
1297                if (mddev->recovery_cp == MaxSector)
1298                        sb->state = (1<< MD_SB_CLEAN);
1299        } else
1300                sb->recovery_cp = 0;
1301
1302        sb->layout = mddev->layout;
1303        sb->chunk_size = mddev->chunk_sectors << 9;
1304
1305        if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1306                sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1307
1308        sb->disks[0].state = (1<<MD_DISK_REMOVED);
1309        rdev_for_each(rdev2, mddev) {
1310                mdp_disk_t *d;
1311                int desc_nr;
1312                int is_active = test_bit(In_sync, &rdev2->flags);
1313
1314                if (rdev2->raid_disk >= 0 &&
1315                    sb->minor_version >= 91)
1316                        /* we have nowhere to store the recovery_offset,
1317                         * but if it is not below the reshape_position,
1318                         * we can piggy-back on that.
1319                         */
1320                        is_active = 1;
1321                if (rdev2->raid_disk < 0 ||
1322                    test_bit(Faulty, &rdev2->flags))
1323                        is_active = 0;
1324                if (is_active)
1325                        desc_nr = rdev2->raid_disk;
1326                else
1327                        desc_nr = next_spare++;
1328                rdev2->desc_nr = desc_nr;
1329                d = &sb->disks[rdev2->desc_nr];
1330                nr_disks++;
1331                d->number = rdev2->desc_nr;
1332                d->major = MAJOR(rdev2->bdev->bd_dev);
1333                d->minor = MINOR(rdev2->bdev->bd_dev);
1334                if (is_active)
1335                        d->raid_disk = rdev2->raid_disk;
1336                else
1337                        d->raid_disk = rdev2->desc_nr; /* compatibility */
1338                if (test_bit(Faulty, &rdev2->flags))
1339                        d->state = (1<<MD_DISK_FAULTY);
1340                else if (is_active) {
1341                        d->state = (1<<MD_DISK_ACTIVE);
1342                        if (test_bit(In_sync, &rdev2->flags))
1343                                d->state |= (1<<MD_DISK_SYNC);
1344                        active++;
1345                        working++;
1346                } else {
1347                        d->state = 0;
1348                        spare++;
1349                        working++;
1350                }
1351                if (test_bit(WriteMostly, &rdev2->flags))
1352                        d->state |= (1<<MD_DISK_WRITEMOSTLY);
1353        }
1354        /* now set the "removed" and "faulty" bits on any missing devices */
1355        for (i=0 ; i < mddev->raid_disks ; i++) {
1356                mdp_disk_t *d = &sb->disks[i];
1357                if (d->state == 0 && d->number == 0) {
1358                        d->number = i;
1359                        d->raid_disk = i;
1360                        d->state = (1<<MD_DISK_REMOVED);
1361                        d->state |= (1<<MD_DISK_FAULTY);
1362                        failed++;
1363                }
1364        }
1365        sb->nr_disks = nr_disks;
1366        sb->active_disks = active;
1367        sb->working_disks = working;
1368        sb->failed_disks = failed;
1369        sb->spare_disks = spare;
1370
1371        sb->this_disk = sb->disks[rdev->desc_nr];
1372        sb->sb_csum = calc_sb_csum(sb);
1373}
1374
1375/*
1376 * rdev_size_change for 0.90.0
1377 */
1378static unsigned long long
1379super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1380{
1381        if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1382                return 0; /* component must fit device */
1383        if (rdev->mddev->bitmap_info.offset)
1384                return 0; /* can't move bitmap */
1385        rdev->sb_start = calc_dev_sboffset(rdev);
1386        if (!num_sectors || num_sectors > rdev->sb_start)
1387                num_sectors = rdev->sb_start;
1388        /* Limit to 4TB as metadata cannot record more than that.
1389         * 4TB == 2^32 KB, or 2*2^32 sectors.
1390         */
1391        if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1392                num_sectors = (2ULL << 32) - 2;
1393        md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1394                       rdev->sb_page);
1395        md_super_wait(rdev->mddev);
1396        return num_sectors;
1397}
1398
1399static int
1400super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1401{
1402        /* non-zero offset changes not possible with v0.90 */
1403        return new_offset == 0;
1404}
1405
1406/*
1407 * version 1 superblock
1408 */
1409
1410static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1411{
1412        __le32 disk_csum;
1413        u32 csum;
1414        unsigned long long newcsum;
1415        int size = 256 + le32_to_cpu(sb->max_dev)*2;
1416        __le32 *isuper = (__le32*)sb;
1417
1418        disk_csum = sb->sb_csum;
1419        sb->sb_csum = 0;
1420        newcsum = 0;
1421        for (; size >= 4; size -= 4)
1422                newcsum += le32_to_cpu(*isuper++);
1423
1424        if (size == 2)
1425                newcsum += le16_to_cpu(*(__le16*) isuper);
1426
1427        csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1428        sb->sb_csum = disk_csum;
1429        return cpu_to_le32(csum);
1430}
1431
1432static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1433                            int acknowledged);
1434static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1435{
1436        struct mdp_superblock_1 *sb;
1437        int ret;
1438        sector_t sb_start;
1439        sector_t sectors;
1440        char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1441        int bmask;
1442
1443        /*
1444         * Calculate the position of the superblock in 512byte sectors.
1445         * It is always aligned to a 4K boundary and
1446         * depeding on minor_version, it can be:
1447         * 0: At least 8K, but less than 12K, from end of device
1448         * 1: At start of device
1449         * 2: 4K from start of device.
1450         */
1451        switch(minor_version) {
1452        case 0:
1453                sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1454                sb_start -= 8*2;
1455                sb_start &= ~(sector_t)(4*2-1);
1456                break;
1457        case 1:
1458                sb_start = 0;
1459                break;
1460        case 2:
1461                sb_start = 8;
1462                break;
1463        default:
1464                return -EINVAL;
1465        }
1466        rdev->sb_start = sb_start;
1467
1468        /* superblock is rarely larger than 1K, but it can be larger,
1469         * and it is safe to read 4k, so we do that
1470         */
1471        ret = read_disk_sb(rdev, 4096);
1472        if (ret) return ret;
1473
1474
1475        sb = page_address(rdev->sb_page);
1476
1477        if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1478            sb->major_version != cpu_to_le32(1) ||
1479            le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1480            le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1481            (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1482                return -EINVAL;
1483
1484        if (calc_sb_1_csum(sb) != sb->sb_csum) {
1485                printk("md: invalid superblock checksum on %s\n",
1486                        bdevname(rdev->bdev,b));
1487                return -EINVAL;
1488        }
1489        if (le64_to_cpu(sb->data_size) < 10) {
1490                printk("md: data_size too small on %s\n",
1491                       bdevname(rdev->bdev,b));
1492                return -EINVAL;
1493        }
1494        if (sb->pad0 ||
1495            sb->pad3[0] ||
1496            memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1497                /* Some padding is non-zero, might be a new feature */
1498                return -EINVAL;
1499
1500        rdev->preferred_minor = 0xffff;
1501        rdev->data_offset = le64_to_cpu(sb->data_offset);
1502        rdev->new_data_offset = rdev->data_offset;
1503        if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1504            (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1505                rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1506        atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1507
1508        rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1509        bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1510        if (rdev->sb_size & bmask)
1511                rdev->sb_size = (rdev->sb_size | bmask) + 1;
1512
1513        if (minor_version
1514            && rdev->data_offset < sb_start + (rdev->sb_size/512))
1515                return -EINVAL;
1516        if (minor_version
1517            && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1518                return -EINVAL;
1519
1520        if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1521                rdev->desc_nr = -1;
1522        else
1523                rdev->desc_nr = le32_to_cpu(sb->dev_number);
1524
1525        if (!rdev->bb_page) {
1526                rdev->bb_page = alloc_page(GFP_KERNEL);
1527                if (!rdev->bb_page)
1528                        return -ENOMEM;
1529        }
1530        if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1531            rdev->badblocks.count == 0) {
1532                /* need to load the bad block list.
1533                 * Currently we limit it to one page.
1534                 */
1535                s32 offset;
1536                sector_t bb_sector;
1537                u64 *bbp;
1538                int i;
1539                int sectors = le16_to_cpu(sb->bblog_size);
1540                if (sectors > (PAGE_SIZE / 512))
1541                        return -EINVAL;
1542                offset = le32_to_cpu(sb->bblog_offset);
1543                if (offset == 0)
1544                        return -EINVAL;
1545                bb_sector = (long long)offset;
1546                if (!sync_page_io(rdev, bb_sector, sectors << 9,
1547                                  rdev->bb_page, READ, true))
1548                        return -EIO;
1549                bbp = (u64 *)page_address(rdev->bb_page);
1550                rdev->badblocks.shift = sb->bblog_shift;
1551                for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1552                        u64 bb = le64_to_cpu(*bbp);
1553                        int count = bb & (0x3ff);
1554                        u64 sector = bb >> 10;
1555                        sector <<= sb->bblog_shift;
1556                        count <<= sb->bblog_shift;
1557                        if (bb + 1 == 0)
1558                                break;
1559                        if (md_set_badblocks(&rdev->badblocks,
1560                                             sector, count, 1) == 0)
1561                                return -EINVAL;
1562                }
1563        } else if (sb->bblog_offset == 0)
1564                rdev->badblocks.shift = -1;
1565
1566        if (!refdev) {
1567                ret = 1;
1568        } else {
1569                __u64 ev1, ev2;
1570                struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1571
1572                if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1573                    sb->level != refsb->level ||
1574                    sb->layout != refsb->layout ||
1575                    sb->chunksize != refsb->chunksize) {
1576                        printk(KERN_WARNING "md: %s has strangely different"
1577                                " superblock to %s\n",
1578                                bdevname(rdev->bdev,b),
1579                                bdevname(refdev->bdev,b2));
1580                        return -EINVAL;
1581                }
1582                ev1 = le64_to_cpu(sb->events);
1583                ev2 = le64_to_cpu(refsb->events);
1584
1585                if (ev1 > ev2)
1586                        ret = 1;
1587                else
1588                        ret = 0;
1589        }
1590        if (minor_version) {
1591                sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1592                sectors -= rdev->data_offset;
1593        } else
1594                sectors = rdev->sb_start;
1595        if (sectors < le64_to_cpu(sb->data_size))
1596                return -EINVAL;
1597        rdev->sectors = le64_to_cpu(sb->data_size);
1598        return ret;
1599}
1600
1601static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1602{
1603        struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1604        __u64 ev1 = le64_to_cpu(sb->events);
1605
1606        rdev->raid_disk = -1;
1607        clear_bit(Faulty, &rdev->flags);
1608        clear_bit(In_sync, &rdev->flags);
1609        clear_bit(WriteMostly, &rdev->flags);
1610
1611        if (mddev->raid_disks == 0) {
1612                mddev->major_version = 1;
1613                mddev->patch_version = 0;
1614                mddev->external = 0;
1615                mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1616                mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1617                mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1618                mddev->level = le32_to_cpu(sb->level);
1619                mddev->clevel[0] = 0;
1620                mddev->layout = le32_to_cpu(sb->layout);
1621                mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1622                mddev->dev_sectors = le64_to_cpu(sb->size);
1623                mddev->events = ev1;
1624                mddev->bitmap_info.offset = 0;
1625                mddev->bitmap_info.space = 0;
1626                /* Default location for bitmap is 1K after superblock
1627                 * using 3K - total of 4K
1628                 */
1629                mddev->bitmap_info.default_offset = 1024 >> 9;
1630                mddev->bitmap_info.default_space = (4096-1024) >> 9;
1631                mddev->reshape_backwards = 0;
1632
1633                mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1634                memcpy(mddev->uuid, sb->set_uuid, 16);
1635
1636                mddev->max_disks =  (4096-256)/2;
1637
1638                if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1639                    mddev->bitmap_info.file == NULL) {
1640                        mddev->bitmap_info.offset =
1641                                (__s32)le32_to_cpu(sb->bitmap_offset);
1642                        /* Metadata doesn't record how much space is available.
1643                         * For 1.0, we assume we can use up to the superblock
1644                         * if before, else to 4K beyond superblock.
1645                         * For others, assume no change is possible.
1646                         */
1647                        if (mddev->minor_version > 0)
1648                                mddev->bitmap_info.space = 0;
1649                        else if (mddev->bitmap_info.offset > 0)
1650                                mddev->bitmap_info.space =
1651                                        8 - mddev->bitmap_info.offset;
1652                        else
1653                                mddev->bitmap_info.space =
1654                                        -mddev->bitmap_info.offset;
1655                }
1656
1657                if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1658                        mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1659                        mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1660                        mddev->new_level = le32_to_cpu(sb->new_level);
1661                        mddev->new_layout = le32_to_cpu(sb->new_layout);
1662                        mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1663                        if (mddev->delta_disks < 0 ||
1664                            (mddev->delta_disks == 0 &&
1665                             (le32_to_cpu(sb->feature_map)
1666                              & MD_FEATURE_RESHAPE_BACKWARDS)))
1667                                mddev->reshape_backwards = 1;
1668                } else {
1669                        mddev->reshape_position = MaxSector;
1670                        mddev->delta_disks = 0;
1671                        mddev->new_level = mddev->level;
1672                        mddev->new_layout = mddev->layout;
1673                        mddev->new_chunk_sectors = mddev->chunk_sectors;
1674                }
1675
1676        } else if (mddev->pers == NULL) {
1677                /* Insist of good event counter while assembling, except for
1678                 * spares (which don't need an event count) */
1679                ++ev1;
1680                if (rdev->desc_nr >= 0 &&
1681                    rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1682                    le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1683                        if (ev1 < mddev->events)
1684                                return -EINVAL;
1685        } else if (mddev->bitmap) {
1686                /* If adding to array with a bitmap, then we can accept an
1687                 * older device, but not too old.
1688                 */
1689                if (ev1 < mddev->bitmap->events_cleared)
1690                        return 0;
1691        } else {
1692                if (ev1 < mddev->events)
1693                        /* just a hot-add of a new device, leave raid_disk at -1 */
1694                        return 0;
1695        }
1696        if (mddev->level != LEVEL_MULTIPATH) {
1697                int role;
1698                if (rdev->desc_nr < 0 ||
1699                    rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1700                        role = 0xffff;
1701                        rdev->desc_nr = -1;
1702                } else
1703                        role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1704                switch(role) {
1705                case 0xffff: /* spare */
1706                        break;
1707                case 0xfffe: /* faulty */
1708                        set_bit(Faulty, &rdev->flags);
1709                        break;
1710                default:
1711                        if ((le32_to_cpu(sb->feature_map) &
1712                             MD_FEATURE_RECOVERY_OFFSET))
1713                                rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1714                        else
1715                                set_bit(In_sync, &rdev->flags);
1716                        rdev->raid_disk = role;
1717                        break;
1718                }
1719                if (sb->devflags & WriteMostly1)
1720                        set_bit(WriteMostly, &rdev->flags);
1721                if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1722                        set_bit(Replacement, &rdev->flags);
1723        } else /* MULTIPATH are always insync */
1724                set_bit(In_sync, &rdev->flags);
1725
1726        return 0;
1727}
1728
1729static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1730{
1731        struct mdp_superblock_1 *sb;
1732        struct md_rdev *rdev2;
1733        int max_dev, i;
1734        /* make rdev->sb match mddev and rdev data. */
1735
1736        sb = page_address(rdev->sb_page);
1737
1738        sb->feature_map = 0;
1739        sb->pad0 = 0;
1740        sb->recovery_offset = cpu_to_le64(0);
1741        memset(sb->pad3, 0, sizeof(sb->pad3));
1742
1743        sb->utime = cpu_to_le64((__u64)mddev->utime);
1744        sb->events = cpu_to_le64(mddev->events);
1745        if (mddev->in_sync)
1746                sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1747        else
1748                sb->resync_offset = cpu_to_le64(0);
1749
1750        sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1751
1752        sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1753        sb->size = cpu_to_le64(mddev->dev_sectors);
1754        sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1755        sb->level = cpu_to_le32(mddev->level);
1756        sb->layout = cpu_to_le32(mddev->layout);
1757
1758        if (test_bit(WriteMostly, &rdev->flags))
1759                sb->devflags |= WriteMostly1;
1760        else
1761                sb->devflags &= ~WriteMostly1;
1762        sb->data_offset = cpu_to_le64(rdev->data_offset);
1763        sb->data_size = cpu_to_le64(rdev->sectors);
1764
1765        if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1766                sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1767                sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1768        }
1769
1770        if (rdev->raid_disk >= 0 &&
1771            !test_bit(In_sync, &rdev->flags)) {
1772                sb->feature_map |=
1773                        cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1774                sb->recovery_offset =
1775                        cpu_to_le64(rdev->recovery_offset);
1776        }
1777        if (test_bit(Replacement, &rdev->flags))
1778                sb->feature_map |=
1779                        cpu_to_le32(MD_FEATURE_REPLACEMENT);
1780
1781        if (mddev->reshape_position != MaxSector) {
1782                sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1783                sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1784                sb->new_layout = cpu_to_le32(mddev->new_layout);
1785                sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1786                sb->new_level = cpu_to_le32(mddev->new_level);
1787                sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1788                if (mddev->delta_disks == 0 &&
1789                    mddev->reshape_backwards)
1790                        sb->feature_map
1791                                |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1792                if (rdev->new_data_offset != rdev->data_offset) {
1793                        sb->feature_map
1794                                |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1795                        sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1796                                                             - rdev->data_offset));
1797                }
1798        }
1799
1800        if (rdev->badblocks.count == 0)
1801                /* Nothing to do for bad blocks*/ ;
1802        else if (sb->bblog_offset == 0)
1803                /* Cannot record bad blocks on this device */
1804                md_error(mddev, rdev);
1805        else {
1806                struct badblocks *bb = &rdev->badblocks;
1807                u64 *bbp = (u64 *)page_address(rdev->bb_page);
1808                u64 *p = bb->page;
1809                sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1810                if (bb->changed) {
1811                        unsigned seq;
1812
1813retry:
1814                        seq = read_seqbegin(&bb->lock);
1815
1816                        memset(bbp, 0xff, PAGE_SIZE);
1817
1818                        for (i = 0 ; i < bb->count ; i++) {
1819                                u64 internal_bb = p[i];
1820                                u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1821                                                | BB_LEN(internal_bb));
1822                                bbp[i] = cpu_to_le64(store_bb);
1823                        }
1824                        bb->changed = 0;
1825                        if (read_seqretry(&bb->lock, seq))
1826                                goto retry;
1827
1828                        bb->sector = (rdev->sb_start +
1829                                      (int)le32_to_cpu(sb->bblog_offset));
1830                        bb->size = le16_to_cpu(sb->bblog_size);
1831                }
1832        }
1833
1834        max_dev = 0;
1835        rdev_for_each(rdev2, mddev)
1836                if (rdev2->desc_nr+1 > max_dev)
1837                        max_dev = rdev2->desc_nr+1;
1838
1839        if (max_dev > le32_to_cpu(sb->max_dev)) {
1840                int bmask;
1841                sb->max_dev = cpu_to_le32(max_dev);
1842                rdev->sb_size = max_dev * 2 + 256;
1843                bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1844                if (rdev->sb_size & bmask)
1845                        rdev->sb_size = (rdev->sb_size | bmask) + 1;
1846        } else
1847                max_dev = le32_to_cpu(sb->max_dev);
1848
1849        for (i=0; i<max_dev;i++)
1850                sb->dev_roles[i] = cpu_to_le16(0xfffe);
1851        
1852        rdev_for_each(rdev2, mddev) {
1853                i = rdev2->desc_nr;
1854                if (test_bit(Faulty, &rdev2->flags))
1855                        sb->dev_roles[i] = cpu_to_le16(0xfffe);
1856                else if (test_bit(In_sync, &rdev2->flags))
1857                        sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1858                else if (rdev2->raid_disk >= 0)
1859                        sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1860                else
1861                        sb->dev_roles[i] = cpu_to_le16(0xffff);
1862        }
1863
1864        sb->sb_csum = calc_sb_1_csum(sb);
1865}
1866
1867static unsigned long long
1868super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1869{
1870        struct mdp_superblock_1 *sb;
1871        sector_t max_sectors;
1872        if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1873                return 0; /* component must fit device */
1874        if (rdev->data_offset != rdev->new_data_offset)
1875                return 0; /* too confusing */
1876        if (rdev->sb_start < rdev->data_offset) {
1877                /* minor versions 1 and 2; superblock before data */
1878                max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1879                max_sectors -= rdev->data_offset;
1880                if (!num_sectors || num_sectors > max_sectors)
1881                        num_sectors = max_sectors;
1882        } else if (rdev->mddev->bitmap_info.offset) {
1883                /* minor version 0 with bitmap we can't move */
1884                return 0;
1885        } else {
1886                /* minor version 0; superblock after data */
1887                sector_t sb_start;
1888                sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1889                sb_start &= ~(sector_t)(4*2 - 1);
1890                max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1891                if (!num_sectors || num_sectors > max_sectors)
1892                        num_sectors = max_sectors;
1893                rdev->sb_start = sb_start;
1894        }
1895        sb = page_address(rdev->sb_page);
1896        sb->data_size = cpu_to_le64(num_sectors);
1897        sb->super_offset = rdev->sb_start;
1898        sb->sb_csum = calc_sb_1_csum(sb);
1899        md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1900                       rdev->sb_page);
1901        md_super_wait(rdev->mddev);
1902        return num_sectors;
1903
1904}
1905
1906static int
1907super_1_allow_new_offset(struct md_rdev *rdev,
1908                         unsigned long long new_offset)
1909{
1910        /* All necessary checks on new >= old have been done */
1911        struct bitmap *bitmap;
1912        if (new_offset >= rdev->data_offset)
1913                return 1;
1914
1915        /* with 1.0 metadata, there is no metadata to tread on
1916         * so we can always move back */
1917        if (rdev->mddev->minor_version == 0)
1918                return 1;
1919
1920        /* otherwise we must be sure not to step on
1921         * any metadata, so stay:
1922         * 36K beyond start of superblock
1923         * beyond end of badblocks
1924         * beyond write-intent bitmap
1925         */
1926        if (rdev->sb_start + (32+4)*2 > new_offset)
1927                return 0;
1928        bitmap = rdev->mddev->bitmap;
1929        if (bitmap && !rdev->mddev->bitmap_info.file &&
1930            rdev->sb_start + rdev->mddev->bitmap_info.offset +
1931            bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1932                return 0;
1933        if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1934                return 0;
1935
1936        return 1;
1937}
1938
1939static struct super_type super_types[] = {
1940        [0] = {
1941                .name   = "0.90.0",
1942                .owner  = THIS_MODULE,
1943                .load_super         = super_90_load,
1944                .validate_super     = super_90_validate,
1945                .sync_super         = super_90_sync,
1946                .rdev_size_change   = super_90_rdev_size_change,
1947                .allow_new_offset   = super_90_allow_new_offset,
1948        },
1949        [1] = {
1950                .name   = "md-1",
1951                .owner  = THIS_MODULE,
1952                .load_super         = super_1_load,
1953                .validate_super     = super_1_validate,
1954                .sync_super         = super_1_sync,
1955                .rdev_size_change   = super_1_rdev_size_change,
1956                .allow_new_offset   = super_1_allow_new_offset,
1957        },
1958};
1959
1960static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1961{
1962        if (mddev->sync_super) {
1963                mddev->sync_super(mddev, rdev);
1964                return;
1965        }
1966
1967        BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1968
1969        super_types[mddev->major_version].sync_super(mddev, rdev);
1970}
1971
1972static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1973{
1974        struct md_rdev *rdev, *rdev2;
1975
1976        rcu_read_lock();
1977        rdev_for_each_rcu(rdev, mddev1)
1978                rdev_for_each_rcu(rdev2, mddev2)
1979                        if (rdev->bdev->bd_contains ==
1980                            rdev2->bdev->bd_contains) {
1981                                rcu_read_unlock();
1982                                return 1;
1983                        }
1984        rcu_read_unlock();
1985        return 0;
1986}
1987
1988static LIST_HEAD(pending_raid_disks);
1989
1990/*
1991 * Try to register data integrity profile for an mddev
1992 *
1993 * This is called when an array is started and after a disk has been kicked
1994 * from the array. It only succeeds if all working and active component devices
1995 * are integrity capable with matching profiles.
1996 */
1997int md_integrity_register(struct mddev *mddev)
1998{
1999        struct md_rdev *rdev, *reference = NULL;
2000
2001        if (list_empty(&mddev->disks))
2002                return 0; /* nothing to do */
2003        if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2004                return 0; /* shouldn't register, or already is */
2005        rdev_for_each(rdev, mddev) {
2006                /* skip spares and non-functional disks */
2007                if (test_bit(Faulty, &rdev->flags))
2008                        continue;
2009                if (rdev->raid_disk < 0)
2010                        continue;
2011                if (!reference) {
2012                        /* Use the first rdev as the reference */
2013                        reference = rdev;
2014                        continue;
2015                }
2016                /* does this rdev's profile match the reference profile? */
2017                if (blk_integrity_compare(reference->bdev->bd_disk,
2018                                rdev->bdev->bd_disk) < 0)
2019                        return -EINVAL;
2020        }
2021        if (!reference || !bdev_get_integrity(reference->bdev))
2022                return 0;
2023        /*
2024         * All component devices are integrity capable and have matching
2025         * profiles, register the common profile for the md device.
2026         */
2027        if (blk_integrity_register(mddev->gendisk,
2028                        bdev_get_integrity(reference->bdev)) != 0) {
2029                printk(KERN_ERR "md: failed to register integrity for %s\n",
2030                        mdname(mddev));
2031                return -EINVAL;
2032        }
2033        printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2034        if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2035                printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2036                       mdname(mddev));
2037                return -EINVAL;
2038        }
2039        return 0;
2040}
2041EXPORT_SYMBOL(md_integrity_register);
2042
2043/* Disable data integrity if non-capable/non-matching disk is being added */
2044void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2045{
2046        struct blk_integrity *bi_rdev;
2047        struct blk_integrity *bi_mddev;
2048
2049        if (!mddev->gendisk)
2050                return;
2051
2052        bi_rdev = bdev_get_integrity(rdev->bdev);
2053        bi_mddev = blk_get_integrity(mddev->gendisk);
2054
2055        if (!bi_mddev) /* nothing to do */
2056                return;
2057        if (rdev->raid_disk < 0) /* skip spares */
2058                return;
2059        if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2060                                             rdev->bdev->bd_disk) >= 0)
2061                return;
2062        printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2063        blk_integrity_unregister(mddev->gendisk);
2064}
2065EXPORT_SYMBOL(md_integrity_add_rdev);
2066
2067static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
2068{
2069        char b[BDEVNAME_SIZE];
2070        struct kobject *ko;
2071        char *s;
2072        int err;
2073
2074        if (rdev->mddev) {
2075                MD_BUG();
2076                return -EINVAL;
2077        }
2078
2079        /* prevent duplicates */
2080        if (find_rdev(mddev, rdev->bdev->bd_dev))
2081                return -EEXIST;
2082
2083        /* make sure rdev->sectors exceeds mddev->dev_sectors */
2084        if (rdev->sectors && (mddev->dev_sectors == 0 ||
2085                        rdev->sectors < mddev->dev_sectors)) {
2086                if (mddev->pers) {
2087                        /* Cannot change size, so fail
2088                         * If mddev->level <= 0, then we don't care
2089                         * about aligning sizes (e.g. linear)
2090                         */
2091                        if (mddev->level > 0)
2092                                return -ENOSPC;
2093                } else
2094                        mddev->dev_sectors = rdev->sectors;
2095        }
2096
2097        /* Verify rdev->desc_nr is unique.
2098         * If it is -1, assign a free number, else
2099         * check number is not in use
2100         */
2101        if (rdev->desc_nr < 0) {
2102                int choice = 0;
2103                if (mddev->pers) choice = mddev->raid_disks;
2104                while (find_rdev_nr(mddev, choice))
2105                        choice++;
2106                rdev->desc_nr = choice;
2107        } else {
2108                if (find_rdev_nr(mddev, rdev->desc_nr))
2109                        return -EBUSY;
2110        }
2111        if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2112                printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2113                       mdname(mddev), mddev->max_disks);
2114                return -EBUSY;
2115        }
2116        bdevname(rdev->bdev,b);
2117        while ( (s=strchr(b, '/')) != NULL)
2118                *s = '!';
2119
2120        rdev->mddev = mddev;
2121        printk(KERN_INFO "md: bind<%s>\n", b);
2122
2123        if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2124                goto fail;
2125
2126        ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2127        if (sysfs_create_link(&rdev->kobj, ko, "block"))
2128                /* failure here is OK */;
2129        rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2130
2131        list_add_rcu(&rdev->same_set, &mddev->disks);
2132        bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2133
2134        /* May as well allow recovery to be retried once */
2135        mddev->recovery_disabled++;
2136
2137        return 0;
2138
2139 fail:
2140        printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2141               b, mdname(mddev));
2142        return err;
2143}
2144
2145static void md_delayed_delete(struct work_struct *ws)
2146{
2147        struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2148        kobject_del(&rdev->kobj);
2149        kobject_put(&rdev->kobj);
2150}
2151
2152static void unbind_rdev_from_array(struct md_rdev * rdev)
2153{
2154        char b[BDEVNAME_SIZE];
2155        if (!rdev->mddev) {
2156                MD_BUG();
2157                return;
2158        }
2159        bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2160        list_del_rcu(&rdev->same_set);
2161        printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2162        rdev->mddev = NULL;
2163        sysfs_remove_link(&rdev->kobj, "block");
2164        sysfs_put(rdev->sysfs_state);
2165        rdev->sysfs_state = NULL;
2166        rdev->badblocks.count = 0;
2167        /* We need to delay this, otherwise we can deadlock when
2168         * writing to 'remove' to "dev/state".  We also need
2169         * to delay it due to rcu usage.
2170         */
2171        synchronize_rcu();
2172        INIT_WORK(&rdev->del_work, md_delayed_delete);
2173        kobject_get(&rdev->kobj);
2174        queue_work(md_misc_wq, &rdev->del_work);
2175}
2176
2177/*
2178 * prevent the device from being mounted, repartitioned or
2179 * otherwise reused by a RAID array (or any other kernel
2180 * subsystem), by bd_claiming the device.
2181 */
2182static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2183{
2184        int err = 0;
2185        struct block_device *bdev;
2186        char b[BDEVNAME_SIZE];
2187
2188        bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2189                                 shared ? (struct md_rdev *)lock_rdev : rdev);
2190        if (IS_ERR(bdev)) {
2191                printk(KERN_ERR "md: could not open %s.\n",
2192                        __bdevname(dev, b));
2193                return PTR_ERR(bdev);
2194        }
2195        rdev->bdev = bdev;
2196        return err;
2197}
2198
2199static void unlock_rdev(struct md_rdev *rdev)
2200{
2201        struct block_device *bdev = rdev->bdev;
2202        rdev->bdev = NULL;
2203        if (!bdev)
2204                MD_BUG();
2205        blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2206}
2207
2208void md_autodetect_dev(dev_t dev);
2209
2210static void export_rdev(struct md_rdev * rdev)
2211{
2212        char b[BDEVNAME_SIZE];
2213        printk(KERN_INFO "md: export_rdev(%s)\n",
2214                bdevname(rdev->bdev,b));
2215        if (rdev->mddev)
2216                MD_BUG();
2217        md_rdev_clear(rdev);
2218#ifndef MODULE
2219        if (test_bit(AutoDetected, &rdev->flags))
2220                md_autodetect_dev(rdev->bdev->bd_dev);
2221#endif
2222        unlock_rdev(rdev);
2223        kobject_put(&rdev->kobj);
2224}
2225
2226static void kick_rdev_from_array(struct md_rdev * rdev)
2227{
2228        unbind_rdev_from_array(rdev);
2229        export_rdev(rdev);
2230}
2231
2232static void export_array(struct mddev *mddev)
2233{
2234        struct md_rdev *rdev, *tmp;
2235
2236        rdev_for_each_safe(rdev, tmp, mddev) {
2237                if (!rdev->mddev) {
2238                        MD_BUG();
2239                        continue;
2240                }
2241                kick_rdev_from_array(rdev);
2242        }
2243        if (!list_empty(&mddev->disks))
2244                MD_BUG();
2245        mddev->raid_disks = 0;
2246        mddev->major_version = 0;
2247}
2248
2249static void print_desc(mdp_disk_t *desc)
2250{
2251        printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2252                desc->major,desc->minor,desc->raid_disk,desc->state);
2253}
2254
2255static void print_sb_90(mdp_super_t *sb)
2256{
2257        int i;
2258
2259        printk(KERN_INFO 
2260                "md:  SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2261                sb->major_version, sb->minor_version, sb->patch_version,
2262                sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2263                sb->ctime);
2264        printk(KERN_INFO "md:     L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2265                sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2266                sb->md_minor, sb->layout, sb->chunk_size);
2267        printk(KERN_INFO "md:     UT:%08x ST:%d AD:%d WD:%d"
2268                " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2269                sb->utime, sb->state, sb->active_disks, sb->working_disks,
2270                sb->failed_disks, sb->spare_disks,
2271                sb->sb_csum, (unsigned long)sb->events_lo);
2272
2273        printk(KERN_INFO);
2274        for (i = 0; i < MD_SB_DISKS; i++) {
2275                mdp_disk_t *desc;
2276
2277                desc = sb->disks + i;
2278                if (desc->number || desc->major || desc->minor ||
2279                    desc->raid_disk || (desc->state && (desc->state != 4))) {
2280                        printk("     D %2d: ", i);
2281                        print_desc(desc);
2282                }
2283        }
2284        printk(KERN_INFO "md:     THIS: ");
2285        print_desc(&sb->this_disk);
2286}
2287
2288static void print_sb_1(struct mdp_superblock_1 *sb)
2289{
2290        __u8 *uuid;
2291
2292        uuid = sb->set_uuid;
2293        printk(KERN_INFO
2294               "md:  SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2295               "md:    Name: \"%s\" CT:%llu\n",
2296                le32_to_cpu(sb->major_version),
2297                le32_to_cpu(sb->feature_map),
2298                uuid,
2299                sb->set_name,
2300                (unsigned long long)le64_to_cpu(sb->ctime)
2301                       & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2302
2303        uuid = sb->device_uuid;
2304        printk(KERN_INFO
2305               "md:       L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2306                        " RO:%llu\n"
2307               "md:     Dev:%08x UUID: %pU\n"
2308               "md:       (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2309               "md:         (MaxDev:%u) \n",
2310                le32_to_cpu(sb->level),
2311                (unsigned long long)le64_to_cpu(sb->size),
2312                le32_to_cpu(sb->raid_disks),
2313                le32_to_cpu(sb->layout),
2314                le32_to_cpu(sb->chunksize),
2315                (unsigned long long)le64_to_cpu(sb->data_offset),
2316                (unsigned long long)le64_to_cpu(sb->data_size),
2317                (unsigned long long)le64_to_cpu(sb->super_offset),
2318                (unsigned long long)le64_to_cpu(sb->recovery_offset),
2319                le32_to_cpu(sb->dev_number),
2320                uuid,
2321                sb->devflags,
2322                (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2323                (unsigned long long)le64_to_cpu(sb->events),
2324                (unsigned long long)le64_to_cpu(sb->resync_offset),
2325                le32_to_cpu(sb->sb_csum),
2326                le32_to_cpu(sb->max_dev)
2327                );
2328}
2329
2330static void print_rdev(struct md_rdev *rdev, int major_version)
2331{
2332        char b[BDEVNAME_SIZE];
2333        printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2334                bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2335                test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2336                rdev->desc_nr);
2337        if (rdev->sb_loaded) {
2338                printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2339                switch (major_version) {
2340                case 0:
2341                        print_sb_90(page_address(rdev->sb_page));
2342                        break;
2343                case 1:
2344                        print_sb_1(page_address(rdev->sb_page));
2345                        break;
2346                }
2347        } else
2348                printk(KERN_INFO "md: no rdev superblock!\n");
2349}
2350
2351static void md_print_devices(void)
2352{
2353        struct list_head *tmp;
2354        struct md_rdev *rdev;
2355        struct mddev *mddev;
2356        char b[BDEVNAME_SIZE];
2357
2358        printk("\n");
2359        printk("md:     **********************************\n");
2360        printk("md:     * <COMPLETE RAID STATE PRINTOUT> *\n");
2361        printk("md:     **********************************\n");
2362        for_each_mddev(mddev, tmp) {
2363
2364                if (mddev->bitmap)
2365                        bitmap_print_sb(mddev->bitmap);
2366                else
2367                        printk("%s: ", mdname(mddev));
2368                rdev_for_each(rdev, mddev)
2369                        printk("<%s>", bdevname(rdev->bdev,b));
2370                printk("\n");
2371
2372                rdev_for_each(rdev, mddev)
2373                        print_rdev(rdev, mddev->major_version);
2374        }
2375        printk("md:     **********************************\n");
2376        printk("\n");
2377}
2378
2379
2380static void sync_sbs(struct mddev * mddev, int nospares)
2381{
2382        /* Update each superblock (in-memory image), but
2383         * if we are allowed to, skip spares which already
2384         * have the right event counter, or have one earlier
2385         * (which would mean they aren't being marked as dirty
2386         * with the rest of the array)
2387         */
2388        struct md_rdev *rdev;
2389        rdev_for_each(rdev, mddev) {
2390                if (rdev->sb_events == mddev->events ||
2391                    (nospares &&
2392                     rdev->raid_disk < 0 &&
2393                     rdev->sb_events+1 == mddev->events)) {
2394                        /* Don't update this superblock */
2395                        rdev->sb_loaded = 2;
2396                } else {
2397                        sync_super(mddev, rdev);
2398                        rdev->sb_loaded = 1;
2399                }
2400        }
2401}
2402
2403static void md_update_sb(struct mddev * mddev, int force_change)
2404{
2405        struct md_rdev *rdev;
2406        int sync_req;
2407        int nospares = 0;
2408        int any_badblocks_changed = 0;
2409
2410repeat:
2411        /* First make sure individual recovery_offsets are correct */
2412        rdev_for_each(rdev, mddev) {
2413                if (rdev->raid_disk >= 0 &&
2414                    mddev->delta_disks >= 0 &&
2415                    !test_bit(In_sync, &rdev->flags) &&
2416                    mddev->curr_resync_completed > rdev->recovery_offset)
2417                                rdev->recovery_offset = mddev->curr_resync_completed;
2418
2419        }       
2420        if (!mddev->persistent) {
2421                clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2422                clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2423                if (!mddev->external) {
2424                        clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2425                        rdev_for_each(rdev, mddev) {
2426                                if (rdev->badblocks.changed) {
2427                                        rdev->badblocks.changed = 0;
2428                                        md_ack_all_badblocks(&rdev->badblocks);
2429                                        md_error(mddev, rdev);
2430                                }
2431                                clear_bit(Blocked, &rdev->flags);
2432                                clear_bit(BlockedBadBlocks, &rdev->flags);
2433                                wake_up(&rdev->blocked_wait);
2434                        }
2435                }
2436                wake_up(&mddev->sb_wait);
2437                return;
2438        }
2439
2440        spin_lock_irq(&mddev->write_lock);
2441
2442        mddev->utime = get_seconds();
2443
2444        if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2445                force_change = 1;
2446        if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2447                /* just a clean<-> dirty transition, possibly leave spares alone,
2448                 * though if events isn't the right even/odd, we will have to do
2449                 * spares after all
2450                 */
2451                nospares = 1;
2452        if (force_change)
2453                nospares = 0;
2454        if (mddev->degraded)
2455                /* If the array is degraded, then skipping spares is both
2456                 * dangerous and fairly pointless.
2457                 * Dangerous because a device that was removed from the array
2458                 * might have a event_count that still looks up-to-date,
2459                 * so it can be re-added without a resync.
2460                 * Pointless because if there are any spares to skip,
2461                 * then a recovery will happen and soon that array won't
2462                 * be degraded any more and the spare can go back to sleep then.
2463                 */
2464                nospares = 0;
2465
2466        sync_req = mddev->in_sync;
2467
2468        /* If this is just a dirty<->clean transition, and the array is clean
2469         * and 'events' is odd, we can roll back to the previous clean state */
2470        if (nospares
2471            && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2472            && mddev->can_decrease_events
2473            && mddev->events != 1) {
2474                mddev->events--;
2475                mddev->can_decrease_events = 0;
2476        } else {
2477                /* otherwise we have to go forward and ... */
2478                mddev->events ++;
2479                mddev->can_decrease_events = nospares;
2480        }
2481
2482        if (!mddev->events) {
2483                /*
2484                 * oops, this 64-bit counter should never wrap.
2485                 * Either we are in around ~1 trillion A.C., assuming
2486                 * 1 reboot per second, or we have a bug:
2487                 */
2488                MD_BUG();
2489                mddev->events --;
2490        }
2491
2492        rdev_for_each(rdev, mddev) {
2493                if (rdev->badblocks.changed)
2494                        any_badblocks_changed++;
2495                if (test_bit(Faulty, &rdev->flags))
2496                        set_bit(FaultRecorded, &rdev->flags);
2497        }
2498
2499        sync_sbs(mddev, nospares);
2500        spin_unlock_irq(&mddev->write_lock);
2501
2502        pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2503                 mdname(mddev), mddev->in_sync);
2504
2505        bitmap_update_sb(mddev->bitmap);
2506        rdev_for_each(rdev, mddev) {
2507                char b[BDEVNAME_SIZE];
2508
2509                if (rdev->sb_loaded != 1)
2510                        continue; /* no noise on spare devices */
2511
2512                if (!test_bit(Faulty, &rdev->flags) &&
2513                    rdev->saved_raid_disk == -1) {
2514                        md_super_write(mddev,rdev,
2515                                       rdev->sb_start, rdev->sb_size,
2516                                       rdev->sb_page);
2517                        pr_debug("md: (write) %s's sb offset: %llu\n",
2518                                 bdevname(rdev->bdev, b),
2519                                 (unsigned long long)rdev->sb_start);
2520                        rdev->sb_events = mddev->events;
2521                        if (rdev->badblocks.size) {
2522                                md_super_write(mddev, rdev,
2523                                               rdev->badblocks.sector,
2524                                               rdev->badblocks.size << 9,
2525                                               rdev->bb_page);
2526                                rdev->badblocks.size = 0;
2527                        }
2528
2529                } else if (test_bit(Faulty, &rdev->flags))
2530                        pr_debug("md: %s (skipping faulty)\n",
2531                                 bdevname(rdev->bdev, b));
2532                else
2533                        pr_debug("(skipping incremental s/r ");
2534
2535                if (mddev->level == LEVEL_MULTIPATH)
2536                        /* only need to write one superblock... */
2537                        break;
2538        }
2539        md_super_wait(mddev);
2540        /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2541
2542        spin_lock_irq(&mddev->write_lock);
2543        if (mddev->in_sync != sync_req ||
2544            test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2545                /* have to write it out again */
2546                spin_unlock_irq(&mddev->write_lock);
2547                goto repeat;
2548        }
2549        clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2550        spin_unlock_irq(&mddev->write_lock);
2551        wake_up(&mddev->sb_wait);
2552        if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2553                sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2554
2555        rdev_for_each(rdev, mddev) {
2556                if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2557                        clear_bit(Blocked, &rdev->flags);
2558
2559                if (any_badblocks_changed)
2560                        md_ack_all_badblocks(&rdev->badblocks);
2561                clear_bit(BlockedBadBlocks, &rdev->flags);
2562                wake_up(&rdev->blocked_wait);
2563        }
2564}
2565
2566/* words written to sysfs files may, or may not, be \n terminated.
2567 * We want to accept with case. For this we use cmd_match.
2568 */
2569static int cmd_match(const char *cmd, const char *str)
2570{
2571        /* See if cmd, written into a sysfs file, matches
2572         * str.  They must either be the same, or cmd can
2573         * have a trailing newline
2574         */
2575        while (*cmd && *str && *cmd == *str) {
2576                cmd++;
2577                str++;
2578        }
2579        if (*cmd == '\n')
2580                cmd++;
2581        if (*str || *cmd)
2582                return 0;
2583        return 1;
2584}
2585
2586struct rdev_sysfs_entry {
2587        struct attribute attr;
2588        ssize_t (*show)(struct md_rdev *, char *);
2589        ssize_t (*store)(struct md_rdev *, const char *, size_t);
2590};
2591
2592static ssize_t
2593state_show(struct md_rdev *rdev, char *page)
2594{
2595        char *sep = "";
2596        size_t len = 0;
2597
2598        if (test_bit(Faulty, &rdev->flags) ||
2599            rdev->badblocks.unacked_exist) {
2600                len+= sprintf(page+len, "%sfaulty",sep);
2601                sep = ",";
2602        }
2603        if (test_bit(In_sync, &rdev->flags)) {
2604                len += sprintf(page+len, "%sin_sync",sep);
2605                sep = ",";
2606        }
2607        if (test_bit(WriteMostly, &rdev->flags)) {
2608                len += sprintf(page+len, "%swrite_mostly",sep);
2609                sep = ",";
2610        }
2611        if (test_bit(Blocked, &rdev->flags) ||
2612            (rdev->badblocks.unacked_exist
2613             && !test_bit(Faulty, &rdev->flags))) {
2614                len += sprintf(page+len, "%sblocked", sep);
2615                sep = ",";
2616        }
2617        if (!test_bit(Faulty, &rdev->flags) &&
2618            !test_bit(In_sync, &rdev->flags)) {
2619                len += sprintf(page+len, "%sspare", sep);
2620                sep = ",";
2621        }
2622        if (test_bit(WriteErrorSeen, &rdev->flags)) {
2623                len += sprintf(page+len, "%swrite_error", sep);
2624                sep = ",";
2625        }
2626        if (test_bit(WantReplacement, &rdev->flags)) {
2627                len += sprintf(page+len, "%swant_replacement", sep);
2628                sep = ",";
2629        }
2630        if (test_bit(Replacement, &rdev->flags)) {
2631                len += sprintf(page+len, "%sreplacement", sep);
2632                sep = ",";
2633        }
2634
2635        return len+sprintf(page+len, "\n");
2636}
2637
2638static ssize_t
2639state_store(struct md_rdev *rdev, const char *buf, size_t len)
2640{
2641        /* can write
2642         *  faulty  - simulates an error
2643         *  remove  - disconnects the device
2644         *  writemostly - sets write_mostly
2645         *  -writemostly - clears write_mostly
2646         *  blocked - sets the Blocked flags
2647         *  -blocked - clears the Blocked and possibly simulates an error
2648         *  insync - sets Insync providing device isn't active
2649         *  write_error - sets WriteErrorSeen
2650         *  -write_error - clears WriteErrorSeen
2651         */
2652        int err = -EINVAL;
2653        if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2654                md_error(rdev->mddev, rdev);
2655                if (test_bit(Faulty, &rdev->flags))
2656                        err = 0;
2657                else
2658                        err = -EBUSY;
2659        } else if (cmd_match(buf, "remove")) {
2660                if (rdev->raid_disk >= 0)
2661                        err = -EBUSY;
2662                else {
2663                        struct mddev *mddev = rdev->mddev;
2664                        kick_rdev_from_array(rdev);
2665                        if (mddev->pers)
2666                                md_update_sb(mddev, 1);
2667                        md_new_event(mddev);
2668                        err = 0;
2669                }
2670        } else if (cmd_match(buf, "writemostly")) {
2671                set_bit(WriteMostly, &rdev->flags);
2672                err = 0;
2673        } else if (cmd_match(buf, "-writemostly")) {
2674                clear_bit(WriteMostly, &rdev->flags);
2675                err = 0;
2676        } else if (cmd_match(buf, "blocked")) {
2677                set_bit(Blocked, &rdev->flags);
2678                err = 0;
2679        } else if (cmd_match(buf, "-blocked")) {
2680                if (!test_bit(Faulty, &rdev->flags) &&
2681                    rdev->badblocks.unacked_exist) {
2682                        /* metadata handler doesn't understand badblocks,
2683                         * so we need to fail the device
2684                         */
2685                        md_error(rdev->mddev, rdev);
2686                }
2687                clear_bit(Blocked, &rdev->flags);
2688                clear_bit(BlockedBadBlocks, &rdev->flags);
2689                wake_up(&rdev->blocked_wait);
2690                set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2691                md_wakeup_thread(rdev->mddev->thread);
2692
2693                err = 0;
2694        } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2695                set_bit(In_sync, &rdev->flags);
2696                err = 0;
2697        } else if (cmd_match(buf, "write_error")) {
2698                set_bit(WriteErrorSeen, &rdev->flags);
2699                err = 0;
2700        } else if (cmd_match(buf, "-write_error")) {
2701                clear_bit(WriteErrorSeen, &rdev->flags);
2702                err = 0;
2703        } else if (cmd_match(buf, "want_replacement")) {
2704                /* Any non-spare device that is not a replacement can
2705                 * become want_replacement at any time, but we then need to
2706                 * check if recovery is needed.
2707                 */
2708                if (rdev->raid_disk >= 0 &&
2709                    !test_bit(Replacement, &rdev->flags))
2710                        set_bit(WantReplacement, &rdev->flags);
2711                set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2712                md_wakeup_thread(rdev->mddev->thread);
2713                err = 0;
2714        } else if (cmd_match(buf, "-want_replacement")) {
2715                /* Clearing 'want_replacement' is always allowed.
2716                 * Once replacements starts it is too late though.
2717                 */
2718                err = 0;
2719                clear_bit(WantReplacement, &rdev->flags);
2720        } else if (cmd_match(buf, "replacement")) {
2721                /* Can only set a device as a replacement when array has not
2722                 * yet been started.  Once running, replacement is automatic
2723                 * from spares, or by assigning 'slot'.
2724                 */
2725                if (rdev->mddev->pers)
2726                        err = -EBUSY;
2727                else {
2728                        set_bit(Replacement, &rdev->flags);
2729                        err = 0;
2730                }
2731        } else if (cmd_match(buf, "-replacement")) {
2732                /* Similarly, can only clear Replacement before start */
2733                if (rdev->mddev->pers)
2734                        err = -EBUSY;
2735                else {
2736                        clear_bit(Replacement, &rdev->flags);
2737                        err = 0;
2738                }
2739        }
2740        if (!err)
2741                sysfs_notify_dirent_safe(rdev->sysfs_state);
2742        return err ? err : len;
2743}
2744static struct rdev_sysfs_entry rdev_state =
2745__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2746
2747static ssize_t
2748errors_show(struct md_rdev *rdev, char *page)
2749{
2750        return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2751}
2752
2753static ssize_t
2754errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2755{
2756        char *e;
2757        unsigned long n = simple_strtoul(buf, &e, 10);
2758        if (*buf && (*e == 0 || *e == '\n')) {
2759                atomic_set(&rdev->corrected_errors, n);
2760                return len;
2761        }
2762        return -EINVAL;
2763}
2764static struct rdev_sysfs_entry rdev_errors =
2765__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2766
2767static ssize_t
2768slot_show(struct md_rdev *rdev, char *page)
2769{
2770        if (rdev->raid_disk < 0)
2771                return sprintf(page, "none\n");
2772        else
2773                return sprintf(page, "%d\n", rdev->raid_disk);
2774}
2775
2776static ssize_t
2777slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2778{
2779        char *e;
2780        int err;
2781        int slot = simple_strtoul(buf, &e, 10);
2782        if (strncmp(buf, "none", 4)==0)
2783                slot = -1;
2784        else if (e==buf || (*e && *e!= '\n'))
2785                return -EINVAL;
2786        if (rdev->mddev->pers && slot == -1) {
2787                /* Setting 'slot' on an active array requires also
2788                 * updating the 'rd%d' link, and communicating
2789                 * with the personality with ->hot_*_disk.
2790                 * For now we only support removing
2791                 * failed/spare devices.  This normally happens automatically,
2792                 * but not when the metadata is externally managed.
2793                 */
2794                if (rdev->raid_disk == -1)
2795                        return -EEXIST;
2796                /* personality does all needed checks */
2797                if (rdev->mddev->pers->hot_remove_disk == NULL)
2798                        return -EINVAL;
2799                err = rdev->mddev->pers->
2800                        hot_remove_disk(rdev->mddev, rdev);
2801                if (err)
2802                        return err;
2803                sysfs_unlink_rdev(rdev->mddev, rdev);
2804                rdev->raid_disk = -1;
2805                set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2806                md_wakeup_thread(rdev->mddev->thread);
2807        } else if (rdev->mddev->pers) {
2808                /* Activating a spare .. or possibly reactivating
2809                 * if we ever get bitmaps working here.
2810                 */
2811
2812                if (rdev->raid_disk != -1)
2813                        return -EBUSY;
2814
2815                if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2816                        return -EBUSY;
2817
2818                if (rdev->mddev->pers->hot_add_disk == NULL)
2819                        return -EINVAL;
2820
2821                if (slot >= rdev->mddev->raid_disks &&
2822                    slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2823                        return -ENOSPC;
2824
2825                rdev->raid_disk = slot;
2826                if (test_bit(In_sync, &rdev->flags))
2827                        rdev->saved_raid_disk = slot;
2828                else
2829                        rdev->saved_raid_disk = -1;
2830                clear_bit(In_sync, &rdev->flags);
2831                err = rdev->mddev->pers->
2832                        hot_add_disk(rdev->mddev, rdev);
2833                if (err) {
2834                        rdev->raid_disk = -1;
2835                        return err;
2836                } else
2837                        sysfs_notify_dirent_safe(rdev->sysfs_state);
2838                if (sysfs_link_rdev(rdev->mddev, rdev))
2839                        /* failure here is OK */;
2840                /* don't wakeup anyone, leave that to userspace. */
2841        } else {
2842                if (slot >= rdev->mddev->raid_disks &&
2843                    slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2844                        return -ENOSPC;
2845                rdev->raid_disk = slot;
2846                /* assume it is working */
2847                clear_bit(Faulty, &rdev->flags);
2848                clear_bit(WriteMostly, &rdev->flags);
2849                set_bit(In_sync, &rdev->flags);
2850                sysfs_notify_dirent_safe(rdev->sysfs_state);
2851        }
2852        return len;
2853}
2854
2855
2856static struct rdev_sysfs_entry rdev_slot =
2857__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2858
2859static ssize_t
2860offset_show(struct md_rdev *rdev, char *page)
2861{
2862        return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2863}
2864
2865static ssize_t
2866offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2867{
2868        unsigned long long offset;
2869        if (strict_strtoull(buf, 10, &offset) < 0)
2870                return -EINVAL;
2871        if (rdev->mddev->pers && rdev->raid_disk >= 0)
2872                return -EBUSY;
2873        if (rdev->sectors && rdev->mddev->external)
2874                /* Must set offset before size, so overlap checks
2875                 * can be sane */
2876                return -EBUSY;
2877        rdev->data_offset = offset;
2878        rdev->new_data_offset = offset;
2879        return len;
2880}
2881
2882static struct rdev_sysfs_entry rdev_offset =
2883__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2884
2885static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2886{
2887        return sprintf(page, "%llu\n",
2888                       (unsigned long long)rdev->new_data_offset);
2889}
2890
2891static ssize_t new_offset_store(struct md_rdev *rdev,
2892                                const char *buf, size_t len)
2893{
2894        unsigned long long new_offset;
2895        struct mddev *mddev = rdev->mddev;
2896
2897        if (strict_strtoull(buf, 10, &new_offset) < 0)
2898                return -EINVAL;
2899
2900        if (mddev->sync_thread)
2901                return -EBUSY;
2902        if (new_offset == rdev->data_offset)
2903                /* reset is always permitted */
2904                ;
2905        else if (new_offset > rdev->data_offset) {
2906                /* must not push array size beyond rdev_sectors */
2907                if (new_offset - rdev->data_offset
2908                    + mddev->dev_sectors > rdev->sectors)
2909                                return -E2BIG;
2910        }
2911        /* Metadata worries about other space details. */
2912
2913        /* decreasing the offset is inconsistent with a backwards
2914         * reshape.
2915         */
2916        if (new_offset < rdev->data_offset &&
2917            mddev->reshape_backwards)
2918                return -EINVAL;
2919        /* Increasing offset is inconsistent with forwards
2920         * reshape.  reshape_direction should be set to
2921         * 'backwards' first.
2922         */
2923        if (new_offset > rdev->data_offset &&
2924            !mddev->reshape_backwards)
2925                return -EINVAL;
2926
2927        if (mddev->pers && mddev->persistent &&
2928            !super_types[mddev->major_version]
2929            .allow_new_offset(rdev, new_offset))
2930                return -E2BIG;
2931        rdev->new_data_offset = new_offset;
2932        if (new_offset > rdev->data_offset)
2933                mddev->reshape_backwards = 1;
2934        else if (new_offset < rdev->data_offset)
2935                mddev->reshape_backwards = 0;
2936
2937        return len;
2938}
2939static struct rdev_sysfs_entry rdev_new_offset =
2940__ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2941
2942static ssize_t
2943rdev_size_show(struct md_rdev *rdev, char *page)
2944{
2945        return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2946}
2947
2948static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2949{
2950        /* check if two start/length pairs overlap */
2951        if (s1+l1 <= s2)
2952                return 0;
2953        if (s2+l2 <= s1)
2954                return 0;
2955        return 1;
2956}
2957
2958static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2959{
2960        unsigned long long blocks;
2961        sector_t new;
2962
2963        if (strict_strtoull(buf, 10, &blocks) < 0)
2964                return -EINVAL;
2965
2966        if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2967                return -EINVAL; /* sector conversion overflow */
2968
2969        new = blocks * 2;
2970        if (new != blocks * 2)
2971                return -EINVAL; /* unsigned long long to sector_t overflow */
2972
2973        *sectors = new;
2974        return 0;
2975}
2976
2977static ssize_t
2978rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2979{
2980        struct mddev *my_mddev = rdev->mddev;
2981        sector_t oldsectors = rdev->sectors;
2982        sector_t sectors;
2983
2984        if (strict_blocks_to_sectors(buf, &sectors) < 0)
2985                return -EINVAL;
2986        if (rdev->data_offset != rdev->new_data_offset)
2987                return -EINVAL; /* too confusing */
2988        if (my_mddev->pers && rdev->raid_disk >= 0) {
2989                if (my_mddev->persistent) {
2990                        sectors = super_types[my_mddev->major_version].
2991                                rdev_size_change(rdev, sectors);
2992                        if (!sectors)
2993                                return -EBUSY;
2994                } else if (!sectors)
2995                        sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2996                                rdev->data_offset;
2997        }
2998        if (sectors < my_mddev->dev_sectors)
2999                return -EINVAL; /* component must fit device */
3000
3001        rdev->sectors = sectors;
3002        if (sectors > oldsectors && my_mddev->external) {
3003                /* need to check that all other rdevs with the same ->bdev
3004                 * do not overlap.  We need to unlock the mddev to avoid
3005                 * a deadlock.  We have already changed rdev->sectors, and if
3006                 * we have to change it back, we will have the lock again.
3007                 */
3008                struct mddev *mddev;
3009                int overlap = 0;
3010                struct list_head *tmp;
3011
3012                mddev_unlock(my_mddev);
3013                for_each_mddev(mddev, tmp) {
3014                        struct md_rdev *rdev2;
3015
3016                        mddev_lock(mddev);
3017                        rdev_for_each(rdev2, mddev)
3018                                if (rdev->bdev == rdev2->bdev &&
3019                                    rdev != rdev2 &&
3020                                    overlaps(rdev->data_offset, rdev->sectors,
3021                                             rdev2->data_offset,
3022                                             rdev2->sectors)) {
3023                                        overlap = 1;
3024                                        break;
3025                                }
3026                        mddev_unlock(mddev);
3027                        if (overlap) {
3028                                mddev_put(mddev);
3029                                break;
3030                        }
3031                }
3032                mddev_lock(my_mddev);
3033                if (overlap) {
3034                        /* Someone else could have slipped in a size
3035                         * change here, but doing so is just silly.
3036                         * We put oldsectors back because we *know* it is
3037                         * safe, and trust userspace not to race with
3038                         * itself
3039                         */
3040                        rdev->sectors = oldsectors;
3041                        return -EBUSY;
3042                }
3043        }
3044        return len;
3045}
3046
3047static struct rdev_sysfs_entry rdev_size =
3048__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3049
3050
3051static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3052{
3053        unsigned long long recovery_start = rdev->recovery_offset;
3054
3055        if (test_bit(In_sync, &rdev->flags) ||
3056            recovery_start == MaxSector)
3057                return sprintf(page, "none\n");
3058
3059        return sprintf(page, "%llu\n", recovery_start);
3060}
3061
3062static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3063{
3064        unsigned long long recovery_start;
3065
3066        if (cmd_match(buf, "none"))
3067                recovery_start = MaxSector;
3068        else if (strict_strtoull(buf, 10, &recovery_start))
3069                return -EINVAL;
3070
3071        if (rdev->mddev->pers &&
3072            rdev->raid_disk >= 0)
3073                return -EBUSY;
3074
3075        rdev->recovery_offset = recovery_start;
3076        if (recovery_start == MaxSector)
3077                set_bit(In_sync, &rdev->flags);
3078        else
3079                clear_bit(In_sync, &rdev->flags);
3080        return len;
3081}
3082
3083static struct rdev_sysfs_entry rdev_recovery_start =
3084__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3085
3086
3087static ssize_t
3088badblocks_show(struct badblocks *bb, char *page, int unack);
3089static ssize_t
3090badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3091
3092static ssize_t bb_show(struct md_rdev *rdev, char *page)
3093{
3094        return badblocks_show(&rdev->badblocks, page, 0);
3095}
3096static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3097{
3098        int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3099        /* Maybe that ack was all we needed */
3100        if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3101                wake_up(&rdev->blocked_wait);
3102        return rv;
3103}
3104static struct rdev_sysfs_entry rdev_bad_blocks =
3105__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3106
3107
3108static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3109{
3110        return badblocks_show(&rdev->badblocks, page, 1);
3111}
3112static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3113{
3114        return badblocks_store(&rdev->badblocks, page, len, 1);
3115}
3116static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3117__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3118
3119static struct attribute *rdev_default_attrs[] = {
3120        &rdev_state.attr,
3121        &rdev_errors.attr,
3122        &rdev_slot.attr,
3123        &rdev_offset.attr,
3124        &rdev_new_offset.attr,
3125        &rdev_size.attr,
3126        &rdev_recovery_start.attr,
3127        &rdev_bad_blocks.attr,
3128        &rdev_unack_bad_blocks.attr,
3129        NULL,
3130};
3131static ssize_t
3132rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3133{
3134        struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3135        struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3136        struct mddev *mddev = rdev->mddev;
3137        ssize_t rv;
3138
3139        if (!entry->show)
3140                return -EIO;
3141
3142        rv = mddev ? mddev_lock(mddev) : -EBUSY;
3143        if (!rv) {
3144                if (rdev->mddev == NULL)
3145                        rv = -EBUSY;
3146                else
3147                        rv = entry->show(rdev, page);
3148                mddev_unlock(mddev);
3149        }
3150        return rv;
3151}
3152
3153static ssize_t
3154rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3155              const char *page, size_t length)
3156{
3157        struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3158        struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3159        ssize_t rv;
3160        struct mddev *mddev = rdev->mddev;
3161
3162        if (!entry->store)
3163                return -EIO;
3164        if (!capable(CAP_SYS_ADMIN))
3165                return -EACCES;
3166        rv = mddev ? mddev_lock(mddev): -EBUSY;
3167        if (!rv) {
3168                if (rdev->mddev == NULL)
3169                        rv = -EBUSY;
3170                else
3171                        rv = entry->store(rdev, page, length);
3172                mddev_unlock(mddev);
3173        }
3174        return rv;
3175}
3176
3177static void rdev_free(struct kobject *ko)
3178{
3179        struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3180        kfree(rdev);
3181}
3182static const struct sysfs_ops rdev_sysfs_ops = {
3183        .show           = rdev_attr_show,
3184        .store          = rdev_attr_store,
3185};
3186static struct kobj_type rdev_ktype = {
3187        .release        = rdev_free,
3188        .sysfs_ops      = &rdev_sysfs_ops,
3189        .default_attrs  = rdev_default_attrs,
3190};
3191
3192int md_rdev_init(struct md_rdev *rdev)
3193{
3194        rdev->desc_nr = -1;
3195        rdev->saved_raid_disk = -1;
3196        rdev->raid_disk = -1;
3197        rdev->flags = 0;
3198        rdev->data_offset = 0;
3199        rdev->new_data_offset = 0;
3200        rdev->sb_events = 0;
3201        rdev->last_read_error.tv_sec  = 0;
3202        rdev->last_read_error.tv_nsec = 0;
3203        rdev->sb_loaded = 0;
3204        rdev->bb_page = NULL;
3205        atomic_set(&rdev->nr_pending, 0);
3206        atomic_set(&rdev->read_errors, 0);
3207        atomic_set(&rdev->corrected_errors, 0);
3208
3209        INIT_LIST_HEAD(&rdev->same_set);
3210        init_waitqueue_head(&rdev->blocked_wait);
3211
3212        /* Add space to store bad block list.
3213         * This reserves the space even on arrays where it cannot
3214         * be used - I wonder if that matters
3215         */
3216        rdev->badblocks.count = 0;
3217        rdev->badblocks.shift = 0;
3218        rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3219        seqlock_init(&rdev->badblocks.lock);
3220        if (rdev->badblocks.page == NULL)
3221                return -ENOMEM;
3222
3223        return 0;
3224}
3225EXPORT_SYMBOL_GPL(md_rdev_init);
3226/*
3227 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3228 *
3229 * mark the device faulty if:
3230 *
3231 *   - the device is nonexistent (zero size)
3232 *   - the device has no valid superblock
3233 *
3234 * a faulty rdev _never_ has rdev->sb set.
3235 */
3236static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3237{
3238        char b[BDEVNAME_SIZE];
3239        int err;
3240        struct md_rdev *rdev;
3241        sector_t size;
3242
3243        rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3244        if (!rdev) {
3245                printk(KERN_ERR "md: could not alloc mem for new device!\n");
3246                return ERR_PTR(-ENOMEM);
3247        }
3248
3249        err = md_rdev_init(rdev);
3250        if (err)
3251                goto abort_free;
3252        err = alloc_disk_sb(rdev);
3253        if (err)
3254                goto abort_free;
3255
3256        err = lock_rdev(rdev, newdev, super_format == -2);
3257        if (err)
3258                goto abort_free;
3259
3260        kobject_init(&rdev->kobj, &rdev_ktype);
3261
3262        size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3263        if (!size) {
3264                printk(KERN_WARNING 
3265                        "md: %s has zero or unknown size, marking faulty!\n",
3266                        bdevname(rdev->bdev,b));
3267                err = -EINVAL;
3268                goto abort_free;
3269        }
3270
3271        if (super_format >= 0) {
3272                err = super_types[super_format].
3273                        load_super(rdev, NULL, super_minor);
3274                if (err == -EINVAL) {
3275                        printk(KERN_WARNING
3276                                "md: %s does not have a valid v%d.%d "
3277                               "superblock, not importing!\n",
3278                                bdevname(rdev->bdev,b),
3279                               super_format, super_minor);
3280                        goto abort_free;
3281                }
3282                if (err < 0) {
3283                        printk(KERN_WARNING 
3284                                "md: could not read %s's sb, not importing!\n",
3285                                bdevname(rdev->bdev,b));
3286                        goto abort_free;
3287                }
3288        }
3289        if (super_format == -1)
3290                /* hot-add for 0.90, or non-persistent: so no badblocks */
3291                rdev->badblocks.shift = -1;
3292
3293        return rdev;
3294
3295abort_free:
3296        if (rdev->bdev)
3297                unlock_rdev(rdev);
3298        md_rdev_clear(rdev);
3299        kfree(rdev);
3300        return ERR_PTR(err);
3301}
3302
3303/*
3304 * Check a full RAID array for plausibility
3305 */
3306
3307
3308static void analyze_sbs(struct mddev * mddev)
3309{
3310        int i;
3311        struct md_rdev *rdev, *freshest, *tmp;
3312        char b[BDEVNAME_SIZE];
3313
3314        freshest = NULL;
3315        rdev_for_each_safe(rdev, tmp, mddev)
3316                switch (super_types[mddev->major_version].
3317                        load_super(rdev, freshest, mddev->minor_version)) {
3318                case 1:
3319                        freshest = rdev;
3320                        break;
3321                case 0:
3322                        break;
3323                default:
3324                        printk( KERN_ERR \
3325                                "md: fatal superblock inconsistency in %s"
3326                                " -- removing from array\n", 
3327                                bdevname(rdev->bdev,b));
3328                        kick_rdev_from_array(rdev);
3329                }
3330
3331
3332        super_types[mddev->major_version].
3333                validate_super(mddev, freshest);
3334
3335        i = 0;
3336        rdev_for_each_safe(rdev, tmp, mddev) {
3337                if (mddev->max_disks &&
3338                    (rdev->desc_nr >= mddev->max_disks ||
3339                     i > mddev->max_disks)) {
3340                        printk(KERN_WARNING
3341                               "md: %s: %s: only %d devices permitted\n",
3342                               mdname(mddev), bdevname(rdev->bdev, b),
3343                               mddev->max_disks);
3344                        kick_rdev_from_array(rdev);
3345                        continue;
3346                }
3347                if (rdev != freshest)
3348                        if (super_types[mddev->major_version].
3349                            validate_super(mddev, rdev)) {
3350                                printk(KERN_WARNING "md: kicking non-fresh %s"
3351                                        " from array!\n",
3352                                        bdevname(rdev->bdev,b));
3353                                kick_rdev_from_array(rdev);
3354                                continue;
3355                        }
3356                if (mddev->level == LEVEL_MULTIPATH) {
3357                        rdev->desc_nr = i++;
3358                        rdev->raid_disk = rdev->desc_nr;
3359                        set_bit(In_sync, &rdev->flags);
3360                } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
3361                        rdev->raid_disk = -1;
3362                        clear_bit(In_sync, &rdev->flags);
3363                }
3364        }
3365}
3366
3367/* Read a fixed-point number.
3368 * Numbers in sysfs attributes should be in "standard" units where
3369 * possible, so time should be in seconds.
3370 * However we internally use a a much smaller unit such as 
3371 * milliseconds or jiffies.
3372 * This function takes a decimal number with a possible fractional
3373 * component, and produces an integer which is the result of
3374 * multiplying that number by 10^'scale'.
3375 * all without any floating-point arithmetic.
3376 */
3377int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3378{
3379        unsigned long result = 0;
3380        long decimals = -1;
3381        while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3382                if (*cp == '.')
3383                        decimals = 0;
3384                else if (decimals < scale) {
3385                        unsigned int value;
3386                        value = *cp - '0';
3387                        result = result * 10 + value;
3388                        if (decimals >= 0)
3389                                decimals++;
3390                }
3391                cp++;
3392        }
3393        if (*cp == '\n')
3394                cp++;
3395        if (*cp)
3396                return -EINVAL;
3397        if (decimals < 0)
3398                decimals = 0;
3399        while (decimals < scale) {
3400                result *= 10;
3401                decimals ++;
3402        }
3403        *res = result;
3404        return 0;
3405}
3406
3407
3408static void md_safemode_timeout(unsigned long data);
3409
3410static ssize_t
3411safe_delay_show(struct mddev *mddev, char *page)
3412{
3413        int msec = (mddev->safemode_delay*1000)/HZ;
3414        return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3415}
3416static ssize_t
3417safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3418{
3419        unsigned long msec;
3420
3421        if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3422                return -EINVAL;
3423        if (msec == 0)
3424                mddev->safemode_delay = 0;
3425        else {
3426                unsigned long old_delay = mddev->safemode_delay;
3427                mddev->safemode_delay = (msec*HZ)/1000;
3428                if (mddev->safemode_delay == 0)
3429                        mddev->safemode_delay = 1;
3430                if (mddev->safemode_delay < old_delay)
3431                        md_safemode_timeout((unsigned long)mddev);
3432        }
3433        return len;
3434}
3435static struct md_sysfs_entry md_safe_delay =
3436__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3437
3438static ssize_t
3439level_show(struct mddev *mddev, char *page)
3440{
3441        struct md_personality *p = mddev->pers;
3442        if (p)
3443                return sprintf(page, "%s\n", p->name);
3444        else if (mddev->clevel[0])
3445                return sprintf(page, "%s\n", mddev->clevel);
3446        else if (mddev->level != LEVEL_NONE)
3447                return sprintf(page, "%d\n", mddev->level);
3448        else
3449                return 0;
3450}
3451
3452static ssize_t
3453level_store(struct mddev *mddev, const char *buf, size_t len)
3454{
3455        char clevel[16];
3456        ssize_t rv = len;
3457        struct md_personality *pers;
3458        long level;
3459        void *priv;
3460        struct md_rdev *rdev;
3461
3462        if (mddev->pers == NULL) {
3463                if (len == 0)
3464                        return 0;
3465                if (len >= sizeof(mddev->clevel))
3466                        return -ENOSPC;
3467                strncpy(mddev->clevel, buf, len);
3468                if (mddev->clevel[len-1] == '\n')
3469                        len--;
3470                mddev->clevel[len] = 0;
3471                mddev->level = LEVEL_NONE;
3472                return rv;
3473        }
3474
3475        /* request to change the personality.  Need to ensure:
3476         *  - array is not engaged in resync/recovery/reshape
3477         *  - old personality can be suspended
3478         *  - new personality will access other array.
3479         */
3480
3481        if (mddev->sync_thread ||
3482            mddev->reshape_position != MaxSector ||
3483            mddev->sysfs_active)
3484                return -EBUSY;
3485
3486        if (!mddev->pers->quiesce) {
3487                printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3488                       mdname(mddev), mddev->pers->name);
3489                return -EINVAL;
3490        }
3491
3492        /* Now find the new personality */
3493        if (len == 0 || len >= sizeof(clevel))
3494                return -EINVAL;
3495        strncpy(clevel, buf, len);
3496        if (clevel[len-1] == '\n')
3497                len--;
3498        clevel[len] = 0;
3499        if (strict_strtol(clevel, 10, &level))
3500                level = LEVEL_NONE;
3501
3502        if (request_module("md-%s", clevel) != 0)
3503                request_module("md-level-%s", clevel);
3504        spin_lock(&pers_lock);
3505        pers = find_pers(level, clevel);
3506        if (!pers || !try_module_get(pers->owner)) {
3507                spin_unlock(&pers_lock);
3508                printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3509                return -EINVAL;
3510        }
3511        spin_unlock(&pers_lock);
3512
3513        if (pers == mddev->pers) {
3514                /* Nothing to do! */
3515                module_put(pers->owner);
3516                return rv;
3517        }
3518        if (!pers->takeover) {
3519                module_put(pers->owner);
3520                printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3521                       mdname(mddev), clevel);
3522                return -EINVAL;
3523        }
3524
3525        rdev_for_each(rdev, mddev)
3526                rdev->new_raid_disk = rdev->raid_disk;
3527
3528        /* ->takeover must set new_* and/or delta_disks
3529         * if it succeeds, and may set them when it fails.
3530         */
3531        priv = pers->takeover(mddev);
3532        if (IS_ERR(priv)) {
3533                mddev->new_level = mddev->level;
3534                mddev->new_layout = mddev->layout;
3535                mddev->new_chunk_sectors = mddev->chunk_sectors;
3536                mddev->raid_disks -= mddev->delta_disks;
3537                mddev->delta_disks = 0;
3538                mddev->reshape_backwards = 0;
3539                module_put(pers->owner);
3540                printk(KERN_WARNING "md: %s: %s would not accept array\n",
3541                       mdname(mddev), clevel);
3542                return PTR_ERR(priv);
3543        }
3544
3545        /* Looks like we have a winner */
3546        mddev_suspend(mddev);
3547        mddev->pers->stop(mddev);
3548        
3549        if (mddev->pers->sync_request == NULL &&
3550            pers->sync_request != NULL) {
3551                /* need to add the md_redundancy_group */
3552                if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3553                        printk(KERN_WARNING
3554                               "md: cannot register extra attributes for %s\n",
3555                               mdname(mddev));
3556                mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
3557        }               
3558        if (mddev->pers->sync_request != NULL &&
3559            pers->sync_request == NULL) {
3560                /* need to remove the md_redundancy_group */
3561                if (mddev->to_remove == NULL)
3562                        mddev->to_remove = &md_redundancy_group;
3563        }
3564
3565        if (mddev->pers->sync_request == NULL &&
3566            mddev->external) {
3567                /* We are converting from a no-redundancy array
3568                 * to a redundancy array and metadata is managed
3569                 * externally so we need to be sure that writes
3570                 * won't block due to a need to transition
3571                 *      clean->dirty
3572                 * until external management is started.
3573                 */
3574                mddev->in_sync = 0;
3575                mddev->safemode_delay = 0;
3576                mddev->safemode = 0;
3577        }
3578
3579        rdev_for_each(rdev, mddev) {
3580                if (rdev->raid_disk < 0)
3581                        continue;
3582                if (rdev->new_raid_disk >= mddev->raid_disks)
3583                        rdev->new_raid_disk = -1;
3584                if (rdev->new_raid_disk == rdev->raid_disk)
3585                        continue;
3586                sysfs_unlink_rdev(mddev, rdev);
3587        }
3588        rdev_for_each(rdev, mddev) {
3589                if (rdev->raid_disk < 0)
3590                        continue;
3591                if (rdev->new_raid_disk == rdev->raid_disk)
3592                        continue;
3593                rdev->raid_disk = rdev->new_raid_disk;
3594                if (rdev->raid_disk < 0)
3595                        clear_bit(In_sync, &rdev->flags);
3596                else {
3597                        if (sysfs_link_rdev(mddev, rdev))
3598                                printk(KERN_WARNING "md: cannot register rd%d"
3599                                       " for %s after level change\n",
3600                                       rdev->raid_disk, mdname(mddev));
3601                }
3602        }
3603
3604        module_put(mddev->pers->owner);
3605        mddev->pers = pers;
3606        mddev->private = priv;
3607        strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3608        mddev->level = mddev->new_level;
3609        mddev->layout = mddev->new_layout;
3610        mddev->chunk_sectors = mddev->new_chunk_sectors;
3611        mddev->delta_disks = 0;
3612        mddev->reshape_backwards = 0;
3613        mddev->degraded = 0;
3614        if (mddev->pers->sync_request == NULL) {
3615                /* this is now an array without redundancy, so
3616                 * it must always be in_sync
3617                 */
3618                mddev->in_sync = 1;
3619                del_timer_sync(&mddev->safemode_timer);
3620        }
3621        pers->run(mddev);
3622        set_bit(MD_CHANGE_DEVS, &mddev->flags);
3623        mddev_resume(mddev);
3624        sysfs_notify(&mddev->kobj, NULL, "level");
3625        md_new_event(mddev);
3626        return rv;
3627}
3628
3629static struct md_sysfs_entry md_level =
3630__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3631
3632
3633static ssize_t
3634layout_show(struct mddev *mddev, char *page)
3635{
3636        /* just a number, not meaningful for all levels */
3637        if (mddev->reshape_position != MaxSector &&
3638            mddev->layout != mddev->new_layout)
3639                return sprintf(page, "%d (%d)\n",
3640                               mddev->new_layout, mddev->layout);
3641        return sprintf(page, "%d\n", mddev->layout);
3642}
3643
3644static ssize_t
3645layout_store(struct mddev *mddev, const char *buf, size_t len)
3646{
3647        char *e;
3648        unsigned long n = simple_strtoul(buf, &e, 10);
3649
3650        if (!*buf || (*e && *e != '\n'))
3651                return -EINVAL;
3652
3653        if (mddev->pers) {
3654                int err;
3655                if (mddev->pers->check_reshape == NULL)
3656                        return -EBUSY;
3657                mddev->new_layout = n;
3658                err = mddev->pers->check_reshape(mddev);
3659                if (err) {
3660                        mddev->new_layout = mddev->layout;
3661                        return err;
3662                }
3663        } else {
3664                mddev->new_layout = n;
3665                if (mddev->reshape_position == MaxSector)
3666                        mddev->layout = n;
3667        }
3668        return len;
3669}
3670static struct md_sysfs_entry md_layout =
3671__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3672
3673
3674static ssize_t
3675raid_disks_show(struct mddev *mddev, char *page)
3676{
3677        if (mddev->raid_disks == 0)
3678                return 0;
3679        if (mddev->reshape_position != MaxSector &&
3680            mddev->delta_disks != 0)
3681                return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3682                               mddev->raid_disks - mddev->delta_disks);
3683        return sprintf(page, "%d\n", mddev->raid_disks);
3684}
3685
3686static int update_raid_disks(struct mddev *mddev, int raid_disks);
3687
3688static ssize_t
3689raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3690{
3691        char *e;
3692        int rv = 0;
3693        unsigned long n = simple_strtoul(buf, &e, 10);
3694
3695        if (!*buf || (*e && *e != '\n'))
3696                return -EINVAL;
3697
3698        if (mddev->pers)
3699                rv = update_raid_disks(mddev, n);
3700        else if (mddev->reshape_position != MaxSector) {
3701                struct md_rdev *rdev;
3702                int olddisks = mddev->raid_disks - mddev->delta_disks;
3703
3704                rdev_for_each(rdev, mddev) {
3705                        if (olddisks < n &&
3706                            rdev->data_offset < rdev->new_data_offset)
3707                                return -EINVAL;
3708                        if (olddisks > n &&
3709                            rdev->data_offset > rdev->new_data_offset)
3710                                return -EINVAL;
3711                }
3712                mddev->delta_disks = n - olddisks;
3713                mddev->raid_disks = n;
3714                mddev->reshape_backwards = (mddev->delta_disks < 0);
3715        } else
3716                mddev->raid_disks = n;
3717        return rv ? rv : len;
3718}
3719static struct md_sysfs_entry md_raid_disks =
3720__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3721
3722static ssize_t
3723chunk_size_show(struct mddev *mddev, char *page)
3724{
3725        if (mddev->reshape_position != MaxSector &&
3726            mddev->chunk_sectors != mddev->new_chunk_sectors)
3727                return sprintf(page, "%d (%d)\n",
3728                               mddev->new_chunk_sectors << 9,
3729                               mddev->chunk_sectors << 9);
3730        return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3731}
3732
3733static ssize_t
3734chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3735{
3736        char *e;
3737        unsigned long n = simple_strtoul(buf, &e, 10);
3738
3739        if (!*buf || (*e && *e != '\n'))
3740                return -EINVAL;
3741
3742        if (mddev->pers) {
3743                int err;
3744                if (mddev->pers->check_reshape == NULL)
3745                        return -EBUSY;
3746                mddev->new_chunk_sectors = n >> 9;
3747                err = mddev->pers->check_reshape(mddev);
3748                if (err) {
3749                        mddev->new_chunk_sectors = mddev->chunk_sectors;
3750                        return err;
3751                }
3752        } else {
3753                mddev->new_chunk_sectors = n >> 9;
3754                if (mddev->reshape_position == MaxSector)
3755                        mddev->chunk_sectors = n >> 9;
3756        }
3757        return len;
3758}
3759static struct md_sysfs_entry md_chunk_size =
3760__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3761
3762static ssize_t
3763resync_start_show(struct mddev *mddev, char *page)
3764{
3765        if (mddev->recovery_cp == MaxSector)
3766                return sprintf(page, "none\n");
3767        return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3768}
3769
3770static ssize_t
3771resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3772{
3773        char *e;
3774        unsigned long long n = simple_strtoull(buf, &e, 10);
3775
3776        if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3777                return -EBUSY;
3778        if (cmd_match(buf, "none"))
3779                n = MaxSector;
3780        else if (!*buf || (*e && *e != '\n'))
3781                return -EINVAL;
3782
3783        mddev->recovery_cp = n;
3784        if (mddev->pers)
3785                set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3786        return len;
3787}
3788static struct md_sysfs_entry md_resync_start =
3789__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3790
3791/*
3792 * The array state can be:
3793 *
3794 * clear
3795 *     No devices, no size, no level
3796 *     Equivalent to STOP_ARRAY ioctl
3797 * inactive
3798 *     May have some settings, but array is not active
3799 *        all IO results in error
3800 *     When written, doesn't tear down array, but just stops it
3801 * suspended (not supported yet)
3802 *     All IO requests will block. The array can be reconfigured.
3803 *     Writing this, if accepted, will block until array is quiescent
3804 * readonly
3805 *     no resync can happen.  no superblocks get written.
3806 *     write requests fail
3807 * read-auto
3808 *     like readonly, but behaves like 'clean' on a write request.
3809 *
3810 * clean - no pending writes, but otherwise active.
3811 *     When written to inactive array, starts without resync
3812 *     If a write request arrives then
3813 *       if metadata is known, mark 'dirty' and switch to 'active'.
3814 *       if not known, block and switch to write-pending
3815 *     If written to an active array that has pending writes, then fails.
3816 * active
3817 *     fully active: IO and resync can be happening.
3818 *     When written to inactive array, starts with resync
3819 *
3820 * write-pending
3821 *     clean, but writes are blocked waiting for 'active' to be written.
3822 *
3823 * active-idle
3824 *     like active, but no writes have been seen for a while (100msec).
3825 *
3826 */
3827enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3828                   write_pending, active_idle, bad_word};
3829static char *array_states[] = {
3830        "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3831        "write-pending", "active-idle", NULL };
3832
3833static int match_word(const char *word, char **list)
3834{
3835        int n;
3836        for (n=0; list[n]; n++)
3837                if (cmd_match(word, list[n]))
3838                        break;
3839        return n;
3840}
3841
3842static ssize_t
3843array_state_show(struct mddev *mddev, char *page)
3844{
3845        enum array_state st = inactive;
3846
3847        if (mddev->pers)
3848                switch(mddev->ro) {
3849                case 1:
3850                        st = readonly;
3851                        break;
3852                case 2:
3853                        st = read_auto;
3854                        break;
3855                case 0:
3856                        if (mddev->in_sync)
3857                                st = clean;
3858                        else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3859                                st = write_pending;
3860                        else if (mddev->safemode)
3861                                st = active_idle;
3862                        else
3863                                st = active;
3864                }
3865        else {
3866                if (list_empty(&mddev->disks) &&
3867                    mddev->raid_disks == 0 &&
3868                    mddev->dev_sectors == 0)
3869                        st = clear;
3870                else
3871                        st = inactive;
3872        }
3873        return sprintf(page, "%s\n", array_states[st]);
3874}
3875
3876static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3877static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
3878static int do_md_run(struct mddev * mddev);
3879static int restart_array(struct mddev *mddev);
3880
3881static ssize_t
3882array_state_store(struct mddev *mddev, const char *buf, size_t len)
3883{
3884        int err = -EINVAL;
3885        enum array_state st = match_word(buf, array_states);
3886        switch(st) {
3887        case bad_word:
3888                break;
3889        case clear:
3890                /* stopping an active array */
3891                err = do_md_stop(mddev, 0, NULL);
3892                break;
3893        case inactive:
3894                /* stopping an active array */
3895                if (mddev->pers)
3896                        err = do_md_stop(mddev, 2, NULL);
3897                else
3898                        err = 0; /* already inactive */
3899                break;
3900        case suspended:
3901                break; /* not supported yet */
3902        case readonly:
3903                if (mddev->pers)
3904                        err = md_set_readonly(mddev, NULL);
3905                else {
3906                        mddev->ro = 1;
3907                        set_disk_ro(mddev->gendisk, 1);
3908                        err = do_md_run(mddev);
3909                }
3910                break;
3911        case read_auto:
3912                if (mddev->pers) {
3913                        if (mddev->ro == 0)
3914                                err = md_set_readonly(mddev, NULL);
3915                        else if (mddev->ro == 1)
3916                                err = restart_array(mddev);
3917                        if (err == 0) {
3918                                mddev->ro = 2;
3919                                set_disk_ro(mddev->gendisk, 0);
3920                        }
3921                } else {
3922                        mddev->ro = 2;
3923                        err = do_md_run(mddev);
3924                }
3925                break;
3926        case clean:
3927                if (mddev->pers) {
3928                        restart_array(mddev);
3929                        spin_lock_irq(&mddev->write_lock);
3930                        if (atomic_read(&mddev->writes_pending) == 0) {
3931                                if (mddev->in_sync == 0) {
3932                                        mddev->in_sync = 1;
3933                                        if (mddev->safemode == 1)
3934                                                mddev->safemode = 0;
3935                                        set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3936                                }
3937                                err = 0;
3938                        } else
3939                                err = -EBUSY;
3940                        spin_unlock_irq(&mddev->write_lock);
3941                } else
3942                        err = -EINVAL;
3943                break;
3944        case active:
3945                if (mddev->pers) {
3946                        restart_array(mddev);
3947                        clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3948                        wake_up(&mddev->sb_wait);
3949                        err = 0;
3950                } else {
3951                        mddev->ro = 0;
3952                        set_disk_ro(mddev->gendisk, 0);
3953                        err = do_md_run(mddev);
3954                }
3955                break;
3956        case write_pending:
3957        case active_idle:
3958                /* these cannot be set */
3959                break;
3960        }
3961        if (err)
3962                return err;
3963        else {
3964                if (mddev->hold_active == UNTIL_IOCTL)
3965                        mddev->hold_active = 0;
3966                sysfs_notify_dirent_safe(mddev->sysfs_state);
3967                return len;
3968        }
3969}
3970static struct md_sysfs_entry md_array_state =
3971__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3972
3973static ssize_t
3974max_corrected_read_errors_show(struct mddev *mddev, char *page) {
3975        return sprintf(page, "%d\n",
3976                       atomic_read(&mddev->max_corr_read_errors));
3977}
3978
3979static ssize_t
3980max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
3981{
3982        char *e;
3983        unsigned long n = simple_strtoul(buf, &e, 10);
3984
3985        if (*buf && (*e == 0 || *e == '\n')) {
3986                atomic_set(&mddev->max_corr_read_errors, n);
3987                return len;
3988        }
3989        return -EINVAL;
3990}
3991
3992static struct md_sysfs_entry max_corr_read_errors =
3993__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3994        max_corrected_read_errors_store);
3995
3996static ssize_t
3997null_show(struct mddev *mddev, char *page)
3998{
3999        return -EINVAL;
4000}
4001
4002static ssize_t
4003new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4004{
4005        /* buf must be %d:%d\n? giving major and minor numbers */
4006        /* The new device is added to the array.
4007         * If the array has a persistent superblock, we read the
4008         * superblock to initialise info and check validity.
4009         * Otherwise, only checking done is that in bind_rdev_to_array,
4010         * which mainly checks size.
4011         */
4012        char *e;
4013        int major = simple_strtoul(buf, &e, 10);
4014        int minor;
4015        dev_t dev;
4016        struct md_rdev *rdev;
4017        int err;
4018
4019        if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4020                return -EINVAL;
4021        minor = simple_strtoul(e+1, &e, 10);
4022        if (*e && *e != '\n')
4023                return -EINVAL;
4024        dev = MKDEV(major, minor);
4025        if (major != MAJOR(dev) ||
4026            minor != MINOR(dev))
4027                return -EOVERFLOW;
4028
4029
4030        if (mddev->persistent) {
4031                rdev = md_import_device(dev, mddev->major_version,
4032                                        mddev->minor_version);
4033                if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4034                        struct md_rdev *rdev0
4035                                = list_entry(mddev->disks.next,
4036                                             struct md_rdev, same_set);
4037                        err = super_types[mddev->major_version]
4038                                .load_super(rdev, rdev0, mddev->minor_version);
4039                        if (err < 0)
4040                                goto out;
4041                }
4042        } else if (mddev->external)
4043                rdev = md_import_device(dev, -2, -1);
4044        else
4045                rdev = md_import_device(dev, -1, -1);
4046
4047        if (IS_ERR(rdev))
4048                return PTR_ERR(rdev);
4049        err = bind_rdev_to_array(rdev, mddev);
4050 out:
4051        if (err)
4052                export_rdev(rdev);
4053        return err ? err : len;
4054}
4055
4056static struct md_sysfs_entry md_new_device =
4057__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4058
4059static ssize_t
4060bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4061{
4062        char *end;
4063        unsigned long chunk, end_chunk;
4064
4065        if (!mddev->bitmap)
4066                goto out;
4067        /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4068        while (*buf) {
4069                chunk = end_chunk = simple_strtoul(buf, &end, 0);
4070                if (buf == end) break;
4071                if (*end == '-') { /* range */
4072                        buf = end + 1;
4073                        end_chunk = simple_strtoul(buf, &end, 0);
4074                        if (buf == end) break;
4075                }
4076                if (*end && !isspace(*end)) break;
4077                bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4078                buf = skip_spaces(end);
4079        }
4080        bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4081out:
4082        return len;
4083}
4084
4085static struct md_sysfs_entry md_bitmap =
4086__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4087
4088static ssize_t
4089size_show(struct mddev *mddev, char *page)
4090{
4091        return sprintf(page, "%llu\n",
4092                (unsigned long long)mddev->dev_sectors / 2);
4093}
4094
4095static int update_size(struct mddev *mddev, sector_t num_sectors);
4096
4097static ssize_t
4098size_store(struct mddev *mddev, const char *buf, size_t len)
4099{
4100        /* If array is inactive, we can reduce the component size, but
4101         * not increase it (except from 0).
4102         * If array is active, we can try an on-line resize
4103         */
4104        sector_t sectors;
4105        int err = strict_blocks_to_sectors(buf, &sectors);
4106
4107        if (err < 0)
4108                return err;
4109        if (mddev->pers) {
4110                err = update_size(mddev, sectors);
4111                md_update_sb(mddev, 1);
4112        } else {
4113                if (mddev->dev_sectors == 0 ||
4114                    mddev->dev_sectors > sectors)
4115                        mddev->dev_sectors = sectors;
4116                else
4117                        err = -ENOSPC;
4118        }
4119        return err ? err : len;
4120}
4121
4122static struct md_sysfs_entry md_size =
4123__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4124
4125
4126/* Metadata version.
4127 * This is one of
4128 *   'none' for arrays with no metadata (good luck...)
4129 *   'external' for arrays with externally managed metadata,
4130 * or N.M for internally known formats
4131 */
4132static ssize_t
4133metadata_show(struct mddev *mddev, char *page)
4134{
4135        if (mddev->persistent)
4136                return sprintf(page, "%d.%d\n",
4137                               mddev->major_version, mddev->minor_version);
4138        else if (mddev->external)
4139                return sprintf(page, "external:%s\n", mddev->metadata_type);
4140        else
4141                return sprintf(page, "none\n");
4142}
4143
4144static ssize_t
4145metadata_store(struct mddev *mddev, const char *buf, size_t len)
4146{
4147        int major, minor;
4148        char *e;
4149        /* Changing the details of 'external' metadata is
4150         * always permitted.  Otherwise there must be
4151         * no devices attached to the array.
4152         */
4153        if (mddev->external && strncmp(buf, "external:", 9) == 0)
4154                ;
4155        else if (!list_empty(&mddev->disks))
4156                return -EBUSY;
4157
4158        if (cmd_match(buf, "none")) {
4159                mddev->persistent = 0;
4160                mddev->external = 0;
4161                mddev->major_version = 0;
4162                mddev->minor_version = 90;
4163                return len;
4164        }
4165        if (strncmp(buf, "external:", 9) == 0) {
4166                size_t namelen = len-9;
4167                if (namelen >= sizeof(mddev->metadata_type))
4168                        namelen = sizeof(mddev->metadata_type)-1;
4169                strncpy(mddev->metadata_type, buf+9, namelen);
4170                mddev->metadata_type[namelen] = 0;
4171                if (namelen && mddev->metadata_type[namelen-1] == '\n')
4172                        mddev->metadata_type[--namelen] = 0;
4173                mddev->persistent = 0;
4174                mddev->external = 1;
4175                mddev->major_version = 0;
4176                mddev->minor_version = 90;
4177                return len;
4178        }
4179        major = simple_strtoul(buf, &e, 10);
4180        if (e==buf || *e != '.')
4181                return -EINVAL;
4182        buf = e+1;
4183        minor = simple_strtoul(buf, &e, 10);
4184        if (e==buf || (*e && *e != '\n') )
4185                return -EINVAL;
4186        if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4187                return -ENOENT;
4188        mddev->major_version = major;
4189        mddev->minor_version = minor;
4190        mddev->persistent = 1;
4191        mddev->external = 0;
4192        return len;
4193}
4194
4195static struct md_sysfs_entry md_metadata =
4196__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4197
4198static ssize_t
4199action_show(struct mddev *mddev, char *page)
4200{
4201        char *type = "idle";
4202        if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4203                type = "frozen";
4204        else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4205            (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
4206                if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4207                        type = "reshape";
4208                else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4209                        if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4210                                type = "resync";
4211                        else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4212                                type = "check";
4213                        else
4214                                type = "repair";
4215                } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
4216                        type = "recover";
4217        }
4218        return sprintf(page, "%s\n", type);
4219}
4220
4221static void reap_sync_thread(struct mddev *mddev);
4222
4223static ssize_t
4224action_store(struct mddev *mddev, const char *page, size_t len)
4225{
4226        if (!mddev->pers || !mddev->pers->sync_request)
4227                return -EINVAL;
4228
4229        if (cmd_match(page, "frozen"))
4230                set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4231        else
4232                clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4233
4234        if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4235                if (mddev->sync_thread) {
4236                        set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4237                        reap_sync_thread(mddev);
4238                }
4239        } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4240                   test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
4241                return -EBUSY;
4242        else if (cmd_match(page, "resync"))
4243                set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4244        else if (cmd_match(page, "recover")) {
4245                set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4246                set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4247        } else if (cmd_match(page, "reshape")) {
4248                int err;
4249                if (mddev->pers->start_reshape == NULL)
4250                        return -EINVAL;
4251                err = mddev->pers->start_reshape(mddev);
4252                if (err)
4253                        return err;
4254                sysfs_notify(&mddev->kobj, NULL, "degraded");
4255        } else {
4256                if (cmd_match(page, "check"))
4257                        set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4258                else if (!cmd_match(page, "repair"))
4259                        return -EINVAL;
4260                set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4261                set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4262        }
4263        if (mddev->ro == 2) {
4264                /* A write to sync_action is enough to justify
4265                 * canceling read-auto mode
4266                 */
4267                mddev->ro = 0;
4268                md_wakeup_thread(mddev->sync_thread);
4269        }
4270        set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4271        md_wakeup_thread(mddev->thread);
4272        sysfs_notify_dirent_safe(mddev->sysfs_action);
4273        return len;
4274}
4275
4276static ssize_t
4277mismatch_cnt_show(struct mddev *mddev, char *page)
4278{
4279        return sprintf(page, "%llu\n",
4280                       (unsigned long long)
4281                       atomic64_read(&mddev->resync_mismatches));
4282}
4283
4284static struct md_sysfs_entry md_scan_mode =
4285__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4286
4287
4288static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4289
4290static ssize_t
4291sync_min_show(struct mddev *mddev, char *page)
4292{
4293        return sprintf(page, "%d (%s)\n", speed_min(mddev),
4294                       mddev->sync_speed_min ? "local": "system");
4295}
4296
4297static ssize_t
4298sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4299{
4300        int min;
4301        char *e;
4302        if (strncmp(buf, "system", 6)==0) {
4303                mddev->sync_speed_min = 0;
4304                return len;
4305        }
4306        min = simple_strtoul(buf, &e, 10);
4307        if (buf == e || (*e && *e != '\n') || min <= 0)
4308                return -EINVAL;
4309        mddev->sync_speed_min = min;
4310        return len;
4311}
4312
4313static struct md_sysfs_entry md_sync_min =
4314__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4315
4316static ssize_t
4317sync_max_show(struct mddev *mddev, char *page)
4318{
4319        return sprintf(page, "%d (%s)\n", speed_max(mddev),
4320                       mddev->sync_speed_max ? "local": "system");
4321}
4322
4323static ssize_t
4324sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4325{
4326        int max;
4327        char *e;
4328        if (strncmp(buf, "system", 6)==0) {
4329                mddev->sync_speed_max = 0;
4330                return len;
4331        }
4332        max = simple_strtoul(buf, &e, 10);
4333        if (buf == e || (*e && *e != '\n') || max <= 0)
4334                return -EINVAL;
4335        mddev->sync_speed_max = max;
4336        return len;
4337}
4338
4339static struct md_sysfs_entry md_sync_max =
4340__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4341
4342static ssize_t
4343degraded_show(struct mddev *mddev, char *page)
4344{
4345        return sprintf(page, "%d\n", mddev->degraded);
4346}
4347static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4348
4349static ssize_t
4350sync_force_parallel_show(struct mddev *mddev, char *page)
4351{
4352        return sprintf(page, "%d\n", mddev->parallel_resync);
4353}
4354
4355static ssize_t
4356sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4357{
4358        long n;
4359
4360        if (strict_strtol(buf, 10, &n))
4361                return -EINVAL;
4362
4363        if (n != 0 && n != 1)
4364                return -EINVAL;
4365
4366        mddev->parallel_resync = n;
4367
4368        if (mddev->sync_thread)
4369                wake_up(&resync_wait);
4370
4371        return len;
4372}
4373
4374/* force parallel resync, even with shared block devices */
4375static struct md_sysfs_entry md_sync_force_parallel =
4376__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4377       sync_force_parallel_show, sync_force_parallel_store);
4378
4379static ssize_t
4380sync_speed_show(struct mddev *mddev, char *page)
4381{
4382        unsigned long resync, dt, db;
4383        if (mddev->curr_resync == 0)
4384                return sprintf(page, "none\n");
4385        resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4386        dt = (jiffies - mddev->resync_mark) / HZ;
4387        if (!dt) dt++;
4388        db = resync - mddev->resync_mark_cnt;
4389        return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4390}
4391
4392static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4393
4394static ssize_t
4395sync_completed_show(struct mddev *mddev, char *page)
4396{
4397        unsigned long long max_sectors, resync;
4398
4399        if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4400                return sprintf(page, "none\n");
4401
4402        if (mddev->curr_resync == 1 ||
4403            mddev->curr_resync == 2)
4404                return sprintf(page, "delayed\n");
4405
4406        if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4407            test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4408                max_sectors = mddev->resync_max_sectors;
4409        else
4410                max_sectors = mddev->dev_sectors;
4411
4412        resync = mddev->curr_resync_completed;
4413        return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4414}
4415
4416static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
4417
4418static ssize_t
4419min_sync_show(struct mddev *mddev, char *page)
4420{
4421        return sprintf(page, "%llu\n",
4422                       (unsigned long long)mddev->resync_min);
4423}
4424static ssize_t
4425min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4426{
4427        unsigned long long min;
4428        if (strict_strtoull(buf, 10, &min))
4429                return -EINVAL;
4430        if (min > mddev->resync_max)
4431                return -EINVAL;
4432        if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4433                return -EBUSY;
4434
4435        /* Must be a multiple of chunk_size */
4436        if (mddev->chunk_sectors) {
4437                sector_t temp = min;
4438                if (sector_div(temp, mddev->chunk_sectors))
4439                        return -EINVAL;
4440        }
4441        mddev->resync_min = min;
4442
4443        return len;
4444}
4445
4446static struct md_sysfs_entry md_min_sync =
4447__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4448
4449static ssize_t
4450max_sync_show(struct mddev *mddev, char *page)
4451{
4452        if (mddev->resync_max == MaxSector)
4453                return sprintf(page, "max\n");
4454        else
4455                return sprintf(page, "%llu\n",
4456                               (unsigned long long)mddev->resync_max);
4457}
4458static ssize_t
4459max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4460{
4461        if (strncmp(buf, "max", 3) == 0)
4462                mddev->resync_max = MaxSector;
4463        else {
4464                unsigned long long max;
4465                if (strict_strtoull(buf, 10, &max))
4466                        return -EINVAL;
4467                if (max < mddev->resync_min)
4468                        return -EINVAL;
4469                if (max < mddev->resync_max &&
4470                    mddev->ro == 0 &&
4471                    test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4472                        return -EBUSY;
4473
4474                /* Must be a multiple of chunk_size */
4475                if (mddev->chunk_sectors) {
4476                        sector_t temp = max;
4477                        if (sector_div(temp, mddev->chunk_sectors))
4478                                return -EINVAL;
4479                }
4480                mddev->resync_max = max;
4481        }
4482        wake_up(&mddev->recovery_wait);
4483        return len;
4484}
4485
4486static struct md_sysfs_entry md_max_sync =
4487__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4488
4489static ssize_t
4490suspend_lo_show(struct mddev *mddev, char *page)
4491{
4492        return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4493}
4494
4495static ssize_t
4496suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4497{
4498        char *e;
4499        unsigned long long new = simple_strtoull(buf, &e, 10);
4500        unsigned long long old = mddev->suspend_lo;
4501
4502        if (mddev->pers == NULL || 
4503            mddev->pers->quiesce == NULL)
4504                return -EINVAL;
4505        if (buf == e || (*e && *e != '\n'))
4506                return -EINVAL;
4507
4508        mddev->suspend_lo = new;
4509        if (new >= old)
4510                /* Shrinking suspended region */
4511                mddev->pers->quiesce(mddev, 2);
4512        else {
4513                /* Expanding suspended region - need to wait */
4514                mddev->pers->quiesce(mddev, 1);
4515                mddev->pers->quiesce(mddev, 0);
4516        }
4517        return len;
4518}
4519static struct md_sysfs_entry md_suspend_lo =
4520__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4521
4522
4523static ssize_t
4524suspend_hi_show(struct mddev *mddev, char *page)
4525{
4526        return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4527}
4528
4529static ssize_t
4530suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4531{
4532        char *e;
4533        unsigned long long new = simple_strtoull(buf, &e, 10);
4534        unsigned long long old = mddev->suspend_hi;
4535
4536        if (mddev->pers == NULL ||
4537            mddev->pers->quiesce == NULL)
4538                return -EINVAL;
4539        if (buf == e || (*e && *e != '\n'))
4540                return -EINVAL;
4541
4542        mddev->suspend_hi = new;
4543        if (new <= old)
4544                /* Shrinking suspended region */
4545                mddev->pers->quiesce(mddev, 2);
4546        else {
4547                /* Expanding suspended region - need to wait */
4548                mddev->pers->quiesce(mddev, 1);
4549                mddev->pers->quiesce(mddev, 0);
4550        }
4551        return len;
4552}
4553static struct md_sysfs_entry md_suspend_hi =
4554__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4555
4556static ssize_t
4557reshape_position_show(struct mddev *mddev, char *page)
4558{
4559        if (mddev->reshape_position != MaxSector)
4560                return sprintf(page, "%llu\n",
4561                               (unsigned long long)mddev->reshape_position);
4562        strcpy(page, "none\n");
4563        return 5;
4564}
4565
4566static ssize_t
4567reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4568{
4569        struct md_rdev *rdev;
4570        char *e;
4571        unsigned long long new = simple_strtoull(buf, &e, 10);
4572        if (mddev->pers)
4573                return -EBUSY;
4574        if (buf == e || (*e && *e != '\n'))
4575                return -EINVAL;
4576        mddev->reshape_position = new;
4577        mddev->delta_disks = 0;
4578        mddev->reshape_backwards = 0;
4579        mddev->new_level = mddev->level;
4580        mddev->new_layout = mddev->layout;
4581        mddev->new_chunk_sectors = mddev->chunk_sectors;
4582        rdev_for_each(rdev, mddev)
4583                rdev->new_data_offset = rdev->data_offset;
4584        return len;
4585}
4586
4587static struct md_sysfs_entry md_reshape_position =
4588__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4589       reshape_position_store);
4590
4591static ssize_t
4592reshape_direction_show(struct mddev *mddev, char *page)
4593{
4594        return sprintf(page, "%s\n",
4595                       mddev->reshape_backwards ? "backwards" : "forwards");
4596}
4597
4598static ssize_t
4599reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4600{
4601        int backwards = 0;
4602        if (cmd_match(buf, "forwards"))
4603                backwards = 0;
4604        else if (cmd_match(buf, "backwards"))
4605                backwards = 1;
4606        else
4607                return -EINVAL;
4608        if (mddev->reshape_backwards == backwards)
4609                return len;
4610
4611        /* check if we are allowed to change */
4612        if (mddev->delta_disks)
4613                return -EBUSY;
4614
4615        if (mddev->persistent &&
4616            mddev->major_version == 0)
4617                return -EINVAL;
4618
4619        mddev->reshape_backwards = backwards;
4620        return len;
4621}
4622
4623static struct md_sysfs_entry md_reshape_direction =
4624__ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4625       reshape_direction_store);
4626
4627static ssize_t
4628array_size_show(struct mddev *mddev, char *page)
4629{
4630        if (mddev->external_size)
4631                return sprintf(page, "%llu\n",
4632                               (unsigned long long)mddev->array_sectors/2);
4633        else
4634                return sprintf(page, "default\n");
4635}
4636
4637static ssize_t
4638array_size_store(struct mddev *mddev, const char *buf, size_t len)
4639{
4640        sector_t sectors;
4641
4642        if (strncmp(buf, "default", 7) == 0) {
4643                if (mddev->pers)
4644                        sectors = mddev->pers->size(mddev, 0, 0);
4645                else
4646                        sectors = mddev->array_sectors;
4647
4648                mddev->external_size = 0;
4649        } else {
4650                if (strict_blocks_to_sectors(buf, &sectors) < 0)
4651                        return -EINVAL;
4652                if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4653                        return -E2BIG;
4654
4655                mddev->external_size = 1;
4656        }
4657
4658        mddev->array_sectors = sectors;
4659        if (mddev->pers) {
4660                set_capacity(mddev->gendisk, mddev->array_sectors);
4661                revalidate_disk(mddev->gendisk);
4662        }
4663        return len;
4664}
4665
4666static struct md_sysfs_entry md_array_size =
4667__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4668       array_size_store);
4669
4670static struct attribute *md_default_attrs[] = {
4671        &md_level.attr,
4672        &md_layout.attr,
4673        &md_raid_disks.attr,
4674        &md_chunk_size.attr,
4675        &md_size.attr,
4676        &md_resync_start.attr,
4677        &md_metadata.attr,
4678        &md_new_device.attr,
4679        &md_safe_delay.attr,
4680        &md_array_state.attr,
4681        &md_reshape_position.attr,
4682        &md_reshape_direction.attr,
4683        &md_array_size.attr,
4684        &max_corr_read_errors.attr,
4685        NULL,
4686};
4687
4688static struct attribute *md_redundancy_attrs[] = {
4689        &md_scan_mode.attr,
4690        &md_mismatches.attr,
4691        &md_sync_min.attr,
4692        &md_sync_max.attr,
4693        &md_sync_speed.attr,
4694        &md_sync_force_parallel.attr,
4695        &md_sync_completed.attr,
4696        &md_min_sync.attr,
4697        &md_max_sync.attr,
4698        &md_suspend_lo.attr,
4699        &md_suspend_hi.attr,
4700        &md_bitmap.attr,
4701        &md_degraded.attr,
4702        NULL,
4703};
4704static struct attribute_group md_redundancy_group = {
4705        .name = NULL,
4706        .attrs = md_redundancy_attrs,
4707};
4708
4709
4710static ssize_t
4711md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4712{
4713        struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4714        struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4715        ssize_t rv;
4716
4717        if (!entry->show)
4718                return -EIO;
4719        spin_lock(&all_mddevs_lock);
4720        if (list_empty(&mddev->all_mddevs)) {
4721                spin_unlock(&all_mddevs_lock);
4722                return -EBUSY;
4723        }
4724        mddev_get(mddev);
4725        spin_unlock(&all_mddevs_lock);
4726
4727        rv = mddev_lock(mddev);
4728        if (!rv) {
4729                rv = entry->show(mddev, page);
4730                mddev_unlock(mddev);
4731        }
4732        mddev_put(mddev);
4733        return rv;
4734}
4735
4736static ssize_t
4737md_attr_store(struct kobject *kobj, struct attribute *attr,
4738              const char *page, size_t length)
4739{
4740        struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4741        struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4742        ssize_t rv;
4743
4744        if (!entry->store)
4745                return -EIO;
4746        if (!capable(CAP_SYS_ADMIN))
4747                return -EACCES;
4748        spin_lock(&all_mddevs_lock);
4749        if (list_empty(&mddev->all_mddevs)) {
4750                spin_unlock(&all_mddevs_lock);
4751                return -EBUSY;
4752        }
4753        mddev_get(mddev);
4754        spin_unlock(&all_mddevs_lock);
4755        if (entry->store == new_dev_store)
4756                flush_workqueue(md_misc_wq);
4757        rv = mddev_lock(mddev);
4758        if (!rv) {
4759                rv = entry->store(mddev, page, length);
4760                mddev_unlock(mddev);
4761        }
4762        mddev_put(mddev);
4763        return rv;
4764}
4765
4766static void md_free(struct kobject *ko)
4767{
4768        struct mddev *mddev = container_of(ko, struct mddev, kobj);
4769
4770        if (mddev->sysfs_state)
4771                sysfs_put(mddev->sysfs_state);
4772
4773        if (mddev->gendisk) {
4774                del_gendisk(mddev->gendisk);
4775                put_disk(mddev->gendisk);
4776        }
4777        if (mddev->queue)
4778                blk_cleanup_queue(mddev->queue);
4779
4780        kfree(mddev);
4781}
4782
4783static const struct sysfs_ops md_sysfs_ops = {
4784        .show   = md_attr_show,
4785        .store  = md_attr_store,
4786};
4787static struct kobj_type md_ktype = {
4788        .release        = md_free,
4789        .sysfs_ops      = &md_sysfs_ops,
4790        .default_attrs  = md_default_attrs,
4791};
4792
4793int mdp_major = 0;
4794
4795static void mddev_delayed_delete(struct work_struct *ws)
4796{
4797        struct mddev *mddev = container_of(ws, struct mddev, del_work);
4798
4799        sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4800        kobject_del(&mddev->kobj);
4801        kobject_put(&mddev->kobj);
4802}
4803
4804static int md_alloc(dev_t dev, char *name)
4805{
4806        static DEFINE_MUTEX(disks_mutex);
4807        struct mddev *mddev = mddev_find(dev);
4808        struct gendisk *disk;
4809        int partitioned;
4810        int shift;
4811        int unit;
4812        int error;
4813
4814        if (!mddev)
4815                return -ENODEV;
4816
4817        partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4818        shift = partitioned ? MdpMinorShift : 0;
4819        unit = MINOR(mddev->unit) >> shift;
4820
4821        /* wait for any previous instance of this device to be
4822         * completely removed (mddev_delayed_delete).
4823         */
4824        flush_workqueue(md_misc_wq);
4825
4826        mutex_lock(&disks_mutex);
4827        error = -EEXIST;
4828        if (mddev->gendisk)
4829                goto abort;
4830
4831        if (name) {
4832                /* Need to ensure that 'name' is not a duplicate.
4833                 */
4834                struct mddev *mddev2;
4835                spin_lock(&all_mddevs_lock);
4836
4837                list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4838                        if (mddev2->gendisk &&
4839                            strcmp(mddev2->gendisk->disk_name, name) == 0) {
4840                                spin_unlock(&all_mddevs_lock);
4841                                goto abort;
4842                        }
4843                spin_unlock(&all_mddevs_lock);
4844        }
4845
4846        error = -ENOMEM;
4847        mddev->queue = blk_alloc_queue(GFP_KERNEL);
4848        if (!mddev->queue)
4849                goto abort;
4850        mddev->queue->queuedata = mddev;
4851
4852        blk_queue_make_request(mddev->queue, md_make_request);
4853        blk_set_stacking_limits(&mddev->queue->limits);
4854
4855        disk = alloc_disk(1 << shift);
4856        if (!disk) {
4857                blk_cleanup_queue(mddev->queue);
4858                mddev->queue = NULL;
4859                goto abort;
4860        }
4861        disk->major = MAJOR(mddev->unit);
4862        disk->first_minor = unit << shift;
4863        if (name)
4864                strcpy(disk->disk_name, name);
4865        else if (partitioned)
4866                sprintf(disk->disk_name, "md_d%d", unit);
4867        else
4868                sprintf(disk->disk_name, "md%d", unit);
4869        disk->fops = &md_fops;
4870        disk->private_data = mddev;
4871        disk->queue = mddev->queue;
4872        blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4873        /* Allow extended partitions.  This makes the
4874         * 'mdp' device redundant, but we can't really
4875         * remove it now.
4876         */
4877        disk->flags |= GENHD_FL_EXT_DEVT;
4878        mddev->gendisk = disk;
4879        /* As soon as we call add_disk(), another thread could get
4880         * through to md_open, so make sure it doesn't get too far
4881         */
4882        mutex_lock(&mddev->open_mutex);
4883        add_disk(disk);
4884
4885        error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4886                                     &disk_to_dev(disk)->kobj, "%s", "md");
4887        if (error) {
4888                /* This isn't possible, but as kobject_init_and_add is marked
4889                 * __must_check, we must do something with the result
4890                 */
4891                printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4892                       disk->disk_name);
4893                error = 0;
4894        }
4895        if (mddev->kobj.sd &&
4896            sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4897                printk(KERN_DEBUG "pointless warning\n");
4898        mutex_unlock(&mddev->open_mutex);
4899 abort:
4900        mutex_unlock(&disks_mutex);
4901        if (!error && mddev->kobj.sd) {
4902                kobject_uevent(&mddev->kobj, KOBJ_ADD);
4903                mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4904        }
4905        mddev_put(mddev);
4906        return error;
4907}
4908
4909static struct kobject *md_probe(dev_t dev, int *part, void *data)
4910{
4911        md_alloc(dev, NULL);
4912        return NULL;
4913}
4914
4915static int add_named_array(const char *val, struct kernel_param *kp)
4916{
4917        /* val must be "md_*" where * is not all digits.
4918         * We allocate an array with a large free minor number, and
4919         * set the name to val.  val must not already be an active name.
4920         */
4921        int len = strlen(val);
4922        char buf[DISK_NAME_LEN];
4923
4924        while (len && val[len-1] == '\n')
4925                len--;
4926        if (len >= DISK_NAME_LEN)
4927                return -E2BIG;
4928        strlcpy(buf, val, len+1);
4929        if (strncmp(buf, "md_", 3) != 0)
4930                return -EINVAL;
4931        return md_alloc(0, buf);
4932}
4933
4934static void md_safemode_timeout(unsigned long data)
4935{
4936        struct mddev *mddev = (struct mddev *) data;
4937
4938        if (!atomic_read(&mddev->writes_pending)) {
4939                mddev->safemode = 1;
4940                if (mddev->external)
4941                        sysfs_notify_dirent_safe(mddev->sysfs_state);
4942        }
4943        md_wakeup_thread(mddev->thread);
4944}
4945
4946static int start_dirty_degraded;
4947
4948int md_run(struct mddev *mddev)
4949{
4950        int err;
4951        struct md_rdev *rdev;
4952        struct md_personality *pers;
4953
4954        if (list_empty(&mddev->disks))
4955                /* cannot run an array with no devices.. */
4956                return -EINVAL;
4957
4958        if (mddev->pers)
4959                return -EBUSY;
4960        /* Cannot run until previous stop completes properly */
4961        if (mddev->sysfs_active)
4962                return -EBUSY;
4963
4964        /*
4965         * Analyze all RAID superblock(s)
4966         */
4967        if (!mddev->raid_disks) {
4968                if (!mddev->persistent)
4969                        return -EINVAL;
4970                analyze_sbs(mddev);
4971        }
4972
4973        if (mddev->level != LEVEL_NONE)
4974                request_module("md-level-%d", mddev->level);
4975        else if (mddev->clevel[0])
4976                request_module("md-%s", mddev->clevel);
4977
4978        /*
4979         * Drop all container device buffers, from now on
4980         * the only valid external interface is through the md
4981         * device.
4982         */
4983        rdev_for_each(rdev, mddev) {
4984                if (test_bit(Faulty, &rdev->flags))
4985                        continue;
4986                sync_blockdev(rdev->bdev);
4987                invalidate_bdev(rdev->bdev);
4988
4989                /* perform some consistency tests on the device.
4990                 * We don't want the data to overlap the metadata,
4991                 * Internal Bitmap issues have been handled elsewhere.
4992                 */
4993                if (rdev->meta_bdev) {
4994                        /* Nothing to check */;
4995                } else if (rdev->data_offset < rdev->sb_start) {
4996                        if (mddev->dev_sectors &&
4997                            rdev->data_offset + mddev->dev_sectors
4998                            > rdev->sb_start) {
4999                                printk("md: %s: data overlaps metadata\n",
5000                                       mdname(mddev));
5001                                return -EINVAL;
5002                        }
5003                } else {
5004                        if (rdev->sb_start + rdev->sb_size/512
5005                            > rdev->data_offset) {
5006                                printk("md: %s: metadata overlaps data\n",
5007                                       mdname(mddev));
5008                                return -EINVAL;
5009                        }
5010                }
5011                sysfs_notify_dirent_safe(rdev->sysfs_state);
5012        }
5013
5014        if (mddev->bio_set == NULL)
5015                mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
5016
5017        spin_lock(&pers_lock);
5018        pers = find_pers(mddev->level, mddev->clevel);
5019        if (!pers || !try_module_get(pers->owner)) {
5020                spin_unlock(&pers_lock);
5021                if (mddev->level != LEVEL_NONE)
5022                        printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5023                               mddev->level);
5024                else
5025                        printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5026                               mddev->clevel);
5027                return -EINVAL;
5028        }
5029        mddev->pers = pers;
5030        spin_unlock(&pers_lock);
5031        if (mddev->level != pers->level) {
5032                mddev->level = pers->level;
5033                mddev->new_level = pers->level;
5034        }
5035        strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5036
5037        if (mddev->reshape_position != MaxSector &&
5038            pers->start_reshape == NULL) {
5039                /* This personality cannot handle reshaping... */
5040                mddev->pers = NULL;
5041                module_put(pers->owner);
5042                return -EINVAL;
5043        }
5044
5045        if (pers->sync_request) {
5046                /* Warn if this is a potentially silly
5047                 * configuration.
5048                 */
5049                char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5050                struct md_rdev *rdev2;
5051                int warned = 0;
5052
5053                rdev_for_each(rdev, mddev)
5054                        rdev_for_each(rdev2, mddev) {
5055                                if (rdev < rdev2 &&
5056                                    rdev->bdev->bd_contains ==
5057                                    rdev2->bdev->bd_contains) {
5058                                        printk(KERN_WARNING
5059                                               "%s: WARNING: %s appears to be"
5060                                               " on the same physical disk as"
5061                                               " %s.\n",
5062                                               mdname(mddev),
5063                                               bdevname(rdev->bdev,b),
5064                                               bdevname(rdev2->bdev,b2));
5065                                        warned = 1;
5066                                }
5067                        }
5068
5069                if (warned)
5070                        printk(KERN_WARNING
5071                               "True protection against single-disk"
5072                               " failure might be compromised.\n");
5073        }
5074
5075        mddev->recovery = 0;
5076        /* may be over-ridden by personality */
5077        mddev->resync_max_sectors = mddev->dev_sectors;
5078
5079        mddev->ok_start_degraded = start_dirty_degraded;
5080
5081        if (start_readonly && mddev->ro == 0)
5082                mddev->ro = 2; /* read-only, but switch on first write */
5083
5084        err = mddev->pers->run(mddev);
5085        if (err)
5086                printk(KERN_ERR "md: pers->run() failed ...\n");
5087        else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5088                WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5089                          " but 'external_size' not in effect?\n", __func__);
5090                printk(KERN_ERR
5091                       "md: invalid array_size %llu > default size %llu\n",
5092                       (unsigned long long)mddev->array_sectors / 2,
5093                       (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5094                err = -EINVAL;
5095                mddev->pers->stop(mddev);
5096        }
5097        if (err == 0 && mddev->pers->sync_request &&
5098            (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5099                err = bitmap_create(mddev);
5100                if (err) {
5101                        printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5102                               mdname(mddev), err);
5103                        mddev->pers->stop(mddev);
5104                }
5105        }
5106        if (err) {
5107                module_put(mddev->pers->owner);
5108                mddev->pers = NULL;
5109                bitmap_destroy(mddev);
5110                return err;
5111        }
5112        if (mddev->pers->sync_request) {
5113                if (mddev->kobj.sd &&
5114                    sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5115                        printk(KERN_WARNING
5116                               "md: cannot register extra attributes for %s\n",
5117                               mdname(mddev));
5118                mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5119        } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5120                mddev->ro = 0;
5121
5122        atomic_set(&mddev->writes_pending,0);
5123        atomic_set(&mddev->max_corr_read_errors,
5124                   MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5125        mddev->safemode = 0;
5126        mddev->safemode_timer.function = md_safemode_timeout;
5127        mddev->safemode_timer.data = (unsigned long) mddev;
5128        mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5129        mddev->in_sync = 1;
5130        smp_wmb();
5131        mddev->ready = 1;
5132        rdev_for_each(rdev, mddev)
5133                if (rdev->raid_disk >= 0)
5134                        if (sysfs_link_rdev(mddev, rdev))
5135                                /* failure here is OK */;
5136        
5137        set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5138        
5139        if (mddev->flags)
5140                md_update_sb(mddev, 0);
5141
5142        md_new_event(mddev);
5143        sysfs_notify_dirent_safe(mddev->sysfs_state);
5144        sysfs_notify_dirent_safe(mddev->sysfs_action);
5145        sysfs_notify(&mddev->kobj, NULL, "degraded");
5146        return 0;
5147}
5148EXPORT_SYMBOL_GPL(md_run);
5149
5150static int do_md_run(struct mddev *mddev)
5151{
5152        int err;
5153
5154        err = md_run(mddev);
5155        if (err)
5156                goto out;
5157        err = bitmap_load(mddev);
5158        if (err) {
5159                bitmap_destroy(mddev);
5160                goto out;
5161        }
5162
5163        md_wakeup_thread(mddev->thread);
5164        md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5165
5166        set_capacity(mddev->gendisk, mddev->array_sectors);
5167        revalidate_disk(mddev->gendisk);
5168        mddev->changed = 1;
5169        kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5170out:
5171        return err;
5172}
5173
5174static int restart_array(struct mddev *mddev)
5175{
5176        struct gendisk *disk = mddev->gendisk;
5177
5178        /* Complain if it has no devices */
5179        if (list_empty(&mddev->disks))
5180                return -ENXIO;
5181        if (!mddev->pers)
5182                return -EINVAL;
5183        if (!mddev->ro)
5184                return -EBUSY;
5185        mddev->safemode = 0;
5186        mddev->ro = 0;
5187        set_disk_ro(disk, 0);
5188        printk(KERN_INFO "md: %s switched to read-write mode.\n",
5189                mdname(mddev));
5190        /* Kick recovery or resync if necessary */
5191        set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5192        md_wakeup_thread(mddev->thread);
5193        md_wakeup_thread(mddev->sync_thread);
5194        sysfs_notify_dirent_safe(mddev->sysfs_state);
5195        return 0;
5196