[PATCH] md: convert recently exported symbol to GPL
[linux-3.10.git] / drivers / md / md.c
1 /*
2    md.c : Multiple Devices driver for Linux
3           Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5      completely rewritten, based on the MD driver code from Marc Zyngier
6
7    Changes:
8
9    - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10    - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11    - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12    - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13    - kmod support by: Cyrus Durgin
14    - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15    - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17    - lots of fixes and improvements to the RAID1/RAID5 and generic
18      RAID code (such as request based resynchronization):
19
20      Neil Brown <neilb@cse.unsw.edu.au>.
21
22    - persistent bitmap code
23      Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
25    This program is free software; you can redistribute it and/or modify
26    it under the terms of the GNU General Public License as published by
27    the Free Software Foundation; either version 2, or (at your option)
28    any later version.
29
30    You should have received a copy of the GNU General Public License
31    (for example /usr/src/linux/COPYING); if not, write to the Free
32    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 */
34
35 #include <linux/module.h>
36 #include <linux/config.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/devfs_fs_kernel.h>
43 #include <linux/buffer_head.h> /* for invalidate_bdev */
44 #include <linux/suspend.h>
45 #include <linux/poll.h>
46
47 #include <linux/init.h>
48
49 #include <linux/file.h>
50
51 #ifdef CONFIG_KMOD
52 #include <linux/kmod.h>
53 #endif
54
55 #include <asm/unaligned.h>
56
57 #define MAJOR_NR MD_MAJOR
58 #define MD_DRIVER
59
60 /* 63 partitions with the alternate major number (mdp) */
61 #define MdpMinorShift 6
62
63 #define DEBUG 0
64 #define dprintk(x...) ((void)(DEBUG && printk(x)))
65
66
67 #ifndef MODULE
68 static void autostart_arrays (int part);
69 #endif
70
71 static mdk_personality_t *pers[MAX_PERSONALITY];
72 static DEFINE_SPINLOCK(pers_lock);
73
74 /*
75  * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
76  * is 1000 KB/sec, so the extra system load does not show up that much.
77  * Increase it if you want to have more _guaranteed_ speed. Note that
78  * the RAID driver will use the maximum available bandwidth if the IO
79  * subsystem is idle. There is also an 'absolute maximum' reconstruction
80  * speed limit - in case reconstruction slows down your system despite
81  * idle IO detection.
82  *
83  * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
84  */
85
86 static int sysctl_speed_limit_min = 1000;
87 static int sysctl_speed_limit_max = 200000;
88
89 static struct ctl_table_header *raid_table_header;
90
91 static ctl_table raid_table[] = {
92         {
93                 .ctl_name       = DEV_RAID_SPEED_LIMIT_MIN,
94                 .procname       = "speed_limit_min",
95                 .data           = &sysctl_speed_limit_min,
96                 .maxlen         = sizeof(int),
97                 .mode           = 0644,
98                 .proc_handler   = &proc_dointvec,
99         },
100         {
101                 .ctl_name       = DEV_RAID_SPEED_LIMIT_MAX,
102                 .procname       = "speed_limit_max",
103                 .data           = &sysctl_speed_limit_max,
104                 .maxlen         = sizeof(int),
105                 .mode           = 0644,
106                 .proc_handler   = &proc_dointvec,
107         },
108         { .ctl_name = 0 }
109 };
110
111 static ctl_table raid_dir_table[] = {
112         {
113                 .ctl_name       = DEV_RAID,
114                 .procname       = "raid",
115                 .maxlen         = 0,
116                 .mode           = 0555,
117                 .child          = raid_table,
118         },
119         { .ctl_name = 0 }
120 };
121
122 static ctl_table raid_root_table[] = {
123         {
124                 .ctl_name       = CTL_DEV,
125                 .procname       = "dev",
126                 .maxlen         = 0,
127                 .mode           = 0555,
128                 .child          = raid_dir_table,
129         },
130         { .ctl_name = 0 }
131 };
132
133 static struct block_device_operations md_fops;
134
135 static int start_readonly;
136
137 /*
138  * We have a system wide 'event count' that is incremented
139  * on any 'interesting' event, and readers of /proc/mdstat
140  * can use 'poll' or 'select' to find out when the event
141  * count increases.
142  *
143  * Events are:
144  *  start array, stop array, error, add device, remove device,
145  *  start build, activate spare
146  */
147 DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
148 static atomic_t md_event_count;
149 void md_new_event(mddev_t *mddev)
150 {
151         atomic_inc(&md_event_count);
152         wake_up(&md_event_waiters);
153 }
154
155 /*
156  * Enables to iterate over all existing md arrays
157  * all_mddevs_lock protects this list.
158  */
159 static LIST_HEAD(all_mddevs);
160 static DEFINE_SPINLOCK(all_mddevs_lock);
161
162
163 /*
164  * iterates through all used mddevs in the system.
165  * We take care to grab the all_mddevs_lock whenever navigating
166  * the list, and to always hold a refcount when unlocked.
167  * Any code which breaks out of this loop while own
168  * a reference to the current mddev and must mddev_put it.
169  */
170 #define ITERATE_MDDEV(mddev,tmp)                                        \
171                                                                         \
172         for (({ spin_lock(&all_mddevs_lock);                            \
173                 tmp = all_mddevs.next;                                  \
174                 mddev = NULL;});                                        \
175              ({ if (tmp != &all_mddevs)                                 \
176                         mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
177                 spin_unlock(&all_mddevs_lock);                          \
178                 if (mddev) mddev_put(mddev);                            \
179                 mddev = list_entry(tmp, mddev_t, all_mddevs);           \
180                 tmp != &all_mddevs;});                                  \
181              ({ spin_lock(&all_mddevs_lock);                            \
182                 tmp = tmp->next;})                                      \
183                 )
184
185
186 static int md_fail_request (request_queue_t *q, struct bio *bio)
187 {
188         bio_io_error(bio, bio->bi_size);
189         return 0;
190 }
191
192 static inline mddev_t *mddev_get(mddev_t *mddev)
193 {
194         atomic_inc(&mddev->active);
195         return mddev;
196 }
197
198 static void mddev_put(mddev_t *mddev)
199 {
200         if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
201                 return;
202         if (!mddev->raid_disks && list_empty(&mddev->disks)) {
203                 list_del(&mddev->all_mddevs);
204                 blk_put_queue(mddev->queue);
205                 kobject_unregister(&mddev->kobj);
206         }
207         spin_unlock(&all_mddevs_lock);
208 }
209
210 static mddev_t * mddev_find(dev_t unit)
211 {
212         mddev_t *mddev, *new = NULL;
213
214  retry:
215         spin_lock(&all_mddevs_lock);
216         list_for_each_entry(mddev, &all_mddevs, all_mddevs)
217                 if (mddev->unit == unit) {
218                         mddev_get(mddev);
219                         spin_unlock(&all_mddevs_lock);
220                         kfree(new);
221                         return mddev;
222                 }
223
224         if (new) {
225                 list_add(&new->all_mddevs, &all_mddevs);
226                 spin_unlock(&all_mddevs_lock);
227                 return new;
228         }
229         spin_unlock(&all_mddevs_lock);
230
231         new = kzalloc(sizeof(*new), GFP_KERNEL);
232         if (!new)
233                 return NULL;
234
235         new->unit = unit;
236         if (MAJOR(unit) == MD_MAJOR)
237                 new->md_minor = MINOR(unit);
238         else
239                 new->md_minor = MINOR(unit) >> MdpMinorShift;
240
241         init_MUTEX(&new->reconfig_sem);
242         INIT_LIST_HEAD(&new->disks);
243         INIT_LIST_HEAD(&new->all_mddevs);
244         init_timer(&new->safemode_timer);
245         atomic_set(&new->active, 1);
246         spin_lock_init(&new->write_lock);
247         init_waitqueue_head(&new->sb_wait);
248
249         new->queue = blk_alloc_queue(GFP_KERNEL);
250         if (!new->queue) {
251                 kfree(new);
252                 return NULL;
253         }
254
255         blk_queue_make_request(new->queue, md_fail_request);
256
257         goto retry;
258 }
259
260 static inline int mddev_lock(mddev_t * mddev)
261 {
262         return down_interruptible(&mddev->reconfig_sem);
263 }
264
265 static inline void mddev_lock_uninterruptible(mddev_t * mddev)
266 {
267         down(&mddev->reconfig_sem);
268 }
269
270 static inline int mddev_trylock(mddev_t * mddev)
271 {
272         return down_trylock(&mddev->reconfig_sem);
273 }
274
275 static inline void mddev_unlock(mddev_t * mddev)
276 {
277         up(&mddev->reconfig_sem);
278
279         md_wakeup_thread(mddev->thread);
280 }
281
282 mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
283 {
284         mdk_rdev_t * rdev;
285         struct list_head *tmp;
286
287         ITERATE_RDEV(mddev,rdev,tmp) {
288                 if (rdev->desc_nr == nr)
289                         return rdev;
290         }
291         return NULL;
292 }
293
294 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
295 {
296         struct list_head *tmp;
297         mdk_rdev_t *rdev;
298
299         ITERATE_RDEV(mddev,rdev,tmp) {
300                 if (rdev->bdev->bd_dev == dev)
301                         return rdev;
302         }
303         return NULL;
304 }
305
306 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
307 {
308         sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
309         return MD_NEW_SIZE_BLOCKS(size);
310 }
311
312 static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
313 {
314         sector_t size;
315
316         size = rdev->sb_offset;
317
318         if (chunk_size)
319                 size &= ~((sector_t)chunk_size/1024 - 1);
320         return size;
321 }
322
323 static int alloc_disk_sb(mdk_rdev_t * rdev)
324 {
325         if (rdev->sb_page)
326                 MD_BUG();
327
328         rdev->sb_page = alloc_page(GFP_KERNEL);
329         if (!rdev->sb_page) {
330                 printk(KERN_ALERT "md: out of memory.\n");
331                 return -EINVAL;
332         }
333
334         return 0;
335 }
336
337 static void free_disk_sb(mdk_rdev_t * rdev)
338 {
339         if (rdev->sb_page) {
340                 put_page(rdev->sb_page);
341                 rdev->sb_loaded = 0;
342                 rdev->sb_page = NULL;
343                 rdev->sb_offset = 0;
344                 rdev->size = 0;
345         }
346 }
347
348
349 static int super_written(struct bio *bio, unsigned int bytes_done, int error)
350 {
351         mdk_rdev_t *rdev = bio->bi_private;
352         mddev_t *mddev = rdev->mddev;
353         if (bio->bi_size)
354                 return 1;
355
356         if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
357                 md_error(mddev, rdev);
358
359         if (atomic_dec_and_test(&mddev->pending_writes))
360                 wake_up(&mddev->sb_wait);
361         bio_put(bio);
362         return 0;
363 }
364
365 static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
366 {
367         struct bio *bio2 = bio->bi_private;
368         mdk_rdev_t *rdev = bio2->bi_private;
369         mddev_t *mddev = rdev->mddev;
370         if (bio->bi_size)
371                 return 1;
372
373         if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
374             error == -EOPNOTSUPP) {
375                 unsigned long flags;
376                 /* barriers don't appear to be supported :-( */
377                 set_bit(BarriersNotsupp, &rdev->flags);
378                 mddev->barriers_work = 0;
379                 spin_lock_irqsave(&mddev->write_lock, flags);
380                 bio2->bi_next = mddev->biolist;
381                 mddev->biolist = bio2;
382                 spin_unlock_irqrestore(&mddev->write_lock, flags);
383                 wake_up(&mddev->sb_wait);
384                 bio_put(bio);
385                 return 0;
386         }
387         bio_put(bio2);
388         bio->bi_private = rdev;
389         return super_written(bio, bytes_done, error);
390 }
391
392 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
393                    sector_t sector, int size, struct page *page)
394 {
395         /* write first size bytes of page to sector of rdev
396          * Increment mddev->pending_writes before returning
397          * and decrement it on completion, waking up sb_wait
398          * if zero is reached.
399          * If an error occurred, call md_error
400          *
401          * As we might need to resubmit the request if BIO_RW_BARRIER
402          * causes ENOTSUPP, we allocate a spare bio...
403          */
404         struct bio *bio = bio_alloc(GFP_NOIO, 1);
405         int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
406
407         bio->bi_bdev = rdev->bdev;
408         bio->bi_sector = sector;
409         bio_add_page(bio, page, size, 0);
410         bio->bi_private = rdev;
411         bio->bi_end_io = super_written;
412         bio->bi_rw = rw;
413
414         atomic_inc(&mddev->pending_writes);
415         if (!test_bit(BarriersNotsupp, &rdev->flags)) {
416                 struct bio *rbio;
417                 rw |= (1<<BIO_RW_BARRIER);
418                 rbio = bio_clone(bio, GFP_NOIO);
419                 rbio->bi_private = bio;
420                 rbio->bi_end_io = super_written_barrier;
421                 submit_bio(rw, rbio);
422         } else
423                 submit_bio(rw, bio);
424 }
425
426 void md_super_wait(mddev_t *mddev)
427 {
428         /* wait for all superblock writes that were scheduled to complete.
429          * if any had to be retried (due to BARRIER problems), retry them
430          */
431         DEFINE_WAIT(wq);
432         for(;;) {
433                 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
434                 if (atomic_read(&mddev->pending_writes)==0)
435                         break;
436                 while (mddev->biolist) {
437                         struct bio *bio;
438                         spin_lock_irq(&mddev->write_lock);
439                         bio = mddev->biolist;
440                         mddev->biolist = bio->bi_next ;
441                         bio->bi_next = NULL;
442                         spin_unlock_irq(&mddev->write_lock);
443                         submit_bio(bio->bi_rw, bio);
444                 }
445                 schedule();
446         }
447         finish_wait(&mddev->sb_wait, &wq);
448 }
449
450 static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
451 {
452         if (bio->bi_size)
453                 return 1;
454
455         complete((struct completion*)bio->bi_private);
456         return 0;
457 }
458
459 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
460                    struct page *page, int rw)
461 {
462         struct bio *bio = bio_alloc(GFP_NOIO, 1);
463         struct completion event;
464         int ret;
465
466         rw |= (1 << BIO_RW_SYNC);
467
468         bio->bi_bdev = bdev;
469         bio->bi_sector = sector;
470         bio_add_page(bio, page, size, 0);
471         init_completion(&event);
472         bio->bi_private = &event;
473         bio->bi_end_io = bi_complete;
474         submit_bio(rw, bio);
475         wait_for_completion(&event);
476
477         ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
478         bio_put(bio);
479         return ret;
480 }
481 EXPORT_SYMBOL_GPL(sync_page_io);
482
483 static int read_disk_sb(mdk_rdev_t * rdev, int size)
484 {
485         char b[BDEVNAME_SIZE];
486         if (!rdev->sb_page) {
487                 MD_BUG();
488                 return -EINVAL;
489         }
490         if (rdev->sb_loaded)
491                 return 0;
492
493
494         if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
495                 goto fail;
496         rdev->sb_loaded = 1;
497         return 0;
498
499 fail:
500         printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
501                 bdevname(rdev->bdev,b));
502         return -EINVAL;
503 }
504
505 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
506 {
507         if (    (sb1->set_uuid0 == sb2->set_uuid0) &&
508                 (sb1->set_uuid1 == sb2->set_uuid1) &&
509                 (sb1->set_uuid2 == sb2->set_uuid2) &&
510                 (sb1->set_uuid3 == sb2->set_uuid3))
511
512                 return 1;
513
514         return 0;
515 }
516
517
518 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
519 {
520         int ret;
521         mdp_super_t *tmp1, *tmp2;
522
523         tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
524         tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
525
526         if (!tmp1 || !tmp2) {
527                 ret = 0;
528                 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
529                 goto abort;
530         }
531
532         *tmp1 = *sb1;
533         *tmp2 = *sb2;
534
535         /*
536          * nr_disks is not constant
537          */
538         tmp1->nr_disks = 0;
539         tmp2->nr_disks = 0;
540
541         if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
542                 ret = 0;
543         else
544                 ret = 1;
545
546 abort:
547         kfree(tmp1);
548         kfree(tmp2);
549         return ret;
550 }
551
552 static unsigned int calc_sb_csum(mdp_super_t * sb)
553 {
554         unsigned int disk_csum, csum;
555
556         disk_csum = sb->sb_csum;
557         sb->sb_csum = 0;
558         csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
559         sb->sb_csum = disk_csum;
560         return csum;
561 }
562
563
564 /*
565  * Handle superblock details.
566  * We want to be able to handle multiple superblock formats
567  * so we have a common interface to them all, and an array of
568  * different handlers.
569  * We rely on user-space to write the initial superblock, and support
570  * reading and updating of superblocks.
571  * Interface methods are:
572  *   int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
573  *      loads and validates a superblock on dev.
574  *      if refdev != NULL, compare superblocks on both devices
575  *    Return:
576  *      0 - dev has a superblock that is compatible with refdev
577  *      1 - dev has a superblock that is compatible and newer than refdev
578  *          so dev should be used as the refdev in future
579  *     -EINVAL superblock incompatible or invalid
580  *     -othererror e.g. -EIO
581  *
582  *   int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
583  *      Verify that dev is acceptable into mddev.
584  *       The first time, mddev->raid_disks will be 0, and data from
585  *       dev should be merged in.  Subsequent calls check that dev
586  *       is new enough.  Return 0 or -EINVAL
587  *
588  *   void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
589  *     Update the superblock for rdev with data in mddev
590  *     This does not write to disc.
591  *
592  */
593
594 struct super_type  {
595         char            *name;
596         struct module   *owner;
597         int             (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
598         int             (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
599         void            (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
600 };
601
602 /*
603  * load_super for 0.90.0 
604  */
605 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
606 {
607         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
608         mdp_super_t *sb;
609         int ret;
610         sector_t sb_offset;
611
612         /*
613          * Calculate the position of the superblock,
614          * it's at the end of the disk.
615          *
616          * It also happens to be a multiple of 4Kb.
617          */
618         sb_offset = calc_dev_sboffset(rdev->bdev);
619         rdev->sb_offset = sb_offset;
620
621         ret = read_disk_sb(rdev, MD_SB_BYTES);
622         if (ret) return ret;
623
624         ret = -EINVAL;
625
626         bdevname(rdev->bdev, b);
627         sb = (mdp_super_t*)page_address(rdev->sb_page);
628
629         if (sb->md_magic != MD_SB_MAGIC) {
630                 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
631                        b);
632                 goto abort;
633         }
634
635         if (sb->major_version != 0 ||
636             sb->minor_version != 90) {
637                 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
638                         sb->major_version, sb->minor_version,
639                         b);
640                 goto abort;
641         }
642
643         if (sb->raid_disks <= 0)
644                 goto abort;
645
646         if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
647                 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
648                         b);
649                 goto abort;
650         }
651
652         rdev->preferred_minor = sb->md_minor;
653         rdev->data_offset = 0;
654         rdev->sb_size = MD_SB_BYTES;
655
656         if (sb->level == LEVEL_MULTIPATH)
657                 rdev->desc_nr = -1;
658         else
659                 rdev->desc_nr = sb->this_disk.number;
660
661         if (refdev == 0)
662                 ret = 1;
663         else {
664                 __u64 ev1, ev2;
665                 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
666                 if (!uuid_equal(refsb, sb)) {
667                         printk(KERN_WARNING "md: %s has different UUID to %s\n",
668                                 b, bdevname(refdev->bdev,b2));
669                         goto abort;
670                 }
671                 if (!sb_equal(refsb, sb)) {
672                         printk(KERN_WARNING "md: %s has same UUID"
673                                " but different superblock to %s\n",
674                                b, bdevname(refdev->bdev, b2));
675                         goto abort;
676                 }
677                 ev1 = md_event(sb);
678                 ev2 = md_event(refsb);
679                 if (ev1 > ev2)
680                         ret = 1;
681                 else 
682                         ret = 0;
683         }
684         rdev->size = calc_dev_size(rdev, sb->chunk_size);
685
686  abort:
687         return ret;
688 }
689
690 /*
691  * validate_super for 0.90.0
692  */
693 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
694 {
695         mdp_disk_t *desc;
696         mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
697
698         rdev->raid_disk = -1;
699         rdev->flags = 0;
700         if (mddev->raid_disks == 0) {
701                 mddev->major_version = 0;
702                 mddev->minor_version = sb->minor_version;
703                 mddev->patch_version = sb->patch_version;
704                 mddev->persistent = ! sb->not_persistent;
705                 mddev->chunk_size = sb->chunk_size;
706                 mddev->ctime = sb->ctime;
707                 mddev->utime = sb->utime;
708                 mddev->level = sb->level;
709                 mddev->layout = sb->layout;
710                 mddev->raid_disks = sb->raid_disks;
711                 mddev->size = sb->size;
712                 mddev->events = md_event(sb);
713                 mddev->bitmap_offset = 0;
714                 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
715
716                 if (sb->state & (1<<MD_SB_CLEAN))
717                         mddev->recovery_cp = MaxSector;
718                 else {
719                         if (sb->events_hi == sb->cp_events_hi && 
720                                 sb->events_lo == sb->cp_events_lo) {
721                                 mddev->recovery_cp = sb->recovery_cp;
722                         } else
723                                 mddev->recovery_cp = 0;
724                 }
725
726                 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
727                 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
728                 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
729                 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
730
731                 mddev->max_disks = MD_SB_DISKS;
732
733                 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
734                     mddev->bitmap_file == NULL) {
735                         if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
736                             && mddev->level != 10) {
737                                 /* FIXME use a better test */
738                                 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
739                                 return -EINVAL;
740                         }
741                         mddev->bitmap_offset = mddev->default_bitmap_offset;
742                 }
743
744         } else if (mddev->pers == NULL) {
745                 /* Insist on good event counter while assembling */
746                 __u64 ev1 = md_event(sb);
747                 ++ev1;
748                 if (ev1 < mddev->events) 
749                         return -EINVAL;
750         } else if (mddev->bitmap) {
751                 /* if adding to array with a bitmap, then we can accept an
752                  * older device ... but not too old.
753                  */
754                 __u64 ev1 = md_event(sb);
755                 if (ev1 < mddev->bitmap->events_cleared)
756                         return 0;
757         } else /* just a hot-add of a new device, leave raid_disk at -1 */
758                 return 0;
759
760         if (mddev->level != LEVEL_MULTIPATH) {
761                 desc = sb->disks + rdev->desc_nr;
762
763                 if (desc->state & (1<<MD_DISK_FAULTY))
764                         set_bit(Faulty, &rdev->flags);
765                 else if (desc->state & (1<<MD_DISK_SYNC) &&
766                          desc->raid_disk < mddev->raid_disks) {
767                         set_bit(In_sync, &rdev->flags);
768                         rdev->raid_disk = desc->raid_disk;
769                 }
770                 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
771                         set_bit(WriteMostly, &rdev->flags);
772         } else /* MULTIPATH are always insync */
773                 set_bit(In_sync, &rdev->flags);
774         return 0;
775 }
776
777 /*
778  * sync_super for 0.90.0
779  */
780 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
781 {
782         mdp_super_t *sb;
783         struct list_head *tmp;
784         mdk_rdev_t *rdev2;
785         int next_spare = mddev->raid_disks;
786
787
788         /* make rdev->sb match mddev data..
789          *
790          * 1/ zero out disks
791          * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
792          * 3/ any empty disks < next_spare become removed
793          *
794          * disks[0] gets initialised to REMOVED because
795          * we cannot be sure from other fields if it has
796          * been initialised or not.
797          */
798         int i;
799         int active=0, working=0,failed=0,spare=0,nr_disks=0;
800
801         rdev->sb_size = MD_SB_BYTES;
802
803         sb = (mdp_super_t*)page_address(rdev->sb_page);
804
805         memset(sb, 0, sizeof(*sb));
806
807         sb->md_magic = MD_SB_MAGIC;
808         sb->major_version = mddev->major_version;
809         sb->minor_version = mddev->minor_version;
810         sb->patch_version = mddev->patch_version;
811         sb->gvalid_words  = 0; /* ignored */
812         memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
813         memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
814         memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
815         memcpy(&sb->set_uuid3, mddev->uuid+12,4);
816
817         sb->ctime = mddev->ctime;
818         sb->level = mddev->level;
819         sb->size  = mddev->size;
820         sb->raid_disks = mddev->raid_disks;
821         sb->md_minor = mddev->md_minor;
822         sb->not_persistent = !mddev->persistent;
823         sb->utime = mddev->utime;
824         sb->state = 0;
825         sb->events_hi = (mddev->events>>32);
826         sb->events_lo = (u32)mddev->events;
827
828         if (mddev->in_sync)
829         {
830                 sb->recovery_cp = mddev->recovery_cp;
831                 sb->cp_events_hi = (mddev->events>>32);
832                 sb->cp_events_lo = (u32)mddev->events;
833                 if (mddev->recovery_cp == MaxSector)
834                         sb->state = (1<< MD_SB_CLEAN);
835         } else
836                 sb->recovery_cp = 0;
837
838         sb->layout = mddev->layout;
839         sb->chunk_size = mddev->chunk_size;
840
841         if (mddev->bitmap && mddev->bitmap_file == NULL)
842                 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
843
844         sb->disks[0].state = (1<<MD_DISK_REMOVED);
845         ITERATE_RDEV(mddev,rdev2,tmp) {
846                 mdp_disk_t *d;
847                 int desc_nr;
848                 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
849                     && !test_bit(Faulty, &rdev2->flags))
850                         desc_nr = rdev2->raid_disk;
851                 else
852                         desc_nr = next_spare++;
853                 rdev2->desc_nr = desc_nr;
854                 d = &sb->disks[rdev2->desc_nr];
855                 nr_disks++;
856                 d->number = rdev2->desc_nr;
857                 d->major = MAJOR(rdev2->bdev->bd_dev);
858                 d->minor = MINOR(rdev2->bdev->bd_dev);
859                 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
860                     && !test_bit(Faulty, &rdev2->flags))
861                         d->raid_disk = rdev2->raid_disk;
862                 else
863                         d->raid_disk = rdev2->desc_nr; /* compatibility */
864                 if (test_bit(Faulty, &rdev2->flags)) {
865                         d->state = (1<<MD_DISK_FAULTY);
866                         failed++;
867                 } else if (test_bit(In_sync, &rdev2->flags)) {
868                         d->state = (1<<MD_DISK_ACTIVE);
869                         d->state |= (1<<MD_DISK_SYNC);
870                         active++;
871                         working++;
872                 } else {
873                         d->state = 0;
874                         spare++;
875                         working++;
876                 }
877                 if (test_bit(WriteMostly, &rdev2->flags))
878                         d->state |= (1<<MD_DISK_WRITEMOSTLY);
879         }
880         /* now set the "removed" and "faulty" bits on any missing devices */
881         for (i=0 ; i < mddev->raid_disks ; i++) {
882                 mdp_disk_t *d = &sb->disks[i];
883                 if (d->state == 0 && d->number == 0) {
884                         d->number = i;
885                         d->raid_disk = i;
886                         d->state = (1<<MD_DISK_REMOVED);
887                         d->state |= (1<<MD_DISK_FAULTY);
888                         failed++;
889                 }
890         }
891         sb->nr_disks = nr_disks;
892         sb->active_disks = active;
893         sb->working_disks = working;
894         sb->failed_disks = failed;
895         sb->spare_disks = spare;
896
897         sb->this_disk = sb->disks[rdev->desc_nr];
898         sb->sb_csum = calc_sb_csum(sb);
899 }
900
901 /*
902  * version 1 superblock
903  */
904
905 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
906 {
907         unsigned int disk_csum, csum;
908         unsigned long long newcsum;
909         int size = 256 + le32_to_cpu(sb->max_dev)*2;
910         unsigned int *isuper = (unsigned int*)sb;
911         int i;
912
913         disk_csum = sb->sb_csum;
914         sb->sb_csum = 0;
915         newcsum = 0;
916         for (i=0; size>=4; size -= 4 )
917                 newcsum += le32_to_cpu(*isuper++);
918
919         if (size == 2)
920                 newcsum += le16_to_cpu(*(unsigned short*) isuper);
921
922         csum = (newcsum & 0xffffffff) + (newcsum >> 32);
923         sb->sb_csum = disk_csum;
924         return cpu_to_le32(csum);
925 }
926
927 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
928 {
929         struct mdp_superblock_1 *sb;
930         int ret;
931         sector_t sb_offset;
932         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
933         int bmask;
934
935         /*
936          * Calculate the position of the superblock.
937          * It is always aligned to a 4K boundary and
938          * depeding on minor_version, it can be:
939          * 0: At least 8K, but less than 12K, from end of device
940          * 1: At start of device
941          * 2: 4K from start of device.
942          */
943         switch(minor_version) {
944         case 0:
945                 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
946                 sb_offset -= 8*2;
947                 sb_offset &= ~(sector_t)(4*2-1);
948                 /* convert from sectors to K */
949                 sb_offset /= 2;
950                 break;
951         case 1:
952                 sb_offset = 0;
953                 break;
954         case 2:
955                 sb_offset = 4;
956                 break;
957         default:
958                 return -EINVAL;
959         }
960         rdev->sb_offset = sb_offset;
961
962         /* superblock is rarely larger than 1K, but it can be larger,
963          * and it is safe to read 4k, so we do that
964          */
965         ret = read_disk_sb(rdev, 4096);
966         if (ret) return ret;
967
968
969         sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
970
971         if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
972             sb->major_version != cpu_to_le32(1) ||
973             le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
974             le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
975             (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
976                 return -EINVAL;
977
978         if (calc_sb_1_csum(sb) != sb->sb_csum) {
979                 printk("md: invalid superblock checksum on %s\n",
980                         bdevname(rdev->bdev,b));
981                 return -EINVAL;
982         }
983         if (le64_to_cpu(sb->data_size) < 10) {
984                 printk("md: data_size too small on %s\n",
985                        bdevname(rdev->bdev,b));
986                 return -EINVAL;
987         }
988         rdev->preferred_minor = 0xffff;
989         rdev->data_offset = le64_to_cpu(sb->data_offset);
990
991         rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
992         bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
993         if (rdev->sb_size & bmask)
994                 rdev-> sb_size = (rdev->sb_size | bmask)+1;
995
996         if (refdev == 0)
997                 return 1;
998         else {
999                 __u64 ev1, ev2;
1000                 struct mdp_superblock_1 *refsb = 
1001                         (struct mdp_superblock_1*)page_address(refdev->sb_page);
1002
1003                 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1004                     sb->level != refsb->level ||
1005                     sb->layout != refsb->layout ||
1006                     sb->chunksize != refsb->chunksize) {
1007                         printk(KERN_WARNING "md: %s has strangely different"
1008                                 " superblock to %s\n",
1009                                 bdevname(rdev->bdev,b),
1010                                 bdevname(refdev->bdev,b2));
1011                         return -EINVAL;
1012                 }
1013                 ev1 = le64_to_cpu(sb->events);
1014                 ev2 = le64_to_cpu(refsb->events);
1015
1016                 if (ev1 > ev2)
1017                         return 1;
1018         }
1019         if (minor_version) 
1020                 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1021         else
1022                 rdev->size = rdev->sb_offset;
1023         if (rdev->size < le64_to_cpu(sb->data_size)/2)
1024                 return -EINVAL;
1025         rdev->size = le64_to_cpu(sb->data_size)/2;
1026         if (le32_to_cpu(sb->chunksize))
1027                 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1028         return 0;
1029 }
1030
1031 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1032 {
1033         struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1034
1035         rdev->raid_disk = -1;
1036         rdev->flags = 0;
1037         if (mddev->raid_disks == 0) {
1038                 mddev->major_version = 1;
1039                 mddev->patch_version = 0;
1040                 mddev->persistent = 1;
1041                 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1042                 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1043                 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1044                 mddev->level = le32_to_cpu(sb->level);
1045                 mddev->layout = le32_to_cpu(sb->layout);
1046                 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1047                 mddev->size = le64_to_cpu(sb->size)/2;
1048                 mddev->events = le64_to_cpu(sb->events);
1049                 mddev->bitmap_offset = 0;
1050                 mddev->default_bitmap_offset = 1024;
1051                 
1052                 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1053                 memcpy(mddev->uuid, sb->set_uuid, 16);
1054
1055                 mddev->max_disks =  (4096-256)/2;
1056
1057                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1058                     mddev->bitmap_file == NULL ) {
1059                         if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1060                             && mddev->level != 10) {
1061                                 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
1062                                 return -EINVAL;
1063                         }
1064                         mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1065                 }
1066         } else if (mddev->pers == NULL) {
1067                 /* Insist of good event counter while assembling */
1068                 __u64 ev1 = le64_to_cpu(sb->events);
1069                 ++ev1;
1070                 if (ev1 < mddev->events)
1071                         return -EINVAL;
1072         } else if (mddev->bitmap) {
1073                 /* If adding to array with a bitmap, then we can accept an
1074                  * older device, but not too old.
1075                  */
1076                 __u64 ev1 = le64_to_cpu(sb->events);
1077                 if (ev1 < mddev->bitmap->events_cleared)
1078                         return 0;
1079         } else /* just a hot-add of a new device, leave raid_disk at -1 */
1080                 return 0;
1081
1082         if (mddev->level != LEVEL_MULTIPATH) {
1083                 int role;
1084                 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1085                 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1086                 switch(role) {
1087                 case 0xffff: /* spare */
1088                         break;
1089                 case 0xfffe: /* faulty */
1090                         set_bit(Faulty, &rdev->flags);
1091                         break;
1092                 default:
1093                         set_bit(In_sync, &rdev->flags);
1094                         rdev->raid_disk = role;
1095                         break;
1096                 }
1097                 if (sb->devflags & WriteMostly1)
1098                         set_bit(WriteMostly, &rdev->flags);
1099         } else /* MULTIPATH are always insync */
1100                 set_bit(In_sync, &rdev->flags);
1101
1102         return 0;
1103 }
1104
1105 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1106 {
1107         struct mdp_superblock_1 *sb;
1108         struct list_head *tmp;
1109         mdk_rdev_t *rdev2;
1110         int max_dev, i;
1111         /* make rdev->sb match mddev and rdev data. */
1112
1113         sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1114
1115         sb->feature_map = 0;
1116         sb->pad0 = 0;
1117         memset(sb->pad1, 0, sizeof(sb->pad1));
1118         memset(sb->pad2, 0, sizeof(sb->pad2));
1119         memset(sb->pad3, 0, sizeof(sb->pad3));
1120
1121         sb->utime = cpu_to_le64((__u64)mddev->utime);
1122         sb->events = cpu_to_le64(mddev->events);
1123         if (mddev->in_sync)
1124                 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1125         else
1126                 sb->resync_offset = cpu_to_le64(0);
1127
1128         if (mddev->bitmap && mddev->bitmap_file == NULL) {
1129                 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1130                 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1131         }
1132
1133         max_dev = 0;
1134         ITERATE_RDEV(mddev,rdev2,tmp)
1135                 if (rdev2->desc_nr+1 > max_dev)
1136                         max_dev = rdev2->desc_nr+1;
1137         
1138         sb->max_dev = cpu_to_le32(max_dev);
1139         for (i=0; i<max_dev;i++)
1140                 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1141         
1142         ITERATE_RDEV(mddev,rdev2,tmp) {
1143                 i = rdev2->desc_nr;
1144                 if (test_bit(Faulty, &rdev2->flags))
1145                         sb->dev_roles[i] = cpu_to_le16(0xfffe);
1146                 else if (test_bit(In_sync, &rdev2->flags))
1147                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1148                 else
1149                         sb->dev_roles[i] = cpu_to_le16(0xffff);
1150         }
1151
1152         sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1153         sb->sb_csum = calc_sb_1_csum(sb);
1154 }
1155
1156
1157 static struct super_type super_types[] = {
1158         [0] = {
1159                 .name   = "0.90.0",
1160                 .owner  = THIS_MODULE,
1161                 .load_super     = super_90_load,
1162                 .validate_super = super_90_validate,
1163                 .sync_super     = super_90_sync,
1164         },
1165         [1] = {
1166                 .name   = "md-1",
1167                 .owner  = THIS_MODULE,
1168                 .load_super     = super_1_load,
1169                 .validate_super = super_1_validate,
1170                 .sync_super     = super_1_sync,
1171         },
1172 };
1173         
1174 static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1175 {
1176         struct list_head *tmp;
1177         mdk_rdev_t *rdev;
1178
1179         ITERATE_RDEV(mddev,rdev,tmp)
1180                 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1181                         return rdev;
1182
1183         return NULL;
1184 }
1185
1186 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1187 {
1188         struct list_head *tmp;
1189         mdk_rdev_t *rdev;
1190
1191         ITERATE_RDEV(mddev1,rdev,tmp)
1192                 if (match_dev_unit(mddev2, rdev))
1193                         return 1;
1194
1195         return 0;
1196 }
1197
1198 static LIST_HEAD(pending_raid_disks);
1199
1200 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1201 {
1202         mdk_rdev_t *same_pdev;
1203         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1204         struct kobject *ko;
1205
1206         if (rdev->mddev) {
1207                 MD_BUG();
1208                 return -EINVAL;
1209         }
1210         same_pdev = match_dev_unit(mddev, rdev);
1211         if (same_pdev)
1212                 printk(KERN_WARNING
1213                         "%s: WARNING: %s appears to be on the same physical"
1214                         " disk as %s. True\n     protection against single-disk"
1215                         " failure might be compromised.\n",
1216                         mdname(mddev), bdevname(rdev->bdev,b),
1217                         bdevname(same_pdev->bdev,b2));
1218
1219         /* Verify rdev->desc_nr is unique.
1220          * If it is -1, assign a free number, else
1221          * check number is not in use
1222          */
1223         if (rdev->desc_nr < 0) {
1224                 int choice = 0;
1225                 if (mddev->pers) choice = mddev->raid_disks;
1226                 while (find_rdev_nr(mddev, choice))
1227                         choice++;
1228                 rdev->desc_nr = choice;
1229         } else {
1230                 if (find_rdev_nr(mddev, rdev->desc_nr))
1231                         return -EBUSY;
1232         }
1233         bdevname(rdev->bdev,b);
1234         if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1235                 return -ENOMEM;
1236                         
1237         list_add(&rdev->same_set, &mddev->disks);
1238         rdev->mddev = mddev;
1239         printk(KERN_INFO "md: bind<%s>\n", b);
1240
1241         rdev->kobj.parent = &mddev->kobj;
1242         kobject_add(&rdev->kobj);
1243
1244         if (rdev->bdev->bd_part)
1245                 ko = &rdev->bdev->bd_part->kobj;
1246         else
1247                 ko = &rdev->bdev->bd_disk->kobj;
1248         sysfs_create_link(&rdev->kobj, ko, "block");
1249         return 0;
1250 }
1251
1252 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1253 {
1254         char b[BDEVNAME_SIZE];
1255         if (!rdev->mddev) {
1256                 MD_BUG();
1257                 return;
1258         }
1259         list_del_init(&rdev->same_set);
1260         printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1261         rdev->mddev = NULL;
1262         sysfs_remove_link(&rdev->kobj, "block");
1263         kobject_del(&rdev->kobj);
1264 }
1265
1266 /*
1267  * prevent the device from being mounted, repartitioned or
1268  * otherwise reused by a RAID array (or any other kernel
1269  * subsystem), by bd_claiming the device.
1270  */
1271 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1272 {
1273         int err = 0;
1274         struct block_device *bdev;
1275         char b[BDEVNAME_SIZE];
1276
1277         bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1278         if (IS_ERR(bdev)) {
1279                 printk(KERN_ERR "md: could not open %s.\n",
1280                         __bdevname(dev, b));
1281                 return PTR_ERR(bdev);
1282         }
1283         err = bd_claim(bdev, rdev);
1284         if (err) {
1285                 printk(KERN_ERR "md: could not bd_claim %s.\n",
1286                         bdevname(bdev, b));
1287                 blkdev_put(bdev);
1288                 return err;
1289         }
1290         rdev->bdev = bdev;
1291         return err;
1292 }
1293
1294 static void unlock_rdev(mdk_rdev_t *rdev)
1295 {
1296         struct block_device *bdev = rdev->bdev;
1297         rdev->bdev = NULL;
1298         if (!bdev)
1299                 MD_BUG();
1300         bd_release(bdev);
1301         blkdev_put(bdev);
1302 }
1303
1304 void md_autodetect_dev(dev_t dev);
1305
1306 static void export_rdev(mdk_rdev_t * rdev)
1307 {
1308         char b[BDEVNAME_SIZE];
1309         printk(KERN_INFO "md: export_rdev(%s)\n",
1310                 bdevname(rdev->bdev,b));
1311         if (rdev->mddev)
1312                 MD_BUG();
1313         free_disk_sb(rdev);
1314         list_del_init(&rdev->same_set);
1315 #ifndef MODULE
1316         md_autodetect_dev(rdev->bdev->bd_dev);
1317 #endif
1318         unlock_rdev(rdev);
1319         kobject_put(&rdev->kobj);
1320 }
1321
1322 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1323 {
1324         unbind_rdev_from_array(rdev);
1325         export_rdev(rdev);
1326 }
1327
1328 static void export_array(mddev_t *mddev)
1329 {
1330         struct list_head *tmp;
1331         mdk_rdev_t *rdev;
1332
1333         ITERATE_RDEV(mddev,rdev,tmp) {
1334                 if (!rdev->mddev) {
1335                         MD_BUG();
1336                         continue;
1337                 }
1338                 kick_rdev_from_array(rdev);
1339         }
1340         if (!list_empty(&mddev->disks))
1341                 MD_BUG();
1342         mddev->raid_disks = 0;
1343         mddev->major_version = 0;
1344 }
1345
1346 static void print_desc(mdp_disk_t *desc)
1347 {
1348         printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1349                 desc->major,desc->minor,desc->raid_disk,desc->state);
1350 }
1351
1352 static void print_sb(mdp_super_t *sb)
1353 {
1354         int i;
1355
1356         printk(KERN_INFO 
1357                 "md:  SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1358                 sb->major_version, sb->minor_version, sb->patch_version,
1359                 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1360                 sb->ctime);
1361         printk(KERN_INFO "md:     L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1362                 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1363                 sb->md_minor, sb->layout, sb->chunk_size);
1364         printk(KERN_INFO "md:     UT:%08x ST:%d AD:%d WD:%d"
1365                 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1366                 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1367                 sb->failed_disks, sb->spare_disks,
1368                 sb->sb_csum, (unsigned long)sb->events_lo);
1369
1370         printk(KERN_INFO);
1371         for (i = 0; i < MD_SB_DISKS; i++) {
1372                 mdp_disk_t *desc;
1373
1374                 desc = sb->disks + i;
1375                 if (desc->number || desc->major || desc->minor ||
1376                     desc->raid_disk || (desc->state && (desc->state != 4))) {
1377                         printk("     D %2d: ", i);
1378                         print_desc(desc);
1379                 }
1380         }
1381         printk(KERN_INFO "md:     THIS: ");
1382         print_desc(&sb->this_disk);
1383
1384 }
1385
1386 static void print_rdev(mdk_rdev_t *rdev)
1387 {
1388         char b[BDEVNAME_SIZE];
1389         printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1390                 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1391                 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1392                 rdev->desc_nr);
1393         if (rdev->sb_loaded) {
1394                 printk(KERN_INFO "md: rdev superblock:\n");
1395                 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1396         } else
1397                 printk(KERN_INFO "md: no rdev superblock!\n");
1398 }
1399
1400 void md_print_devices(void)
1401 {
1402         struct list_head *tmp, *tmp2;
1403         mdk_rdev_t *rdev;
1404         mddev_t *mddev;
1405         char b[BDEVNAME_SIZE];
1406
1407         printk("\n");
1408         printk("md:     **********************************\n");
1409         printk("md:     * <COMPLETE RAID STATE PRINTOUT> *\n");
1410         printk("md:     **********************************\n");
1411         ITERATE_MDDEV(mddev,tmp) {
1412
1413                 if (mddev->bitmap)
1414                         bitmap_print_sb(mddev->bitmap);
1415                 else
1416                         printk("%s: ", mdname(mddev));
1417                 ITERATE_RDEV(mddev,rdev,tmp2)
1418                         printk("<%s>", bdevname(rdev->bdev,b));
1419                 printk("\n");
1420
1421                 ITERATE_RDEV(mddev,rdev,tmp2)
1422                         print_rdev(rdev);
1423         }
1424         printk("md:     **********************************\n");
1425         printk("\n");
1426 }
1427
1428
1429 static void sync_sbs(mddev_t * mddev)
1430 {
1431         mdk_rdev_t *rdev;
1432         struct list_head *tmp;
1433
1434         ITERATE_RDEV(mddev,rdev,tmp) {
1435                 super_types[mddev->major_version].
1436                         sync_super(mddev, rdev);
1437                 rdev->sb_loaded = 1;
1438         }
1439 }
1440
1441 static void md_update_sb(mddev_t * mddev)
1442 {
1443         int err;
1444         struct list_head *tmp;
1445         mdk_rdev_t *rdev;
1446         int sync_req;
1447
1448 repeat:
1449         spin_lock_irq(&mddev->write_lock);
1450         sync_req = mddev->in_sync;
1451         mddev->utime = get_seconds();
1452         mddev->events ++;
1453
1454         if (!mddev->events) {
1455                 /*
1456                  * oops, this 64-bit counter should never wrap.
1457                  * Either we are in around ~1 trillion A.C., assuming
1458                  * 1 reboot per second, or we have a bug:
1459                  */
1460                 MD_BUG();
1461                 mddev->events --;
1462         }
1463         mddev->sb_dirty = 2;
1464         sync_sbs(mddev);
1465
1466         /*
1467          * do not write anything to disk if using
1468          * nonpersistent superblocks
1469          */
1470         if (!mddev->persistent) {
1471                 mddev->sb_dirty = 0;
1472                 spin_unlock_irq(&mddev->write_lock);
1473                 wake_up(&mddev->sb_wait);
1474                 return;
1475         }
1476         spin_unlock_irq(&mddev->write_lock);
1477
1478         dprintk(KERN_INFO 
1479                 "md: updating %s RAID superblock on device (in sync %d)\n",
1480                 mdname(mddev),mddev->in_sync);
1481
1482         err = bitmap_update_sb(mddev->bitmap);
1483         ITERATE_RDEV(mddev,rdev,tmp) {
1484                 char b[BDEVNAME_SIZE];
1485                 dprintk(KERN_INFO "md: ");
1486                 if (test_bit(Faulty, &rdev->flags))
1487                         dprintk("(skipping faulty ");
1488
1489                 dprintk("%s ", bdevname(rdev->bdev,b));
1490                 if (!test_bit(Faulty, &rdev->flags)) {
1491                         md_super_write(mddev,rdev,
1492                                        rdev->sb_offset<<1, rdev->sb_size,
1493                                        rdev->sb_page);
1494                         dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1495                                 bdevname(rdev->bdev,b),
1496                                 (unsigned long long)rdev->sb_offset);
1497
1498                 } else
1499                         dprintk(")\n");
1500                 if (mddev->level == LEVEL_MULTIPATH)
1501                         /* only need to write one superblock... */
1502                         break;
1503         }
1504         md_super_wait(mddev);
1505         /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1506
1507         spin_lock_irq(&mddev->write_lock);
1508         if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
1509                 /* have to write it out again */
1510                 spin_unlock_irq(&mddev->write_lock);
1511                 goto repeat;
1512         }
1513         mddev->sb_dirty = 0;
1514         spin_unlock_irq(&mddev->write_lock);
1515         wake_up(&mddev->sb_wait);
1516
1517 }
1518
1519 struct rdev_sysfs_entry {
1520         struct attribute attr;
1521         ssize_t (*show)(mdk_rdev_t *, char *);
1522         ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1523 };
1524
1525 static ssize_t
1526 state_show(mdk_rdev_t *rdev, char *page)
1527 {
1528         char *sep = "";
1529         int len=0;
1530
1531         if (test_bit(Faulty, &rdev->flags)) {
1532                 len+= sprintf(page+len, "%sfaulty",sep);
1533                 sep = ",";
1534         }
1535         if (test_bit(In_sync, &rdev->flags)) {
1536                 len += sprintf(page+len, "%sin_sync",sep);
1537                 sep = ",";
1538         }
1539         if (!test_bit(Faulty, &rdev->flags) &&
1540             !test_bit(In_sync, &rdev->flags)) {
1541                 len += sprintf(page+len, "%sspare", sep);
1542                 sep = ",";
1543         }
1544         return len+sprintf(page+len, "\n");
1545 }
1546
1547 static struct rdev_sysfs_entry
1548 rdev_state = __ATTR_RO(state);
1549
1550 static ssize_t
1551 super_show(mdk_rdev_t *rdev, char *page)
1552 {
1553         if (rdev->sb_loaded && rdev->sb_size) {
1554                 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1555                 return rdev->sb_size;
1556         } else
1557                 return 0;
1558 }
1559 static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1560
1561 static struct attribute *rdev_default_attrs[] = {
1562         &rdev_state.attr,
1563         &rdev_super.attr,
1564         NULL,
1565 };
1566 static ssize_t
1567 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1568 {
1569         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1570         mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1571
1572         if (!entry->show)
1573                 return -EIO;
1574         return entry->show(rdev, page);
1575 }
1576
1577 static ssize_t
1578 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1579               const char *page, size_t length)
1580 {
1581         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1582         mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1583
1584         if (!entry->store)
1585                 return -EIO;
1586         return entry->store(rdev, page, length);
1587 }
1588
1589 static void rdev_free(struct kobject *ko)
1590 {
1591         mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1592         kfree(rdev);
1593 }
1594 static struct sysfs_ops rdev_sysfs_ops = {
1595         .show           = rdev_attr_show,
1596         .store          = rdev_attr_store,
1597 };
1598 static struct kobj_type rdev_ktype = {
1599         .release        = rdev_free,
1600         .sysfs_ops      = &rdev_sysfs_ops,
1601         .default_attrs  = rdev_default_attrs,
1602 };
1603
1604 /*
1605  * Import a device. If 'super_format' >= 0, then sanity check the superblock
1606  *
1607  * mark the device faulty if:
1608  *
1609  *   - the device is nonexistent (zero size)
1610  *   - the device has no valid superblock
1611  *
1612  * a faulty rdev _never_ has rdev->sb set.
1613  */
1614 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1615 {
1616         char b[BDEVNAME_SIZE];
1617         int err;
1618         mdk_rdev_t *rdev;
1619         sector_t size;
1620
1621         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1622         if (!rdev) {
1623                 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1624                 return ERR_PTR(-ENOMEM);
1625         }
1626
1627         if ((err = alloc_disk_sb(rdev)))
1628                 goto abort_free;
1629
1630         err = lock_rdev(rdev, newdev);
1631         if (err)
1632                 goto abort_free;
1633
1634         rdev->kobj.parent = NULL;
1635         rdev->kobj.ktype = &rdev_ktype;
1636         kobject_init(&rdev->kobj);
1637
1638         rdev->desc_nr = -1;
1639         rdev->flags = 0;
1640         rdev->data_offset = 0;
1641         atomic_set(&rdev->nr_pending, 0);
1642         atomic_set(&rdev->read_errors, 0);
1643
1644         size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1645         if (!size) {
1646                 printk(KERN_WARNING 
1647                         "md: %s has zero or unknown size, marking faulty!\n",
1648                         bdevname(rdev->bdev,b));
1649                 err = -EINVAL;
1650                 goto abort_free;
1651         }
1652
1653         if (super_format >= 0) {
1654                 err = super_types[super_format].
1655                         load_super(rdev, NULL, super_minor);
1656                 if (err == -EINVAL) {
1657                         printk(KERN_WARNING 
1658                                 "md: %s has invalid sb, not importing!\n",
1659                                 bdevname(rdev->bdev,b));
1660                         goto abort_free;
1661                 }
1662                 if (err < 0) {
1663                         printk(KERN_WARNING 
1664                                 "md: could not read %s's sb, not importing!\n",
1665                                 bdevname(rdev->bdev,b));
1666                         goto abort_free;
1667                 }
1668         }
1669         INIT_LIST_HEAD(&rdev->same_set);
1670
1671         return rdev;
1672
1673 abort_free:
1674         if (rdev->sb_page) {
1675                 if (rdev->bdev)
1676                         unlock_rdev(rdev);
1677                 free_disk_sb(rdev);
1678         }
1679         kfree(rdev);
1680         return ERR_PTR(err);
1681 }
1682
1683 /*
1684  * Check a full RAID array for plausibility
1685  */
1686
1687
1688 static void analyze_sbs(mddev_t * mddev)
1689 {
1690         int i;
1691         struct list_head *tmp;
1692         mdk_rdev_t *rdev, *freshest;
1693         char b[BDEVNAME_SIZE];
1694
1695         freshest = NULL;
1696         ITERATE_RDEV(mddev,rdev,tmp)
1697                 switch (super_types[mddev->major_version].
1698                         load_super(rdev, freshest, mddev->minor_version)) {
1699                 case 1:
1700                         freshest = rdev;
1701                         break;
1702                 case 0:
1703                         break;
1704                 default:
1705                         printk( KERN_ERR \
1706                                 "md: fatal superblock inconsistency in %s"
1707                                 " -- removing from array\n", 
1708                                 bdevname(rdev->bdev,b));
1709                         kick_rdev_from_array(rdev);
1710                 }
1711
1712
1713         super_types[mddev->major_version].
1714                 validate_super(mddev, freshest);
1715
1716         i = 0;
1717         ITERATE_RDEV(mddev,rdev,tmp) {
1718                 if (rdev != freshest)
1719                         if (super_types[mddev->major_version].
1720                             validate_super(mddev, rdev)) {
1721                                 printk(KERN_WARNING "md: kicking non-fresh %s"
1722                                         " from array!\n",
1723                                         bdevname(rdev->bdev,b));
1724                                 kick_rdev_from_array(rdev);
1725                                 continue;
1726                         }
1727                 if (mddev->level == LEVEL_MULTIPATH) {
1728                         rdev->desc_nr = i++;
1729                         rdev->raid_disk = rdev->desc_nr;
1730                         set_bit(In_sync, &rdev->flags);
1731                 }
1732         }
1733
1734
1735
1736         if (mddev->recovery_cp != MaxSector &&
1737             mddev->level >= 1)
1738                 printk(KERN_ERR "md: %s: raid array is not clean"
1739                        " -- starting background reconstruction\n",
1740                        mdname(mddev));
1741
1742 }
1743
1744 static ssize_t
1745 level_show(mddev_t *mddev, char *page)
1746 {
1747         mdk_personality_t *p = mddev->pers;
1748         if (p == NULL && mddev->raid_disks == 0)
1749                 return 0;
1750         if (mddev->level >= 0)
1751                 return sprintf(page, "raid%d\n", mddev->level);
1752         else
1753                 return sprintf(page, "%s\n", p->name);
1754 }
1755
1756 static struct md_sysfs_entry md_level = __ATTR_RO(level);
1757
1758 static ssize_t
1759 raid_disks_show(mddev_t *mddev, char *page)
1760 {
1761         if (mddev->raid_disks == 0)
1762                 return 0;
1763         return sprintf(page, "%d\n", mddev->raid_disks);
1764 }
1765
1766 static struct md_sysfs_entry md_raid_disks = __ATTR_RO(raid_disks);
1767
1768 static ssize_t
1769 action_show(mddev_t *mddev, char *page)
1770 {
1771         char *type = "idle";
1772         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1773             test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
1774                 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1775                         if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1776                                 type = "resync";
1777                         else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
1778                                 type = "check";
1779                         else
1780                                 type = "repair";
1781                 } else
1782                         type = "recover";
1783         }
1784         return sprintf(page, "%s\n", type);
1785 }
1786
1787 static ssize_t
1788 action_store(mddev_t *mddev, const char *page, size_t len)
1789 {
1790         if (!mddev->pers || !mddev->pers->sync_request)
1791                 return -EINVAL;
1792
1793         if (strcmp(page, "idle")==0 || strcmp(page, "idle\n")==0) {
1794                 if (mddev->sync_thread) {
1795                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1796                         md_unregister_thread(mddev->sync_thread);
1797                         mddev->sync_thread = NULL;
1798                         mddev->recovery = 0;
1799                 }
1800                 return len;
1801         }
1802
1803         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
1804             test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
1805                 return -EBUSY;
1806         if (strcmp(page, "resync")==0 || strcmp(page, "resync\n")==0 ||
1807             strcmp(page, "recover")==0 || strcmp(page, "recover\n")==0)
1808                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1809         else {
1810                 if (strcmp(page, "check")==0 || strcmp(page, "check\n")==0)
1811                         set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
1812                 else if (strcmp(page, "repair")!=0 && strcmp(page, "repair\n")!=0)
1813                         return -EINVAL;
1814                 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
1815                 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
1816                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1817         }
1818         md_wakeup_thread(mddev->thread);
1819         return len;
1820 }
1821
1822 static ssize_t
1823 mismatch_cnt_show(mddev_t *mddev, char *page)
1824 {
1825         return sprintf(page, "%llu\n",
1826                        (unsigned long long) mddev->resync_mismatches);
1827 }
1828
1829 static struct md_sysfs_entry
1830 md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
1831
1832
1833 static struct md_sysfs_entry
1834 md_mismatches = __ATTR_RO(mismatch_cnt);
1835
1836 static struct attribute *md_default_attrs[] = {
1837         &md_level.attr,
1838         &md_raid_disks.attr,
1839         NULL,
1840 };
1841
1842 static struct attribute *md_redundancy_attrs[] = {
1843         &md_scan_mode.attr,
1844         &md_mismatches.attr,
1845         NULL,
1846 };
1847 static struct attribute_group md_redundancy_group = {
1848         .name = NULL,
1849         .attrs = md_redundancy_attrs,
1850 };
1851
1852
1853 static ssize_t
1854 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1855 {
1856         struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1857         mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1858         ssize_t rv;
1859
1860         if (!entry->show)
1861                 return -EIO;
1862         mddev_lock(mddev);
1863         rv = entry->show(mddev, page);
1864         mddev_unlock(mddev);
1865         return rv;
1866 }
1867
1868 static ssize_t
1869 md_attr_store(struct kobject *kobj, struct attribute *attr,
1870               const char *page, size_t length)
1871 {
1872         struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
1873         mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
1874         ssize_t rv;
1875
1876         if (!entry->store)
1877                 return -EIO;
1878         mddev_lock(mddev);
1879         rv = entry->store(mddev, page, length);
1880         mddev_unlock(mddev);
1881         return rv;
1882 }
1883
1884 static void md_free(struct kobject *ko)
1885 {
1886         mddev_t *mddev = container_of(ko, mddev_t, kobj);
1887         kfree(mddev);
1888 }
1889
1890 static struct sysfs_ops md_sysfs_ops = {
1891         .show   = md_attr_show,
1892         .store  = md_attr_store,
1893 };
1894 static struct kobj_type md_ktype = {
1895         .release        = md_free,
1896         .sysfs_ops      = &md_sysfs_ops,
1897         .default_attrs  = md_default_attrs,
1898 };
1899
1900 int mdp_major = 0;
1901
1902 static struct kobject *md_probe(dev_t dev, int *part, void *data)
1903 {
1904         static DECLARE_MUTEX(disks_sem);
1905         mddev_t *mddev = mddev_find(dev);
1906         struct gendisk *disk;
1907         int partitioned = (MAJOR(dev) != MD_MAJOR);
1908         int shift = partitioned ? MdpMinorShift : 0;
1909         int unit = MINOR(dev) >> shift;
1910
1911         if (!mddev)
1912                 return NULL;
1913
1914         down(&disks_sem);
1915         if (mddev->gendisk) {
1916                 up(&disks_sem);
1917                 mddev_put(mddev);
1918                 return NULL;
1919         }
1920         disk = alloc_disk(1 << shift);
1921         if (!disk) {
1922                 up(&disks_sem);
1923                 mddev_put(mddev);
1924                 return NULL;
1925         }
1926         disk->major = MAJOR(dev);
1927         disk->first_minor = unit << shift;
1928         if (partitioned) {
1929                 sprintf(disk->disk_name, "md_d%d", unit);
1930                 sprintf(disk->devfs_name, "md/d%d", unit);
1931         } else {
1932                 sprintf(disk->disk_name, "md%d", unit);
1933                 sprintf(disk->devfs_name, "md/%d", unit);
1934         }
1935         disk->fops = &md_fops;
1936         disk->private_data = mddev;
1937         disk->queue = mddev->queue;
1938         add_disk(disk);
1939         mddev->gendisk = disk;
1940         up(&disks_sem);
1941         mddev->kobj.parent = &disk->kobj;
1942         mddev->kobj.k_name = NULL;
1943         snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
1944         mddev->kobj.ktype = &md_ktype;
1945         kobject_register(&mddev->kobj);
1946         return NULL;
1947 }
1948
1949 void md_wakeup_thread(mdk_thread_t *thread);
1950
1951 static void md_safemode_timeout(unsigned long data)
1952 {
1953         mddev_t *mddev = (mddev_t *) data;
1954
1955         mddev->safemode = 1;
1956         md_wakeup_thread(mddev->thread);
1957 }
1958
1959 static int start_dirty_degraded;
1960
1961 static int do_md_run(mddev_t * mddev)
1962 {
1963         int pnum, err;
1964         int chunk_size;
1965         struct list_head *tmp;
1966         mdk_rdev_t *rdev;
1967         struct gendisk *disk;
1968         char b[BDEVNAME_SIZE];
1969
1970         if (list_empty(&mddev->disks))
1971                 /* cannot run an array with no devices.. */
1972                 return -EINVAL;
1973
1974         if (mddev->pers)
1975                 return -EBUSY;
1976
1977         /*
1978          * Analyze all RAID superblock(s)
1979          */
1980         if (!mddev->raid_disks)
1981                 analyze_sbs(mddev);
1982
1983         chunk_size = mddev->chunk_size;
1984         pnum = level_to_pers(mddev->level);
1985
1986         if ((pnum != MULTIPATH) && (pnum != RAID1)) {
1987                 if (!chunk_size) {
1988                         /*
1989                          * 'default chunksize' in the old md code used to
1990                          * be PAGE_SIZE, baaad.
1991                          * we abort here to be on the safe side. We don't
1992                          * want to continue the bad practice.
1993                          */
1994                         printk(KERN_ERR 
1995                                 "no chunksize specified, see 'man raidtab'\n");
1996                         return -EINVAL;
1997                 }
1998                 if (chunk_size > MAX_CHUNK_SIZE) {
1999                         printk(KERN_ERR "too big chunk_size: %d > %d\n",
2000                                 chunk_size, MAX_CHUNK_SIZE);
2001                         return -EINVAL;
2002                 }
2003                 /*
2004                  * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2005                  */
2006                 if ( (1 << ffz(~chunk_size)) != chunk_size) {
2007                         printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
2008                         return -EINVAL;
2009                 }
2010                 if (chunk_size < PAGE_SIZE) {
2011                         printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2012                                 chunk_size, PAGE_SIZE);
2013                         return -EINVAL;
2014                 }
2015
2016                 /* devices must have minimum size of one chunk */
2017                 ITERATE_RDEV(mddev,rdev,tmp) {
2018                         if (test_bit(Faulty, &rdev->flags))
2019                                 continue;
2020                         if (rdev->size < chunk_size / 1024) {
2021                                 printk(KERN_WARNING
2022                                         "md: Dev %s smaller than chunk_size:"
2023                                         " %lluk < %dk\n",
2024                                         bdevname(rdev->bdev,b),
2025                                         (unsigned long long)rdev->size,
2026                                         chunk_size / 1024);
2027                                 return -EINVAL;
2028                         }
2029                 }
2030         }
2031
2032 #ifdef CONFIG_KMOD
2033         if (!pers[pnum])
2034         {
2035                 request_module("md-personality-%d", pnum);
2036         }
2037 #endif
2038
2039         /*
2040          * Drop all container device buffers, from now on
2041          * the only valid external interface is through the md
2042          * device.
2043          * Also find largest hardsector size
2044          */
2045         ITERATE_RDEV(mddev,rdev,tmp) {
2046                 if (test_bit(Faulty, &rdev->flags))
2047                         continue;
2048                 sync_blockdev(rdev->bdev);
2049                 invalidate_bdev(rdev->bdev, 0);
2050         }
2051
2052         md_probe(mddev->unit, NULL, NULL);
2053         disk = mddev->gendisk;
2054         if (!disk)
2055                 return -ENOMEM;
2056
2057         spin_lock(&pers_lock);
2058         if (!pers[pnum] || !try_module_get(pers[pnum]->owner)) {
2059                 spin_unlock(&pers_lock);
2060                 printk(KERN_WARNING "md: personality %d is not loaded!\n",
2061                        pnum);
2062                 return -EINVAL;
2063         }
2064
2065         mddev->pers = pers[pnum];
2066         spin_unlock(&pers_lock);
2067
2068         mddev->recovery = 0;
2069         mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
2070         mddev->barriers_work = 1;
2071         mddev->ok_start_degraded = start_dirty_degraded;
2072
2073         if (start_readonly)
2074                 mddev->ro = 2; /* read-only, but switch on first write */
2075
2076         err = mddev->pers->run(mddev);
2077         if (!err && mddev->pers->sync_request) {
2078                 err = bitmap_create(mddev);
2079                 if (err) {
2080                         printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2081                                mdname(mddev), err);
2082                         mddev->pers->stop(mddev);
2083                 }
2084         }
2085         if (err) {
2086                 printk(KERN_ERR "md: pers->run() failed ...\n");
2087                 module_put(mddev->pers->owner);
2088                 mddev->pers = NULL;
2089                 bitmap_destroy(mddev);
2090                 return err;
2091         }
2092         if (mddev->pers->sync_request)
2093                 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
2094         else if (mddev->ro == 2) /* auto-readonly not meaningful */
2095                 mddev->ro = 0;
2096
2097         atomic_set(&mddev->writes_pending,0);
2098         mddev->safemode = 0;
2099         mddev->safemode_timer.function = md_safemode_timeout;
2100         mddev->safemode_timer.data = (unsigned long) mddev;
2101         mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2102         mddev->in_sync = 1;
2103
2104         ITERATE_RDEV(mddev,rdev,tmp)
2105                 if (rdev->raid_disk >= 0) {
2106                         char nm[20];
2107                         sprintf(nm, "rd%d", rdev->raid_disk);
2108                         sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2109                 }
2110         
2111         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2112         md_wakeup_thread(mddev->thread);
2113         
2114         if (mddev->sb_dirty)
2115                 md_update_sb(mddev);
2116
2117         set_capacity(disk, mddev->array_size<<1);
2118
2119         /* If we call blk_queue_make_request here, it will
2120          * re-initialise max_sectors etc which may have been
2121          * refined inside -> run.  So just set the bits we need to set.
2122          * Most initialisation happended when we called
2123          * blk_queue_make_request(..., md_fail_request)
2124          * earlier.
2125          */
2126         mddev->queue->queuedata = mddev;
2127         mddev->queue->make_request_fn = mddev->pers->make_request;
2128
2129         mddev->changed = 1;
2130         md_new_event(mddev);
2131         return 0;
2132 }
2133
2134 static int restart_array(mddev_t *mddev)
2135 {
2136         struct gendisk *disk = mddev->gendisk;
2137         int err;
2138
2139         /*
2140          * Complain if it has no devices
2141          */
2142         err = -ENXIO;
2143         if (list_empty(&mddev->disks))
2144                 goto out;
2145
2146         if (mddev->pers) {
2147                 err = -EBUSY;
2148                 if (!mddev->ro)
2149                         goto out;
2150
2151                 mddev->safemode = 0;
2152                 mddev->ro = 0;
2153                 set_disk_ro(disk, 0);
2154
2155                 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2156                         mdname(mddev));
2157                 /*
2158                  * Kick recovery or resync if necessary
2159                  */
2160                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2161                 md_wakeup_thread(mddev->thread);
2162                 err = 0;
2163         } else {
2164                 printk(KERN_ERR "md: %s has no personality assigned.\n",
2165                         mdname(mddev));
2166                 err = -EINVAL;
2167         }
2168
2169 out:
2170         return err;
2171 }
2172
2173 static int do_md_stop(mddev_t * mddev, int ro)
2174 {
2175         int err = 0;
2176         struct gendisk *disk = mddev->gendisk;
2177
2178         if (mddev->pers) {
2179                 if (atomic_read(&mddev->active)>2) {
2180                         printk("md: %s still in use.\n",mdname(mddev));
2181                         return -EBUSY;
2182                 }
2183
2184                 if (mddev->sync_thread) {
2185                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2186                         md_unregister_thread(mddev->sync_thread);
2187                         mddev->sync_thread = NULL;
2188                 }
2189
2190                 del_timer_sync(&mddev->safemode_timer);
2191
2192                 invalidate_partition(disk, 0);
2193
2194                 if (ro) {
2195                         err  = -ENXIO;
2196                         if (mddev->ro==1)
2197                                 goto out;
2198                         mddev->ro = 1;
2199                 } else {
2200                         bitmap_flush(mddev);
2201                         md_super_wait(mddev);
2202                         if (mddev->ro)
2203                                 set_disk_ro(disk, 0);
2204                         blk_queue_make_request(mddev->queue, md_fail_request);
2205                         mddev->pers->stop(mddev);
2206                         if (mddev->pers->sync_request)
2207                                 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2208
2209                         module_put(mddev->pers->owner);
2210                         mddev->pers = NULL;
2211                         if (mddev->ro)
2212                                 mddev->ro = 0;
2213                 }
2214                 if (!mddev->in_sync) {
2215                         /* mark array as shutdown cleanly */
2216                         mddev->in_sync = 1;
2217                         md_update_sb(mddev);
2218                 }
2219                 if (ro)
2220                         set_disk_ro(disk, 1);
2221         }
2222
2223         bitmap_destroy(mddev);
2224         if (mddev->bitmap_file) {
2225                 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2226                 fput(mddev->bitmap_file);
2227                 mddev->bitmap_file = NULL;
2228         }
2229         mddev->bitmap_offset = 0;
2230
2231         /*
2232          * Free resources if final stop
2233          */
2234         if (!ro) {
2235                 mdk_rdev_t *rdev;
2236                 struct list_head *tmp;
2237                 struct gendisk *disk;
2238                 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2239
2240                 ITERATE_RDEV(mddev,rdev,tmp)
2241                         if (rdev->raid_disk >= 0) {
2242                                 char nm[20];
2243                                 sprintf(nm, "rd%d", rdev->raid_disk);
2244                                 sysfs_remove_link(&mddev->kobj, nm);
2245                         }
2246
2247                 export_array(mddev);
2248
2249                 mddev->array_size = 0;
2250                 disk = mddev->gendisk;
2251                 if (disk)
2252                         set_capacity(disk, 0);
2253                 mddev->changed = 1;
2254         } else
2255                 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2256                         mdname(mddev));
2257         err = 0;
2258         md_new_event(mddev);
2259 out:
2260         return err;
2261 }
2262
2263 static void autorun_array(mddev_t *mddev)
2264 {
2265         mdk_rdev_t *rdev;
2266         struct list_head *tmp;
2267         int err;
2268
2269         if (list_empty(&mddev->disks))
2270                 return;
2271
2272         printk(KERN_INFO "md: running: ");
2273
2274         ITERATE_RDEV(mddev,rdev,tmp) {
2275                 char b[BDEVNAME_SIZE];
2276                 printk("<%s>", bdevname(rdev->bdev,b));
2277         }
2278         printk("\n");
2279
2280         err = do_md_run (mddev);
2281         if (err) {
2282                 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2283                 do_md_stop (mddev, 0);
2284         }
2285 }
2286
2287 /*
2288  * lets try to run arrays based on all disks that have arrived
2289  * until now. (those are in pending_raid_disks)
2290  *
2291  * the method: pick the first pending disk, collect all disks with
2292  * the same UUID, remove all from the pending list and put them into
2293  * the 'same_array' list. Then order this list based on superblock
2294  * update time (freshest comes first), kick out 'old' disks and
2295  * compare superblocks. If everything's fine then run it.
2296  *
2297  * If "unit" is allocated, then bump its reference count
2298  */
2299 static void autorun_devices(int part)
2300 {
2301         struct list_head candidates;
2302         struct list_head *tmp;
2303         mdk_rdev_t *rdev0, *rdev;
2304         mddev_t *mddev;
2305         char b[BDEVNAME_SIZE];
2306
2307         printk(KERN_INFO "md: autorun ...\n");
2308         while (!list_empty(&pending_raid_disks)) {
2309                 dev_t dev;
2310                 rdev0 = list_entry(pending_raid_disks.next,
2311                                          mdk_rdev_t, same_set);
2312
2313                 printk(KERN_INFO "md: considering %s ...\n",
2314                         bdevname(rdev0->bdev,b));
2315                 INIT_LIST_HEAD(&candidates);
2316                 ITERATE_RDEV_PENDING(rdev,tmp)
2317                         if (super_90_load(rdev, rdev0, 0) >= 0) {
2318                                 printk(KERN_INFO "md:  adding %s ...\n",
2319                                         bdevname(rdev->bdev,b));
2320                                 list_move(&rdev->same_set, &candidates);
2321                         }
2322                 /*
2323                  * now we have a set of devices, with all of them having
2324                  * mostly sane superblocks. It's time to allocate the
2325                  * mddev.
2326                  */
2327                 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2328                         printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2329                                bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2330                         break;
2331                 }
2332                 if (part)
2333                         dev = MKDEV(mdp_major,
2334                                     rdev0->preferred_minor << MdpMinorShift);
2335                 else
2336                         dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2337
2338                 md_probe(dev, NULL, NULL);
2339                 mddev = mddev_find(dev);
2340                 if (!mddev) {
2341                         printk(KERN_ERR 
2342                                 "md: cannot allocate memory for md drive.\n");
2343                         break;
2344                 }
2345                 if (mddev_lock(mddev)) 
2346                         printk(KERN_WARNING "md: %s locked, cannot run\n",
2347                                mdname(mddev));
2348                 else if (mddev->raid_disks || mddev->major_version
2349                          || !list_empty(&mddev->disks)) {
2350                         printk(KERN_WARNING 
2351                                 "md: %s already running, cannot run %s\n",
2352                                 mdname(mddev), bdevname(rdev0->bdev,b));
2353                         mddev_unlock(mddev);
2354                 } else {
2355                         printk(KERN_INFO "md: created %s\n", mdname(mddev));
2356                         ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2357                                 list_del_init(&rdev->same_set);
2358                                 if (bind_rdev_to_array(rdev, mddev))
2359                                         export_rdev(rdev);
2360                         }
2361                         autorun_array(mddev);
2362                         mddev_unlock(mddev);
2363                 }
2364                 /* on success, candidates will be empty, on error
2365                  * it won't...
2366                  */
2367                 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2368                         export_rdev(rdev);
2369                 mddev_put(mddev);
2370         }
2371         printk(KERN_INFO "md: ... autorun DONE.\n");
2372 }
2373
2374 /*
2375  * import RAID devices based on one partition
2376  * if possible, the array gets run as well.
2377  */
2378
2379 static int autostart_array(dev_t startdev)
2380 {
2381         char b[BDEVNAME_SIZE];
2382         int err = -EINVAL, i;
2383         mdp_super_t *sb = NULL;
2384         mdk_rdev_t *start_rdev = NULL, *rdev;
2385
2386         start_rdev = md_import_device(startdev, 0, 0);
2387         if (IS_ERR(start_rdev))
2388                 return err;
2389
2390
2391         /* NOTE: this can only work for 0.90.0 superblocks */
2392         sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2393         if (sb->major_version != 0 ||
2394             sb->minor_version != 90 ) {
2395                 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2396                 export_rdev(start_rdev);
2397                 return err;
2398         }
2399
2400         if (test_bit(Faulty, &start_rdev->flags)) {
2401                 printk(KERN_WARNING 
2402                         "md: can not autostart based on faulty %s!\n",
2403                         bdevname(start_rdev->bdev,b));
2404                 export_rdev(start_rdev);
2405                 return err;
2406         }
2407         list_add(&start_rdev->same_set, &pending_raid_disks);
2408
2409         for (i = 0; i < MD_SB_DISKS; i++) {
2410                 mdp_disk_t *desc = sb->disks + i;
2411                 dev_t dev = MKDEV(desc->major, desc->minor);
2412
2413                 if (!dev)
2414                         continue;
2415                 if (dev == startdev)
2416                         continue;
2417                 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
2418                         continue;
2419                 rdev = md_import_device(dev, 0, 0);
2420                 if (IS_ERR(rdev))
2421                         continue;
2422
2423                 list_add(&rdev->same_set, &pending_raid_disks);
2424         }
2425
2426         /*
2427          * possibly return codes
2428          */
2429         autorun_devices(0);
2430         return 0;
2431
2432 }
2433
2434
2435 static int get_version(void __user * arg)
2436 {
2437         mdu_version_t ver;
2438
2439         ver.major = MD_MAJOR_VERSION;
2440         ver.minor = MD_MINOR_VERSION;
2441         ver.patchlevel = MD_PATCHLEVEL_VERSION;
2442
2443         if (copy_to_user(arg, &ver, sizeof(ver)))
2444                 return -EFAULT;
2445
2446         return 0;
2447 }
2448
2449 static int get_array_info(mddev_t * mddev, void __user * arg)
2450 {
2451         mdu_array_info_t info;
2452         int nr,working,active,failed,spare;
2453         mdk_rdev_t *rdev;
2454         struct list_head *tmp;
2455
2456         nr=working=active=failed=spare=0;
2457         ITERATE_RDEV(mddev,rdev,tmp) {
2458                 nr++;
2459                 if (test_bit(Faulty, &rdev->flags))
2460                         failed++;
2461                 else {
2462                         working++;
2463                         if (test_bit(In_sync, &rdev->flags))
2464                                 active++;       
2465                         else
2466                                 spare++;
2467                 }
2468         }
2469
2470         info.major_version = mddev->major_version;
2471         info.minor_version = mddev->minor_version;
2472         info.patch_version = MD_PATCHLEVEL_VERSION;
2473         info.ctime         = mddev->ctime;
2474         info.level         = mddev->level;
2475         info.size          = mddev->size;
2476         info.nr_disks      = nr;
2477         info.raid_disks    = mddev->raid_disks;
2478         info.md_minor      = mddev->md_minor;
2479         info.not_persistent= !mddev->persistent;
2480
2481         info.utime         = mddev->utime;
2482         info.state         = 0;
2483         if (mddev->in_sync)
2484                 info.state = (1<<MD_SB_CLEAN);
2485         if (mddev->bitmap && mddev->bitmap_offset)
2486                 info.state = (1<<MD_SB_BITMAP_PRESENT);
2487         info.active_disks  = active;
2488         info.working_disks = working;
2489         info.failed_disks  = failed;
2490         info.spare_disks   = spare;
2491
2492         info.layout        = mddev->layout;
2493         info.chunk_size    = mddev->chunk_size;
2494
2495         if (copy_to_user(arg, &info, sizeof(info)))
2496                 return -EFAULT;
2497
2498         return 0;
2499 }
2500
2501 static int get_bitmap_file(mddev_t * mddev, void __user * arg)
2502 {
2503         mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
2504         char *ptr, *buf = NULL;
2505         int err = -ENOMEM;
2506
2507         file = kmalloc(sizeof(*file), GFP_KERNEL);
2508         if (!file)
2509                 goto out;
2510
2511         /* bitmap disabled, zero the first byte and copy out */
2512         if (!mddev->bitmap || !mddev->bitmap->file) {
2513                 file->pathname[0] = '\0';
2514                 goto copy_out;
2515         }
2516
2517         buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
2518         if (!buf)
2519                 goto out;
2520
2521         ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
2522         if (!ptr)
2523                 goto out;
2524
2525         strcpy(file->pathname, ptr);
2526
2527 copy_out:
2528         err = 0;
2529         if (copy_to_user(arg, file, sizeof(*file)))
2530                 err = -EFAULT;
2531 out:
2532         kfree(buf);
2533         kfree(file);
2534         return err;
2535 }
2536
2537 static int get_disk_info(mddev_t * mddev, void __user * arg)
2538 {
2539         mdu_disk_info_t info;
2540         unsigned int nr;
2541         mdk_rdev_t *rdev;
2542
2543         if (copy_from_user(&info, arg, sizeof(info)))
2544                 return -EFAULT;
2545
2546         nr = info.number;
2547
2548         rdev = find_rdev_nr(mddev, nr);
2549         if (rdev) {
2550                 info.major = MAJOR(rdev->bdev->bd_dev);
2551                 info.minor = MINOR(rdev->bdev->bd_dev);
2552                 info.raid_disk = rdev->raid_disk;
2553                 info.state = 0;
2554                 if (test_bit(Faulty, &rdev->flags))
2555                         info.state |= (1<<MD_DISK_FAULTY);
2556                 else if (test_bit(In_sync, &rdev->flags)) {
2557                         info.state |= (1<<MD_DISK_ACTIVE);
2558                         info.state |= (1<<MD_DISK_SYNC);
2559                 }
2560                 if (test_bit(WriteMostly, &rdev->flags))
2561                         info.state |= (1<<MD_DISK_WRITEMOSTLY);
2562         } else {
2563                 info.major = info.minor = 0;
2564                 info.raid_disk = -1;
2565                 info.state = (1<<MD_DISK_REMOVED);
2566         }
2567
2568         if (copy_to_user(arg, &info, sizeof(info)))
2569                 return -EFAULT;
2570
2571         return 0;
2572 }
2573
2574 static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
2575 {
2576         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
2577         mdk_rdev_t *rdev;
2578         dev_t dev = MKDEV(info->major,info->minor);
2579
2580         if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
2581                 return -EOVERFLOW;
2582
2583         if (!mddev->raid_disks) {
2584                 int err;
2585                 /* expecting a device which has a superblock */
2586                 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
2587                 if (IS_ERR(rdev)) {
2588                         printk(KERN_WARNING 
2589                                 "md: md_import_device returned %ld\n",
2590                                 PTR_ERR(rdev));
2591                         return PTR_ERR(rdev);
2592                 }
2593                 if (!list_empty(&mddev->disks)) {
2594                         mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2595                                                         mdk_rdev_t, same_set);
2596                         int err = super_types[mddev->major_version]
2597                                 .load_super(rdev, rdev0, mddev->minor_version);
2598                         if (err < 0) {
2599                                 printk(KERN_WARNING 
2600                                         "md: %s has different UUID to %s\n",
2601                                         bdevname(rdev->bdev,b), 
2602                                         bdevname(rdev0->bdev,b2));
2603                                 export_rdev(rdev);
2604                                 return -EINVAL;
2605                         }
2606                 }
2607                 err = bind_rdev_to_array(rdev, mddev);
2608                 if (err)
2609                         export_rdev(rdev);
2610                 return err;
2611         }
2612
2613         /*
2614          * add_new_disk can be used once the array is assembled
2615          * to add "hot spares".  They must already have a superblock
2616          * written
2617          */
2618         if (mddev->pers) {
2619                 int err;
2620                 if (!mddev->pers->hot_add_disk) {
2621                         printk(KERN_WARNING 
2622                                 "%s: personality does not support diskops!\n",
2623                                mdname(mddev));
2624                         return -EINVAL;
2625                 }
2626                 if (mddev->persistent)
2627                         rdev = md_import_device(dev, mddev->major_version,
2628                                                 mddev->minor_version);
2629                 else
2630                         rdev = md_import_device(dev, -1, -1);
2631                 if (IS_ERR(rdev)) {
2632                         printk(KERN_WARNING 
2633                                 "md: md_import_device returned %ld\n",
2634                                 PTR_ERR(rdev));
2635                         return PTR_ERR(rdev);
2636                 }
2637                 /* set save_raid_disk if appropriate */
2638                 if (!mddev->persistent) {
2639                         if (info->state & (1<<MD_DISK_SYNC)  &&
2640                             info->raid_disk < mddev->raid_disks)
2641                                 rdev->raid_disk = info->raid_disk;
2642                         else
2643                                 rdev->raid_disk = -1;
2644                 } else
2645                         super_types[mddev->major_version].
2646                                 validate_super(mddev, rdev);
2647                 rdev->saved_raid_disk = rdev->raid_disk;
2648
2649                 clear_bit(In_sync, &rdev->flags); /* just to be sure */
2650                 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2651                         set_bit(WriteMostly, &rdev->flags);
2652
2653                 rdev->raid_disk = -1;
2654                 err = bind_rdev_to_array(rdev, mddev);
2655                 if (err)
2656                         export_rdev(rdev);
2657
2658                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2659                 md_wakeup_thread(mddev->thread);
2660                 return err;
2661         }
2662
2663         /* otherwise, add_new_disk is only allowed
2664          * for major_version==0 superblocks
2665          */
2666         if (mddev->major_version != 0) {
2667                 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
2668                        mdname(mddev));
2669                 return -EINVAL;
2670         }
2671
2672         if (!(info->state & (1<<MD_DISK_FAULTY))) {
2673                 int err;
2674                 rdev = md_import_device (dev, -1, 0);
2675                 if (IS_ERR(rdev)) {
2676                         printk(KERN_WARNING 
2677                                 "md: error, md_import_device() returned %ld\n",
2678                                 PTR_ERR(rdev));
2679                         return PTR_ERR(rdev);
2680                 }
2681                 rdev->desc_nr = info->number;
2682                 if (info->raid_disk < mddev->raid_disks)
2683                         rdev->raid_disk = info->raid_disk;
2684                 else
2685                         rdev->raid_disk = -1;
2686
2687                 rdev->flags = 0;
2688
2689                 if (rdev->raid_disk < mddev->raid_disks)
2690                         if (info->state & (1<<MD_DISK_SYNC))
2691                                 set_bit(In_sync, &rdev->flags);
2692
2693                 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
2694                         set_bit(WriteMostly, &rdev->flags);
2695
2696                 err = bind_rdev_to_array(rdev, mddev);
2697                 if (err) {
2698                         export_rdev(rdev);
2699                         return err;
2700                 }
2701
2702                 if (!mddev->persistent) {
2703                         printk(KERN_INFO "md: nonpersistent superblock ...\n");
2704                         rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2705                 } else 
2706                         rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2707                 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
2708
2709                 if (!mddev->size || (mddev->size > rdev->size))
2710                         mddev->size = rdev->size;
2711         }
2712
2713         return 0;
2714 }
2715
2716 static int hot_remove_disk(mddev_t * mddev, dev_t dev)
2717 {
2718         char b[BDEVNAME_SIZE];
2719         mdk_rdev_t *rdev;
2720
2721         if (!mddev->pers)
2722                 return -ENODEV;
2723
2724         rdev = find_rdev(mddev, dev);
2725         if (!rdev)
2726                 return -ENXIO;
2727
2728         if (rdev->raid_disk >= 0)
2729                 goto busy;
2730
2731         kick_rdev_from_array(rdev);
2732         md_update_sb(mddev);
2733         md_new_event(mddev);
2734
2735         return 0;
2736 busy:
2737         printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
2738                 bdevname(rdev->bdev,b), mdname(mddev));
2739         return -EBUSY;
2740 }
2741
2742 static int hot_add_disk(mddev_t * mddev, dev_t dev)
2743 {
2744         char b[BDEVNAME_SIZE];
2745         int err;
2746         unsigned int size;
2747         mdk_rdev_t *rdev;
2748
2749         if (!mddev->pers)
2750                 return -ENODEV;
2751
2752         if (mddev->major_version != 0) {
2753                 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
2754                         " version-0 superblocks.\n",
2755                         mdname(mddev));
2756                 return -EINVAL;
2757         }
2758         if (!mddev->pers->hot_add_disk) {
2759                 printk(KERN_WARNING 
2760                         "%s: personality does not support diskops!\n",
2761                         mdname(mddev));
2762                 return -EINVAL;
2763         }
2764
2765         rdev = md_import_device (dev, -1, 0);
2766         if (IS_ERR(rdev)) {
2767                 printk(KERN_WARNING 
2768                         "md: error, md_import_device() returned %ld\n",
2769                         PTR_ERR(rdev));
2770                 return -EINVAL;
2771         }
2772
2773         if (mddev->persistent)
2774                 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
2775         else
2776                 rdev->sb_offset =
2777                         rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2778
2779         size = calc_dev_size(rdev, mddev->chunk_size);
2780         rdev->size = size;
2781
2782         if (size < mddev->size) {
2783                 printk(KERN_WARNING 
2784                         "%s: disk size %llu blocks < array size %llu\n",
2785                         mdname(mddev), (unsigned long long)size,
2786                         (unsigned long long)mddev->size);
2787                 err = -ENOSPC;
2788                 goto abort_export;
2789         }
2790
2791         if (test_bit(Faulty, &rdev->flags)) {
2792                 printk(KERN_WARNING 
2793                         "md: can not hot-add faulty %s disk to %s!\n",
2794                         bdevname(rdev->bdev,b), mdname(mddev));
2795                 err = -EINVAL;
2796                 goto abort_export;
2797         }
2798         clear_bit(In_sync, &rdev->flags);
2799         rdev->desc_nr = -1;
2800         bind_rdev_to_array(rdev, mddev);
2801
2802         /*
2803          * The rest should better be atomic, we can have disk failures
2804          * noticed in interrupt contexts ...
2805          */
2806
2807         if (rdev->desc_nr == mddev->max_disks) {
2808                 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
2809                         mdname(mddev));
2810                 err = -EBUSY;
2811                 goto abort_unbind_export;
2812         }
2813
2814         rdev->raid_disk = -1;
2815
2816         md_update_sb(mddev);
2817
2818         /*
2819          * Kick recovery, maybe this spare has to be added to the
2820          * array immediately.
2821          */
2822         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2823         md_wakeup_thread(mddev->thread);
2824         md_new_event(mddev);
2825         return 0;
2826
2827 abort_unbind_export:
2828         unbind_rdev_from_array(rdev);
2829
2830 abort_export:
2831         export_rdev(rdev);
2832         return err;
2833 }
2834
2835 /* similar to deny_write_access, but accounts for our holding a reference
2836  * to the file ourselves */
2837 static int deny_bitmap_write_access(struct file * file)
2838 {
2839         struct inode *inode = file->f_mapping->host;
2840
2841         spin_lock(&inode->i_lock);
2842         if (atomic_read(&inode->i_writecount) > 1) {
2843                 spin_unlock(&inode->i_lock);
2844                 return -ETXTBSY;
2845         }
2846         atomic_set(&inode->i_writecount, -1);
2847         spin_unlock(&inode->i_lock);
2848
2849         return 0;
2850 }
2851
2852 static int set_bitmap_file(mddev_t *mddev, int fd)
2853 {
2854         int err;
2855
2856         if (mddev->pers) {
2857                 if (!mddev->pers->quiesce)
2858                         return -EBUSY;
2859                 if (mddev->recovery || mddev->sync_thread)
2860                         return -EBUSY;
2861                 /* we should be able to change the bitmap.. */
2862         }
2863
2864
2865         if (fd >= 0) {
2866                 if (mddev->bitmap)
2867                         return -EEXIST; /* cannot add when bitmap is present */
2868                 mddev->bitmap_file = fget(fd);
2869
2870                 if (mddev->bitmap_file == NULL) {
2871                         printk(KERN_ERR "%s: error: failed to get bitmap file\n",
2872                                mdname(mddev));
2873                         return -EBADF;
2874                 }
2875
2876                 err = deny_bitmap_write_access(mddev->bitmap_file);
2877                 if (err) {
2878                         printk(KERN_ERR "%s: error: bitmap file is already in use\n",
2879                                mdname(mddev));
2880                         fput(mddev->bitmap_file);
2881                         mddev->bitmap_file = NULL;
2882                         return err;
2883                 }
2884                 mddev->bitmap_offset = 0; /* file overrides offset */
2885         } else if (mddev->bitmap == NULL)
2886                 return -ENOENT; /* cannot remove what isn't there */
2887         err = 0;
2888         if (mddev->pers) {
2889                 mddev->pers->quiesce(mddev, 1);
2890                 if (fd >= 0)
2891                         err = bitmap_create(mddev);
2892                 if (fd < 0 || err)
2893                         bitmap_destroy(mddev);
2894                 mddev->pers->quiesce(mddev, 0);
2895         } else if (fd < 0) {
2896                 if (mddev->bitmap_file)
2897                         fput(mddev->bitmap_file);
2898                 mddev->bitmap_file = NULL;
2899         }
2900
2901         return err;
2902 }
2903
2904 /*
2905  * set_array_info is used two different ways
2906  * The original usage is when creating a new array.
2907  * In this usage, raid_disks is > 0 and it together with
2908  *  level, size, not_persistent,layout,chunksize determine the
2909  *  shape of the array.
2910  *  This will always create an array with a type-0.90.0 superblock.
2911  * The newer usage is when assembling an array.
2912  *  In this case raid_disks will be 0, and the major_version field is
2913  *  use to determine which style super-blocks are to be found on the devices.
2914  *  The minor and patch _version numbers are also kept incase the
2915  *  super_block handler wishes to interpret them.
2916  */
2917 static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
2918 {
2919
2920         if (info->raid_disks == 0) {
2921                 /* just setting version number for superblock loading */
2922                 if (info->major_version < 0 ||
2923                     info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
2924                     super_types[info->major_version].name == NULL) {
2925                         /* maybe try to auto-load a module? */
2926                         printk(KERN_INFO 
2927                                 "md: superblock version %d not known\n",
2928                                 info->major_version);
2929                         return -EINVAL;
2930                 }
2931                 mddev->major_version = info->major_version;
2932                 mddev->minor_version = info->minor_version;
2933                 mddev->patch_version = info->patch_version;
2934                 return 0;
2935         }
2936         mddev->major_version = MD_MAJOR_VERSION;
2937         mddev->minor_version = MD_MINOR_VERSION;
2938         mddev->patch_version = MD_PATCHLEVEL_VERSION;
2939         mddev->ctime         = get_seconds();
2940
2941         mddev->level         = info->level;
2942         mddev->size          = info->size;
2943         mddev->raid_disks    = info->raid_disks;
2944         /* don't set md_minor, it is determined by which /dev/md* was
2945          * openned
2946          */
2947         if (info->state & (1<<MD_SB_CLEAN))
2948                 mddev->recovery_cp = MaxSector;
2949         else
2950                 mddev->recovery_cp = 0;
2951         mddev->persistent    = ! info->not_persistent;
2952
2953         mddev->layout        = info->layout;
2954         mddev->chunk_size    = info->chunk_size;
2955
2956         mddev->max_disks     = MD_SB_DISKS;
2957
2958         mddev->sb_dirty      = 1;
2959
2960         mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
2961         mddev->bitmap_offset = 0;
2962
2963         /*
2964          * Generate a 128 bit UUID
2965          */
2966         get_random_bytes(mddev->uuid, 16);
2967
2968         return 0;
2969 }
2970
2971 /*
2972  * update_array_info is used to change the configuration of an
2973  * on-line array.
2974  * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
2975  * fields in the info are checked against the array.
2976  * Any differences that cannot be handled will cause an error.
2977  * Normally, only one change can be managed at a time.
2978  */
2979 static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
2980 {
2981         int rv = 0;
2982         int cnt = 0;
2983         int state = 0;
2984
2985         /* calculate expected state,ignoring low bits */
2986         if (mddev->bitmap && mddev->bitmap_offset)
2987                 state |= (1 << MD_SB_BITMAP_PRESENT);
2988
2989         if (mddev->major_version != info->major_version ||
2990             mddev->minor_version != info->minor_version ||
2991 /*          mddev->patch_version != info->patch_version || */
2992             mddev->ctime         != info->ctime         ||
2993             mddev->level         != info->level         ||
2994 /*          mddev->layout        != info->layout        || */
2995             !mddev->persistent   != info->not_persistent||
2996             mddev->chunk_size    != info->chunk_size    ||
2997             /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
2998             ((state^info->state) & 0xfffffe00)
2999                 )
3000                 return -EINVAL;
3001         /* Check there is only one change */
3002         if (mddev->size != info->size) cnt++;
3003         if (mddev->raid_disks != info->raid_disks) cnt++;
3004         if (mddev->layout != info->layout) cnt++;
3005         if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
3006         if (cnt == 0) return 0;
3007         if (cnt > 1) return -EINVAL;
3008
3009         if (mddev->layout != info->layout) {
3010                 /* Change layout
3011                  * we don't need to do anything at the md level, the
3012                  * personality will take care of it all.
3013                  */
3014                 if (mddev->pers->reconfig == NULL)
3015                         return -EINVAL;
3016                 else
3017                         return mddev->pers->reconfig(mddev, info->layout, -1);
3018         }
3019         if (mddev->size != info->size) {
3020                 mdk_rdev_t * rdev;
3021                 struct list_head *tmp;
3022                 if (mddev->pers->resize == NULL)
3023                         return -EINVAL;
3024                 /* The "size" is the amount of each device that is used.
3025                  * This can only make sense for arrays with redundancy.
3026                  * linear and raid0 always use whatever space is available
3027                  * We can only consider changing the size if no resync
3028                  * or reconstruction is happening, and if the new size
3029                  * is acceptable. It must fit before the sb_offset or,
3030                  * if that is <data_offset, it must fit before the
3031                  * size of each device.
3032                  * If size is zero, we find the largest size that fits.
3033                  */
3034                 if (mddev->sync_thread)
3035                         return -EBUSY;
3036                 ITERATE_RDEV(mddev,rdev,tmp) {
3037                         sector_t avail;
3038                         int fit = (info->size == 0);
3039                         if (rdev->sb_offset > rdev->data_offset)
3040                                 avail = (rdev->sb_offset*2) - rdev->data_offset;
3041                         else
3042                                 avail = get_capacity(rdev->bdev->bd_disk)
3043                                         - rdev->data_offset;
3044                         if (fit && (info->size == 0 || info->size > avail/2))
3045                                 info->size = avail/2;
3046                         if (avail < ((sector_t)info->size << 1))
3047                                 return -ENOSPC;
3048                 }
3049                 rv = mddev->pers->resize(mddev, (sector_t)info->size *2);
3050                 if (!rv) {
3051                         struct block_device *bdev;
3052
3053                         bdev = bdget_disk(mddev->gendisk, 0);
3054                         if (bdev) {
3055                                 down(&bdev->bd_inode->i_sem);
3056                                 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3057                                 up(&bdev->bd_inode->i_sem);
3058                                 bdput(bdev);
3059                         }
3060                 }
3061         }
3062         if (mddev->raid_disks    != info->raid_disks) {
3063                 /* change the number of raid disks */
3064                 if (mddev->pers->reshape == NULL)
3065                         return -EINVAL;
3066                 if (info->raid_disks <= 0 ||
3067                     info->raid_disks >= mddev->max_disks)
3068                         return -EINVAL;
3069                 if (mddev->sync_thread)
3070                         return -EBUSY;
3071                 rv = mddev->pers->reshape(mddev, info->raid_disks);
3072                 if (!rv) {
3073                         struct block_device *bdev;
3074
3075                         bdev = bdget_disk(mddev->gendisk, 0);
3076                         if (bdev) {
3077                                 down(&bdev->bd_inode->i_sem);
3078                                 i_size_write(bdev->bd_inode, mddev->array_size << 10);
3079                                 up(&bdev->bd_inode->i_sem);
3080                                 bdput(bdev);
3081                         }
3082                 }
3083         }
3084         if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3085                 if (mddev->pers->quiesce == NULL)
3086                         return -EINVAL;
3087                 if (mddev->recovery || mddev->sync_thread)
3088                         return -EBUSY;
3089                 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3090                         /* add the bitmap */
3091                         if (mddev->bitmap)
3092                                 return -EEXIST;
3093                         if (mddev->default_bitmap_offset == 0)
3094                                 return -EINVAL;
3095                         mddev->bitmap_offset = mddev->default_bitmap_offset;
3096                         mddev->pers->quiesce(mddev, 1);
3097                         rv = bitmap_create(mddev);
3098                         if (rv)
3099                                 bitmap_destroy(mddev);
3100                         mddev->pers->quiesce(mddev, 0);
3101                 } else {
3102                         /* remove the bitmap */
3103                         if (!mddev->bitmap)
3104                                 return -ENOENT;
3105                         if (mddev->bitmap->file)
3106                                 return -EINVAL;
3107                         mddev->pers->quiesce(mddev, 1);
3108                         bitmap_destroy(mddev);
3109                         mddev->pers->quiesce(mddev, 0);
3110                         mddev->bitmap_offset = 0;
3111                 }
3112         }
3113         md_update_sb(mddev);
3114         return rv;
3115 }
3116
3117 static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3118 {
3119         mdk_rdev_t *rdev;
3120
3121         if (mddev->pers == NULL)
3122                 return -ENODEV;
3123
3124         rdev = find_rdev(mddev, dev);
3125         if (!rdev)
3126                 return -ENODEV;
3127
3128         md_error(mddev, rdev);
3129         return 0;
3130 }
3131
3132 static int md_ioctl(struct inode *inode, struct file *file,
3133                         unsigned int cmd, unsigned long arg)
3134 {
3135         int err = 0;
3136         void __user *argp = (void __user *)arg;
3137         struct hd_geometry __user *loc = argp;
3138         mddev_t *mddev = NULL;
3139
3140         if (!capable(CAP_SYS_ADMIN))
3141                 return -EACCES;
3142
3143         /*
3144          * Commands dealing with the RAID driver but not any
3145          * particular array:
3146          */
3147         switch (cmd)
3148         {
3149                 case RAID_VERSION:
3150                         err = get_version(argp);
3151                         goto done;
3152
3153                 case PRINT_RAID_DEBUG:
3154                         err = 0;
3155                         md_print_devices();
3156                         goto done;
3157
3158 #ifndef MODULE
3159                 case RAID_AUTORUN:
3160                         err = 0;
3161                         autostart_arrays(arg);
3162                         goto done;
3163 #endif
3164                 default:;
3165         }
3166
3167         /*
3168          * Commands creating/starting a new array:
3169          */
3170
3171         mddev = inode->i_bdev->bd_disk->private_data;
3172
3173         if (!mddev) {
3174                 BUG();
3175                 goto abort;
3176         }
3177
3178
3179         if (cmd == START_ARRAY) {
3180                 /* START_ARRAY doesn't need to lock the array as autostart_array
3181                  * does the locking, and it could even be a different array
3182                  */
3183                 static int cnt = 3;
3184                 if (cnt > 0 ) {
3185                         printk(KERN_WARNING
3186                                "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
3187                                "This will not be supported beyond July 2006\n",
3188                                current->comm, current->pid);
3189                         cnt--;
3190                 }
3191                 err = autostart_array(new_decode_dev(arg));
3192                 if (err) {
3193                         printk(KERN_WARNING "md: autostart failed!\n");
3194                         goto abort;
3195                 }
3196                 goto done;
3197         }
3198
3199         err = mddev_lock(mddev);
3200         if (err) {
3201                 printk(KERN_INFO 
3202                         "md: ioctl lock interrupted, reason %d, cmd %d\n",
3203                         err, cmd);
3204                 goto abort;
3205         }
3206
3207         switch (cmd)
3208         {
3209                 case SET_ARRAY_INFO:
3210                         {
3211                                 mdu_array_info_t info;
3212                                 if (!arg)
3213                                         memset(&info, 0, sizeof(info));
3214                                 else if (copy_from_user(&info, argp, sizeof(info))) {
3215                                         err = -EFAULT;
3216                                         goto abort_unlock;
3217                                 }
3218                                 if (mddev->pers) {
3219                                         err = update_array_info(mddev, &info);
3220                                         if (err) {
3221                                                 printk(KERN_WARNING "md: couldn't update"
3222                                                        " array info. %d\n", err);
3223                                                 goto abort_unlock;
3224                                         }
3225                                         goto done_unlock;
3226                                 }
3227                                 if (!list_empty(&mddev->disks)) {
3228                                         printk(KERN_WARNING
3229                                                "md: array %s already has disks!\n",
3230                                                mdname(mddev));
3231                                         err = -EBUSY;
3232                                         goto abort_unlock;
3233                                 }
3234                                 if (mddev->raid_disks) {
3235                                         printk(KERN_WARNING
3236                                                "md: array %s already initialised!\n",
3237                                                mdname(mddev));
3238                                         err = -EBUSY;
3239                                         goto abort_unlock;
3240                                 }
3241                                 err = set_array_info(mddev, &info);
3242                                 if (err) {
3243                                         printk(KERN_WARNING "md: couldn't set"
3244                                                " array info. %d\n", err);
3245                                         goto abort_unlock;
3246                                 }
3247                         }
3248                         goto done_unlock;
3249
3250                 default:;
3251         }
3252
3253         /*
3254          * Commands querying/configuring an existing array:
3255          */
3256         /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3257          * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3258         if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3259                         && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
3260                 err = -ENODEV;
3261                 goto abort_unlock;
3262         }
3263
3264         /*
3265          * Commands even a read-only array can execute:
3266          */
3267         switch (cmd)
3268         {
3269                 case GET_ARRAY_INFO:
3270                         err = get_array_info(mddev, argp);
3271                         goto done_unlock;
3272
3273                 case GET_BITMAP_FILE:
3274                         err = get_bitmap_file(mddev, argp);
3275                         goto done_unlock;
3276
3277                 case GET_DISK_INFO:
3278                         err = get_disk_info(mddev, argp);
3279                         goto done_unlock;
3280
3281                 case RESTART_ARRAY_RW:
3282                         err = restart_array(mddev);
3283                         goto done_unlock;
3284
3285                 case STOP_ARRAY:
3286                         err = do_md_stop (mddev, 0);
3287                         goto done_unlock;
3288
3289                 case STOP_ARRAY_RO:
3290                         err = do_md_stop (mddev, 1);
3291                         goto done_unlock;
3292
3293         /*
3294          * We have a problem here : there is no easy way to give a CHS
3295          * virtual geometry. We currently pretend that we have a 2 heads
3296          * 4 sectors (with a BIG number of cylinders...). This drives
3297          * dosfs just mad... ;-)
3298          */
3299                 case HDIO_GETGEO:
3300                         if (!loc) {
3301                                 err = -EINVAL;
3302                                 goto abort_unlock;
3303                         }
3304                         err = put_user (2, (char __user *) &loc->heads);
3305                         if (err)
3306                                 goto abort_unlock;
3307                         err = put_user (4, (char __user *) &loc->sectors);
3308                         if (err)
3309                                 goto abort_unlock;
3310                         err = put_user(get_capacity(mddev->gendisk)/8,
3311                                         (short __user *) &loc->cylinders);
3312                         if (err)
3313                                 goto abort_unlock;
3314                         err = put_user (get_start_sect(inode->i_bdev),
3315                                                 (long __user *) &loc->start);
3316                         goto done_unlock;
3317         }
3318
3319         /*
3320          * The remaining ioctls are changing the state of the
3321          * superblock, so we do not allow them on read-only arrays.
3322          * However non-MD ioctls (e.g. get-size) will still come through
3323          * here and hit the 'default' below, so only disallow
3324          * 'md' ioctls, and switch to rw mode if started auto-readonly.
3325          */
3326         if (_IOC_TYPE(cmd) == MD_MAJOR &&
3327             mddev->ro && mddev->pers) {
3328                 if (mddev->ro == 2) {
3329                         mddev->ro = 0;
3330                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3331                 md_wakeup_thread(mddev->thread);
3332
3333                 } else {
3334                         err = -EROFS;
3335                         goto abort_unlock;
3336                 }
3337         }
3338
3339         switch (cmd)
3340         {
3341                 case ADD_NEW_DISK:
3342                 {
3343                         mdu_disk_info_t info;
3344                         if (copy_from_user(&info, argp, sizeof(info)))
3345                                 err = -EFAULT;
3346                         else
3347                                 err = add_new_disk(mddev, &info);
3348                         goto done_unlock;
3349                 }
3350
3351                 case HOT_REMOVE_DISK:
3352                         err = hot_remove_disk(mddev, new_decode_dev(arg));
3353                         goto done_unlock;
3354
3355                 case HOT_ADD_DISK:
3356                         err = hot_add_disk(mddev, new_decode_dev(arg));
3357                         goto done_unlock;
3358
3359                 case SET_DISK_FAULTY:
3360                         err = set_disk_faulty(mddev, new_decode_dev(arg));
3361                         goto done_unlock;
3362
3363                 case RUN_ARRAY:
3364                         err = do_md_run (mddev);
3365                         goto done_unlock;
3366
3367                 case SET_BITMAP_FILE:
3368                         err = set_bitmap_file(mddev, (int)arg);
3369                         goto done_unlock;
3370
3371                 default:
3372                         if (_IOC_TYPE(cmd) == MD_MAJOR)
3373                                 printk(KERN_WARNING "md: %s(pid %d) used"
3374                                         " obsolete MD ioctl, upgrade your"
3375                                         " software to use new ictls.\n",
3376                                         current->comm, current->pid);
3377                         err = -EINVAL;
3378                         goto abort_unlock;
3379         }
3380
3381 done_unlock:
3382 abort_unlock:
3383         mddev_unlock(mddev);
3384
3385         return err;
3386 done:
3387         if (err)
3388                 MD_BUG();
3389 abort:
3390         return err;
3391 }
3392
3393 static int md_open(struct inode *inode, struct file *file)
3394 {
3395         /*
3396          * Succeed if we can lock the mddev, which confirms that
3397          * it isn't being stopped right now.
3398          */
3399         mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3400         int err;
3401
3402         if ((err = mddev_lock(mddev)))
3403                 goto out;
3404
3405         err = 0;
3406         mddev_get(mddev);
3407         mddev_unlock(mddev);
3408
3409         check_disk_change(inode->i_bdev);
3410  out:
3411         return err;
3412 }
3413
3414 static int md_release(struct inode *inode, struct file * file)
3415 {
3416         mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
3417
3418         if (!mddev)
3419                 BUG();
3420         mddev_put(mddev);
3421
3422         return 0;
3423 }
3424
3425 static int md_media_changed(struct gendisk *disk)
3426 {
3427         mddev_t *mddev = disk->private_data;
3428
3429         return mddev->changed;
3430 }
3431
3432 static int md_revalidate(struct gendisk *disk)
3433 {
3434         mddev_t *mddev = disk->private_data;
3435
3436         mddev->changed = 0;
3437         return 0;
3438 }
3439 static struct block_device_operations md_fops =
3440 {
3441         .owner          = THIS_MODULE,
3442         .open           = md_open,
3443         .release        = md_release,
3444         .ioctl          = md_ioctl,
3445         .media_changed  = md_media_changed,
3446         .revalidate_disk= md_revalidate,
3447 };
3448
3449 static int md_thread(void * arg)
3450 {
3451         mdk_thread_t *thread = arg;
3452
3453         /*
3454          * md_thread is a 'system-thread', it's priority should be very
3455          * high. We avoid resource deadlocks individually in each
3456          * raid personality. (RAID5 does preallocation) We also use RR and
3457          * the very same RT priority as kswapd, thus we will never get
3458          * into a priority inversion deadlock.
3459          *
3460          * we definitely have to have equal or higher priority than
3461          * bdflush, otherwise bdflush will deadlock if there are too
3462          * many dirty RAID5 blocks.
3463          */
3464
3465         allow_signal(SIGKILL);
3466         while (!kthread_should_stop()) {
3467
3468                 /* We need to wait INTERRUPTIBLE so that
3469                  * we don't add to the load-average.
3470                  * That means we need to be sure no signals are
3471                  * pending
3472                  */
3473                 if (signal_pending(current))
3474                         flush_signals(current);
3475
3476                 wait_event_interruptible_timeout
3477                         (thread->wqueue,
3478                          test_bit(THREAD_WAKEUP, &thread->flags)
3479                          || kthread_should_stop(),
3480                          thread->timeout);
3481                 try_to_freeze();
3482
3483                 clear_bit(THREAD_WAKEUP, &thread->flags);
3484
3485                 thread->run(thread->mddev);
3486         }
3487
3488         return 0;
3489 }
3490
3491 void md_wakeup_thread(mdk_thread_t *thread)
3492 {
3493         if (thread) {
3494                 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
3495                 set_bit(THREAD_WAKEUP, &thread->flags);
3496                 wake_up(&thread->wqueue);
3497         }
3498 }
3499
3500 mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
3501                                  const char *name)
3502 {
3503         mdk_thread_t *thread;
3504
3505         thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
3506         if (!thread)
3507                 return NULL;
3508
3509         init_waitqueue_head(&thread->wqueue);
3510
3511         thread->run = run;
3512         thread->mddev = mddev;
3513         thread->timeout = MAX_SCHEDULE_TIMEOUT;
3514         thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
3515         if (IS_ERR(thread->tsk)) {
3516                 kfree(thread);
3517                 return NULL;
3518         }
3519         return thread;
3520 }
3521
3522 void md_unregister_thread(mdk_thread_t *thread)
3523 {
3524         dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
3525
3526         kthread_stop(thread->tsk);
3527         kfree(thread);
3528 }
3529
3530 void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
3531 {
3532         if (!mddev) {
3533                 MD_BUG();
3534                 return;
3535         }
3536
3537         if (!rdev || test_bit(Faulty, &rdev->flags))
3538                 return;
3539 /*
3540         dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
3541                 mdname(mddev),
3542                 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
3543                 __builtin_return_address(0),__builtin_return_address(1),
3544                 __builtin_return_address(2),__builtin_return_address(3));
3545 */
3546         if (!mddev->pers->error_handler)
3547                 return;
3548         mddev->pers->error_handler(mddev,rdev);
3549         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3550         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3551         md_wakeup_thread(mddev->thread);
3552         md_new_event(mddev);
3553 }
3554
3555 /* seq_file implementation /proc/mdstat */
3556
3557 static void status_unused(struct seq_file *seq)
3558 {
3559         int i = 0;
3560         mdk_rdev_t *rdev;
3561         struct list_head *tmp;
3562
3563         seq_printf(seq, "unused devices: ");
3564
3565         ITERATE_RDEV_PENDING(rdev,tmp) {
3566                 char b[BDEVNAME_SIZE];
3567                 i++;
3568                 seq_printf(seq, "%s ",
3569                               bdevname(rdev->bdev,b));
3570         }
3571         if (!i)
3572                 seq_printf(seq, "<none>");
3573
3574         seq_printf(seq, "\n");
3575 }
3576
3577
3578 static void status_resync(struct seq_file *seq, mddev_t * mddev)
3579 {
3580         unsigned long max_blocks, resync, res, dt, db, rt;
3581
3582         resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
3583
3584         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3585                 max_blocks = mddev->resync_max_sectors >> 1;
3586         else
3587                 max_blocks = mddev->size;
3588
3589         /*
3590          * Should not happen.
3591          */
3592         if (!max_blocks) {
3593                 MD_BUG();
3594                 return;
3595         }
3596         res = (resync/1024)*1000/(max_blocks/1024 + 1);
3597         {
3598                 int i, x = res/50, y = 20-x;
3599                 seq_printf(seq, "[");
3600                 for (i = 0; i < x; i++)
3601                         seq_printf(seq, "=");
3602                 seq_printf(seq, ">");
3603                 for (i = 0; i < y; i++)
3604                         seq_printf(seq, ".");
3605                 seq_printf(seq, "] ");
3606         }
3607         seq_printf(seq, " %s =%3lu.%lu%% (%lu/%lu)",
3608                       (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
3609                        "resync" : "recovery"),
3610                       res/10, res % 10, resync, max_blocks);
3611
3612         /*
3613          * We do not want to overflow, so the order of operands and
3614          * the * 100 / 100 trick are important. We do a +1 to be
3615          * safe against division by zero. We only estimate anyway.
3616          *
3617          * dt: time from mark until now
3618          * db: blocks written from mark until now
3619          * rt: remaining time
3620          */
3621         dt = ((jiffies - mddev->resync_mark) / HZ);
3622         if (!dt) dt++;
3623         db = resync - (mddev->resync_mark_cnt/2);
3624         rt = (dt * ((max_blocks-resync) / (db/100+1)))/100;
3625
3626         seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
3627
3628         seq_printf(seq, " speed=%ldK/sec", db/dt);
3629 }
3630
3631 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
3632 {
3633         struct list_head *tmp;
3634         loff_t l = *pos;
3635         mddev_t *mddev;
3636
3637         if (l >= 0x10000)
3638                 return NULL;
3639         if (!l--)
3640                 /* header */
3641                 return (void*)1;
3642
3643         spin_lock(&all_mddevs_lock);
3644         list_for_each(tmp,&all_mddevs)
3645                 if (!l--) {
3646                         mddev = list_entry(tmp, mddev_t, all_mddevs);
3647                         mddev_get(mddev);
3648                         spin_unlock(&all_mddevs_lock);
3649                         return mddev;
3650                 }
3651         spin_unlock(&all_mddevs_lock);
3652         if (!l--)
3653                 return (void*)2;/* tail */
3654         return NULL;
3655 }
3656
3657 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3658 {
3659         struct list_head *tmp;
3660         mddev_t *next_mddev, *mddev = v;
3661         
3662         ++*pos;
3663         if (v == (void*)2)
3664                 return NULL;
3665
3666         spin_lock(&all_mddevs_lock);
3667         if (v == (void*)1)
3668                 tmp = all_mddevs.next;
3669         else
3670                 tmp = mddev->all_mddevs.next;
3671         if (tmp != &all_mddevs)
3672                 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
3673         else {
3674                 next_mddev = (void*)2;
3675                 *pos = 0x10000;
3676         }               
3677         spin_unlock(&all_mddevs_lock);
3678
3679         if (v != (void*)1)
3680                 mddev_put(mddev);
3681         return next_mddev;
3682
3683 }
3684
3685 static void md_seq_stop(struct seq_file *seq, void *v)
3686 {
3687         mddev_t *mddev = v;
3688
3689         if (mddev && v != (void*)1 && v != (void*)2)
3690                 mddev_put(mddev);
3691 }
3692
3693 struct mdstat_info {
3694         int event;
3695 };
3696
3697 static int md_seq_show(struct seq_file *seq, void *v)
3698 {
3699         mddev_t *mddev = v;
3700         sector_t size;
3701         struct list_head *tmp2;
3702         mdk_rdev_t *rdev;
3703         struct mdstat_info *mi = seq->private;
3704         int i;
3705         struct bitmap *bitmap;
3706
3707         if (v == (void*)1) {
3708                 seq_printf(seq, "Personalities : ");
3709                 spin_lock(&pers_lock);
3710                 for (i = 0; i < MAX_PERSONALITY; i++)
3711                         if (pers[i])
3712                                 seq_printf(seq, "[%s] ", pers[i]->name);
3713
3714                 spin_unlock(&pers_lock);
3715                 seq_printf(seq, "\n");
3716                 mi->event = atomic_read(&md_event_count);
3717                 return 0;
3718         }
3719         if (v == (void*)2) {
3720                 status_unused(seq);
3721                 return 0;
3722         }
3723
3724         if (mddev_lock(mddev)!=0) 
3725                 return -EINTR;
3726         if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
3727                 seq_printf(seq, "%s : %sactive", mdname(mddev),
3728                                                 mddev->pers ? "" : "in");
3729                 if (mddev->pers) {
3730                         if (mddev->ro==1)
3731                                 seq_printf(seq, " (read-only)");
3732                         if (mddev->ro==2)
3733                                 seq_printf(seq, "(auto-read-only)");
3734                         seq_printf(seq, " %s", mddev->pers->name);
3735                 }
3736
3737                 size = 0;
3738                 ITERATE_RDEV(mddev,rdev,tmp2) {
3739                         char b[BDEVNAME_SIZE];
3740                         seq_printf(seq, " %s[%d]",
3741                                 bdevname(rdev->bdev,b), rdev->desc_nr);
3742                         if (test_bit(WriteMostly, &rdev->flags))
3743                                 seq_printf(seq, "(W)");
3744                         if (test_bit(Faulty, &rdev->flags)) {
3745                                 seq_printf(seq, "(F)");
3746                                 continue;
3747                         } else if (rdev->raid_disk < 0)
3748                                 seq_printf(seq, "(S)"); /* spare */
3749                         size += rdev->size;
3750                 }
3751
3752                 if (!list_empty(&mddev->disks)) {
3753                         if (mddev->pers)
3754                                 seq_printf(seq, "\n      %llu blocks",
3755                                         (unsigned long long)mddev->array_size);
3756                         else
3757                                 seq_printf(seq, "\n      %llu blocks",
3758                                         (unsigned long long)size);
3759                 }
3760                 if (mddev->persistent) {
3761                         if (mddev->major_version != 0 ||
3762                             mddev->minor_version != 90) {
3763                                 seq_printf(seq," super %d.%d",
3764                                            mddev->major_version,
3765                                            mddev->minor_version);
3766                         }
3767                 } else
3768                         seq_printf(seq, " super non-persistent");
3769
3770                 if (mddev->pers) {
3771                         mddev->pers->status (seq, mddev);
3772                         seq_printf(seq, "\n      ");
3773                         if (mddev->pers->sync_request) {
3774                                 if (mddev->curr_resync > 2) {
3775                                         status_resync (seq, mddev);
3776                                         seq_printf(seq, "\n      ");
3777                                 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
3778                                         seq_printf(seq, "\tresync=DELAYED\n      ");
3779                                 else if (mddev->recovery_cp < MaxSector)
3780                                         seq_printf(seq, "\tresync=PENDING\n      ");
3781                         }
3782                 } else
3783                         seq_printf(seq, "\n       ");
3784
3785                 if ((bitmap = mddev->bitmap)) {
3786                         unsigned long chunk_kb;
3787                         unsigned long flags;
3788                         spin_lock_irqsave(&bitmap->lock, flags);
3789                         chunk_kb = bitmap->chunksize >> 10;
3790                         seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
3791                                 "%lu%s chunk",
3792                                 bitmap->pages - bitmap->missing_pages,
3793                                 bitmap->pages,
3794                                 (bitmap->pages - bitmap->missing_pages)
3795                                         << (PAGE_SHIFT - 10),
3796                                 chunk_kb ? chunk_kb : bitmap->chunksize,
3797                                 chunk_kb ? "KB" : "B");
3798                         if (bitmap->file) {
3799                                 seq_printf(seq, ", file: ");
3800                                 seq_path(seq, bitmap->file->f_vfsmnt,
3801                                          bitmap->file->f_dentry," \t\n");
3802                         }
3803
3804                         seq_printf(seq, "\n");
3805                         spin_unlock_irqrestore(&bitmap->lock, flags);
3806                 }
3807
3808                 seq_printf(seq, "\n");
3809         }
3810         mddev_unlock(mddev);
3811         
3812         return 0;
3813 }
3814
3815 static struct seq_operations md_seq_ops = {
3816         .start  = md_seq_start,
3817         .next   = md_seq_next,
3818         .stop   = md_seq_stop,
3819         .show   = md_seq_show,
3820 };
3821
3822 static int md_seq_open(struct inode *inode, struct file *file)
3823 {
3824         int error;
3825         struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
3826         if (mi == NULL)
3827                 return -ENOMEM;
3828
3829         error = seq_open(file, &md_seq_ops);
3830         if (error)
3831                 kfree(mi);
3832         else {
3833                 struct seq_file *p = file->private_data;
3834                 p->private = mi;
3835                 mi->event = atomic_read(&md_event_count);
3836         }
3837         return error;
3838 }
3839
3840 static int md_seq_release(struct inode *inode, struct file *file)
3841 {
3842         struct seq_file *m = file->private_data;
3843         struct mdstat_info *mi = m->private;
3844         m->private = NULL;
3845         kfree(mi);
3846         return seq_release(inode, file);
3847 }
3848
3849 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
3850 {
3851         struct seq_file *m = filp->private_data;
3852         struct mdstat_info *mi = m->private;
3853         int mask;
3854
3855         poll_wait(filp, &md_event_waiters, wait);
3856
3857         /* always allow read */
3858         mask = POLLIN | POLLRDNORM;
3859
3860         if (mi->event != atomic_read(&md_event_count))
3861                 mask |= POLLERR | POLLPRI;
3862         return mask;
3863 }
3864
3865 static struct file_operations md_seq_fops = {
3866         .open           = md_seq_open,
3867         .read           = seq_read,
3868         .llseek         = seq_lseek,
3869         .release        = md_seq_release,
3870         .poll           = mdstat_poll,
3871 };
3872
3873 int register_md_personality(int pnum, mdk_personality_t *p)
3874 {
3875         if (pnum >= MAX_PERSONALITY) {
3876                 printk(KERN_ERR
3877                        "md: tried to install personality %s as nr %d, but max is %lu\n",
3878                        p->name, pnum, MAX_PERSONALITY-1);
3879                 return -EINVAL;
3880         }
3881
3882         spin_lock(&pers_lock);
3883         if (pers[pnum]) {
3884                 spin_unlock(&pers_lock);
3885                 return -EBUSY;
3886         }
3887
3888         pers[pnum] = p;
3889         printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
3890         spin_unlock(&pers_lock);
3891         return 0;
3892 }
3893
3894 int unregister_md_personality(int pnum)
3895 {
3896         if (pnum >= MAX_PERSONALITY)
3897                 return -EINVAL;
3898
3899         printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
3900         spin_lock(&pers_lock);
3901         pers[pnum] = NULL;
3902         spin_unlock(&pers_lock);
3903         return 0;
3904 }
3905
3906 static int is_mddev_idle(mddev_t *mddev)
3907 {
3908         mdk_rdev_t * rdev;
3909         struct list_head *tmp;
3910         int idle;
3911         unsigned long curr_events;
3912
3913         idle = 1;
3914         ITERATE_RDEV(mddev,rdev,tmp) {
3915                 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
3916                 curr_events = disk_stat_read(disk, sectors[0]) + 
3917                                 disk_stat_read(disk, sectors[1]) - 
3918                                 atomic_read(&disk->sync_io);
3919                 /* The difference between curr_events and last_events
3920                  * will be affected by any new non-sync IO (making
3921                  * curr_events bigger) and any difference in the amount of
3922                  * in-flight syncio (making current_events bigger or smaller)
3923                  * The amount in-flight is currently limited to
3924                  * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
3925                  * which is at most 4096 sectors.
3926                  * These numbers are fairly fragile and should be made
3927                  * more robust, probably by enforcing the
3928                  * 'window size' that md_do_sync sort-of uses.
3929                  *
3930                  * Note: the following is an unsigned comparison.
3931                  */
3932                 if ((curr_events - rdev->last_events + 4096) > 8192) {
3933                         rdev->last_events = curr_events;
3934                         idle = 0;
3935                 }
3936         }
3937         return idle;
3938 }
3939
3940 void md_done_sync(mddev_t *mddev, int blocks, int ok)
3941 {
3942         /* another "blocks" (512byte) blocks have been synced */
3943         atomic_sub(blocks, &mddev->recovery_active);
3944         wake_up(&mddev->recovery_wait);
3945         if (!ok) {
3946                 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
3947                 md_wakeup_thread(mddev->thread);
3948                 // stop recovery, signal do_sync ....
3949         }
3950 }
3951
3952
3953 /* md_write_start(mddev, bi)
3954  * If we need to update some array metadata (e.g. 'active' flag
3955  * in superblock) before writing, schedule a superblock update
3956  * and wait for it to complete.
3957  */
3958 void md_write_start(mddev_t *mddev, struct bio *bi)
3959 {
3960         if (bio_data_dir(bi) != WRITE)
3961                 return;
3962
3963         BUG_ON(mddev->ro == 1);
3964         if (mddev->ro == 2) {
3965                 /* need to switch to read/write */
3966                 mddev->ro = 0;
3967                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3968                 md_wakeup_thread(mddev->thread);
3969         }
3970         atomic_inc(&mddev->writes_pending);
3971         if (mddev->in_sync) {
3972                 spin_lock_irq(&mddev->write_lock);
3973                 if (mddev->in_sync) {
3974                         mddev->in_sync = 0;
3975                         mddev->sb_dirty = 1;
3976                         md_wakeup_thread(mddev->thread);
3977                 }
3978                 spin_unlock_irq(&mddev->write_lock);
3979         }
3980         wait_event(mddev->sb_wait, mddev->sb_dirty==0);
3981 }
3982
3983 void md_write_end(mddev_t *mddev)
3984 {
3985         if (atomic_dec_and_test(&mddev->writes_pending)) {
3986                 if (mddev->safemode == 2)
3987                         md_wakeup_thread(mddev->thread);
3988                 else
3989                         mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
3990         }
3991 }
3992
3993 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
3994
3995 #define SYNC_MARKS      10
3996 #define SYNC_MARK_STEP  (3*HZ)
3997 static void md_do_sync(mddev_t *mddev)
3998 {
3999         mddev_t *mddev2;
4000         unsigned int currspeed = 0,
4001                  window;
4002         sector_t max_sectors,j, io_sectors;
4003         unsigned long mark[SYNC_MARKS];
4004         sector_t mark_cnt[SYNC_MARKS];
4005         int last_mark,m;
4006         struct list_head *tmp;
4007         sector_t last_check;
4008         int skipped = 0;
4009
4010         /* just incase thread restarts... */
4011         if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4012                 return;
4013
4014         /* we overload curr_resync somewhat here.
4015          * 0 == not engaged in resync at all
4016          * 2 == checking that there is no conflict with another sync
4017          * 1 == like 2, but have yielded to allow conflicting resync to
4018          *              commense
4019          * other == active in resync - this many blocks
4020          *
4021          * Before starting a resync we must have set curr_resync to
4022          * 2, and then checked that every "conflicting" array has curr_resync
4023          * less than ours.  When we find one that is the same or higher
4024          * we wait on resync_wait.  To avoid deadlock, we reduce curr_resync
4025          * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4026          * This will mean we have to start checking from the beginning again.
4027          *
4028          */
4029
4030         do {
4031                 mddev->curr_resync = 2;
4032
4033         try_again:
4034                 if (kthread_should_stop()) {
4035                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4036                         goto skip;
4037                 }
4038                 ITERATE_MDDEV(mddev2,tmp) {
4039                         if (mddev2 == mddev)
4040                                 continue;
4041                         if (mddev2->curr_resync && 
4042                             match_mddev_units(mddev,mddev2)) {
4043                                 DEFINE_WAIT(wq);
4044                                 if (mddev < mddev2 && mddev->curr_resync == 2) {
4045                                         /* arbitrarily yield */
4046                                         mddev->curr_resync = 1;
4047                                         wake_up(&resync_wait);
4048                                 }
4049                                 if (mddev > mddev2 && mddev->curr_resync == 1)
4050                                         /* no need to wait here, we can wait the next
4051                                          * time 'round when curr_resync == 2
4052                                          */
4053                                         continue;
4054                                 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4055                                 if (!kthread_should_stop() &&
4056                                     mddev2->curr_resync >= mddev->curr_resync) {
4057                                         printk(KERN_INFO "md: delaying resync of %s"
4058                                                " until %s has finished resync (they"
4059                                                " share one or more physical units)\n",
4060                                                mdname(mddev), mdname(mddev2));
4061                                         mddev_put(mddev2);
4062                                         schedule();
4063                                         finish_wait(&resync_wait, &wq);
4064                                         goto try_again;
4065                                 }
4066                                 finish_wait(&resync_wait, &wq);
4067                         }
4068                 }
4069         } while (mddev->curr_resync < 2);
4070
4071         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4072                 /* resync follows the size requested by the personality,
4073                  * which defaults to physical size, but can be virtual size
4074                  */
4075                 max_sectors = mddev->resync_max_sectors;
4076                 mddev->resync_mismatches = 0;
4077         } else
4078                 /* recovery follows the physical size of devices */
4079                 max_sectors = mddev->size << 1;
4080
4081         printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4082         printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
4083                 " %d KB/sec/disc.\n", sysctl_speed_limit_min);
4084         printk(KERN_INFO "md: using maximum available idle IO bandwidth "
4085                "(but not more than %d KB/sec) for reconstruction.\n",
4086                sysctl_speed_limit_max);
4087
4088         is_mddev_idle(mddev); /* this also initializes IO event counters */
4089         /* we don't use the checkpoint if there's a bitmap */
4090         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4091             && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4092                 j = mddev->recovery_cp;
4093         else
4094                 j = 0;
4095         io_sectors = 0;
4096         for (m = 0; m < SYNC_MARKS; m++) {
4097                 mark[m] = jiffies;
4098                 mark_cnt[m] = io_sectors;
4099         }
4100         last_mark = 0;
4101         mddev->resync_mark = mark[last_mark];
4102         mddev->resync_mark_cnt = mark_cnt[last_mark];
4103
4104         /*
4105          * Tune reconstruction:
4106          */
4107         window = 32*(PAGE_SIZE/512);
4108         printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4109                 window/2,(unsigned long long) max_sectors/2);
4110
4111         atomic_set(&mddev->recovery_active, 0);
4112         init_waitqueue_head(&mddev->recovery_wait);
4113         last_check = 0;
4114
4115         if (j>2) {
4116                 printk(KERN_INFO 
4117                         "md: resuming recovery of %s from checkpoint.\n",
4118                         mdname(mddev));
4119                 mddev->curr_resync = j;
4120         }
4121
4122         while (j < max_sectors) {
4123                 sector_t sectors;
4124
4125                 skipped = 0;
4126                 sectors = mddev->pers->sync_request(mddev, j, &skipped,
4127                                             currspeed < sysctl_speed_limit_min);
4128                 if (sectors == 0) {
4129                         set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4130                         goto out;
4131                 }
4132
4133                 if (!skipped) { /* actual IO requested */
4134                         io_sectors += sectors;
4135                         atomic_add(sectors, &mddev->recovery_active);
4136                 }
4137
4138                 j += sectors;
4139                 if (j>1) mddev->curr_resync = j;
4140                 if (last_check == 0)
4141                         /* this is the earliers that rebuilt will be
4142                          * visible in /proc/mdstat
4143                          */
4144                         md_new_event(mddev);
4145
4146                 if (last_check + window > io_sectors || j == max_sectors)
4147                         continue;
4148
4149                 last_check = io_sectors;
4150
4151                 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4152                     test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4153                         break;
4154
4155         repeat:
4156                 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4157                         /* step marks */
4158                         int next = (last_mark+1) % SYNC_MARKS;
4159
4160                         mddev->resync_mark = mark[next];
4161                         mddev->resync_mark_cnt = mark_cnt[next];
4162                         mark[next] = jiffies;
4163                         mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
4164                         last_mark = next;
4165                 }
4166
4167
4168                 if (kthread_should_stop()) {
4169                         /*
4170                          * got a signal, exit.
4171                          */
4172                         printk(KERN_INFO 
4173                                 "md: md_do_sync() got signal ... exiting\n");
4174                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4175                         goto out;
4176                 }
4177
4178                 /*
4179                  * this loop exits only if either when we are slower than
4180                  * the 'hard' speed limit, or the system was IO-idle for
4181                  * a jiffy.
4182                  * the system might be non-idle CPU-wise, but we only care
4183                  * about not overloading the IO subsystem. (things like an
4184                  * e2fsck being done on the RAID array should execute fast)
4185                  */
4186                 mddev->queue->unplug_fn(mddev->queue);
4187                 cond_resched();
4188
4189                 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4190                         /((jiffies-mddev->resync_mark)/HZ +1) +1;
4191
4192                 if (currspeed > sysctl_speed_limit_min) {
4193                         if ((currspeed > sysctl_speed_limit_max) ||
4194                                         !is_mddev_idle(mddev)) {
4195                                 msleep(500);
4196                                 goto repeat;
4197                         }
4198                 }
4199         }
4200         printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4201         /*
4202          * this also signals 'finished resyncing' to md_stop
4203          */
4204  out:
4205         mddev->queue->unplug_fn(mddev->queue);
4206
4207         wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4208
4209         /* tell personality that we are finished */
4210         mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
4211
4212         if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4213             mddev->curr_resync > 2 &&
4214             mddev->curr_resync >= mddev->recovery_cp) {
4215                 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4216                         printk(KERN_INFO 
4217                                 "md: checkpointing recovery of %s.\n",
4218                                 mdname(mddev));
4219                         mddev->recovery_cp = mddev->curr_resync;
4220                 } else
4221                         mddev->recovery_cp = MaxSector;
4222         }
4223
4224  skip:
4225         mddev->curr_resync = 0;
4226         wake_up(&resync_wait);
4227         set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4228         md_wakeup_thread(mddev->thread);
4229 }
4230
4231
4232 /*
4233  * This routine is regularly called by all per-raid-array threads to
4234  * deal with generic issues like resync and super-block update.
4235  * Raid personalities that don't have a thread (linear/raid0) do not
4236  * need this as they never do any recovery or update the superblock.
4237  *
4238  * It does not do any resync itself, but rather "forks" off other threads
4239  * to do that as needed.
4240  * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4241  * "->recovery" and create a thread at ->sync_thread.
4242  * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4243  * and wakeups up this thread which will reap the thread and finish up.
4244  * This thread also removes any faulty devices (with nr_pending == 0).
4245  *
4246  * The overall approach is:
4247  *  1/ if the superblock needs updating, update it.
4248  *  2/ If a recovery thread is running, don't do anything else.
4249  *  3/ If recovery has finished, clean up, possibly marking spares active.
4250  *  4/ If there are any faulty devices, remove them.
4251  *  5/ If array is degraded, try to add spares devices
4252  *  6/ If array has spares or is not in-sync, start a resync thread.
4253  */
4254 void md_check_recovery(mddev_t *mddev)
4255 {
4256         mdk_rdev_t *rdev;
4257         struct list_head *rtmp;
4258
4259
4260         if (mddev->bitmap)
4261                 bitmap_daemon_work(mddev->bitmap);
4262
4263         if (mddev->ro)
4264                 return;
4265
4266         if (signal_pending(current)) {
4267                 if (mddev->pers->sync_request) {
4268                         printk(KERN_INFO "md: %s in immediate safe mode\n",
4269                                mdname(mddev));
4270                         mddev->safemode = 2;
4271                 }
4272                 flush_signals(current);
4273         }
4274
4275         if ( ! (
4276                 mddev->sb_dirty ||
4277                 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
4278                 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4279                 (mddev->safemode == 1) ||
4280                 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4281                  && !mddev->in_sync && mddev->recovery_cp == MaxSector)
4282                 ))
4283                 return;
4284
4285         if (mddev_trylock(mddev)==0) {
4286                 int spares =0;
4287
4288                 spin_lock_irq(&mddev->write_lock);
4289                 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4290                     !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4291                         mddev->in_sync = 1;
4292                         mddev->sb_dirty = 1;
4293                 }
4294                 if (mddev->safemode == 1)
4295                         mddev->safemode = 0;
4296                 spin_unlock_irq(&mddev->write_lock);
4297
4298                 if (mddev->sb_dirty)
4299                         md_update_sb(mddev);
4300
4301
4302                 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4303                     !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4304                         /* resync/recovery still happening */
4305                         clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4306                         goto unlock;
4307                 }
4308                 if (mddev->sync_thread) {
4309                         /* resync has finished, collect result */
4310                         md_unregister_thread(mddev->sync_thread);
4311                         mddev->sync_thread = NULL;
4312                         if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4313                             !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4314                                 /* success...*/
4315                                 /* activate any spares */
4316                                 mddev->pers->spare_active(mddev);
4317                         }
4318                         md_update_sb(mddev);
4319
4320                         /* if array is no-longer degraded, then any saved_raid_disk
4321                          * information must be scrapped
4322                          */
4323                         if (!mddev->degraded)
4324                                 ITERATE_RDEV(mddev,rdev,rtmp)
4325                                         rdev->saved_raid_disk = -1;
4326
4327                         mddev->recovery = 0;
4328                         /* flag recovery needed just to double check */
4329                         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4330                         md_new_event(mddev);
4331                         goto unlock;
4332                 }
4333                 /* Clear some bits that don't mean anything, but
4334                  * might be left set
4335                  */
4336                 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4337                 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4338                 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4339                 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4340
4341                 /* no recovery is running.
4342                  * remove any failed drives, then
4343                  * add spares if possible.
4344                  * Spare are also removed and re-added, to allow
4345                  * the personality to fail the re-add.
4346                  */
4347                 ITERATE_RDEV(mddev,rdev,rtmp)
4348                         if (rdev->raid_disk >= 0 &&
4349                             (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
4350                             atomic_read(&rdev->nr_pending)==0) {
4351                                 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4352                                         char nm[20];
4353                                         sprintf(nm,"rd%d", rdev->raid_disk);
4354                                         sysfs_remove_link(&mddev->kobj, nm);
4355                                         rdev->raid_disk = -1;
4356                                 }
4357                         }
4358
4359                 if (mddev->degraded) {
4360                         ITERATE_RDEV(mddev,rdev,rtmp)
4361                                 if (rdev->raid_disk < 0
4362                                     && !test_bit(Faulty, &rdev->flags)) {
4363                                         if (mddev->pers->hot_add_disk(mddev,rdev)) {
4364                                                 char nm[20];
4365                                                 sprintf(nm, "rd%d", rdev->raid_disk);
4366                                                 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
4367                                                 spares++;
4368                                                 md_new_event(mddev);
4369                                         } else
4370                                                 break;
4371                                 }
4372                 }
4373
4374                 if (spares) {
4375                         clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4376                         clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4377                 } else if (mddev->recovery_cp < MaxSector) {
4378                         set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4379                 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4380                         /* nothing to be done ... */
4381                         goto unlock;
4382
4383                 if (mddev->pers->sync_request) {
4384                         set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4385                         if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4386                                 /* We are adding a device or devices to an array
4387                                  * which has the bitmap stored on all devices.
4388                                  * So make sure all bitmap pages get written
4389                                  */
4390                                 bitmap_write_all(mddev->bitmap);
4391                         }
4392                         mddev->sync_thread = md_register_thread(md_do_sync,
4393                                                                 mddev,
4394                                                                 "%s_resync");
4395                         if (!mddev->sync_thread) {
4396                                 printk(KERN_ERR "%s: could not start resync"
4397                                         " thread...\n", 
4398                                         mdname(mddev));
4399                                 /* leave the spares where they are, it shouldn't hurt */
4400                                 mddev->recovery = 0;
4401                         } else
4402                                 md_wakeup_thread(mddev->sync_thread);
4403                         md_new_event(mddev);
4404                 }
4405         unlock:
4406                 mddev_unlock(mddev);
4407         }
4408 }
4409
4410 static int md_notify_reboot(struct notifier_block *this,
4411                             unsigned long code, void *x)
4412 {
4413         struct list_head *tmp;
4414         mddev_t *mddev;
4415
4416         if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
4417
4418                 printk(KERN_INFO "md: stopping all md devices.\n");
4419
4420                 ITERATE_MDDEV(mddev,tmp)
4421                         if (mddev_trylock(mddev)==0)
4422                                 do_md_stop (mddev, 1);
4423                 /*
4424                  * certain more exotic SCSI devices are known to be
4425                  * volatile wrt too early system reboots. While the
4426                  * right place to handle this issue is the given
4427                  * driver, we do want to have a safe RAID driver ...
4428                  */
4429                 mdelay(1000*1);
4430         }
4431         return NOTIFY_DONE;
4432 }
4433
4434 static struct notifier_block md_notifier = {
4435         .notifier_call  = md_notify_reboot,
4436         .next           = NULL,
4437         .priority       = INT_MAX, /* before any real devices */
4438 };
4439
4440 static void md_geninit(void)
4441 {
4442         struct proc_dir_entry *p;
4443
4444         dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
4445
4446         p = create_proc_entry("mdstat", S_IRUGO, NULL);
4447         if (p)
4448                 p->proc_fops = &md_seq_fops;
4449 }
4450
4451 static int __init md_init(void)
4452 {
4453         int minor;
4454
4455         printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
4456                         " MD_SB_DISKS=%d\n",
4457                         MD_MAJOR_VERSION, MD_MINOR_VERSION,
4458                         MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
4459         printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
4460                         BITMAP_MINOR);
4461
4462         if (register_blkdev(MAJOR_NR, "md"))
4463                 return -1;
4464         if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
4465                 unregister_blkdev(MAJOR_NR, "md");
4466                 return -1;
4467         }
4468         devfs_mk_dir("md");
4469         blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
4470                                 md_probe, NULL, NULL);
4471         blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
4472                             md_probe, NULL, NULL);
4473
4474         for (minor=0; minor < MAX_MD_DEVS; ++minor)
4475                 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
4476                                 S_IFBLK|S_IRUSR|S_IWUSR,
4477                                 "md/%d", minor);
4478
4479         for (minor=0; minor < MAX_MD_DEVS; ++minor)
4480                 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
4481                               S_IFBLK|S_IRUSR|S_IWUSR,
4482                               "md/mdp%d", minor);
4483
4484
4485         register_reboot_notifier(&md_notifier);
4486         raid_table_header = register_sysctl_table(raid_root_table, 1);
4487
4488         md_geninit();
4489         return (0);
4490 }
4491
4492
4493 #ifndef MODULE
4494
4495 /*
4496  * Searches all registered partitions for autorun RAID arrays
4497  * at boot time.
4498  */
4499 static dev_t detected_devices[128];
4500 static int dev_cnt;
4501
4502 void md_autodetect_dev(dev_t dev)
4503 {
4504         if (dev_cnt >= 0 && dev_cnt < 127)
4505                 detected_devices[dev_cnt++] = dev;
4506 }
4507
4508
4509 static void autostart_arrays(int part)
4510 {
4511         mdk_rdev_t *rdev;
4512         int i;
4513
4514         printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
4515
4516         for (i = 0; i < dev_cnt; i++) {
4517                 dev_t dev = detected_devices[i];
4518
4519                 rdev = md_import_device(dev,0, 0);
4520                 if (IS_ERR(rdev))
4521                         continue;
4522
4523                 if (test_bit(Faulty, &rdev->flags)) {
4524                         MD_BUG();
4525                         continue;
4526                 }
4527                 list_add(&rdev->same_set, &pending_raid_disks);
4528         }
4529         dev_cnt = 0;
4530
4531         autorun_devices(part);
4532 }
4533
4534 #endif
4535
4536 static __exit void md_exit(void)
4537 {
4538         mddev_t *mddev;
4539         struct list_head *tmp;
4540         int i;
4541         blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
4542         blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
4543         for (i=0; i < MAX_MD_DEVS; i++)
4544                 devfs_remove("md/%d", i);
4545         for (i=0; i < MAX_MD_DEVS; i++)
4546                 devfs_remove("md/d%d", i);
4547
4548         devfs_remove("md");
4549
4550         unregister_blkdev(MAJOR_NR,"md");
4551         unregister_blkdev(mdp_major, "mdp");
4552         unregister_reboot_notifier(&md_notifier);
4553         unregister_sysctl_table(raid_table_header);
4554         remove_proc_entry("mdstat", NULL);
4555         ITERATE_MDDEV(mddev,tmp) {
4556                 struct gendisk *disk = mddev->gendisk;
4557                 if (!disk)
4558                         continue;
4559                 export_array(mddev);
4560                 del_gendisk(disk);
4561                 put_disk(disk);
4562                 mddev->gendisk = NULL;
4563                 mddev_put(mddev);
4564         }
4565 }
4566
4567 module_init(md_init)
4568 module_exit(md_exit)
4569
4570 static int get_ro(char *buffer, struct kernel_param *kp)
4571 {
4572         return sprintf(buffer, "%d", start_readonly);
4573 }
4574 static int set_ro(const char *val, struct kernel_param *kp)
4575 {
4576         char *e;
4577         int num = simple_strtoul(val, &e, 10);
4578         if (*val && (*e == '\0' || *e == '\n')) {
4579                 start_readonly = num;
4580                 return 0;;
4581         }
4582         return -EINVAL;
4583 }
4584
4585 module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
4586 module_param(start_dirty_degraded, int, 0644);
4587
4588
4589 EXPORT_SYMBOL(register_md_personality);
4590 EXPORT_SYMBOL(unregister_md_personality);
4591 EXPORT_SYMBOL(md_error);
4592 EXPORT_SYMBOL(md_done_sync);
4593 EXPORT_SYMBOL(md_write_start);
4594 EXPORT_SYMBOL(md_write_end);
4595 EXPORT_SYMBOL(md_register_thread);
4596 EXPORT_SYMBOL(md_unregister_thread);
4597 EXPORT_SYMBOL(md_wakeup_thread);
4598 EXPORT_SYMBOL(md_print_devices);
4599 EXPORT_SYMBOL(md_check_recovery);
4600 MODULE_LICENSE("GPL");
4601 MODULE_ALIAS("md");
4602 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);