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