Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6] / 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/raid/md.h>
37 #include <linux/raid/bitmap.h>
38 #include <linux/sysctl.h>
39 #include <linux/buffer_head.h> /* for invalidate_bdev */
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/hdreg.h>
43 #include <linux/proc_fs.h>
44 #include <linux/random.h>
45 #include <linux/reboot.h>
46 #include <linux/file.h>
47 #include <linux/delay.h>
48
49 #define MAJOR_NR MD_MAJOR
50
51 /* 63 partitions with the alternate major number (mdp) */
52 #define MdpMinorShift 6
53
54 #define DEBUG 0
55 #define dprintk(x...) ((void)(DEBUG && printk(x)))
56
57
58 #ifndef MODULE
59 static void autostart_arrays(int part);
60 #endif
61
62 static LIST_HEAD(pers_list);
63 static DEFINE_SPINLOCK(pers_lock);
64
65 static void md_print_devices(void);
66
67 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
68
69 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
70
71 /*
72  * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
73  * is 1000 KB/sec, so the extra system load does not show up that much.
74  * Increase it if you want to have more _guaranteed_ speed. Note that
75  * the RAID driver will use the maximum available bandwidth if the IO
76  * subsystem is idle. There is also an 'absolute maximum' reconstruction
77  * speed limit - in case reconstruction slows down your system despite
78  * idle IO detection.
79  *
80  * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
81  * or /sys/block/mdX/md/sync_speed_{min,max}
82  */
83
84 static int sysctl_speed_limit_min = 1000;
85 static int sysctl_speed_limit_max = 200000;
86 static inline int speed_min(mddev_t *mddev)
87 {
88         return mddev->sync_speed_min ?
89                 mddev->sync_speed_min : sysctl_speed_limit_min;
90 }
91
92 static inline int speed_max(mddev_t *mddev)
93 {
94         return mddev->sync_speed_max ?
95                 mddev->sync_speed_max : sysctl_speed_limit_max;
96 }
97
98 static struct ctl_table_header *raid_table_header;
99
100 static ctl_table raid_table[] = {
101         {
102                 .ctl_name       = DEV_RAID_SPEED_LIMIT_MIN,
103                 .procname       = "speed_limit_min",
104                 .data           = &sysctl_speed_limit_min,
105                 .maxlen         = sizeof(int),
106                 .mode           = S_IRUGO|S_IWUSR,
107                 .proc_handler   = &proc_dointvec,
108         },
109         {
110                 .ctl_name       = DEV_RAID_SPEED_LIMIT_MAX,
111                 .procname       = "speed_limit_max",
112                 .data           = &sysctl_speed_limit_max,
113                 .maxlen         = sizeof(int),
114                 .mode           = S_IRUGO|S_IWUSR,
115                 .proc_handler   = &proc_dointvec,
116         },
117         { .ctl_name = 0 }
118 };
119
120 static ctl_table raid_dir_table[] = {
121         {
122                 .ctl_name       = DEV_RAID,
123                 .procname       = "raid",
124                 .maxlen         = 0,
125                 .mode           = S_IRUGO|S_IXUGO,
126                 .child          = raid_table,
127         },
128         { .ctl_name = 0 }
129 };
130
131 static ctl_table raid_root_table[] = {
132         {
133                 .ctl_name       = CTL_DEV,
134                 .procname       = "dev",
135                 .maxlen         = 0,
136                 .mode           = 0555,
137                 .child          = raid_dir_table,
138         },
139         { .ctl_name = 0 }
140 };
141
142 static struct block_device_operations md_fops;
143
144 static int start_readonly;
145
146 /*
147  * We have a system wide 'event count' that is incremented
148  * on any 'interesting' event, and readers of /proc/mdstat
149  * can use 'poll' or 'select' to find out when the event
150  * count increases.
151  *
152  * Events are:
153  *  start array, stop array, error, add device, remove device,
154  *  start build, activate spare
155  */
156 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
157 static atomic_t md_event_count;
158 void md_new_event(mddev_t *mddev)
159 {
160         atomic_inc(&md_event_count);
161         wake_up(&md_event_waiters);
162 }
163 EXPORT_SYMBOL_GPL(md_new_event);
164
165 /* Alternate version that can be called from interrupts
166  * when calling sysfs_notify isn't needed.
167  */
168 static void md_new_event_inintr(mddev_t *mddev)
169 {
170         atomic_inc(&md_event_count);
171         wake_up(&md_event_waiters);
172 }
173
174 /*
175  * Enables to iterate over all existing md arrays
176  * all_mddevs_lock protects this list.
177  */
178 static LIST_HEAD(all_mddevs);
179 static DEFINE_SPINLOCK(all_mddevs_lock);
180
181
182 /*
183  * iterates through all used mddevs in the system.
184  * We take care to grab the all_mddevs_lock whenever navigating
185  * the list, and to always hold a refcount when unlocked.
186  * Any code which breaks out of this loop while own
187  * a reference to the current mddev and must mddev_put it.
188  */
189 #define for_each_mddev(mddev,tmp)                                       \
190                                                                         \
191         for (({ spin_lock(&all_mddevs_lock);                            \
192                 tmp = all_mddevs.next;                                  \
193                 mddev = NULL;});                                        \
194              ({ if (tmp != &all_mddevs)                                 \
195                         mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
196                 spin_unlock(&all_mddevs_lock);                          \
197                 if (mddev) mddev_put(mddev);                            \
198                 mddev = list_entry(tmp, mddev_t, all_mddevs);           \
199                 tmp != &all_mddevs;});                                  \
200              ({ spin_lock(&all_mddevs_lock);                            \
201                 tmp = tmp->next;})                                      \
202                 )
203
204
205 static int md_fail_request(struct request_queue *q, struct bio *bio)
206 {
207         bio_io_error(bio);
208         return 0;
209 }
210
211 static inline mddev_t *mddev_get(mddev_t *mddev)
212 {
213         atomic_inc(&mddev->active);
214         return mddev;
215 }
216
217 static void mddev_delayed_delete(struct work_struct *ws);
218
219 static void mddev_put(mddev_t *mddev)
220 {
221         if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
222                 return;
223         if (!mddev->raid_disks && list_empty(&mddev->disks) &&
224             !mddev->hold_active) {
225                 list_del(&mddev->all_mddevs);
226                 if (mddev->gendisk) {
227                         /* we did a probe so need to clean up.
228                          * Call schedule_work inside the spinlock
229                          * so that flush_scheduled_work() after
230                          * mddev_find will succeed in waiting for the
231                          * work to be done.
232                          */
233                         INIT_WORK(&mddev->del_work, mddev_delayed_delete);
234                         schedule_work(&mddev->del_work);
235                 } else
236                         kfree(mddev);
237         }
238         spin_unlock(&all_mddevs_lock);
239 }
240
241 static mddev_t * mddev_find(dev_t unit)
242 {
243         mddev_t *mddev, *new = NULL;
244
245  retry:
246         spin_lock(&all_mddevs_lock);
247
248         if (unit) {
249                 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
250                         if (mddev->unit == unit) {
251                                 mddev_get(mddev);
252                                 spin_unlock(&all_mddevs_lock);
253                                 kfree(new);
254                                 return mddev;
255                         }
256
257                 if (new) {
258                         list_add(&new->all_mddevs, &all_mddevs);
259                         spin_unlock(&all_mddevs_lock);
260                         new->hold_active = UNTIL_IOCTL;
261                         return new;
262                 }
263         } else if (new) {
264                 /* find an unused unit number */
265                 static int next_minor = 512;
266                 int start = next_minor;
267                 int is_free = 0;
268                 int dev = 0;
269                 while (!is_free) {
270                         dev = MKDEV(MD_MAJOR, next_minor);
271                         next_minor++;
272                         if (next_minor > MINORMASK)
273                                 next_minor = 0;
274                         if (next_minor == start) {
275                                 /* Oh dear, all in use. */
276                                 spin_unlock(&all_mddevs_lock);
277                                 kfree(new);
278                                 return NULL;
279                         }
280                                 
281                         is_free = 1;
282                         list_for_each_entry(mddev, &all_mddevs, all_mddevs)
283                                 if (mddev->unit == dev) {
284                                         is_free = 0;
285                                         break;
286                                 }
287                 }
288                 new->unit = dev;
289                 new->md_minor = MINOR(dev);
290                 new->hold_active = UNTIL_STOP;
291                 list_add(&new->all_mddevs, &all_mddevs);
292                 spin_unlock(&all_mddevs_lock);
293                 return new;
294         }
295         spin_unlock(&all_mddevs_lock);
296
297         new = kzalloc(sizeof(*new), GFP_KERNEL);
298         if (!new)
299                 return NULL;
300
301         new->unit = unit;
302         if (MAJOR(unit) == MD_MAJOR)
303                 new->md_minor = MINOR(unit);
304         else
305                 new->md_minor = MINOR(unit) >> MdpMinorShift;
306
307         mutex_init(&new->reconfig_mutex);
308         INIT_LIST_HEAD(&new->disks);
309         INIT_LIST_HEAD(&new->all_mddevs);
310         init_timer(&new->safemode_timer);
311         atomic_set(&new->active, 1);
312         atomic_set(&new->openers, 0);
313         spin_lock_init(&new->write_lock);
314         init_waitqueue_head(&new->sb_wait);
315         init_waitqueue_head(&new->recovery_wait);
316         new->reshape_position = MaxSector;
317         new->resync_min = 0;
318         new->resync_max = MaxSector;
319         new->level = LEVEL_NONE;
320
321         goto retry;
322 }
323
324 static inline int mddev_lock(mddev_t * mddev)
325 {
326         return mutex_lock_interruptible(&mddev->reconfig_mutex);
327 }
328
329 static inline int mddev_trylock(mddev_t * mddev)
330 {
331         return mutex_trylock(&mddev->reconfig_mutex);
332 }
333
334 static inline void mddev_unlock(mddev_t * mddev)
335 {
336         mutex_unlock(&mddev->reconfig_mutex);
337
338         md_wakeup_thread(mddev->thread);
339 }
340
341 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
342 {
343         mdk_rdev_t *rdev;
344
345         list_for_each_entry(rdev, &mddev->disks, same_set)
346                 if (rdev->desc_nr == nr)
347                         return rdev;
348
349         return NULL;
350 }
351
352 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
353 {
354         mdk_rdev_t *rdev;
355
356         list_for_each_entry(rdev, &mddev->disks, same_set)
357                 if (rdev->bdev->bd_dev == dev)
358                         return rdev;
359
360         return NULL;
361 }
362
363 static struct mdk_personality *find_pers(int level, char *clevel)
364 {
365         struct mdk_personality *pers;
366         list_for_each_entry(pers, &pers_list, list) {
367                 if (level != LEVEL_NONE && pers->level == level)
368                         return pers;
369                 if (strcmp(pers->name, clevel)==0)
370                         return pers;
371         }
372         return NULL;
373 }
374
375 /* return the offset of the super block in 512byte sectors */
376 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
377 {
378         sector_t num_sectors = bdev->bd_inode->i_size / 512;
379         return MD_NEW_SIZE_SECTORS(num_sectors);
380 }
381
382 static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
383 {
384         sector_t num_sectors = rdev->sb_start;
385
386         if (chunk_size)
387                 num_sectors &= ~((sector_t)chunk_size/512 - 1);
388         return num_sectors;
389 }
390
391 static int alloc_disk_sb(mdk_rdev_t * rdev)
392 {
393         if (rdev->sb_page)
394                 MD_BUG();
395
396         rdev->sb_page = alloc_page(GFP_KERNEL);
397         if (!rdev->sb_page) {
398                 printk(KERN_ALERT "md: out of memory.\n");
399                 return -ENOMEM;
400         }
401
402         return 0;
403 }
404
405 static void free_disk_sb(mdk_rdev_t * rdev)
406 {
407         if (rdev->sb_page) {
408                 put_page(rdev->sb_page);
409                 rdev->sb_loaded = 0;
410                 rdev->sb_page = NULL;
411                 rdev->sb_start = 0;
412                 rdev->size = 0;
413         }
414 }
415
416
417 static void super_written(struct bio *bio, int error)
418 {
419         mdk_rdev_t *rdev = bio->bi_private;
420         mddev_t *mddev = rdev->mddev;
421
422         if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
423                 printk("md: super_written gets error=%d, uptodate=%d\n",
424                        error, test_bit(BIO_UPTODATE, &bio->bi_flags));
425                 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
426                 md_error(mddev, rdev);
427         }
428
429         if (atomic_dec_and_test(&mddev->pending_writes))
430                 wake_up(&mddev->sb_wait);
431         bio_put(bio);
432 }
433
434 static void super_written_barrier(struct bio *bio, int error)
435 {
436         struct bio *bio2 = bio->bi_private;
437         mdk_rdev_t *rdev = bio2->bi_private;
438         mddev_t *mddev = rdev->mddev;
439
440         if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
441             error == -EOPNOTSUPP) {
442                 unsigned long flags;
443                 /* barriers don't appear to be supported :-( */
444                 set_bit(BarriersNotsupp, &rdev->flags);
445                 mddev->barriers_work = 0;
446                 spin_lock_irqsave(&mddev->write_lock, flags);
447                 bio2->bi_next = mddev->biolist;
448                 mddev->biolist = bio2;
449                 spin_unlock_irqrestore(&mddev->write_lock, flags);
450                 wake_up(&mddev->sb_wait);
451                 bio_put(bio);
452         } else {
453                 bio_put(bio2);
454                 bio->bi_private = rdev;
455                 super_written(bio, error);
456         }
457 }
458
459 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
460                    sector_t sector, int size, struct page *page)
461 {
462         /* write first size bytes of page to sector of rdev
463          * Increment mddev->pending_writes before returning
464          * and decrement it on completion, waking up sb_wait
465          * if zero is reached.
466          * If an error occurred, call md_error
467          *
468          * As we might need to resubmit the request if BIO_RW_BARRIER
469          * causes ENOTSUPP, we allocate a spare bio...
470          */
471         struct bio *bio = bio_alloc(GFP_NOIO, 1);
472         int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
473
474         bio->bi_bdev = rdev->bdev;
475         bio->bi_sector = sector;
476         bio_add_page(bio, page, size, 0);
477         bio->bi_private = rdev;
478         bio->bi_end_io = super_written;
479         bio->bi_rw = rw;
480
481         atomic_inc(&mddev->pending_writes);
482         if (!test_bit(BarriersNotsupp, &rdev->flags)) {
483                 struct bio *rbio;
484                 rw |= (1<<BIO_RW_BARRIER);
485                 rbio = bio_clone(bio, GFP_NOIO);
486                 rbio->bi_private = bio;
487                 rbio->bi_end_io = super_written_barrier;
488                 submit_bio(rw, rbio);
489         } else
490                 submit_bio(rw, bio);
491 }
492
493 void md_super_wait(mddev_t *mddev)
494 {
495         /* wait for all superblock writes that were scheduled to complete.
496          * if any had to be retried (due to BARRIER problems), retry them
497          */
498         DEFINE_WAIT(wq);
499         for(;;) {
500                 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
501                 if (atomic_read(&mddev->pending_writes)==0)
502                         break;
503                 while (mddev->biolist) {
504                         struct bio *bio;
505                         spin_lock_irq(&mddev->write_lock);
506                         bio = mddev->biolist;
507                         mddev->biolist = bio->bi_next ;
508                         bio->bi_next = NULL;
509                         spin_unlock_irq(&mddev->write_lock);
510                         submit_bio(bio->bi_rw, bio);
511                 }
512                 schedule();
513         }
514         finish_wait(&mddev->sb_wait, &wq);
515 }
516
517 static void bi_complete(struct bio *bio, int error)
518 {
519         complete((struct completion*)bio->bi_private);
520 }
521
522 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
523                    struct page *page, int rw)
524 {
525         struct bio *bio = bio_alloc(GFP_NOIO, 1);
526         struct completion event;
527         int ret;
528
529         rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
530
531         bio->bi_bdev = bdev;
532         bio->bi_sector = sector;
533         bio_add_page(bio, page, size, 0);
534         init_completion(&event);
535         bio->bi_private = &event;
536         bio->bi_end_io = bi_complete;
537         submit_bio(rw, bio);
538         wait_for_completion(&event);
539
540         ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
541         bio_put(bio);
542         return ret;
543 }
544 EXPORT_SYMBOL_GPL(sync_page_io);
545
546 static int read_disk_sb(mdk_rdev_t * rdev, int size)
547 {
548         char b[BDEVNAME_SIZE];
549         if (!rdev->sb_page) {
550                 MD_BUG();
551                 return -EINVAL;
552         }
553         if (rdev->sb_loaded)
554                 return 0;
555
556
557         if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
558                 goto fail;
559         rdev->sb_loaded = 1;
560         return 0;
561
562 fail:
563         printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
564                 bdevname(rdev->bdev,b));
565         return -EINVAL;
566 }
567
568 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
569 {
570         return  sb1->set_uuid0 == sb2->set_uuid0 &&
571                 sb1->set_uuid1 == sb2->set_uuid1 &&
572                 sb1->set_uuid2 == sb2->set_uuid2 &&
573                 sb1->set_uuid3 == sb2->set_uuid3;
574 }
575
576 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
577 {
578         int ret;
579         mdp_super_t *tmp1, *tmp2;
580
581         tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
582         tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
583
584         if (!tmp1 || !tmp2) {
585                 ret = 0;
586                 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
587                 goto abort;
588         }
589
590         *tmp1 = *sb1;
591         *tmp2 = *sb2;
592
593         /*
594          * nr_disks is not constant
595          */
596         tmp1->nr_disks = 0;
597         tmp2->nr_disks = 0;
598
599         ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
600 abort:
601         kfree(tmp1);
602         kfree(tmp2);
603         return ret;
604 }
605
606
607 static u32 md_csum_fold(u32 csum)
608 {
609         csum = (csum & 0xffff) + (csum >> 16);
610         return (csum & 0xffff) + (csum >> 16);
611 }
612
613 static unsigned int calc_sb_csum(mdp_super_t * sb)
614 {
615         u64 newcsum = 0;
616         u32 *sb32 = (u32*)sb;
617         int i;
618         unsigned int disk_csum, csum;
619
620         disk_csum = sb->sb_csum;
621         sb->sb_csum = 0;
622
623         for (i = 0; i < MD_SB_BYTES/4 ; i++)
624                 newcsum += sb32[i];
625         csum = (newcsum & 0xffffffff) + (newcsum>>32);
626
627
628 #ifdef CONFIG_ALPHA
629         /* This used to use csum_partial, which was wrong for several
630          * reasons including that different results are returned on
631          * different architectures.  It isn't critical that we get exactly
632          * the same return value as before (we always csum_fold before
633          * testing, and that removes any differences).  However as we
634          * know that csum_partial always returned a 16bit value on
635          * alphas, do a fold to maximise conformity to previous behaviour.
636          */
637         sb->sb_csum = md_csum_fold(disk_csum);
638 #else
639         sb->sb_csum = disk_csum;
640 #endif
641         return csum;
642 }
643
644
645 /*
646  * Handle superblock details.
647  * We want to be able to handle multiple superblock formats
648  * so we have a common interface to them all, and an array of
649  * different handlers.
650  * We rely on user-space to write the initial superblock, and support
651  * reading and updating of superblocks.
652  * Interface methods are:
653  *   int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
654  *      loads and validates a superblock on dev.
655  *      if refdev != NULL, compare superblocks on both devices
656  *    Return:
657  *      0 - dev has a superblock that is compatible with refdev
658  *      1 - dev has a superblock that is compatible and newer than refdev
659  *          so dev should be used as the refdev in future
660  *     -EINVAL superblock incompatible or invalid
661  *     -othererror e.g. -EIO
662  *
663  *   int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
664  *      Verify that dev is acceptable into mddev.
665  *       The first time, mddev->raid_disks will be 0, and data from
666  *       dev should be merged in.  Subsequent calls check that dev
667  *       is new enough.  Return 0 or -EINVAL
668  *
669  *   void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
670  *     Update the superblock for rdev with data in mddev
671  *     This does not write to disc.
672  *
673  */
674
675 struct super_type  {
676         char                *name;
677         struct module       *owner;
678         int                 (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
679                                           int minor_version);
680         int                 (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
681         void                (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
682         unsigned long long  (*rdev_size_change)(mdk_rdev_t *rdev,
683                                                 sector_t num_sectors);
684 };
685
686 /*
687  * load_super for 0.90.0 
688  */
689 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
690 {
691         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
692         mdp_super_t *sb;
693         int ret;
694
695         /*
696          * Calculate the position of the superblock (512byte sectors),
697          * it's at the end of the disk.
698          *
699          * It also happens to be a multiple of 4Kb.
700          */
701         rdev->sb_start = calc_dev_sboffset(rdev->bdev);
702
703         ret = read_disk_sb(rdev, MD_SB_BYTES);
704         if (ret) return ret;
705
706         ret = -EINVAL;
707
708         bdevname(rdev->bdev, b);
709         sb = (mdp_super_t*)page_address(rdev->sb_page);
710
711         if (sb->md_magic != MD_SB_MAGIC) {
712                 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
713                        b);
714                 goto abort;
715         }
716
717         if (sb->major_version != 0 ||
718             sb->minor_version < 90 ||
719             sb->minor_version > 91) {
720                 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
721                         sb->major_version, sb->minor_version,
722                         b);
723                 goto abort;
724         }
725
726         if (sb->raid_disks <= 0)
727                 goto abort;
728
729         if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
730                 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
731                         b);
732                 goto abort;
733         }
734
735         rdev->preferred_minor = sb->md_minor;
736         rdev->data_offset = 0;
737         rdev->sb_size = MD_SB_BYTES;
738
739         if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
740                 if (sb->level != 1 && sb->level != 4
741                     && sb->level != 5 && sb->level != 6
742                     && sb->level != 10) {
743                         /* FIXME use a better test */
744                         printk(KERN_WARNING
745                                "md: bitmaps not supported for this level.\n");
746                         goto abort;
747                 }
748         }
749
750         if (sb->level == LEVEL_MULTIPATH)
751                 rdev->desc_nr = -1;
752         else
753                 rdev->desc_nr = sb->this_disk.number;
754
755         if (!refdev) {
756                 ret = 1;
757         } else {
758                 __u64 ev1, ev2;
759                 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
760                 if (!uuid_equal(refsb, sb)) {
761                         printk(KERN_WARNING "md: %s has different UUID to %s\n",
762                                 b, bdevname(refdev->bdev,b2));
763                         goto abort;
764                 }
765                 if (!sb_equal(refsb, sb)) {
766                         printk(KERN_WARNING "md: %s has same UUID"
767                                " but different superblock to %s\n",
768                                b, bdevname(refdev->bdev, b2));
769                         goto abort;
770                 }
771                 ev1 = md_event(sb);
772                 ev2 = md_event(refsb);
773                 if (ev1 > ev2)
774                         ret = 1;
775                 else 
776                         ret = 0;
777         }
778         rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
779
780         if (rdev->size < sb->size && sb->level > 1)
781                 /* "this cannot possibly happen" ... */
782                 ret = -EINVAL;
783
784  abort:
785         return ret;
786 }
787
788 /*
789  * validate_super for 0.90.0
790  */
791 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
792 {
793         mdp_disk_t *desc;
794         mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
795         __u64 ev1 = md_event(sb);
796
797         rdev->raid_disk = -1;
798         clear_bit(Faulty, &rdev->flags);
799         clear_bit(In_sync, &rdev->flags);
800         clear_bit(WriteMostly, &rdev->flags);
801         clear_bit(BarriersNotsupp, &rdev->flags);
802
803         if (mddev->raid_disks == 0) {
804                 mddev->major_version = 0;
805                 mddev->minor_version = sb->minor_version;
806                 mddev->patch_version = sb->patch_version;
807                 mddev->external = 0;
808                 mddev->chunk_size = sb->chunk_size;
809                 mddev->ctime = sb->ctime;
810                 mddev->utime = sb->utime;
811                 mddev->level = sb->level;
812                 mddev->clevel[0] = 0;
813                 mddev->layout = sb->layout;
814                 mddev->raid_disks = sb->raid_disks;
815                 mddev->size = sb->size;
816                 mddev->events = ev1;
817                 mddev->bitmap_offset = 0;
818                 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
819
820                 if (mddev->minor_version >= 91) {
821                         mddev->reshape_position = sb->reshape_position;
822                         mddev->delta_disks = sb->delta_disks;
823                         mddev->new_level = sb->new_level;
824                         mddev->new_layout = sb->new_layout;
825                         mddev->new_chunk = sb->new_chunk;
826                 } else {
827                         mddev->reshape_position = MaxSector;
828                         mddev->delta_disks = 0;
829                         mddev->new_level = mddev->level;
830                         mddev->new_layout = mddev->layout;
831                         mddev->new_chunk = mddev->chunk_size;
832                 }
833
834                 if (sb->state & (1<<MD_SB_CLEAN))
835                         mddev->recovery_cp = MaxSector;
836                 else {
837                         if (sb->events_hi == sb->cp_events_hi && 
838                                 sb->events_lo == sb->cp_events_lo) {
839                                 mddev->recovery_cp = sb->recovery_cp;
840                         } else
841                                 mddev->recovery_cp = 0;
842                 }
843
844                 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
845                 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
846                 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
847                 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
848
849                 mddev->max_disks = MD_SB_DISKS;
850
851                 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
852                     mddev->bitmap_file == NULL)
853                         mddev->bitmap_offset = mddev->default_bitmap_offset;
854
855         } else if (mddev->pers == NULL) {
856                 /* Insist on good event counter while assembling */
857                 ++ev1;
858                 if (ev1 < mddev->events) 
859                         return -EINVAL;
860         } else if (mddev->bitmap) {
861                 /* if adding to array with a bitmap, then we can accept an
862                  * older device ... but not too old.
863                  */
864                 if (ev1 < mddev->bitmap->events_cleared)
865                         return 0;
866         } else {
867                 if (ev1 < mddev->events)
868                         /* just a hot-add of a new device, leave raid_disk at -1 */
869                         return 0;
870         }
871
872         if (mddev->level != LEVEL_MULTIPATH) {
873                 desc = sb->disks + rdev->desc_nr;
874
875                 if (desc->state & (1<<MD_DISK_FAULTY))
876                         set_bit(Faulty, &rdev->flags);
877                 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
878                             desc->raid_disk < mddev->raid_disks */) {
879                         set_bit(In_sync, &rdev->flags);
880                         rdev->raid_disk = desc->raid_disk;
881                 }
882                 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
883                         set_bit(WriteMostly, &rdev->flags);
884         } else /* MULTIPATH are always insync */
885                 set_bit(In_sync, &rdev->flags);
886         return 0;
887 }
888
889 /*
890  * sync_super for 0.90.0
891  */
892 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
893 {
894         mdp_super_t *sb;
895         mdk_rdev_t *rdev2;
896         int next_spare = mddev->raid_disks;
897
898
899         /* make rdev->sb match mddev data..
900          *
901          * 1/ zero out disks
902          * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
903          * 3/ any empty disks < next_spare become removed
904          *
905          * disks[0] gets initialised to REMOVED because
906          * we cannot be sure from other fields if it has
907          * been initialised or not.
908          */
909         int i;
910         int active=0, working=0,failed=0,spare=0,nr_disks=0;
911
912         rdev->sb_size = MD_SB_BYTES;
913
914         sb = (mdp_super_t*)page_address(rdev->sb_page);
915
916         memset(sb, 0, sizeof(*sb));
917
918         sb->md_magic = MD_SB_MAGIC;
919         sb->major_version = mddev->major_version;
920         sb->patch_version = mddev->patch_version;
921         sb->gvalid_words  = 0; /* ignored */
922         memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
923         memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
924         memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
925         memcpy(&sb->set_uuid3, mddev->uuid+12,4);
926
927         sb->ctime = mddev->ctime;
928         sb->level = mddev->level;
929         sb->size  = mddev->size;
930         sb->raid_disks = mddev->raid_disks;
931         sb->md_minor = mddev->md_minor;
932         sb->not_persistent = 0;
933         sb->utime = mddev->utime;
934         sb->state = 0;
935         sb->events_hi = (mddev->events>>32);
936         sb->events_lo = (u32)mddev->events;
937
938         if (mddev->reshape_position == MaxSector)
939                 sb->minor_version = 90;
940         else {
941                 sb->minor_version = 91;
942                 sb->reshape_position = mddev->reshape_position;
943                 sb->new_level = mddev->new_level;
944                 sb->delta_disks = mddev->delta_disks;
945                 sb->new_layout = mddev->new_layout;
946                 sb->new_chunk = mddev->new_chunk;
947         }
948         mddev->minor_version = sb->minor_version;
949         if (mddev->in_sync)
950         {
951                 sb->recovery_cp = mddev->recovery_cp;
952                 sb->cp_events_hi = (mddev->events>>32);
953                 sb->cp_events_lo = (u32)mddev->events;
954                 if (mddev->recovery_cp == MaxSector)
955                         sb->state = (1<< MD_SB_CLEAN);
956         } else
957                 sb->recovery_cp = 0;
958
959         sb->layout = mddev->layout;
960         sb->chunk_size = mddev->chunk_size;
961
962         if (mddev->bitmap && mddev->bitmap_file == NULL)
963                 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
964
965         sb->disks[0].state = (1<<MD_DISK_REMOVED);
966         list_for_each_entry(rdev2, &mddev->disks, same_set) {
967                 mdp_disk_t *d;
968                 int desc_nr;
969                 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
970                     && !test_bit(Faulty, &rdev2->flags))
971                         desc_nr = rdev2->raid_disk;
972                 else
973                         desc_nr = next_spare++;
974                 rdev2->desc_nr = desc_nr;
975                 d = &sb->disks[rdev2->desc_nr];
976                 nr_disks++;
977                 d->number = rdev2->desc_nr;
978                 d->major = MAJOR(rdev2->bdev->bd_dev);
979                 d->minor = MINOR(rdev2->bdev->bd_dev);
980                 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
981                     && !test_bit(Faulty, &rdev2->flags))
982                         d->raid_disk = rdev2->raid_disk;
983                 else
984                         d->raid_disk = rdev2->desc_nr; /* compatibility */
985                 if (test_bit(Faulty, &rdev2->flags))
986                         d->state = (1<<MD_DISK_FAULTY);
987                 else if (test_bit(In_sync, &rdev2->flags)) {
988                         d->state = (1<<MD_DISK_ACTIVE);
989                         d->state |= (1<<MD_DISK_SYNC);
990                         active++;
991                         working++;
992                 } else {
993                         d->state = 0;
994                         spare++;
995                         working++;
996                 }
997                 if (test_bit(WriteMostly, &rdev2->flags))
998                         d->state |= (1<<MD_DISK_WRITEMOSTLY);
999         }
1000         /* now set the "removed" and "faulty" bits on any missing devices */
1001         for (i=0 ; i < mddev->raid_disks ; i++) {
1002                 mdp_disk_t *d = &sb->disks[i];
1003                 if (d->state == 0 && d->number == 0) {
1004                         d->number = i;
1005                         d->raid_disk = i;
1006                         d->state = (1<<MD_DISK_REMOVED);
1007                         d->state |= (1<<MD_DISK_FAULTY);
1008                         failed++;
1009                 }
1010         }
1011         sb->nr_disks = nr_disks;
1012         sb->active_disks = active;
1013         sb->working_disks = working;
1014         sb->failed_disks = failed;
1015         sb->spare_disks = spare;
1016
1017         sb->this_disk = sb->disks[rdev->desc_nr];
1018         sb->sb_csum = calc_sb_csum(sb);
1019 }
1020
1021 /*
1022  * rdev_size_change for 0.90.0
1023  */
1024 static unsigned long long
1025 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1026 {
1027         if (num_sectors && num_sectors < rdev->mddev->size * 2)
1028                 return 0; /* component must fit device */
1029         if (rdev->mddev->bitmap_offset)
1030                 return 0; /* can't move bitmap */
1031         rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1032         if (!num_sectors || num_sectors > rdev->sb_start)
1033                 num_sectors = rdev->sb_start;
1034         md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1035                        rdev->sb_page);
1036         md_super_wait(rdev->mddev);
1037         return num_sectors / 2; /* kB for sysfs */
1038 }
1039
1040
1041 /*
1042  * version 1 superblock
1043  */
1044
1045 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1046 {
1047         __le32 disk_csum;
1048         u32 csum;
1049         unsigned long long newcsum;
1050         int size = 256 + le32_to_cpu(sb->max_dev)*2;
1051         __le32 *isuper = (__le32*)sb;
1052         int i;
1053
1054         disk_csum = sb->sb_csum;
1055         sb->sb_csum = 0;
1056         newcsum = 0;
1057         for (i=0; size>=4; size -= 4 )
1058                 newcsum += le32_to_cpu(*isuper++);
1059
1060         if (size == 2)
1061                 newcsum += le16_to_cpu(*(__le16*) isuper);
1062
1063         csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1064         sb->sb_csum = disk_csum;
1065         return cpu_to_le32(csum);
1066 }
1067
1068 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1069 {
1070         struct mdp_superblock_1 *sb;
1071         int ret;
1072         sector_t sb_start;
1073         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1074         int bmask;
1075
1076         /*
1077          * Calculate the position of the superblock in 512byte sectors.
1078          * It is always aligned to a 4K boundary and
1079          * depeding on minor_version, it can be:
1080          * 0: At least 8K, but less than 12K, from end of device
1081          * 1: At start of device
1082          * 2: 4K from start of device.
1083          */
1084         switch(minor_version) {
1085         case 0:
1086                 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1087                 sb_start -= 8*2;
1088                 sb_start &= ~(sector_t)(4*2-1);
1089                 break;
1090         case 1:
1091                 sb_start = 0;
1092                 break;
1093         case 2:
1094                 sb_start = 8;
1095                 break;
1096         default:
1097                 return -EINVAL;
1098         }
1099         rdev->sb_start = sb_start;
1100
1101         /* superblock is rarely larger than 1K, but it can be larger,
1102          * and it is safe to read 4k, so we do that
1103          */
1104         ret = read_disk_sb(rdev, 4096);
1105         if (ret) return ret;
1106
1107
1108         sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1109
1110         if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1111             sb->major_version != cpu_to_le32(1) ||
1112             le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1113             le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1114             (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1115                 return -EINVAL;
1116
1117         if (calc_sb_1_csum(sb) != sb->sb_csum) {
1118                 printk("md: invalid superblock checksum on %s\n",
1119                         bdevname(rdev->bdev,b));
1120                 return -EINVAL;
1121         }
1122         if (le64_to_cpu(sb->data_size) < 10) {
1123                 printk("md: data_size too small on %s\n",
1124                        bdevname(rdev->bdev,b));
1125                 return -EINVAL;
1126         }
1127         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1128                 if (sb->level != cpu_to_le32(1) &&
1129                     sb->level != cpu_to_le32(4) &&
1130                     sb->level != cpu_to_le32(5) &&
1131                     sb->level != cpu_to_le32(6) &&
1132                     sb->level != cpu_to_le32(10)) {
1133                         printk(KERN_WARNING
1134                                "md: bitmaps not supported for this level.\n");
1135                         return -EINVAL;
1136                 }
1137         }
1138
1139         rdev->preferred_minor = 0xffff;
1140         rdev->data_offset = le64_to_cpu(sb->data_offset);
1141         atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1142
1143         rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1144         bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1145         if (rdev->sb_size & bmask)
1146                 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1147
1148         if (minor_version
1149             && rdev->data_offset < sb_start + (rdev->sb_size/512))
1150                 return -EINVAL;
1151
1152         if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1153                 rdev->desc_nr = -1;
1154         else
1155                 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1156
1157         if (!refdev) {
1158                 ret = 1;
1159         } else {
1160                 __u64 ev1, ev2;
1161                 struct mdp_superblock_1 *refsb = 
1162                         (struct mdp_superblock_1*)page_address(refdev->sb_page);
1163
1164                 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1165                     sb->level != refsb->level ||
1166                     sb->layout != refsb->layout ||
1167                     sb->chunksize != refsb->chunksize) {
1168                         printk(KERN_WARNING "md: %s has strangely different"
1169                                 " superblock to %s\n",
1170                                 bdevname(rdev->bdev,b),
1171                                 bdevname(refdev->bdev,b2));
1172                         return -EINVAL;
1173                 }
1174                 ev1 = le64_to_cpu(sb->events);
1175                 ev2 = le64_to_cpu(refsb->events);
1176
1177                 if (ev1 > ev2)
1178                         ret = 1;
1179                 else
1180                         ret = 0;
1181         }
1182         if (minor_version)
1183                 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1184         else
1185                 rdev->size = rdev->sb_start / 2;
1186         if (rdev->size < le64_to_cpu(sb->data_size)/2)
1187                 return -EINVAL;
1188         rdev->size = le64_to_cpu(sb->data_size)/2;
1189         if (le32_to_cpu(sb->chunksize))
1190                 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1191
1192         if (le64_to_cpu(sb->size) > rdev->size*2)
1193                 return -EINVAL;
1194         return ret;
1195 }
1196
1197 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1198 {
1199         struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1200         __u64 ev1 = le64_to_cpu(sb->events);
1201
1202         rdev->raid_disk = -1;
1203         clear_bit(Faulty, &rdev->flags);
1204         clear_bit(In_sync, &rdev->flags);
1205         clear_bit(WriteMostly, &rdev->flags);
1206         clear_bit(BarriersNotsupp, &rdev->flags);
1207
1208         if (mddev->raid_disks == 0) {
1209                 mddev->major_version = 1;
1210                 mddev->patch_version = 0;
1211                 mddev->external = 0;
1212                 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1213                 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1214                 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1215                 mddev->level = le32_to_cpu(sb->level);
1216                 mddev->clevel[0] = 0;
1217                 mddev->layout = le32_to_cpu(sb->layout);
1218                 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1219                 mddev->size = le64_to_cpu(sb->size)/2;
1220                 mddev->events = ev1;
1221                 mddev->bitmap_offset = 0;
1222                 mddev->default_bitmap_offset = 1024 >> 9;
1223                 
1224                 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1225                 memcpy(mddev->uuid, sb->set_uuid, 16);
1226
1227                 mddev->max_disks =  (4096-256)/2;
1228
1229                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1230                     mddev->bitmap_file == NULL )
1231                         mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1232
1233                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1234                         mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1235                         mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1236                         mddev->new_level = le32_to_cpu(sb->new_level);
1237                         mddev->new_layout = le32_to_cpu(sb->new_layout);
1238                         mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1239                 } else {
1240                         mddev->reshape_position = MaxSector;
1241                         mddev->delta_disks = 0;
1242                         mddev->new_level = mddev->level;
1243                         mddev->new_layout = mddev->layout;
1244                         mddev->new_chunk = mddev->chunk_size;
1245                 }
1246
1247         } else if (mddev->pers == NULL) {
1248                 /* Insist of good event counter while assembling */
1249                 ++ev1;
1250                 if (ev1 < mddev->events)
1251                         return -EINVAL;
1252         } else if (mddev->bitmap) {
1253                 /* If adding to array with a bitmap, then we can accept an
1254                  * older device, but not too old.
1255                  */
1256                 if (ev1 < mddev->bitmap->events_cleared)
1257                         return 0;
1258         } else {
1259                 if (ev1 < mddev->events)
1260                         /* just a hot-add of a new device, leave raid_disk at -1 */
1261                         return 0;
1262         }
1263         if (mddev->level != LEVEL_MULTIPATH) {
1264                 int role;
1265                 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1266                 switch(role) {
1267                 case 0xffff: /* spare */
1268                         break;
1269                 case 0xfffe: /* faulty */
1270                         set_bit(Faulty, &rdev->flags);
1271                         break;
1272                 default:
1273                         if ((le32_to_cpu(sb->feature_map) &
1274                              MD_FEATURE_RECOVERY_OFFSET))
1275                                 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1276                         else
1277                                 set_bit(In_sync, &rdev->flags);
1278                         rdev->raid_disk = role;
1279                         break;
1280                 }
1281                 if (sb->devflags & WriteMostly1)
1282                         set_bit(WriteMostly, &rdev->flags);
1283         } else /* MULTIPATH are always insync */
1284                 set_bit(In_sync, &rdev->flags);
1285
1286         return 0;
1287 }
1288
1289 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1290 {
1291         struct mdp_superblock_1 *sb;
1292         mdk_rdev_t *rdev2;
1293         int max_dev, i;
1294         /* make rdev->sb match mddev and rdev data. */
1295
1296         sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1297
1298         sb->feature_map = 0;
1299         sb->pad0 = 0;
1300         sb->recovery_offset = cpu_to_le64(0);
1301         memset(sb->pad1, 0, sizeof(sb->pad1));
1302         memset(sb->pad2, 0, sizeof(sb->pad2));
1303         memset(sb->pad3, 0, sizeof(sb->pad3));
1304
1305         sb->utime = cpu_to_le64((__u64)mddev->utime);
1306         sb->events = cpu_to_le64(mddev->events);
1307         if (mddev->in_sync)
1308                 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1309         else
1310                 sb->resync_offset = cpu_to_le64(0);
1311
1312         sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1313
1314         sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1315         sb->size = cpu_to_le64(mddev->size<<1);
1316
1317         if (mddev->bitmap && mddev->bitmap_file == NULL) {
1318                 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1319                 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1320         }
1321
1322         if (rdev->raid_disk >= 0 &&
1323             !test_bit(In_sync, &rdev->flags) &&
1324             rdev->recovery_offset > 0) {
1325                 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1326                 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1327         }
1328
1329         if (mddev->reshape_position != MaxSector) {
1330                 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1331                 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1332                 sb->new_layout = cpu_to_le32(mddev->new_layout);
1333                 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1334                 sb->new_level = cpu_to_le32(mddev->new_level);
1335                 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1336         }
1337
1338         max_dev = 0;
1339         list_for_each_entry(rdev2, &mddev->disks, same_set)
1340                 if (rdev2->desc_nr+1 > max_dev)
1341                         max_dev = rdev2->desc_nr+1;
1342
1343         if (max_dev > le32_to_cpu(sb->max_dev))
1344                 sb->max_dev = cpu_to_le32(max_dev);
1345         for (i=0; i<max_dev;i++)
1346                 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1347         
1348         list_for_each_entry(rdev2, &mddev->disks, same_set) {
1349                 i = rdev2->desc_nr;
1350                 if (test_bit(Faulty, &rdev2->flags))
1351                         sb->dev_roles[i] = cpu_to_le16(0xfffe);
1352                 else if (test_bit(In_sync, &rdev2->flags))
1353                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1354                 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1355                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1356                 else
1357                         sb->dev_roles[i] = cpu_to_le16(0xffff);
1358         }
1359
1360         sb->sb_csum = calc_sb_1_csum(sb);
1361 }
1362
1363 static unsigned long long
1364 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1365 {
1366         struct mdp_superblock_1 *sb;
1367         sector_t max_sectors;
1368         if (num_sectors && num_sectors < rdev->mddev->size * 2)
1369                 return 0; /* component must fit device */
1370         if (rdev->sb_start < rdev->data_offset) {
1371                 /* minor versions 1 and 2; superblock before data */
1372                 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1373                 max_sectors -= rdev->data_offset;
1374                 if (!num_sectors || num_sectors > max_sectors)
1375                         num_sectors = max_sectors;
1376         } else if (rdev->mddev->bitmap_offset) {
1377                 /* minor version 0 with bitmap we can't move */
1378                 return 0;
1379         } else {
1380                 /* minor version 0; superblock after data */
1381                 sector_t sb_start;
1382                 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1383                 sb_start &= ~(sector_t)(4*2 - 1);
1384                 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1385                 if (!num_sectors || num_sectors > max_sectors)
1386                         num_sectors = max_sectors;
1387                 rdev->sb_start = sb_start;
1388         }
1389         sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1390         sb->data_size = cpu_to_le64(num_sectors);
1391         sb->super_offset = rdev->sb_start;
1392         sb->sb_csum = calc_sb_1_csum(sb);
1393         md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1394                        rdev->sb_page);
1395         md_super_wait(rdev->mddev);
1396         return num_sectors / 2; /* kB for sysfs */
1397 }
1398
1399 static struct super_type super_types[] = {
1400         [0] = {
1401                 .name   = "0.90.0",
1402                 .owner  = THIS_MODULE,
1403                 .load_super         = super_90_load,
1404                 .validate_super     = super_90_validate,
1405                 .sync_super         = super_90_sync,
1406                 .rdev_size_change   = super_90_rdev_size_change,
1407         },
1408         [1] = {
1409                 .name   = "md-1",
1410                 .owner  = THIS_MODULE,
1411                 .load_super         = super_1_load,
1412                 .validate_super     = super_1_validate,
1413                 .sync_super         = super_1_sync,
1414                 .rdev_size_change   = super_1_rdev_size_change,
1415         },
1416 };
1417
1418 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1419 {
1420         mdk_rdev_t *rdev, *rdev2;
1421
1422         rcu_read_lock();
1423         rdev_for_each_rcu(rdev, mddev1)
1424                 rdev_for_each_rcu(rdev2, mddev2)
1425                         if (rdev->bdev->bd_contains ==
1426                             rdev2->bdev->bd_contains) {
1427                                 rcu_read_unlock();
1428                                 return 1;
1429                         }
1430         rcu_read_unlock();
1431         return 0;
1432 }
1433
1434 static LIST_HEAD(pending_raid_disks);
1435
1436 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1437 {
1438         char b[BDEVNAME_SIZE];
1439         struct kobject *ko;
1440         char *s;
1441         int err;
1442
1443         if (rdev->mddev) {
1444                 MD_BUG();
1445                 return -EINVAL;
1446         }
1447
1448         /* prevent duplicates */
1449         if (find_rdev(mddev, rdev->bdev->bd_dev))
1450                 return -EEXIST;
1451
1452         /* make sure rdev->size exceeds mddev->size */
1453         if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1454                 if (mddev->pers) {
1455                         /* Cannot change size, so fail
1456                          * If mddev->level <= 0, then we don't care
1457                          * about aligning sizes (e.g. linear)
1458                          */
1459                         if (mddev->level > 0)
1460                                 return -ENOSPC;
1461                 } else
1462                         mddev->size = rdev->size;
1463         }
1464
1465         /* Verify rdev->desc_nr is unique.
1466          * If it is -1, assign a free number, else
1467          * check number is not in use
1468          */
1469         if (rdev->desc_nr < 0) {
1470                 int choice = 0;
1471                 if (mddev->pers) choice = mddev->raid_disks;
1472                 while (find_rdev_nr(mddev, choice))
1473                         choice++;
1474                 rdev->desc_nr = choice;
1475         } else {
1476                 if (find_rdev_nr(mddev, rdev->desc_nr))
1477                         return -EBUSY;
1478         }
1479         if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1480                 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1481                        mdname(mddev), mddev->max_disks);
1482                 return -EBUSY;
1483         }
1484         bdevname(rdev->bdev,b);
1485         while ( (s=strchr(b, '/')) != NULL)
1486                 *s = '!';
1487
1488         rdev->mddev = mddev;
1489         printk(KERN_INFO "md: bind<%s>\n", b);
1490
1491         if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1492                 goto fail;
1493
1494         ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
1495         if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1496                 kobject_del(&rdev->kobj);
1497                 goto fail;
1498         }
1499         rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1500
1501         list_add_rcu(&rdev->same_set, &mddev->disks);
1502         bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1503
1504         /* May as well allow recovery to be retried once */
1505         mddev->recovery_disabled = 0;
1506         return 0;
1507
1508  fail:
1509         printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1510                b, mdname(mddev));
1511         return err;
1512 }
1513
1514 static void md_delayed_delete(struct work_struct *ws)
1515 {
1516         mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1517         kobject_del(&rdev->kobj);
1518         kobject_put(&rdev->kobj);
1519 }
1520
1521 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1522 {
1523         char b[BDEVNAME_SIZE];
1524         if (!rdev->mddev) {
1525                 MD_BUG();
1526                 return;
1527         }
1528         bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1529         list_del_rcu(&rdev->same_set);
1530         printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1531         rdev->mddev = NULL;
1532         sysfs_remove_link(&rdev->kobj, "block");
1533         sysfs_put(rdev->sysfs_state);
1534         rdev->sysfs_state = NULL;
1535         /* We need to delay this, otherwise we can deadlock when
1536          * writing to 'remove' to "dev/state".  We also need
1537          * to delay it due to rcu usage.
1538          */
1539         synchronize_rcu();
1540         INIT_WORK(&rdev->del_work, md_delayed_delete);
1541         kobject_get(&rdev->kobj);
1542         schedule_work(&rdev->del_work);
1543 }
1544
1545 /*
1546  * prevent the device from being mounted, repartitioned or
1547  * otherwise reused by a RAID array (or any other kernel
1548  * subsystem), by bd_claiming the device.
1549  */
1550 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1551 {
1552         int err = 0;
1553         struct block_device *bdev;
1554         char b[BDEVNAME_SIZE];
1555
1556         bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1557         if (IS_ERR(bdev)) {
1558                 printk(KERN_ERR "md: could not open %s.\n",
1559                         __bdevname(dev, b));
1560                 return PTR_ERR(bdev);
1561         }
1562         err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1563         if (err) {
1564                 printk(KERN_ERR "md: could not bd_claim %s.\n",
1565                         bdevname(bdev, b));
1566                 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1567                 return err;
1568         }
1569         if (!shared)
1570                 set_bit(AllReserved, &rdev->flags);
1571         rdev->bdev = bdev;
1572         return err;
1573 }
1574
1575 static void unlock_rdev(mdk_rdev_t *rdev)
1576 {
1577         struct block_device *bdev = rdev->bdev;
1578         rdev->bdev = NULL;
1579         if (!bdev)
1580                 MD_BUG();
1581         bd_release(bdev);
1582         blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1583 }
1584
1585 void md_autodetect_dev(dev_t dev);
1586
1587 static void export_rdev(mdk_rdev_t * rdev)
1588 {
1589         char b[BDEVNAME_SIZE];
1590         printk(KERN_INFO "md: export_rdev(%s)\n",
1591                 bdevname(rdev->bdev,b));
1592         if (rdev->mddev)
1593                 MD_BUG();
1594         free_disk_sb(rdev);
1595 #ifndef MODULE
1596         if (test_bit(AutoDetected, &rdev->flags))
1597                 md_autodetect_dev(rdev->bdev->bd_dev);
1598 #endif
1599         unlock_rdev(rdev);
1600         kobject_put(&rdev->kobj);
1601 }
1602
1603 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1604 {
1605         unbind_rdev_from_array(rdev);
1606         export_rdev(rdev);
1607 }
1608
1609 static void export_array(mddev_t *mddev)
1610 {
1611         mdk_rdev_t *rdev, *tmp;
1612
1613         rdev_for_each(rdev, tmp, mddev) {
1614                 if (!rdev->mddev) {
1615                         MD_BUG();
1616                         continue;
1617                 }
1618                 kick_rdev_from_array(rdev);
1619         }
1620         if (!list_empty(&mddev->disks))
1621                 MD_BUG();
1622         mddev->raid_disks = 0;
1623         mddev->major_version = 0;
1624 }
1625
1626 static void print_desc(mdp_disk_t *desc)
1627 {
1628         printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1629                 desc->major,desc->minor,desc->raid_disk,desc->state);
1630 }
1631
1632 static void print_sb_90(mdp_super_t *sb)
1633 {
1634         int i;
1635
1636         printk(KERN_INFO 
1637                 "md:  SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1638                 sb->major_version, sb->minor_version, sb->patch_version,
1639                 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1640                 sb->ctime);
1641         printk(KERN_INFO "md:     L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1642                 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1643                 sb->md_minor, sb->layout, sb->chunk_size);
1644         printk(KERN_INFO "md:     UT:%08x ST:%d AD:%d WD:%d"
1645                 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1646                 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1647                 sb->failed_disks, sb->spare_disks,
1648                 sb->sb_csum, (unsigned long)sb->events_lo);
1649
1650         printk(KERN_INFO);
1651         for (i = 0; i < MD_SB_DISKS; i++) {
1652                 mdp_disk_t *desc;
1653
1654                 desc = sb->disks + i;
1655                 if (desc->number || desc->major || desc->minor ||
1656                     desc->raid_disk || (desc->state && (desc->state != 4))) {
1657                         printk("     D %2d: ", i);
1658                         print_desc(desc);
1659                 }
1660         }
1661         printk(KERN_INFO "md:     THIS: ");
1662         print_desc(&sb->this_disk);
1663 }
1664
1665 static void print_sb_1(struct mdp_superblock_1 *sb)
1666 {
1667         __u8 *uuid;
1668
1669         uuid = sb->set_uuid;
1670         printk(KERN_INFO "md:  SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1671                         ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1672                KERN_INFO "md:    Name: \"%s\" CT:%llu\n",
1673                 le32_to_cpu(sb->major_version),
1674                 le32_to_cpu(sb->feature_map),
1675                 uuid[0], uuid[1], uuid[2], uuid[3],
1676                 uuid[4], uuid[5], uuid[6], uuid[7],
1677                 uuid[8], uuid[9], uuid[10], uuid[11],
1678                 uuid[12], uuid[13], uuid[14], uuid[15],
1679                 sb->set_name,
1680                 (unsigned long long)le64_to_cpu(sb->ctime)
1681                        & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1682
1683         uuid = sb->device_uuid;
1684         printk(KERN_INFO "md:       L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1685                         " RO:%llu\n"
1686                KERN_INFO "md:     Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1687                         ":%02x%02x%02x%02x%02x%02x\n"
1688                KERN_INFO "md:       (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1689                KERN_INFO "md:         (MaxDev:%u) \n",
1690                 le32_to_cpu(sb->level),
1691                 (unsigned long long)le64_to_cpu(sb->size),
1692                 le32_to_cpu(sb->raid_disks),
1693                 le32_to_cpu(sb->layout),
1694                 le32_to_cpu(sb->chunksize),
1695                 (unsigned long long)le64_to_cpu(sb->data_offset),
1696                 (unsigned long long)le64_to_cpu(sb->data_size),
1697                 (unsigned long long)le64_to_cpu(sb->super_offset),
1698                 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1699                 le32_to_cpu(sb->dev_number),
1700                 uuid[0], uuid[1], uuid[2], uuid[3],
1701                 uuid[4], uuid[5], uuid[6], uuid[7],
1702                 uuid[8], uuid[9], uuid[10], uuid[11],
1703                 uuid[12], uuid[13], uuid[14], uuid[15],
1704                 sb->devflags,
1705                 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1706                 (unsigned long long)le64_to_cpu(sb->events),
1707                 (unsigned long long)le64_to_cpu(sb->resync_offset),
1708                 le32_to_cpu(sb->sb_csum),
1709                 le32_to_cpu(sb->max_dev)
1710                 );
1711 }
1712
1713 static void print_rdev(mdk_rdev_t *rdev, int major_version)
1714 {
1715         char b[BDEVNAME_SIZE];
1716         printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1717                 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1718                 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1719                 rdev->desc_nr);
1720         if (rdev->sb_loaded) {
1721                 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1722                 switch (major_version) {
1723                 case 0:
1724                         print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1725                         break;
1726                 case 1:
1727                         print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1728                         break;
1729                 }
1730         } else
1731                 printk(KERN_INFO "md: no rdev superblock!\n");
1732 }
1733
1734 static void md_print_devices(void)
1735 {
1736         struct list_head *tmp;
1737         mdk_rdev_t *rdev;
1738         mddev_t *mddev;
1739         char b[BDEVNAME_SIZE];
1740
1741         printk("\n");
1742         printk("md:     **********************************\n");
1743         printk("md:     * <COMPLETE RAID STATE PRINTOUT> *\n");
1744         printk("md:     **********************************\n");
1745         for_each_mddev(mddev, tmp) {
1746
1747                 if (mddev->bitmap)
1748                         bitmap_print_sb(mddev->bitmap);
1749                 else
1750                         printk("%s: ", mdname(mddev));
1751                 list_for_each_entry(rdev, &mddev->disks, same_set)
1752                         printk("<%s>", bdevname(rdev->bdev,b));
1753                 printk("\n");
1754
1755                 list_for_each_entry(rdev, &mddev->disks, same_set)
1756                         print_rdev(rdev, mddev->major_version);
1757         }
1758         printk("md:     **********************************\n");
1759         printk("\n");
1760 }
1761
1762
1763 static void sync_sbs(mddev_t * mddev, int nospares)
1764 {
1765         /* Update each superblock (in-memory image), but
1766          * if we are allowed to, skip spares which already
1767          * have the right event counter, or have one earlier
1768          * (which would mean they aren't being marked as dirty
1769          * with the rest of the array)
1770          */
1771         mdk_rdev_t *rdev;
1772
1773         list_for_each_entry(rdev, &mddev->disks, same_set) {
1774                 if (rdev->sb_events == mddev->events ||
1775                     (nospares &&
1776                      rdev->raid_disk < 0 &&
1777                      (rdev->sb_events&1)==0 &&
1778                      rdev->sb_events+1 == mddev->events)) {
1779                         /* Don't update this superblock */
1780                         rdev->sb_loaded = 2;
1781                 } else {
1782                         super_types[mddev->major_version].
1783                                 sync_super(mddev, rdev);
1784                         rdev->sb_loaded = 1;
1785                 }
1786         }
1787 }
1788
1789 static void md_update_sb(mddev_t * mddev, int force_change)
1790 {
1791         mdk_rdev_t *rdev;
1792         int sync_req;
1793         int nospares = 0;
1794
1795         if (mddev->external)
1796                 return;
1797 repeat:
1798         spin_lock_irq(&mddev->write_lock);
1799
1800         set_bit(MD_CHANGE_PENDING, &mddev->flags);
1801         if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1802                 force_change = 1;
1803         if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1804                 /* just a clean<-> dirty transition, possibly leave spares alone,
1805                  * though if events isn't the right even/odd, we will have to do
1806                  * spares after all
1807                  */
1808                 nospares = 1;
1809         if (force_change)
1810                 nospares = 0;
1811         if (mddev->degraded)
1812                 /* If the array is degraded, then skipping spares is both
1813                  * dangerous and fairly pointless.
1814                  * Dangerous because a device that was removed from the array
1815                  * might have a event_count that still looks up-to-date,
1816                  * so it can be re-added without a resync.
1817                  * Pointless because if there are any spares to skip,
1818                  * then a recovery will happen and soon that array won't
1819                  * be degraded any more and the spare can go back to sleep then.
1820                  */
1821                 nospares = 0;
1822
1823         sync_req = mddev->in_sync;
1824         mddev->utime = get_seconds();
1825
1826         /* If this is just a dirty<->clean transition, and the array is clean
1827          * and 'events' is odd, we can roll back to the previous clean state */
1828         if (nospares
1829             && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1830             && (mddev->events & 1)
1831             && mddev->events != 1)
1832                 mddev->events--;
1833         else {
1834                 /* otherwise we have to go forward and ... */
1835                 mddev->events ++;
1836                 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1837                         /* .. if the array isn't clean, insist on an odd 'events' */
1838                         if ((mddev->events&1)==0) {
1839                                 mddev->events++;
1840                                 nospares = 0;
1841                         }
1842                 } else {
1843                         /* otherwise insist on an even 'events' (for clean states) */
1844                         if ((mddev->events&1)) {
1845                                 mddev->events++;
1846                                 nospares = 0;
1847                         }
1848                 }
1849         }
1850
1851         if (!mddev->events) {
1852                 /*
1853                  * oops, this 64-bit counter should never wrap.
1854                  * Either we are in around ~1 trillion A.C., assuming
1855                  * 1 reboot per second, or we have a bug:
1856                  */
1857                 MD_BUG();
1858                 mddev->events --;
1859         }
1860
1861         /*
1862          * do not write anything to disk if using
1863          * nonpersistent superblocks
1864          */
1865         if (!mddev->persistent) {
1866                 if (!mddev->external)
1867                         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1868
1869                 spin_unlock_irq(&mddev->write_lock);
1870                 wake_up(&mddev->sb_wait);
1871                 return;
1872         }
1873         sync_sbs(mddev, nospares);
1874         spin_unlock_irq(&mddev->write_lock);
1875
1876         dprintk(KERN_INFO 
1877                 "md: updating %s RAID superblock on device (in sync %d)\n",
1878                 mdname(mddev),mddev->in_sync);
1879
1880         bitmap_update_sb(mddev->bitmap);
1881         list_for_each_entry(rdev, &mddev->disks, same_set) {
1882                 char b[BDEVNAME_SIZE];
1883                 dprintk(KERN_INFO "md: ");
1884                 if (rdev->sb_loaded != 1)
1885                         continue; /* no noise on spare devices */
1886                 if (test_bit(Faulty, &rdev->flags))
1887                         dprintk("(skipping faulty ");
1888
1889                 dprintk("%s ", bdevname(rdev->bdev,b));
1890                 if (!test_bit(Faulty, &rdev->flags)) {
1891                         md_super_write(mddev,rdev,
1892                                        rdev->sb_start, rdev->sb_size,
1893                                        rdev->sb_page);
1894                         dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1895                                 bdevname(rdev->bdev,b),
1896                                 (unsigned long long)rdev->sb_start);
1897                         rdev->sb_events = mddev->events;
1898
1899                 } else
1900                         dprintk(")\n");
1901                 if (mddev->level == LEVEL_MULTIPATH)
1902                         /* only need to write one superblock... */
1903                         break;
1904         }
1905         md_super_wait(mddev);
1906         /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1907
1908         spin_lock_irq(&mddev->write_lock);
1909         if (mddev->in_sync != sync_req ||
1910             test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
1911                 /* have to write it out again */
1912                 spin_unlock_irq(&mddev->write_lock);
1913                 goto repeat;
1914         }
1915         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1916         spin_unlock_irq(&mddev->write_lock);
1917         wake_up(&mddev->sb_wait);
1918
1919 }
1920
1921 /* words written to sysfs files may, or may not, be \n terminated.
1922  * We want to accept with case. For this we use cmd_match.
1923  */
1924 static int cmd_match(const char *cmd, const char *str)
1925 {
1926         /* See if cmd, written into a sysfs file, matches
1927          * str.  They must either be the same, or cmd can
1928          * have a trailing newline
1929          */
1930         while (*cmd && *str && *cmd == *str) {
1931                 cmd++;
1932                 str++;
1933         }
1934         if (*cmd == '\n')
1935                 cmd++;
1936         if (*str || *cmd)
1937                 return 0;
1938         return 1;
1939 }
1940
1941 struct rdev_sysfs_entry {
1942         struct attribute attr;
1943         ssize_t (*show)(mdk_rdev_t *, char *);
1944         ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1945 };
1946
1947 static ssize_t
1948 state_show(mdk_rdev_t *rdev, char *page)
1949 {
1950         char *sep = "";
1951         size_t len = 0;
1952
1953         if (test_bit(Faulty, &rdev->flags)) {
1954                 len+= sprintf(page+len, "%sfaulty",sep);
1955                 sep = ",";
1956         }
1957         if (test_bit(In_sync, &rdev->flags)) {
1958                 len += sprintf(page+len, "%sin_sync",sep);
1959                 sep = ",";
1960         }
1961         if (test_bit(WriteMostly, &rdev->flags)) {
1962                 len += sprintf(page+len, "%swrite_mostly",sep);
1963                 sep = ",";
1964         }
1965         if (test_bit(Blocked, &rdev->flags)) {
1966                 len += sprintf(page+len, "%sblocked", sep);
1967                 sep = ",";
1968         }
1969         if (!test_bit(Faulty, &rdev->flags) &&
1970             !test_bit(In_sync, &rdev->flags)) {
1971                 len += sprintf(page+len, "%sspare", sep);
1972                 sep = ",";
1973         }
1974         return len+sprintf(page+len, "\n");
1975 }
1976
1977 static ssize_t
1978 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1979 {
1980         /* can write
1981          *  faulty  - simulates and error
1982          *  remove  - disconnects the device
1983          *  writemostly - sets write_mostly
1984          *  -writemostly - clears write_mostly
1985          *  blocked - sets the Blocked flag
1986          *  -blocked - clears the Blocked flag
1987          */
1988         int err = -EINVAL;
1989         if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1990                 md_error(rdev->mddev, rdev);
1991                 err = 0;
1992         } else if (cmd_match(buf, "remove")) {
1993                 if (rdev->raid_disk >= 0)
1994                         err = -EBUSY;
1995                 else {
1996                         mddev_t *mddev = rdev->mddev;
1997                         kick_rdev_from_array(rdev);
1998                         if (mddev->pers)
1999                                 md_update_sb(mddev, 1);
2000                         md_new_event(mddev);
2001                         err = 0;
2002                 }
2003         } else if (cmd_match(buf, "writemostly")) {
2004                 set_bit(WriteMostly, &rdev->flags);
2005                 err = 0;
2006         } else if (cmd_match(buf, "-writemostly")) {
2007                 clear_bit(WriteMostly, &rdev->flags);
2008                 err = 0;
2009         } else if (cmd_match(buf, "blocked")) {
2010                 set_bit(Blocked, &rdev->flags);
2011                 err = 0;
2012         } else if (cmd_match(buf, "-blocked")) {
2013                 clear_bit(Blocked, &rdev->flags);
2014                 wake_up(&rdev->blocked_wait);
2015                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2016                 md_wakeup_thread(rdev->mddev->thread);
2017
2018                 err = 0;
2019         }
2020         if (!err && rdev->sysfs_state)
2021                 sysfs_notify_dirent(rdev->sysfs_state);
2022         return err ? err : len;
2023 }
2024 static struct rdev_sysfs_entry rdev_state =
2025 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2026
2027 static ssize_t
2028 errors_show(mdk_rdev_t *rdev, char *page)
2029 {
2030         return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2031 }
2032
2033 static ssize_t
2034 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2035 {
2036         char *e;
2037         unsigned long n = simple_strtoul(buf, &e, 10);
2038         if (*buf && (*e == 0 || *e == '\n')) {
2039                 atomic_set(&rdev->corrected_errors, n);
2040                 return len;
2041         }
2042         return -EINVAL;
2043 }
2044 static struct rdev_sysfs_entry rdev_errors =
2045 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2046
2047 static ssize_t
2048 slot_show(mdk_rdev_t *rdev, char *page)
2049 {
2050         if (rdev->raid_disk < 0)
2051                 return sprintf(page, "none\n");
2052         else
2053                 return sprintf(page, "%d\n", rdev->raid_disk);
2054 }
2055
2056 static ssize_t
2057 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2058 {
2059         char *e;
2060         int err;
2061         char nm[20];
2062         int slot = simple_strtoul(buf, &e, 10);
2063         if (strncmp(buf, "none", 4)==0)
2064                 slot = -1;
2065         else if (e==buf || (*e && *e!= '\n'))
2066                 return -EINVAL;
2067         if (rdev->mddev->pers && slot == -1) {
2068                 /* Setting 'slot' on an active array requires also
2069                  * updating the 'rd%d' link, and communicating
2070                  * with the personality with ->hot_*_disk.
2071                  * For now we only support removing
2072                  * failed/spare devices.  This normally happens automatically,
2073                  * but not when the metadata is externally managed.
2074                  */
2075                 if (rdev->raid_disk == -1)
2076                         return -EEXIST;
2077                 /* personality does all needed checks */
2078                 if (rdev->mddev->pers->hot_add_disk == NULL)
2079                         return -EINVAL;
2080                 err = rdev->mddev->pers->
2081                         hot_remove_disk(rdev->mddev, rdev->raid_disk);
2082                 if (err)
2083                         return err;
2084                 sprintf(nm, "rd%d", rdev->raid_disk);
2085                 sysfs_remove_link(&rdev->mddev->kobj, nm);
2086                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2087                 md_wakeup_thread(rdev->mddev->thread);
2088         } else if (rdev->mddev->pers) {
2089                 mdk_rdev_t *rdev2;
2090                 /* Activating a spare .. or possibly reactivating
2091                  * if we every get bitmaps working here.
2092                  */
2093
2094                 if (rdev->raid_disk != -1)
2095                         return -EBUSY;
2096
2097                 if (rdev->mddev->pers->hot_add_disk == NULL)
2098                         return -EINVAL;
2099
2100                 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
2101                         if (rdev2->raid_disk == slot)
2102                                 return -EEXIST;
2103
2104                 rdev->raid_disk = slot;
2105                 if (test_bit(In_sync, &rdev->flags))
2106                         rdev->saved_raid_disk = slot;
2107                 else
2108                         rdev->saved_raid_disk = -1;
2109                 err = rdev->mddev->pers->
2110                         hot_add_disk(rdev->mddev, rdev);
2111                 if (err) {
2112                         rdev->raid_disk = -1;
2113                         return err;
2114                 } else
2115                         sysfs_notify_dirent(rdev->sysfs_state);
2116                 sprintf(nm, "rd%d", rdev->raid_disk);
2117                 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2118                         printk(KERN_WARNING
2119                                "md: cannot register "
2120                                "%s for %s\n",
2121                                nm, mdname(rdev->mddev));
2122
2123                 /* don't wakeup anyone, leave that to userspace. */
2124         } else {
2125                 if (slot >= rdev->mddev->raid_disks)
2126                         return -ENOSPC;
2127                 rdev->raid_disk = slot;
2128                 /* assume it is working */
2129                 clear_bit(Faulty, &rdev->flags);
2130                 clear_bit(WriteMostly, &rdev->flags);
2131                 set_bit(In_sync, &rdev->flags);
2132                 sysfs_notify_dirent(rdev->sysfs_state);
2133         }
2134         return len;
2135 }
2136
2137
2138 static struct rdev_sysfs_entry rdev_slot =
2139 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2140
2141 static ssize_t
2142 offset_show(mdk_rdev_t *rdev, char *page)
2143 {
2144         return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2145 }
2146
2147 static ssize_t
2148 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2149 {
2150         char *e;
2151         unsigned long long offset = simple_strtoull(buf, &e, 10);
2152         if (e==buf || (*e && *e != '\n'))
2153                 return -EINVAL;
2154         if (rdev->mddev->pers && rdev->raid_disk >= 0)
2155                 return -EBUSY;
2156         if (rdev->size && rdev->mddev->external)
2157                 /* Must set offset before size, so overlap checks
2158                  * can be sane */
2159                 return -EBUSY;
2160         rdev->data_offset = offset;
2161         return len;
2162 }
2163
2164 static struct rdev_sysfs_entry rdev_offset =
2165 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2166
2167 static ssize_t
2168 rdev_size_show(mdk_rdev_t *rdev, char *page)
2169 {
2170         return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2171 }
2172
2173 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2174 {
2175         /* check if two start/length pairs overlap */
2176         if (s1+l1 <= s2)
2177                 return 0;
2178         if (s2+l2 <= s1)
2179                 return 0;
2180         return 1;
2181 }
2182
2183 static ssize_t
2184 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2185 {
2186         unsigned long long size;
2187         unsigned long long oldsize = rdev->size;
2188         mddev_t *my_mddev = rdev->mddev;
2189
2190         if (strict_strtoull(buf, 10, &size) < 0)
2191                 return -EINVAL;
2192         if (my_mddev->pers && rdev->raid_disk >= 0) {
2193                 if (my_mddev->persistent) {
2194                         size = super_types[my_mddev->major_version].
2195                                 rdev_size_change(rdev, size * 2);
2196                         if (!size)
2197                                 return -EBUSY;
2198                 } else if (!size) {
2199                         size = (rdev->bdev->bd_inode->i_size >> 10);
2200                         size -= rdev->data_offset/2;
2201                 }
2202         }
2203         if (size < my_mddev->size)
2204                 return -EINVAL; /* component must fit device */
2205
2206         rdev->size = size;
2207         if (size > oldsize && my_mddev->external) {
2208                 /* need to check that all other rdevs with the same ->bdev
2209                  * do not overlap.  We need to unlock the mddev to avoid
2210                  * a deadlock.  We have already changed rdev->size, and if
2211                  * we have to change it back, we will have the lock again.
2212                  */
2213                 mddev_t *mddev;
2214                 int overlap = 0;
2215                 struct list_head *tmp;
2216
2217                 mddev_unlock(my_mddev);
2218                 for_each_mddev(mddev, tmp) {
2219                         mdk_rdev_t *rdev2;
2220
2221                         mddev_lock(mddev);
2222                         list_for_each_entry(rdev2, &mddev->disks, same_set)
2223                                 if (test_bit(AllReserved, &rdev2->flags) ||
2224                                     (rdev->bdev == rdev2->bdev &&
2225                                      rdev != rdev2 &&
2226                                      overlaps(rdev->data_offset, rdev->size * 2,
2227                                               rdev2->data_offset,
2228                                               rdev2->size * 2))) {
2229                                         overlap = 1;
2230                                         break;
2231                                 }
2232                         mddev_unlock(mddev);
2233                         if (overlap) {
2234                                 mddev_put(mddev);
2235                                 break;
2236                         }
2237                 }
2238                 mddev_lock(my_mddev);
2239                 if (overlap) {
2240                         /* Someone else could have slipped in a size
2241                          * change here, but doing so is just silly.
2242                          * We put oldsize back because we *know* it is
2243                          * safe, and trust userspace not to race with
2244                          * itself
2245                          */
2246                         rdev->size = oldsize;
2247                         return -EBUSY;
2248                 }
2249         }
2250         return len;
2251 }
2252
2253 static struct rdev_sysfs_entry rdev_size =
2254 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2255
2256 static struct attribute *rdev_default_attrs[] = {
2257         &rdev_state.attr,
2258         &rdev_errors.attr,
2259         &rdev_slot.attr,
2260         &rdev_offset.attr,
2261         &rdev_size.attr,
2262         NULL,
2263 };
2264 static ssize_t
2265 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2266 {
2267         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2268         mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2269         mddev_t *mddev = rdev->mddev;
2270         ssize_t rv;
2271
2272         if (!entry->show)
2273                 return -EIO;
2274
2275         rv = mddev ? mddev_lock(mddev) : -EBUSY;
2276         if (!rv) {
2277                 if (rdev->mddev == NULL)
2278                         rv = -EBUSY;
2279                 else
2280                         rv = entry->show(rdev, page);
2281                 mddev_unlock(mddev);
2282         }
2283         return rv;
2284 }
2285
2286 static ssize_t
2287 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2288               const char *page, size_t length)
2289 {
2290         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2291         mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2292         ssize_t rv;
2293         mddev_t *mddev = rdev->mddev;
2294
2295         if (!entry->store)
2296                 return -EIO;
2297         if (!capable(CAP_SYS_ADMIN))
2298                 return -EACCES;
2299         rv = mddev ? mddev_lock(mddev): -EBUSY;
2300         if (!rv) {
2301                 if (rdev->mddev == NULL)
2302                         rv = -EBUSY;
2303                 else
2304                         rv = entry->store(rdev, page, length);
2305                 mddev_unlock(mddev);
2306         }
2307         return rv;
2308 }
2309
2310 static void rdev_free(struct kobject *ko)
2311 {
2312         mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2313         kfree(rdev);
2314 }
2315 static struct sysfs_ops rdev_sysfs_ops = {
2316         .show           = rdev_attr_show,
2317         .store          = rdev_attr_store,
2318 };
2319 static struct kobj_type rdev_ktype = {
2320         .release        = rdev_free,
2321         .sysfs_ops      = &rdev_sysfs_ops,
2322         .default_attrs  = rdev_default_attrs,
2323 };
2324
2325 /*
2326  * Import a device. If 'super_format' >= 0, then sanity check the superblock
2327  *
2328  * mark the device faulty if:
2329  *
2330  *   - the device is nonexistent (zero size)
2331  *   - the device has no valid superblock
2332  *
2333  * a faulty rdev _never_ has rdev->sb set.
2334  */
2335 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2336 {
2337         char b[BDEVNAME_SIZE];
2338         int err;
2339         mdk_rdev_t *rdev;
2340         sector_t size;
2341
2342         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2343         if (!rdev) {
2344                 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2345                 return ERR_PTR(-ENOMEM);
2346         }
2347
2348         if ((err = alloc_disk_sb(rdev)))
2349                 goto abort_free;
2350
2351         err = lock_rdev(rdev, newdev, super_format == -2);
2352         if (err)
2353                 goto abort_free;
2354
2355         kobject_init(&rdev->kobj, &rdev_ktype);
2356
2357         rdev->desc_nr = -1;
2358         rdev->saved_raid_disk = -1;
2359         rdev->raid_disk = -1;
2360         rdev->flags = 0;
2361         rdev->data_offset = 0;
2362         rdev->sb_events = 0;
2363         atomic_set(&rdev->nr_pending, 0);
2364         atomic_set(&rdev->read_errors, 0);
2365         atomic_set(&rdev->corrected_errors, 0);
2366
2367         size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2368         if (!size) {
2369                 printk(KERN_WARNING 
2370                         "md: %s has zero or unknown size, marking faulty!\n",
2371                         bdevname(rdev->bdev,b));
2372                 err = -EINVAL;
2373                 goto abort_free;
2374         }
2375
2376         if (super_format >= 0) {
2377                 err = super_types[super_format].
2378                         load_super(rdev, NULL, super_minor);
2379                 if (err == -EINVAL) {
2380                         printk(KERN_WARNING
2381                                 "md: %s does not have a valid v%d.%d "
2382                                "superblock, not importing!\n",
2383                                 bdevname(rdev->bdev,b),
2384                                super_format, super_minor);
2385                         goto abort_free;
2386                 }
2387                 if (err < 0) {
2388                         printk(KERN_WARNING 
2389                                 "md: could not read %s's sb, not importing!\n",
2390                                 bdevname(rdev->bdev,b));
2391                         goto abort_free;
2392                 }
2393         }
2394
2395         INIT_LIST_HEAD(&rdev->same_set);
2396         init_waitqueue_head(&rdev->blocked_wait);
2397
2398         return rdev;
2399
2400 abort_free:
2401         if (rdev->sb_page) {
2402                 if (rdev->bdev)
2403                         unlock_rdev(rdev);
2404                 free_disk_sb(rdev);
2405         }
2406         kfree(rdev);
2407         return ERR_PTR(err);
2408 }
2409
2410 /*
2411  * Check a full RAID array for plausibility
2412  */
2413
2414
2415 static void analyze_sbs(mddev_t * mddev)
2416 {
2417         int i;
2418         mdk_rdev_t *rdev, *freshest, *tmp;
2419         char b[BDEVNAME_SIZE];
2420
2421         freshest = NULL;
2422         rdev_for_each(rdev, tmp, mddev)
2423                 switch (super_types[mddev->major_version].
2424                         load_super(rdev, freshest, mddev->minor_version)) {
2425                 case 1:
2426                         freshest = rdev;
2427                         break;
2428                 case 0:
2429                         break;
2430                 default:
2431                         printk( KERN_ERR \
2432                                 "md: fatal superblock inconsistency in %s"
2433                                 " -- removing from array\n", 
2434                                 bdevname(rdev->bdev,b));
2435                         kick_rdev_from_array(rdev);
2436                 }
2437
2438
2439         super_types[mddev->major_version].
2440                 validate_super(mddev, freshest);
2441
2442         i = 0;
2443         rdev_for_each(rdev, tmp, mddev) {
2444                 if (rdev->desc_nr >= mddev->max_disks ||
2445                     i > mddev->max_disks) {
2446                         printk(KERN_WARNING
2447                                "md: %s: %s: only %d devices permitted\n",
2448                                mdname(mddev), bdevname(rdev->bdev, b),
2449                                mddev->max_disks);
2450                         kick_rdev_from_array(rdev);
2451                         continue;
2452                 }
2453                 if (rdev != freshest)
2454                         if (super_types[mddev->major_version].
2455                             validate_super(mddev, rdev)) {
2456                                 printk(KERN_WARNING "md: kicking non-fresh %s"
2457                                         " from array!\n",
2458                                         bdevname(rdev->bdev,b));
2459                                 kick_rdev_from_array(rdev);
2460                                 continue;
2461                         }
2462                 if (mddev->level == LEVEL_MULTIPATH) {
2463                         rdev->desc_nr = i++;
2464                         rdev->raid_disk = rdev->desc_nr;
2465                         set_bit(In_sync, &rdev->flags);
2466                 } else if (rdev->raid_disk >= mddev->raid_disks) {
2467                         rdev->raid_disk = -1;
2468                         clear_bit(In_sync, &rdev->flags);
2469                 }
2470         }
2471
2472
2473
2474         if (mddev->recovery_cp != MaxSector &&
2475             mddev->level >= 1)
2476                 printk(KERN_ERR "md: %s: raid array is not clean"
2477                        " -- starting background reconstruction\n",
2478                        mdname(mddev));
2479
2480 }
2481
2482 static void md_safemode_timeout(unsigned long data);
2483
2484 static ssize_t
2485 safe_delay_show(mddev_t *mddev, char *page)
2486 {
2487         int msec = (mddev->safemode_delay*1000)/HZ;
2488         return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2489 }
2490 static ssize_t
2491 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2492 {
2493         int scale=1;
2494         int dot=0;
2495         int i;
2496         unsigned long msec;
2497         char buf[30];
2498
2499         /* remove a period, and count digits after it */
2500         if (len >= sizeof(buf))
2501                 return -EINVAL;
2502         strlcpy(buf, cbuf, sizeof(buf));
2503         for (i=0; i<len; i++) {
2504                 if (dot) {
2505                         if (isdigit(buf[i])) {
2506                                 buf[i-1] = buf[i];
2507                                 scale *= 10;
2508                         }
2509                         buf[i] = 0;
2510                 } else if (buf[i] == '.') {
2511                         dot=1;
2512                         buf[i] = 0;
2513                 }
2514         }
2515         if (strict_strtoul(buf, 10, &msec) < 0)
2516                 return -EINVAL;
2517         msec = (msec * 1000) / scale;
2518         if (msec == 0)
2519                 mddev->safemode_delay = 0;
2520         else {
2521                 unsigned long old_delay = mddev->safemode_delay;
2522                 mddev->safemode_delay = (msec*HZ)/1000;
2523                 if (mddev->safemode_delay == 0)
2524                         mddev->safemode_delay = 1;
2525                 if (mddev->safemode_delay < old_delay)
2526                         md_safemode_timeout((unsigned long)mddev);
2527         }
2528         return len;
2529 }
2530 static struct md_sysfs_entry md_safe_delay =
2531 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2532
2533 static ssize_t
2534 level_show(mddev_t *mddev, char *page)
2535 {
2536         struct mdk_personality *p = mddev->pers;
2537         if (p)
2538                 return sprintf(page, "%s\n", p->name);
2539         else if (mddev->clevel[0])
2540                 return sprintf(page, "%s\n", mddev->clevel);
2541         else if (mddev->level != LEVEL_NONE)
2542                 return sprintf(page, "%d\n", mddev->level);
2543         else
2544                 return 0;
2545 }
2546
2547 static ssize_t
2548 level_store(mddev_t *mddev, const char *buf, size_t len)
2549 {
2550         ssize_t rv = len;
2551         if (mddev->pers)
2552                 return -EBUSY;
2553         if (len == 0)
2554                 return 0;
2555         if (len >= sizeof(mddev->clevel))
2556                 return -ENOSPC;
2557         strncpy(mddev->clevel, buf, len);
2558         if (mddev->clevel[len-1] == '\n')
2559                 len--;
2560         mddev->clevel[len] = 0;
2561         mddev->level = LEVEL_NONE;
2562         return rv;
2563 }
2564
2565 static struct md_sysfs_entry md_level =
2566 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2567
2568
2569 static ssize_t
2570 layout_show(mddev_t *mddev, char *page)
2571 {
2572         /* just a number, not meaningful for all levels */
2573         if (mddev->reshape_position != MaxSector &&
2574             mddev->layout != mddev->new_layout)
2575                 return sprintf(page, "%d (%d)\n",
2576                                mddev->new_layout, mddev->layout);
2577         return sprintf(page, "%d\n", mddev->layout);
2578 }
2579
2580 static ssize_t
2581 layout_store(mddev_t *mddev, const char *buf, size_t len)
2582 {
2583         char *e;
2584         unsigned long n = simple_strtoul(buf, &e, 10);
2585
2586         if (!*buf || (*e && *e != '\n'))
2587                 return -EINVAL;
2588
2589         if (mddev->pers)
2590                 return -EBUSY;
2591         if (mddev->reshape_position != MaxSector)
2592                 mddev->new_layout = n;
2593         else
2594                 mddev->layout = n;
2595         return len;
2596 }
2597 static struct md_sysfs_entry md_layout =
2598 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2599
2600
2601 static ssize_t
2602 raid_disks_show(mddev_t *mddev, char *page)
2603 {
2604         if (mddev->raid_disks == 0)
2605                 return 0;
2606         if (mddev->reshape_position != MaxSector &&
2607             mddev->delta_disks != 0)
2608                 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2609                                mddev->raid_disks - mddev->delta_disks);
2610         return sprintf(page, "%d\n", mddev->raid_disks);
2611 }
2612
2613 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2614
2615 static ssize_t
2616 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2617 {
2618         char *e;
2619         int rv = 0;
2620         unsigned long n = simple_strtoul(buf, &e, 10);
2621
2622         if (!*buf || (*e && *e != '\n'))
2623                 return -EINVAL;
2624
2625         if (mddev->pers)
2626                 rv = update_raid_disks(mddev, n);
2627         else if (mddev->reshape_position != MaxSector) {
2628                 int olddisks = mddev->raid_disks - mddev->delta_disks;
2629                 mddev->delta_disks = n - olddisks;
2630                 mddev->raid_disks = n;
2631         } else
2632                 mddev->raid_disks = n;
2633         return rv ? rv : len;
2634 }
2635 static struct md_sysfs_entry md_raid_disks =
2636 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2637
2638 static ssize_t
2639 chunk_size_show(mddev_t *mddev, char *page)
2640 {
2641         if (mddev->reshape_position != MaxSector &&
2642             mddev->chunk_size != mddev->new_chunk)
2643                 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2644                                mddev->chunk_size);
2645         return sprintf(page, "%d\n", mddev->chunk_size);
2646 }
2647
2648 static ssize_t
2649 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2650 {
2651         /* can only set chunk_size if array is not yet active */
2652         char *e;
2653         unsigned long n = simple_strtoul(buf, &e, 10);
2654
2655         if (!*buf || (*e && *e != '\n'))
2656                 return -EINVAL;
2657
2658         if (mddev->pers)
2659                 return -EBUSY;
2660         else if (mddev->reshape_position != MaxSector)
2661                 mddev->new_chunk = n;
2662         else
2663                 mddev->chunk_size = n;
2664         return len;
2665 }
2666 static struct md_sysfs_entry md_chunk_size =
2667 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2668
2669 static ssize_t
2670 resync_start_show(mddev_t *mddev, char *page)
2671 {
2672         return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2673 }
2674
2675 static ssize_t
2676 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2677 {
2678         char *e;
2679         unsigned long long n = simple_strtoull(buf, &e, 10);
2680
2681         if (mddev->pers)
2682                 return -EBUSY;
2683         if (!*buf || (*e && *e != '\n'))
2684                 return -EINVAL;
2685
2686         mddev->recovery_cp = n;
2687         return len;
2688 }
2689 static struct md_sysfs_entry md_resync_start =
2690 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2691
2692 /*
2693  * The array state can be:
2694  *
2695  * clear
2696  *     No devices, no size, no level
2697  *     Equivalent to STOP_ARRAY ioctl
2698  * inactive
2699  *     May have some settings, but array is not active
2700  *        all IO results in error
2701  *     When written, doesn't tear down array, but just stops it
2702  * suspended (not supported yet)
2703  *     All IO requests will block. The array can be reconfigured.
2704  *     Writing this, if accepted, will block until array is quiescent
2705  * readonly
2706  *     no resync can happen.  no superblocks get written.
2707  *     write requests fail
2708  * read-auto
2709  *     like readonly, but behaves like 'clean' on a write request.
2710  *
2711  * clean - no pending writes, but otherwise active.
2712  *     When written to inactive array, starts without resync
2713  *     If a write request arrives then
2714  *       if metadata is known, mark 'dirty' and switch to 'active'.
2715  *       if not known, block and switch to write-pending
2716  *     If written to an active array that has pending writes, then fails.
2717  * active
2718  *     fully active: IO and resync can be happening.
2719  *     When written to inactive array, starts with resync
2720  *
2721  * write-pending
2722  *     clean, but writes are blocked waiting for 'active' to be written.
2723  *
2724  * active-idle
2725  *     like active, but no writes have been seen for a while (100msec).
2726  *
2727  */
2728 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2729                    write_pending, active_idle, bad_word};
2730 static char *array_states[] = {
2731         "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2732         "write-pending", "active-idle", NULL };
2733
2734 static int match_word(const char *word, char **list)
2735 {
2736         int n;
2737         for (n=0; list[n]; n++)
2738                 if (cmd_match(word, list[n]))
2739                         break;
2740         return n;
2741 }
2742
2743 static ssize_t
2744 array_state_show(mddev_t *mddev, char *page)
2745 {
2746         enum array_state st = inactive;
2747
2748         if (mddev->pers)
2749                 switch(mddev->ro) {
2750                 case 1:
2751                         st = readonly;
2752                         break;
2753                 case 2:
2754                         st = read_auto;
2755                         break;
2756                 case 0:
2757                         if (mddev->in_sync)
2758                                 st = clean;
2759                         else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2760                                 st = write_pending;
2761                         else if (mddev->safemode)
2762                                 st = active_idle;
2763                         else
2764                                 st = active;
2765                 }
2766         else {
2767                 if (list_empty(&mddev->disks) &&
2768                     mddev->raid_disks == 0 &&
2769                     mddev->size == 0)
2770                         st = clear;
2771                 else
2772                         st = inactive;
2773         }
2774         return sprintf(page, "%s\n", array_states[st]);
2775 }
2776
2777 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
2778 static int do_md_run(mddev_t * mddev);
2779 static int restart_array(mddev_t *mddev);
2780
2781 static ssize_t
2782 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2783 {
2784         int err = -EINVAL;
2785         enum array_state st = match_word(buf, array_states);
2786         switch(st) {
2787         case bad_word:
2788                 break;
2789         case clear:
2790                 /* stopping an active array */
2791                 if (atomic_read(&mddev->openers) > 0)
2792                         return -EBUSY;
2793                 err = do_md_stop(mddev, 0, 0);
2794                 break;
2795         case inactive:
2796                 /* stopping an active array */
2797                 if (mddev->pers) {
2798                         if (atomic_read(&mddev->openers) > 0)
2799                                 return -EBUSY;
2800                         err = do_md_stop(mddev, 2, 0);
2801                 } else
2802                         err = 0; /* already inactive */
2803                 break;
2804         case suspended:
2805                 break; /* not supported yet */
2806         case readonly:
2807                 if (mddev->pers)
2808                         err = do_md_stop(mddev, 1, 0);
2809                 else {
2810                         mddev->ro = 1;
2811                         set_disk_ro(mddev->gendisk, 1);
2812                         err = do_md_run(mddev);
2813                 }
2814                 break;
2815         case read_auto:
2816                 if (mddev->pers) {
2817                         if (mddev->ro == 0)
2818                                 err = do_md_stop(mddev, 1, 0);
2819                         else if (mddev->ro == 1)
2820                                 err = restart_array(mddev);
2821                         if (err == 0) {
2822                                 mddev->ro = 2;
2823                                 set_disk_ro(mddev->gendisk, 0);
2824                         }
2825                 } else {
2826                         mddev->ro = 2;
2827                         err = do_md_run(mddev);
2828                 }
2829                 break;
2830         case clean:
2831                 if (mddev->pers) {
2832                         restart_array(mddev);
2833                         spin_lock_irq(&mddev->write_lock);
2834                         if (atomic_read(&mddev->writes_pending) == 0) {
2835                                 if (mddev->in_sync == 0) {
2836                                         mddev->in_sync = 1;
2837                                         if (mddev->safemode == 1)
2838                                                 mddev->safemode = 0;
2839                                         if (mddev->persistent)
2840                                                 set_bit(MD_CHANGE_CLEAN,
2841                                                         &mddev->flags);
2842                                 }
2843                                 err = 0;
2844                         } else
2845                                 err = -EBUSY;
2846                         spin_unlock_irq(&mddev->write_lock);
2847                 } else {
2848                         mddev->ro = 0;
2849                         mddev->recovery_cp = MaxSector;
2850                         err = do_md_run(mddev);
2851                 }
2852                 break;
2853         case active:
2854                 if (mddev->pers) {
2855                         restart_array(mddev);
2856                         if (mddev->external)
2857                                 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2858                         wake_up(&mddev->sb_wait);
2859                         err = 0;
2860                 } else {
2861                         mddev->ro = 0;
2862                         set_disk_ro(mddev->gendisk, 0);
2863                         err = do_md_run(mddev);
2864                 }
2865                 break;
2866         case write_pending:
2867         case active_idle:
2868                 /* these cannot be set */
2869                 break;
2870         }
2871         if (err)
2872                 return err;
2873         else {
2874                 sysfs_notify_dirent(mddev->sysfs_state);
2875                 return len;
2876         }
2877 }
2878 static struct md_sysfs_entry md_array_state =
2879 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
2880
2881 static ssize_t
2882 null_show(mddev_t *mddev, char *page)
2883 {
2884         return -EINVAL;
2885 }
2886
2887 static ssize_t
2888 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2889 {
2890         /* buf must be %d:%d\n? giving major and minor numbers */
2891         /* The new device is added to the array.
2892          * If the array has a persistent superblock, we read the
2893          * superblock to initialise info and check validity.
2894          * Otherwise, only checking done is that in bind_rdev_to_array,
2895          * which mainly checks size.
2896          */
2897         char *e;
2898         int major = simple_strtoul(buf, &e, 10);
2899         int minor;
2900         dev_t dev;
2901         mdk_rdev_t *rdev;
2902         int err;
2903
2904         if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2905                 return -EINVAL;
2906         minor = simple_strtoul(e+1, &e, 10);
2907         if (*e && *e != '\n')
2908                 return -EINVAL;
2909         dev = MKDEV(major, minor);
2910         if (major != MAJOR(dev) ||
2911             minor != MINOR(dev))
2912                 return -EOVERFLOW;
2913
2914
2915         if (mddev->persistent) {
2916                 rdev = md_import_device(dev, mddev->major_version,
2917                                         mddev->minor_version);
2918                 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2919                         mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2920                                                        mdk_rdev_t, same_set);
2921                         err = super_types[mddev->major_version]
2922                                 .load_super(rdev, rdev0, mddev->minor_version);
2923                         if (err < 0)
2924                                 goto out;
2925                 }
2926         } else if (mddev->external)
2927                 rdev = md_import_device(dev, -2, -1);
2928         else
2929                 rdev = md_import_device(dev, -1, -1);
2930
2931         if (IS_ERR(rdev))
2932                 return PTR_ERR(rdev);
2933         err = bind_rdev_to_array(rdev, mddev);
2934  out:
2935         if (err)
2936                 export_rdev(rdev);
2937         return err ? err : len;
2938 }
2939
2940 static struct md_sysfs_entry md_new_device =
2941 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
2942
2943 static ssize_t
2944 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2945 {
2946         char *end;
2947         unsigned long chunk, end_chunk;
2948
2949         if (!mddev->bitmap)
2950                 goto out;
2951         /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2952         while (*buf) {
2953                 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2954                 if (buf == end) break;
2955                 if (*end == '-') { /* range */
2956                         buf = end + 1;
2957                         end_chunk = simple_strtoul(buf, &end, 0);
2958                         if (buf == end) break;
2959                 }
2960                 if (*end && !isspace(*end)) break;
2961                 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2962                 buf = end;
2963                 while (isspace(*buf)) buf++;
2964         }
2965         bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2966 out:
2967         return len;
2968 }
2969
2970 static struct md_sysfs_entry md_bitmap =
2971 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2972
2973 static ssize_t
2974 size_show(mddev_t *mddev, char *page)
2975 {
2976         return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2977 }
2978
2979 static int update_size(mddev_t *mddev, sector_t num_sectors);
2980
2981 static ssize_t
2982 size_store(mddev_t *mddev, const char *buf, size_t len)
2983 {
2984         /* If array is inactive, we can reduce the component size, but
2985          * not increase it (except from 0).
2986          * If array is active, we can try an on-line resize
2987          */
2988         char *e;
2989         int err = 0;
2990         unsigned long long size = simple_strtoull(buf, &e, 10);
2991         if (!*buf || *buf == '\n' ||
2992             (*e && *e != '\n'))
2993                 return -EINVAL;
2994
2995         if (mddev->pers) {
2996                 err = update_size(mddev, size * 2);
2997                 md_update_sb(mddev, 1);
2998         } else {
2999                 if (mddev->size == 0 ||
3000                     mddev->size > size)
3001                         mddev->size = size;
3002                 else
3003                         err = -ENOSPC;
3004         }
3005         return err ? err : len;
3006 }
3007
3008 static struct md_sysfs_entry md_size =
3009 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
3010
3011
3012 /* Metdata version.
3013  * This is one of
3014  *   'none' for arrays with no metadata (good luck...)
3015  *   'external' for arrays with externally managed metadata,
3016  * or N.M for internally known formats
3017  */
3018 static ssize_t
3019 metadata_show(mddev_t *mddev, char *page)
3020 {
3021         if (mddev->persistent)
3022                 return sprintf(page, "%d.%d\n",
3023                                mddev->major_version, mddev->minor_version);
3024         else if (mddev->external)
3025                 return sprintf(page, "external:%s\n", mddev->metadata_type);
3026         else
3027                 return sprintf(page, "none\n");
3028 }
3029
3030 static ssize_t
3031 metadata_store(mddev_t *mddev, const char *buf, size_t len)
3032 {
3033         int major, minor;
3034         char *e;
3035         /* Changing the details of 'external' metadata is
3036          * always permitted.  Otherwise there must be
3037          * no devices attached to the array.
3038          */
3039         if (mddev->external && strncmp(buf, "external:", 9) == 0)
3040                 ;
3041         else if (!list_empty(&mddev->disks))
3042                 return -EBUSY;
3043
3044         if (cmd_match(buf, "none")) {
3045                 mddev->persistent = 0;
3046                 mddev->external = 0;
3047                 mddev->major_version = 0;
3048                 mddev->minor_version = 90;
3049                 return len;
3050         }
3051         if (strncmp(buf, "external:", 9) == 0) {
3052                 size_t namelen = len-9;
3053                 if (namelen >= sizeof(mddev->metadata_type))
3054                         namelen = sizeof(mddev->metadata_type)-1;
3055                 strncpy(mddev->metadata_type, buf+9, namelen);
3056                 mddev->metadata_type[namelen] = 0;
3057                 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3058                         mddev->metadata_type[--namelen] = 0;
3059                 mddev->persistent = 0;
3060                 mddev->external = 1;
3061                 mddev->major_version = 0;
3062                 mddev->minor_version = 90;
3063                 return len;
3064         }
3065         major = simple_strtoul(buf, &e, 10);
3066         if (e==buf || *e != '.')
3067                 return -EINVAL;
3068         buf = e+1;
3069         minor = simple_strtoul(buf, &e, 10);
3070         if (e==buf || (*e && *e != '\n') )
3071                 return -EINVAL;
3072         if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
3073                 return -ENOENT;
3074         mddev->major_version = major;
3075         mddev->minor_version = minor;
3076         mddev->persistent = 1;
3077         mddev->external = 0;
3078         return len;
3079 }
3080
3081 static struct md_sysfs_entry md_metadata =
3082 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
3083
3084 static ssize_t
3085 action_show(mddev_t *mddev, char *page)
3086 {
3087         char *type = "idle";
3088         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3089             (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
3090                 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3091                         type = "reshape";
3092                 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3093                         if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3094                                 type = "resync";
3095                         else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3096                                 type = "check";
3097                         else
3098                                 type = "repair";
3099                 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3100                         type = "recover";
3101         }
3102         return sprintf(page, "%s\n", type);
3103 }
3104
3105 static ssize_t
3106 action_store(mddev_t *mddev, const char *page, size_t len)
3107 {
3108         if (!mddev->pers || !mddev->pers->sync_request)
3109                 return -EINVAL;
3110
3111         if (cmd_match(page, "idle")) {
3112                 if (mddev->sync_thread) {
3113                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3114                         md_unregister_thread(mddev->sync_thread);
3115                         mddev->sync_thread = NULL;
3116                         mddev->recovery = 0;
3117                 }
3118         } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3119                    test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3120                 return -EBUSY;
3121         else if (cmd_match(page, "resync"))
3122                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3123         else if (cmd_match(page, "recover")) {
3124                 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3125                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3126         } else if (cmd_match(page, "reshape")) {
3127                 int err;
3128                 if (mddev->pers->start_reshape == NULL)
3129                         return -EINVAL;
3130                 err = mddev->pers->start_reshape(mddev);
3131                 if (err)
3132                         return err;
3133                 sysfs_notify(&mddev->kobj, NULL, "degraded");
3134         } else {
3135                 if (cmd_match(page, "check"))
3136                         set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3137                 else if (!cmd_match(page, "repair"))
3138                         return -EINVAL;
3139                 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3140                 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3141         }
3142         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3143         md_wakeup_thread(mddev->thread);
3144         sysfs_notify_dirent(mddev->sysfs_action);
3145         return len;
3146 }
3147
3148 static ssize_t
3149 mismatch_cnt_show(mddev_t *mddev, char *page)
3150 {
3151         return sprintf(page, "%llu\n",
3152                        (unsigned long long) mddev->resync_mismatches);
3153 }
3154
3155 static struct md_sysfs_entry md_scan_mode =
3156 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3157
3158
3159 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3160
3161 static ssize_t
3162 sync_min_show(mddev_t *mddev, char *page)
3163 {
3164         return sprintf(page, "%d (%s)\n", speed_min(mddev),
3165                        mddev->sync_speed_min ? "local": "system");
3166 }
3167
3168 static ssize_t
3169 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3170 {
3171         int min;
3172         char *e;
3173         if (strncmp(buf, "system", 6)==0) {
3174                 mddev->sync_speed_min = 0;
3175                 return len;
3176         }
3177         min = simple_strtoul(buf, &e, 10);
3178         if (buf == e || (*e && *e != '\n') || min <= 0)
3179                 return -EINVAL;
3180         mddev->sync_speed_min = min;
3181         return len;
3182 }
3183
3184 static struct md_sysfs_entry md_sync_min =
3185 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3186
3187 static ssize_t
3188 sync_max_show(mddev_t *mddev, char *page)
3189 {
3190         return sprintf(page, "%d (%s)\n", speed_max(mddev),
3191                        mddev->sync_speed_max ? "local": "system");
3192 }
3193
3194 static ssize_t
3195 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3196 {
3197         int max;
3198         char *e;
3199         if (strncmp(buf, "system", 6)==0) {
3200                 mddev->sync_speed_max = 0;
3201                 return len;
3202         }
3203         max = simple_strtoul(buf, &e, 10);
3204         if (buf == e || (*e && *e != '\n') || max <= 0)
3205                 return -EINVAL;
3206         mddev->sync_speed_max = max;
3207         return len;
3208 }
3209
3210 static struct md_sysfs_entry md_sync_max =
3211 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3212
3213 static ssize_t
3214 degraded_show(mddev_t *mddev, char *page)
3215 {
3216         return sprintf(page, "%d\n", mddev->degraded);
3217 }
3218 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3219
3220 static ssize_t
3221 sync_force_parallel_show(mddev_t *mddev, char *page)
3222 {
3223         return sprintf(page, "%d\n", mddev->parallel_resync);
3224 }
3225
3226 static ssize_t
3227 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3228 {
3229         long n;
3230
3231         if (strict_strtol(buf, 10, &n))
3232                 return -EINVAL;
3233
3234         if (n != 0 && n != 1)
3235                 return -EINVAL;
3236
3237         mddev->parallel_resync = n;
3238
3239         if (mddev->sync_thread)
3240                 wake_up(&resync_wait);
3241
3242         return len;
3243 }
3244
3245 /* force parallel resync, even with shared block devices */
3246 static struct md_sysfs_entry md_sync_force_parallel =
3247 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3248        sync_force_parallel_show, sync_force_parallel_store);
3249
3250 static ssize_t
3251 sync_speed_show(mddev_t *mddev, char *page)
3252 {
3253         unsigned long resync, dt, db;
3254         resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3255         dt = (jiffies - mddev->resync_mark) / HZ;
3256         if (!dt) dt++;
3257         db = resync - mddev->resync_mark_cnt;
3258         return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3259 }
3260
3261 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3262
3263 static ssize_t
3264 sync_completed_show(mddev_t *mddev, char *page)
3265 {
3266         unsigned long max_blocks, resync;
3267
3268         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3269                 max_blocks = mddev->resync_max_sectors;
3270         else
3271                 max_blocks = mddev->size << 1;
3272
3273         resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3274         return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3275 }
3276
3277 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3278
3279 static ssize_t
3280 min_sync_show(mddev_t *mddev, char *page)
3281 {
3282         return sprintf(page, "%llu\n",
3283                        (unsigned long long)mddev->resync_min);
3284 }
3285 static ssize_t
3286 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3287 {
3288         unsigned long long min;
3289         if (strict_strtoull(buf, 10, &min))
3290                 return -EINVAL;
3291         if (min > mddev->resync_max)
3292                 return -EINVAL;
3293         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3294                 return -EBUSY;
3295
3296         /* Must be a multiple of chunk_size */
3297         if (mddev->chunk_size) {
3298                 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3299                         return -EINVAL;
3300         }
3301         mddev->resync_min = min;
3302
3303         return len;
3304 }
3305
3306 static struct md_sysfs_entry md_min_sync =
3307 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3308
3309 static ssize_t
3310 max_sync_show(mddev_t *mddev, char *page)
3311 {
3312         if (mddev->resync_max == MaxSector)
3313                 return sprintf(page, "max\n");
3314         else
3315                 return sprintf(page, "%llu\n",
3316                                (unsigned long long)mddev->resync_max);
3317 }
3318 static ssize_t
3319 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3320 {
3321         if (strncmp(buf, "max", 3) == 0)
3322                 mddev->resync_max = MaxSector;
3323         else {
3324                 unsigned long long max;
3325                 if (strict_strtoull(buf, 10, &max))
3326                         return -EINVAL;
3327                 if (max < mddev->resync_min)
3328                         return -EINVAL;
3329                 if (max < mddev->resync_max &&
3330                     test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3331                         return -EBUSY;
3332
3333                 /* Must be a multiple of chunk_size */
3334                 if (mddev->chunk_size) {
3335                         if (max & (sector_t)((mddev->chunk_size>>9)-1))
3336                                 return -EINVAL;
3337                 }
3338                 mddev->resync_max = max;
3339         }
3340         wake_up(&mddev->recovery_wait);
3341         return len;
3342 }
3343
3344 static struct md_sysfs_entry md_max_sync =
3345 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3346
3347 static ssize_t
3348 suspend_lo_show(mddev_t *mddev, char *page)
3349 {
3350         return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3351 }
3352
3353 static ssize_t
3354 suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3355 {
3356         char *e;
3357         unsigned long long new = simple_strtoull(buf, &e, 10);
3358
3359         if (mddev->pers->quiesce == NULL)
3360                 return -EINVAL;
3361         if (buf == e || (*e && *e != '\n'))
3362                 return -EINVAL;
3363         if (new >= mddev->suspend_hi ||
3364             (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3365                 mddev->suspend_lo = new;
3366                 mddev->pers->quiesce(mddev, 2);
3367                 return len;
3368         } else
3369                 return -EINVAL;
3370 }
3371 static struct md_sysfs_entry md_suspend_lo =
3372 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3373
3374
3375 static ssize_t
3376 suspend_hi_show(mddev_t *mddev, char *page)
3377 {
3378         return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3379 }
3380
3381 static ssize_t
3382 suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3383 {
3384         char *e;
3385         unsigned long long new = simple_strtoull(buf, &e, 10);
3386
3387         if (mddev->pers->quiesce == NULL)
3388                 return -EINVAL;
3389         if (buf == e || (*e && *e != '\n'))
3390                 return -EINVAL;
3391         if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3392             (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3393                 mddev->suspend_hi = new;
3394                 mddev->pers->quiesce(mddev, 1);
3395                 mddev->pers->quiesce(mddev, 0);
3396                 return len;
3397         } else
3398                 return -EINVAL;
3399 }
3400 static struct md_sysfs_entry md_suspend_hi =
3401 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3402
3403 static ssize_t
3404 reshape_position_show(mddev_t *mddev, char *page)
3405 {
3406         if (mddev->reshape_position != MaxSector)
3407                 return sprintf(page, "%llu\n",
3408                                (unsigned long long)mddev->reshape_position);
3409         strcpy(page, "none\n");
3410         return 5;
3411 }
3412
3413 static ssize_t
3414 reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3415 {
3416         char *e;
3417         unsigned long long new = simple_strtoull(buf, &e, 10);
3418         if (mddev->pers)
3419                 return -EBUSY;
3420         if (buf == e || (*e && *e != '\n'))
3421                 return -EINVAL;
3422         mddev->reshape_position = new;
3423         mddev->delta_disks = 0;
3424         mddev->new_level = mddev->level;
3425         mddev->new_layout = mddev->layout;
3426         mddev->new_chunk = mddev->chunk_size;
3427         return len;
3428 }
3429
3430 static struct md_sysfs_entry md_reshape_position =
3431 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3432        reshape_position_store);
3433
3434
3435 static struct attribute *md_default_attrs[] = {
3436         &md_level.attr,
3437         &md_layout.attr,
3438         &md_raid_disks.attr,
3439         &md_chunk_size.attr,
3440         &md_size.attr,
3441         &md_resync_start.attr,
3442         &md_metadata.attr,
3443         &md_new_device.attr,
3444         &md_safe_delay.attr,
3445         &md_array_state.attr,
3446         &md_reshape_position.attr,
3447         NULL,
3448 };
3449
3450 static struct attribute *md_redundancy_attrs[] = {
3451         &md_scan_mode.attr,
3452         &md_mismatches.attr,
3453         &md_sync_min.attr,
3454         &md_sync_max.attr,
3455         &md_sync_speed.attr,
3456         &md_sync_force_parallel.attr,
3457         &md_sync_completed.attr,
3458         &md_min_sync.attr,
3459         &md_max_sync.attr,
3460         &md_suspend_lo.attr,
3461         &md_suspend_hi.attr,
3462         &md_bitmap.attr,
3463         &md_degraded.attr,
3464         NULL,
3465 };
3466 static struct attribute_group md_redundancy_group = {
3467         .name = NULL,
3468         .attrs = md_redundancy_attrs,
3469 };
3470
3471
3472 static ssize_t
3473 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3474 {
3475         struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3476         mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3477         ssize_t rv;
3478
3479         if (!entry->show)
3480                 return -EIO;
3481         rv = mddev_lock(mddev);
3482         if (!rv) {
3483                 rv = entry->show(mddev, page);
3484                 mddev_unlock(mddev);
3485         }
3486         return rv;
3487 }
3488
3489 static ssize_t
3490 md_attr_store(struct kobject *kobj, struct attribute *attr,
3491               const char *page, size_t length)
3492 {
3493         struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3494         mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
3495         ssize_t rv;
3496
3497         if (!entry->store)
3498                 return -EIO;
3499         if (!capable(CAP_SYS_ADMIN))
3500                 return -EACCES;
3501         rv = mddev_lock(mddev);
3502         if (mddev->hold_active == UNTIL_IOCTL)
3503                 mddev->hold_active = 0;
3504         if (!rv) {
3505                 rv = entry->store(mddev, page, length);
3506                 mddev_unlock(mddev);
3507         }
3508         return rv;
3509 }
3510
3511 static void md_free(struct kobject *ko)
3512 {
3513         mddev_t *mddev = container_of(ko, mddev_t, kobj);
3514
3515         if (mddev->sysfs_state)
3516                 sysfs_put(mddev->sysfs_state);
3517
3518         if (mddev->gendisk) {
3519                 del_gendisk(mddev->gendisk);
3520                 put_disk(mddev->gendisk);
3521         }
3522         if (mddev->queue)
3523                 blk_cleanup_queue(mddev->queue);
3524
3525         kfree(mddev);
3526 }
3527
3528 static struct sysfs_ops md_sysfs_ops = {
3529         .show   = md_attr_show,
3530         .store  = md_attr_store,
3531 };
3532 static struct kobj_type md_ktype = {
3533         .release        = md_free,
3534         .sysfs_ops      = &md_sysfs_ops,
3535         .default_attrs  = md_default_attrs,
3536 };
3537
3538 int mdp_major = 0;
3539
3540 static void mddev_delayed_delete(struct work_struct *ws)
3541 {
3542         mddev_t *mddev = container_of(ws, mddev_t, del_work);
3543
3544         if (mddev->private == &md_redundancy_group) {
3545                 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3546                 if (mddev->sysfs_action)
3547                         sysfs_put(mddev->sysfs_action);
3548                 mddev->sysfs_action = NULL;
3549                 mddev->private = NULL;
3550         }
3551         kobject_del(&mddev->kobj);
3552         kobject_put(&mddev->kobj);
3553 }
3554
3555 static int md_alloc(dev_t dev, char *name)
3556 {
3557         static DEFINE_MUTEX(disks_mutex);
3558         mddev_t *mddev = mddev_find(dev);
3559         struct gendisk *disk;
3560         int partitioned;
3561         int shift;
3562         int unit;
3563         int error;
3564
3565         if (!mddev)
3566                 return -ENODEV;
3567
3568         partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3569         shift = partitioned ? MdpMinorShift : 0;
3570         unit = MINOR(mddev->unit) >> shift;
3571
3572         /* wait for any previous instance if this device
3573          * to be completed removed (mddev_delayed_delete).
3574          */
3575         flush_scheduled_work();
3576
3577         mutex_lock(&disks_mutex);
3578         if (mddev->gendisk) {
3579                 mutex_unlock(&disks_mutex);
3580                 mddev_put(mddev);
3581                 return -EEXIST;
3582         }
3583
3584         if (name) {
3585                 /* Need to ensure that 'name' is not a duplicate.
3586                  */
3587                 mddev_t *mddev2;
3588                 spin_lock(&all_mddevs_lock);
3589
3590                 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3591                         if (mddev2->gendisk &&
3592                             strcmp(mddev2->gendisk->disk_name, name) == 0) {
3593                                 spin_unlock(&all_mddevs_lock);
3594                                 return -EEXIST;
3595                         }
3596                 spin_unlock(&all_mddevs_lock);
3597         }
3598
3599         mddev->queue = blk_alloc_queue(GFP_KERNEL);
3600         if (!mddev->queue) {
3601                 mutex_unlock(&disks_mutex);
3602                 mddev_put(mddev);
3603                 return -ENOMEM;
3604         }
3605         /* Can be unlocked because the queue is new: no concurrency */
3606         queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3607
3608         blk_queue_make_request(mddev->queue, md_fail_request);
3609
3610         disk = alloc_disk(1 << shift);
3611         if (!disk) {
3612                 mutex_unlock(&disks_mutex);
3613                 blk_cleanup_queue(mddev->queue);
3614                 mddev->queue = NULL;
3615                 mddev_put(mddev);
3616                 return -ENOMEM;
3617         }
3618         disk->major = MAJOR(mddev->unit);
3619         disk->first_minor = unit << shift;
3620         if (name)
3621                 strcpy(disk->disk_name, name);
3622         else if (partitioned)
3623                 sprintf(disk->disk_name, "md_d%d", unit);
3624         else
3625                 sprintf(disk->disk_name, "md%d", unit);
3626         disk->fops = &md_fops;
3627         disk->private_data = mddev;
3628         disk->queue = mddev->queue;
3629         /* Allow extended partitions.  This makes the
3630          * 'mdp' device redundant, but we can't really
3631          * remove it now.
3632          */
3633         disk->flags |= GENHD_FL_EXT_DEVT;
3634         add_disk(disk);
3635         mddev->gendisk = disk;
3636         error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3637                                      &disk_to_dev(disk)->kobj, "%s", "md");
3638         mutex_unlock(&disks_mutex);
3639         if (error)
3640                 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3641                        disk->disk_name);
3642         else {
3643                 kobject_uevent(&mddev->kobj, KOBJ_ADD);
3644                 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3645         }
3646         mddev_put(mddev);
3647         return 0;
3648 }
3649
3650 static struct kobject *md_probe(dev_t dev, int *part, void *data)
3651 {
3652         md_alloc(dev, NULL);
3653         return NULL;
3654 }
3655
3656 static int add_named_array(const char *val, struct kernel_param *kp)
3657 {
3658         /* val must be "md_*" where * is not all digits.
3659          * We allocate an array with a large free minor number, and
3660          * set the name to val.  val must not already be an active name.
3661          */
3662         int len = strlen(val);
3663         char buf[DISK_NAME_LEN];
3664
3665         while (len && val[len-1] == '\n')
3666                 len--;
3667         if (len >= DISK_NAME_LEN)
3668                 return -E2BIG;
3669         strlcpy(buf, val, len+1);
3670         if (strncmp(buf, "md_", 3) != 0)
3671                 return -EINVAL;
3672         return md_alloc(0, buf);
3673 }
3674
3675 static void md_safemode_timeout(unsigned long data)
3676 {
3677         mddev_t *mddev = (mddev_t *) data;
3678
3679         if (!atomic_read(&mddev->writes_pending)) {
3680                 mddev->safemode = 1;
3681                 if (mddev->external)
3682                         sysfs_notify_dirent(mddev->sysfs_state);
3683         }
3684         md_wakeup_thread(mddev->thread);
3685 }
3686
3687 static int start_dirty_degraded;
3688
3689 static int do_md_run(mddev_t * mddev)
3690 {
3691         int err;
3692         int chunk_size;
3693         mdk_rdev_t *rdev;
3694         struct gendisk *disk;
3695         struct mdk_personality *pers;
3696         char b[BDEVNAME_SIZE];
3697
3698         if (list_empty(&mddev->disks))
3699                 /* cannot run an array with no devices.. */
3700                 return -EINVAL;
3701
3702         if (mddev->pers)
3703                 return -EBUSY;
3704
3705         /*
3706          * Analyze all RAID superblock(s)
3707          */
3708         if (!mddev->raid_disks) {
3709                 if (!mddev->persistent)
3710                         return -EINVAL;
3711                 analyze_sbs(mddev);
3712         }
3713
3714         chunk_size = mddev->chunk_size;
3715
3716         if (chunk_size) {
3717                 if (chunk_size > MAX_CHUNK_SIZE) {
3718                         printk(KERN_ERR "too big chunk_size: %d > %d\n",
3719                                 chunk_size, MAX_CHUNK_SIZE);
3720                         return -EINVAL;
3721                 }
3722                 /*
3723                  * chunk-size has to be a power of 2
3724                  */
3725                 if ( (1 << ffz(~chunk_size)) != chunk_size) {
3726                         printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
3727                         return -EINVAL;
3728                 }
3729
3730                 /* devices must have minimum size of one chunk */
3731                 list_for_each_entry(rdev, &mddev->disks, same_set) {
3732                         if (test_bit(Faulty, &rdev->flags))
3733                                 continue;
3734                         if (rdev->size < chunk_size / 1024) {
3735                                 printk(KERN_WARNING
3736                                         "md: Dev %s smaller than chunk_size:"
3737                                         " %lluk < %dk\n",
3738                                         bdevname(rdev->bdev,b),
3739                                         (unsigned long long)rdev->size,
3740                                         chunk_size / 1024);
3741                                 return -EINVAL;
3742                         }
3743                 }
3744         }
3745
3746         if (mddev->level != LEVEL_NONE)
3747                 request_module("md-level-%d", mddev->level);
3748         else if (mddev->clevel[0])
3749                 request_module("md-%s", mddev->clevel);
3750
3751         /*
3752          * Drop all container device buffers, from now on
3753          * the only valid external interface is through the md
3754          * device.
3755          */
3756         list_for_each_entry(rdev, &mddev->disks, same_set) {
3757                 if (test_bit(Faulty, &rdev->flags))
3758                         continue;
3759                 sync_blockdev(rdev->bdev);
3760                 invalidate_bdev(rdev->bdev);
3761
3762                 /* perform some consistency tests on the device.
3763                  * We don't want the data to overlap the metadata,
3764                  * Internal Bitmap issues has handled elsewhere.
3765                  */
3766                 if (rdev->data_offset < rdev->sb_start) {
3767                         if (mddev->size &&
3768                             rdev->data_offset + mddev->size*2
3769                             > rdev->sb_start) {
3770                                 printk("md: %s: data overlaps metadata\n",
3771                                        mdname(mddev));
3772                                 return -EINVAL;
3773                         }
3774                 } else {
3775                         if (rdev->sb_start + rdev->sb_size/512
3776                             > rdev->data_offset) {
3777                                 printk("md: %s: metadata overlaps data\n",
3778                                        mdname(mddev));
3779                                 return -EINVAL;
3780                         }
3781                 }
3782                 sysfs_notify_dirent(rdev->sysfs_state);
3783         }
3784
3785         md_probe(mddev->unit, NULL, NULL);
3786         disk = mddev->gendisk;
3787         if (!disk)
3788                 return -ENOMEM;
3789
3790         spin_lock(&pers_lock);
3791         pers = find_pers(mddev->level, mddev->clevel);
3792         if (!pers || !try_module_get(pers->owner)) {
3793                 spin_unlock(&pers_lock);
3794                 if (mddev->level != LEVEL_NONE)
3795                         printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3796                                mddev->level);
3797                 else
3798                         printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3799                                mddev->clevel);
3800                 return -EINVAL;
3801         }
3802         mddev->pers = pers;
3803         spin_unlock(&pers_lock);
3804         mddev->level = pers->level;
3805         strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3806
3807         if (mddev->reshape_position != MaxSector &&
3808             pers->start_reshape == NULL) {
3809                 /* This personality cannot handle reshaping... */
3810                 mddev->pers = NULL;
3811                 module_put(pers->owner);
3812                 return -EINVAL;
3813         }
3814
3815         if (pers->sync_request) {
3816                 /* Warn if this is a potentially silly
3817                  * configuration.
3818                  */
3819                 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3820                 mdk_rdev_t *rdev2;
3821                 int warned = 0;
3822
3823                 list_for_each_entry(rdev, &mddev->disks, same_set)
3824                         list_for_each_entry(rdev2, &mddev->disks, same_set) {
3825                                 if (rdev < rdev2 &&
3826                                     rdev->bdev->bd_contains ==
3827                                     rdev2->bdev->bd_contains) {
3828                                         printk(KERN_WARNING
3829                                                "%s: WARNING: %s appears to be"
3830                                                " on the same physical disk as"
3831                                                " %s.\n",
3832                                                mdname(mddev),
3833                                                bdevname(rdev->bdev,b),
3834                                                bdevname(rdev2->bdev,b2));
3835                                         warned = 1;
3836                                 }
3837                         }
3838
3839                 if (warned)
3840                         printk(KERN_WARNING
3841                                "True protection against single-disk"
3842                                " failure might be compromised.\n");
3843         }
3844
3845         mddev->recovery = 0;
3846         mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
3847         mddev->barriers_work = 1;
3848         mddev->ok_start_degraded = start_dirty_degraded;
3849
3850         if (start_readonly)
3851                 mddev->ro = 2; /* read-only, but switch on first write */
3852
3853         err = mddev->pers->run(mddev);
3854         if (err)
3855                 printk(KERN_ERR "md: pers->run() failed ...\n");
3856         else if (mddev->pers->sync_request) {
3857                 err = bitmap_create(mddev);
3858                 if (err) {
3859                         printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3860                                mdname(mddev), err);
3861                         mddev->pers->stop(mddev);
3862                 }
3863         }
3864         if (err) {
3865                 module_put(mddev->pers->owner);
3866                 mddev->pers = NULL;
3867                 bitmap_destroy(mddev);
3868                 return err;
3869         }
3870         if (mddev->pers->sync_request) {
3871                 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3872                         printk(KERN_WARNING
3873                                "md: cannot register extra attributes for %s\n",
3874                                mdname(mddev));
3875                 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3876         } else if (mddev->ro == 2) /* auto-readonly not meaningful */
3877                 mddev->ro = 0;
3878
3879         atomic_set(&mddev->writes_pending,0);
3880         mddev->safemode = 0;
3881         mddev->safemode_timer.function = md_safemode_timeout;
3882         mddev->safemode_timer.data = (unsigned long) mddev;
3883         mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
3884         mddev->in_sync = 1;
3885
3886         list_for_each_entry(rdev, &mddev->disks, same_set)
3887                 if (rdev->raid_disk >= 0) {
3888                         char nm[20];
3889                         sprintf(nm, "rd%d", rdev->raid_disk);
3890                         if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3891                                 printk("md: cannot register %s for %s\n",
3892                                        nm, mdname(mddev));
3893                 }
3894         
3895         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3896         
3897         if (mddev->flags)
3898                 md_update_sb(mddev, 0);
3899
3900         set_capacity(disk, mddev->array_sectors);
3901
3902         /* If we call blk_queue_make_request here, it will
3903          * re-initialise max_sectors etc which may have been
3904          * refined inside -> run.  So just set the bits we need to set.
3905          * Most initialisation happended when we called
3906          * blk_queue_make_request(..., md_fail_request)
3907          * earlier.
3908          */
3909         mddev->queue->queuedata = mddev;
3910         mddev->queue->make_request_fn = mddev->pers->make_request;
3911
3912         /* If there is a partially-recovered drive we need to
3913          * start recovery here.  If we leave it to md_check_recovery,
3914          * it will remove the drives and not do the right thing
3915          */
3916         if (mddev->degraded && !mddev->sync_thread) {
3917                 int spares = 0;
3918                 list_for_each_entry(rdev, &mddev->disks, same_set)
3919                         if (rdev->raid_disk >= 0 &&
3920                             !test_bit(In_sync, &rdev->flags) &&
3921                             !test_bit(Faulty, &rdev->flags))
3922                                 /* complete an interrupted recovery */
3923                                 spares++;
3924                 if (spares && mddev->pers->sync_request) {
3925                         mddev->recovery = 0;
3926                         set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3927                         mddev->sync_thread = md_register_thread(md_do_sync,
3928                                                                 mddev,
3929                                                                 "%s_resync");
3930                         if (!mddev->sync_thread) {
3931                                 printk(KERN_ERR "%s: could not start resync"
3932                                        " thread...\n",
3933                                        mdname(mddev));
3934                                 /* leave the spares where they are, it shouldn't hurt */
3935                                 mddev->recovery = 0;
3936                         }
3937                 }
3938         }
3939         md_wakeup_thread(mddev->thread);
3940         md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
3941
3942         mddev->changed = 1;
3943         md_new_event(mddev);
3944         sysfs_notify_dirent(mddev->sysfs_state);
3945         if (mddev->sysfs_action)
3946                 sysfs_notify_dirent(mddev->sysfs_action);
3947         sysfs_notify(&mddev->kobj, NULL, "degraded");
3948         kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
3949         return 0;
3950 }
3951
3952 static int restart_array(mddev_t *mddev)
3953 {
3954         struct gendisk *disk = mddev->gendisk;
3955
3956         /* Complain if it has no devices */
3957         if (list_empty(&mddev->disks))
3958                 return -ENXIO;
3959         if (!mddev->pers)
3960                 return -EINVAL;
3961         if (!mddev->ro)
3962                 return -EBUSY;
3963         mddev->safemode = 0;
3964         mddev->ro = 0;
3965         set_disk_ro(disk, 0);
3966         printk(KERN_INFO "md: %s switched to read-write mode.\n",
3967                 mdname(mddev));
3968         /* Kick recovery or resync if necessary */
3969         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3970         md_wakeup_thread(mddev->thread);
3971         md_wakeup_thread(mddev->sync_thread);
3972         sysfs_notify_dirent(mddev->sysfs_state);
3973         return 0;
3974 }
3975
3976 /* similar to deny_write_access, but accounts for our holding a reference
3977  * to the file ourselves */
3978 static int deny_bitmap_write_access(struct file * file)
3979 {
3980         struct inode *inode = file->f_mapping->host;
3981
3982         spin_lock(&inode->i_lock);
3983         if (atomic_read(&inode->i_writecount) > 1) {
3984                 spin_unlock(&inode->i_lock);
3985                 return -ETXTBSY;
3986         }
3987         atomic_set(&inode->i_writecount, -1);
3988         spin_unlock(&inode->i_lock);
3989
3990         return 0;
3991 }
3992
3993 static void restore_bitmap_write_access(struct file *file)
3994 {
3995         struct inode *inode = file->f_mapping->host;
3996
3997         spin_lock(&inode->i_lock);
3998         atomic_set(&inode->i_writecount, 1);
3999         spin_unlock(&inode->i_lock);
4000 }
4001
4002 /* mode:
4003  *   0 - completely stop and dis-assemble array
4004  *   1 - switch to readonly
4005  *   2 - stop but do not disassemble array
4006  */
4007 static int do_md_stop(mddev_t * mddev, int mode, int is_open)
4008 {
4009         int err = 0;
4010         struct gendisk *disk = mddev->gendisk;
4011
4012         if (atomic_read(&mddev->openers) > is_open) {
4013                 printk("md: %s still in use.\n",mdname(mddev));
4014                 return -EBUSY;
4015         }
4016
4017         if (mddev->pers) {
4018
4019                 if (mddev->sync_thread) {
4020                         set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4021                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4022                         md_unregister_thread(mddev->sync_thread);
4023                         mddev->sync_thread = NULL;
4024                 }
4025
4026                 del_timer_sync(&mddev->safemode_timer);
4027
4028                 switch(mode) {
4029                 case 1: /* readonly */
4030                         err  = -ENXIO;
4031                         if (mddev->ro==1)
4032                                 goto out;
4033                         mddev->ro = 1;
4034                         break;
4035                 case 0: /* disassemble */
4036                 case 2: /* stop */
4037                         bitmap_flush(mddev);
4038                         md_super_wait(mddev);
4039                         if (mddev->ro)
4040                                 set_disk_ro(disk, 0);
4041                         blk_queue_make_request(mddev->queue, md_fail_request);
4042                         mddev->pers->stop(mddev);
4043                         mddev->queue->merge_bvec_fn = NULL;
4044                         mddev->queue->unplug_fn = NULL;
4045                         mddev->queue->backing_dev_info.congested_fn = NULL;
4046                         module_put(mddev->pers->owner);
4047                         if (mddev->pers->sync_request)
4048                                 mddev->private = &md_redundancy_group;
4049                         mddev->pers = NULL;
4050                         /* tell userspace to handle 'inactive' */
4051                         sysfs_notify_dirent(mddev->sysfs_state);
4052
4053                         set_capacity(disk, 0);
4054                         mddev->changed = 1;
4055
4056                         if (mddev->ro)
4057                                 mddev->ro = 0;
4058                 }
4059                 if (!mddev->in_sync || mddev->flags) {
4060                         /* mark array as shutdown cleanly */
4061                         mddev->in_sync = 1;
4062                         md_update_sb(mddev, 1);
4063                 }
4064                 if (mode == 1)
4065                         set_disk_ro(disk, 1);
4066                 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4067         }
4068
4069         /*
4070          * Free resources if final stop
4071          */
4072         if (mode == 0) {
4073                 mdk_rdev_t *rdev;
4074
4075                 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4076
4077                 bitmap_destroy(mddev);
4078                 if (mddev->bitmap_file) {
4079                         restore_bitmap_write_access(mddev->bitmap_file);
4080                         fput(mddev->bitmap_file);
4081                         mddev->bitmap_file = NULL;
4082                 }
4083                 mddev->bitmap_offset = 0;
4084
4085                 list_for_each_entry(rdev, &mddev->disks, same_set)
4086                         if (rdev->raid_disk >= 0) {
4087                                 char nm[20];
4088                                 sprintf(nm, "rd%d", rdev->raid_disk);
4089                                 sysfs_remove_link(&mddev->kobj, nm);
4090                         }
4091
4092                 /* make sure all md_delayed_delete calls have finished */
4093                 flush_scheduled_work();
4094
4095                 export_array(mddev);
4096
4097                 mddev->array_sectors = 0;
4098                 mddev->size = 0;
4099                 mddev->raid_disks = 0;
4100                 mddev->recovery_cp = 0;
4101                 mddev->resync_min = 0;
4102                 mddev->resync_max = MaxSector;
4103                 mddev->reshape_position = MaxSector;
4104                 mddev->external = 0;
4105                 mddev->persistent = 0;
4106                 mddev->level = LEVEL_NONE;
4107                 mddev->clevel[0] = 0;
4108                 mddev->flags = 0;
4109                 mddev->ro = 0;
4110                 mddev->metadata_type[0] = 0;
4111                 mddev->chunk_size = 0;
4112                 mddev->ctime = mddev->utime = 0;
4113                 mddev->layout = 0;
4114                 mddev->max_disks = 0;
4115                 mddev->events = 0;
4116                 mddev->delta_disks = 0;
4117                 mddev->new_level = LEVEL_NONE;
4118                 mddev->new_layout = 0;
4119                 mddev->new_chunk = 0;
4120                 mddev->curr_resync = 0;
4121                 mddev->resync_mismatches = 0;
4122                 mddev->suspend_lo = mddev->suspend_hi = 0;
4123                 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4124                 mddev->recovery = 0;
4125                 mddev->in_sync = 0;
4126                 mddev->changed = 0;
4127                 mddev->degraded = 0;
4128                 mddev->barriers_work = 0;
4129                 mddev->safemode = 0;
4130                 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4131                 if (mddev->hold_active == UNTIL_STOP)
4132                         mddev->hold_active = 0;
4133
4134         } else if (mddev->pers)
4135                 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4136                         mdname(mddev));
4137         err = 0;
4138         md_new_event(mddev);
4139         sysfs_notify_dirent(mddev->sysfs_state);
4140 out:
4141         return err;
4142 }
4143
4144 #ifndef MODULE
4145 static void autorun_array(mddev_t *mddev)
4146 {
4147         mdk_rdev_t *rdev;
4148         int err;
4149
4150         if (list_empty(&mddev->disks))
4151                 return;
4152
4153         printk(KERN_INFO "md: running: ");
4154
4155         list_for_each_entry(rdev, &mddev->disks, same_set) {
4156                 char b[BDEVNAME_SIZE];
4157                 printk("<%s>", bdevname(rdev->bdev,b));
4158         }
4159         printk("\n");
4160
4161         err = do_md_run(mddev);
4162         if (err) {
4163                 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4164                 do_md_stop(mddev, 0, 0);
4165         }
4166 }
4167
4168 /*
4169  * lets try to run arrays based on all disks that have arrived
4170  * until now. (those are in pending_raid_disks)
4171  *
4172  * the method: pick the first pending disk, collect all disks with
4173  * the same UUID, remove all from the pending list and put them into
4174  * the 'same_array' list. Then order this list based on superblock
4175  * update time (freshest comes first), kick out 'old' disks and
4176  * compare superblocks. If everything's fine then run it.
4177  *
4178  * If "unit" is allocated, then bump its reference count
4179  */
4180 static void autorun_devices(int part)
4181 {
4182         mdk_rdev_t *rdev0, *rdev, *tmp;
4183         mddev_t *mddev;
4184         char b[BDEVNAME_SIZE];
4185
4186         printk(KERN_INFO "md: autorun ...\n");
4187         while (!list_empty(&pending_raid_disks)) {
4188                 int unit;
4189                 dev_t dev;
4190                 LIST_HEAD(candidates);
4191                 rdev0 = list_entry(pending_raid_disks.next,
4192                                          mdk_rdev_t, same_set);
4193
4194                 printk(KERN_INFO "md: considering %s ...\n",
4195                         bdevname(rdev0->bdev,b));
4196                 INIT_LIST_HEAD(&candidates);
4197                 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
4198                         if (super_90_load(rdev, rdev0, 0) >= 0) {
4199                                 printk(KERN_INFO "md:  adding %s ...\n",
4200                                         bdevname(rdev->bdev,b));
4201                                 list_move(&rdev->same_set, &candidates);
4202                         }
4203                 /*
4204                  * now we have a set of devices, with all of them having
4205                  * mostly sane superblocks. It's time to allocate the
4206                  * mddev.
4207                  */
4208                 if (part) {
4209                         dev = MKDEV(mdp_major,
4210                                     rdev0->preferred_minor << MdpMinorShift);
4211                         unit = MINOR(dev) >> MdpMinorShift;
4212                 } else {
4213                         dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4214                         unit = MINOR(dev);
4215                 }
4216                 if (rdev0->preferred_minor != unit) {
4217                         printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4218                                bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4219                         break;
4220                 }
4221
4222                 md_probe(dev, NULL, NULL);
4223                 mddev = mddev_find(dev);
4224                 if (!mddev || !mddev->gendisk) {
4225                         if (mddev)
4226                                 mddev_put(mddev);
4227                         printk(KERN_ERR
4228                                 "md: cannot allocate memory for md drive.\n");
4229                         break;
4230                 }
4231                 if (mddev_lock(mddev)) 
4232                         printk(KERN_WARNING "md: %s locked, cannot run\n",
4233                                mdname(mddev));
4234                 else if (mddev->raid_disks || mddev->major_version
4235                          || !list_empty(&mddev->disks)) {
4236                         printk(KERN_WARNING 
4237                                 "md: %s already running, cannot run %s\n",
4238                                 mdname(mddev), bdevname(rdev0->bdev,b));
4239                         mddev_unlock(mddev);
4240                 } else {
4241                         printk(KERN_INFO "md: created %s\n", mdname(mddev));
4242                         mddev->persistent = 1;
4243                         rdev_for_each_list(rdev, tmp, &candidates) {
4244                                 list_del_init(&rdev->same_set);
4245                                 if (bind_rdev_to_array(rdev, mddev))
4246                                         export_rdev(rdev);
4247                         }
4248                         autorun_array(mddev);
4249                         mddev_unlock(mddev);
4250                 }
4251                 /* on success, candidates will be empty, on error
4252                  * it won't...
4253                  */
4254                 rdev_for_each_list(rdev, tmp, &candidates) {
4255                         list_del_init(&rdev->same_set);
4256                         export_rdev(rdev);
4257                 }
4258                 mddev_put(mddev);
4259         }
4260         printk(KERN_INFO "md: ... autorun DONE.\n");
4261 }
4262 #endif /* !MODULE */
4263
4264 static int get_version(void __user * arg)
4265 {
4266         mdu_version_t ver;
4267
4268         ver.major = MD_MAJOR_VERSION;
4269         ver.minor = MD_MINOR_VERSION;
4270         ver.patchlevel = MD_PATCHLEVEL_VERSION;
4271
4272         if (copy_to_user(arg, &ver, sizeof(ver)))
4273                 return -EFAULT;
4274
4275         return 0;
4276 }
4277
4278 static int get_array_info(mddev_t * mddev, void __user * arg)
4279 {
4280         mdu_array_info_t info;
4281         int nr,working,active,failed,spare;
4282         mdk_rdev_t *rdev;
4283
4284         nr=working=active=failed=spare=0;
4285         list_for_each_entry(rdev, &mddev->disks, same_set) {
4286                 nr++;
4287                 if (test_bit(Faulty, &rdev->flags))
4288                         failed++;
4289                 else {
4290                         working++;
4291                         if (test_bit(In_sync, &rdev->flags))
4292                                 active++;       
4293                         else
4294                                 spare++;
4295                 }
4296         }
4297
4298         info.major_version = mddev->major_version;
4299         info.minor_version = mddev->minor_version;
4300         info.patch_version = MD_PATCHLEVEL_VERSION;
4301         info.ctime         = mddev->ctime;
4302         info.level         = mddev->level;
4303         info.size          = mddev->size;
4304         if (info.size != mddev->size) /* overflow */
4305                 info.size = -1;
4306         info.nr_disks      = nr;
4307         info.raid_disks    = mddev->raid_disks;
4308         info.md_minor      = mddev->md_minor;
4309         info.not_persistent= !mddev->persistent;
4310
4311         info.utime         = mddev->utime;
4312         info.state         = 0;
4313         if (mddev->in_sync)
4314                 info.state = (1<<MD_SB_CLEAN);
4315         if (mddev->bitmap && mddev->bitmap_offset)
4316                 info.state = (1<<MD_SB_BITMAP_PRESENT);
4317         info.active_disks  = active;
4318         info.working_disks = working;
4319         info.failed_disks  = failed;
4320         info.spare_disks   = spare;
4321
4322         info.layout        = mddev->layout;
4323         info.chunk_size    = mddev->chunk_size;
4324
4325         if (copy_to_user(arg, &info, sizeof(info)))
4326                 return -EFAULT;
4327
4328         return 0;
4329 }
4330
4331 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
4332 {
4333         mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4334         char *ptr, *buf = NULL;
4335         int err = -ENOMEM;
4336
4337         if (md_allow_write(mddev))
4338                 file = kmalloc(sizeof(*file), GFP_NOIO);
4339         else
4340                 file = kmalloc(sizeof(*file), GFP_KERNEL);
4341
4342         if (!file)
4343                 goto out;
4344
4345         /* bitmap disabled, zero the first byte and copy out */
4346         if (!mddev->bitmap || !mddev->bitmap->file) {
4347                 file->pathname[0] = '\0';
4348                 goto copy_out;
4349         }
4350
4351         buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4352         if (!buf)
4353                 goto out;
4354
4355         ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4356         if (IS_ERR(ptr))
4357                 goto out;
4358
4359         strcpy(file->pathname, ptr);
4360
4361 copy_out:
4362         err = 0;
4363         if (copy_to_user(arg, file, sizeof(*file)))
4364                 err = -EFAULT;
4365 out:
4366         kfree(buf);
4367         kfree(file);
4368         return err;
4369 }
4370
4371 static int get_disk_info(mddev_t * mddev, void __user * arg)
4372 {
4373         mdu_disk_info_t info;
4374         mdk_rdev_t *rdev;
4375
4376         if (copy_from_user(&info, arg, sizeof(info)))
4377                 return -EFAULT;
4378
4379         rdev = find_rdev_nr(mddev, info.number);
4380         if (rdev) {
4381                 info.major = MAJOR(rdev->bdev->bd_dev);
4382                 info.minor = MINOR(rdev->bdev->bd_dev);
4383                 info.raid_disk = rdev->raid_disk;
4384                 info.state = 0;
4385                 if (test_bit(Faulty, &rdev->flags))
4386                         info.state |= (1<<MD_DISK_FAULTY);
4387                 else if (test_bit(In_sync, &rdev->flags)) {
4388                         info.state |= (1<<MD_DISK_ACTIVE);
4389                         info.state |= (1<<MD_DISK_SYNC);
4390                 }
4391                 if (test_bit(WriteMostly, &rdev->flags))
4392                         info.state |= (1<<MD_DISK_WRITEMOSTLY);
4393         } else {
4394                 info.major = info.minor = 0;
4395                 info.raid_disk = -1;
4396                 info.state = (1<<MD_DISK_REMOVED);
4397         }
4398
4399         if (copy_to_user(arg, &info, sizeof(info)))
4400                 return -EFAULT;
4401
4402         return 0;
4403 }
4404
4405 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4406 {
4407         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4408         mdk_rdev_t *rdev;
4409         dev_t dev = MKDEV(info->major,info->minor);
4410
4411         if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4412                 return -EOVERFLOW;
4413
4414         if (!mddev->raid_disks) {
4415                 int err;
4416                 /* expecting a device which has a superblock */
4417                 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4418                 if (IS_ERR(rdev)) {
4419                         printk(KERN_WARNING 
4420                                 "md: md_import_device returned %ld\n",
4421                                 PTR_ERR(rdev));
4422                         return PTR_ERR(rdev);
4423                 }
4424                 if (!list_empty(&mddev->disks)) {
4425                         mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4426                                                         mdk_rdev_t, same_set);
4427                         int err = super_types[mddev->major_version]
4428                                 .load_super(rdev, rdev0, mddev->minor_version);
4429                         if (err < 0) {
4430                                 printk(KERN_WARNING 
4431                                         "md: %s has different UUID to %s\n",
4432                                         bdevname(rdev->bdev,b), 
4433                                         bdevname(rdev0->bdev,b2));
4434                                 export_rdev(rdev);
4435                                 return -EINVAL;
4436                         }
4437                 }
4438                 err = bind_rdev_to_array(rdev, mddev);
4439                 if (err)
4440                         export_rdev(rdev);
4441                 return err;
4442         }
4443
4444         /*
4445          * add_new_disk can be used once the array is assembled
4446          * to add "hot spares".  They must already have a superblock
4447          * written
4448          */
4449         if (mddev->pers) {
4450                 int err;
4451                 if (!mddev->pers->hot_add_disk) {
4452                         printk(KERN_WARNING 
4453                                 "%s: personality does not support diskops!\n",
4454                                mdname(mddev));
4455                         return -EINVAL;
4456                 }
4457                 if (mddev->persistent)
4458                         rdev = md_import_device(dev, mddev->major_version,
4459                                                 mddev->minor_version);
4460                 else
4461                         rdev = md_import_device(dev, -1, -1);
4462                 if (IS_ERR(rdev)) {
4463                         printk(KERN_WARNING 
4464                                 "md: md_import_device returned %ld\n",
4465                                 PTR_ERR(rdev));
4466                         return PTR_ERR(rdev);
4467                 }
4468                 /* set save_raid_disk if appropriate */
4469                 if (!mddev->persistent) {
4470                         if (info->state & (1<<MD_DISK_SYNC)  &&
4471                             info->raid_disk < mddev->raid_disks)
4472                                 rdev->raid_disk = info->raid_disk;
4473                         else
4474                                 rdev->raid_disk = -1;
4475                 } else
4476                         super_types[mddev->major_version].
4477                                 validate_super(mddev, rdev);
4478                 rdev->saved_raid_disk = rdev->raid_disk;
4479
4480                 clear_bit(In_sync, &rdev->flags); /* just to be sure */
4481                 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4482                         set_bit(WriteMostly, &rdev->flags);
4483
4484                 rdev->raid_disk = -1;
4485                 err = bind_rdev_to_array(rdev, mddev);
4486                 if (!err && !mddev->pers->hot_remove_disk) {
4487                         /* If there is hot_add_disk but no hot_remove_disk
4488                          * then added disks for geometry changes,
4489                          * and should be added immediately.
4490                          */
4491                         super_types[mddev->major_version].
4492                                 validate_super(mddev, rdev);
4493                         err = mddev->pers->hot_add_disk(mddev, rdev);
4494                         if (err)
4495                                 unbind_rdev_from_array(rdev);
4496                 }
4497                 if (err)
4498                         export_rdev(rdev);
4499                 else
4500                         sysfs_notify_dirent(rdev->sysfs_state);
4501
4502                 md_update_sb(mddev, 1);
4503                 if (mddev->degraded)
4504                         set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4505                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4506                 md_wakeup_thread(mddev->thread);
4507                 return err;
4508         }
4509
4510         /* otherwise, add_new_disk is only allowed
4511          * for major_version==0 superblocks
4512          */
4513         if (mddev->major_version != 0) {
4514                 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4515                        mdname(mddev));
4516                 return -EINVAL;
4517         }
4518
4519         if (!(info->state & (1<<MD_DISK_FAULTY))) {
4520                 int err;
4521                 rdev = md_import_device(dev, -1, 0);
4522                 if (IS_ERR(rdev)) {
4523                         printk(KERN_WARNING 
4524                                 "md: error, md_import_device() returned %ld\n",
4525                                 PTR_ERR(rdev));
4526                         return PTR_ERR(rdev);
4527                 }
4528                 rdev->desc_nr = info->number;
4529                 if (info->raid_disk < mddev->raid_disks)
4530                         rdev->raid_disk = info->raid_disk;
4531                 else
4532                         rdev->raid_disk = -1;
4533
4534                 if (rdev->raid_disk < mddev->raid_disks)
4535                         if (info->state & (1<<MD_DISK_SYNC))
4536                                 set_bit(In_sync, &rdev->flags);
4537
4538                 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4539                         set_bit(WriteMostly, &rdev->flags);
4540
4541                 if (!mddev->persistent) {
4542                         printk(KERN_INFO "md: nonpersistent superblock ...\n");
4543                         rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4544                 } else 
4545                         rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4546                 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
4547
4548                 err = bind_rdev_to_array(rdev, mddev);
4549                 if (err) {
4550                         export_rdev(rdev);
4551                         return err;
4552                 }
4553         }
4554
4555         return 0;
4556 }
4557
4558 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4559 {
4560         char b[BDEVNAME_SIZE];
4561         mdk_rdev_t *rdev;
4562
4563         rdev = find_rdev(mddev, dev);
4564         if (!rdev)
4565                 return -ENXIO;
4566
4567         if (rdev->raid_disk >= 0)
4568                 goto busy;
4569
4570         kick_rdev_from_array(rdev);
4571         md_update_sb(mddev, 1);
4572         md_new_event(mddev);
4573
4574         return 0;
4575 busy:
4576         printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
4577                 bdevname(rdev->bdev,b), mdname(mddev));
4578         return -EBUSY;
4579 }
4580
4581 static int hot_add_disk(mddev_t * mddev, dev_t dev)
4582 {
4583         char b[BDEVNAME_SIZE];
4584         int err;
4585         mdk_rdev_t *rdev;
4586
4587         if (!mddev->pers)
4588                 return -ENODEV;
4589
4590         if (mddev->major_version != 0) {
4591                 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4592                         " version-0 superblocks.\n",
4593                         mdname(mddev));
4594                 return -EINVAL;
4595         }
4596         if (!mddev->pers->hot_add_disk) {
4597                 printk(KERN_WARNING 
4598                         "%s: personality does not support diskops!\n",
4599                         mdname(mddev));
4600                 return -EINVAL;
4601         }
4602
4603         rdev = md_import_device(dev, -1, 0);
4604         if (IS_ERR(rdev)) {
4605                 printk(KERN_WARNING 
4606                         "md: error, md_import_device() returned %ld\n",
4607                         PTR_ERR(rdev));
4608                 return -EINVAL;
4609         }
4610
4611         if (mddev->persistent)
4612                 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
4613         else
4614                 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
4615
4616         rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
4617
4618         if (test_bit(Faulty, &rdev->flags)) {
4619                 printk(KERN_WARNING 
4620                         "md: can not hot-add faulty %s disk to %s!\n",
4621                         bdevname(rdev->bdev,b), mdname(mddev));
4622                 err = -EINVAL;
4623                 goto abort_export;
4624         }
4625         clear_bit(In_sync, &rdev->flags);
4626         rdev->desc_nr = -1;
4627         rdev->saved_raid_disk = -1;
4628         err = bind_rdev_to_array(rdev, mddev);
4629         if (err)
4630                 goto abort_export;
4631
4632         /*
4633          * The rest should better be atomic, we can have disk failures
4634          * noticed in interrupt contexts ...
4635          */
4636
4637         rdev->raid_disk = -1;
4638
4639         md_update_sb(mddev, 1);
4640
4641         /*
4642          * Kick recovery, maybe this spare has to be added to the
4643          * array immediately.
4644          */
4645         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4646         md_wakeup_thread(mddev->thread);
4647         md_new_event(mddev);
4648         return 0;
4649
4650 abort_export:
4651         export_rdev(rdev);
4652         return err;
4653 }
4654
4655 static int set_bitmap_file(mddev_t *mddev, int fd)
4656 {
4657         int err;
4658
4659         if (mddev->pers) {
4660                 if (!mddev->pers->quiesce)
4661                         return -EBUSY;
4662                 if (mddev->recovery || mddev->sync_thread)
4663                         return -EBUSY;
4664                 /* we should be able to change the bitmap.. */
4665         }
4666
4667
4668         if (fd >= 0) {
4669                 if (mddev->bitmap)
4670                         return -EEXIST; /* cannot add when bitmap is present */
4671                 mddev->bitmap_file = fget(fd);
4672
4673                 if (mddev->bitmap_file == NULL) {
4674                         printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4675                                mdname(mddev));
4676                         return -EBADF;
4677                 }
4678
4679                 err = deny_bitmap_write_access(mddev->bitmap_file);
4680                 if (err) {
4681                         printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4682                                mdname(mddev));
4683                         fput(mddev->bitmap_file);
4684                         mddev->bitmap_file = NULL;
4685                         return err;
4686                 }
4687                 mddev->bitmap_offset = 0; /* file overrides offset */
4688         } else if (mddev->bitmap == NULL)
4689                 return -ENOENT; /* cannot remove what isn't there */
4690         err = 0;
4691         if (mddev->pers) {
4692                 mddev->pers->quiesce(mddev, 1);
4693                 if (fd >= 0)
4694                         err = bitmap_create(mddev);
4695                 if (fd < 0 || err) {
4696                         bitmap_destroy(mddev);
4697                         fd = -1; /* make sure to put the file */
4698                 }
4699                 mddev->pers->quiesce(mddev, 0);
4700         }
4701         if (fd < 0) {
4702                 if (mddev->bitmap_file) {
4703                         restore_bitmap_write_access(mddev->bitmap_file);
4704                         fput(mddev->bitmap_file);
4705                 }
4706                 mddev->bitmap_file = NULL;
4707         }
4708
4709         return err;
4710 }
4711
4712 /*
4713  * set_array_info is used two different ways
4714  * The original usage is when creating a new array.
4715  * In this usage, raid_disks is > 0 and it together with
4716  *  level, size, not_persistent,layout,chunksize determine the
4717  *  shape of the array.
4718  *  This will always create an array with a type-0.90.0 superblock.
4719  * The newer usage is when assembling an array.
4720  *  In this case raid_disks will be 0, and the major_version field is
4721  *  use to determine which style super-blocks are to be found on the devices.
4722  *  The minor and patch _version numbers are also kept incase the
4723  *  super_block handler wishes to interpret them.
4724  */
4725 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4726 {
4727
4728         if (info->raid_disks == 0) {
4729                 /* just setting version number for superblock loading */
4730                 if (info->major_version < 0 ||
4731                     info->major_version >= ARRAY_SIZE(super_types) ||
4732                     super_types[info->major_version].name == NULL) {
4733                         /* maybe try to auto-load a module? */
4734                         printk(KERN_INFO 
4735                                 "md: superblock version %d not known\n",
4736                                 info->major_version);
4737                         return -EINVAL;
4738                 }
4739                 mddev->major_version = info->major_version;
4740                 mddev->minor_version = info->minor_version;
4741                 mddev->patch_version = info->patch_version;
4742                 mddev->persistent = !info->not_persistent;
4743                 return 0;
4744         }
4745         mddev->major_version = MD_MAJOR_VERSION;
4746         mddev->minor_version = MD_MINOR_VERSION;
4747         mddev->patch_version = MD_PATCHLEVEL_VERSION;
4748         mddev->ctime         = get_seconds();
4749
4750         mddev->level         = info->level;
4751         mddev->clevel[0]     = 0;
4752         mddev->size          = info->size;
4753         mddev->raid_disks    = info->raid_disks;
4754         /* don't set md_minor, it is determined by which /dev/md* was
4755          * openned
4756          */
4757         if (info->state & (1<<MD_SB_CLEAN))
4758                 mddev->recovery_cp = MaxSector;
4759         else
4760                 mddev->recovery_cp = 0;
4761         mddev->persistent    = ! info->not_persistent;
4762         mddev->external      = 0;
4763
4764         mddev->layout        = info->layout;
4765         mddev->chunk_size    = info->chunk_size;
4766
4767         mddev->max_disks     = MD_SB_DISKS;
4768
4769         if (mddev->persistent)
4770                 mddev->flags         = 0;
4771         set_bit(MD_CHANGE_DEVS, &mddev->flags);
4772
4773         mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4774         mddev->bitmap_offset = 0;
4775
4776         mddev->reshape_position = MaxSector;
4777
4778         /*
4779          * Generate a 128 bit UUID
4780          */
4781         get_random_bytes(mddev->uuid, 16);
4782
4783         mddev->new_level = mddev->level;
4784         mddev->new_chunk = mddev->chunk_size;
4785         mddev->new_layout = mddev->layout;
4786         mddev->delta_disks = 0;
4787
4788         return 0;
4789 }
4790
4791 static int update_size(mddev_t *mddev, sector_t num_sectors)
4792 {
4793         mdk_rdev_t *rdev;
4794         int rv;
4795         int fit = (num_sectors == 0);
4796
4797         if (mddev->pers->resize == NULL)
4798                 return -EINVAL;
4799         /* The "num_sectors" is the number of sectors of each device that
4800          * is used.  This can only make sense for arrays with redundancy.
4801          * linear and raid0 always use whatever space is available. We can only
4802          * consider changing this number if no resync or reconstruction is
4803          * happening, and if the new size is acceptable. It must fit before the
4804          * sb_start or, if that is <data_offset, it must fit before the size
4805          * of each device.  If num_sectors is zero, we find the largest size
4806          * that fits.
4807
4808          */
4809         if (mddev->sync_thread)
4810                 return -EBUSY;
4811         if (mddev->bitmap)
4812                 /* Sorry, cannot grow a bitmap yet, just remove it,
4813                  * grow, and re-add.
4814                  */
4815                 return -EBUSY;
4816         list_for_each_entry(rdev, &mddev->disks, same_set) {
4817                 sector_t avail;
4818                 avail = rdev->size * 2;
4819
4820                 if (fit && (num_sectors == 0 || num_sectors > avail))
4821                         num_sectors = avail;
4822                 if (avail < num_sectors)
4823                         return -ENOSPC;
4824         }
4825         rv = mddev->pers->resize(mddev, num_sectors);
4826         if (!rv) {
4827                 struct block_device *bdev;
4828
4829                 bdev = bdget_disk(mddev->gendisk, 0);
4830                 if (bdev) {
4831                         mutex_lock(&bdev->bd_inode->i_mutex);
4832                         i_size_write(bdev->bd_inode,
4833                                      (loff_t)mddev->array_sectors << 9);
4834                         mutex_unlock(&bdev->bd_inode->i_mutex);
4835                         bdput(bdev);
4836                 }
4837         }
4838         return rv;
4839 }
4840
4841 static int update_raid_disks(mddev_t *mddev, int raid_disks)
4842 {
4843         int rv;
4844         /* change the number of raid disks */
4845         if (mddev->pers->check_reshape == NULL)
4846                 return -EINVAL;
4847         if (raid_disks <= 0 ||
4848             raid_disks >= mddev->max_disks)
4849                 return -EINVAL;
4850         if (mddev->sync_thread || mddev->reshape_position != MaxSector)
4851                 return -EBUSY;
4852         mddev->delta_disks = raid_disks - mddev->raid_disks;
4853
4854         rv = mddev->pers->check_reshape(mddev);
4855         return rv;
4856 }
4857
4858
4859 /*
4860  * update_array_info is used to change the configuration of an
4861  * on-line array.
4862  * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4863  * fields in the info are checked against the array.
4864  * Any differences that cannot be handled will cause an error.
4865  * Normally, only one change can be managed at a time.
4866  */
4867 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4868 {
4869         int rv = 0;
4870         int cnt = 0;
4871         int state = 0;
4872
4873         /* calculate expected state,ignoring low bits */
4874         if (mddev->bitmap && mddev->bitmap_offset)
4875                 state |= (1 << MD_SB_BITMAP_PRESENT);
4876
4877         if (mddev->major_version != info->major_version ||
4878             mddev->minor_version != info->minor_version ||
4879 /*          mddev->patch_version != info->patch_version || */
4880             mddev->ctime         != info->ctime         ||
4881             mddev->level         != info->level         ||
4882 /*          mddev->layout        != info->layout        || */
4883             !mddev->persistent   != info->not_persistent||
4884             mddev->chunk_size    != info->chunk_size    ||
4885             /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4886             ((state^info->state) & 0xfffffe00)
4887                 )
4888                 return -EINVAL;
4889         /* Check there is only one change */
4890         if (info->size >= 0 && mddev->size != info->size) cnt++;
4891         if (mddev->raid_disks != info->raid_disks) cnt++;
4892         if (mddev->layout != info->layout) cnt++;
4893         if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
4894         if (cnt == 0) return 0;
4895         if (cnt > 1) return -EINVAL;
4896
4897         if (mddev->layout != info->layout) {
4898                 /* Change layout
4899                  * we don't need to do anything at the md level, the
4900                  * personality will take care of it all.
4901                  */
4902                 if (mddev->pers->reconfig == NULL)
4903                         return -EINVAL;
4904                 else
4905                         return mddev->pers->reconfig(mddev, info->layout, -1);
4906         }
4907         if (info->size >= 0 && mddev->size != info->size)
4908                 rv = update_size(mddev, (sector_t)info->size * 2);
4909
4910         if (mddev->raid_disks    != info->raid_disks)
4911                 rv = update_raid_disks(mddev, info->raid_disks);
4912
4913         if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4914                 if (mddev->pers->quiesce == NULL)
4915                         return -EINVAL;
4916                 if (mddev->recovery || mddev->sync_thread)
4917                         return -EBUSY;
4918                 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4919                         /* add the bitmap */
4920                         if (mddev->bitmap)
4921                                 return -EEXIST;
4922                         if (mddev->default_bitmap_offset == 0)
4923                                 return -EINVAL;
4924                         mddev->bitmap_offset = mddev->default_bitmap_offset;
4925                         mddev->pers->quiesce(mddev, 1);
4926                         rv = bitmap_create(mddev);
4927                         if (rv)
4928                                 bitmap_destroy(mddev);
4929                         mddev->pers->quiesce(mddev, 0);
4930                 } else {
4931                         /* remove the bitmap */
4932                         if (!mddev->bitmap)
4933                                 return -ENOENT;
4934                         if (mddev->bitmap->file)
4935                                 return -EINVAL;
4936                         mddev->pers->quiesce(mddev, 1);
4937                         bitmap_destroy(mddev);
4938                         mddev->pers->quiesce(mddev, 0);
4939                         mddev->bitmap_offset = 0;
4940                 }
4941         }
4942         md_update_sb(mddev, 1);
4943         return rv;
4944 }
4945
4946 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4947 {
4948         mdk_rdev_t *rdev;
4949
4950         if (mddev->pers == NULL)
4951                 return -ENODEV;
4952
4953         rdev = find_rdev(mddev, dev);
4954         if (!rdev)
4955                 return -ENODEV;
4956
4957         md_error(mddev, rdev);
4958         return 0;
4959 }
4960
4961 /*
4962  * We have a problem here : there is no easy way to give a CHS
4963  * virtual geometry. We currently pretend that we have a 2 heads
4964  * 4 sectors (with a BIG number of cylinders...). This drives
4965  * dosfs just mad... ;-)
4966  */
4967 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4968 {
4969         mddev_t *mddev = bdev->bd_disk->private_data;
4970
4971         geo->heads = 2;
4972         geo->sectors = 4;
4973         geo->cylinders = get_capacity(mddev->gendisk) / 8;
4974         return 0;
4975 }
4976
4977 static int md_ioctl(struct block_device *bdev, fmode_t mode,
4978                         unsigned int cmd, unsigned long arg)
4979 {
4980         int err = 0;
4981         void __user *argp = (void __user *)arg;
4982         mddev_t *mddev = NULL;
4983
4984         if (!capable(CAP_SYS_ADMIN))
4985                 return -EACCES;
4986
4987         /*
4988          * Commands dealing with the RAID driver but not any
4989          * particular array:
4990          */
4991         switch (cmd)
4992         {
4993                 case RAID_VERSION:
4994                         err = get_version(argp);
4995                         goto done;
4996
4997                 case PRINT_RAID_DEBUG:
4998                         err = 0;
4999                         md_print_devices();
5000                         goto done;
5001
5002 #ifndef MODULE
5003                 case RAID_AUTORUN:
5004                         err = 0;
5005                         autostart_arrays(arg);
5006                         goto done;
5007 #endif
5008                 default:;
5009         }
5010
5011         /*
5012          * Commands creating/starting a new array:
5013          */
5014
5015         mddev = bdev->bd_disk->private_data;
5016
5017         if (!mddev) {
5018                 BUG();
5019                 goto abort;
5020         }
5021
5022         err = mddev_lock(mddev);
5023         if (err) {
5024                 printk(KERN_INFO 
5025                         "md: ioctl lock interrupted, reason %d, cmd %d\n",
5026                         err, cmd);
5027                 goto abort;
5028         }
5029
5030         switch (cmd)
5031         {
5032                 case SET_ARRAY_INFO:
5033                         {
5034                                 mdu_array_info_t info;
5035                                 if (!arg)
5036                                         memset(&info, 0, sizeof(info));
5037                                 else if (copy_from_user(&info, argp, sizeof(info))) {
5038                                         err = -EFAULT;
5039                                         goto abort_unlock;
5040                                 }
5041                                 if (mddev->pers) {
5042                                         err = update_array_info(mddev, &info);
5043                                         if (err) {
5044                                                 printk(KERN_WARNING "md: couldn't update"
5045                                                        " array info. %d\n", err);
5046                                                 goto abort_unlock;
5047                                         }
5048                                         goto done_unlock;
5049                                 }
5050                                 if (!list_empty(&mddev->disks)) {
5051                                         printk(KERN_WARNING
5052                                                "md: array %s already has disks!\n",
5053                                                mdname(mddev));
5054                                         err = -EBUSY;
5055                                         goto abort_unlock;
5056                                 }
5057                                 if (mddev->raid_disks) {
5058                                         printk(KERN_WARNING
5059                                                "md: array %s already initialised!\n",
5060                                                mdname(mddev));
5061                                         err = -EBUSY;
5062                                         goto abort_unlock;
5063                                 }
5064                                 err = set_array_info(mddev, &info);
5065                                 if (err) {
5066                                         printk(KERN_WARNING "md: couldn't set"
5067                                                " array info. %d\n", err);
5068                                         goto abort_unlock;
5069                                 }
5070                         }
5071                         goto done_unlock;
5072
5073                 default:;
5074         }
5075
5076         /*
5077          * Commands querying/configuring an existing array:
5078          */
5079         /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
5080          * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
5081         if ((!mddev->raid_disks && !mddev->external)
5082             && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5083             && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5084             && cmd != GET_BITMAP_FILE) {
5085                 err = -ENODEV;
5086                 goto abort_unlock;
5087         }
5088
5089         /*
5090          * Commands even a read-only array can execute:
5091          */
5092         switch (cmd)
5093         {
5094                 case GET_ARRAY_INFO:
5095                         err = get_array_info(mddev, argp);
5096                         goto done_unlock;
5097
5098                 case GET_BITMAP_FILE:
5099                         err = get_bitmap_file(mddev, argp);
5100                         goto done_unlock;
5101
5102                 case GET_DISK_INFO:
5103                         err = get_disk_info(mddev, argp);
5104                         goto done_unlock;
5105
5106                 case RESTART_ARRAY_RW:
5107                         err = restart_array(mddev);
5108                         goto done_unlock;
5109
5110                 case STOP_ARRAY:
5111                         err = do_md_stop(mddev, 0, 1);
5112                         goto done_unlock;
5113
5114                 case STOP_ARRAY_RO:
5115                         err = do_md_stop(mddev, 1, 1);
5116                         goto done_unlock;
5117
5118         }
5119
5120         /*
5121          * The remaining ioctls are changing the state of the
5122          * superblock, so we do not allow them on read-only arrays.
5123          * However non-MD ioctls (e.g. get-size) will still come through
5124          * here and hit the 'default' below, so only disallow
5125          * 'md' ioctls, and switch to rw mode if started auto-readonly.
5126          */
5127         if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
5128                 if (mddev->ro == 2) {
5129                         mddev->ro = 0;
5130                         sysfs_notify_dirent(mddev->sysfs_state);
5131                         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5132                         md_wakeup_thread(mddev->thread);
5133                 } else {
5134                         err = -EROFS;
5135                         goto abort_unlock;
5136                 }
5137         }
5138
5139         switch (cmd)
5140         {
5141                 case ADD_NEW_DISK:
5142                 {
5143                         mdu_disk_info_t info;
5144                         if (copy_from_user(&info, argp, sizeof(info)))
5145                                 err = -EFAULT;
5146                         else
5147                                 err = add_new_disk(mddev, &info);
5148                         goto done_unlock;
5149                 }
5150
5151                 case HOT_REMOVE_DISK:
5152                         err = hot_remove_disk(mddev, new_decode_dev(arg));
5153                         goto done_unlock;
5154
5155                 case HOT_ADD_DISK:
5156                         err = hot_add_disk(mddev, new_decode_dev(arg));
5157                         goto done_unlock;
5158
5159                 case SET_DISK_FAULTY:
5160                         err = set_disk_faulty(mddev, new_decode_dev(arg));
5161                         goto done_unlock;
5162
5163                 case RUN_ARRAY:
5164                         err = do_md_run(mddev);
5165                         goto done_unlock;
5166
5167                 case SET_BITMAP_FILE:
5168                         err = set_bitmap_file(mddev, (int)arg);
5169                         goto done_unlock;
5170
5171                 default:
5172                         err = -EINVAL;
5173                         goto abort_unlock;
5174         }
5175
5176 done_unlock:
5177 abort_unlock:
5178         if (mddev->hold_active == UNTIL_IOCTL &&
5179             err != -EINVAL)
5180                 mddev->hold_active = 0;
5181         mddev_unlock(mddev);
5182
5183         return err;
5184 done:
5185         if (err)
5186                 MD_BUG();
5187 abort:
5188         return err;
5189 }
5190
5191 static int md_open(struct block_device *bdev, fmode_t mode)
5192 {
5193         /*
5194          * Succeed if we can lock the mddev, which confirms that
5195          * it isn't being stopped right now.
5196          */
5197         mddev_t *mddev = mddev_find(bdev->bd_dev);
5198         int err;
5199
5200         if (mddev->gendisk != bdev->bd_disk) {
5201                 /* we are racing with mddev_put which is discarding this
5202                  * bd_disk.
5203                  */
5204                 mddev_put(mddev);
5205                 /* Wait until bdev->bd_disk is definitely gone */
5206                 flush_scheduled_work();
5207                 /* Then retry the open from the top */
5208                 return -ERESTARTSYS;
5209         }
5210         BUG_ON(mddev != bdev->bd_disk->private_data);
5211
5212         if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
5213                 goto out;
5214
5215         err = 0;
5216         atomic_inc(&mddev->openers);
5217         mddev_unlock(mddev);
5218
5219         check_disk_change(bdev);
5220  out:
5221         return err;
5222 }
5223
5224 static int md_release(struct gendisk *disk, fmode_t mode)
5225 {
5226         mddev_t *mddev = disk->private_data;
5227
5228         BUG_ON(!mddev);
5229         atomic_dec(&mddev->openers);
5230         mddev_put(mddev);
5231
5232         return 0;
5233 }
5234
5235 static int md_media_changed(struct gendisk *disk)
5236 {
5237         mddev_t *mddev = disk->private_data;
5238
5239         return mddev->changed;
5240 }
5241
5242 static int md_revalidate(struct gendisk *disk)
5243 {
5244         mddev_t *mddev = disk->private_data;
5245
5246         mddev->changed = 0;
5247         return 0;
5248 }
5249 static struct block_device_operations md_fops =
5250 {
5251         .owner          = THIS_MODULE,
5252         .open           = md_open,
5253         .release        = md_release,
5254         .locked_ioctl   = md_ioctl,
5255         .getgeo         = md_getgeo,
5256         .media_changed  = md_media_changed,
5257         .revalidate_disk= md_revalidate,
5258 };
5259
5260 static int md_thread(void * arg)
5261 {
5262         mdk_thread_t *thread = arg;
5263
5264         /*
5265          * md_thread is a 'system-thread', it's priority should be very
5266          * high. We avoid resource deadlocks individually in each
5267          * raid personality. (RAID5 does preallocation) We also use RR and
5268          * the very same RT priority as kswapd, thus we will never get
5269          * into a priority inversion deadlock.
5270          *
5271          * we definitely have to have equal or higher priority than
5272          * bdflush, otherwise bdflush will deadlock if there are too
5273          * many dirty RAID5 blocks.
5274          */
5275
5276         allow_signal(SIGKILL);
5277         while (!kthread_should_stop()) {
5278
5279                 /* We need to wait INTERRUPTIBLE so that
5280                  * we don't add to the load-average.
5281                  * That means we need to be sure no signals are
5282                  * pending
5283                  */
5284                 if (signal_pending(current))
5285                         flush_signals(current);
5286
5287                 wait_event_interruptible_timeout
5288                         (thread->wqueue,
5289                          test_bit(THREAD_WAKEUP, &thread->flags)
5290                          || kthread_should_stop(),
5291                          thread->timeout);
5292
5293                 clear_bit(THREAD_WAKEUP, &thread->flags);
5294
5295                 thread->run(thread->mddev);
5296         }
5297
5298         return 0;
5299 }
5300
5301 void md_wakeup_thread(mdk_thread_t *thread)
5302 {
5303         if (thread) {
5304                 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5305                 set_bit(THREAD_WAKEUP, &thread->flags);
5306                 wake_up(&thread->wqueue);
5307         }
5308 }
5309
5310 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5311                                  const char *name)
5312 {
5313         mdk_thread_t *thread;
5314
5315         thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
5316         if (!thread)
5317                 return NULL;
5318
5319         init_waitqueue_head(&thread->wqueue);
5320
5321         thread->run = run;
5322         thread->mddev = mddev;
5323         thread->timeout = MAX_SCHEDULE_TIMEOUT;
5324         thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
5325         if (IS_ERR(thread->tsk)) {
5326                 kfree(thread);
5327                 return NULL;
5328         }
5329         return thread;
5330 }
5331
5332 void md_unregister_thread(mdk_thread_t *thread)
5333 {
5334         dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
5335
5336         kthread_stop(thread->tsk);
5337         kfree(thread);
5338 }
5339
5340 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5341 {
5342         if (!mddev) {
5343                 MD_BUG();
5344                 return;
5345         }
5346
5347         if (!rdev || test_bit(Faulty, &rdev->flags))
5348                 return;
5349
5350         if (mddev->external)
5351                 set_bit(Blocked, &rdev->flags);
5352 /*
5353         dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5354                 mdname(mddev),
5355                 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5356                 __builtin_return_address(0),__builtin_return_address(1),
5357                 __builtin_return_address(2),__builtin_return_address(3));
5358 */
5359         if (!mddev->pers)
5360                 return;
5361         if (!mddev->pers->error_handler)
5362                 return;
5363         mddev->pers->error_handler(mddev,rdev);
5364         if (mddev->degraded)
5365                 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5366         set_bit(StateChanged, &rdev->flags);
5367         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5368         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5369         md_wakeup_thread(mddev->thread);
5370         md_new_event_inintr(mddev);
5371 }
5372
5373 /* seq_file implementation /proc/mdstat */
5374
5375 static void status_unused(struct seq_file *seq)
5376 {
5377         int i = 0;
5378         mdk_rdev_t *rdev;
5379
5380         seq_printf(seq, "unused devices: ");
5381
5382         list_for_each_entry(rdev, &pending_raid_disks, same_set) {
5383                 char b[BDEVNAME_SIZE];
5384                 i++;
5385                 seq_printf(seq, "%s ",
5386                               bdevname(rdev->bdev,b));
5387         }
5388         if (!i)
5389                 seq_printf(seq, "<none>");
5390
5391         seq_printf(seq, "\n");
5392 }
5393
5394
5395 static void status_resync(struct seq_file *seq, mddev_t * mddev)
5396 {
5397         sector_t max_blocks, resync, res;
5398         unsigned long dt, db, rt;
5399         int scale;
5400         unsigned int per_milli;
5401
5402         resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5403
5404         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5405                 max_blocks = mddev->resync_max_sectors >> 1;
5406         else
5407                 max_blocks = mddev->size;
5408
5409         /*
5410          * Should not happen.
5411          */
5412         if (!max_blocks) {
5413                 MD_BUG();
5414                 return;
5415         }
5416         /* Pick 'scale' such that (resync>>scale)*1000 will fit
5417          * in a sector_t, and (max_blocks>>scale) will fit in a
5418          * u32, as those are the requirements for sector_div.
5419          * Thus 'scale' must be at least 10
5420          */
5421         scale = 10;
5422         if (sizeof(sector_t) > sizeof(unsigned long)) {
5423                 while ( max_blocks/2 > (1ULL<<(scale+32)))
5424                         scale++;
5425         }
5426         res = (resync>>scale)*1000;
5427         sector_div(res, (u32)((max_blocks>>scale)+1));
5428
5429         per_milli = res;
5430         {
5431                 int i, x = per_milli/50, y = 20-x;
5432                 seq_printf(seq, "[");
5433                 for (i = 0; i < x; i++)
5434                         seq_printf(seq, "=");
5435                 seq_printf(seq, ">");
5436                 for (i = 0; i < y; i++)
5437                         seq_printf(seq, ".");
5438                 seq_printf(seq, "] ");
5439         }
5440         seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
5441                    (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5442                     "reshape" :
5443                     (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5444                      "check" :
5445                      (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5446                       "resync" : "recovery"))),
5447                    per_milli/10, per_milli % 10,
5448                    (unsigned long long) resync,
5449                    (unsigned long long) max_blocks);
5450
5451         /*
5452          * We do not want to overflow, so the order of operands and
5453          * the * 100 / 100 trick are important. We do a +1 to be
5454          * safe against division by zero. We only estimate anyway.
5455          *
5456          * dt: time from mark until now
5457          * db: blocks written from mark until now
5458          * rt: remaining time
5459          */
5460         dt = ((jiffies - mddev->resync_mark) / HZ);
5461         if (!dt) dt++;
5462         db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5463                 - mddev->resync_mark_cnt;
5464         rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
5465
5466         seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5467
5468         seq_printf(seq, " speed=%ldK/sec", db/2/dt);
5469 }
5470
5471 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5472 {
5473         struct list_head *tmp;
5474         loff_t l = *pos;
5475         mddev_t *mddev;
5476
5477         if (l >= 0x10000)
5478                 return NULL;
5479         if (!l--)
5480                 /* header */
5481                 return (void*)1;
5482
5483         spin_lock(&all_mddevs_lock);
5484         list_for_each(tmp,&all_mddevs)
5485                 if (!l--) {
5486                         mddev = list_entry(tmp, mddev_t, all_mddevs);
5487                         mddev_get(mddev);
5488                         spin_unlock(&all_mddevs_lock);
5489                         return mddev;
5490                 }
5491         spin_unlock(&all_mddevs_lock);
5492         if (!l--)
5493                 return (void*)2;/* tail */
5494         return NULL;
5495 }
5496
5497 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5498 {
5499         struct list_head *tmp;
5500         mddev_t *next_mddev, *mddev = v;
5501         
5502         ++*pos;
5503         if (v == (void*)2)
5504                 return NULL;
5505
5506         spin_lock(&all_mddevs_lock);
5507         if (v == (void*)1)
5508                 tmp = all_mddevs.next;
5509         else
5510                 tmp = mddev->all_mddevs.next;
5511         if (tmp != &all_mddevs)
5512                 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5513         else {
5514                 next_mddev = (void*)2;
5515                 *pos = 0x10000;
5516         }               
5517         spin_unlock(&all_mddevs_lock);
5518
5519         if (v != (void*)1)
5520                 mddev_put(mddev);
5521         return next_mddev;
5522
5523 }
5524
5525 static void md_seq_stop(struct seq_file *seq, void *v)
5526 {
5527         mddev_t *mddev = v;
5528
5529         if (mddev && v != (void*)1 && v != (void*)2)
5530                 mddev_put(mddev);
5531 }
5532
5533 struct mdstat_info {
5534         int event;
5535 };
5536
5537 static int md_seq_show(struct seq_file *seq, void *v)
5538 {
5539         mddev_t *mddev = v;
5540         sector_t size;
5541         mdk_rdev_t *rdev;
5542         struct mdstat_info *mi = seq->private;
5543         struct bitmap *bitmap;
5544
5545         if (v == (void*)1) {
5546                 struct mdk_personality *pers;
5547                 seq_printf(seq, "Personalities : ");
5548                 spin_lock(&pers_lock);
5549                 list_for_each_entry(pers, &pers_list, list)
5550                         seq_printf(seq, "[%s] ", pers->name);
5551
5552                 spin_unlock(&pers_lock);
5553                 seq_printf(seq, "\n");
5554                 mi->event = atomic_read(&md_event_count);
5555                 return 0;
5556         }
5557         if (v == (void*)2) {
5558                 status_unused(seq);
5559                 return 0;
5560         }
5561
5562         if (mddev_lock(mddev) < 0)
5563                 return -EINTR;
5564
5565         if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5566                 seq_printf(seq, "%s : %sactive", mdname(mddev),
5567                                                 mddev->pers ? "" : "in");
5568                 if (mddev->pers) {
5569                         if (mddev->ro==1)
5570                                 seq_printf(seq, " (read-only)");
5571                         if (mddev->ro==2)
5572                                 seq_printf(seq, " (auto-read-only)");
5573                         seq_printf(seq, " %s", mddev->pers->name);
5574                 }
5575
5576                 size = 0;
5577                 list_for_each_entry(rdev, &mddev->disks, same_set) {
5578                         char b[BDEVNAME_SIZE];
5579                         seq_printf(seq, " %s[%d]",
5580                                 bdevname(rdev->bdev,b), rdev->desc_nr);
5581                         if (test_bit(WriteMostly, &rdev->flags))
5582                                 seq_printf(seq, "(W)");
5583                         if (test_bit(Faulty, &rdev->flags)) {
5584                                 seq_printf(seq, "(F)");
5585                                 continue;
5586                         } else if (rdev->raid_disk < 0)
5587                                 seq_printf(seq, "(S)"); /* spare */
5588                         size += rdev->size;
5589                 }
5590
5591                 if (!list_empty(&mddev->disks)) {
5592                         if (mddev->pers)
5593                                 seq_printf(seq, "\n      %llu blocks",
5594                                            (unsigned long long)
5595                                            mddev->array_sectors / 2);
5596                         else
5597                                 seq_printf(seq, "\n      %llu blocks",
5598                                            (unsigned long long)size);
5599                 }
5600                 if (mddev->persistent) {
5601                         if (mddev->major_version != 0 ||
5602                             mddev->minor_version != 90) {
5603                                 seq_printf(seq," super %d.%d",
5604                                            mddev->major_version,
5605                                            mddev->minor_version);
5606                         }
5607                 } else if (mddev->external)
5608                         seq_printf(seq, " super external:%s",
5609                                    mddev->metadata_type);
5610                 else
5611                         seq_printf(seq, " super non-persistent");
5612
5613                 if (mddev->pers) {
5614                         mddev->pers->status(seq, mddev);
5615                         seq_printf(seq, "\n      ");
5616                         if (mddev->pers->sync_request) {
5617                                 if (mddev->curr_resync > 2) {
5618                                         status_resync(seq, mddev);
5619                                         seq_printf(seq, "\n      ");
5620                                 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5621                                         seq_printf(seq, "\tresync=DELAYED\n      ");
5622                                 else if (mddev->recovery_cp < MaxSector)
5623                                         seq_printf(seq, "\tresync=PENDING\n      ");
5624                         }
5625                 } else
5626                         seq_printf(seq, "\n       ");
5627
5628                 if ((bitmap = mddev->bitmap)) {
5629                         unsigned long chunk_kb;
5630                         unsigned long flags;
5631                         spin_lock_irqsave(&bitmap->lock, flags);
5632                         chunk_kb = bitmap->chunksize >> 10;
5633                         seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5634                                 "%lu%s chunk",
5635                                 bitmap->pages - bitmap->missing_pages,
5636                                 bitmap->pages,
5637                                 (bitmap->pages - bitmap->missing_pages)
5638                                         << (PAGE_SHIFT - 10),
5639                                 chunk_kb ? chunk_kb : bitmap->chunksize,
5640                                 chunk_kb ? "KB" : "B");
5641                         if (bitmap->file) {
5642                                 seq_printf(seq, ", file: ");
5643                                 seq_path(seq, &bitmap->file->f_path, " \t\n");
5644                         }
5645
5646                         seq_printf(seq, "\n");
5647                         spin_unlock_irqrestore(&bitmap->lock, flags);
5648                 }
5649
5650                 seq_printf(seq, "\n");
5651         }
5652         mddev_unlock(mddev);
5653         
5654         return 0;
5655 }
5656
5657 static struct seq_operations md_seq_ops = {
5658         .start  = md_seq_start,
5659         .next   = md_seq_next,
5660         .stop   = md_seq_stop,
5661         .show   = md_seq_show,
5662 };
5663
5664 static int md_seq_open(struct inode *inode, struct file *file)
5665 {
5666         int error;
5667         struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5668         if (mi == NULL)
5669                 return -ENOMEM;
5670
5671         error = seq_open(file, &md_seq_ops);
5672         if (error)
5673                 kfree(mi);
5674         else {
5675                 struct seq_file *p = file->private_data;
5676                 p->private = mi;
5677                 mi->event = atomic_read(&md_event_count);
5678         }
5679         return error;
5680 }
5681
5682 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5683 {
5684         struct seq_file *m = filp->private_data;
5685         struct mdstat_info *mi = m->private;
5686         int mask;
5687
5688         poll_wait(filp, &md_event_waiters, wait);
5689
5690         /* always allow read */
5691         mask = POLLIN | POLLRDNORM;
5692
5693         if (mi->event != atomic_read(&md_event_count))
5694                 mask |= POLLERR | POLLPRI;
5695         return mask;
5696 }
5697
5698 static const struct file_operations md_seq_fops = {
5699         .owner          = THIS_MODULE,
5700         .open           = md_seq_open,
5701         .read           = seq_read,
5702         .llseek         = seq_lseek,
5703         .release        = seq_release_private,
5704         .poll           = mdstat_poll,
5705 };
5706
5707 int register_md_personality(struct mdk_personality *p)
5708 {
5709         spin_lock(&pers_lock);
5710         list_add_tail(&p->list, &pers_list);
5711         printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
5712         spin_unlock(&pers_lock);
5713         return 0;
5714 }
5715
5716 int unregister_md_personality(struct mdk_personality *p)
5717 {
5718         printk(KERN_INFO "md: %s personality unregistered\n", p->name);
5719         spin_lock(&pers_lock);
5720         list_del_init(&p->list);
5721         spin_unlock(&pers_lock);
5722         return 0;
5723 }
5724
5725 static int is_mddev_idle(mddev_t *mddev)
5726 {
5727         mdk_rdev_t * rdev;
5728         int idle;
5729         long curr_events;
5730
5731         idle = 1;
5732         rcu_read_lock();
5733         rdev_for_each_rcu(rdev, mddev) {
5734                 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
5735                 curr_events = part_stat_read(&disk->part0, sectors[0]) +
5736                                 part_stat_read(&disk->part0, sectors[1]) -
5737                                 atomic_read(&disk->sync_io);
5738                 /* sync IO will cause sync_io to increase before the disk_stats
5739                  * as sync_io is counted when a request starts, and
5740                  * disk_stats is counted when it completes.
5741                  * So resync activity will cause curr_events to be smaller than
5742                  * when there was no such activity.
5743                  * non-sync IO will cause disk_stat to increase without
5744                  * increasing sync_io so curr_events will (eventually)
5745                  * be larger than it was before.  Once it becomes
5746                  * substantially larger, the test below will cause
5747                  * the array to appear non-idle, and resync will slow
5748                  * down.
5749                  * If there is a lot of outstanding resync activity when
5750                  * we set last_event to curr_events, then all that activity
5751                  * completing might cause the array to appear non-idle
5752                  * and resync will be slowed down even though there might
5753                  * not have been non-resync activity.  This will only
5754                  * happen once though.  'last_events' will soon reflect
5755                  * the state where there is little or no outstanding
5756                  * resync requests, and further resync activity will
5757                  * always make curr_events less than last_events.
5758                  *
5759                  */
5760                 if (curr_events - rdev->last_events > 4096) {
5761                         rdev->last_events = curr_events;
5762                         idle = 0;
5763                 }
5764         }
5765         rcu_read_unlock();
5766         return idle;
5767 }
5768
5769 void md_done_sync(mddev_t *mddev, int blocks, int ok)
5770 {
5771         /* another "blocks" (512byte) blocks have been synced */
5772         atomic_sub(blocks, &mddev->recovery_active);
5773         wake_up(&mddev->recovery_wait);
5774         if (!ok) {
5775                 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5776                 md_wakeup_thread(mddev->thread);
5777                 // stop recovery, signal do_sync ....
5778         }
5779 }
5780
5781
5782 /* md_write_start(mddev, bi)
5783  * If we need to update some array metadata (e.g. 'active' flag
5784  * in superblock) before writing, schedule a superblock update
5785  * and wait for it to complete.
5786  */
5787 void md_write_start(mddev_t *mddev, struct bio *bi)
5788 {
5789         int did_change = 0;
5790         if (bio_data_dir(bi) != WRITE)
5791                 return;
5792
5793         BUG_ON(mddev->ro == 1);
5794         if (mddev->ro == 2) {
5795                 /* need to switch to read/write */
5796                 mddev->ro = 0;
5797                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5798                 md_wakeup_thread(mddev->thread);
5799                 md_wakeup_thread(mddev->sync_thread);
5800                 did_change = 1;
5801         }
5802         atomic_inc(&mddev->writes_pending);
5803         if (mddev->safemode == 1)
5804                 mddev->safemode = 0;
5805         if (mddev->in_sync) {
5806                 spin_lock_irq(&mddev->write_lock);
5807                 if (mddev->in_sync) {
5808                         mddev->in_sync = 0;
5809                         set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5810                         md_wakeup_thread(mddev->thread);
5811                         did_change = 1;
5812                 }
5813                 spin_unlock_irq(&mddev->write_lock);
5814         }
5815         if (did_change)
5816                 sysfs_notify_dirent(mddev->sysfs_state);
5817         wait_event(mddev->sb_wait,
5818                    !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5819                    !test_bit(MD_CHANGE_PENDING, &mddev->flags));
5820 }
5821
5822 void md_write_end(mddev_t *mddev)
5823 {
5824         if (atomic_dec_and_test(&mddev->writes_pending)) {
5825                 if (mddev->safemode == 2)
5826                         md_wakeup_thread(mddev->thread);
5827                 else if (mddev->safemode_delay)
5828                         mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5829         }
5830 }
5831
5832 /* md_allow_write(mddev)
5833  * Calling this ensures that the array is marked 'active' so that writes
5834  * may proceed without blocking.  It is important to call this before
5835  * attempting a GFP_KERNEL allocation while holding the mddev lock.
5836  * Must be called with mddev_lock held.
5837  *
5838  * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5839  * is dropped, so return -EAGAIN after notifying userspace.
5840  */
5841 int md_allow_write(mddev_t *mddev)
5842 {
5843         if (!mddev->pers)
5844                 return 0;
5845         if (mddev->ro)
5846                 return 0;
5847         if (!mddev->pers->sync_request)
5848                 return 0;
5849
5850         spin_lock_irq(&mddev->write_lock);
5851         if (mddev->in_sync) {
5852                 mddev->in_sync = 0;
5853                 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5854                 if (mddev->safemode_delay &&
5855                     mddev->safemode == 0)
5856                         mddev->safemode = 1;
5857                 spin_unlock_irq(&mddev->write_lock);
5858                 md_update_sb(mddev, 0);
5859                 sysfs_notify_dirent(mddev->sysfs_state);
5860         } else
5861                 spin_unlock_irq(&mddev->write_lock);
5862
5863         if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5864                 return -EAGAIN;
5865         else
5866                 return 0;
5867 }
5868 EXPORT_SYMBOL_GPL(md_allow_write);
5869
5870 #define SYNC_MARKS      10
5871 #define SYNC_MARK_STEP  (3*HZ)
5872 void md_do_sync(mddev_t *mddev)
5873 {
5874         mddev_t *mddev2;
5875         unsigned int currspeed = 0,
5876                  window;
5877         sector_t max_sectors,j, io_sectors;
5878         unsigned long mark[SYNC_MARKS];
5879         sector_t mark_cnt[SYNC_MARKS];
5880         int last_mark,m;
5881         struct list_head *tmp;
5882         sector_t last_check;
5883         int skipped = 0;
5884         mdk_rdev_t *rdev;
5885         char *desc;
5886
5887         /* just incase thread restarts... */
5888         if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5889                 return;
5890         if (mddev->ro) /* never try to sync a read-only array */
5891                 return;
5892
5893         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5894                 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5895                         desc = "data-check";
5896                 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5897                         desc = "requested-resync";
5898                 else
5899                         desc = "resync";
5900         } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5901                 desc = "reshape";
5902         else
5903                 desc = "recovery";
5904
5905         /* we overload curr_resync somewhat here.
5906          * 0 == not engaged in resync at all
5907          * 2 == checking that there is no conflict with another sync
5908          * 1 == like 2, but have yielded to allow conflicting resync to
5909          *              commense
5910          * other == active in resync - this many blocks
5911          *
5912          * Before starting a resync we must have set curr_resync to
5913          * 2, and then checked that every "conflicting" array has curr_resync
5914          * less than ours.  When we find one that is the same or higher
5915          * we wait on resync_wait.  To avoid deadlock, we reduce curr_resync
5916          * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5917          * This will mean we have to start checking from the beginning again.
5918          *
5919          */
5920
5921         do {
5922                 mddev->curr_resync = 2;
5923
5924         try_again:
5925                 if (kthread_should_stop()) {
5926                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5927                         goto skip;
5928                 }
5929                 for_each_mddev(mddev2, tmp) {
5930                         if (mddev2 == mddev)
5931                                 continue;
5932                         if (!mddev->parallel_resync
5933                         &&  mddev2->curr_resync
5934                         &&  match_mddev_units(mddev, mddev2)) {
5935                                 DEFINE_WAIT(wq);
5936                                 if (mddev < mddev2 && mddev->curr_resync == 2) {
5937                                         /* arbitrarily yield */
5938                                         mddev->curr_resync = 1;
5939                                         wake_up(&resync_wait);
5940                                 }
5941                                 if (mddev > mddev2 && mddev->curr_resync == 1)
5942                                         /* no need to wait here, we can wait the next
5943                                          * time 'round when curr_resync == 2
5944                                          */
5945                                         continue;
5946                                 /* We need to wait 'interruptible' so as not to
5947                                  * contribute to the load average, and not to
5948                                  * be caught by 'softlockup'
5949                                  */
5950                                 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
5951                                 if (!kthread_should_stop() &&
5952                                     mddev2->curr_resync >= mddev->curr_resync) {
5953                                         printk(KERN_INFO "md: delaying %s of %s"
5954                                                " until %s has finished (they"
5955                                                " share one or more physical units)\n",
5956                                                desc, mdname(mddev), mdname(mddev2));
5957                                         mddev_put(mddev2);
5958                                         if (signal_pending(current))
5959                                                 flush_signals(current);
5960                                         schedule();
5961                                         finish_wait(&resync_wait, &wq);
5962                                         goto try_again;
5963                                 }
5964                                 finish_wait(&resync_wait, &wq);
5965                         }
5966                 }
5967         } while (mddev->curr_resync < 2);
5968
5969         j = 0;
5970         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5971                 /* resync follows the size requested by the personality,
5972                  * which defaults to physical size, but can be virtual size
5973                  */
5974                 max_sectors = mddev->resync_max_sectors;
5975                 mddev->resync_mismatches = 0;
5976                 /* we don't use the checkpoint if there's a bitmap */
5977                 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5978                         j = mddev->resync_min;
5979                 else if (!mddev->bitmap)
5980                         j = mddev->recovery_cp;
5981
5982         } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5983                 max_sectors = mddev->size << 1;
5984         else {
5985                 /* recovery follows the physical size of devices */
5986                 max_sectors = mddev->size << 1;
5987                 j = MaxSector;
5988                 list_for_each_entry(rdev, &mddev->disks, same_set)
5989                         if (rdev->raid_disk >= 0 &&
5990                             !test_bit(Faulty, &rdev->flags) &&
5991                             !test_bit(In_sync, &rdev->flags) &&
5992                             rdev->recovery_offset < j)
5993                                 j = rdev->recovery_offset;
5994         }
5995
5996         printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5997         printk(KERN_INFO "md: minimum _guaranteed_  speed:"
5998                 " %d KB/sec/disk.\n", speed_min(mddev));
5999         printk(KERN_INFO "md: using maximum available idle IO bandwidth "
6000                "(but not more than %d KB/sec) for %s.\n",
6001                speed_max(mddev), desc);
6002
6003         is_mddev_idle(mddev); /* this also initializes IO event counters */
6004
6005         io_sectors = 0;
6006         for (m = 0; m < SYNC_MARKS; m++) {
6007                 mark[m] = jiffies;
6008                 mark_cnt[m] = io_sectors;
6009         }
6010         last_mark = 0;
6011         mddev->resync_mark = mark[last_mark];
6012         mddev->resync_mark_cnt = mark_cnt[last_mark];
6013
6014         /*
6015          * Tune reconstruction:
6016          */
6017         window = 32*(PAGE_SIZE/512);
6018         printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6019                 window/2,(unsigned long long) max_sectors/2);
6020
6021         atomic_set(&mddev->recovery_active, 0);
6022         last_check = 0;
6023
6024         if (j>2) {
6025                 printk(KERN_INFO 
6026                        "md: resuming %s of %s from checkpoint.\n",
6027                        desc, mdname(mddev));
6028                 mddev->curr_resync = j;
6029         }
6030
6031         while (j < max_sectors) {
6032                 sector_t sectors;
6033
6034                 skipped = 0;
6035                 if (j >= mddev->resync_max) {
6036                         sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6037                         wait_event(mddev->recovery_wait,
6038                                    mddev->resync_max > j
6039                                    || kthread_should_stop());
6040                 }
6041                 if (kthread_should_stop())
6042                         goto interrupted;
6043                 sectors = mddev->pers->sync_request(mddev, j, &skipped,
6044                                                   currspeed < speed_min(mddev));
6045                 if (sectors == 0) {
6046                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6047                         goto out;
6048                 }
6049
6050                 if (!skipped) { /* actual IO requested */
6051                         io_sectors += sectors;
6052                         atomic_add(sectors, &mddev->recovery_active);
6053                 }
6054
6055                 j += sectors;
6056                 if (j>1) mddev->curr_resync = j;
6057                 mddev->curr_mark_cnt = io_sectors;
6058                 if (last_check == 0)
6059                         /* this is the earliers that rebuilt will be
6060                          * visible in /proc/mdstat
6061                          */
6062                         md_new_event(mddev);
6063
6064                 if (last_check + window > io_sectors || j == max_sectors)
6065                         continue;
6066
6067                 last_check = io_sectors;
6068
6069                 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6070                         break;
6071
6072         repeat:
6073                 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6074                         /* step marks */
6075                         int next = (last_mark+1) % SYNC_MARKS;
6076
6077                         mddev->resync_mark = mark[next];
6078                         mddev->resync_mark_cnt = mark_cnt[next];
6079                         mark[next] = jiffies;
6080                         mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
6081                         last_mark = next;
6082                 }
6083
6084
6085                 if (kthread_should_stop())
6086                         goto interrupted;
6087
6088
6089                 /*
6090                  * this loop exits only if either when we are slower than
6091                  * the 'hard' speed limit, or the system was IO-idle for
6092                  * a jiffy.
6093                  * the system might be non-idle CPU-wise, but we only care
6094                  * about not overloading the IO subsystem. (things like an
6095                  * e2fsck being done on the RAID array should execute fast)
6096                  */
6097                 blk_unplug(mddev->queue);
6098                 cond_resched();
6099
6100                 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6101                         /((jiffies-mddev->resync_mark)/HZ +1) +1;
6102
6103                 if (currspeed > speed_min(mddev)) {
6104                         if ((currspeed > speed_max(mddev)) ||
6105                                         !is_mddev_idle(mddev)) {
6106                                 msleep(500);
6107                                 goto repeat;
6108                         }
6109                 }
6110         }
6111         printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
6112         /*
6113          * this also signals 'finished resyncing' to md_stop
6114          */
6115  out:
6116         blk_unplug(mddev->queue);
6117
6118         wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6119
6120         /* tell personality that we are finished */
6121         mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
6122
6123         if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
6124             mddev->curr_resync > 2) {
6125                 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6126                         if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6127                                 if (mddev->curr_resync >= mddev->recovery_cp) {
6128                                         printk(KERN_INFO
6129                                                "md: checkpointing %s of %s.\n",
6130                                                desc, mdname(mddev));
6131                                         mddev->recovery_cp = mddev->curr_resync;
6132                                 }
6133                         } else
6134                                 mddev->recovery_cp = MaxSector;
6135                 } else {
6136                         if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6137                                 mddev->curr_resync = MaxSector;
6138                         list_for_each_entry(rdev, &mddev->disks, same_set)
6139                                 if (rdev->raid_disk >= 0 &&
6140                                     !test_bit(Faulty, &rdev->flags) &&
6141                                     !test_bit(In_sync, &rdev->flags) &&
6142                                     rdev->recovery_offset < mddev->curr_resync)
6143                                         rdev->recovery_offset = mddev->curr_resync;
6144                 }
6145         }
6146         set_bit(MD_CHANGE_DEVS, &mddev->flags);
6147
6148  skip:
6149         mddev->curr_resync = 0;
6150         mddev->resync_min = 0;
6151         mddev->resync_max = MaxSector;
6152         sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6153         wake_up(&resync_wait);
6154         set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6155         md_wakeup_thread(mddev->thread);
6156         return;
6157
6158  interrupted:
6159         /*
6160          * got a signal, exit.
6161          */
6162         printk(KERN_INFO
6163                "md: md_do_sync() got signal ... exiting\n");
6164         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6165         goto out;
6166
6167 }
6168 EXPORT_SYMBOL_GPL(md_do_sync);
6169
6170
6171 static int remove_and_add_spares(mddev_t *mddev)
6172 {
6173         mdk_rdev_t *rdev;
6174         int spares = 0;
6175
6176         list_for_each_entry(rdev, &mddev->disks, same_set)
6177                 if (rdev->raid_disk >= 0 &&
6178                     !test_bit(Blocked, &rdev->flags) &&
6179                     (test_bit(Faulty, &rdev->flags) ||
6180                      ! test_bit(In_sync, &rdev->flags)) &&
6181                     atomic_read(&rdev->nr_pending)==0) {
6182                         if (mddev->pers->hot_remove_disk(
6183                                     mddev, rdev->raid_disk)==0) {
6184                                 char nm[20];
6185                                 sprintf(nm,"rd%d", rdev->raid_disk);
6186                                 sysfs_remove_link(&mddev->kobj, nm);
6187                                 rdev->raid_disk = -1;
6188                         }
6189                 }
6190
6191         if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
6192                 list_for_each_entry(rdev, &mddev->disks, same_set) {
6193                         if (rdev->raid_disk >= 0 &&
6194                             !test_bit(In_sync, &rdev->flags) &&
6195                             !test_bit(Blocked, &rdev->flags))
6196                                 spares++;
6197                         if (rdev->raid_disk < 0
6198                             && !test_bit(Faulty, &rdev->flags)) {
6199                                 rdev->recovery_offset = 0;
6200                                 if (mddev->pers->
6201                                     hot_add_disk(mddev, rdev) == 0) {
6202                                         char nm[20];
6203                                         sprintf(nm, "rd%d", rdev->raid_disk);
6204                                         if (sysfs_create_link(&mddev->kobj,
6205                                                               &rdev->kobj, nm))
6206                                                 printk(KERN_WARNING
6207                                                        "md: cannot register "
6208                                                        "%s for %s\n",
6209                                                        nm, mdname(mddev));
6210                                         spares++;
6211                                         md_new_event(mddev);
6212                                 } else
6213                                         break;
6214                         }
6215                 }
6216         }
6217         return spares;
6218 }
6219 /*
6220  * This routine is regularly called by all per-raid-array threads to
6221  * deal with generic issues like resync and super-block update.
6222  * Raid personalities that don't have a thread (linear/raid0) do not
6223  * need this as they never do any recovery or update the superblock.
6224  *
6225  * It does not do any resync itself, but rather "forks" off other threads
6226  * to do that as needed.
6227  * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6228  * "->recovery" and create a thread at ->sync_thread.
6229  * When the thread finishes it sets MD_RECOVERY_DONE
6230  * and wakeups up this thread which will reap the thread and finish up.
6231  * This thread also removes any faulty devices (with nr_pending == 0).
6232  *
6233  * The overall approach is:
6234  *  1/ if the superblock needs updating, update it.
6235  *  2/ If a recovery thread is running, don't do anything else.
6236  *  3/ If recovery has finished, clean up, possibly marking spares active.
6237  *  4/ If there are any faulty devices, remove them.
6238  *  5/ If array is degraded, try to add spares devices
6239  *  6/ If array has spares or is not in-sync, start a resync thread.
6240  */
6241 void md_check_recovery(mddev_t *mddev)
6242 {
6243         mdk_rdev_t *rdev;
6244
6245
6246         if (mddev->bitmap)
6247                 bitmap_daemon_work(mddev->bitmap);
6248
6249         if (mddev->ro)
6250                 return;
6251
6252         if (signal_pending(current)) {
6253                 if (mddev->pers->sync_request && !mddev->external) {
6254                         printk(KERN_INFO "md: %s in immediate safe mode\n",
6255                                mdname(mddev));
6256                         mddev->safemode = 2;
6257                 }
6258                 flush_signals(current);
6259         }
6260
6261         if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6262                 return;
6263         if ( ! (
6264                 (mddev->flags && !mddev->external) ||
6265                 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
6266                 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
6267                 (mddev->external == 0 && mddev->safemode == 1) ||
6268                 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6269                  && !mddev->in_sync && mddev->recovery_cp == MaxSector)
6270                 ))
6271                 return;
6272
6273         if (mddev_trylock(mddev)) {
6274                 int spares = 0;
6275
6276                 if (mddev->ro) {
6277                         /* Only thing we do on a ro array is remove
6278                          * failed devices.
6279                          */
6280                         remove_and_add_spares(mddev);
6281                         clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6282                         goto unlock;
6283                 }
6284
6285                 if (!mddev->external) {
6286                         int did_change = 0;
6287                         spin_lock_irq(&mddev->write_lock);
6288                         if (mddev->safemode &&
6289                             !atomic_read(&mddev->writes_pending) &&
6290                             !mddev->in_sync &&
6291                             mddev->recovery_cp == MaxSector) {
6292                                 mddev->in_sync = 1;
6293                                 did_change = 1;
6294                                 if (mddev->persistent)
6295                                         set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6296                         }
6297                         if (mddev->safemode == 1)
6298                                 mddev->safemode = 0;
6299                         spin_unlock_irq(&mddev->write_lock);
6300                         if (did_change)
6301                                 sysfs_notify_dirent(mddev->sysfs_state);
6302                 }
6303
6304                 if (mddev->flags)
6305                         md_update_sb(mddev, 0);
6306
6307                 list_for_each_entry(rdev, &mddev->disks, same_set)
6308                         if (test_and_clear_bit(StateChanged, &rdev->flags))
6309                                 sysfs_notify_dirent(rdev->sysfs_state);
6310
6311
6312                 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6313                     !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6314                         /* resync/recovery still happening */
6315                         clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6316                         goto unlock;
6317                 }
6318                 if (mddev->sync_thread) {
6319                         /* resync has finished, collect result */
6320                         md_unregister_thread(mddev->sync_thread);
6321                         mddev->sync_thread = NULL;
6322                         if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6323                             !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
6324                                 /* success...*/
6325                                 /* activate any spares */
6326                                 if (mddev->pers->spare_active(mddev))
6327                                         sysfs_notify(&mddev->kobj, NULL,
6328                                                      "degraded");
6329                         }
6330                         md_update_sb(mddev, 1);
6331
6332                         /* if array is no-longer degraded, then any saved_raid_disk
6333                          * information must be scrapped
6334                          */
6335                         if (!mddev->degraded)
6336                                 list_for_each_entry(rdev, &mddev->disks, same_set)
6337                                         rdev->saved_raid_disk = -1;
6338
6339                         mddev->recovery = 0;
6340                         /* flag recovery needed just to double check */
6341                         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6342                         sysfs_notify_dirent(mddev->sysfs_action);
6343                         md_new_event(mddev);
6344                         goto unlock;
6345                 }
6346                 /* Set RUNNING before clearing NEEDED to avoid
6347                  * any transients in the value of "sync_action".
6348                  */
6349                 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6350                 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6351                 /* Clear some bits that don't mean anything, but
6352                  * might be left set
6353                  */
6354                 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6355                 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
6356
6357                 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6358                         goto unlock;
6359                 /* no recovery is running.
6360                  * remove any failed drives, then
6361                  * add spares if possible.
6362                  * Spare are also removed and re-added, to allow
6363                  * the personality to fail the re-add.
6364                  */
6365
6366                 if (mddev->reshape_position != MaxSector) {
6367                         if (mddev->pers->check_reshape(mddev) != 0)
6368                                 /* Cannot proceed */
6369                                 goto unlock;
6370                         set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6371                         clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6372                 } else if ((spares = remove_and_add_spares(mddev))) {
6373                         clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6374                         clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6375                         clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
6376                         set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6377                 } else if (mddev->recovery_cp < MaxSector) {
6378                         set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6379                         clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6380                 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6381                         /* nothing to be done ... */
6382                         goto unlock;
6383
6384                 if (mddev->pers->sync_request) {
6385                         if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6386                                 /* We are adding a device or devices to an array
6387                                  * which has the bitmap stored on all devices.
6388                                  * So make sure all bitmap pages get written
6389                                  */
6390                                 bitmap_write_all(mddev->bitmap);
6391                         }
6392                         mddev->sync_thread = md_register_thread(md_do_sync,
6393                                                                 mddev,
6394                                                                 "%s_resync");
6395                         if (!mddev->sync_thread) {
6396                                 printk(KERN_ERR "%s: could not start resync"
6397                                         " thread...\n", 
6398                                         mdname(mddev));
6399                                 /* leave the spares where they are, it shouldn't hurt */
6400                                 mddev->recovery = 0;
6401                         } else
6402                                 md_wakeup_thread(mddev->sync_thread);
6403                         sysfs_notify_dirent(mddev->sysfs_action);
6404                         md_new_event(mddev);
6405                 }
6406         unlock:
6407                 if (!mddev->sync_thread) {
6408                         clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6409                         if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6410                                                &mddev->recovery))
6411                                 if (mddev->sysfs_action)
6412                                         sysfs_notify_dirent(mddev->sysfs_action);
6413                 }
6414                 mddev_unlock(mddev);
6415         }
6416 }
6417
6418 void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6419 {
6420         sysfs_notify_dirent(rdev->sysfs_state);
6421         wait_event_timeout(rdev->blocked_wait,
6422                            !test_bit(Blocked, &rdev->flags),
6423                            msecs_to_jiffies(5000));
6424         rdev_dec_pending(rdev, mddev);
6425 }
6426 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6427
6428 static int md_notify_reboot(struct notifier_block *this,
6429                             unsigned long code, void *x)
6430 {
6431         struct list_head *tmp;
6432         mddev_t *mddev;
6433
6434         if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6435
6436                 printk(KERN_INFO "md: stopping all md devices.\n");
6437
6438                 for_each_mddev(mddev, tmp)
6439                         if (mddev_trylock(mddev)) {
6440                                 /* Force a switch to readonly even array
6441                                  * appears to still be in use.  Hence
6442                                  * the '100'.
6443                                  */
6444                                 do_md_stop(mddev, 1, 100);
6445                                 mddev_unlock(mddev);
6446                         }
6447                 /*
6448                  * certain more exotic SCSI devices are known to be
6449                  * volatile wrt too early system reboots. While the
6450                  * right place to handle this issue is the given
6451                  * driver, we do want to have a safe RAID driver ...
6452                  */
6453                 mdelay(1000*1);
6454         }
6455         return NOTIFY_DONE;
6456 }
6457
6458 static struct notifier_block md_notifier = {
6459         .notifier_call  = md_notify_reboot,
6460         .next           = NULL,
6461         .priority       = INT_MAX, /* before any real devices */
6462 };
6463
6464 static void md_geninit(void)
6465 {
6466         dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6467
6468         proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
6469 }
6470
6471 static int __init md_init(void)
6472 {
6473         if (register_blkdev(MAJOR_NR, "md"))
6474                 return -1;
6475         if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6476                 unregister_blkdev(MAJOR_NR, "md");
6477                 return -1;
6478         }
6479         blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6480                             md_probe, NULL, NULL);
6481         blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
6482                             md_probe, NULL, NULL);
6483
6484         register_reboot_notifier(&md_notifier);
6485         raid_table_header = register_sysctl_table(raid_root_table);
6486
6487         md_geninit();
6488         return 0;
6489 }
6490
6491
6492 #ifndef MODULE
6493
6494 /*
6495  * Searches all registered partitions for autorun RAID arrays
6496  * at boot time.
6497  */
6498
6499 static LIST_HEAD(all_detected_devices);
6500 struct detected_devices_node {
6501         struct list_head list;
6502         dev_t dev;
6503 };
6504
6505 void md_autodetect_dev(dev_t dev)
6506 {
6507         struct detected_devices_node *node_detected_dev;
6508
6509         node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6510         if (node_detected_dev) {
6511                 node_detected_dev->dev = dev;
6512                 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6513         } else {
6514                 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6515                         ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6516         }
6517 }
6518
6519
6520 static void autostart_arrays(int part)
6521 {
6522         mdk_rdev_t *rdev;
6523         struct detected_devices_node *node_detected_dev;
6524         dev_t dev;
6525         int i_scanned, i_passed;
6526
6527         i_scanned = 0;
6528         i_passed = 0;
6529
6530         printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6531
6532         while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6533                 i_scanned++;
6534                 node_detected_dev = list_entry(all_detected_devices.next,
6535                                         struct detected_devices_node, list);
6536                 list_del(&node_detected_dev->list);
6537                 dev = node_detected_dev->dev;
6538                 kfree(node_detected_dev);
6539                 rdev = md_import_device(dev,0, 90);
6540                 if (IS_ERR(rdev))
6541                         continue;
6542
6543                 if (test_bit(Faulty, &rdev->flags)) {
6544                         MD_BUG();
6545                         continue;
6546                 }
6547                 set_bit(AutoDetected, &rdev->flags);
6548                 list_add(&rdev->same_set, &pending_raid_disks);
6549                 i_passed++;
6550         }
6551
6552         printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6553                                                 i_scanned, i_passed);
6554
6555         autorun_devices(part);
6556 }
6557
6558 #endif /* !MODULE */
6559
6560 static __exit void md_exit(void)
6561 {
6562         mddev_t *mddev;
6563         struct list_head *tmp;
6564
6565         blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6566         blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
6567
6568         unregister_blkdev(MAJOR_NR,"md");
6569         unregister_blkdev(mdp_major, "mdp");
6570         unregister_reboot_notifier(&md_notifier);
6571         unregister_sysctl_table(raid_table_header);
6572         remove_proc_entry("mdstat", NULL);
6573         for_each_mddev(mddev, tmp) {
6574                 export_array(mddev);
6575                 mddev->hold_active = 0;
6576         }
6577 }
6578
6579 subsys_initcall(md_init);
6580 module_exit(md_exit)
6581
6582 static int get_ro(char *buffer, struct kernel_param *kp)
6583 {
6584         return sprintf(buffer, "%d", start_readonly);
6585 }
6586 static int set_ro(const char *val, struct kernel_param *kp)
6587 {
6588         char *e;
6589         int num = simple_strtoul(val, &e, 10);
6590         if (*val && (*e == '\0' || *e == '\n')) {
6591                 start_readonly = num;
6592                 return 0;
6593         }
6594         return -EINVAL;
6595 }
6596
6597 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6598 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
6599
6600 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
6601
6602 EXPORT_SYMBOL(register_md_personality);
6603 EXPORT_SYMBOL(unregister_md_personality);
6604 EXPORT_SYMBOL(md_error);
6605 EXPORT_SYMBOL(md_done_sync);
6606 EXPORT_SYMBOL(md_write_start);
6607 EXPORT_SYMBOL(md_write_end);
6608 EXPORT_SYMBOL(md_register_thread);
6609 EXPORT_SYMBOL(md_unregister_thread);
6610 EXPORT_SYMBOL(md_wakeup_thread);
6611 EXPORT_SYMBOL(md_check_recovery);
6612 MODULE_LICENSE("GPL");
6613 MODULE_ALIAS("md");
6614 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);