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