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