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