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