Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfashe...
[pandora-kernel.git] / drivers / md / md.c
1 /*
2    md.c : Multiple Devices driver for Linux
3           Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5      completely rewritten, based on the MD driver code from Marc Zyngier
6
7    Changes:
8
9    - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10    - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11    - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12    - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13    - kmod support by: Cyrus Durgin
14    - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15    - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17    - lots of fixes and improvements to the RAID1/RAID5 and generic
18      RAID code (such as request based resynchronization):
19
20      Neil Brown <neilb@cse.unsw.edu.au>.
21
22    - persistent bitmap code
23      Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
25    This program is free software; you can redistribute it and/or modify
26    it under the terms of the GNU General Public License as published by
27    the Free Software Foundation; either version 2, or (at your option)
28    any later version.
29
30    You should have received a copy of the GNU General Public License
31    (for example /usr/src/linux/COPYING); if not, write to the Free
32    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 */
34
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/kthread.h>
38 #include <linux/linkage.h>
39 #include <linux/raid/md.h>
40 #include <linux/raid/bitmap.h>
41 #include <linux/sysctl.h>
42 #include <linux/buffer_head.h> /* for invalidate_bdev */
43 #include <linux/poll.h>
44 #include <linux/mutex.h>
45 #include <linux/ctype.h>
46 #include <linux/freezer.h>
47
48 #include <linux/init.h>
49
50 #include <linux/file.h>
51
52 #ifdef CONFIG_KMOD
53 #include <linux/kmod.h>
54 #endif
55
56 #include <asm/unaligned.h>
57
58 #define MAJOR_NR MD_MAJOR
59 #define MD_DRIVER
60
61 /* 63 partitions with the alternate major number (mdp) */
62 #define MdpMinorShift 6
63
64 #define DEBUG 0
65 #define dprintk(x...) ((void)(DEBUG && printk(x)))
66
67
68 #ifndef MODULE
69 static void autostart_arrays (int part);
70 #endif
71
72 static LIST_HEAD(pers_list);
73 static DEFINE_SPINLOCK(pers_lock);
74
75 static void md_print_devices(void);
76
77 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
78
79 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
80
81 /*
82  * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83  * is 1000 KB/sec, so the extra system load does not show up that much.
84  * Increase it if you want to have more _guaranteed_ speed. Note that
85  * the RAID driver will use the maximum available bandwidth if the IO
86  * subsystem is idle. There is also an 'absolute maximum' reconstruction
87  * speed limit - in case reconstruction slows down your system despite
88  * idle IO detection.
89  *
90  * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
91  * or /sys/block/mdX/md/sync_speed_{min,max}
92  */
93
94 static int sysctl_speed_limit_min = 1000;
95 static int sysctl_speed_limit_max = 200000;
96 static inline int speed_min(mddev_t *mddev)
97 {
98         return mddev->sync_speed_min ?
99                 mddev->sync_speed_min : sysctl_speed_limit_min;
100 }
101
102 static inline int speed_max(mddev_t *mddev)
103 {
104         return mddev->sync_speed_max ?
105                 mddev->sync_speed_max : sysctl_speed_limit_max;
106 }
107
108 static struct ctl_table_header *raid_table_header;
109
110 static ctl_table raid_table[] = {
111         {
112                 .ctl_name       = DEV_RAID_SPEED_LIMIT_MIN,
113                 .procname       = "speed_limit_min",
114                 .data           = &sysctl_speed_limit_min,
115                 .maxlen         = sizeof(int),
116                 .mode           = S_IRUGO|S_IWUSR,
117                 .proc_handler   = &proc_dointvec,
118         },
119         {
120                 .ctl_name       = DEV_RAID_SPEED_LIMIT_MAX,
121                 .procname       = "speed_limit_max",
122                 .data           = &sysctl_speed_limit_max,
123                 .maxlen         = sizeof(int),
124                 .mode           = S_IRUGO|S_IWUSR,
125                 .proc_handler   = &proc_dointvec,
126         },
127         { .ctl_name = 0 }
128 };
129
130 static ctl_table raid_dir_table[] = {
131         {
132                 .ctl_name       = DEV_RAID,
133                 .procname       = "raid",
134                 .maxlen         = 0,
135                 .mode           = S_IRUGO|S_IXUGO,
136                 .child          = raid_table,
137         },
138         { .ctl_name = 0 }
139 };
140
141 static ctl_table raid_root_table[] = {
142         {
143                 .ctl_name       = CTL_DEV,
144                 .procname       = "dev",
145                 .maxlen         = 0,
146                 .mode           = 0555,
147                 .child          = raid_dir_table,
148         },
149         { .ctl_name = 0 }
150 };
151
152 static struct block_device_operations md_fops;
153
154 static int start_readonly;
155
156 /*
157  * We have a system wide 'event count' that is incremented
158  * on any 'interesting' event, and readers of /proc/mdstat
159  * can use 'poll' or 'select' to find out when the event
160  * count increases.
161  *
162  * Events are:
163  *  start array, stop array, error, add device, remove device,
164  *  start build, activate spare
165  */
166 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
167 static atomic_t md_event_count;
168 void md_new_event(mddev_t *mddev)
169 {
170         atomic_inc(&md_event_count);
171         wake_up(&md_event_waiters);
172 }
173 EXPORT_SYMBOL_GPL(md_new_event);
174
175 /* Alternate version that can be called from interrupts
176  * when calling sysfs_notify isn't needed.
177  */
178 static void md_new_event_inintr(mddev_t *mddev)
179 {
180         atomic_inc(&md_event_count);
181         wake_up(&md_event_waiters);
182 }
183
184 /*
185  * Enables to iterate over all existing md arrays
186  * all_mddevs_lock protects this list.
187  */
188 static LIST_HEAD(all_mddevs);
189 static DEFINE_SPINLOCK(all_mddevs_lock);
190
191
192 /*
193  * iterates through all used mddevs in the system.
194  * We take care to grab the all_mddevs_lock whenever navigating
195  * the list, and to always hold a refcount when unlocked.
196  * Any code which breaks out of this loop while own
197  * a reference to the current mddev and must mddev_put it.
198  */
199 #define for_each_mddev(mddev,tmp)                                       \
200                                                                         \
201         for (({ spin_lock(&all_mddevs_lock);                            \
202                 tmp = all_mddevs.next;                                  \
203                 mddev = NULL;});                                        \
204              ({ if (tmp != &all_mddevs)                                 \
205                         mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206                 spin_unlock(&all_mddevs_lock);                          \
207                 if (mddev) mddev_put(mddev);                            \
208                 mddev = list_entry(tmp, mddev_t, all_mddevs);           \
209                 tmp != &all_mddevs;});                                  \
210              ({ spin_lock(&all_mddevs_lock);                            \
211                 tmp = tmp->next;})                                      \
212                 )
213
214
215 static int md_fail_request (struct request_queue *q, struct bio *bio)
216 {
217         bio_io_error(bio);
218         return 0;
219 }
220
221 static inline mddev_t *mddev_get(mddev_t *mddev)
222 {
223         atomic_inc(&mddev->active);
224         return mddev;
225 }
226
227 static void mddev_put(mddev_t *mddev)
228 {
229         if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
230                 return;
231         if (!mddev->raid_disks && list_empty(&mddev->disks)) {
232                 list_del(&mddev->all_mddevs);
233                 spin_unlock(&all_mddevs_lock);
234                 blk_cleanup_queue(mddev->queue);
235                 kobject_put(&mddev->kobj);
236         } else
237                 spin_unlock(&all_mddevs_lock);
238 }
239
240 static mddev_t * mddev_find(dev_t unit)
241 {
242         mddev_t *mddev, *new = NULL;
243
244  retry:
245         spin_lock(&all_mddevs_lock);
246         list_for_each_entry(mddev, &all_mddevs, all_mddevs)
247                 if (mddev->unit == unit) {
248                         mddev_get(mddev);
249                         spin_unlock(&all_mddevs_lock);
250                         kfree(new);
251                         return mddev;
252                 }
253
254         if (new) {
255                 list_add(&new->all_mddevs, &all_mddevs);
256                 spin_unlock(&all_mddevs_lock);
257                 return new;
258         }
259         spin_unlock(&all_mddevs_lock);
260
261         new = kzalloc(sizeof(*new), GFP_KERNEL);
262         if (!new)
263                 return NULL;
264
265         new->unit = unit;
266         if (MAJOR(unit) == MD_MAJOR)
267                 new->md_minor = MINOR(unit);
268         else
269                 new->md_minor = MINOR(unit) >> MdpMinorShift;
270
271         mutex_init(&new->reconfig_mutex);
272         INIT_LIST_HEAD(&new->disks);
273         INIT_LIST_HEAD(&new->all_mddevs);
274         init_timer(&new->safemode_timer);
275         atomic_set(&new->active, 1);
276         atomic_set(&new->openers, 0);
277         spin_lock_init(&new->write_lock);
278         init_waitqueue_head(&new->sb_wait);
279         init_waitqueue_head(&new->recovery_wait);
280         new->reshape_position = MaxSector;
281         new->resync_min = 0;
282         new->resync_max = MaxSector;
283         new->level = LEVEL_NONE;
284
285         new->queue = blk_alloc_queue(GFP_KERNEL);
286         if (!new->queue) {
287                 kfree(new);
288                 return NULL;
289         }
290         /* Can be unlocked because the queue is new: no concurrency */
291         queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
292
293         blk_queue_make_request(new->queue, md_fail_request);
294
295         goto retry;
296 }
297
298 static inline int mddev_lock(mddev_t * mddev)
299 {
300         return mutex_lock_interruptible(&mddev->reconfig_mutex);
301 }
302
303 static inline int mddev_trylock(mddev_t * mddev)
304 {
305         return mutex_trylock(&mddev->reconfig_mutex);
306 }
307
308 static inline void mddev_unlock(mddev_t * mddev)
309 {
310         mutex_unlock(&mddev->reconfig_mutex);
311
312         md_wakeup_thread(mddev->thread);
313 }
314
315 static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
316 {
317         mdk_rdev_t * rdev;
318         struct list_head *tmp;
319
320         rdev_for_each(rdev, tmp, mddev) {
321                 if (rdev->desc_nr == nr)
322                         return rdev;
323         }
324         return NULL;
325 }
326
327 static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
328 {
329         struct list_head *tmp;
330         mdk_rdev_t *rdev;
331
332         rdev_for_each(rdev, tmp, mddev) {
333                 if (rdev->bdev->bd_dev == dev)
334                         return rdev;
335         }
336         return NULL;
337 }
338
339 static struct mdk_personality *find_pers(int level, char *clevel)
340 {
341         struct mdk_personality *pers;
342         list_for_each_entry(pers, &pers_list, list) {
343                 if (level != LEVEL_NONE && pers->level == level)
344                         return pers;
345                 if (strcmp(pers->name, clevel)==0)
346                         return pers;
347         }
348         return NULL;
349 }
350
351 /* return the offset of the super block in 512byte sectors */
352 static inline sector_t calc_dev_sboffset(struct block_device *bdev)
353 {
354         sector_t num_sectors = bdev->bd_inode->i_size / 512;
355         return MD_NEW_SIZE_SECTORS(num_sectors);
356 }
357
358 static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
359 {
360         sector_t num_sectors = rdev->sb_start;
361
362         if (chunk_size)
363                 num_sectors &= ~((sector_t)chunk_size/512 - 1);
364         return num_sectors;
365 }
366
367 static int alloc_disk_sb(mdk_rdev_t * rdev)
368 {
369         if (rdev->sb_page)
370                 MD_BUG();
371
372         rdev->sb_page = alloc_page(GFP_KERNEL);
373         if (!rdev->sb_page) {
374                 printk(KERN_ALERT "md: out of memory.\n");
375                 return -ENOMEM;
376         }
377
378         return 0;
379 }
380
381 static void free_disk_sb(mdk_rdev_t * rdev)
382 {
383         if (rdev->sb_page) {
384                 put_page(rdev->sb_page);
385                 rdev->sb_loaded = 0;
386                 rdev->sb_page = NULL;
387                 rdev->sb_start = 0;
388                 rdev->size = 0;
389         }
390 }
391
392
393 static void super_written(struct bio *bio, int error)
394 {
395         mdk_rdev_t *rdev = bio->bi_private;
396         mddev_t *mddev = rdev->mddev;
397
398         if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
399                 printk("md: super_written gets error=%d, uptodate=%d\n",
400                        error, test_bit(BIO_UPTODATE, &bio->bi_flags));
401                 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
402                 md_error(mddev, rdev);
403         }
404
405         if (atomic_dec_and_test(&mddev->pending_writes))
406                 wake_up(&mddev->sb_wait);
407         bio_put(bio);
408 }
409
410 static void super_written_barrier(struct bio *bio, int error)
411 {
412         struct bio *bio2 = bio->bi_private;
413         mdk_rdev_t *rdev = bio2->bi_private;
414         mddev_t *mddev = rdev->mddev;
415
416         if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
417             error == -EOPNOTSUPP) {
418                 unsigned long flags;
419                 /* barriers don't appear to be supported :-( */
420                 set_bit(BarriersNotsupp, &rdev->flags);
421                 mddev->barriers_work = 0;
422                 spin_lock_irqsave(&mddev->write_lock, flags);
423                 bio2->bi_next = mddev->biolist;
424                 mddev->biolist = bio2;
425                 spin_unlock_irqrestore(&mddev->write_lock, flags);
426                 wake_up(&mddev->sb_wait);
427                 bio_put(bio);
428         } else {
429                 bio_put(bio2);
430                 bio->bi_private = rdev;
431                 super_written(bio, error);
432         }
433 }
434
435 void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
436                    sector_t sector, int size, struct page *page)
437 {
438         /* write first size bytes of page to sector of rdev
439          * Increment mddev->pending_writes before returning
440          * and decrement it on completion, waking up sb_wait
441          * if zero is reached.
442          * If an error occurred, call md_error
443          *
444          * As we might need to resubmit the request if BIO_RW_BARRIER
445          * causes ENOTSUPP, we allocate a spare bio...
446          */
447         struct bio *bio = bio_alloc(GFP_NOIO, 1);
448         int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
449
450         bio->bi_bdev = rdev->bdev;
451         bio->bi_sector = sector;
452         bio_add_page(bio, page, size, 0);
453         bio->bi_private = rdev;
454         bio->bi_end_io = super_written;
455         bio->bi_rw = rw;
456
457         atomic_inc(&mddev->pending_writes);
458         if (!test_bit(BarriersNotsupp, &rdev->flags)) {
459                 struct bio *rbio;
460                 rw |= (1<<BIO_RW_BARRIER);
461                 rbio = bio_clone(bio, GFP_NOIO);
462                 rbio->bi_private = bio;
463                 rbio->bi_end_io = super_written_barrier;
464                 submit_bio(rw, rbio);
465         } else
466                 submit_bio(rw, bio);
467 }
468
469 void md_super_wait(mddev_t *mddev)
470 {
471         /* wait for all superblock writes that were scheduled to complete.
472          * if any had to be retried (due to BARRIER problems), retry them
473          */
474         DEFINE_WAIT(wq);
475         for(;;) {
476                 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
477                 if (atomic_read(&mddev->pending_writes)==0)
478                         break;
479                 while (mddev->biolist) {
480                         struct bio *bio;
481                         spin_lock_irq(&mddev->write_lock);
482                         bio = mddev->biolist;
483                         mddev->biolist = bio->bi_next ;
484                         bio->bi_next = NULL;
485                         spin_unlock_irq(&mddev->write_lock);
486                         submit_bio(bio->bi_rw, bio);
487                 }
488                 schedule();
489         }
490         finish_wait(&mddev->sb_wait, &wq);
491 }
492
493 static void bi_complete(struct bio *bio, int error)
494 {
495         complete((struct completion*)bio->bi_private);
496 }
497
498 int sync_page_io(struct block_device *bdev, sector_t sector, int size,
499                    struct page *page, int rw)
500 {
501         struct bio *bio = bio_alloc(GFP_NOIO, 1);
502         struct completion event;
503         int ret;
504
505         rw |= (1 << BIO_RW_SYNC);
506
507         bio->bi_bdev = bdev;
508         bio->bi_sector = sector;
509         bio_add_page(bio, page, size, 0);
510         init_completion(&event);
511         bio->bi_private = &event;
512         bio->bi_end_io = bi_complete;
513         submit_bio(rw, bio);
514         wait_for_completion(&event);
515
516         ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
517         bio_put(bio);
518         return ret;
519 }
520 EXPORT_SYMBOL_GPL(sync_page_io);
521
522 static int read_disk_sb(mdk_rdev_t * rdev, int size)
523 {
524         char b[BDEVNAME_SIZE];
525         if (!rdev->sb_page) {
526                 MD_BUG();
527                 return -EINVAL;
528         }
529         if (rdev->sb_loaded)
530                 return 0;
531
532
533         if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
534                 goto fail;
535         rdev->sb_loaded = 1;
536         return 0;
537
538 fail:
539         printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
540                 bdevname(rdev->bdev,b));
541         return -EINVAL;
542 }
543
544 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
545 {
546         return  sb1->set_uuid0 == sb2->set_uuid0 &&
547                 sb1->set_uuid1 == sb2->set_uuid1 &&
548                 sb1->set_uuid2 == sb2->set_uuid2 &&
549                 sb1->set_uuid3 == sb2->set_uuid3;
550 }
551
552 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
553 {
554         int ret;
555         mdp_super_t *tmp1, *tmp2;
556
557         tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
558         tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
559
560         if (!tmp1 || !tmp2) {
561                 ret = 0;
562                 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
563                 goto abort;
564         }
565
566         *tmp1 = *sb1;
567         *tmp2 = *sb2;
568
569         /*
570          * nr_disks is not constant
571          */
572         tmp1->nr_disks = 0;
573         tmp2->nr_disks = 0;
574
575         ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
576 abort:
577         kfree(tmp1);
578         kfree(tmp2);
579         return ret;
580 }
581
582
583 static u32 md_csum_fold(u32 csum)
584 {
585         csum = (csum & 0xffff) + (csum >> 16);
586         return (csum & 0xffff) + (csum >> 16);
587 }
588
589 static unsigned int calc_sb_csum(mdp_super_t * sb)
590 {
591         u64 newcsum = 0;
592         u32 *sb32 = (u32*)sb;
593         int i;
594         unsigned int disk_csum, csum;
595
596         disk_csum = sb->sb_csum;
597         sb->sb_csum = 0;
598
599         for (i = 0; i < MD_SB_BYTES/4 ; i++)
600                 newcsum += sb32[i];
601         csum = (newcsum & 0xffffffff) + (newcsum>>32);
602
603
604 #ifdef CONFIG_ALPHA
605         /* This used to use csum_partial, which was wrong for several
606          * reasons including that different results are returned on
607          * different architectures.  It isn't critical that we get exactly
608          * the same return value as before (we always csum_fold before
609          * testing, and that removes any differences).  However as we
610          * know that csum_partial always returned a 16bit value on
611          * alphas, do a fold to maximise conformity to previous behaviour.
612          */
613         sb->sb_csum = md_csum_fold(disk_csum);
614 #else
615         sb->sb_csum = disk_csum;
616 #endif
617         return csum;
618 }
619
620
621 /*
622  * Handle superblock details.
623  * We want to be able to handle multiple superblock formats
624  * so we have a common interface to them all, and an array of
625  * different handlers.
626  * We rely on user-space to write the initial superblock, and support
627  * reading and updating of superblocks.
628  * Interface methods are:
629  *   int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
630  *      loads and validates a superblock on dev.
631  *      if refdev != NULL, compare superblocks on both devices
632  *    Return:
633  *      0 - dev has a superblock that is compatible with refdev
634  *      1 - dev has a superblock that is compatible and newer than refdev
635  *          so dev should be used as the refdev in future
636  *     -EINVAL superblock incompatible or invalid
637  *     -othererror e.g. -EIO
638  *
639  *   int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
640  *      Verify that dev is acceptable into mddev.
641  *       The first time, mddev->raid_disks will be 0, and data from
642  *       dev should be merged in.  Subsequent calls check that dev
643  *       is new enough.  Return 0 or -EINVAL
644  *
645  *   void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
646  *     Update the superblock for rdev with data in mddev
647  *     This does not write to disc.
648  *
649  */
650
651 struct super_type  {
652         char                *name;
653         struct module       *owner;
654         int                 (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
655                                           int minor_version);
656         int                 (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
657         void                (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
658         unsigned long long  (*rdev_size_change)(mdk_rdev_t *rdev,
659                                                 sector_t num_sectors);
660 };
661
662 /*
663  * load_super for 0.90.0 
664  */
665 static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
666 {
667         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
668         mdp_super_t *sb;
669         int ret;
670
671         /*
672          * Calculate the position of the superblock (512byte sectors),
673          * it's at the end of the disk.
674          *
675          * It also happens to be a multiple of 4Kb.
676          */
677         rdev->sb_start = calc_dev_sboffset(rdev->bdev);
678
679         ret = read_disk_sb(rdev, MD_SB_BYTES);
680         if (ret) return ret;
681
682         ret = -EINVAL;
683
684         bdevname(rdev->bdev, b);
685         sb = (mdp_super_t*)page_address(rdev->sb_page);
686
687         if (sb->md_magic != MD_SB_MAGIC) {
688                 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
689                        b);
690                 goto abort;
691         }
692
693         if (sb->major_version != 0 ||
694             sb->minor_version < 90 ||
695             sb->minor_version > 91) {
696                 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
697                         sb->major_version, sb->minor_version,
698                         b);
699                 goto abort;
700         }
701
702         if (sb->raid_disks <= 0)
703                 goto abort;
704
705         if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
706                 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
707                         b);
708                 goto abort;
709         }
710
711         rdev->preferred_minor = sb->md_minor;
712         rdev->data_offset = 0;
713         rdev->sb_size = MD_SB_BYTES;
714
715         if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
716                 if (sb->level != 1 && sb->level != 4
717                     && sb->level != 5 && sb->level != 6
718                     && sb->level != 10) {
719                         /* FIXME use a better test */
720                         printk(KERN_WARNING
721                                "md: bitmaps not supported for this level.\n");
722                         goto abort;
723                 }
724         }
725
726         if (sb->level == LEVEL_MULTIPATH)
727                 rdev->desc_nr = -1;
728         else
729                 rdev->desc_nr = sb->this_disk.number;
730
731         if (!refdev) {
732                 ret = 1;
733         } else {
734                 __u64 ev1, ev2;
735                 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
736                 if (!uuid_equal(refsb, sb)) {
737                         printk(KERN_WARNING "md: %s has different UUID to %s\n",
738                                 b, bdevname(refdev->bdev,b2));
739                         goto abort;
740                 }
741                 if (!sb_equal(refsb, sb)) {
742                         printk(KERN_WARNING "md: %s has same UUID"
743                                " but different superblock to %s\n",
744                                b, bdevname(refdev->bdev, b2));
745                         goto abort;
746                 }
747                 ev1 = md_event(sb);
748                 ev2 = md_event(refsb);
749                 if (ev1 > ev2)
750                         ret = 1;
751                 else 
752                         ret = 0;
753         }
754         rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
755
756         if (rdev->size < sb->size && sb->level > 1)
757                 /* "this cannot possibly happen" ... */
758                 ret = -EINVAL;
759
760  abort:
761         return ret;
762 }
763
764 /*
765  * validate_super for 0.90.0
766  */
767 static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
768 {
769         mdp_disk_t *desc;
770         mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
771         __u64 ev1 = md_event(sb);
772
773         rdev->raid_disk = -1;
774         clear_bit(Faulty, &rdev->flags);
775         clear_bit(In_sync, &rdev->flags);
776         clear_bit(WriteMostly, &rdev->flags);
777         clear_bit(BarriersNotsupp, &rdev->flags);
778
779         if (mddev->raid_disks == 0) {
780                 mddev->major_version = 0;
781                 mddev->minor_version = sb->minor_version;
782                 mddev->patch_version = sb->patch_version;
783                 mddev->external = 0;
784                 mddev->chunk_size = sb->chunk_size;
785                 mddev->ctime = sb->ctime;
786                 mddev->utime = sb->utime;
787                 mddev->level = sb->level;
788                 mddev->clevel[0] = 0;
789                 mddev->layout = sb->layout;
790                 mddev->raid_disks = sb->raid_disks;
791                 mddev->size = sb->size;
792                 mddev->events = ev1;
793                 mddev->bitmap_offset = 0;
794                 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
795
796                 if (mddev->minor_version >= 91) {
797                         mddev->reshape_position = sb->reshape_position;
798                         mddev->delta_disks = sb->delta_disks;
799                         mddev->new_level = sb->new_level;
800                         mddev->new_layout = sb->new_layout;
801                         mddev->new_chunk = sb->new_chunk;
802                 } else {
803                         mddev->reshape_position = MaxSector;
804                         mddev->delta_disks = 0;
805                         mddev->new_level = mddev->level;
806                         mddev->new_layout = mddev->layout;
807                         mddev->new_chunk = mddev->chunk_size;
808                 }
809
810                 if (sb->state & (1<<MD_SB_CLEAN))
811                         mddev->recovery_cp = MaxSector;
812                 else {
813                         if (sb->events_hi == sb->cp_events_hi && 
814                                 sb->events_lo == sb->cp_events_lo) {
815                                 mddev->recovery_cp = sb->recovery_cp;
816                         } else
817                                 mddev->recovery_cp = 0;
818                 }
819
820                 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
821                 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
822                 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
823                 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
824
825                 mddev->max_disks = MD_SB_DISKS;
826
827                 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
828                     mddev->bitmap_file == NULL)
829                         mddev->bitmap_offset = mddev->default_bitmap_offset;
830
831         } else if (mddev->pers == NULL) {
832                 /* Insist on good event counter while assembling */
833                 ++ev1;
834                 if (ev1 < mddev->events) 
835                         return -EINVAL;
836         } else if (mddev->bitmap) {
837                 /* if adding to array with a bitmap, then we can accept an
838                  * older device ... but not too old.
839                  */
840                 if (ev1 < mddev->bitmap->events_cleared)
841                         return 0;
842         } else {
843                 if (ev1 < mddev->events)
844                         /* just a hot-add of a new device, leave raid_disk at -1 */
845                         return 0;
846         }
847
848         if (mddev->level != LEVEL_MULTIPATH) {
849                 desc = sb->disks + rdev->desc_nr;
850
851                 if (desc->state & (1<<MD_DISK_FAULTY))
852                         set_bit(Faulty, &rdev->flags);
853                 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
854                             desc->raid_disk < mddev->raid_disks */) {
855                         set_bit(In_sync, &rdev->flags);
856                         rdev->raid_disk = desc->raid_disk;
857                 }
858                 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
859                         set_bit(WriteMostly, &rdev->flags);
860         } else /* MULTIPATH are always insync */
861                 set_bit(In_sync, &rdev->flags);
862         return 0;
863 }
864
865 /*
866  * sync_super for 0.90.0
867  */
868 static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
869 {
870         mdp_super_t *sb;
871         struct list_head *tmp;
872         mdk_rdev_t *rdev2;
873         int next_spare = mddev->raid_disks;
874
875
876         /* make rdev->sb match mddev data..
877          *
878          * 1/ zero out disks
879          * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
880          * 3/ any empty disks < next_spare become removed
881          *
882          * disks[0] gets initialised to REMOVED because
883          * we cannot be sure from other fields if it has
884          * been initialised or not.
885          */
886         int i;
887         int active=0, working=0,failed=0,spare=0,nr_disks=0;
888
889         rdev->sb_size = MD_SB_BYTES;
890
891         sb = (mdp_super_t*)page_address(rdev->sb_page);
892
893         memset(sb, 0, sizeof(*sb));
894
895         sb->md_magic = MD_SB_MAGIC;
896         sb->major_version = mddev->major_version;
897         sb->patch_version = mddev->patch_version;
898         sb->gvalid_words  = 0; /* ignored */
899         memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
900         memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
901         memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
902         memcpy(&sb->set_uuid3, mddev->uuid+12,4);
903
904         sb->ctime = mddev->ctime;
905         sb->level = mddev->level;
906         sb->size  = mddev->size;
907         sb->raid_disks = mddev->raid_disks;
908         sb->md_minor = mddev->md_minor;
909         sb->not_persistent = 0;
910         sb->utime = mddev->utime;
911         sb->state = 0;
912         sb->events_hi = (mddev->events>>32);
913         sb->events_lo = (u32)mddev->events;
914
915         if (mddev->reshape_position == MaxSector)
916                 sb->minor_version = 90;
917         else {
918                 sb->minor_version = 91;
919                 sb->reshape_position = mddev->reshape_position;
920                 sb->new_level = mddev->new_level;
921                 sb->delta_disks = mddev->delta_disks;
922                 sb->new_layout = mddev->new_layout;
923                 sb->new_chunk = mddev->new_chunk;
924         }
925         mddev->minor_version = sb->minor_version;
926         if (mddev->in_sync)
927         {
928                 sb->recovery_cp = mddev->recovery_cp;
929                 sb->cp_events_hi = (mddev->events>>32);
930                 sb->cp_events_lo = (u32)mddev->events;
931                 if (mddev->recovery_cp == MaxSector)
932                         sb->state = (1<< MD_SB_CLEAN);
933         } else
934                 sb->recovery_cp = 0;
935
936         sb->layout = mddev->layout;
937         sb->chunk_size = mddev->chunk_size;
938
939         if (mddev->bitmap && mddev->bitmap_file == NULL)
940                 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
941
942         sb->disks[0].state = (1<<MD_DISK_REMOVED);
943         rdev_for_each(rdev2, tmp, mddev) {
944                 mdp_disk_t *d;
945                 int desc_nr;
946                 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
947                     && !test_bit(Faulty, &rdev2->flags))
948                         desc_nr = rdev2->raid_disk;
949                 else
950                         desc_nr = next_spare++;
951                 rdev2->desc_nr = desc_nr;
952                 d = &sb->disks[rdev2->desc_nr];
953                 nr_disks++;
954                 d->number = rdev2->desc_nr;
955                 d->major = MAJOR(rdev2->bdev->bd_dev);
956                 d->minor = MINOR(rdev2->bdev->bd_dev);
957                 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
958                     && !test_bit(Faulty, &rdev2->flags))
959                         d->raid_disk = rdev2->raid_disk;
960                 else
961                         d->raid_disk = rdev2->desc_nr; /* compatibility */
962                 if (test_bit(Faulty, &rdev2->flags))
963                         d->state = (1<<MD_DISK_FAULTY);
964                 else if (test_bit(In_sync, &rdev2->flags)) {
965                         d->state = (1<<MD_DISK_ACTIVE);
966                         d->state |= (1<<MD_DISK_SYNC);
967                         active++;
968                         working++;
969                 } else {
970                         d->state = 0;
971                         spare++;
972                         working++;
973                 }
974                 if (test_bit(WriteMostly, &rdev2->flags))
975                         d->state |= (1<<MD_DISK_WRITEMOSTLY);
976         }
977         /* now set the "removed" and "faulty" bits on any missing devices */
978         for (i=0 ; i < mddev->raid_disks ; i++) {
979                 mdp_disk_t *d = &sb->disks[i];
980                 if (d->state == 0 && d->number == 0) {
981                         d->number = i;
982                         d->raid_disk = i;
983                         d->state = (1<<MD_DISK_REMOVED);
984                         d->state |= (1<<MD_DISK_FAULTY);
985                         failed++;
986                 }
987         }
988         sb->nr_disks = nr_disks;
989         sb->active_disks = active;
990         sb->working_disks = working;
991         sb->failed_disks = failed;
992         sb->spare_disks = spare;
993
994         sb->this_disk = sb->disks[rdev->desc_nr];
995         sb->sb_csum = calc_sb_csum(sb);
996 }
997
998 /*
999  * rdev_size_change for 0.90.0
1000  */
1001 static unsigned long long
1002 super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1003 {
1004         if (num_sectors && num_sectors < rdev->mddev->size * 2)
1005                 return 0; /* component must fit device */
1006         if (rdev->mddev->bitmap_offset)
1007                 return 0; /* can't move bitmap */
1008         rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1009         if (!num_sectors || num_sectors > rdev->sb_start)
1010                 num_sectors = rdev->sb_start;
1011         md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1012                        rdev->sb_page);
1013         md_super_wait(rdev->mddev);
1014         return num_sectors / 2; /* kB for sysfs */
1015 }
1016
1017
1018 /*
1019  * version 1 superblock
1020  */
1021
1022 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1023 {
1024         __le32 disk_csum;
1025         u32 csum;
1026         unsigned long long newcsum;
1027         int size = 256 + le32_to_cpu(sb->max_dev)*2;
1028         __le32 *isuper = (__le32*)sb;
1029         int i;
1030
1031         disk_csum = sb->sb_csum;
1032         sb->sb_csum = 0;
1033         newcsum = 0;
1034         for (i=0; size>=4; size -= 4 )
1035                 newcsum += le32_to_cpu(*isuper++);
1036
1037         if (size == 2)
1038                 newcsum += le16_to_cpu(*(__le16*) isuper);
1039
1040         csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1041         sb->sb_csum = disk_csum;
1042         return cpu_to_le32(csum);
1043 }
1044
1045 static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1046 {
1047         struct mdp_superblock_1 *sb;
1048         int ret;
1049         sector_t sb_start;
1050         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1051         int bmask;
1052
1053         /*
1054          * Calculate the position of the superblock in 512byte sectors.
1055          * It is always aligned to a 4K boundary and
1056          * depeding on minor_version, it can be:
1057          * 0: At least 8K, but less than 12K, from end of device
1058          * 1: At start of device
1059          * 2: 4K from start of device.
1060          */
1061         switch(minor_version) {
1062         case 0:
1063                 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1064                 sb_start -= 8*2;
1065                 sb_start &= ~(sector_t)(4*2-1);
1066                 break;
1067         case 1:
1068                 sb_start = 0;
1069                 break;
1070         case 2:
1071                 sb_start = 8;
1072                 break;
1073         default:
1074                 return -EINVAL;
1075         }
1076         rdev->sb_start = sb_start;
1077
1078         /* superblock is rarely larger than 1K, but it can be larger,
1079          * and it is safe to read 4k, so we do that
1080          */
1081         ret = read_disk_sb(rdev, 4096);
1082         if (ret) return ret;
1083
1084
1085         sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1086
1087         if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1088             sb->major_version != cpu_to_le32(1) ||
1089             le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1090             le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1091             (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1092                 return -EINVAL;
1093
1094         if (calc_sb_1_csum(sb) != sb->sb_csum) {
1095                 printk("md: invalid superblock checksum on %s\n",
1096                         bdevname(rdev->bdev,b));
1097                 return -EINVAL;
1098         }
1099         if (le64_to_cpu(sb->data_size) < 10) {
1100                 printk("md: data_size too small on %s\n",
1101                        bdevname(rdev->bdev,b));
1102                 return -EINVAL;
1103         }
1104         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1105                 if (sb->level != cpu_to_le32(1) &&
1106                     sb->level != cpu_to_le32(4) &&
1107                     sb->level != cpu_to_le32(5) &&
1108                     sb->level != cpu_to_le32(6) &&
1109                     sb->level != cpu_to_le32(10)) {
1110                         printk(KERN_WARNING
1111                                "md: bitmaps not supported for this level.\n");
1112                         return -EINVAL;
1113                 }
1114         }
1115
1116         rdev->preferred_minor = 0xffff;
1117         rdev->data_offset = le64_to_cpu(sb->data_offset);
1118         atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1119
1120         rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1121         bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
1122         if (rdev->sb_size & bmask)
1123                 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1124
1125         if (minor_version
1126             && rdev->data_offset < sb_start + (rdev->sb_size/512))
1127                 return -EINVAL;
1128
1129         if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1130                 rdev->desc_nr = -1;
1131         else
1132                 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1133
1134         if (!refdev) {
1135                 ret = 1;
1136         } else {
1137                 __u64 ev1, ev2;
1138                 struct mdp_superblock_1 *refsb = 
1139                         (struct mdp_superblock_1*)page_address(refdev->sb_page);
1140
1141                 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1142                     sb->level != refsb->level ||
1143                     sb->layout != refsb->layout ||
1144                     sb->chunksize != refsb->chunksize) {
1145                         printk(KERN_WARNING "md: %s has strangely different"
1146                                 " superblock to %s\n",
1147                                 bdevname(rdev->bdev,b),
1148                                 bdevname(refdev->bdev,b2));
1149                         return -EINVAL;
1150                 }
1151                 ev1 = le64_to_cpu(sb->events);
1152                 ev2 = le64_to_cpu(refsb->events);
1153
1154                 if (ev1 > ev2)
1155                         ret = 1;
1156                 else
1157                         ret = 0;
1158         }
1159         if (minor_version)
1160                 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1161         else
1162                 rdev->size = rdev->sb_start / 2;
1163         if (rdev->size < le64_to_cpu(sb->data_size)/2)
1164                 return -EINVAL;
1165         rdev->size = le64_to_cpu(sb->data_size)/2;
1166         if (le32_to_cpu(sb->chunksize))
1167                 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
1168
1169         if (le64_to_cpu(sb->size) > rdev->size*2)
1170                 return -EINVAL;
1171         return ret;
1172 }
1173
1174 static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1175 {
1176         struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1177         __u64 ev1 = le64_to_cpu(sb->events);
1178
1179         rdev->raid_disk = -1;
1180         clear_bit(Faulty, &rdev->flags);
1181         clear_bit(In_sync, &rdev->flags);
1182         clear_bit(WriteMostly, &rdev->flags);
1183         clear_bit(BarriersNotsupp, &rdev->flags);
1184
1185         if (mddev->raid_disks == 0) {
1186                 mddev->major_version = 1;
1187                 mddev->patch_version = 0;
1188                 mddev->external = 0;
1189                 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1190                 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1191                 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1192                 mddev->level = le32_to_cpu(sb->level);
1193                 mddev->clevel[0] = 0;
1194                 mddev->layout = le32_to_cpu(sb->layout);
1195                 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1196                 mddev->size = le64_to_cpu(sb->size)/2;
1197                 mddev->events = ev1;
1198                 mddev->bitmap_offset = 0;
1199                 mddev->default_bitmap_offset = 1024 >> 9;
1200                 
1201                 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1202                 memcpy(mddev->uuid, sb->set_uuid, 16);
1203
1204                 mddev->max_disks =  (4096-256)/2;
1205
1206                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1207                     mddev->bitmap_file == NULL )
1208                         mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1209
1210                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1211                         mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1212                         mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1213                         mddev->new_level = le32_to_cpu(sb->new_level);
1214                         mddev->new_layout = le32_to_cpu(sb->new_layout);
1215                         mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1216                 } else {
1217                         mddev->reshape_position = MaxSector;
1218                         mddev->delta_disks = 0;
1219                         mddev->new_level = mddev->level;
1220                         mddev->new_layout = mddev->layout;
1221                         mddev->new_chunk = mddev->chunk_size;
1222                 }
1223
1224         } else if (mddev->pers == NULL) {
1225                 /* Insist of good event counter while assembling */
1226                 ++ev1;
1227                 if (ev1 < mddev->events)
1228                         return -EINVAL;
1229         } else if (mddev->bitmap) {
1230                 /* If adding to array with a bitmap, then we can accept an
1231                  * older device, but not too old.
1232                  */
1233                 if (ev1 < mddev->bitmap->events_cleared)
1234                         return 0;
1235         } else {
1236                 if (ev1 < mddev->events)
1237                         /* just a hot-add of a new device, leave raid_disk at -1 */
1238                         return 0;
1239         }
1240         if (mddev->level != LEVEL_MULTIPATH) {
1241                 int role;
1242                 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1243                 switch(role) {
1244                 case 0xffff: /* spare */
1245                         break;
1246                 case 0xfffe: /* faulty */
1247                         set_bit(Faulty, &rdev->flags);
1248                         break;
1249                 default:
1250                         if ((le32_to_cpu(sb->feature_map) &
1251                              MD_FEATURE_RECOVERY_OFFSET))
1252                                 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1253                         else
1254                                 set_bit(In_sync, &rdev->flags);
1255                         rdev->raid_disk = role;
1256                         break;
1257                 }
1258                 if (sb->devflags & WriteMostly1)
1259                         set_bit(WriteMostly, &rdev->flags);
1260         } else /* MULTIPATH are always insync */
1261                 set_bit(In_sync, &rdev->flags);
1262
1263         return 0;
1264 }
1265
1266 static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1267 {
1268         struct mdp_superblock_1 *sb;
1269         struct list_head *tmp;
1270         mdk_rdev_t *rdev2;
1271         int max_dev, i;
1272         /* make rdev->sb match mddev and rdev data. */
1273
1274         sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1275
1276         sb->feature_map = 0;
1277         sb->pad0 = 0;
1278         sb->recovery_offset = cpu_to_le64(0);
1279         memset(sb->pad1, 0, sizeof(sb->pad1));
1280         memset(sb->pad2, 0, sizeof(sb->pad2));
1281         memset(sb->pad3, 0, sizeof(sb->pad3));
1282
1283         sb->utime = cpu_to_le64((__u64)mddev->utime);
1284         sb->events = cpu_to_le64(mddev->events);
1285         if (mddev->in_sync)
1286                 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1287         else
1288                 sb->resync_offset = cpu_to_le64(0);
1289
1290         sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1291
1292         sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1293         sb->size = cpu_to_le64(mddev->size<<1);
1294
1295         if (mddev->bitmap && mddev->bitmap_file == NULL) {
1296                 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
1297                 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1298         }
1299
1300         if (rdev->raid_disk >= 0 &&
1301             !test_bit(In_sync, &rdev->flags) &&
1302             rdev->recovery_offset > 0) {
1303                 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1304                 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1305         }
1306
1307         if (mddev->reshape_position != MaxSector) {
1308                 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1309                 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1310                 sb->new_layout = cpu_to_le32(mddev->new_layout);
1311                 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1312                 sb->new_level = cpu_to_le32(mddev->new_level);
1313                 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1314         }
1315
1316         max_dev = 0;
1317         rdev_for_each(rdev2, tmp, mddev)
1318                 if (rdev2->desc_nr+1 > max_dev)
1319                         max_dev = rdev2->desc_nr+1;
1320
1321         if (max_dev > le32_to_cpu(sb->max_dev))
1322                 sb->max_dev = cpu_to_le32(max_dev);
1323         for (i=0; i<max_dev;i++)
1324                 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1325         
1326         rdev_for_each(rdev2, tmp, mddev) {
1327                 i = rdev2->desc_nr;
1328                 if (test_bit(Faulty, &rdev2->flags))
1329                         sb->dev_roles[i] = cpu_to_le16(0xfffe);
1330                 else if (test_bit(In_sync, &rdev2->flags))
1331                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1332                 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1333                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1334                 else
1335                         sb->dev_roles[i] = cpu_to_le16(0xffff);
1336         }
1337
1338         sb->sb_csum = calc_sb_1_csum(sb);
1339 }
1340
1341 static unsigned long long
1342 super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
1343 {
1344         struct mdp_superblock_1 *sb;
1345         sector_t max_sectors;
1346         if (num_sectors && num_sectors < rdev->mddev->size * 2)
1347                 return 0; /* component must fit device */
1348         if (rdev->sb_start < rdev->data_offset) {
1349                 /* minor versions 1 and 2; superblock before data */
1350                 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1351                 max_sectors -= rdev->data_offset;
1352                 if (!num_sectors || num_sectors > max_sectors)
1353                         num_sectors = max_sectors;
1354         } else if (rdev->mddev->bitmap_offset) {
1355                 /* minor version 0 with bitmap we can't move */
1356                 return 0;
1357         } else {
1358                 /* minor version 0; superblock after data */
1359                 sector_t sb_start;
1360                 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1361                 sb_start &= ~(sector_t)(4*2 - 1);
1362                 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1363                 if (!num_sectors || num_sectors > max_sectors)
1364                         num_sectors = max_sectors;
1365                 rdev->sb_start = sb_start;
1366         }
1367         sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1368         sb->data_size = cpu_to_le64(num_sectors);
1369         sb->super_offset = rdev->sb_start;
1370         sb->sb_csum = calc_sb_1_csum(sb);
1371         md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1372                        rdev->sb_page);
1373         md_super_wait(rdev->mddev);
1374         return num_sectors / 2; /* kB for sysfs */
1375 }
1376
1377 static struct super_type super_types[] = {
1378         [0] = {
1379                 .name   = "0.90.0",
1380                 .owner  = THIS_MODULE,
1381                 .load_super         = super_90_load,
1382                 .validate_super     = super_90_validate,
1383                 .sync_super         = super_90_sync,
1384                 .rdev_size_change   = super_90_rdev_size_change,
1385         },
1386         [1] = {
1387                 .name   = "md-1",
1388                 .owner  = THIS_MODULE,
1389                 .load_super         = super_1_load,
1390                 .validate_super     = super_1_validate,
1391                 .sync_super         = super_1_sync,
1392                 .rdev_size_change   = super_1_rdev_size_change,
1393         },
1394 };
1395
1396 static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1397 {
1398         mdk_rdev_t *rdev, *rdev2;
1399
1400         rcu_read_lock();
1401         rdev_for_each_rcu(rdev, mddev1)
1402                 rdev_for_each_rcu(rdev2, mddev2)
1403                         if (rdev->bdev->bd_contains ==
1404                             rdev2->bdev->bd_contains) {
1405                                 rcu_read_unlock();
1406                                 return 1;
1407                         }
1408         rcu_read_unlock();
1409         return 0;
1410 }
1411
1412 static LIST_HEAD(pending_raid_disks);
1413
1414 static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1415 {
1416         char b[BDEVNAME_SIZE];
1417         struct kobject *ko;
1418         char *s;
1419         int err;
1420
1421         if (rdev->mddev) {
1422                 MD_BUG();
1423                 return -EINVAL;
1424         }
1425
1426         /* prevent duplicates */
1427         if (find_rdev(mddev, rdev->bdev->bd_dev))
1428                 return -EEXIST;
1429
1430         /* make sure rdev->size exceeds mddev->size */
1431         if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1432                 if (mddev->pers) {
1433                         /* Cannot change size, so fail
1434                          * If mddev->level <= 0, then we don't care
1435                          * about aligning sizes (e.g. linear)
1436                          */
1437                         if (mddev->level > 0)
1438                                 return -ENOSPC;
1439                 } else
1440                         mddev->size = rdev->size;
1441         }
1442
1443         /* Verify rdev->desc_nr is unique.
1444          * If it is -1, assign a free number, else
1445          * check number is not in use
1446          */
1447         if (rdev->desc_nr < 0) {
1448                 int choice = 0;
1449                 if (mddev->pers) choice = mddev->raid_disks;
1450                 while (find_rdev_nr(mddev, choice))
1451                         choice++;
1452                 rdev->desc_nr = choice;
1453         } else {
1454                 if (find_rdev_nr(mddev, rdev->desc_nr))
1455                         return -EBUSY;
1456         }
1457         bdevname(rdev->bdev,b);
1458         while ( (s=strchr(b, '/')) != NULL)
1459                 *s = '!';
1460
1461         rdev->mddev = mddev;
1462         printk(KERN_INFO "md: bind<%s>\n", b);
1463
1464         if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
1465                 goto fail;
1466
1467         if (rdev->bdev->bd_part)
1468                 ko = &rdev->bdev->bd_part->dev.kobj;
1469         else
1470                 ko = &rdev->bdev->bd_disk->dev.kobj;
1471         if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1472                 kobject_del(&rdev->kobj);
1473                 goto fail;
1474         }
1475         list_add_rcu(&rdev->same_set, &mddev->disks);
1476         bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
1477         return 0;
1478
1479  fail:
1480         printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1481                b, mdname(mddev));
1482         return err;
1483 }
1484
1485 static void md_delayed_delete(struct work_struct *ws)
1486 {
1487         mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1488         kobject_del(&rdev->kobj);
1489         kobject_put(&rdev->kobj);
1490 }
1491
1492 static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1493 {
1494         char b[BDEVNAME_SIZE];
1495         if (!rdev->mddev) {
1496                 MD_BUG();
1497                 return;
1498         }
1499         bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1500         list_del_rcu(&rdev->same_set);
1501         printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1502         rdev->mddev = NULL;
1503         sysfs_remove_link(&rdev->kobj, "block");
1504
1505         /* We need to delay this, otherwise we can deadlock when
1506          * writing to 'remove' to "dev/state".  We also need
1507          * to delay it due to rcu usage.
1508          */
1509         synchronize_rcu();
1510         INIT_WORK(&rdev->del_work, md_delayed_delete);
1511         kobject_get(&rdev->kobj);
1512         schedule_work(&rdev->del_work);
1513 }
1514
1515 /*
1516  * prevent the device from being mounted, repartitioned or
1517  * otherwise reused by a RAID array (or any other kernel
1518  * subsystem), by bd_claiming the device.
1519  */
1520 static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1521 {
1522         int err = 0;
1523         struct block_device *bdev;
1524         char b[BDEVNAME_SIZE];
1525
1526         bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1527         if (IS_ERR(bdev)) {
1528                 printk(KERN_ERR "md: could not open %s.\n",
1529                         __bdevname(dev, b));
1530                 return PTR_ERR(bdev);
1531         }
1532         err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1533         if (err) {
1534                 printk(KERN_ERR "md: could not bd_claim %s.\n",
1535                         bdevname(bdev, b));
1536                 blkdev_put(bdev);
1537                 return err;
1538         }
1539         if (!shared)
1540                 set_bit(AllReserved, &rdev->flags);
1541         rdev->bdev = bdev;
1542         return err;
1543 }
1544
1545 static void unlock_rdev(mdk_rdev_t *rdev)
1546 {
1547         struct block_device *bdev = rdev->bdev;
1548         rdev->bdev = NULL;
1549         if (!bdev)
1550                 MD_BUG();
1551         bd_release(bdev);
1552         blkdev_put(bdev);
1553 }
1554
1555 void md_autodetect_dev(dev_t dev);
1556
1557 static void export_rdev(mdk_rdev_t * rdev)
1558 {
1559         char b[BDEVNAME_SIZE];
1560         printk(KERN_INFO "md: export_rdev(%s)\n",
1561                 bdevname(rdev->bdev,b));
1562         if (rdev->mddev)
1563                 MD_BUG();
1564         free_disk_sb(rdev);
1565 #ifndef MODULE
1566         if (test_bit(AutoDetected, &rdev->flags))
1567                 md_autodetect_dev(rdev->bdev->bd_dev);
1568 #endif
1569         unlock_rdev(rdev);
1570         kobject_put(&rdev->kobj);
1571 }
1572
1573 static void kick_rdev_from_array(mdk_rdev_t * rdev)
1574 {
1575         unbind_rdev_from_array(rdev);
1576         export_rdev(rdev);
1577 }
1578
1579 static void export_array(mddev_t *mddev)
1580 {
1581         struct list_head *tmp;
1582         mdk_rdev_t *rdev;
1583
1584         rdev_for_each(rdev, tmp, mddev) {
1585                 if (!rdev->mddev) {
1586                         MD_BUG();
1587                         continue;
1588                 }
1589                 kick_rdev_from_array(rdev);
1590         }
1591         if (!list_empty(&mddev->disks))
1592                 MD_BUG();
1593         mddev->raid_disks = 0;
1594         mddev->major_version = 0;
1595 }
1596
1597 static void print_desc(mdp_disk_t *desc)
1598 {
1599         printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1600                 desc->major,desc->minor,desc->raid_disk,desc->state);
1601 }
1602
1603 static void print_sb(mdp_super_t *sb)
1604 {
1605         int i;
1606
1607         printk(KERN_INFO 
1608                 "md:  SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1609                 sb->major_version, sb->minor_version, sb->patch_version,
1610                 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1611                 sb->ctime);
1612         printk(KERN_INFO "md:     L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1613                 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1614                 sb->md_minor, sb->layout, sb->chunk_size);
1615         printk(KERN_INFO "md:     UT:%08x ST:%d AD:%d WD:%d"
1616                 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1617                 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1618                 sb->failed_disks, sb->spare_disks,
1619                 sb->sb_csum, (unsigned long)sb->events_lo);
1620
1621         printk(KERN_INFO);
1622         for (i = 0; i < MD_SB_DISKS; i++) {
1623                 mdp_disk_t *desc;
1624
1625                 desc = sb->disks + i;
1626                 if (desc->number || desc->major || desc->minor ||
1627                     desc->raid_disk || (desc->state && (desc->state != 4))) {
1628                         printk("     D %2d: ", i);
1629                         print_desc(desc);
1630                 }
1631         }
1632         printk(KERN_INFO "md:     THIS: ");
1633         print_desc(&sb->this_disk);
1634
1635 }
1636
1637 static void print_rdev(mdk_rdev_t *rdev)
1638 {
1639         char b[BDEVNAME_SIZE];
1640         printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1641                 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
1642                 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1643                 rdev->desc_nr);
1644         if (rdev->sb_loaded) {
1645                 printk(KERN_INFO "md: rdev superblock:\n");
1646                 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1647         } else
1648                 printk(KERN_INFO "md: no rdev superblock!\n");
1649 }
1650
1651 static void md_print_devices(void)
1652 {
1653         struct list_head *tmp, *tmp2;
1654         mdk_rdev_t *rdev;
1655         mddev_t *mddev;
1656         char b[BDEVNAME_SIZE];
1657
1658         printk("\n");
1659         printk("md:     **********************************\n");
1660         printk("md:     * <COMPLETE RAID STATE PRINTOUT> *\n");
1661         printk("md:     **********************************\n");
1662         for_each_mddev(mddev, tmp) {
1663
1664                 if (mddev->bitmap)
1665                         bitmap_print_sb(mddev->bitmap);
1666                 else
1667                         printk("%s: ", mdname(mddev));
1668                 rdev_for_each(rdev, tmp2, mddev)
1669                         printk("<%s>", bdevname(rdev->bdev,b));
1670                 printk("\n");
1671
1672                 rdev_for_each(rdev, tmp2, mddev)
1673                         print_rdev(rdev);
1674         }
1675         printk("md:     **********************************\n");
1676         printk("\n");
1677 }
1678
1679
1680 static void sync_sbs(mddev_t * mddev, int nospares)
1681 {
1682         /* Update each superblock (in-memory image), but
1683          * if we are allowed to, skip spares which already
1684          * have the right event counter, or have one earlier
1685          * (which would mean they aren't being marked as dirty
1686          * with the rest of the array)
1687          */
1688         mdk_rdev_t *rdev;
1689         struct list_head *tmp;
1690
1691         rdev_for_each(rdev, tmp, mddev) {
1692                 if (rdev->sb_events == mddev->events ||
1693                     (nospares &&
1694                      rdev->raid_disk < 0 &&
1695                      (rdev->sb_events&1)==0 &&
1696                      rdev->sb_events+1 == mddev->events)) {
1697                         /* Don't update this superblock */
1698                         rdev->sb_loaded = 2;
1699                 } else {
1700                         super_types[mddev->major_version].
1701                                 sync_super(mddev, rdev);
1702                         rdev->sb_loaded = 1;
1703                 }
1704         }
1705 }
1706
1707 static void md_update_sb(mddev_t * mddev, int force_change)
1708 {
1709         struct list_head *tmp;
1710         mdk_rdev_t *rdev;
1711         int sync_req;
1712         int nospares = 0;
1713
1714         if (mddev->external)
1715                 return;
1716 repeat:
1717         spin_lock_irq(&mddev->write_lock);
1718
1719         set_bit(MD_CHANGE_PENDING, &mddev->flags);
1720         if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1721                 force_change = 1;
1722         if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1723                 /* just a clean<-> dirty transition, possibly leave spares alone,
1724                  * though if events isn't the right even/odd, we will have to do
1725                  * spares after all
1726                  */
1727                 nospares = 1;
1728         if (force_change)
1729                 nospares = 0;
1730         if (mddev->degraded)
1731                 /* If the array is degraded, then skipping spares is both
1732                  * dangerous and fairly pointless.
1733                  * Dangerous because a device that was removed from the array
1734                  * might have a event_count that still looks up-to-date,
1735                  * so it can be re-added without a resync.
1736                  * Pointless because if there are any spares to skip,
1737                  * then a recovery will happen and soon that array won't
1738                  * be degraded any more and the spare can go back to sleep then.
1739                  */
1740                 nospares = 0;
1741
1742         sync_req = mddev->in_sync;
1743         mddev->utime = get_seconds();
1744
1745         /* If this is just a dirty<->clean transition, and the array is clean
1746          * and 'events' is odd, we can roll back to the previous clean state */
1747         if (nospares
1748             && (mddev->in_sync && mddev->recovery_cp == MaxSector)
1749             && (mddev->events & 1)
1750             && mddev->events != 1)
1751                 mddev->events--;
1752         else {
1753                 /* otherwise we have to go forward and ... */
1754                 mddev->events ++;
1755                 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1756                         /* .. if the array isn't clean, insist on an odd 'events' */
1757                         if ((mddev->events&1)==0) {
1758                                 mddev->events++;
1759                                 nospares = 0;
1760                         }
1761                 } else {
1762                         /* otherwise insist on an even 'events' (for clean states) */
1763                         if ((mddev->events&1)) {
1764                                 mddev->events++;
1765                                 nospares = 0;
1766                         }
1767                 }
1768         }
1769
1770         if (!mddev->events) {
1771                 /*
1772                  * oops, this 64-bit counter should never wrap.
1773                  * Either we are in around ~1 trillion A.C., assuming
1774                  * 1 reboot per second, or we have a bug:
1775                  */
1776                 MD_BUG();
1777                 mddev->events --;
1778         }
1779
1780         /*
1781          * do not write anything to disk if using
1782          * nonpersistent superblocks
1783          */
1784         if (!mddev->persistent) {
1785                 if (!mddev->external)
1786                         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1787
1788                 spin_unlock_irq(&mddev->write_lock);
1789                 wake_up(&mddev->sb_wait);
1790                 return;
1791         }
1792         sync_sbs(mddev, nospares);
1793         spin_unlock_irq(&mddev->write_lock);
1794
1795         dprintk(KERN_INFO 
1796                 "md: updating %s RAID superblock on device (in sync %d)\n",
1797                 mdname(mddev),mddev->in_sync);
1798
1799         bitmap_update_sb(mddev->bitmap);
1800         rdev_for_each(rdev, tmp, mddev) {
1801                 char b[BDEVNAME_SIZE];
1802                 dprintk(KERN_INFO "md: ");
1803                 if (rdev->sb_loaded != 1)
1804                         continue; /* no noise on spare devices */
1805                 if (test_bit(Faulty, &rdev->flags))
1806                         dprintk("(skipping faulty ");
1807
1808                 dprintk("%s ", bdevname(rdev->bdev,b));
1809                 if (!test_bit(Faulty, &rdev->flags)) {
1810                         md_super_write(mddev,rdev,
1811                                        rdev->sb_start, rdev->sb_size,
1812                                        rdev->sb_page);
1813                         dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1814                                 bdevname(rdev->bdev,b),
1815                                 (unsigned long long)rdev->sb_start);
1816                         rdev->sb_events = mddev->events;
1817
1818                 } else
1819                         dprintk(")\n");
1820                 if (mddev->level == LEVEL_MULTIPATH)
1821                         /* only need to write one superblock... */
1822                         break;
1823         }
1824         md_super_wait(mddev);
1825         /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
1826
1827         spin_lock_irq(&mddev->write_lock);
1828         if (mddev->in_sync != sync_req ||
1829             test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
1830                 /* have to write it out again */
1831                 spin_unlock_irq(&mddev->write_lock);
1832                 goto repeat;
1833         }
1834         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1835         spin_unlock_irq(&mddev->write_lock);
1836         wake_up(&mddev->sb_wait);
1837
1838 }
1839
1840 /* words written to sysfs files may, or may not, be \n terminated.
1841  * We want to accept with case. For this we use cmd_match.
1842  */
1843 static int cmd_match(const char *cmd, const char *str)
1844 {
1845         /* See if cmd, written into a sysfs file, matches
1846          * str.  They must either be the same, or cmd can
1847          * have a trailing newline
1848          */
1849         while (*cmd && *str && *cmd == *str) {
1850                 cmd++;
1851                 str++;
1852         }
1853         if (*cmd == '\n')
1854                 cmd++;
1855         if (*str || *cmd)
1856                 return 0;
1857         return 1;
1858 }
1859
1860 struct rdev_sysfs_entry {
1861         struct attribute attr;
1862         ssize_t (*show)(mdk_rdev_t *, char *);
1863         ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1864 };
1865
1866 static ssize_t
1867 state_show(mdk_rdev_t *rdev, char *page)
1868 {
1869         char *sep = "";
1870         size_t len = 0;
1871
1872         if (test_bit(Faulty, &rdev->flags)) {
1873                 len+= sprintf(page+len, "%sfaulty",sep);
1874                 sep = ",";
1875         }
1876         if (test_bit(In_sync, &rdev->flags)) {
1877                 len += sprintf(page+len, "%sin_sync",sep);
1878                 sep = ",";
1879         }
1880         if (test_bit(WriteMostly, &rdev->flags)) {
1881                 len += sprintf(page+len, "%swrite_mostly",sep);
1882                 sep = ",";
1883         }
1884         if (test_bit(Blocked, &rdev->flags)) {
1885                 len += sprintf(page+len, "%sblocked", sep);
1886                 sep = ",";
1887         }
1888         if (!test_bit(Faulty, &rdev->flags) &&
1889             !test_bit(In_sync, &rdev->flags)) {
1890                 len += sprintf(page+len, "%sspare", sep);
1891                 sep = ",";
1892         }
1893         return len+sprintf(page+len, "\n");
1894 }
1895
1896 static ssize_t
1897 state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1898 {
1899         /* can write
1900          *  faulty  - simulates and error
1901          *  remove  - disconnects the device
1902          *  writemostly - sets write_mostly
1903          *  -writemostly - clears write_mostly
1904          *  blocked - sets the Blocked flag
1905          *  -blocked - clears the Blocked flag
1906          */
1907         int err = -EINVAL;
1908         if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1909                 md_error(rdev->mddev, rdev);
1910                 err = 0;
1911         } else if (cmd_match(buf, "remove")) {
1912                 if (rdev->raid_disk >= 0)
1913                         err = -EBUSY;
1914                 else {
1915                         mddev_t *mddev = rdev->mddev;
1916                         kick_rdev_from_array(rdev);
1917                         if (mddev->pers)
1918                                 md_update_sb(mddev, 1);
1919                         md_new_event(mddev);
1920                         err = 0;
1921                 }
1922         } else if (cmd_match(buf, "writemostly")) {
1923                 set_bit(WriteMostly, &rdev->flags);
1924                 err = 0;
1925         } else if (cmd_match(buf, "-writemostly")) {
1926                 clear_bit(WriteMostly, &rdev->flags);
1927                 err = 0;
1928         } else if (cmd_match(buf, "blocked")) {
1929                 set_bit(Blocked, &rdev->flags);
1930                 err = 0;
1931         } else if (cmd_match(buf, "-blocked")) {
1932                 clear_bit(Blocked, &rdev->flags);
1933                 wake_up(&rdev->blocked_wait);
1934                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1935                 md_wakeup_thread(rdev->mddev->thread);
1936
1937                 err = 0;
1938         }
1939         if (!err)
1940                 sysfs_notify(&rdev->kobj, NULL, "state");
1941         return err ? err : len;
1942 }
1943 static struct rdev_sysfs_entry rdev_state =
1944 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
1945
1946 static ssize_t
1947 errors_show(mdk_rdev_t *rdev, char *page)
1948 {
1949         return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1950 }
1951
1952 static ssize_t
1953 errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1954 {
1955         char *e;
1956         unsigned long n = simple_strtoul(buf, &e, 10);
1957         if (*buf && (*e == 0 || *e == '\n')) {
1958                 atomic_set(&rdev->corrected_errors, n);
1959                 return len;
1960         }
1961         return -EINVAL;
1962 }
1963 static struct rdev_sysfs_entry rdev_errors =
1964 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
1965
1966 static ssize_t
1967 slot_show(mdk_rdev_t *rdev, char *page)
1968 {
1969         if (rdev->raid_disk < 0)
1970                 return sprintf(page, "none\n");
1971         else
1972                 return sprintf(page, "%d\n", rdev->raid_disk);
1973 }
1974
1975 static ssize_t
1976 slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1977 {
1978         char *e;
1979         int err;
1980         char nm[20];
1981         int slot = simple_strtoul(buf, &e, 10);
1982         if (strncmp(buf, "none", 4)==0)
1983                 slot = -1;
1984         else if (e==buf || (*e && *e!= '\n'))
1985                 return -EINVAL;
1986         if (rdev->mddev->pers && slot == -1) {
1987                 /* Setting 'slot' on an active array requires also
1988                  * updating the 'rd%d' link, and communicating
1989                  * with the personality with ->hot_*_disk.
1990                  * For now we only support removing
1991                  * failed/spare devices.  This normally happens automatically,
1992                  * but not when the metadata is externally managed.
1993                  */
1994                 if (rdev->raid_disk == -1)
1995                         return -EEXIST;
1996                 /* personality does all needed checks */
1997                 if (rdev->mddev->pers->hot_add_disk == NULL)
1998                         return -EINVAL;
1999                 err = rdev->mddev->pers->
2000                         hot_remove_disk(rdev->mddev, rdev->raid_disk);
2001                 if (err)
2002                         return err;
2003                 sprintf(nm, "rd%d", rdev->raid_disk);
2004                 sysfs_remove_link(&rdev->mddev->kobj, nm);
2005                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2006                 md_wakeup_thread(rdev->mddev->thread);
2007         } else if (rdev->mddev->pers) {
2008                 mdk_rdev_t *rdev2;
2009                 struct list_head *tmp;
2010                 /* Activating a spare .. or possibly reactivating
2011                  * if we every get bitmaps working here.
2012                  */
2013
2014                 if (rdev->raid_disk != -1)
2015                         return -EBUSY;
2016
2017                 if (rdev->mddev->pers->hot_add_disk == NULL)
2018                         return -EINVAL;
2019
2020                 rdev_for_each(rdev2, tmp, rdev->mddev)
2021                         if (rdev2->raid_disk == slot)
2022                                 return -EEXIST;
2023
2024                 rdev->raid_disk = slot;
2025                 if (test_bit(In_sync, &rdev->flags))
2026                         rdev->saved_raid_disk = slot;
2027                 else
2028                         rdev->saved_raid_disk = -1;
2029                 err = rdev->mddev->pers->
2030                         hot_add_disk(rdev->mddev, rdev);
2031                 if (err) {
2032                         rdev->raid_disk = -1;
2033                         return err;
2034                 } else
2035                         sysfs_notify(&rdev->kobj, NULL, "state");
2036                 sprintf(nm, "rd%d", rdev->raid_disk);
2037                 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2038                         printk(KERN_WARNING
2039                                "md: cannot register "
2040                                "%s for %s\n",
2041                                nm, mdname(rdev->mddev));
2042
2043                 /* don't wakeup anyone, leave that to userspace. */
2044         } else {
2045                 if (slot >= rdev->mddev->raid_disks)
2046                         return -ENOSPC;
2047                 rdev->raid_disk = slot;
2048                 /* assume it is working */
2049                 clear_bit(Faulty, &rdev->flags);
2050                 clear_bit(WriteMostly, &rdev->flags);
2051                 set_bit(In_sync, &rdev->flags);
2052                 sysfs_notify(&rdev->kobj, NULL, "state");
2053         }
2054         return len;
2055 }
2056
2057
2058 static struct rdev_sysfs_entry rdev_slot =
2059 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2060
2061 static ssize_t
2062 offset_show(mdk_rdev_t *rdev, char *page)
2063 {
2064         return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2065 }
2066
2067 static ssize_t
2068 offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2069 {
2070         char *e;
2071         unsigned long long offset = simple_strtoull(buf, &e, 10);
2072         if (e==buf || (*e && *e != '\n'))
2073                 return -EINVAL;
2074         if (rdev->mddev->pers && rdev->raid_disk >= 0)
2075                 return -EBUSY;
2076         if (rdev->size && rdev->mddev->external)
2077                 /* Must set offset before size, so overlap checks
2078                  * can be sane */
2079                 return -EBUSY;
2080         rdev->data_offset = offset;
2081         return len;
2082 }
2083
2084 static struct rdev_sysfs_entry rdev_offset =
2085 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2086
2087 static ssize_t
2088 rdev_size_show(mdk_rdev_t *rdev, char *page)
2089 {
2090         return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2091 }
2092
2093 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2094 {
2095         /* check if two start/length pairs overlap */
2096         if (s1+l1 <= s2)
2097                 return 0;
2098         if (s2+l2 <= s1)
2099                 return 0;
2100         return 1;
2101 }
2102
2103 static ssize_t
2104 rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2105 {
2106         unsigned long long size;
2107         unsigned long long oldsize = rdev->size;
2108         mddev_t *my_mddev = rdev->mddev;
2109
2110         if (strict_strtoull(buf, 10, &size) < 0)
2111                 return -EINVAL;
2112         if (size < my_mddev->size)
2113                 return -EINVAL;
2114         if (my_mddev->pers && rdev->raid_disk >= 0) {
2115                 if (my_mddev->persistent) {
2116                         size = super_types[my_mddev->major_version].
2117                                 rdev_size_change(rdev, size * 2);
2118                         if (!size)
2119                                 return -EBUSY;
2120                 } else if (!size) {
2121                         size = (rdev->bdev->bd_inode->i_size >> 10);
2122                         size -= rdev->data_offset/2;
2123                 }
2124                 if (size < my_mddev->size)
2125                         return -EINVAL; /* component must fit device */
2126         }
2127
2128         rdev->size = size;
2129         if (size > oldsize && my_mddev->external) {
2130                 /* need to check that all other rdevs with the same ->bdev
2131                  * do not overlap.  We need to unlock the mddev to avoid
2132                  * a deadlock.  We have already changed rdev->size, and if
2133                  * we have to change it back, we will have the lock again.
2134                  */
2135                 mddev_t *mddev;
2136                 int overlap = 0;
2137                 struct list_head *tmp, *tmp2;
2138
2139                 mddev_unlock(my_mddev);
2140                 for_each_mddev(mddev, tmp) {
2141                         mdk_rdev_t *rdev2;
2142
2143                         mddev_lock(mddev);
2144                         rdev_for_each(rdev2, tmp2, mddev)
2145                                 if (test_bit(AllReserved, &rdev2->flags) ||
2146                                     (rdev->bdev == rdev2->bdev &&
2147                                      rdev != rdev2 &&
2148                                      overlaps(rdev->data_offset, rdev->size * 2,
2149                                               rdev2->data_offset,
2150                                               rdev2->size * 2))) {
2151                                         overlap = 1;
2152                                         break;
2153                                 }
2154                         mddev_unlock(mddev);
2155                         if (overlap) {
2156                                 mddev_put(mddev);
2157                                 break;
2158                         }
2159                 }
2160                 mddev_lock(my_mddev);
2161                 if (overlap) {
2162                         /* Someone else could have slipped in a size
2163                          * change here, but doing so is just silly.
2164                          * We put oldsize back because we *know* it is
2165                          * safe, and trust userspace not to race with
2166                          * itself
2167                          */
2168                         rdev->size = oldsize;
2169                         return -EBUSY;
2170                 }
2171         }
2172         return len;
2173 }
2174
2175 static struct rdev_sysfs_entry rdev_size =
2176 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
2177
2178 static struct attribute *rdev_default_attrs[] = {
2179         &rdev_state.attr,
2180         &rdev_errors.attr,
2181         &rdev_slot.attr,
2182         &rdev_offset.attr,
2183         &rdev_size.attr,
2184         NULL,
2185 };
2186 static ssize_t
2187 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2188 {
2189         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2190         mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2191         mddev_t *mddev = rdev->mddev;
2192         ssize_t rv;
2193
2194         if (!entry->show)
2195                 return -EIO;
2196
2197         rv = mddev ? mddev_lock(mddev) : -EBUSY;
2198         if (!rv) {
2199                 if (rdev->mddev == NULL)
2200                         rv = -EBUSY;
2201                 else
2202                         rv = entry->show(rdev, page);
2203                 mddev_unlock(mddev);
2204         }
2205         return rv;
2206 }
2207
2208 static ssize_t
2209 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2210               const char *page, size_t length)
2211 {
2212         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2213         mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2214         ssize_t rv;
2215         mddev_t *mddev = rdev->mddev;
2216
2217         if (!entry->store)
2218                 return -EIO;
2219         if (!capable(CAP_SYS_ADMIN))
2220                 return -EACCES;
2221         rv = mddev ? mddev_lock(mddev): -EBUSY;
2222         if (!rv) {
2223                 if (rdev->mddev == NULL)
2224                         rv = -EBUSY;
2225                 else
2226                         rv = entry->store(rdev, page, length);
2227                 mddev_unlock(mddev);
2228         }
2229         return rv;
2230 }
2231
2232 static void rdev_free(struct kobject *ko)
2233 {
2234         mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2235         kfree(rdev);
2236 }
2237 static struct sysfs_ops rdev_sysfs_ops = {
2238         .show           = rdev_attr_show,
2239         .store          = rdev_attr_store,
2240 };
2241 static struct kobj_type rdev_ktype = {
2242         .release        = rdev_free,
2243         .sysfs_ops      = &rdev_sysfs_ops,
2244         .default_attrs  = rdev_default_attrs,
2245 };
2246
2247 /*
2248  * Import a device. If 'super_format' >= 0, then sanity check the superblock
2249  *
2250  * mark the device faulty if:
2251  *
2252  *   - the device is nonexistent (zero size)
2253  *   - the device has no valid superblock
2254  *
2255  * a faulty rdev _never_ has rdev->sb set.
2256  */
2257 static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2258 {
2259         char b[BDEVNAME_SIZE];
2260         int err;
2261         mdk_rdev_t *rdev;
2262         sector_t size;
2263
2264         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2265         if (!rdev) {
2266                 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2267                 return ERR_PTR(-ENOMEM);
2268         }
2269
2270         if ((err = alloc_disk_sb(rdev)))
2271                 goto abort_free;
2272
2273         err = lock_rdev(rdev, newdev, super_format == -2);
2274         if (err)
2275                 goto abort_free;
2276
2277         kobject_init(&rdev->kobj, &rdev_ktype);
2278
2279         rdev->desc_nr = -1;
2280         rdev->saved_raid_disk = -1;
2281         rdev->raid_disk = -1;
2282         rdev->flags = 0;
2283         rdev->data_offset = 0;
2284         rdev->sb_events = 0;
2285         atomic_set(&rdev->nr_pending, 0);
2286         atomic_set(&rdev->read_errors, 0);
2287         atomic_set(&rdev->corrected_errors, 0);
2288
2289         size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2290         if (!size) {
2291                 printk(KERN_WARNING 
2292                         "md: %s has zero or unknown size, marking faulty!\n",
2293                         bdevname(rdev->bdev,b));
2294                 err = -EINVAL;
2295                 goto abort_free;
2296         }
2297
2298         if (super_format >= 0) {
2299                 err = super_types[super_format].
2300                         load_super(rdev, NULL, super_minor);
2301                 if (err == -EINVAL) {
2302                         printk(KERN_WARNING
2303                                 "md: %s does not have a valid v%d.%d "
2304                                "superblock, not importing!\n",
2305                                 bdevname(rdev->bdev,b),
2306                                super_format, super_minor);
2307                         goto abort_free;
2308                 }
2309                 if (err < 0) {
2310                         printk(KERN_WARNING 
2311                                 "md: could not read %s's sb, not importing!\n",
2312                                 bdevname(rdev->bdev,b));
2313                         goto abort_free;
2314                 }
2315         }
2316
2317         INIT_LIST_HEAD(&rdev->same_set);
2318         init_waitqueue_head(&rdev->blocked_wait);
2319
2320         return rdev;
2321
2322 abort_free:
2323         if (rdev->sb_page) {
2324                 if (rdev->bdev)
2325                         unlock_rdev(rdev);
2326                 free_disk_sb(rdev);
2327         }
2328         kfree(rdev);
2329         return ERR_PTR(err);
2330 }
2331
2332 /*
2333  * Check a full RAID array for plausibility
2334  */
2335
2336
2337 static void analyze_sbs(mddev_t * mddev)
2338 {
2339         int i;
2340         struct list_head *tmp;
2341         mdk_rdev_t *rdev, *freshest;
2342         char b[BDEVNAME_SIZE];
2343
2344         freshest = NULL;
2345         rdev_for_each(rdev, tmp, mddev)
2346                 switch (super_types[mddev->major_version].
2347                         load_super(rdev, freshest, mddev->minor_version)) {
2348                 case 1:
2349                         freshest = rdev;
2350                         break;
2351                 case 0:
2352                         break;
2353                 default:
2354                         printk( KERN_ERR \
2355                                 "md: fatal superblock inconsistency in %s"
2356                                 " -- removing from array\n", 
2357                                 bdevname(rdev->bdev,b));
2358                         kick_rdev_from_array(rdev);
2359                 }
2360
2361
2362         super_types[mddev->major_version].
2363                 validate_super(mddev, freshest);
2364
2365         i = 0;
2366         rdev_for_each(rdev, tmp, mddev) {
2367                 if (rdev != freshest)
2368                         if (super_types[mddev->major_version].
2369                             validate_super(mddev, rdev)) {
2370                                 printk(KERN_WARNING "md: kicking non-fresh %s"
2371                                         " from array!\n",
2372                                         bdevname(rdev->bdev,b));
2373                                 kick_rdev_from_array(rdev);
2374                                 continue;
2375                         }
2376                 if (mddev->level == LEVEL_MULTIPATH) {
2377                         rdev->desc_nr = i++;
2378                         rdev->raid_disk = rdev->desc_nr;
2379                         set_bit(In_sync, &rdev->flags);
2380                 } else if (rdev->raid_disk >= mddev->raid_disks) {
2381                         rdev->raid_disk = -1;
2382                         clear_bit(In_sync, &rdev->flags);
2383                 }
2384         }
2385
2386
2387
2388         if (mddev->recovery_cp != MaxSector &&
2389             mddev->level >= 1)
2390                 printk(KERN_ERR "md: %s: raid array is not clean"
2391                        " -- starting background reconstruction\n",
2392                        mdname(mddev));
2393
2394 }
2395
2396 static ssize_t
2397 safe_delay_show(mddev_t *mddev, char *page)
2398 {
2399         int msec = (mddev->safemode_delay*1000)/HZ;
2400         return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2401 }
2402 static ssize_t
2403 safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2404 {
2405         int scale=1;
2406         int dot=0;
2407         int i;
2408         unsigned long msec;
2409         char buf[30];
2410         char *e;
2411         /* remove a period, and count digits after it */
2412         if (len >= sizeof(buf))
2413                 return -EINVAL;
2414         strlcpy(buf, cbuf, len);
2415         buf[len] = 0;
2416         for (i=0; i<len; i++) {
2417                 if (dot) {
2418                         if (isdigit(buf[i])) {
2419                                 buf[i-1] = buf[i];
2420                                 scale *= 10;
2421                         }
2422                         buf[i] = 0;
2423                 } else if (buf[i] == '.') {
2424                         dot=1;
2425                         buf[i] = 0;
2426                 }
2427         }
2428         msec = simple_strtoul(buf, &e, 10);
2429         if (e == buf || (*e && *e != '\n'))
2430                 return -EINVAL;
2431         msec = (msec * 1000) / scale;
2432         if (msec == 0)
2433                 mddev->safemode_delay = 0;
2434         else {
2435                 mddev->safemode_delay = (msec*HZ)/1000;
2436                 if (mddev->safemode_delay == 0)
2437                         mddev->safemode_delay = 1;
2438         }
2439         return len;
2440 }
2441 static struct md_sysfs_entry md_safe_delay =
2442 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
2443
2444 static ssize_t
2445 level_show(mddev_t *mddev, char *page)
2446 {
2447         struct mdk_personality *p = mddev->pers;
2448         if (p)
2449                 return sprintf(page, "%s\n", p->name);
2450         else if (mddev->clevel[0])
2451                 return sprintf(page, "%s\n", mddev->clevel);
2452         else if (mddev->level != LEVEL_NONE)
2453                 return sprintf(page, "%d\n", mddev->level);
2454         else
2455                 return 0;
2456 }
2457
2458 static ssize_t
2459 level_store(mddev_t *mddev, const char *buf, size_t len)
2460 {
2461         ssize_t rv = len;
2462         if (mddev->pers)
2463                 return -EBUSY;
2464         if (len == 0)
2465                 return 0;
2466         if (len >= sizeof(mddev->clevel))
2467                 return -ENOSPC;
2468         strncpy(mddev->clevel, buf, len);
2469         if (mddev->clevel[len-1] == '\n')
2470                 len--;
2471         mddev->clevel[len] = 0;
2472         mddev->level = LEVEL_NONE;
2473         return rv;
2474 }
2475
2476 static struct md_sysfs_entry md_level =
2477 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
2478
2479
2480 static ssize_t
2481 layout_show(mddev_t *mddev, char *page)
2482 {
2483         /* just a number, not meaningful for all levels */
2484         if (mddev->reshape_position != MaxSector &&
2485             mddev->layout != mddev->new_layout)
2486                 return sprintf(page, "%d (%d)\n",
2487                                mddev->new_layout, mddev->layout);
2488         return sprintf(page, "%d\n", mddev->layout);
2489 }
2490
2491 static ssize_t
2492 layout_store(mddev_t *mddev, const char *buf, size_t len)
2493 {
2494         char *e;
2495         unsigned long n = simple_strtoul(buf, &e, 10);
2496
2497         if (!*buf || (*e && *e != '\n'))
2498                 return -EINVAL;
2499
2500         if (mddev->pers)
2501                 return -EBUSY;
2502         if (mddev->reshape_position != MaxSector)
2503                 mddev->new_layout = n;
2504         else
2505                 mddev->layout = n;
2506         return len;
2507 }
2508 static struct md_sysfs_entry md_layout =
2509 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
2510
2511
2512 static ssize_t
2513 raid_disks_show(mddev_t *mddev, char *page)
2514 {
2515         if (mddev->raid_disks == 0)
2516                 return 0;
2517         if (mddev->reshape_position != MaxSector &&
2518             mddev->delta_disks != 0)
2519                 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2520                                mddev->raid_disks - mddev->delta_disks);
2521         return sprintf(page, "%d\n", mddev->raid_disks);
2522 }
2523
2524 static int update_raid_disks(mddev_t *mddev, int raid_disks);
2525
2526 static ssize_t
2527 raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2528 {
2529         char *e;
2530         int rv = 0;
2531         unsigned long n = simple_strtoul(buf, &e, 10);
2532
2533         if (!*buf || (*e && *e != '\n'))
2534                 return -EINVAL;
2535
2536         if (mddev->pers)
2537                 rv = update_raid_disks(mddev, n);
2538         else if (mddev->reshape_position != MaxSector) {
2539                 int olddisks = mddev->raid_disks - mddev->delta_disks;
2540                 mddev->delta_disks = n - olddisks;
2541                 mddev->raid_disks = n;
2542         } else
2543                 mddev->raid_disks = n;
2544         return rv ? rv : len;
2545 }
2546 static struct md_sysfs_entry md_raid_disks =
2547 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
2548
2549 static ssize_t
2550 chunk_size_show(mddev_t *mddev, char *page)
2551 {
2552         if (mddev->reshape_position != MaxSector &&
2553             mddev->chunk_size != mddev->new_chunk)
2554                 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2555                                mddev->chunk_size);
2556         return sprintf(page, "%d\n", mddev->chunk_size);
2557 }
2558
2559 static ssize_t
2560 chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2561 {
2562         /* can only set chunk_size if array is not yet active */
2563         char *e;
2564         unsigned long n = simple_strtoul(buf, &e, 10);
2565
2566         if (!*buf || (*e && *e != '\n'))
2567                 return -EINVAL;
2568
2569         if (mddev->pers)
2570                 return -EBUSY;
2571         else if (mddev->reshape_position != MaxSector)
2572                 mddev->new_chunk = n;
2573         else
2574                 mddev->chunk_size = n;
2575         return len;
2576 }
2577 static struct md_sysfs_entry md_chunk_size =
2578 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
2579
2580 static ssize_t
2581 resync_start_show(mddev_t *mddev, char *page)
2582 {
2583         return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2584 }
2585
2586 static ssize_t
2587 resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2588 {
2589         char *e;
2590         unsigned long long n = simple_strtoull(buf, &e, 10);
2591
2592         if (mddev->pers)
2593                 return -EBUSY;
2594         if (!*buf || (*e && *e != '\n'))
2595                 return -EINVAL;
2596
2597         mddev->recovery_cp = n;
2598         return len;
2599 }
2600 static struct md_sysfs_entry md_resync_start =
2601 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
2602
2603 /*
2604  * The array state can be:
2605  *
2606  * clear
2607  *     No devices, no size, no level
2608  *     Equivalent to STOP_ARRAY ioctl
2609  * inactive
2610  *     May have some settings, but array is not active
2611  *        all IO results in error
2612  *     When written, doesn't tear down array, but just stops it
2613  * suspended (not supported yet)
2614  *     All IO requests will block. The array can be reconfigured.
2615  *     Writing this, if accepted, will block until array is quiescent
2616  * readonly
2617  *     no resync can happen.  no superblocks get written.
2618  *     write requests fail
2619  * read-auto
2620  *     like readonly, but behaves like 'clean' on a write request.
2621  *
2622  * clean - no pending writes, but otherwise active.
2623  *     When written to inactive array, starts without resync
2624  *     If a write request arrives then
2625  *       if metadata is known, mark 'dirty' and switch to 'active'.
2626  *       if not known, block and switch to write-pending
2627  *     If written to an active array that has pending writes, then fails.
2628  * active
2629  *     fully active: IO and resync can be happening.
2630  *     When written to inactive array, starts with resync
2631  *
2632  * write-pending
2633  *     clean, but writes are blocked waiting for 'active' to be written.
2634  *
2635  * active-idle
2636  *     like active, but no writes have been seen for a while (100msec).
2637  *
2638  */
2639 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2640                    write_pending, active_idle, bad_word};
2641 static char *array_states[] = {
2642         "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2643         "write-pending", "active-idle", NULL };
2644
2645 static int match_word(const char *word, char **list)
2646 {
2647         int n;
2648         for (n=0; list[n]; n++)
2649                 if (cmd_match(word, list[n]))
2650                         break;
2651         return n;
2652 }
2653
2654 static ssize_t
2655 array_state_show(mddev_t *mddev, char *page)
2656 {
2657         enum array_state st = inactive;
2658
2659         if (mddev->pers)
2660                 switch(mddev->ro) {
2661                 case 1:
2662                         st = readonly;
2663                         break;
2664                 case 2:
2665                         st = read_auto;
2666                         break;
2667                 case 0:
2668                         if (mddev->in_sync)
2669                                 st = clean;
2670                         else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2671                                 st = write_pending;
2672                         else if (mddev->safemode)
2673                                 st = active_idle;
2674                         else
2675                                 st = active;
2676                 }
2677         else {
2678                 if (list_empty(&mddev->disks) &&
2679                     mddev->raid_disks == 0 &&
2680                     mddev->size == 0)
2681                         st = clear;
2682                 else
2683                         st = inactive;
2684         }
2685         return sprintf(page, "%s\n", array_states[st]);
2686 }
2687
2688 static int do_md_stop(mddev_t * mddev, int ro, int is_open);
2689 static int do_md_run(mddev_t * mddev);
2690 static int restart_array(mddev_t *mddev);
2691
2692 static ssize_t
2693 array_state_store(mddev_t *mddev, const char *buf, size_t len)
2694 {
2695         int err = -EINVAL;
2696         enum array_state st = match_word(buf, array_states);
2697         switch(st) {
2698         case bad_word:
2699                 break;
2700         case clear:
2701                 /* stopping an active array */
2702                 if (atomic_read(&mddev->openers) > 0)
2703                         return -EBUSY;
2704                 err = do_md_stop(mddev, 0, 0);
2705                 break;
2706         case inactive:
2707                 /* stopping an active array */
2708                 if (mddev->pers) {
2709                         if (atomic_read(&mddev->openers) > 0)
2710                                 return -EBUSY;
2711                         err = do_md_stop(mddev, 2, 0);
2712                 } else
2713                         err = 0; /* already inactive */
2714                 break;
2715         case suspended:
2716                 break; /* not supported yet */
2717         case readonly:
2718                 if (mddev->pers)
2719                         err = do_md_stop(mddev, 1, 0);
2720                 else {
2721                         mddev->ro = 1;
2722                         set_disk_ro(mddev->gendisk, 1);
2723                         err = do_md_run(mddev);
2724                 }
2725                 break;
2726         case read_auto:
2727                 if (mddev->pers) {
2728                         if (mddev->ro != 1)
2729                                 err = do_md_stop(mddev, 1, 0);
2730                         else
2731                                 err = restart_array(mddev);
2732                         if (err == 0) {
2733                                 mddev->ro = 2;
2734                                 set_disk_ro(mddev->gendisk, 0);
2735                         }
2736                 } else {
2737                         mddev->ro = 2;
2738                         err = do_md_run(mddev);
2739                 }
2740                 break;
2741         case clean:
2742                 if (mddev->pers) {
2743                         restart_array(mddev);
2744                         spin_lock_irq(&mddev->write_lock);
2745                         if (atomic_read(&mddev->writes_pending) == 0) {
2746                                 if (mddev->in_sync == 0) {
2747                                         mddev->in_sync = 1;
2748                                         if (mddev->safemode == 1)
2749                                                 mddev->safemode = 0;
2750                                         if (mddev->persistent)
2751                                                 set_bit(MD_CHANGE_CLEAN,
2752                                                         &mddev->flags);
2753                                 }
2754                                 err = 0;
2755                         } else
2756                                 err = -EBUSY;
2757                         spin_unlock_irq(&mddev->write_lock);
2758                 } else {
2759                         mddev->ro = 0;
2760                         mddev->recovery_cp = MaxSector;
2761                         err = do_md_run(mddev);
2762                 }
2763                 break;
2764         case active:
2765                 if (mddev->pers) {
2766                         restart_array(mddev);
2767                         if (mddev->external)
2768                                 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2769                         wake_up(&mddev->sb_wait);
2770                         err = 0;
2771                 } else {
2772                         mddev->ro = 0;
2773                         set_disk_ro(mddev->gendisk, 0);
2774                         err = do_md_run(mddev);
2775                 }
2776                 break;
2777         case write_pending:
2778         case active_idle:
2779                 /* these cannot be set */
2780                 break;
2781         }
2782         if (err)
2783                 return err;
2784         else {
2785                 sysfs_notify(&mddev->kobj, NULL, "array_state");
2786                 return len;
2787         }
2788 }
2789 static struct md_sysfs_entry md_array_state =
2790 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
2791
2792 static ssize_t
2793 null_show(mddev_t *mddev, char *page)
2794 {
2795         return -EINVAL;
2796 }
2797
2798 static ssize_t
2799 new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2800 {
2801         /* buf must be %d:%d\n? giving major and minor numbers */
2802         /* The new device is added to the array.
2803          * If the array has a persistent superblock, we read the
2804          * superblock to initialise info and check validity.
2805          * Otherwise, only checking done is that in bind_rdev_to_array,
2806          * which mainly checks size.
2807          */
2808         char *e;
2809         int major = simple_strtoul(buf, &e, 10);
2810         int minor;
2811         dev_t dev;
2812         mdk_rdev_t *rdev;
2813         int err;
2814
2815         if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2816                 return -EINVAL;
2817         minor = simple_strtoul(e+1, &e, 10);
2818         if (*e && *e != '\n')
2819                 return -EINVAL;
2820         dev = MKDEV(major, minor);
2821         if (major != MAJOR(dev) ||
2822             minor != MINOR(dev))
2823                 return -EOVERFLOW;
2824
2825
2826         if (mddev->persistent) {
2827                 rdev = md_import_device(dev, mddev->major_version,
2828                                         mddev->minor_version);
2829                 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2830                         mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2831                                                        mdk_rdev_t, same_set);
2832                         err = super_types[mddev->major_version]
2833                                 .load_super(rdev, rdev0, mddev->minor_version);
2834                         if (err < 0)
2835                                 goto out;
2836                 }
2837         } else if (mddev->external)
2838                 rdev = md_import_device(dev, -2, -1);
2839         else
2840                 rdev = md_import_device(dev, -1, -1);
2841
2842         if (IS_ERR(rdev))
2843                 return PTR_ERR(rdev);
2844         err = bind_rdev_to_array(rdev, mddev);
2845  out:
2846         if (err)
2847                 export_rdev(rdev);
2848         return err ? err : len;
2849 }
2850
2851 static struct md_sysfs_entry md_new_device =
2852 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
2853
2854 static ssize_t
2855 bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2856 {
2857         char *end;
2858         unsigned long chunk, end_chunk;
2859
2860         if (!mddev->bitmap)
2861                 goto out;
2862         /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2863         while (*buf) {
2864                 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2865                 if (buf == end) break;
2866                 if (*end == '-') { /* range */
2867                         buf = end + 1;
2868                         end_chunk = simple_strtoul(buf, &end, 0);
2869                         if (buf == end) break;
2870                 }
2871                 if (*end && !isspace(*end)) break;
2872                 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2873                 buf = end;
2874                 while (isspace(*buf)) buf++;
2875         }
2876         bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2877 out:
2878         return len;
2879 }
2880
2881 static struct md_sysfs_entry md_bitmap =
2882 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2883
2884 static ssize_t
2885 size_show(mddev_t *mddev, char *page)
2886 {
2887         return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2888 }
2889
2890 static int update_size(mddev_t *mddev, sector_t num_sectors);
2891
2892 static ssize_t
2893 size_store(mddev_t *mddev, const char *buf, size_t len)
2894 {
2895         /* If array is inactive, we can reduce the component size, but
2896          * not increase it (except from 0).
2897          * If array is active, we can try an on-line resize
2898          */
2899         char *e;
2900         int err = 0;
2901         unsigned long long size = simple_strtoull(buf, &e, 10);
2902         if (!*buf || *buf == '\n' ||
2903             (*e && *e != '\n'))
2904                 return -EINVAL;
2905
2906         if (mddev->pers) {
2907                 err = update_size(mddev, size * 2);
2908                 md_update_sb(mddev, 1);
2909         } else {
2910                 if (mddev->size == 0 ||
2911                     mddev->size > size)
2912                         mddev->size = size;
2913                 else
2914                         err = -ENOSPC;
2915         }
2916         return err ? err : len;
2917 }
2918
2919 static struct md_sysfs_entry md_size =
2920 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
2921
2922
2923 /* Metdata version.
2924  * This is one of
2925  *   'none' for arrays with no metadata (good luck...)
2926  *   'external' for arrays with externally managed metadata,
2927  * or N.M for internally known formats
2928  */
2929 static ssize_t
2930 metadata_show(mddev_t *mddev, char *page)
2931 {
2932         if (mddev->persistent)
2933                 return sprintf(page, "%d.%d\n",
2934                                mddev->major_version, mddev->minor_version);
2935         else if (mddev->external)
2936                 return sprintf(page, "external:%s\n", mddev->metadata_type);
2937         else
2938                 return sprintf(page, "none\n");
2939 }
2940
2941 static ssize_t
2942 metadata_store(mddev_t *mddev, const char *buf, size_t len)
2943 {
2944         int major, minor;
2945         char *e;
2946         if (!list_empty(&mddev->disks))
2947                 return -EBUSY;
2948
2949         if (cmd_match(buf, "none")) {
2950                 mddev->persistent = 0;
2951                 mddev->external = 0;
2952                 mddev->major_version = 0;
2953                 mddev->minor_version = 90;
2954                 return len;
2955         }
2956         if (strncmp(buf, "external:", 9) == 0) {
2957                 size_t namelen = len-9;
2958                 if (namelen >= sizeof(mddev->metadata_type))
2959                         namelen = sizeof(mddev->metadata_type)-1;
2960                 strncpy(mddev->metadata_type, buf+9, namelen);
2961                 mddev->metadata_type[namelen] = 0;
2962                 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2963                         mddev->metadata_type[--namelen] = 0;
2964                 mddev->persistent = 0;
2965                 mddev->external = 1;
2966                 mddev->major_version = 0;
2967                 mddev->minor_version = 90;
2968                 return len;
2969         }
2970         major = simple_strtoul(buf, &e, 10);
2971         if (e==buf || *e != '.')
2972                 return -EINVAL;
2973         buf = e+1;
2974         minor = simple_strtoul(buf, &e, 10);
2975         if (e==buf || (*e && *e != '\n') )
2976                 return -EINVAL;
2977         if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
2978                 return -ENOENT;
2979         mddev->major_version = major;
2980         mddev->minor_version = minor;
2981         mddev->persistent = 1;
2982         mddev->external = 0;
2983         return len;
2984 }
2985
2986 static struct md_sysfs_entry md_metadata =
2987 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
2988
2989 static ssize_t
2990 action_show(mddev_t *mddev, char *page)
2991 {
2992         char *type = "idle";
2993         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2994             (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
2995                 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2996                         type = "reshape";
2997                 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2998                         if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2999                                 type = "resync";
3000                         else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3001                                 type = "check";
3002                         else
3003                                 type = "repair";
3004                 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
3005                         type = "recover";
3006         }
3007         return sprintf(page, "%s\n", type);
3008 }
3009
3010 static ssize_t
3011 action_store(mddev_t *mddev, const char *page, size_t len)
3012 {
3013         if (!mddev->pers || !mddev->pers->sync_request)
3014                 return -EINVAL;
3015
3016         if (cmd_match(page, "idle")) {
3017                 if (mddev->sync_thread) {
3018                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3019                         md_unregister_thread(mddev->sync_thread);
3020                         mddev->sync_thread = NULL;
3021                         mddev->recovery = 0;
3022                 }
3023         } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3024                    test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
3025                 return -EBUSY;
3026         else if (cmd_match(page, "resync"))
3027                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3028         else if (cmd_match(page, "recover")) {
3029                 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3030                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3031         } else if (cmd_match(page, "reshape")) {
3032                 int err;
3033                 if (mddev->pers->start_reshape == NULL)
3034                         return -EINVAL;
3035                 err = mddev->pers->start_reshape(mddev);
3036                 if (err)
3037                         return err;
3038                 sysfs_notify(&mddev->kobj, NULL, "degraded");
3039         } else {
3040                 if (cmd_match(page, "check"))
3041                         set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3042                 else if (!cmd_match(page, "repair"))
3043                         return -EINVAL;
3044                 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3045                 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3046         }
3047         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3048         md_wakeup_thread(mddev->thread);
3049         sysfs_notify(&mddev->kobj, NULL, "sync_action");
3050         return len;
3051 }
3052
3053 static ssize_t
3054 mismatch_cnt_show(mddev_t *mddev, char *page)
3055 {
3056         return sprintf(page, "%llu\n",
3057                        (unsigned long long) mddev->resync_mismatches);
3058 }
3059
3060 static struct md_sysfs_entry md_scan_mode =
3061 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
3062
3063
3064 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
3065
3066 static ssize_t
3067 sync_min_show(mddev_t *mddev, char *page)
3068 {
3069         return sprintf(page, "%d (%s)\n", speed_min(mddev),
3070                        mddev->sync_speed_min ? "local": "system");
3071 }
3072
3073 static ssize_t
3074 sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3075 {
3076         int min;
3077         char *e;
3078         if (strncmp(buf, "system", 6)==0) {
3079                 mddev->sync_speed_min = 0;
3080                 return len;
3081         }
3082         min = simple_strtoul(buf, &e, 10);
3083         if (buf == e || (*e && *e != '\n') || min <= 0)
3084                 return -EINVAL;
3085         mddev->sync_speed_min = min;
3086         return len;
3087 }
3088
3089 static struct md_sysfs_entry md_sync_min =
3090 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3091
3092 static ssize_t
3093 sync_max_show(mddev_t *mddev, char *page)
3094 {
3095         return sprintf(page, "%d (%s)\n", speed_max(mddev),
3096                        mddev->sync_speed_max ? "local": "system");
3097 }
3098
3099 static ssize_t
3100 sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3101 {
3102         int max;
3103         char *e;
3104         if (strncmp(buf, "system", 6)==0) {
3105                 mddev->sync_speed_max = 0;
3106                 return len;
3107         }
3108         max = simple_strtoul(buf, &e, 10);
3109         if (buf == e || (*e && *e != '\n') || max <= 0)
3110                 return -EINVAL;
3111         mddev->sync_speed_max = max;
3112         return len;
3113 }
3114
3115 static struct md_sysfs_entry md_sync_max =
3116 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3117
3118 static ssize_t
3119 degraded_show(mddev_t *mddev, char *page)
3120 {
3121         return sprintf(page, "%d\n", mddev->degraded);
3122 }
3123 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
3124
3125 static ssize_t
3126 sync_force_parallel_show(mddev_t *mddev, char *page)
3127 {
3128         return sprintf(page, "%d\n", mddev->parallel_resync);
3129 }
3130
3131 static ssize_t
3132 sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3133 {
3134         long n;
3135
3136         if (strict_strtol(buf, 10, &n))
3137                 return -EINVAL;
3138
3139         if (n != 0 && n != 1)
3140                 return -EINVAL;
3141
3142         mddev->parallel_resync = n;
3143
3144         if (mddev->sync_thread)
3145                 wake_up(&resync_wait);
3146
3147         return len;
3148 }
3149
3150 /* force parallel resync, even with shared block devices */
3151 static struct md_sysfs_entry md_sync_force_parallel =
3152 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3153        sync_force_parallel_show, sync_force_parallel_store);
3154
3155 static ssize_t
3156 sync_speed_show(mddev_t *mddev, char *page)
3157 {
3158         unsigned long resync, dt, db;
3159         resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3160         dt = (jiffies - mddev->resync_mark) / HZ;
3161         if (!dt) dt++;
3162         db = resync - mddev->resync_mark_cnt;
3163         return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
3164 }
3165
3166 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
3167
3168 static ssize_t
3169 sync_completed_show(mddev_t *mddev, char *page)
3170 {
3171         unsigned long max_blocks, resync;
3172
3173         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3174                 max_blocks = mddev->resync_max_sectors;
3175         else
3176                 max_blocks = mddev->size << 1;
3177
3178         resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3179         return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3180 }
3181
3182 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
3183
3184 static ssize_t
3185 min_sync_show(mddev_t *mddev, char *page)
3186 {
3187         return sprintf(page, "%llu\n",
3188                        (unsigned long long)mddev->resync_min);
3189 }
3190 static ssize_t
3191 min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3192 {
3193         unsigned long long min;
3194         if (strict_strtoull(buf, 10, &min))
3195                 return -EINVAL;
3196         if (min > mddev->resync_max)
3197                 return -EINVAL;
3198         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3199                 return -EBUSY;
3200
3201         /* Must be a multiple of chunk_size */
3202         if (mddev->chunk_size) {
3203                 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3204                         return -EINVAL;
3205         }
3206         mddev->resync_min = min;
3207
3208         return len;
3209 }
3210
3211 static struct md_sysfs_entry md_min_sync =
3212 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3213
3214 static ssize_t
3215 max_sync_show(mddev_t *mddev, char *page)
3216 {
3217         if (mddev->resync_max == MaxSector)
3218                 return sprintf(page, "max\n");
3219         else
3220                 return sprintf(page, "%llu\n",
3221                                (unsigned long long)mddev->resync_max);
3222 }
3223 static ssize_t
3224 max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3225 {
3226         if (strncmp(buf, "max", 3) == 0)
3227                 mddev->resync_max = MaxSector;