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