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