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