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