Root/drivers/md/md.c

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