block_device_operations->release() should return void
[linux-3.10.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         bio->bi_sector += offset;
201         bio->bi_size = size;
202         offset <<= 9;
203         clear_bit(BIO_SEG_VALID, &bio->bi_flags);
204
205         while (bio->bi_idx < bio->bi_vcnt &&
206                bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
207                 /* remove this whole bio_vec */
208                 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
209                 bio->bi_idx++;
210         }
211         if (bio->bi_idx < bio->bi_vcnt) {
212                 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
213                 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
214         }
215         /* avoid any complications with bi_idx being non-zero*/
216         if (bio->bi_idx) {
217                 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
218                         (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
219                 bio->bi_vcnt -= bio->bi_idx;
220                 bio->bi_idx = 0;
221         }
222         /* Make sure vcnt and last bv are not too big */
223         bio_for_each_segment(bvec, bio, i) {
224                 if (sofar + bvec->bv_len > size)
225                         bvec->bv_len = size - sofar;
226                 if (bvec->bv_len == 0) {
227                         bio->bi_vcnt = i;
228                         break;
229                 }
230                 sofar += bvec->bv_len;
231         }
232 }
233 EXPORT_SYMBOL_GPL(md_trim_bio);
234
235 /*
236  * We have a system wide 'event count' that is incremented
237  * on any 'interesting' event, and readers of /proc/mdstat
238  * can use 'poll' or 'select' to find out when the event
239  * count increases.
240  *
241  * Events are:
242  *  start array, stop array, error, add device, remove device,
243  *  start build, activate spare
244  */
245 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
246 static atomic_t md_event_count;
247 void md_new_event(struct mddev *mddev)
248 {
249         atomic_inc(&md_event_count);
250         wake_up(&md_event_waiters);
251 }
252 EXPORT_SYMBOL_GPL(md_new_event);
253
254 /* Alternate version that can be called from interrupts
255  * when calling sysfs_notify isn't needed.
256  */
257 static void md_new_event_inintr(struct mddev *mddev)
258 {
259         atomic_inc(&md_event_count);
260         wake_up(&md_event_waiters);
261 }
262
263 /*
264  * Enables to iterate over all existing md arrays
265  * all_mddevs_lock protects this list.
266  */
267 static LIST_HEAD(all_mddevs);
268 static DEFINE_SPINLOCK(all_mddevs_lock);
269
270
271 /*
272  * iterates through all used mddevs in the system.
273  * We take care to grab the all_mddevs_lock whenever navigating
274  * the list, and to always hold a refcount when unlocked.
275  * Any code which breaks out of this loop while own
276  * a reference to the current mddev and must mddev_put it.
277  */
278 #define for_each_mddev(_mddev,_tmp)                                     \
279                                                                         \
280         for (({ spin_lock(&all_mddevs_lock);                            \
281                 _tmp = all_mddevs.next;                                 \
282                 _mddev = NULL;});                                       \
283              ({ if (_tmp != &all_mddevs)                                \
284                         mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
285                 spin_unlock(&all_mddevs_lock);                          \
286                 if (_mddev) mddev_put(_mddev);                          \
287                 _mddev = list_entry(_tmp, struct mddev, all_mddevs);    \
288                 _tmp != &all_mddevs;});                                 \
289              ({ spin_lock(&all_mddevs_lock);                            \
290                 _tmp = _tmp->next;})                                    \
291                 )
292
293
294 /* Rather than calling directly into the personality make_request function,
295  * IO requests come here first so that we can check if the device is
296  * being suspended pending a reconfiguration.
297  * We hold a refcount over the call to ->make_request.  By the time that
298  * call has finished, the bio has been linked into some internal structure
299  * and so is visible to ->quiesce(), so we don't need the refcount any more.
300  */
301 static void md_make_request(struct request_queue *q, struct bio *bio)
302 {
303         const int rw = bio_data_dir(bio);
304         struct mddev *mddev = q->queuedata;
305         int cpu;
306         unsigned int sectors;
307
308         if (mddev == NULL || mddev->pers == NULL
309             || !mddev->ready) {
310                 bio_io_error(bio);
311                 return;
312         }
313         if (mddev->ro == 1 && unlikely(rw == WRITE)) {
314                 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
315                 return;
316         }
317         smp_rmb(); /* Ensure implications of  'active' are visible */
318         rcu_read_lock();
319         if (mddev->suspended) {
320                 DEFINE_WAIT(__wait);
321                 for (;;) {
322                         prepare_to_wait(&mddev->sb_wait, &__wait,
323                                         TASK_UNINTERRUPTIBLE);
324                         if (!mddev->suspended)
325                                 break;
326                         rcu_read_unlock();
327                         schedule();
328                         rcu_read_lock();
329                 }
330                 finish_wait(&mddev->sb_wait, &__wait);
331         }
332         atomic_inc(&mddev->active_io);
333         rcu_read_unlock();
334
335         /*
336          * save the sectors now since our bio can
337          * go away inside make_request
338          */
339         sectors = bio_sectors(bio);
340         mddev->pers->make_request(mddev, bio);
341
342         cpu = part_stat_lock();
343         part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
344         part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
345         part_stat_unlock();
346
347         if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
348                 wake_up(&mddev->sb_wait);
349 }
350
351 /* mddev_suspend makes sure no new requests are submitted
352  * to the device, and that any requests that have been submitted
353  * are completely handled.
354  * Once ->stop is called and completes, the module will be completely
355  * unused.
356  */
357 void mddev_suspend(struct mddev *mddev)
358 {
359         BUG_ON(mddev->suspended);
360         mddev->suspended = 1;
361         synchronize_rcu();
362         wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
363         mddev->pers->quiesce(mddev, 1);
364
365         del_timer_sync(&mddev->safemode_timer);
366 }
367 EXPORT_SYMBOL_GPL(mddev_suspend);
368
369 void mddev_resume(struct mddev *mddev)
370 {
371         mddev->suspended = 0;
372         wake_up(&mddev->sb_wait);
373         mddev->pers->quiesce(mddev, 0);
374
375         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
376         md_wakeup_thread(mddev->thread);
377         md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
378 }
379 EXPORT_SYMBOL_GPL(mddev_resume);
380
381 int mddev_congested(struct mddev *mddev, int bits)
382 {
383         return mddev->suspended;
384 }
385 EXPORT_SYMBOL(mddev_congested);
386
387 /*
388  * Generic flush handling for md
389  */
390
391 static void md_end_flush(struct bio *bio, int err)
392 {
393         struct md_rdev *rdev = bio->bi_private;
394         struct mddev *mddev = rdev->mddev;
395
396         rdev_dec_pending(rdev, mddev);
397
398         if (atomic_dec_and_test(&mddev->flush_pending)) {
399                 /* The pre-request flush has finished */
400                 queue_work(md_wq, &mddev->flush_work);
401         }
402         bio_put(bio);
403 }
404
405 static void md_submit_flush_data(struct work_struct *ws);
406
407 static void submit_flushes(struct work_struct *ws)
408 {
409         struct mddev *mddev = container_of(ws, struct mddev, flush_work);
410         struct md_rdev *rdev;
411
412         INIT_WORK(&mddev->flush_work, md_submit_flush_data);
413         atomic_set(&mddev->flush_pending, 1);
414         rcu_read_lock();
415         rdev_for_each_rcu(rdev, mddev)
416                 if (rdev->raid_disk >= 0 &&
417                     !test_bit(Faulty, &rdev->flags)) {
418                         /* Take two references, one is dropped
419                          * when request finishes, one after
420                          * we reclaim rcu_read_lock
421                          */
422                         struct bio *bi;
423                         atomic_inc(&rdev->nr_pending);
424                         atomic_inc(&rdev->nr_pending);
425                         rcu_read_unlock();
426                         bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
427                         bi->bi_end_io = md_end_flush;
428                         bi->bi_private = rdev;
429                         bi->bi_bdev = rdev->bdev;
430                         atomic_inc(&mddev->flush_pending);
431                         submit_bio(WRITE_FLUSH, bi);
432                         rcu_read_lock();
433                         rdev_dec_pending(rdev, mddev);
434                 }
435         rcu_read_unlock();
436         if (atomic_dec_and_test(&mddev->flush_pending))
437                 queue_work(md_wq, &mddev->flush_work);
438 }
439
440 static void md_submit_flush_data(struct work_struct *ws)
441 {
442         struct mddev *mddev = container_of(ws, struct mddev, flush_work);
443         struct bio *bio = mddev->flush_bio;
444
445         if (bio->bi_size == 0)
446                 /* an empty barrier - all done */
447                 bio_endio(bio, 0);
448         else {
449                 bio->bi_rw &= ~REQ_FLUSH;
450                 mddev->pers->make_request(mddev, bio);
451         }
452
453         mddev->flush_bio = NULL;
454         wake_up(&mddev->sb_wait);
455 }
456
457 void md_flush_request(struct mddev *mddev, struct bio *bio)
458 {
459         spin_lock_irq(&mddev->write_lock);
460         wait_event_lock_irq(mddev->sb_wait,
461                             !mddev->flush_bio,
462                             mddev->write_lock);
463         mddev->flush_bio = bio;
464         spin_unlock_irq(&mddev->write_lock);
465
466         INIT_WORK(&mddev->flush_work, submit_flushes);
467         queue_work(md_wq, &mddev->flush_work);
468 }
469 EXPORT_SYMBOL(md_flush_request);
470
471 void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
472 {
473         struct mddev *mddev = cb->data;
474         md_wakeup_thread(mddev->thread);
475         kfree(cb);
476 }
477 EXPORT_SYMBOL(md_unplug);
478
479 static inline struct mddev *mddev_get(struct mddev *mddev)
480 {
481         atomic_inc(&mddev->active);
482         return mddev;
483 }
484
485 static void mddev_delayed_delete(struct work_struct *ws);
486
487 static void mddev_put(struct mddev *mddev)
488 {
489         struct bio_set *bs = NULL;
490
491         if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
492                 return;
493         if (!mddev->raid_disks && list_empty(&mddev->disks) &&
494             mddev->ctime == 0 && !mddev->hold_active) {
495                 /* Array is not configured at all, and not held active,
496                  * so destroy it */
497                 list_del_init(&mddev->all_mddevs);
498                 bs = mddev->bio_set;
499                 mddev->bio_set = NULL;
500                 if (mddev->gendisk) {
501                         /* We did a probe so need to clean up.  Call
502                          * queue_work inside the spinlock so that
503                          * flush_workqueue() after mddev_find will
504                          * succeed in waiting for the work to be done.
505                          */
506                         INIT_WORK(&mddev->del_work, mddev_delayed_delete);
507                         queue_work(md_misc_wq, &mddev->del_work);
508                 } else
509                         kfree(mddev);
510         }
511         spin_unlock(&all_mddevs_lock);
512         if (bs)
513                 bioset_free(bs);
514 }
515
516 void mddev_init(struct mddev *mddev)
517 {
518         mutex_init(&mddev->open_mutex);
519         mutex_init(&mddev->reconfig_mutex);
520         mutex_init(&mddev->bitmap_info.mutex);
521         INIT_LIST_HEAD(&mddev->disks);
522         INIT_LIST_HEAD(&mddev->all_mddevs);
523         init_timer(&mddev->safemode_timer);
524         atomic_set(&mddev->active, 1);
525         atomic_set(&mddev->openers, 0);
526         atomic_set(&mddev->active_io, 0);
527         spin_lock_init(&mddev->write_lock);
528         atomic_set(&mddev->flush_pending, 0);
529         init_waitqueue_head(&mddev->sb_wait);
530         init_waitqueue_head(&mddev->recovery_wait);
531         mddev->reshape_position = MaxSector;
532         mddev->reshape_backwards = 0;
533         mddev->resync_min = 0;
534         mddev->resync_max = MaxSector;
535         mddev->level = LEVEL_NONE;
536 }
537 EXPORT_SYMBOL_GPL(mddev_init);
538
539 static struct mddev * mddev_find(dev_t unit)
540 {
541         struct mddev *mddev, *new = NULL;
542
543         if (unit && MAJOR(unit) != MD_MAJOR)
544                 unit &= ~((1<<MdpMinorShift)-1);
545
546  retry:
547         spin_lock(&all_mddevs_lock);
548
549         if (unit) {
550                 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
551                         if (mddev->unit == unit) {
552                                 mddev_get(mddev);
553                                 spin_unlock(&all_mddevs_lock);
554                                 kfree(new);
555                                 return mddev;
556                         }
557
558                 if (new) {
559                         list_add(&new->all_mddevs, &all_mddevs);
560                         spin_unlock(&all_mddevs_lock);
561                         new->hold_active = UNTIL_IOCTL;
562                         return new;
563                 }
564         } else if (new) {
565                 /* find an unused unit number */
566                 static int next_minor = 512;
567                 int start = next_minor;
568                 int is_free = 0;
569                 int dev = 0;
570                 while (!is_free) {
571                         dev = MKDEV(MD_MAJOR, next_minor);
572                         next_minor++;
573                         if (next_minor > MINORMASK)
574                                 next_minor = 0;
575                         if (next_minor == start) {
576                                 /* Oh dear, all in use. */
577                                 spin_unlock(&all_mddevs_lock);
578                                 kfree(new);
579                                 return NULL;
580                         }
581                                 
582                         is_free = 1;
583                         list_for_each_entry(mddev, &all_mddevs, all_mddevs)
584                                 if (mddev->unit == dev) {
585                                         is_free = 0;
586                                         break;
587                                 }
588                 }
589                 new->unit = dev;
590                 new->md_minor = MINOR(dev);
591                 new->hold_active = UNTIL_STOP;
592                 list_add(&new->all_mddevs, &all_mddevs);
593                 spin_unlock(&all_mddevs_lock);
594                 return new;
595         }
596         spin_unlock(&all_mddevs_lock);
597
598         new = kzalloc(sizeof(*new), GFP_KERNEL);
599         if (!new)
600                 return NULL;
601
602         new->unit = unit;
603         if (MAJOR(unit) == MD_MAJOR)
604                 new->md_minor = MINOR(unit);
605         else
606                 new->md_minor = MINOR(unit) >> MdpMinorShift;
607
608         mddev_init(new);
609
610         goto retry;
611 }
612
613 static inline int mddev_lock(struct mddev * mddev)
614 {
615         return mutex_lock_interruptible(&mddev->reconfig_mutex);
616 }
617
618 static inline int mddev_is_locked(struct mddev *mddev)
619 {
620         return mutex_is_locked(&mddev->reconfig_mutex);
621 }
622
623 static inline int mddev_trylock(struct mddev * mddev)
624 {
625         return mutex_trylock(&mddev->reconfig_mutex);
626 }
627
628 static struct attribute_group md_redundancy_group;
629
630 static void mddev_unlock(struct mddev * mddev)
631 {
632         if (mddev->to_remove) {
633                 /* These cannot be removed under reconfig_mutex as
634                  * an access to the files will try to take reconfig_mutex
635                  * while holding the file unremovable, which leads to
636                  * a deadlock.
637                  * So hold set sysfs_active while the remove in happeing,
638                  * and anything else which might set ->to_remove or my
639                  * otherwise change the sysfs namespace will fail with
640                  * -EBUSY if sysfs_active is still set.
641                  * We set sysfs_active under reconfig_mutex and elsewhere
642                  * test it under the same mutex to ensure its correct value
643                  * is seen.
644                  */
645                 struct attribute_group *to_remove = mddev->to_remove;
646                 mddev->to_remove = NULL;
647                 mddev->sysfs_active = 1;
648                 mutex_unlock(&mddev->reconfig_mutex);
649
650                 if (mddev->kobj.sd) {
651                         if (to_remove != &md_redundancy_group)
652                                 sysfs_remove_group(&mddev->kobj, to_remove);
653                         if (mddev->pers == NULL ||
654                             mddev->pers->sync_request == NULL) {
655                                 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
656                                 if (mddev->sysfs_action)
657                                         sysfs_put(mddev->sysfs_action);
658                                 mddev->sysfs_action = NULL;
659                         }
660                 }
661                 mddev->sysfs_active = 0;
662         } else
663                 mutex_unlock(&mddev->reconfig_mutex);
664
665         /* As we've dropped the mutex we need a spinlock to
666          * make sure the thread doesn't disappear
667          */
668         spin_lock(&pers_lock);
669         md_wakeup_thread(mddev->thread);
670         spin_unlock(&pers_lock);
671 }
672
673 static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
674 {
675         struct md_rdev *rdev;
676
677         rdev_for_each(rdev, mddev)
678                 if (rdev->desc_nr == nr)
679                         return rdev;
680
681         return NULL;
682 }
683
684 static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
685 {
686         struct md_rdev *rdev;
687
688         rdev_for_each_rcu(rdev, mddev)
689                 if (rdev->desc_nr == nr)
690                         return rdev;
691
692         return NULL;
693 }
694
695 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
696 {
697         struct md_rdev *rdev;
698
699         rdev_for_each(rdev, mddev)
700                 if (rdev->bdev->bd_dev == dev)
701                         return rdev;
702
703         return NULL;
704 }
705
706 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
707 {
708         struct md_rdev *rdev;
709
710         rdev_for_each_rcu(rdev, mddev)
711                 if (rdev->bdev->bd_dev == dev)
712                         return rdev;
713
714         return NULL;
715 }
716
717 static struct md_personality *find_pers(int level, char *clevel)
718 {
719         struct md_personality *pers;
720         list_for_each_entry(pers, &pers_list, list) {
721                 if (level != LEVEL_NONE && pers->level == level)
722                         return pers;
723                 if (strcmp(pers->name, clevel)==0)
724                         return pers;
725         }
726         return NULL;
727 }
728
729 /* return the offset of the super block in 512byte sectors */
730 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
731 {
732         sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
733         return MD_NEW_SIZE_SECTORS(num_sectors);
734 }
735
736 static int alloc_disk_sb(struct md_rdev * rdev)
737 {
738         if (rdev->sb_page)
739                 MD_BUG();
740
741         rdev->sb_page = alloc_page(GFP_KERNEL);
742         if (!rdev->sb_page) {
743                 printk(KERN_ALERT "md: out of memory.\n");
744                 return -ENOMEM;
745         }
746
747         return 0;
748 }
749
750 void md_rdev_clear(struct md_rdev *rdev)
751 {
752         if (rdev->sb_page) {
753                 put_page(rdev->sb_page);
754                 rdev->sb_loaded = 0;
755                 rdev->sb_page = NULL;
756                 rdev->sb_start = 0;
757                 rdev->sectors = 0;
758         }
759         if (rdev->bb_page) {
760                 put_page(rdev->bb_page);
761                 rdev->bb_page = NULL;
762         }
763         kfree(rdev->badblocks.page);
764         rdev->badblocks.page = NULL;
765 }
766 EXPORT_SYMBOL_GPL(md_rdev_clear);
767
768 static void super_written(struct bio *bio, int error)
769 {
770         struct md_rdev *rdev = bio->bi_private;
771         struct mddev *mddev = rdev->mddev;
772
773         if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
774                 printk("md: super_written gets error=%d, uptodate=%d\n",
775                        error, test_bit(BIO_UPTODATE, &bio->bi_flags));
776                 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
777                 md_error(mddev, rdev);
778         }
779
780         if (atomic_dec_and_test(&mddev->pending_writes))
781                 wake_up(&mddev->sb_wait);
782         bio_put(bio);
783 }
784
785 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
786                    sector_t sector, int size, struct page *page)
787 {
788         /* write first size bytes of page to sector of rdev
789          * Increment mddev->pending_writes before returning
790          * and decrement it on completion, waking up sb_wait
791          * if zero is reached.
792          * If an error occurred, call md_error
793          */
794         struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
795
796         bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
797         bio->bi_sector = sector;
798         bio_add_page(bio, page, size, 0);
799         bio->bi_private = rdev;
800         bio->bi_end_io = super_written;
801
802         atomic_inc(&mddev->pending_writes);
803         submit_bio(WRITE_FLUSH_FUA, bio);
804 }
805
806 void md_super_wait(struct mddev *mddev)
807 {
808         /* wait for all superblock writes that were scheduled to complete */
809         DEFINE_WAIT(wq);
810         for(;;) {
811                 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
812                 if (atomic_read(&mddev->pending_writes)==0)
813                         break;
814                 schedule();
815         }
816         finish_wait(&mddev->sb_wait, &wq);
817 }
818
819 static void bi_complete(struct bio *bio, int error)
820 {
821         complete((struct completion*)bio->bi_private);
822 }
823
824 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
825                  struct page *page, int rw, bool metadata_op)
826 {
827         struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
828         struct completion event;
829         int ret;
830
831         rw |= REQ_SYNC;
832
833         bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
834                 rdev->meta_bdev : rdev->bdev;
835         if (metadata_op)
836                 bio->bi_sector = sector + rdev->sb_start;
837         else if (rdev->mddev->reshape_position != MaxSector &&
838                  (rdev->mddev->reshape_backwards ==
839                   (sector >= rdev->mddev->reshape_position)))
840                 bio->bi_sector = sector + rdev->new_data_offset;
841         else
842                 bio->bi_sector = sector + rdev->data_offset;
843         bio_add_page(bio, page, size, 0);
844         init_completion(&event);
845         bio->bi_private = &event;
846         bio->bi_end_io = bi_complete;
847         submit_bio(rw, bio);
848         wait_for_completion(&event);
849
850         ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
851         bio_put(bio);
852         return ret;
853 }
854 EXPORT_SYMBOL_GPL(sync_page_io);
855
856 static int read_disk_sb(struct md_rdev * rdev, int size)
857 {
858         char b[BDEVNAME_SIZE];
859         if (!rdev->sb_page) {
860                 MD_BUG();
861                 return -EINVAL;
862         }
863         if (rdev->sb_loaded)
864                 return 0;
865
866
867         if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
868                 goto fail;
869         rdev->sb_loaded = 1;
870         return 0;
871
872 fail:
873         printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
874                 bdevname(rdev->bdev,b));
875         return -EINVAL;
876 }
877
878 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
879 {
880         return  sb1->set_uuid0 == sb2->set_uuid0 &&
881                 sb1->set_uuid1 == sb2->set_uuid1 &&
882                 sb1->set_uuid2 == sb2->set_uuid2 &&
883                 sb1->set_uuid3 == sb2->set_uuid3;
884 }
885
886 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
887 {
888         int ret;
889         mdp_super_t *tmp1, *tmp2;
890
891         tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
892         tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
893
894         if (!tmp1 || !tmp2) {
895                 ret = 0;
896                 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
897                 goto abort;
898         }
899
900         *tmp1 = *sb1;
901         *tmp2 = *sb2;
902
903         /*
904          * nr_disks is not constant
905          */
906         tmp1->nr_disks = 0;
907         tmp2->nr_disks = 0;
908
909         ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
910 abort:
911         kfree(tmp1);
912         kfree(tmp2);
913         return ret;
914 }
915
916
917 static u32 md_csum_fold(u32 csum)
918 {
919         csum = (csum & 0xffff) + (csum >> 16);
920         return (csum & 0xffff) + (csum >> 16);
921 }
922
923 static unsigned int calc_sb_csum(mdp_super_t * sb)
924 {
925         u64 newcsum = 0;
926         u32 *sb32 = (u32*)sb;
927         int i;
928         unsigned int disk_csum, csum;
929
930         disk_csum = sb->sb_csum;
931         sb->sb_csum = 0;
932
933         for (i = 0; i < MD_SB_BYTES/4 ; i++)
934                 newcsum += sb32[i];
935         csum = (newcsum & 0xffffffff) + (newcsum>>32);
936
937
938 #ifdef CONFIG_ALPHA
939         /* This used to use csum_partial, which was wrong for several
940          * reasons including that different results are returned on
941          * different architectures.  It isn't critical that we get exactly
942          * the same return value as before (we always csum_fold before
943          * testing, and that removes any differences).  However as we
944          * know that csum_partial always returned a 16bit value on
945          * alphas, do a fold to maximise conformity to previous behaviour.
946          */
947         sb->sb_csum = md_csum_fold(disk_csum);
948 #else
949         sb->sb_csum = disk_csum;
950 #endif
951         return csum;
952 }
953
954
955 /*
956  * Handle superblock details.
957  * We want to be able to handle multiple superblock formats
958  * so we have a common interface to them all, and an array of
959  * different handlers.
960  * We rely on user-space to write the initial superblock, and support
961  * reading and updating of superblocks.
962  * Interface methods are:
963  *   int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
964  *      loads and validates a superblock on dev.
965  *      if refdev != NULL, compare superblocks on both devices
966  *    Return:
967  *      0 - dev has a superblock that is compatible with refdev
968  *      1 - dev has a superblock that is compatible and newer than refdev
969  *          so dev should be used as the refdev in future
970  *     -EINVAL superblock incompatible or invalid
971  *     -othererror e.g. -EIO
972  *
973  *   int validate_super(struct mddev *mddev, struct md_rdev *dev)
974  *      Verify that dev is acceptable into mddev.
975  *       The first time, mddev->raid_disks will be 0, and data from
976  *       dev should be merged in.  Subsequent calls check that dev
977  *       is new enough.  Return 0 or -EINVAL
978  *
979  *   void sync_super(struct mddev *mddev, struct md_rdev *dev)
980  *     Update the superblock for rdev with data in mddev
981  *     This does not write to disc.
982  *
983  */
984
985 struct super_type  {
986         char                *name;
987         struct module       *owner;
988         int                 (*load_super)(struct md_rdev *rdev,
989                                           struct md_rdev *refdev,
990                                           int minor_version);
991         int                 (*validate_super)(struct mddev *mddev,
992                                               struct md_rdev *rdev);
993         void                (*sync_super)(struct mddev *mddev,
994                                           struct md_rdev *rdev);
995         unsigned long long  (*rdev_size_change)(struct md_rdev *rdev,
996                                                 sector_t num_sectors);
997         int                 (*allow_new_offset)(struct md_rdev *rdev,
998                                                 unsigned long long new_offset);
999 };
1000
1001 /*
1002  * Check that the given mddev has no bitmap.
1003  *
1004  * This function is called from the run method of all personalities that do not
1005  * support bitmaps. It prints an error message and returns non-zero if mddev
1006  * has a bitmap. Otherwise, it returns 0.
1007  *
1008  */
1009 int md_check_no_bitmap(struct mddev *mddev)
1010 {
1011         if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
1012                 return 0;
1013         printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1014                 mdname(mddev), mddev->pers->name);
1015         return 1;
1016 }
1017 EXPORT_SYMBOL(md_check_no_bitmap);
1018
1019 /*
1020  * load_super for 0.90.0 
1021  */
1022 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1023 {
1024         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1025         mdp_super_t *sb;
1026         int ret;
1027
1028         /*
1029          * Calculate the position of the superblock (512byte sectors),
1030          * it's at the end of the disk.
1031          *
1032          * It also happens to be a multiple of 4Kb.
1033          */
1034         rdev->sb_start = calc_dev_sboffset(rdev);
1035
1036         ret = read_disk_sb(rdev, MD_SB_BYTES);
1037         if (ret) return ret;
1038
1039         ret = -EINVAL;
1040
1041         bdevname(rdev->bdev, b);
1042         sb = page_address(rdev->sb_page);
1043
1044         if (sb->md_magic != MD_SB_MAGIC) {
1045                 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1046                        b);
1047                 goto abort;
1048         }
1049
1050         if (sb->major_version != 0 ||
1051             sb->minor_version < 90 ||
1052             sb->minor_version > 91) {
1053                 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1054                         sb->major_version, sb->minor_version,
1055                         b);
1056                 goto abort;
1057         }
1058
1059         if (sb->raid_disks <= 0)
1060                 goto abort;
1061
1062         if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1063                 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1064                         b);
1065                 goto abort;
1066         }
1067
1068         rdev->preferred_minor = sb->md_minor;
1069         rdev->data_offset = 0;
1070         rdev->new_data_offset = 0;
1071         rdev->sb_size = MD_SB_BYTES;
1072         rdev->badblocks.shift = -1;
1073
1074         if (sb->level == LEVEL_MULTIPATH)
1075                 rdev->desc_nr = -1;
1076         else
1077                 rdev->desc_nr = sb->this_disk.number;
1078
1079         if (!refdev) {
1080                 ret = 1;
1081         } else {
1082                 __u64 ev1, ev2;
1083                 mdp_super_t *refsb = page_address(refdev->sb_page);
1084                 if (!uuid_equal(refsb, sb)) {
1085                         printk(KERN_WARNING "md: %s has different UUID to %s\n",
1086                                 b, bdevname(refdev->bdev,b2));
1087                         goto abort;
1088                 }
1089                 if (!sb_equal(refsb, sb)) {
1090                         printk(KERN_WARNING "md: %s has same UUID"
1091                                " but different superblock to %s\n",
1092                                b, bdevname(refdev->bdev, b2));
1093                         goto abort;
1094                 }
1095                 ev1 = md_event(sb);
1096                 ev2 = md_event(refsb);
1097                 if (ev1 > ev2)
1098                         ret = 1;
1099                 else 
1100                         ret = 0;
1101         }
1102         rdev->sectors = rdev->sb_start;
1103         /* Limit to 4TB as metadata cannot record more than that.
1104          * (not needed for Linear and RAID0 as metadata doesn't
1105          * record this size)
1106          */
1107         if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1108                 rdev->sectors = (2ULL << 32) - 2;
1109
1110         if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1111                 /* "this cannot possibly happen" ... */
1112                 ret = -EINVAL;
1113
1114  abort:
1115         return ret;
1116 }
1117
1118 /*
1119  * validate_super for 0.90.0
1120  */
1121 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1122 {
1123         mdp_disk_t *desc;
1124         mdp_super_t *sb = page_address(rdev->sb_page);
1125         __u64 ev1 = md_event(sb);
1126
1127         rdev->raid_disk = -1;
1128         clear_bit(Faulty, &rdev->flags);
1129         clear_bit(In_sync, &rdev->flags);
1130         clear_bit(WriteMostly, &rdev->flags);
1131
1132         if (mddev->raid_disks == 0) {
1133                 mddev->major_version = 0;
1134                 mddev->minor_version = sb->minor_version;
1135                 mddev->patch_version = sb->patch_version;
1136                 mddev->external = 0;
1137                 mddev->chunk_sectors = sb->chunk_size >> 9;
1138                 mddev->ctime = sb->ctime;
1139                 mddev->utime = sb->utime;
1140                 mddev->level = sb->level;
1141                 mddev->clevel[0] = 0;
1142                 mddev->layout = sb->layout;
1143                 mddev->raid_disks = sb->raid_disks;
1144                 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1145                 mddev->events = ev1;
1146                 mddev->bitmap_info.offset = 0;
1147                 mddev->bitmap_info.space = 0;
1148                 /* bitmap can use 60 K after the 4K superblocks */
1149                 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1150                 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1151                 mddev->reshape_backwards = 0;
1152
1153                 if (mddev->minor_version >= 91) {
1154                         mddev->reshape_position = sb->reshape_position;
1155                         mddev->delta_disks = sb->delta_disks;
1156                         mddev->new_level = sb->new_level;
1157                         mddev->new_layout = sb->new_layout;
1158                         mddev->new_chunk_sectors = sb->new_chunk >> 9;
1159                         if (mddev->delta_disks < 0)
1160                                 mddev->reshape_backwards = 1;
1161                 } else {
1162                         mddev->reshape_position = MaxSector;
1163                         mddev->delta_disks = 0;
1164                         mddev->new_level = mddev->level;
1165                         mddev->new_layout = mddev->layout;
1166                         mddev->new_chunk_sectors = mddev->chunk_sectors;
1167                 }
1168
1169                 if (sb->state & (1<<MD_SB_CLEAN))
1170                         mddev->recovery_cp = MaxSector;
1171                 else {
1172                         if (sb->events_hi == sb->cp_events_hi && 
1173                                 sb->events_lo == sb->cp_events_lo) {
1174                                 mddev->recovery_cp = sb->recovery_cp;
1175                         } else
1176                                 mddev->recovery_cp = 0;
1177                 }
1178
1179                 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1180                 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1181                 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1182                 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1183
1184                 mddev->max_disks = MD_SB_DISKS;
1185
1186                 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1187                     mddev->bitmap_info.file == NULL) {
1188                         mddev->bitmap_info.offset =
1189                                 mddev->bitmap_info.default_offset;
1190                         mddev->bitmap_info.space =
1191                                 mddev->bitmap_info.space;
1192                 }
1193
1194         } else if (mddev->pers == NULL) {
1195                 /* Insist on good event counter while assembling, except
1196                  * for spares (which don't need an event count) */
1197                 ++ev1;
1198                 if (sb->disks[rdev->desc_nr].state & (
1199                             (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1200                         if (ev1 < mddev->events) 
1201                                 return -EINVAL;
1202         } else if (mddev->bitmap) {
1203                 /* if adding to array with a bitmap, then we can accept an
1204                  * older device ... but not too old.
1205                  */
1206                 if (ev1 < mddev->bitmap->events_cleared)
1207                         return 0;
1208         } else {
1209                 if (ev1 < mddev->events)
1210                         /* just a hot-add of a new device, leave raid_disk at -1 */
1211                         return 0;
1212         }
1213
1214         if (mddev->level != LEVEL_MULTIPATH) {
1215                 desc = sb->disks + rdev->desc_nr;
1216
1217                 if (desc->state & (1<<MD_DISK_FAULTY))
1218                         set_bit(Faulty, &rdev->flags);
1219                 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1220                             desc->raid_disk < mddev->raid_disks */) {
1221                         set_bit(In_sync, &rdev->flags);
1222                         rdev->raid_disk = desc->raid_disk;
1223                 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1224                         /* active but not in sync implies recovery up to
1225                          * reshape position.  We don't know exactly where
1226                          * that is, so set to zero for now */
1227                         if (mddev->minor_version >= 91) {
1228                                 rdev->recovery_offset = 0;
1229                                 rdev->raid_disk = desc->raid_disk;
1230                         }
1231                 }
1232                 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1233                         set_bit(WriteMostly, &rdev->flags);
1234         } else /* MULTIPATH are always insync */
1235                 set_bit(In_sync, &rdev->flags);
1236         return 0;
1237 }
1238
1239 /*
1240  * sync_super for 0.90.0
1241  */
1242 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1243 {
1244         mdp_super_t *sb;
1245         struct md_rdev *rdev2;
1246         int next_spare = mddev->raid_disks;
1247
1248
1249         /* make rdev->sb match mddev data..
1250          *
1251          * 1/ zero out disks
1252          * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1253          * 3/ any empty disks < next_spare become removed
1254          *
1255          * disks[0] gets initialised to REMOVED because
1256          * we cannot be sure from other fields if it has
1257          * been initialised or not.
1258          */
1259         int i;
1260         int active=0, working=0,failed=0,spare=0,nr_disks=0;
1261
1262         rdev->sb_size = MD_SB_BYTES;
1263
1264         sb = page_address(rdev->sb_page);
1265
1266         memset(sb, 0, sizeof(*sb));
1267
1268         sb->md_magic = MD_SB_MAGIC;
1269         sb->major_version = mddev->major_version;
1270         sb->patch_version = mddev->patch_version;
1271         sb->gvalid_words  = 0; /* ignored */
1272         memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1273         memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1274         memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1275         memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1276
1277         sb->ctime = mddev->ctime;
1278         sb->level = mddev->level;
1279         sb->size = mddev->dev_sectors / 2;
1280         sb->raid_disks = mddev->raid_disks;
1281         sb->md_minor = mddev->md_minor;
1282         sb->not_persistent = 0;
1283         sb->utime = mddev->utime;
1284         sb->state = 0;
1285         sb->events_hi = (mddev->events>>32);
1286         sb->events_lo = (u32)mddev->events;
1287
1288         if (mddev->reshape_position == MaxSector)
1289                 sb->minor_version = 90;
1290         else {
1291                 sb->minor_version = 91;
1292                 sb->reshape_position = mddev->reshape_position;
1293                 sb->new_level = mddev->new_level;
1294                 sb->delta_disks = mddev->delta_disks;
1295                 sb->new_layout = mddev->new_layout;
1296                 sb->new_chunk = mddev->new_chunk_sectors << 9;
1297         }
1298         mddev->minor_version = sb->minor_version;
1299         if (mddev->in_sync)
1300         {
1301                 sb->recovery_cp = mddev->recovery_cp;
1302                 sb->cp_events_hi = (mddev->events>>32);
1303                 sb->cp_events_lo = (u32)mddev->events;
1304                 if (mddev->recovery_cp == MaxSector)
1305                         sb->state = (1<< MD_SB_CLEAN);
1306         } else
1307                 sb->recovery_cp = 0;
1308
1309         sb->layout = mddev->layout;
1310         sb->chunk_size = mddev->chunk_sectors << 9;
1311
1312         if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1313                 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1314
1315         sb->disks[0].state = (1<<MD_DISK_REMOVED);
1316         rdev_for_each(rdev2, mddev) {
1317                 mdp_disk_t *d;
1318                 int desc_nr;
1319                 int is_active = test_bit(In_sync, &rdev2->flags);
1320
1321                 if (rdev2->raid_disk >= 0 &&
1322                     sb->minor_version >= 91)
1323                         /* we have nowhere to store the recovery_offset,
1324                          * but if it is not below the reshape_position,
1325                          * we can piggy-back on that.
1326                          */
1327                         is_active = 1;
1328                 if (rdev2->raid_disk < 0 ||
1329                     test_bit(Faulty, &rdev2->flags))
1330                         is_active = 0;
1331                 if (is_active)
1332                         desc_nr = rdev2->raid_disk;
1333                 else
1334                         desc_nr = next_spare++;
1335                 rdev2->desc_nr = desc_nr;
1336                 d = &sb->disks[rdev2->desc_nr];
1337                 nr_disks++;
1338                 d->number = rdev2->desc_nr;
1339                 d->major = MAJOR(rdev2->bdev->bd_dev);
1340                 d->minor = MINOR(rdev2->bdev->bd_dev);
1341                 if (is_active)
1342                         d->raid_disk = rdev2->raid_disk;
1343                 else
1344                         d->raid_disk = rdev2->desc_nr; /* compatibility */
1345                 if (test_bit(Faulty, &rdev2->flags))
1346                         d->state = (1<<MD_DISK_FAULTY);
1347                 else if (is_active) {
1348                         d->state = (1<<MD_DISK_ACTIVE);
1349                         if (test_bit(In_sync, &rdev2->flags))
1350                                 d->state |= (1<<MD_DISK_SYNC);
1351                         active++;
1352                         working++;
1353                 } else {
1354                         d->state = 0;
1355                         spare++;
1356                         working++;
1357                 }
1358                 if (test_bit(WriteMostly, &rdev2->flags))
1359                         d->state |= (1<<MD_DISK_WRITEMOSTLY);
1360         }
1361         /* now set the "removed" and "faulty" bits on any missing devices */
1362         for (i=0 ; i < mddev->raid_disks ; i++) {
1363                 mdp_disk_t *d = &sb->disks[i];
1364                 if (d->state == 0 && d->number == 0) {
1365                         d->number = i;
1366                         d->raid_disk = i;
1367                         d->state = (1<<MD_DISK_REMOVED);
1368                         d->state |= (1<<MD_DISK_FAULTY);
1369                         failed++;
1370                 }
1371         }
1372         sb->nr_disks = nr_disks;
1373         sb->active_disks = active;
1374         sb->working_disks = working;
1375         sb->failed_disks = failed;
1376         sb->spare_disks = spare;
1377
1378         sb->this_disk = sb->disks[rdev->desc_nr];
1379         sb->sb_csum = calc_sb_csum(sb);
1380 }
1381
1382 /*
1383  * rdev_size_change for 0.90.0
1384  */
1385 static unsigned long long
1386 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1387 {
1388         if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1389                 return 0; /* component must fit device */
1390         if (rdev->mddev->bitmap_info.offset)
1391                 return 0; /* can't move bitmap */
1392         rdev->sb_start = calc_dev_sboffset(rdev);
1393         if (!num_sectors || num_sectors > rdev->sb_start)
1394                 num_sectors = rdev->sb_start;
1395         /* Limit to 4TB as metadata cannot record more than that.
1396          * 4TB == 2^32 KB, or 2*2^32 sectors.
1397          */
1398         if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1399                 num_sectors = (2ULL << 32) - 2;
1400         md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1401                        rdev->sb_page);
1402         md_super_wait(rdev->mddev);
1403         return num_sectors;
1404 }
1405
1406 static int
1407 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1408 {
1409         /* non-zero offset changes not possible with v0.90 */
1410         return new_offset == 0;
1411 }
1412
1413 /*
1414  * version 1 superblock
1415  */
1416
1417 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1418 {
1419         __le32 disk_csum;
1420         u32 csum;
1421         unsigned long long newcsum;
1422         int size = 256 + le32_to_cpu(sb->max_dev)*2;
1423         __le32 *isuper = (__le32*)sb;
1424
1425         disk_csum = sb->sb_csum;
1426         sb->sb_csum = 0;
1427         newcsum = 0;
1428         for (; size >= 4; size -= 4)
1429                 newcsum += le32_to_cpu(*isuper++);
1430
1431         if (size == 2)
1432                 newcsum += le16_to_cpu(*(__le16*) isuper);
1433
1434         csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1435         sb->sb_csum = disk_csum;
1436         return cpu_to_le32(csum);
1437 }
1438
1439 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1440                             int acknowledged);
1441 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1442 {
1443         struct mdp_superblock_1 *sb;
1444         int ret;
1445         sector_t sb_start;
1446         sector_t sectors;
1447         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1448         int bmask;
1449
1450         /*
1451          * Calculate the position of the superblock in 512byte sectors.
1452          * It is always aligned to a 4K boundary and
1453          * depeding on minor_version, it can be:
1454          * 0: At least 8K, but less than 12K, from end of device
1455          * 1: At start of device
1456          * 2: 4K from start of device.
1457          */
1458         switch(minor_version) {
1459         case 0:
1460                 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1461                 sb_start -= 8*2;
1462                 sb_start &= ~(sector_t)(4*2-1);
1463                 break;
1464         case 1:
1465                 sb_start = 0;
1466                 break;
1467         case 2:
1468                 sb_start = 8;
1469                 break;
1470         default:
1471                 return -EINVAL;
1472         }
1473         rdev->sb_start = sb_start;
1474
1475         /* superblock is rarely larger than 1K, but it can be larger,
1476          * and it is safe to read 4k, so we do that
1477          */
1478         ret = read_disk_sb(rdev, 4096);
1479         if (ret) return ret;
1480
1481
1482         sb = page_address(rdev->sb_page);
1483
1484         if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1485             sb->major_version != cpu_to_le32(1) ||
1486             le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1487             le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1488             (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1489                 return -EINVAL;
1490
1491         if (calc_sb_1_csum(sb) != sb->sb_csum) {
1492                 printk("md: invalid superblock checksum on %s\n",
1493                         bdevname(rdev->bdev,b));
1494                 return -EINVAL;
1495         }
1496         if (le64_to_cpu(sb->data_size) < 10) {
1497                 printk("md: data_size too small on %s\n",
1498                        bdevname(rdev->bdev,b));
1499                 return -EINVAL;
1500         }
1501         if (sb->pad0 ||
1502             sb->pad3[0] ||
1503             memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1504                 /* Some padding is non-zero, might be a new feature */
1505                 return -EINVAL;
1506
1507         rdev->preferred_minor = 0xffff;
1508         rdev->data_offset = le64_to_cpu(sb->data_offset);
1509         rdev->new_data_offset = rdev->data_offset;
1510         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1511             (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1512                 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1513         atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1514
1515         rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1516         bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1517         if (rdev->sb_size & bmask)
1518                 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1519
1520         if (minor_version
1521             && rdev->data_offset < sb_start + (rdev->sb_size/512))
1522                 return -EINVAL;
1523         if (minor_version
1524             && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1525                 return -EINVAL;
1526
1527         if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1528                 rdev->desc_nr = -1;
1529         else
1530                 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1531
1532         if (!rdev->bb_page) {
1533                 rdev->bb_page = alloc_page(GFP_KERNEL);
1534                 if (!rdev->bb_page)
1535                         return -ENOMEM;
1536         }
1537         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1538             rdev->badblocks.count == 0) {
1539                 /* need to load the bad block list.
1540                  * Currently we limit it to one page.
1541                  */
1542                 s32 offset;
1543                 sector_t bb_sector;
1544                 u64 *bbp;
1545                 int i;
1546                 int sectors = le16_to_cpu(sb->bblog_size);
1547                 if (sectors > (PAGE_SIZE / 512))
1548                         return -EINVAL;
1549                 offset = le32_to_cpu(sb->bblog_offset);
1550                 if (offset == 0)
1551                         return -EINVAL;
1552                 bb_sector = (long long)offset;
1553                 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1554                                   rdev->bb_page, READ, true))
1555                         return -EIO;
1556                 bbp = (u64 *)page_address(rdev->bb_page);
1557                 rdev->badblocks.shift = sb->bblog_shift;
1558                 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1559                         u64 bb = le64_to_cpu(*bbp);
1560                         int count = bb & (0x3ff);
1561                         u64 sector = bb >> 10;
1562                         sector <<= sb->bblog_shift;
1563                         count <<= sb->bblog_shift;
1564                         if (bb + 1 == 0)
1565                                 break;
1566                         if (md_set_badblocks(&rdev->badblocks,
1567                                              sector, count, 1) == 0)
1568                                 return -EINVAL;
1569                 }
1570         } else if (sb->bblog_offset != 0)
1571                 rdev->badblocks.shift = 0;
1572
1573         if (!refdev) {
1574                 ret = 1;
1575         } else {
1576                 __u64 ev1, ev2;
1577                 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1578
1579                 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1580                     sb->level != refsb->level ||
1581                     sb->layout != refsb->layout ||
1582                     sb->chunksize != refsb->chunksize) {
1583                         printk(KERN_WARNING "md: %s has strangely different"
1584                                 " superblock to %s\n",
1585                                 bdevname(rdev->bdev,b),
1586                                 bdevname(refdev->bdev,b2));
1587                         return -EINVAL;
1588                 }
1589                 ev1 = le64_to_cpu(sb->events);
1590                 ev2 = le64_to_cpu(refsb->events);
1591
1592                 if (ev1 > ev2)
1593                         ret = 1;
1594                 else
1595                         ret = 0;
1596         }
1597         if (minor_version) {
1598                 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1599                 sectors -= rdev->data_offset;
1600         } else
1601                 sectors = rdev->sb_start;
1602         if (sectors < le64_to_cpu(sb->data_size))
1603                 return -EINVAL;
1604         rdev->sectors = le64_to_cpu(sb->data_size);
1605         return ret;
1606 }
1607
1608 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1609 {
1610         struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1611         __u64 ev1 = le64_to_cpu(sb->events);
1612
1613         rdev->raid_disk = -1;
1614         clear_bit(Faulty, &rdev->flags);
1615         clear_bit(In_sync, &rdev->flags);
1616         clear_bit(WriteMostly, &rdev->flags);
1617
1618         if (mddev->raid_disks == 0) {
1619                 mddev->major_version = 1;
1620                 mddev->patch_version = 0;
1621                 mddev->external = 0;
1622                 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1623                 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1624                 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1625                 mddev->level = le32_to_cpu(sb->level);
1626                 mddev->clevel[0] = 0;
1627                 mddev->layout = le32_to_cpu(sb->layout);
1628                 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1629                 mddev->dev_sectors = le64_to_cpu(sb->size);
1630                 mddev->events = ev1;
1631                 mddev->bitmap_info.offset = 0;
1632                 mddev->bitmap_info.space = 0;
1633                 /* Default location for bitmap is 1K after superblock
1634                  * using 3K - total of 4K
1635                  */
1636                 mddev->bitmap_info.default_offset = 1024 >> 9;
1637                 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1638                 mddev->reshape_backwards = 0;
1639
1640                 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1641                 memcpy(mddev->uuid, sb->set_uuid, 16);
1642
1643                 mddev->max_disks =  (4096-256)/2;
1644
1645                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1646                     mddev->bitmap_info.file == NULL) {
1647                         mddev->bitmap_info.offset =
1648                                 (__s32)le32_to_cpu(sb->bitmap_offset);
1649                         /* Metadata doesn't record how much space is available.
1650                          * For 1.0, we assume we can use up to the superblock
1651                          * if before, else to 4K beyond superblock.
1652                          * For others, assume no change is possible.
1653                          */
1654                         if (mddev->minor_version > 0)
1655                                 mddev->bitmap_info.space = 0;
1656                         else if (mddev->bitmap_info.offset > 0)
1657                                 mddev->bitmap_info.space =
1658                                         8 - mddev->bitmap_info.offset;
1659                         else
1660                                 mddev->bitmap_info.space =
1661                                         -mddev->bitmap_info.offset;
1662                 }
1663
1664                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1665                         mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1666                         mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1667                         mddev->new_level = le32_to_cpu(sb->new_level);
1668                         mddev->new_layout = le32_to_cpu(sb->new_layout);
1669                         mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1670                         if (mddev->delta_disks < 0 ||
1671                             (mddev->delta_disks == 0 &&
1672                              (le32_to_cpu(sb->feature_map)
1673                               & MD_FEATURE_RESHAPE_BACKWARDS)))
1674                                 mddev->reshape_backwards = 1;
1675                 } else {
1676                         mddev->reshape_position = MaxSector;
1677                         mddev->delta_disks = 0;
1678                         mddev->new_level = mddev->level;
1679                         mddev->new_layout = mddev->layout;
1680                         mddev->new_chunk_sectors = mddev->chunk_sectors;
1681                 }
1682
1683         } else if (mddev->pers == NULL) {
1684                 /* Insist of good event counter while assembling, except for
1685                  * spares (which don't need an event count) */
1686                 ++ev1;
1687                 if (rdev->desc_nr >= 0 &&
1688                     rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1689                     le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1690                         if (ev1 < mddev->events)
1691                                 return -EINVAL;
1692         } else if (mddev->bitmap) {
1693                 /* If adding to array with a bitmap, then we can accept an
1694                  * older device, but not too old.
1695                  */
1696                 if (ev1 < mddev->bitmap->events_cleared)
1697                         return 0;
1698         } else {
1699                 if (ev1 < mddev->events)
1700                         /* just a hot-add of a new device, leave raid_disk at -1 */
1701                         return 0;
1702         }
1703         if (mddev->level != LEVEL_MULTIPATH) {
1704                 int role;
1705                 if (rdev->desc_nr < 0 ||
1706                     rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1707                         role = 0xffff;
1708                         rdev->desc_nr = -1;
1709                 } else
1710                         role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1711                 switch(role) {
1712                 case 0xffff: /* spare */
1713                         break;
1714                 case 0xfffe: /* faulty */
1715                         set_bit(Faulty, &rdev->flags);
1716                         break;
1717                 default:
1718                         if ((le32_to_cpu(sb->feature_map) &
1719                              MD_FEATURE_RECOVERY_OFFSET))
1720                                 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1721                         else
1722                                 set_bit(In_sync, &rdev->flags);
1723                         rdev->raid_disk = role;
1724                         break;
1725                 }
1726                 if (sb->devflags & WriteMostly1)
1727                         set_bit(WriteMostly, &rdev->flags);
1728                 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1729                         set_bit(Replacement, &rdev->flags);
1730         } else /* MULTIPATH are always insync */
1731                 set_bit(In_sync, &rdev->flags);
1732
1733         return 0;
1734 }
1735
1736 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1737 {
1738         struct mdp_superblock_1 *sb;
1739         struct md_rdev *rdev2;
1740         int max_dev, i;
1741         /* make rdev->sb match mddev and rdev data. */
1742
1743         sb = page_address(rdev->sb_page);
1744
1745         sb->feature_map = 0;
1746         sb->pad0 = 0;
1747         sb->recovery_offset = cpu_to_le64(0);
1748         memset(sb->pad3, 0, sizeof(sb->pad3));
1749
1750         sb->utime = cpu_to_le64((__u64)mddev->utime);
1751         sb->events = cpu_to_le64(mddev->events);
1752         if (mddev->in_sync)
1753                 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1754         else
1755                 sb->resync_offset = cpu_to_le64(0);
1756
1757         sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1758
1759         sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1760         sb->size = cpu_to_le64(mddev->dev_sectors);
1761         sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1762         sb->level = cpu_to_le32(mddev->level);
1763         sb->layout = cpu_to_le32(mddev->layout);
1764
1765         if (test_bit(WriteMostly, &rdev->flags))
1766                 sb->devflags |= WriteMostly1;
1767         else
1768                 sb->devflags &= ~WriteMostly1;
1769         sb->data_offset = cpu_to_le64(rdev->data_offset);
1770         sb->data_size = cpu_to_le64(rdev->sectors);
1771
1772         if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1773                 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1774                 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1775         }
1776
1777         if (rdev->raid_disk >= 0 &&
1778             !test_bit(In_sync, &rdev->flags)) {
1779                 sb->feature_map |=
1780                         cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1781                 sb->recovery_offset =
1782                         cpu_to_le64(rdev->recovery_offset);
1783         }
1784         if (test_bit(Replacement, &rdev->flags))
1785                 sb->feature_map |=
1786                         cpu_to_le32(MD_FEATURE_REPLACEMENT);
1787
1788         if (mddev->reshape_position != MaxSector) {
1789                 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1790                 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1791                 sb->new_layout = cpu_to_le32(mddev->new_layout);
1792                 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1793                 sb->new_level = cpu_to_le32(mddev->new_level);
1794                 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1795                 if (mddev->delta_disks == 0 &&
1796                     mddev->reshape_backwards)
1797                         sb->feature_map
1798                                 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1799                 if (rdev->new_data_offset != rdev->data_offset) {
1800                         sb->feature_map
1801                                 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1802                         sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1803                                                              - rdev->data_offset));
1804                 }
1805         }
1806
1807         if (rdev->badblocks.count == 0)
1808                 /* Nothing to do for bad blocks*/ ;
1809         else if (sb->bblog_offset == 0)
1810                 /* Cannot record bad blocks on this device */
1811                 md_error(mddev, rdev);
1812         else {
1813                 struct badblocks *bb = &rdev->badblocks;
1814                 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1815                 u64 *p = bb->page;
1816                 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1817                 if (bb->changed) {
1818                         unsigned seq;
1819
1820 retry:
1821                         seq = read_seqbegin(&bb->lock);
1822
1823                         memset(bbp, 0xff, PAGE_SIZE);
1824
1825                         for (i = 0 ; i < bb->count ; i++) {
1826                                 u64 internal_bb = p[i];
1827                                 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1828                                                 | BB_LEN(internal_bb));
1829                                 bbp[i] = cpu_to_le64(store_bb);
1830                         }
1831                         bb->changed = 0;
1832                         if (read_seqretry(&bb->lock, seq))
1833                                 goto retry;
1834
1835                         bb->sector = (rdev->sb_start +
1836                                       (int)le32_to_cpu(sb->bblog_offset));
1837                         bb->size = le16_to_cpu(sb->bblog_size);
1838                 }
1839         }
1840
1841         max_dev = 0;
1842         rdev_for_each(rdev2, mddev)
1843                 if (rdev2->desc_nr+1 > max_dev)
1844                         max_dev = rdev2->desc_nr+1;
1845
1846         if (max_dev > le32_to_cpu(sb->max_dev)) {
1847                 int bmask;
1848                 sb->max_dev = cpu_to_le32(max_dev);
1849                 rdev->sb_size = max_dev * 2 + 256;
1850                 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1851                 if (rdev->sb_size & bmask)
1852                         rdev->sb_size = (rdev->sb_size | bmask) + 1;
1853         } else
1854                 max_dev = le32_to_cpu(sb->max_dev);
1855
1856         for (i=0; i<max_dev;i++)
1857                 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1858         
1859         rdev_for_each(rdev2, mddev) {
1860                 i = rdev2->desc_nr;
1861                 if (test_bit(Faulty, &rdev2->flags))
1862                         sb->dev_roles[i] = cpu_to_le16(0xfffe);
1863                 else if (test_bit(In_sync, &rdev2->flags))
1864                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1865                 else if (rdev2->raid_disk >= 0)
1866                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1867                 else
1868                         sb->dev_roles[i] = cpu_to_le16(0xffff);
1869         }
1870
1871         sb->sb_csum = calc_sb_1_csum(sb);
1872 }
1873
1874 static unsigned long long
1875 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1876 {
1877         struct mdp_superblock_1 *sb;
1878         sector_t max_sectors;
1879         if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1880                 return 0; /* component must fit device */
1881         if (rdev->data_offset != rdev->new_data_offset)
1882                 return 0; /* too confusing */
1883         if (rdev->sb_start < rdev->data_offset) {
1884                 /* minor versions 1 and 2; superblock before data */
1885                 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1886                 max_sectors -= rdev->data_offset;
1887                 if (!num_sectors || num_sectors > max_sectors)
1888                         num_sectors = max_sectors;
1889         } else if (rdev->mddev->bitmap_info.offset) {
1890                 /* minor version 0 with bitmap we can't move */
1891                 return 0;
1892         } else {
1893                 /* minor version 0; superblock after data */
1894                 sector_t sb_start;
1895                 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1896                 sb_start &= ~(sector_t)(4*2 - 1);
1897                 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1898                 if (!num_sectors || num_sectors > max_sectors)
1899                         num_sectors = max_sectors;
1900                 rdev->sb_start = sb_start;
1901         }
1902         sb = page_address(rdev->sb_page);
1903         sb->data_size = cpu_to_le64(num_sectors);
1904         sb->super_offset = rdev->sb_start;
1905         sb->sb_csum = calc_sb_1_csum(sb);
1906         md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1907                        rdev->sb_page);
1908         md_super_wait(rdev->mddev);
1909         return num_sectors;
1910
1911 }
1912
1913 static int
1914 super_1_allow_new_offset(struct md_rdev *rdev,
1915                          unsigned long long new_offset)
1916 {
1917         /* All necessary checks on new >= old have been done */
1918         struct bitmap *bitmap;
1919         if (new_offset >= rdev->data_offset)
1920                 return 1;
1921
1922         /* with 1.0 metadata, there is no metadata to tread on
1923          * so we can always move back */
1924         if (rdev->mddev->minor_version == 0)
1925                 return 1;
1926
1927         /* otherwise we must be sure not to step on
1928          * any metadata, so stay:
1929          * 36K beyond start of superblock
1930          * beyond end of badblocks
1931          * beyond write-intent bitmap
1932          */
1933         if (rdev->sb_start + (32+4)*2 > new_offset)
1934                 return 0;
1935         bitmap = rdev->mddev->bitmap;
1936         if (bitmap && !rdev->mddev->bitmap_info.file &&
1937             rdev->sb_start + rdev->mddev->bitmap_info.offset +
1938             bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1939                 return 0;
1940         if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1941                 return 0;
1942
1943         return 1;
1944 }
1945
1946 static struct super_type super_types[] = {
1947         [0] = {
1948                 .name   = "0.90.0",
1949                 .owner  = THIS_MODULE,
1950                 .load_super         = super_90_load,
1951                 .validate_super     = super_90_validate,
1952                 .sync_super         = super_90_sync,
1953                 .rdev_size_change   = super_90_rdev_size_change,
1954                 .allow_new_offset   = super_90_allow_new_offset,
1955         },
1956         [1] = {
1957                 .name   = "md-1",
1958                 .owner  = THIS_MODULE,
1959                 .load_super         = super_1_load,
1960                 .validate_super     = super_1_validate,
1961                 .sync_super         = super_1_sync,
1962                 .rdev_size_change   = super_1_rdev_size_change,
1963                 .allow_new_offset   = super_1_allow_new_offset,
1964         },
1965 };
1966
1967 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1968 {
1969         if (mddev->sync_super) {
1970                 mddev->sync_super(mddev, rdev);
1971                 return;
1972         }
1973
1974         BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1975
1976         super_types[mddev->major_version].sync_super(mddev, rdev);
1977 }
1978
1979 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1980 {
1981         struct md_rdev *rdev, *rdev2;
1982
1983         rcu_read_lock();
1984         rdev_for_each_rcu(rdev, mddev1)
1985                 rdev_for_each_rcu(rdev2, mddev2)
1986                         if (rdev->bdev->bd_contains ==
1987                             rdev2->bdev->bd_contains) {
1988                                 rcu_read_unlock();
1989                                 return 1;
1990                         }
1991         rcu_read_unlock();
1992         return 0;
1993 }
1994
1995 static LIST_HEAD(pending_raid_disks);
1996
1997 /*
1998  * Try to register data integrity profile for an mddev
1999  *
2000  * This is called when an array is started and after a disk has been kicked
2001  * from the array. It only succeeds if all working and active component devices
2002  * are integrity capable with matching profiles.
2003  */
2004 int md_integrity_register(struct mddev *mddev)
2005 {
2006         struct md_rdev *rdev, *reference = NULL;
2007
2008         if (list_empty(&mddev->disks))
2009                 return 0; /* nothing to do */
2010         if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2011                 return 0; /* shouldn't register, or already is */
2012         rdev_for_each(rdev, mddev) {
2013                 /* skip spares and non-functional disks */
2014                 if (test_bit(Faulty, &rdev->flags))
2015                         continue;
2016                 if (rdev->raid_disk < 0)
2017                         continue;
2018                 if (!reference) {
2019                         /* Use the first rdev as the reference */
2020                         reference = rdev;
2021                         continue;
2022                 }
2023                 /* does this rdev's profile match the reference profile? */
2024                 if (blk_integrity_compare(reference->bdev->bd_disk,
2025                                 rdev->bdev->bd_disk) < 0)
2026                         return -EINVAL;
2027         }
2028         if (!reference || !bdev_get_integrity(reference->bdev))
2029                 return 0;
2030         /*
2031          * All component devices are integrity capable and have matching
2032          * profiles, register the common profile for the md device.
2033          */
2034         if (blk_integrity_register(mddev->gendisk,
2035                         bdev_get_integrity(reference->bdev)) != 0) {
2036                 printk(KERN_ERR "md: failed to register integrity for %s\n",
2037                         mdname(mddev));
2038                 return -EINVAL;
2039         }
2040         printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
2041         if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2042                 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
2043                        mdname(mddev));
2044                 return -EINVAL;
2045         }
2046         return 0;
2047 }
2048 EXPORT_SYMBOL(md_integrity_register);
2049
2050 /* Disable data integrity if non-capable/non-matching disk is being added */
2051 void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2052 {
2053         struct blk_integrity *bi_rdev;
2054         struct blk_integrity *bi_mddev;
2055
2056         if (!mddev->gendisk)
2057                 return;
2058
2059         bi_rdev = bdev_get_integrity(rdev->bdev);
2060         bi_mddev = blk_get_integrity(mddev->gendisk);
2061
2062         if (!bi_mddev) /* nothing to do */
2063                 return;
2064         if (rdev->raid_disk < 0) /* skip spares */
2065                 return;
2066         if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2067                                              rdev->bdev->bd_disk) >= 0)
2068                 return;
2069         printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2070         blk_integrity_unregister(mddev->gendisk);
2071 }
2072 EXPORT_SYMBOL(md_integrity_add_rdev);
2073
2074 static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
2075 {
2076         char b[BDEVNAME_SIZE];
2077         struct kobject *ko;
2078         char *s;
2079         int err;
2080
2081         if (rdev->mddev) {
2082                 MD_BUG();
2083                 return -EINVAL;
2084         }
2085
2086         /* prevent duplicates */
2087         if (find_rdev(mddev, rdev->bdev->bd_dev))
2088                 return -EEXIST;
2089
2090         /* make sure rdev->sectors exceeds mddev->dev_sectors */
2091         if (rdev->sectors && (mddev->dev_sectors == 0 ||
2092                         rdev->sectors < mddev->dev_sectors)) {
2093                 if (mddev->pers) {
2094                         /* Cannot change size, so fail
2095                          * If mddev->level <= 0, then we don't care
2096                          * about aligning sizes (e.g. linear)
2097                          */
2098                         if (mddev->level > 0)
2099                                 return -ENOSPC;
2100                 } else
2101                         mddev->dev_sectors = rdev->sectors;
2102         }
2103
2104         /* Verify rdev->desc_nr is unique.
2105          * If it is -1, assign a free number, else
2106          * check number is not in use
2107          */
2108         if (rdev->desc_nr < 0) {
2109                 int choice = 0;
2110                 if (mddev->pers) choice = mddev->raid_disks;
2111                 while (find_rdev_nr(mddev, choice))
2112                         choice++;
2113                 rdev->desc_nr = choice;
2114         } else {
2115                 if (find_rdev_nr(mddev, rdev->desc_nr))
2116                         return -EBUSY;
2117         }
2118         if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2119                 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2120                        mdname(mddev), mddev->max_disks);
2121                 return -EBUSY;
2122         }
2123         bdevname(rdev->bdev,b);
2124         while ( (s=strchr(b, '/')) != NULL)
2125                 *s = '!';
2126
2127         rdev->mddev = mddev;
2128         printk(KERN_INFO "md: bind<%s>\n", b);
2129
2130         if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2131                 goto fail;
2132
2133         ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2134         if (sysfs_create_link(&rdev->kobj, ko, "block"))
2135                 /* failure here is OK */;
2136         rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2137
2138         list_add_rcu(&rdev->same_set, &mddev->disks);
2139         bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2140
2141         /* May as well allow recovery to be retried once */
2142         mddev->recovery_disabled++;
2143
2144         return 0;
2145
2146  fail:
2147         printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2148                b, mdname(mddev));
2149         return err;
2150 }
2151
2152 static void md_delayed_delete(struct work_struct *ws)
2153 {
2154         struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2155         kobject_del(&rdev->kobj);
2156         kobject_put(&rdev->kobj);
2157 }
2158
2159 static void unbind_rdev_from_array(struct md_rdev * rdev)
2160 {
2161         char b[BDEVNAME_SIZE];
2162         if (!rdev->mddev) {
2163                 MD_BUG();
2164                 return;
2165         }
2166         bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2167         list_del_rcu(&rdev->same_set);
2168         printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2169         rdev->mddev = NULL;
2170         sysfs_remove_link(&rdev->kobj, "block");
2171         sysfs_put(rdev->sysfs_state);
2172         rdev->sysfs_state = NULL;
2173         rdev->badblocks.count = 0;
2174         /* We need to delay this, otherwise we can deadlock when
2175          * writing to 'remove' to "dev/state".  We also need
2176          * to delay it due to rcu usage.
2177          */
2178         synchronize_rcu();
2179         INIT_WORK(&rdev->del_work, md_delayed_delete);
2180         kobject_get(&rdev->kobj);
2181         queue_work(md_misc_wq, &rdev->del_work);
2182 }
2183
2184 /*
2185  * prevent the device from being mounted, repartitioned or
2186  * otherwise reused by a RAID array (or any other kernel
2187  * subsystem), by bd_claiming the device.
2188  */
2189 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2190 {
2191         int err = 0;
2192         struct block_device *bdev;
2193         char b[BDEVNAME_SIZE];
2194
2195         bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2196                                  shared ? (struct md_rdev *)lock_rdev : rdev);
2197         if (IS_ERR(bdev)) {
2198                 printk(KERN_ERR "md: could not open %s.\n",
2199                         __bdevname(dev, b));
2200                 return PTR_ERR(bdev);
2201         }
2202         rdev->bdev = bdev;
2203         return err;
2204 }
2205
2206 static void unlock_rdev(struct md_rdev *rdev)
2207 {
2208         struct block_device *bdev = rdev->bdev;
2209         rdev->bdev = NULL;
2210         if (!bdev)
2211                 MD_BUG();
2212         blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2213 }
2214
2215 void md_autodetect_dev(dev_t dev);
2216
2217 static void export_rdev(struct md_rdev * rdev)
2218 {
2219         char b[BDEVNAME_SIZE];
2220         printk(KERN_INFO "md: export_rdev(%s)\n",
2221                 bdevname(rdev->bdev,b));
2222         if (rdev->mddev)
2223                 MD_BUG();
2224         md_rdev_clear(rdev);
2225 #ifndef MODULE
2226         if (test_bit(AutoDetected, &rdev->flags))
2227                 md_autodetect_dev(rdev->bdev->bd_dev);
2228 #endif
2229         unlock_rdev(rdev);
2230         kobject_put(&rdev->kobj);
2231 }
2232
2233 static void kick_rdev_from_array(struct md_rdev * rdev)
2234 {
2235         unbind_rdev_from_array(rdev);
2236         export_rdev(rdev);
2237 }
2238
2239 static void export_array(struct mddev *mddev)
2240 {
2241         struct md_rdev *rdev, *tmp;
2242
2243         rdev_for_each_safe(rdev, tmp, mddev) {
2244                 if (!rdev->mddev) {
2245                         MD_BUG();
2246                         continue;
2247                 }
2248                 kick_rdev_from_array(rdev);
2249         }
2250         if (!list_empty(&mddev->disks))
2251                 MD_BUG();
2252         mddev->raid_disks = 0;
2253         mddev->major_version = 0;
2254 }
2255
2256 static void print_desc(mdp_disk_t *desc)
2257 {
2258         printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2259                 desc->major,desc->minor,desc->raid_disk,desc->state);
2260 }
2261
2262 static void print_sb_90(mdp_super_t *sb)
2263 {
2264         int i;
2265
2266         printk(KERN_INFO 
2267                 "md:  SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2268                 sb->major_version, sb->minor_version, sb->patch_version,
2269                 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2270                 sb->ctime);
2271         printk(KERN_INFO "md:     L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2272                 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2273                 sb->md_minor, sb->layout, sb->chunk_size);
2274         printk(KERN_INFO "md:     UT:%08x ST:%d AD:%d WD:%d"
2275                 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2276                 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2277                 sb->failed_disks, sb->spare_disks,
2278                 sb->sb_csum, (unsigned long)sb->events_lo);
2279
2280         printk(KERN_INFO);
2281         for (i = 0; i < MD_SB_DISKS; i++) {
2282                 mdp_disk_t *desc;
2283
2284                 desc = sb->disks + i;
2285                 if (desc->number || desc->major || desc->minor ||
2286                     desc->raid_disk || (desc->state && (desc->state != 4))) {
2287                         printk("     D %2d: ", i);
2288                         print_desc(desc);
2289                 }
2290         }
2291         printk(KERN_INFO "md:     THIS: ");
2292         print_desc(&sb->this_disk);
2293 }
2294
2295 static void print_sb_1(struct mdp_superblock_1 *sb)
2296 {
2297         __u8 *uuid;
2298
2299         uuid = sb->set_uuid;
2300         printk(KERN_INFO
2301                "md:  SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2302                "md:    Name: \"%s\" CT:%llu\n",
2303                 le32_to_cpu(sb->major_version),
2304                 le32_to_cpu(sb->feature_map),
2305                 uuid,
2306                 sb->set_name,
2307                 (unsigned long long)le64_to_cpu(sb->ctime)
2308                        & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2309
2310         uuid = sb->device_uuid;
2311         printk(KERN_INFO
2312                "md:       L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2313                         " RO:%llu\n"
2314                "md:     Dev:%08x UUID: %pU\n"
2315                "md:       (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2316                "md:         (MaxDev:%u) \n",
2317                 le32_to_cpu(sb->level),
2318                 (unsigned long long)le64_to_cpu(sb->size),
2319                 le32_to_cpu(sb->raid_disks),
2320                 le32_to_cpu(sb->layout),
2321                 le32_to_cpu(sb->chunksize),
2322                 (unsigned long long)le64_to_cpu(sb->data_offset),
2323                 (unsigned long long)le64_to_cpu(sb->data_size),
2324                 (unsigned long long)le64_to_cpu(sb->super_offset),
2325                 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2326                 le32_to_cpu(sb->dev_number),
2327                 uuid,
2328                 sb->devflags,
2329                 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2330                 (unsigned long long)le64_to_cpu(sb->events),
2331                 (unsigned long long)le64_to_cpu(sb->resync_offset),
2332                 le32_to_cpu(sb->sb_csum),
2333                 le32_to_cpu(sb->max_dev)
2334                 );
2335 }
2336
2337 static void print_rdev(struct md_rdev *rdev, int major_version)
2338 {
2339         char b[BDEVNAME_SIZE];
2340         printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2341                 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2342                 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2343                 rdev->desc_nr);
2344         if (rdev->sb_loaded) {
2345                 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2346                 switch (major_version) {
2347                 case 0:
2348                         print_sb_90(page_address(rdev->sb_page));
2349                         break;
2350                 case 1:
2351                         print_sb_1(page_address(rdev->sb_page));
2352                         break;
2353                 }
2354         } else
2355                 printk(KERN_INFO "md: no rdev superblock!\n");
2356 }
2357
2358 static void md_print_devices(void)
2359 {
2360         struct list_head *tmp;
2361         struct md_rdev *rdev;
2362         struct mddev *mddev;
2363         char b[BDEVNAME_SIZE];
2364
2365         printk("\n");
2366         printk("md:     **********************************\n");
2367         printk("md:     * <COMPLETE RAID STATE PRINTOUT> *\n");
2368         printk("md:     **********************************\n");
2369         for_each_mddev(mddev, tmp) {
2370
2371                 if (mddev->bitmap)
2372                         bitmap_print_sb(mddev->bitmap);
2373                 else
2374                         printk("%s: ", mdname(mddev));
2375                 rdev_for_each(rdev, mddev)
2376                         printk("<%s>", bdevname(rdev->bdev,b));
2377                 printk("\n");
2378
2379                 rdev_for_each(rdev, mddev)
2380                         print_rdev(rdev, mddev->major_version);
2381         }
2382         printk("md:     **********************************\n");
2383         printk("\n");
2384 }
2385
2386
2387 static void sync_sbs(struct mddev * mddev, int nospares)
2388 {
2389         /* Update each superblock (in-memory image), but
2390          * if we are allowed to, skip spares which already
2391          * have the right event counter, or have one earlier
2392          * (which would mean they aren't being marked as dirty
2393          * with the rest of the array)
2394          */
2395         struct md_rdev *rdev;
2396         rdev_for_each(rdev, mddev) {
2397                 if (rdev->sb_events == mddev->events ||
2398                     (nospares &&
2399                      rdev->raid_disk < 0 &&
2400                      rdev->sb_events+1 == mddev->events)) {
2401                         /* Don't update this superblock */
2402                         rdev->sb_loaded = 2;
2403                 } else {
2404                         sync_super(mddev, rdev);
2405                         rdev->sb_loaded = 1;
2406                 }
2407         }
2408 }
2409
2410 static void md_update_sb(struct mddev * mddev, int force_change)
2411 {
2412         struct md_rdev *rdev;
2413         int sync_req;
2414         int nospares = 0;
2415         int any_badblocks_changed = 0;
2416
2417         if (mddev->ro) {
2418                 if (force_change)
2419                         set_bit(MD_CHANGE_DEVS, &mddev->flags);
2420                 return;
2421         }
2422 repeat:
2423         /* First make sure individual recovery_offsets are correct */
2424         rdev_for_each(rdev, mddev) {
2425                 if (rdev->raid_disk >= 0 &&
2426                     mddev->delta_disks >= 0 &&
2427                     !test_bit(In_sync, &rdev->flags) &&
2428                     mddev->curr_resync_completed > rdev->recovery_offset)
2429                                 rdev->recovery_offset = mddev->curr_resync_completed;
2430
2431         }       
2432         if (!mddev->persistent) {
2433                 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2434                 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2435                 if (!mddev->external) {
2436                         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2437                         rdev_for_each(rdev, mddev) {
2438                                 if (rdev->badblocks.changed) {
2439                                         rdev->badblocks.changed = 0;
2440                                         md_ack_all_badblocks(&rdev->badblocks);
2441                                         md_error(mddev, rdev);
2442                                 }
2443                                 clear_bit(Blocked, &rdev->flags);
2444                                 clear_bit(BlockedBadBlocks, &rdev->flags);
2445                                 wake_up(&rdev->blocked_wait);
2446                         }
2447                 }
2448                 wake_up(&mddev->sb_wait);
2449                 return;
2450         }
2451
2452         spin_lock_irq(&mddev->write_lock);
2453
2454         mddev->utime = get_seconds();
2455
2456         if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2457                 force_change = 1;
2458         if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2459                 /* just a clean<-> dirty transition, possibly leave spares alone,
2460                  * though if events isn't the right even/odd, we will have to do
2461                  * spares after all
2462                  */
2463                 nospares = 1;
2464         if (force_change)
2465                 nospares = 0;
2466         if (mddev->degraded)
2467                 /* If the array is degraded, then skipping spares is both
2468                  * dangerous and fairly pointless.
2469                  * Dangerous because a device that was removed from the array
2470                  * might have a event_count that still looks up-to-date,
2471                  * so it can be re-added without a resync.
2472                  * Pointless because if there are any spares to skip,
2473                  * then a recovery will happen and soon that array won't
2474                  * be degraded any more and the spare can go back to sleep then.
2475                  */
2476                 nospares = 0;
2477
2478         sync_req = mddev->in_sync;
2479
2480         /* If this is just a dirty<->clean transition, and the array is clean
2481          * and 'events' is odd, we can roll back to the previous clean state */
2482         if (nospares
2483             && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2484             && mddev->can_decrease_events
2485             && mddev->events != 1) {
2486                 mddev->events--;
2487                 mddev->can_decrease_events = 0;
2488         } else {
2489                 /* otherwise we have to go forward and ... */
2490                 mddev->events ++;
2491                 mddev->can_decrease_events = nospares;
2492         }
2493
2494         if (!mddev->events) {
2495                 /*
2496                  * oops, this 64-bit counter should never wrap.
2497                  * Either we are in around ~1 trillion A.C., assuming
2498                  * 1 reboot per second, or we have a bug:
2499                  */
2500                 MD_BUG();
2501                 mddev->events --;
2502         }
2503
2504         rdev_for_each(rdev, mddev) {
2505                 if (rdev->badblocks.changed)
2506                         any_badblocks_changed++;
2507                 if (test_bit(Faulty, &rdev->flags))
2508                         set_bit(FaultRecorded, &rdev->flags);
2509         }
2510
2511         sync_sbs(mddev, nospares);
2512         spin_unlock_irq(&mddev->write_lock);
2513
2514         pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2515                  mdname(mddev), mddev->in_sync);
2516
2517         bitmap_update_sb(mddev->bitmap);
2518         rdev_for_each(rdev, mddev) {
2519                 char b[BDEVNAME_SIZE];
2520
2521                 if (rdev->sb_loaded != 1)
2522                         continue; /* no noise on spare devices */
2523
2524                 if (!test_bit(Faulty, &rdev->flags) &&
2525                     rdev->saved_raid_disk == -1) {
2526                         md_super_write(mddev,rdev,
2527                                        rdev->sb_start, rdev->sb_size,
2528                                        rdev->sb_page);
2529                         pr_debug("md: (write) %s's sb offset: %llu\n",
2530                                  bdevname(rdev->bdev, b),
2531                                  (unsigned long long)rdev->sb_start);
2532                         rdev->sb_events = mddev->events;
2533                         if (rdev->badblocks.size) {
2534                                 md_super_write(mddev, rdev,
2535                                                rdev->badblocks.sector,
2536                                                rdev->badblocks.size << 9,
2537                                                rdev->bb_page);
2538                                 rdev->badblocks.size = 0;
2539                         }
2540
2541                 } else if (test_bit(Faulty, &rdev->flags))
2542                         pr_debug("md: %s (skipping faulty)\n",
2543                                  bdevname(rdev->bdev, b));
2544                 else
2545                         pr_debug("(skipping incremental s/r ");
2546
2547                 if (mddev->level == LEVEL_MULTIPATH)
2548                         /* only need to write one superblock... */
2549                         break;
2550         }
2551         md_super_wait(mddev);
2552         /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2553
2554         spin_lock_irq(&mddev->write_lock);
2555         if (mddev->in_sync != sync_req ||
2556             test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2557                 /* have to write it out again */
2558                 spin_unlock_irq(&mddev->write_lock);
2559                 goto repeat;
2560         }
2561         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2562         spin_unlock_irq(&mddev->write_lock);
2563         wake_up(&mddev->sb_wait);
2564         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2565                 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2566
2567         rdev_for_each(rdev, mddev) {
2568                 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2569                         clear_bit(Blocked, &rdev->flags);
2570
2571                 if (any_badblocks_changed)
2572                         md_ack_all_badblocks(&rdev->badblocks);
2573                 clear_bit(BlockedBadBlocks, &rdev->flags);
2574                 wake_up(&rdev->blocked_wait);
2575         }
2576 }
2577
2578 /* words written to sysfs files may, or may not, be \n terminated.
2579  * We want to accept with case. For this we use cmd_match.
2580  */
2581 static int cmd_match(const char *cmd, const char *str)
2582 {
2583         /* See if cmd, written into a sysfs file, matches
2584          * str.  They must either be the same, or cmd can
2585          * have a trailing newline
2586          */
2587         while (*cmd && *str && *cmd == *str) {
2588                 cmd++;
2589                 str++;
2590         }
2591         if (*cmd == '\n')
2592                 cmd++;
2593         if (*str || *cmd)
2594                 return 0;
2595         return 1;
2596 }
2597
2598 struct rdev_sysfs_entry {
2599         struct attribute attr;
2600         ssize_t (*show)(struct md_rdev *, char *);
2601         ssize_t (*store)(struct md_rdev *, const char *, size_t);
2602 };
2603
2604 static ssize_t
2605 state_show(struct md_rdev *rdev, char *page)
2606 {
2607         char *sep = "";
2608         size_t len = 0;
2609
2610         if (test_bit(Faulty, &rdev->flags) ||
2611             rdev->badblocks.unacked_exist) {
2612                 len+= sprintf(page+len, "%sfaulty",sep);
2613                 sep = ",";
2614         }
2615         if (test_bit(In_sync, &rdev->flags)) {
2616                 len += sprintf(page+len, "%sin_sync",sep);
2617                 sep = ",";
2618         }
2619         if (test_bit(WriteMostly, &rdev->flags)) {
2620                 len += sprintf(page+len, "%swrite_mostly",sep);
2621                 sep = ",";
2622         }
2623         if (test_bit(Blocked, &rdev->flags) ||
2624             (rdev->badblocks.unacked_exist
2625              && !test_bit(Faulty, &rdev->flags))) {
2626                 len += sprintf(page+len, "%sblocked", sep);
2627                 sep = ",";
2628         }
2629         if (!test_bit(Faulty, &rdev->flags) &&
2630             !test_bit(In_sync, &rdev->flags)) {
2631                 len += sprintf(page+len, "%sspare", sep);
2632                 sep = ",";
2633         }
2634         if (test_bit(WriteErrorSeen, &rdev->flags)) {
2635                 len += sprintf(page+len, "%swrite_error", sep);
2636                 sep = ",";
2637         }
2638         if (test_bit(WantReplacement, &rdev->flags)) {
2639                 len += sprintf(page+len, "%swant_replacement", sep);
2640                 sep = ",";
2641         }
2642         if (test_bit(Replacement, &rdev->flags)) {
2643                 len += sprintf(page+len, "%sreplacement", sep);
2644                 sep = ",";
2645         }
2646
2647         return len+sprintf(page+len, "\n");
2648 }
2649
2650 static ssize_t
2651 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2652 {
2653         /* can write
2654          *  faulty  - simulates an error
2655          *  remove  - disconnects the device
2656          *  writemostly - sets write_mostly
2657          *  -writemostly - clears write_mostly
2658          *  blocked - sets the Blocked flags
2659          *  -blocked - clears the Blocked and possibly simulates an error
2660          *  insync - sets Insync providing device isn't active
2661          *  write_error - sets WriteErrorSeen
2662          *  -write_error - clears WriteErrorSeen
2663          */
2664         int err = -EINVAL;
2665         if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2666                 md_error(rdev->mddev, rdev);
2667                 if (test_bit(Faulty, &rdev->flags))
2668                         err = 0;
2669                 else
2670                         err = -EBUSY;
2671         } else if (cmd_match(buf, "remove")) {
2672                 if (rdev->raid_disk >= 0)
2673                         err = -EBUSY;
2674                 else {
2675                         struct mddev *mddev = rdev->mddev;
2676                         kick_rdev_from_array(rdev);
2677                         if (mddev->pers)
2678                                 md_update_sb(mddev, 1);
2679                         md_new_event(mddev);
2680                         err = 0;
2681                 }
2682         } else if (cmd_match(buf, "writemostly")) {
2683                 set_bit(WriteMostly, &rdev->flags);
2684                 err = 0;
2685         } else if (cmd_match(buf, "-writemostly")) {
2686                 clear_bit(WriteMostly, &rdev->flags);
2687                 err = 0;
2688         } else if (cmd_match(buf, "blocked")) {
2689                 set_bit(Blocked, &rdev->flags);
2690                 err = 0;
2691         } else if (cmd_match(buf, "-blocked")) {
2692                 if (!test_bit(Faulty, &rdev->flags) &&
2693                     rdev->badblocks.unacked_exist) {
2694                         /* metadata handler doesn't understand badblocks,
2695                          * so we need to fail the device
2696                          */
2697                         md_error(rdev->mddev, rdev);
2698                 }
2699                 clear_bit(Blocked, &rdev->flags);
2700                 clear_bit(BlockedBadBlocks, &rdev->flags);
2701                 wake_up(&rdev->blocked_wait);
2702                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2703                 md_wakeup_thread(rdev->mddev->thread);
2704
2705                 err = 0;
2706         } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2707                 set_bit(In_sync, &rdev->flags);
2708                 err = 0;
2709         } else if (cmd_match(buf, "write_error")) {
2710                 set_bit(WriteErrorSeen, &rdev->flags);
2711                 err = 0;
2712         } else if (cmd_match(buf, "-write_error")) {
2713                 clear_bit(WriteErrorSeen, &rdev->flags);
2714                 err = 0;
2715         } else if (cmd_match(buf, "want_replacement")) {
2716                 /* Any non-spare device that is not a replacement can
2717                  * become want_replacement at any time, but we then need to
2718                  * check if recovery is needed.
2719                  */
2720                 if (rdev->raid_disk >= 0 &&
2721                     !test_bit(Replacement, &rdev->flags))
2722                         set_bit(WantReplacement, &rdev->flags);
2723                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2724                 md_wakeup_thread(rdev->mddev->thread);
2725                 err = 0;
2726         } else if (cmd_match(buf, "-want_replacement")) {
2727                 /* Clearing 'want_replacement' is always allowed.
2728                  * Once replacements starts it is too late though.
2729                  */
2730                 err = 0;
2731                 clear_bit(WantReplacement, &rdev->flags);
2732         } else if (cmd_match(buf, "replacement")) {
2733                 /* Can only set a device as a replacement when array has not
2734                  * yet been started.  Once running, replacement is automatic
2735                  * from spares, or by assigning 'slot'.
2736                  */
2737                 if (rdev->mddev->pers)
2738                         err = -EBUSY;
2739                 else {
2740                         set_bit(Replacement, &rdev->flags);
2741                         err = 0;
2742                 }
2743         } else if (cmd_match(buf, "-replacement")) {
2744                 /* Similarly, can only clear Replacement before start */
2745                 if (rdev->mddev->pers)
2746                         err = -EBUSY;
2747                 else {
2748                         clear_bit(Replacement, &rdev->flags);
2749                         err = 0;
2750                 }
2751         }
2752         if (!err)
2753                 sysfs_notify_dirent_safe(rdev->sysfs_state);
2754         return err ? err : len;
2755 }
2756 static struct rdev_sysfs_entry rdev_state =
2757 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2758
2759 static ssize_t
2760 errors_show(struct md_rdev *rdev, char *page)
2761 {
2762         return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2763 }
2764
2765 static ssize_t
2766 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2767 {
2768         char *e;
2769         unsigned long n = simple_strtoul(buf, &e, 10);
2770         if (*buf && (*e == 0 || *e == '\n')) {
2771                 atomic_set(&rdev->corrected_errors, n);
2772                 return len;
2773         }
2774         return -EINVAL;
2775 }
2776 static struct rdev_sysfs_entry rdev_errors =
2777 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2778
2779 static ssize_t
2780 slot_show(struct md_rdev *rdev, char *page)
2781 {
2782         if (rdev->raid_disk < 0)
2783                 return sprintf(page, "none\n");
2784         else
2785                 return sprintf(page, "%d\n", rdev->raid_disk);
2786 }
2787
2788 static ssize_t
2789 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2790 {
2791         char *e;
2792         int err;
2793         int slot = simple_strtoul(buf, &e, 10);
2794         if (strncmp(buf, "none", 4)==0)
2795                 slot = -1;
2796         else if (e==buf || (*e && *e!= '\n'))
2797                 return -EINVAL;
2798         if (rdev->mddev->pers && slot == -1) {
2799                 /* Setting 'slot' on an active array requires also
2800                  * updating the 'rd%d' link, and communicating
2801                  * with the personality with ->hot_*_disk.
2802                  * For now we only support removing
2803                  * failed/spare devices.  This normally happens automatically,
2804                  * but not when the metadata is externally managed.
2805                  */
2806                 if (rdev->raid_disk == -1)
2807                         return -EEXIST;
2808                 /* personality does all needed checks */
2809                 if (rdev->mddev->pers->hot_remove_disk == NULL)
2810                         return -EINVAL;
2811                 clear_bit(Blocked, &rdev->flags);
2812                 remove_and_add_spares(rdev->mddev, rdev);
2813                 if (rdev->raid_disk >= 0)
2814                         return -EBUSY;
2815                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2816                 md_wakeup_thread(rdev->mddev->thread);
2817         } else if (rdev->mddev->pers) {
2818                 /* Activating a spare .. or possibly reactivating
2819                  * if we ever get bitmaps working here.
2820                  */
2821
2822                 if (rdev->raid_disk != -1)
2823                         return -EBUSY;
2824
2825                 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2826                         return -EBUSY;
2827
2828                 if (rdev->mddev->pers->hot_add_disk == NULL)
2829                         return -EINVAL;
2830
2831                 if (slot >= rdev->mddev->raid_disks &&
2832                     slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2833                         return -ENOSPC;
2834
2835                 rdev->raid_disk = slot;
2836                 if (test_bit(In_sync, &rdev->flags))
2837                         rdev->saved_raid_disk = slot;
2838                 else
2839                         rdev->saved_raid_disk = -1;
2840                 clear_bit(In_sync, &rdev->flags);
2841                 err = rdev->mddev->pers->
2842                         hot_add_disk(rdev->mddev, rdev);
2843                 if (err) {
2844                         rdev->raid_disk = -1;
2845                         return err;
2846                 } else
2847                         sysfs_notify_dirent_safe(rdev->sysfs_state);
2848                 if (sysfs_link_rdev(rdev->mddev, rdev))
2849                         /* failure here is OK */;
2850                 /* don't wakeup anyone, leave that to userspace. */
2851         } else {
2852                 if (slot >= rdev->mddev->raid_disks &&
2853                     slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2854                         return -ENOSPC;
2855                 rdev->raid_disk = slot;
2856                 /* assume it is working */
2857                 clear_bit(Faulty, &rdev->flags);
2858                 clear_bit(WriteMostly, &rdev->flags);
2859                 set_bit(In_sync, &rdev->flags);
2860                 sysfs_notify_dirent_safe(rdev->sysfs_state);
2861         }
2862         return len;
2863 }
2864
2865
2866 static struct rdev_sysfs_entry rdev_slot =
2867 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2868
2869 static ssize_t
2870 offset_show(struct md_rdev *rdev, char *page)
2871 {
2872         return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2873 }
2874
2875 static ssize_t
2876 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2877 {
2878         unsigned long long offset;
2879         if (strict_strtoull(buf, 10, &offset) < 0)
2880                 return -EINVAL;
2881         if (rdev->mddev->pers && rdev->raid_disk >= 0)
2882                 return -EBUSY;
2883         if (rdev->sectors && rdev->mddev->external)
2884                 /* Must set offset before size, so overlap checks
2885                  * can be sane */
2886                 return -EBUSY;
2887         rdev->data_offset = offset;
2888         rdev->new_data_offset = offset;
2889         return len;
2890 }
2891
2892 static struct rdev_sysfs_entry rdev_offset =
2893 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2894
2895 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2896 {
2897         return sprintf(page, "%llu\n",
2898                        (unsigned long long)rdev->new_data_offset);
2899 }
2900
2901 static ssize_t new_offset_store(struct md_rdev *rdev,
2902                                 const char *buf, size_t len)
2903 {
2904         unsigned long long new_offset;
2905         struct mddev *mddev = rdev->mddev;
2906
2907         if (strict_strtoull(buf, 10, &new_offset) < 0)
2908                 return -EINVAL;
2909
2910         if (mddev->sync_thread)
2911                 return -EBUSY;
2912         if (new_offset == rdev->data_offset)
2913                 /* reset is always permitted */
2914                 ;
2915         else if (new_offset > rdev->data_offset) {
2916                 /* must not push array size beyond rdev_sectors */
2917                 if (new_offset - rdev->data_offset
2918                     + mddev->dev_sectors > rdev->sectors)
2919                                 return -E2BIG;
2920         }
2921         /* Metadata worries about other space details. */
2922
2923         /* decreasing the offset is inconsistent with a backwards
2924          * reshape.
2925          */
2926         if (new_offset < rdev->data_offset &&
2927             mddev->reshape_backwards)
2928                 return -EINVAL;
2929         /* Increasing offset is inconsistent with forwards
2930          * reshape.  reshape_direction should be set to
2931          * 'backwards' first.
2932          */
2933         if (new_offset > rdev->data_offset &&
2934             !mddev->reshape_backwards)
2935                 return -EINVAL;
2936
2937         if (mddev->pers && mddev->persistent &&
2938             !super_types[mddev->major_version]
2939             .allow_new_offset(rdev, new_offset))
2940                 return -E2BIG;
2941         rdev->new_data_offset = new_offset;
2942         if (new_offset > rdev->data_offset)
2943                 mddev->reshape_backwards = 1;
2944         else if (new_offset < rdev->data_offset)
2945                 mddev->reshape_backwards = 0;
2946
2947         return len;
2948 }
2949 static struct rdev_sysfs_entry rdev_new_offset =
2950 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2951
2952 static ssize_t
2953 rdev_size_show(struct md_rdev *rdev, char *page)
2954 {
2955         return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2956 }
2957
2958 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2959 {
2960         /* check if two start/length pairs overlap */
2961         if (s1+l1 <= s2)
2962                 return 0;
2963         if (s2+l2 <= s1)
2964                 return 0;
2965         return 1;
2966 }
2967
2968 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2969 {
2970         unsigned long long blocks;
2971         sector_t new;
2972
2973         if (strict_strtoull(buf, 10, &blocks) < 0)
2974                 return -EINVAL;
2975
2976         if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2977                 return -EINVAL; /* sector conversion overflow */
2978
2979         new = blocks * 2;
2980         if (new != blocks * 2)
2981                 return -EINVAL; /* unsigned long long to sector_t overflow */
2982
2983         *sectors = new;
2984         return 0;
2985 }
2986
2987 static ssize_t
2988 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2989 {
2990         struct mddev *my_mddev = rdev->mddev;
2991         sector_t oldsectors = rdev->sectors;
2992         sector_t sectors;
2993
2994         if (strict_blocks_to_sectors(buf, &sectors) < 0)
2995                 return -EINVAL;
2996         if (rdev->data_offset != rdev->new_data_offset)
2997                 return -EINVAL; /* too confusing */
2998         if (my_mddev->pers && rdev->raid_disk >= 0) {
2999                 if (my_mddev->persistent) {
3000                         sectors = super_types[my_mddev->major_version].
3001                                 rdev_size_change(rdev, sectors);
3002                         if (!sectors)
3003                                 return -EBUSY;
3004                 } else if (!sectors)
3005                         sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
3006                                 rdev->data_offset;
3007                 if (!my_mddev->pers->resize)
3008                         /* Cannot change size for RAID0 or Linear etc */
3009                         return -EINVAL;
3010         }
3011         if (sectors < my_mddev->dev_sectors)
3012                 return -EINVAL; /* component must fit device */
3013
3014         rdev->sectors = sectors;
3015         if (sectors > oldsectors && my_mddev->external) {
3016                 /* need to check that all other rdevs with the same ->bdev
3017                  * do not overlap.  We need to unlock the mddev to avoid
3018                  * a deadlock.  We have already changed rdev->sectors, and if
3019                  * we have to change it back, we will have the lock again.
3020                  */
3021                 struct mddev *mddev;
3022                 int overlap = 0;
3023                 struct list_head *tmp;
3024
3025                 mddev_unlock(my_mddev);
3026                 for_each_mddev(mddev, tmp) {
3027                         struct md_rdev *rdev2;
3028
3029                         mddev_lock(mddev);
3030                         rdev_for_each(rdev2, mddev)
3031                                 if (rdev->bdev == rdev2->bdev &&
3032                                     rdev != rdev2 &&
3033                                     overlaps(rdev->data_offset, rdev->sectors,
3034                                              rdev2->data_offset,
3035                                              rdev2->sectors)) {
3036                                         overlap = 1;
3037                                         break;
3038                                 }
3039                         mddev_unlock(mddev);
3040                         if (overlap) {
3041                                 mddev_put(mddev);
3042                                 break;
3043                         }
3044                 }
3045                 mddev_lock(my_mddev);
3046                 if (overlap) {
3047                         /* Someone else could have slipped in a size
3048                          * change here, but doing so is just silly.
3049                          * We put oldsectors back because we *know* it is
3050                          * safe, and trust userspace not to race with
3051                          * itself
3052                          */
3053                         rdev->sectors = oldsectors;
3054                         return -EBUSY;
3055                 }
3056         }
3057         return len;
3058 }
3059
3060 static struct rdev_sysfs_entry rdev_size =
3061 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3062
3063
3064 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3065 {
3066         unsigned long long recovery_start = rdev->recovery_offset;
3067
3068         if (test_bit(In_sync, &rdev->flags) ||
3069             recovery_start == MaxSector)
3070                 return sprintf(page, "none\n");
3071
3072         return sprintf(page, "%llu\n", recovery_start);
3073 }
3074
3075 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3076 {
3077         unsigned long long recovery_start;
3078
3079         if (cmd_match(buf, "none"))
3080                 recovery_start = MaxSector;
3081         else if (strict_strtoull(buf, 10, &recovery_start))
3082                 return -EINVAL;
3083
3084         if (rdev->mddev->pers &&
3085             rdev->raid_disk >= 0)
3086                 return -EBUSY;
3087
3088         rdev->recovery_offset = recovery_start;
3089         if (recovery_start == MaxSector)
3090                 set_bit(In_sync, &rdev->flags);
3091         else
3092                 clear_bit(In_sync, &rdev->flags);
3093         return len;
3094 }
3095
3096 static struct rdev_sysfs_entry rdev_recovery_start =
3097 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3098
3099
3100 static ssize_t
3101 badblocks_show(struct badblocks *bb, char *page, int unack);
3102 static ssize_t
3103 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3104
3105 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3106 {
3107         return badblocks_show(&rdev->badblocks, page, 0);
3108 }
3109 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3110 {
3111         int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3112         /* Maybe that ack was all we needed */
3113         if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3114                 wake_up(&rdev->blocked_wait);
3115         return rv;
3116 }
3117 static struct rdev_sysfs_entry rdev_bad_blocks =
3118 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3119
3120
3121 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3122 {
3123         return badblocks_show(&rdev->badblocks, page, 1);
3124 }
3125 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3126 {
3127         return badblocks_store(&rdev->badblocks, page, len, 1);
3128 }
3129 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3130 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3131
3132 static struct attribute *rdev_default_attrs[] = {
3133         &rdev_state.attr,
3134         &rdev_errors.attr,
3135         &rdev_slot.attr,
3136         &rdev_offset.attr,
3137         &rdev_new_offset.attr,
3138         &rdev_size.attr,
3139         &rdev_recovery_start.attr,
3140         &rdev_bad_blocks.attr,
3141         &rdev_unack_bad_blocks.attr,
3142         NULL,
3143 };
3144 static ssize_t
3145 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3146 {
3147         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3148         struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3149         struct mddev *mddev = rdev->mddev;
3150         ssize_t rv;
3151
3152         if (!entry->show)
3153                 return -EIO;
3154
3155         rv = mddev ? mddev_lock(mddev) : -EBUSY;
3156         if (!rv) {
3157                 if (rdev->mddev == NULL)
3158                         rv = -EBUSY;
3159                 else
3160                         rv = entry->show(rdev, page);
3161                 mddev_unlock(mddev);
3162         }
3163         return rv;
3164 }
3165
3166 static ssize_t
3167 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3168               const char *page, size_t length)
3169 {
3170         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3171         struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3172         ssize_t rv;
3173         struct mddev *mddev = rdev->mddev;
3174
3175         if (!entry->store)
3176                 return -EIO;
3177         if (!capable(CAP_SYS_ADMIN))
3178                 return -EACCES;
3179         rv = mddev ? mddev_lock(mddev): -EBUSY;
3180         if (!rv) {
3181                 if (rdev->mddev == NULL)
3182                         rv = -EBUSY;
3183                 else
3184                         rv = entry->store(rdev, page, length);
3185                 mddev_unlock(mddev);
3186         }
3187         return rv;
3188 }
3189
3190 static void rdev_free(struct kobject *ko)
3191 {
3192         struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3193         kfree(rdev);
3194 }
3195 static const struct sysfs_ops rdev_sysfs_ops = {
3196         .show           = rdev_attr_show,
3197         .store          = rdev_attr_store,
3198 };
3199 static struct kobj_type rdev_ktype = {
3200         .release        = rdev_free,
3201         .sysfs_ops      = &rdev_sysfs_ops,
3202         .default_attrs  = rdev_default_attrs,
3203 };
3204
3205 int md_rdev_init(struct md_rdev *rdev)
3206 {
3207         rdev->desc_nr = -1;
3208         rdev->saved_raid_disk = -1;
3209         rdev->raid_disk = -1;
3210         rdev->flags = 0;
3211         rdev->data_offset = 0;
3212         rdev->new_data_offset = 0;
3213         rdev->sb_events = 0;
3214         rdev->last_read_error.tv_sec  = 0;
3215         rdev->last_read_error.tv_nsec = 0;
3216         rdev->sb_loaded = 0;
3217         rdev->bb_page = NULL;
3218         atomic_set(&rdev->nr_pending, 0);
3219         atomic_set(&rdev->read_errors, 0);
3220         atomic_set(&rdev->corrected_errors, 0);
3221
3222         INIT_LIST_HEAD(&rdev->same_set);
3223         init_waitqueue_head(&rdev->blocked_wait);
3224
3225         /* Add space to store bad block list.
3226          * This reserves the space even on arrays where it cannot
3227          * be used - I wonder if that matters
3228          */
3229         rdev->badblocks.count = 0;
3230         rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
3231         rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3232         seqlock_init(&rdev->badblocks.lock);
3233         if (rdev->badblocks.page == NULL)
3234                 return -ENOMEM;
3235
3236         return 0;
3237 }
3238 EXPORT_SYMBOL_GPL(md_rdev_init);
3239 /*
3240  * Import a device. If 'super_format' >= 0, then sanity check the superblock
3241  *
3242  * mark the device faulty if:
3243  *
3244  *   - the device is nonexistent (zero size)
3245  *   - the device has no valid superblock
3246  *
3247  * a faulty rdev _never_ has rdev->sb set.
3248  */
3249 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3250 {
3251         char b[BDEVNAME_SIZE];
3252         int err;
3253         struct md_rdev *rdev;
3254         sector_t size;
3255
3256         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3257         if (!rdev) {
3258                 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3259                 return ERR_PTR(-ENOMEM);
3260         }
3261
3262         err = md_rdev_init(rdev);
3263         if (err)
3264                 goto abort_free;
3265         err = alloc_disk_sb(rdev);
3266         if (err)
3267                 goto abort_free;
3268
3269         err = lock_rdev(rdev, newdev, super_format == -2);
3270         if (err)
3271                 goto abort_free;
3272
3273         kobject_init(&rdev->kobj, &rdev_ktype);
3274
3275         size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3276         if (!size) {
3277                 printk(KERN_WARNING 
3278                         "md: %s has zero or unknown size, marking faulty!\n",
3279                         bdevname(rdev->bdev,b));
3280                 err = -EINVAL;
3281                 goto abort_free;
3282         }
3283
3284         if (super_format >= 0) {
3285                 err = super_types[super_format].
3286                         load_super(rdev, NULL, super_minor);
3287                 if (err == -EINVAL) {
3288                         printk(KERN_WARNING
3289                                 "md: %s does not have a valid v%d.%d "
3290                                "superblock, not importing!\n",
3291                                 bdevname(rdev->bdev,b),
3292                                super_format, super_minor);
3293                         goto abort_free;
3294                 }
3295                 if (err < 0) {
3296                         printk(KERN_WARNING 
3297                                 "md: could not read %s's sb, not importing!\n",
3298                                 bdevname(rdev->bdev,b));
3299                         goto abort_free;
3300                 }
3301         }
3302
3303         return rdev;
3304
3305 abort_free:
3306         if (rdev->bdev)
3307                 unlock_rdev(rdev);
3308         md_rdev_clear(rdev);
3309         kfree(rdev);
3310         return ERR_PTR(err);
3311 }
3312
3313 /*
3314  * Check a full RAID array for plausibility
3315  */
3316
3317
3318 static void analyze_sbs(struct mddev * mddev)
3319 {
3320         int i;
3321         struct md_rdev *rdev, *freshest, *tmp;
3322         char b[BDEVNAME_SIZE];
3323
3324         freshest = NULL;
3325         rdev_for_each_safe(rdev, tmp, mddev)
3326                 switch (super_types[mddev->major_version].
3327                         load_super(rdev, freshest, mddev->minor_version)) {
3328                 case 1:
3329                         freshest = rdev;
3330                         break;
3331                 case 0:
3332                         break;
3333                 default:
3334                         printk( KERN_ERR \
3335                                 "md: fatal superblock inconsistency in %s"
3336                                 " -- removing from array\n", 
3337                                 bdevname(rdev->bdev,b));
3338                         kick_rdev_from_array(rdev);
3339                 }
3340
3341
3342         super_types[mddev->major_version].
3343                 validate_super(mddev, freshest);
3344
3345         i = 0;
3346         rdev_for_each_safe(rdev, tmp, mddev) {
3347                 if (mddev->max_disks &&
3348                     (rdev->desc_nr >= mddev->max_disks ||
3349                      i > mddev->max_disks)) {
3350                         printk(KERN_WARNING
3351                                "md: %s: %s: only %d devices permitted\n",
3352                                mdname(mddev), bdevname(rdev->bdev, b),
3353                                mddev->max_disks);
3354                         kick_rdev_from_array(rdev);
3355                         continue;
3356                 }
3357                 if (rdev != freshest)
3358                         if (super_types[mddev->major_version].
3359                             validate_super(mddev, rdev)) {
3360                                 printk(KERN_WARNING "md: kicking non-fresh %s"
3361                                         " from array!\n",
3362                                         bdevname(rdev->bdev,b));
3363                                 kick_rdev_from_array(rdev);
3364                                 continue;
3365                         }
3366                 if (mddev->level == LEVEL_MULTIPATH) {
3367                         rdev->desc_nr = i++;
3368                         rdev->raid_disk = rdev->desc_nr;
3369                         set_bit(In_sync, &rdev->flags);
3370                 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
3371                         rdev->raid_disk = -1;
3372                         clear_bit(In_sync, &rdev->flags);
3373                 }
3374         }
3375 }
3376
3377 /* Read a fixed-point number.
3378  * Numbers in sysfs attributes should be in "standard" units where
3379  * possible, so time should be in seconds.
3380  * However we internally use a a much smaller unit such as 
3381  * milliseconds or jiffies.
3382  * This function takes a decimal number with a possible fractional
3383  * component, and produces an integer which is the result of
3384  * multiplying that number by 10^'scale'.
3385  * all without any floating-point arithmetic.
3386  */
3387 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3388 {
3389         unsigned long result = 0;
3390         long decimals = -1;
3391         while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3392                 if (*cp == '.')
3393                         decimals = 0;
3394                 else if (decimals < scale) {
3395                         unsigned int value;
3396                         value = *cp - '0';
3397                         result = result * 10 + value;
3398                         if (decimals >= 0)
3399                                 decimals++;
3400                 }
3401                 cp++;
3402         }
3403         if (*cp == '\n')
3404                 cp++;
3405         if (*cp)
3406                 return -EINVAL;
3407         if (decimals < 0)
3408                 decimals = 0;
3409         while (decimals < scale) {
3410                 result *= 10;
3411                 decimals ++;
3412         }
3413         *res = result;
3414         return 0;
3415 }
3416
3417
3418 static void md_safemode_timeout(unsigned long data);
3419
3420 static ssize_t
3421 safe_delay_show(struct mddev *mddev, char *page)
3422 {
3423         int msec = (mddev->safemode_delay*1000)/HZ;
3424         return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3425 }
3426 static ssize_t
3427 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3428 {
3429         unsigned long msec;
3430
3431         if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3432                 return -EINVAL;
3433         if (msec == 0)
3434                 mddev->safemode_delay = 0;
3435         else {
3436                 unsigned long old_delay = mddev->safemode_delay;
3437                 mddev->safemode_delay = (msec*HZ)/1000;
3438                 if (mddev->safemode_delay == 0)
3439                         mddev->safemode_delay = 1;
3440                 if (mddev->safemode_delay < old_delay)
3441                         md_safemode_timeout((unsigned long)mddev);
3442         }
3443         return len;
3444 }
3445 static struct md_sysfs_entry md_safe_delay =
3446 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3447
3448 static ssize_t
3449 level_show(struct mddev *mddev, char *page)
3450 {
3451         struct md_personality *p = mddev->pers;
3452         if (p)
3453                 return sprintf(page, "%s\n", p->name);
3454         else if (mddev->clevel[0])
3455                 return sprintf(page, "%s\n", mddev->clevel);
3456         else if (mddev->level != LEVEL_NONE)
3457                 return sprintf(page, "%d\n", mddev->level);
3458         else
3459                 return 0;
3460 }
3461
3462 static ssize_t
3463 level_store(struct mddev *mddev, const char *buf, size_t len)
3464 {
3465         char clevel[16];
3466         ssize_t rv = len;
3467         struct md_personality *pers;
3468         long level;
3469         void *priv;
3470         struct md_rdev *rdev;
3471
3472         if (mddev->pers == NULL) {
3473                 if (len == 0)
3474                         return 0;
3475                 if (len >= sizeof(mddev->clevel))
3476                         return -ENOSPC;
3477                 strncpy(mddev->clevel, buf, len);
3478                 if (mddev->clevel[len-1] == '\n')
3479                         len--;
3480                 mddev->clevel[len] = 0;
3481                 mddev->level = LEVEL_NONE;
3482                 return rv;
3483         }
3484
3485         /* request to change the personality.  Need to ensure:
3486          *  - array is not engaged in resync/recovery/reshape
3487          *  - old personality can be suspended
3488          *  - new personality will access other array.
3489          */
3490
3491         if (mddev->sync_thread ||
3492             mddev->reshape_position != MaxSector ||
3493             mddev->sysfs_active)
3494                 return -EBUSY;
3495
3496         if (!mddev->pers->quiesce) {
3497                 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3498                        mdname(mddev), mddev->pers->name);
3499                 return -EINVAL;
3500         }
3501
3502         /* Now find the new personality */
3503         if (len == 0 || len >= sizeof(clevel))
3504                 return -EINVAL;
3505         strncpy(clevel, buf, len);
3506         if (clevel[len-1] == '\n')
3507                 len--;
3508         clevel[len] = 0;
3509         if (strict_strtol(clevel, 10, &level))
3510                 level = LEVEL_NONE;
3511
3512         if (request_module("md-%s", clevel) != 0)
3513                 request_module("md-level-%s", clevel);
3514         spin_lock(&pers_lock);
3515         pers = find_pers(level, clevel);
3516         if (!pers || !try_module_get(pers->owner)) {
3517                 spin_unlock(&pers_lock);
3518                 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3519                 return -EINVAL;
3520         }
3521         spin_unlock(&pers_lock);
3522
3523         if (pers == mddev->pers) {
3524                 /* Nothing to do! */
3525                 module_put(pers->owner);
3526                 return rv;
3527         }
3528         if (!pers->takeover) {
3529                 module_put(pers->owner);
3530                 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3531                        mdname(mddev), clevel);
3532                 return -EINVAL;
3533         }
3534
3535         rdev_for_each(rdev, mddev)
3536                 rdev->new_raid_disk = rdev->raid_disk;
3537
3538         /* ->takeover must set new_* and/or delta_disks
3539          * if it succeeds, and may set them when it fails.
3540          */
3541         priv = pers->takeover(mddev);
3542         if (IS_ERR(priv)) {
3543                 mddev->new_level = mddev->level;
3544                 mddev->new_layout = mddev->layout;
3545                 mddev->new_chunk_sectors = mddev->chunk_sectors;
3546                 mddev->raid_disks -= mddev->delta_disks;
3547                 mddev->delta_disks = 0;
3548                 mddev->reshape_backwards = 0;
3549                 module_put(pers->owner);
3550                 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3551                        mdname(mddev), clevel);
3552                 return PTR_ERR(priv);
3553         }
3554
3555         /* Looks like we have a winner */
3556         mddev_suspend(mddev);
3557         mddev->pers->stop(mddev);
3558         
3559         if (mddev->pers->sync_request == NULL &&
3560             pers->sync_request != NULL) {
3561                 /* need to add the md_redundancy_group */
3562                 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3563                         printk(KERN_WARNING
3564                                "md: cannot register extra attributes for %s\n",
3565                                mdname(mddev));
3566                 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
3567         }               
3568         if (mddev->pers->sync_request != NULL &&
3569             pers->sync_request == NULL) {
3570                 /* need to remove the md_redundancy_group */
3571                 if (mddev->to_remove == NULL)
3572                         mddev->to_remove = &md_redundancy_group;
3573         }
3574
3575         if (mddev->pers->sync_request == NULL &&
3576             mddev->external) {
3577                 /* We are converting from a no-redundancy array
3578                  * to a redundancy array and metadata is managed
3579                  * externally so we need to be sure that writes
3580                  * won't block due to a need to transition
3581                  *      clean->dirty
3582                  * until external management is started.
3583                  */
3584                 mddev->in_sync = 0;
3585                 mddev->safemode_delay = 0;
3586                 mddev->safemode = 0;
3587         }
3588
3589         rdev_for_each(rdev, mddev) {
3590                 if (rdev->raid_disk < 0)
3591                         continue;
3592                 if (rdev->new_raid_disk >= mddev->raid_disks)
3593                         rdev->new_raid_disk = -1;
3594                 if (rdev->new_raid_disk == rdev->raid_disk)
3595                         continue;
3596                 sysfs_unlink_rdev(mddev, rdev);
3597         }
3598         rdev_for_each(rdev, mddev) {
3599                 if (rdev->raid_disk < 0)
3600                         continue;
3601                 if (rdev->new_raid_disk == rdev->raid_disk)
3602                         continue;
3603                 rdev->raid_disk = rdev->new_raid_disk;
3604                 if (rdev->raid_disk < 0)
3605                         clear_bit(In_sync, &rdev->flags);
3606                 else {
3607                         if (sysfs_link_rdev(mddev, rdev))
3608                                 printk(KERN_WARNING "md: cannot register rd%d"
3609                                        " for %s after level change\n",
3610                                        rdev->raid_disk, mdname(mddev));
3611                 }
3612         }
3613
3614         module_put(mddev->pers->owner);
3615         mddev->pers = pers;
3616         mddev->private = priv;
3617         strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3618         mddev->level = mddev->new_level;
3619         mddev->layout = mddev->new_layout;
3620         mddev->chunk_sectors = mddev->new_chunk_sectors;
3621         mddev->delta_disks = 0;
3622         mddev->reshape_backwards = 0;
3623         mddev->degraded = 0;
3624         if (mddev->pers->sync_request == NULL) {
3625                 /* this is now an array without redundancy, so
3626                  * it must always be in_sync
3627                  */
3628                 mddev->in_sync = 1;
3629                 del_timer_sync(&mddev->safemode_timer);
3630         }
3631         pers->run(mddev);
3632         set_bit(MD_CHANGE_DEVS, &mddev->flags);
3633         mddev_resume(mddev);
3634         sysfs_notify(&mddev->kobj, NULL, "level");
3635         md_new_event(mddev);
3636         return rv;
3637 }
3638
3639 static struct md_sysfs_entry md_level =
3640 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3641
3642
3643 static ssize_t
3644 layout_show(struct mddev *mddev, char *page)
3645 {
3646         /* just a number, not meaningful for all levels */
3647         if (mddev->reshape_position != MaxSector &&
3648             mddev->layout != mddev->new_layout)
3649                 return sprintf(page, "%d (%d)\n",
3650                                mddev->new_layout, mddev->layout);
3651         return sprintf(page, "%d\n", mddev->layout);
3652 }
3653
3654 static ssize_t
3655 layout_store(struct mddev *mddev, const char *buf, size_t len)
3656 {
3657         char *e;
3658         unsigned long n = simple_strtoul(buf, &e, 10);
3659
3660         if (!*buf || (*e && *e != '\n'))
3661                 return -EINVAL;
3662
3663         if (mddev->pers) {
3664                 int err;
3665                 if (mddev->pers->check_reshape == NULL)
3666                         return -EBUSY;
3667                 mddev->new_layout = n;
3668                 err = mddev->pers->check_reshape(mddev);
3669                 if (err) {
3670                         mddev->new_layout = mddev->layout;
3671                         return err;
3672                 }
3673         } else {
3674                 mddev->new_layout = n;
3675                 if (mddev->reshape_position == MaxSector)
3676                         mddev->layout = n;
3677         }
3678         return len;
3679 }
3680 static struct md_sysfs_entry md_layout =
3681 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3682
3683
3684 static ssize_t
3685 raid_disks_show(struct mddev *mddev, char *page)
3686 {
3687         if (mddev->raid_disks == 0)
3688                 return 0;
3689         if (mddev->reshape_position != MaxSector &&
3690             mddev->delta_disks != 0)
3691                 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3692                                mddev->raid_disks - mddev->delta_disks);
3693         return sprintf(page, "%d\n", mddev->raid_disks);
3694 }
3695
3696 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3697
3698 static ssize_t
3699 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3700 {
3701         char *e;
3702         int rv = 0;
3703         unsigned long n = simple_strtoul(buf, &e, 10);
3704
3705         if (!*buf || (*e && *e != '\n'))
3706                 return -EINVAL;
3707
3708         if (mddev->pers)
3709                 rv = update_raid_disks(mddev, n);
3710         else if (mddev->reshape_position != MaxSector) {
3711                 struct md_rdev *rdev;
3712                 int olddisks = mddev->raid_disks - mddev->delta_disks;
3713
3714                 rdev_for_each(rdev, mddev) {
3715                         if (olddisks < n &&
3716                             rdev->data_offset < rdev->new_data_offset)
3717                                 return -EINVAL;
3718                         if (olddisks > n &&
3719                             rdev->data_offset > rdev->new_data_offset)
3720                                 return -EINVAL;
3721                 }
3722                 mddev->delta_disks = n - olddisks;
3723                 mddev->raid_disks = n;
3724                 mddev->reshape_backwards = (mddev->delta_disks < 0);
3725         } else
3726                 mddev->raid_disks = n;
3727         return rv ? rv : len;
3728 }
3729 static struct md_sysfs_entry md_raid_disks =
3730 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3731
3732 static ssize_t
3733 chunk_size_show(struct mddev *mddev, char *page)
3734 {
3735         if (mddev->reshape_position != MaxSector &&
3736             mddev->chunk_sectors != mddev->new_chunk_sectors)
3737                 return sprintf(page, "%d (%d)\n",
3738                                mddev->new_chunk_sectors << 9,
3739                                mddev->chunk_sectors << 9);
3740         return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3741 }
3742
3743 static ssize_t
3744 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3745 {
3746         char *e;
3747         unsigned long n = simple_strtoul(buf, &e, 10);
3748
3749         if (!*buf || (*e && *e != '\n'))
3750                 return -EINVAL;
3751
3752         if (mddev->pers) {
3753                 int err;
3754                 if (mddev->pers->check_reshape == NULL)
3755                         return -EBUSY;
3756                 mddev->new_chunk_sectors = n >> 9;
3757                 err = mddev->pers->check_reshape(mddev);
3758                 if (err) {
3759                         mddev->new_chunk_sectors = mddev->chunk_sectors;
3760                         return err;
3761                 }
3762         } else {
3763                 mddev->new_chunk_sectors = n >> 9;
3764                 if (mddev->reshape_position == MaxSector)
3765                         mddev->chunk_sectors = n >> 9;
3766         }
3767         return len;
3768 }
3769 static struct md_sysfs_entry md_chunk_size =
3770 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3771
3772 static ssize_t
3773 resync_start_show(struct mddev *mddev, char *page)
3774 {
3775         if (mddev->recovery_cp == MaxSector)
3776                 return sprintf(page, "none\n");
3777         return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3778 }
3779
3780 static ssize_t
3781 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3782 {
3783         char *e;
3784         unsigned long long n = simple_strtoull(buf, &e, 10);
3785
3786         if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3787                 return -EBUSY;
3788         if (cmd_match(buf, "none"))
3789                 n = MaxSector;
3790         else if (!*buf || (*e && *e != '\n'))
3791                 return -EINVAL;
3792
3793         mddev->recovery_cp = n;
3794         if (mddev->pers)
3795                 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3796         return len;
3797 }
3798 static struct md_sysfs_entry md_resync_start =
3799 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3800
3801 /*
3802  * The array state can be:
3803  *
3804  * clear
3805  *     No devices, no size, no level
3806  *     Equivalent to STOP_ARRAY ioctl
3807  * inactive
3808  *     May have some settings, but array is not active
3809  *        all IO results in error
3810  *     When written, doesn't tear down array, but just stops it
3811  * suspended (not supported yet)
3812  *     All IO requests will block. The array can be reconfigured.
3813  *     Writing this, if accepted, will block until array is quiescent
3814  * readonly
3815  *     no resync can happen.  no superblocks get written.
3816  *     write requests fail
3817  * read-auto
3818  *     like readonly, but behaves like 'clean' on a write request.
3819  *
3820  * clean - no pending writes, but otherwise active.
3821  *     When written to inactive array, starts without resync
3822  *     If a write request arrives then
3823  *       if metadata is known, mark 'dirty' and switch to 'active'.
3824  *       if not known, block and switch to write-pending
3825  *     If written to an active array that has pending writes, then fails.
3826  * active
3827  *     fully active: IO and resync can be happening.
3828  *     When written to inactive array, starts with resync
3829  *
3830  * write-pending
3831  *     clean, but writes are blocked waiting for 'active' to be written.
3832  *
3833  * active-idle
3834  *     like active, but no writes have been seen for a while (100msec).
3835  *
3836  */
3837 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3838                    write_pending, active_idle, bad_word};
3839 static char *array_states[] = {
3840         "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3841         "write-pending", "active-idle", NULL };
3842
3843 static int match_word(const char *word, char **list)
3844 {
3845         int n;
3846         for (n=0; list[n]; n++)
3847                 if (cmd_match(word, list[n]))
3848                         break;
3849         return n;
3850 }
3851
3852 static ssize_t
3853 array_state_show(struct mddev *mddev, char *page)
3854 {
3855         enum array_state st = inactive;
3856
3857         if (mddev->pers)
3858                 switch(mddev->ro) {
3859                 case 1:
3860                         st = readonly;
3861                         break;
3862                 case 2:
3863                         st = read_auto;
3864                         break;
3865                 case 0:
3866                         if (mddev->in_sync)
3867                                 st = clean;
3868                         else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3869                                 st = write_pending;
3870                         else if (mddev->safemode)
3871                                 st = active_idle;
3872                         else
3873                                 st = active;
3874                 }
3875         else {
3876                 if (list_empty(&mddev->disks) &&
3877                     mddev->raid_disks == 0 &&
3878                     mddev->dev_sectors == 0)
3879                         st = clear;
3880                 else
3881                         st = inactive;
3882         }
3883         return sprintf(page, "%s\n", array_states[st]);
3884 }
3885
3886 static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3887 static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
3888 static int do_md_run(struct mddev * mddev);
3889 static int restart_array(struct mddev *mddev);
3890
3891 static ssize_t
3892 array_state_store(struct mddev *mddev, const char *buf, size_t len)
3893 {
3894         int err = -EINVAL;
3895         enum array_state st = match_word(buf, array_states);
3896         switch(st) {
3897         case bad_word:
3898                 break;
3899         case clear:
3900                 /* stopping an active array */
3901                 err = do_md_stop(mddev, 0, NULL);
3902                 break;
3903         case inactive:
3904                 /* stopping an active array */
3905                 if (mddev->pers)
3906                         err = do_md_stop(mddev, 2, NULL);
3907                 else
3908                         err = 0; /* already inactive */
3909                 break;
3910         case suspended:
3911                 break; /* not supported yet */
3912         case readonly:
3913                 if (mddev->pers)
3914                         err = md_set_readonly(mddev, NULL);
3915                 else {
3916                         mddev->ro = 1;
3917                         set_disk_ro(mddev->gendisk, 1);
3918                         err = do_md_run(mddev);
3919                 }
3920                 break;
3921         case read_auto:
3922                 if (mddev->pers) {
3923                         if (mddev->ro == 0)
3924                                 err = md_set_readonly(mddev, NULL);
3925                         else if (mddev->ro == 1)
3926                                 err = restart_array(mddev);
3927                         if (err == 0) {
3928                                 mddev->ro = 2;
3929                                 set_disk_ro(mddev->gendisk, 0);
3930                         }
3931                 } else {
3932                         mddev->ro = 2;
3933                         err = do_md_run(mddev);
3934                 }
3935                 break;
3936         case clean:
3937                 if (mddev->pers) {
3938                         restart_array(mddev);
3939                         spin_lock_irq(&mddev->write_lock);
3940                         if (atomic_read(&mddev->writes_pending) == 0) {
3941                                 if (mddev->in_sync == 0) {
3942                                         mddev->in_sync = 1;
3943                                         if (mddev->safemode == 1)
3944                                                 mddev->safemode = 0;
3945                                         set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3946                                 }
3947                                 err = 0;
3948                         } else
3949                                 err = -EBUSY;
3950                         spin_unlock_irq(&mddev->write_lock);
3951                 } else
3952                         err = -EINVAL;
3953                 break;
3954         case active:
3955                 if (mddev->pers) {
3956                         restart_array(mddev);
3957                         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3958                         wake_up(&mddev->sb_wait);
3959                         err = 0;
3960                 } else {
3961                         mddev->ro = 0;
3962                         set_disk_ro(mddev->gendisk, 0);
3963                         err = do_md_run(mddev);
3964                 }
3965                 break;
3966         case write_pending:
3967         case active_idle:
3968                 /* these cannot be set */
3969                 break;
3970         }
3971         if (err)
3972                 return err;
3973         else {
3974                 if (mddev->hold_active == UNTIL_IOCTL)
3975                         mddev->hold_active = 0;
3976                 sysfs_notify_dirent_safe(mddev->sysfs_state);
3977                 return len;
3978         }
3979 }
3980 static struct md_sysfs_entry md_array_state =
3981 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3982
3983 static ssize_t
3984 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
3985         return sprintf(page, "%d\n",
3986                        atomic_read(&mddev->max_corr_read_errors));
3987 }
3988
3989 static ssize_t
3990 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
3991 {
3992         char *e;
3993         unsigned long n = simple_strtoul(buf, &e, 10);
3994
3995         if (*buf && (*e == 0 || *e == '\n')) {
3996                 atomic_set(&mddev->max_corr_read_errors, n);
3997                 return len;
3998         }
3999         return -EINVAL;
4000 }
4001
4002 static struct md_sysfs_entry max_corr_read_errors =
4003 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
4004         max_corrected_read_errors_store);
4005
4006 static ssize_t
4007 null_show(struct mddev *mddev, char *page)
4008 {
4009         return -EINVAL;
4010 }
4011
4012 static ssize_t
4013 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
4014 {
4015         /* buf must be %d:%d\n? giving major and minor numbers */
4016         /* The new device is added to the array.
4017          * If the array has a persistent superblock, we read the
4018          * superblock to initialise info and check validity.
4019          * Otherwise, only checking done is that in bind_rdev_to_array,
4020          * which mainly checks size.
4021          */
4022         char *e;
4023         int major = simple_strtoul(buf, &e, 10);
4024         int minor;
4025         dev_t dev;
4026         struct md_rdev *rdev;
4027         int err;
4028
4029         if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
4030                 return -EINVAL;
4031         minor = simple_strtoul(e+1, &e, 10);
4032         if (*e && *e != '\n')
4033                 return -EINVAL;
4034         dev = MKDEV(major, minor);
4035         if (major != MAJOR(dev) ||
4036             minor != MINOR(dev))
4037                 return -EOVERFLOW;
4038
4039
4040         if (mddev->persistent) {
4041                 rdev = md_import_device(dev, mddev->major_version,
4042                                         mddev->minor_version);
4043                 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4044                         struct md_rdev *rdev0
4045                                 = list_entry(mddev->disks.next,
4046                                              struct md_rdev, same_set);
4047                         err = super_types[mddev->major_version]
4048                                 .load_super(rdev, rdev0, mddev->minor_version);
4049                         if (err < 0)
4050                                 goto out;
4051                 }
4052         } else if (mddev->external)
4053                 rdev = md_import_device(dev, -2, -1);
4054         else
4055                 rdev = md_import_device(dev, -1, -1);
4056
4057         if (IS_ERR(rdev))
4058                 return PTR_ERR(rdev);
4059         err = bind_rdev_to_array(rdev, mddev);
4060  out:
4061         if (err)
4062                 export_rdev(rdev);
4063         return err ? err : len;
4064 }
4065
4066 static struct md_sysfs_entry md_new_device =
4067 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4068
4069 static ssize_t
4070 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4071 {
4072         char *end;
4073         unsigned long chunk, end_chunk;
4074
4075         if (!mddev->bitmap)
4076                 goto out;
4077         /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4078         while (*buf) {
4079                 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4080                 if (buf == end) break;
4081                 if (*end == '-') { /* range */
4082                         buf = end + 1;
4083                         end_chunk = simple_strtoul(buf, &end, 0);
4084                         if (buf == end) break;
4085                 }
4086                 if (*end && !isspace(*end)) break;
4087                 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4088                 buf = skip_spaces(end);
4089         }
4090         bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4091 out:
4092         return len;
4093 }
4094
4095 static struct md_sysfs_entry md_bitmap =
4096 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4097
4098 static ssize_t
4099 size_show(struct mddev *mddev, char *page)
4100 {
4101         return sprintf(page, "%llu\n",
4102                 (unsigned long long)mddev->dev_sectors / 2);
4103 }
4104
4105 static int update_size(struct mddev *mddev, sector_t num_sectors);
4106
4107 static ssize_t
4108 size_store(struct mddev *mddev, const char *buf, size_t len)
4109 {
4110         /* If array is inactive, we can reduce the component size, but
4111          * not increase it (except from 0).
4112          * If array is active, we can try an on-line resize
4113          */
4114         sector_t sectors;
4115         int err = strict_blocks_to_sectors(buf, &sectors);
4116
4117         if (err < 0)
4118                 return err;
4119         if (mddev->pers) {
4120                 err = update_size(mddev, sectors);
4121                 md_update_sb(mddev, 1);
4122         } else {
4123                 if (mddev->dev_sectors == 0 ||
4124                     mddev->dev_sectors > sectors)
4125                         mddev->dev_sectors = sectors;
4126                 else
4127                         err = -ENOSPC;
4128         }
4129         return err ? err : len;
4130 }
4131
4132 static struct md_sysfs_entry md_size =
4133 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4134
4135
4136 /* Metadata version.
4137  * This is one of
4138  *   'none' for arrays with no metadata (good luck...)
4139  *   'external' for arrays with externally managed metadata,
4140  * or N.M for internally known formats
4141  */
4142 static ssize_t
4143 metadata_show(struct mddev *mddev, char *page)
4144 {
4145         if (mddev->persistent)
4146                 return sprintf(page, "%d.%d\n",
4147                                mddev->major_version, mddev->minor_version);
4148         else if (mddev->external)
4149                 return sprintf(page, "external:%s\n", mddev->metadata_type);
4150         else
4151                 return sprintf(page, "none\n");
4152 }
4153
4154 static ssize_t
4155 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4156 {
4157         int major, minor;
4158         char *e;
4159         /* Changing the details of 'external' metadata is
4160          * always permitted.  Otherwise there must be
4161          * no devices attached to the array.
4162          */
4163         if (mddev->external && strncmp(buf, "external:", 9) == 0)
4164                 ;
4165         else if (!list_empty(&mddev->disks))
4166                 return -EBUSY;
4167
4168         if (cmd_match(buf, "none")) {
4169                 mddev->persistent = 0;
4170                 mddev->external = 0;
4171                 mddev->major_version = 0;
4172                 mddev->minor_version = 90;
4173                 return len;
4174         }
4175         if (strncmp(buf, "external:", 9) == 0) {
4176                 size_t namelen = len-9;
4177                 if (namelen >= sizeof(mddev->metadata_type))
4178                         namelen = sizeof(mddev->metadata_type)-1;
4179                 strncpy(mddev->metadata_type, buf+9, namelen);
4180                 mddev->metadata_type[namelen] = 0;
4181                 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4182                         mddev->metadata_type[--namelen] = 0;
4183                 mddev->persistent = 0;
4184                 mddev->external = 1;
4185                 mddev->major_version = 0;
4186                 mddev->minor_version = 90;
4187                 return len;
4188         }
4189         major = simple_strtoul(buf, &e, 10);
4190         if (e==buf || *e != '.')
4191                 return -EINVAL;
4192         buf = e+1;
4193         minor = simple_strtoul(buf, &e, 10);
4194         if (e==buf || (*e && *e != '\n') )
4195                 return -EINVAL;
4196         if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4197                 return -ENOENT;
4198         mddev->major_version = major;
4199         mddev->minor_version = minor;
4200         mddev->persistent = 1;
4201         mddev->external = 0;
4202         return len;
4203 }
4204
4205 static struct md_sysfs_entry md_metadata =
4206 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4207
4208 static ssize_t
4209 action_show(struct mddev *mddev, char *page)
4210 {
4211         char *type = "idle";
4212         if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4213                 type = "frozen";
4214         else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4215             (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
4216                 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4217                         type = "reshape";
4218                 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4219                         if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4220                                 type = "resync";
4221                         else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4222                                 type = "check";
4223                         else
4224                                 type = "repair";
4225                 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
4226                         type = "recover";
4227         }
4228         return sprintf(page, "%s\n", type);
4229 }
4230
4231 static ssize_t
4232 action_store(struct mddev *mddev, const char *page, size_t len)
4233 {
4234         if (!mddev->pers || !mddev->pers->sync_request)
4235                 return -EINVAL;
4236
4237         if (cmd_match(page, "frozen"))
4238                 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4239         else
4240                 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4241
4242         if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4243                 if (mddev->sync_thread) {
4244                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4245                         md_reap_sync_thread(mddev);
4246                 }
4247         } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4248                    test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
4249                 return -EBUSY;
4250         else if (cmd_match(page, "resync"))
4251                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4252         else if (cmd_match(page, "recover")) {
4253                 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4254                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4255         } else if (cmd_match(page, "reshape")) {
4256                 int err;
4257                 if (mddev->pers->start_reshape == NULL)
4258                         return -EINVAL;
4259                 err = mddev->pers->start_reshape(mddev);
4260                 if (err)
4261                         return err;
4262                 sysfs_notify(&mddev->kobj, NULL, "degraded");
4263         } else {
4264                 if (cmd_match(page, "check"))
4265                         set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4266                 else if (!cmd_match(page, "repair"))
4267                         return -EINVAL;
4268                 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4269                 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4270         }
4271         if (mddev->ro == 2) {
4272                 /* A write to sync_action is enough to justify
4273                  * canceling read-auto mode
4274                  */
4275                 mddev->ro = 0;
4276                 md_wakeup_thread(mddev->sync_thread);
4277         }
4278         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4279         md_wakeup_thread(mddev->thread);
4280         sysfs_notify_dirent_safe(mddev->sysfs_action);
4281         return len;
4282 }
4283
4284 static ssize_t
4285 mismatch_cnt_show(struct mddev *mddev, char *page)
4286 {
4287         return sprintf(page, "%llu\n",
4288                        (unsigned long long)
4289                        atomic64_read(&mddev->resync_mismatches));
4290 }
4291
4292 static struct md_sysfs_entry md_scan_mode =
4293 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4294
4295
4296 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4297
4298 static ssize_t
4299 sync_min_show(struct mddev *mddev, char *page)
4300 {
4301         return sprintf(page, "%d (%s)\n", speed_min(mddev),
4302                        mddev->sync_speed_min ? "local": "system");
4303 }
4304
4305 static ssize_t
4306 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4307 {
4308         int min;
4309         char *e;
4310         if (strncmp(buf, "system", 6)==0) {
4311                 mddev->sync_speed_min = 0;
4312                 return len;
4313         }
4314         min = simple_strtoul(buf, &e, 10);
4315         if (buf == e || (*e && *e != '\n') || min <= 0)
4316                 return -EINVAL;
4317         mddev->sync_speed_min = min;
4318         return len;
4319 }
4320
4321 static struct md_sysfs_entry md_sync_min =
4322 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4323
4324 static ssize_t
4325 sync_max_show(struct mddev *mddev, char *page)
4326 {
4327         return sprintf(page, "%d (%s)\n", speed_max(mddev),
4328                        mddev->sync_speed_max ? "local": "system");
4329 }
4330
4331 static ssize_t
4332 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4333 {
4334         int max;
4335         char *e;
4336         if (strncmp(buf, "system", 6)==0) {
4337                 mddev->sync_speed_max = 0;
4338                 return len;
4339         }
4340         max = simple_strtoul(buf, &e, 10);
4341         if (buf == e || (*e && *e != '\n') || max <= 0)
4342                 return -EINVAL;
4343         mddev->sync_speed_max = max;
4344         return len;
4345 }
4346
4347 static struct md_sysfs_entry md_sync_max =
4348 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4349
4350 static ssize_t
4351 degraded_show(struct mddev *mddev, char *page)
4352 {
4353         return sprintf(page, "%d\n", mddev->degraded);
4354 }
4355 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4356
4357 static ssize_t
4358 sync_force_parallel_show(struct mddev *mddev, char *page)
4359 {
4360         return sprintf(page, "%d\n", mddev->parallel_resync);
4361 }
4362
4363 static ssize_t
4364 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4365 {
4366         long n;
4367
4368         if (strict_strtol(buf, 10, &n))
4369                 return -EINVAL;
4370
4371         if (n != 0 && n != 1)
4372                 return -EINVAL;
4373
4374         mddev->parallel_resync = n;
4375
4376         if (mddev->sync_thread)
4377                 wake_up(&resync_wait);
4378
4379         return len;
4380 }
4381
4382 /* force parallel resync, even with shared block devices */
4383 static struct md_sysfs_entry md_sync_force_parallel =
4384 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4385        sync_force_parallel_show, sync_force_parallel_store);
4386
4387 static ssize_t
4388 sync_speed_show(struct mddev *mddev, char *page)
4389 {
4390         unsigned long resync, dt, db;
4391         if (mddev->curr_resync == 0)
4392                 return sprintf(page, "none\n");
4393         resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4394         dt = (jiffies - mddev->resync_mark) / HZ;
4395         if (!dt) dt++;
4396         db = resync - mddev->resync_mark_cnt;
4397         return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4398 }
4399
4400 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4401
4402 static ssize_t
4403 sync_completed_show(struct mddev *mddev, char *page)
4404 {
4405         unsigned long long max_sectors, resync;
4406
4407         if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4408                 return sprintf(page, "none\n");
4409
4410         if (mddev->curr_resync == 1 ||
4411             mddev->curr_resync == 2)
4412                 return sprintf(page, "delayed\n");
4413
4414         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4415             test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4416                 max_sectors = mddev->resync_max_sectors;
4417         else
4418                 max_sectors = mddev->dev_sectors;
4419
4420         resync = mddev->curr_resync_completed;
4421         return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4422 }
4423
4424 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
4425
4426 static ssize_t
4427 min_sync_show(struct mddev *mddev, char *page)
4428 {
4429         return sprintf(page, "%llu\n",
4430                        (unsigned long long)mddev->resync_min);
4431 }
4432 static ssize_t
4433 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4434 {
4435         unsigned long long min;
4436         if (strict_strtoull(buf, 10, &min))
4437                 return -EINVAL;
4438         if (min > mddev->resync_max)
4439                 return -EINVAL;
4440         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4441                 return -EBUSY;
4442
4443         /* Must be a multiple of chunk_size */
4444         if (mddev->chunk_sectors) {
4445                 sector_t temp = min;
4446                 if (sector_div(temp, mddev->chunk_sectors))
4447                         return -EINVAL;
4448         }
4449         mddev->resync_min = min;
4450
4451         return len;
4452 }
4453
4454 static struct md_sysfs_entry md_min_sync =
4455 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4456
4457 static ssize_t
4458 max_sync_show(struct mddev *mddev, char *page)
4459 {
4460         if (mddev->resync_max == MaxSector)
4461                 return sprintf(page, "max\n");
4462         else
4463                 return sprintf(page, "%llu\n",
4464                                (unsigned long long)mddev->resync_max);
4465 }
4466 static ssize_t
4467 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4468 {
4469         if (strncmp(buf, "max", 3) == 0)
4470                 mddev->resync_max = MaxSector;
4471         else {
4472                 unsigned long long max;
4473                 if (strict_strtoull(buf, 10, &max))
4474                         return -EINVAL;
4475                 if (max < mddev->resync_min)
4476                         return -EINVAL;
4477                 if (max < mddev->resync_max &&
4478                     mddev->ro == 0 &&
4479                     test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4480                         return -EBUSY;
4481
4482                 /* Must be a multiple of chunk_size */
4483                 if (mddev->chunk_sectors) {
4484                         sector_t temp = max;
4485                         if (sector_div(temp, mddev->chunk_sectors))
4486                                 return -EINVAL;
4487                 }
4488                 mddev->resync_max = max;
4489         }
4490         wake_up(&mddev->recovery_wait);
4491         return len;
4492 }
4493
4494 static struct md_sysfs_entry md_max_sync =
4495 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4496
4497 static ssize_t
4498 suspend_lo_show(struct mddev *mddev, char *page)
4499 {
4500         return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4501 }
4502
4503 static ssize_t
4504 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4505 {
4506         char *e;
4507         unsigned long long new = simple_strtoull(buf, &e, 10);
4508         unsigned long long old = mddev->suspend_lo;
4509
4510         if (mddev->pers == NULL || 
4511             mddev->pers->quiesce == NULL)
4512                 return -EINVAL;
4513         if (buf == e || (*e && *e != '\n'))
4514                 return -EINVAL;
4515
4516         mddev->suspend_lo = new;
4517         if (new >= old)
4518                 /* Shrinking suspended region */
4519                 mddev->pers->quiesce(mddev, 2);
4520         else {
4521                 /* Expanding suspended region - need to wait */
4522                 mddev->pers->quiesce(mddev, 1);
4523                 mddev->pers->quiesce(mddev, 0);
4524         }
4525         return len;
4526 }
4527 static struct md_sysfs_entry md_suspend_lo =
4528 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4529
4530
4531 static ssize_t
4532 suspend_hi_show(struct mddev *mddev, char *page)
4533 {
4534         return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4535 }
4536
4537 static ssize_t
4538 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4539 {
4540         char *e;
4541         unsigned long long new = simple_strtoull(buf, &e, 10);
4542         unsigned long long old = mddev->suspend_hi;
4543
4544         if (mddev->pers == NULL ||
4545             mddev->pers->quiesce == NULL)
4546                 return -EINVAL;
4547         if (buf == e || (*e && *e != '\n'))
4548                 return -EINVAL;
4549
4550         mddev->suspend_hi = new;
4551         if (new <= old)
4552                 /* Shrinking suspended region */
4553                 mddev->pers->quiesce(mddev, 2);
4554         else {
4555                 /* Expanding suspended region - need to wait */
4556                 mddev->pers->quiesce(mddev, 1);
4557                 mddev->pers->quiesce(mddev, 0);
4558         }
4559         return len;
4560 }
4561 static struct md_sysfs_entry md_suspend_hi =
4562 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4563
4564 static ssize_t
4565 reshape_position_show(struct mddev *mddev, char *page)
4566 {
4567         if (mddev->reshape_position != MaxSector)
4568                 return sprintf(page, "%llu\n",
4569                                (unsigned long long)mddev->reshape_position);
4570         strcpy(page, "none\n");
4571         return 5;
4572 }
4573
4574 static ssize_t
4575 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4576 {
4577         struct md_rdev *rdev;
4578         char *e;
4579         unsigned long long new = simple_strtoull(buf, &e, 10);
4580         if (mddev->pers)
4581                 return -EBUSY;
4582         if (buf == e || (*e && *e != '\n'))
4583                 return -EINVAL;
4584         mddev->reshape_position = new;
4585         mddev->delta_disks = 0;
4586         mddev->reshape_backwards = 0;
4587         mddev->new_level = mddev->level;
4588         mddev->new_layout = mddev->layout;
4589         mddev->new_chunk_sectors = mddev->chunk_sectors;
4590         rdev_for_each(rdev, mddev)
4591                 rdev->new_data_offset = rdev->data_offset;
4592         return len;
4593 }
4594
4595 static struct md_sysfs_entry md_reshape_position =
4596 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4597        reshape_position_store);
4598
4599 static ssize_t
4600 reshape_direction_show(struct mddev *mddev, char *page)
4601 {
4602         return sprintf(page, "%s\n",
4603                        mddev->reshape_backwards ? "backwards" : "forwards");
4604 }
4605
4606 static ssize_t
4607 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4608 {
4609         int backwards = 0;
4610         if (cmd_match(buf, "forwards"))
4611                 backwards = 0;
4612         else if (cmd_match(buf, "backwards"))
4613                 backwards = 1;
4614         else
4615                 return -EINVAL;
4616         if (mddev->reshape_backwards == backwards)
4617                 return len;
4618
4619         /* check if we are allowed to change */
4620         if (mddev->delta_disks)
4621                 return -EBUSY;
4622
4623         if (mddev->persistent &&
4624             mddev->major_version == 0)
4625                 return -EINVAL;
4626
4627         mddev->reshape_backwards = backwards;
4628         return len;
4629 }
4630
4631 static struct md_sysfs_entry md_reshape_direction =
4632 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4633        reshape_direction_store);
4634
4635 static ssize_t
4636 array_size_show(struct mddev *mddev, char *page)
4637 {
4638         if (mddev->external_size)
4639                 return sprintf(page, "%llu\n",
4640                                (unsigned long long)mddev->array_sectors/2);
4641         else
4642                 return sprintf(page, "default\n");
4643 }
4644
4645 static ssize_t
4646 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4647 {
4648         sector_t sectors;
4649
4650         if (strncmp(buf, "default", 7) == 0) {
4651                 if (mddev->pers)
4652                         sectors = mddev->pers->size(mddev, 0, 0);
4653                 else
4654                         sectors = mddev->array_sectors;
4655
4656                 mddev->external_size = 0;
4657         } else {
4658                 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4659                         return -EINVAL;
4660                 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4661                         return -E2BIG;
4662
4663                 mddev->external_size = 1;
4664         }
4665
4666         mddev->array_sectors = sectors;
4667         if (mddev->pers) {
4668                 set_capacity(mddev->gendisk, mddev->array_sectors);
4669                 revalidate_disk(mddev->gendisk);
4670         }
4671         return len;
4672 }
4673
4674 static struct md_sysfs_entry md_array_size =
4675 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4676        array_size_store);
4677
4678 static struct attribute *md_default_attrs[] = {
4679         &md_level.attr,
4680         &md_layout.attr,
4681         &md_raid_disks.attr,
4682         &md_chunk_size.attr,
4683         &md_size.attr,
4684         &md_resync_start.attr,
4685         &md_metadata.attr,
4686         &md_new_device.attr,
4687         &md_safe_delay.attr,
4688         &md_array_state.attr,
4689         &md_reshape_position.attr,
4690         &md_reshape_direction.attr,
4691         &md_array_size.attr,
4692         &max_corr_read_errors.attr,
4693         NULL,
4694 };
4695
4696 static struct attribute *md_redundancy_attrs[] = {
4697         &md_scan_mode.attr,
4698         &md_mismatches.attr,
4699         &md_sync_min.attr,
4700         &md_sync_max.attr,
4701         &md_sync_speed.attr,
4702         &md_sync_force_parallel.attr,
4703         &md_sync_completed.attr,
4704         &md_min_sync.attr,
4705         &md_max_sync.attr,
4706         &md_suspend_lo.attr,
4707         &md_suspend_hi.attr,
4708         &md_bitmap.attr,
4709         &md_degraded.attr,
4710         NULL,
4711 };
4712 static struct attribute_group md_redundancy_group = {
4713         .name = NULL,
4714         .attrs = md_redundancy_attrs,
4715 };
4716
4717
4718 static ssize_t
4719 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4720 {
4721         struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4722         struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4723         ssize_t rv;
4724
4725         if (!entry->show)
4726                 return -EIO;
4727         spin_lock(&all_mddevs_lock);
4728         if (list_empty(&mddev->all_mddevs)) {
4729                 spin_unlock(&all_mddevs_lock);
4730                 return -EBUSY;
4731         }
4732         mddev_get(mddev);
4733         spin_unlock(&all_mddevs_lock);
4734
4735         rv = mddev_lock(mddev);
4736         if (!rv) {
4737                 rv = entry->show(mddev, page);
4738                 mddev_unlock(mddev);
4739         }
4740         mddev_put(mddev);
4741         return rv;
4742 }
4743
4744 static ssize_t
4745 md_attr_store(struct kobject *kobj, struct attribute *attr,
4746               const char *page, size_t length)
4747 {
4748         struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4749         struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4750         ssize_t rv;
4751
4752         if (!entry->store)
4753                 return -EIO;
4754         if (!capable(CAP_SYS_ADMIN))
4755                 return -EACCES;
4756         spin_lock(&all_mddevs_lock);
4757         if (list_empty(&mddev->all_mddevs)) {
4758                 spin_unlock(&all_mddevs_lock);
4759                 return -EBUSY;
4760         }
4761         mddev_get(mddev);
4762         spin_unlock(&all_mddevs_lock);
4763         if (entry->store == new_dev_store)
4764                 flush_workqueue(md_misc_wq);
4765         rv = mddev_lock(mddev);
4766         if (!rv) {
4767                 rv = entry->store(mddev, page, length);
4768                 mddev_unlock(mddev);
4769         }
4770         mddev_put(mddev);
4771         return rv;
4772 }
4773
4774 static void md_free(struct kobject *ko)
4775 {
4776         struct mddev *mddev = container_of(ko, struct mddev, kobj);
4777
4778         if (mddev->sysfs_state)
4779                 sysfs_put(mddev->sysfs_state);
4780
4781         if (mddev->gendisk) {
4782                 del_gendisk(mddev->gendisk);
4783                 put_disk(mddev->gendisk);
4784         }
4785         if (mddev->queue)
4786                 blk_cleanup_queue(mddev->queue);
4787
4788         kfree(mddev);
4789 }
4790
4791 static const struct sysfs_ops md_sysfs_ops = {
4792         .show   = md_attr_show,
4793         .store  = md_attr_store,
4794 };
4795 static struct kobj_type md_ktype = {
4796         .release        = md_free,
4797         .sysfs_ops      = &md_sysfs_ops,
4798         .default_attrs  = md_default_attrs,
4799 };
4800
4801 int mdp_major = 0;
4802
4803 static void mddev_delayed_delete(struct work_struct *ws)
4804 {
4805         struct mddev *mddev = container_of(ws, struct mddev, del_work);
4806
4807         sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4808         kobject_del(&mddev->kobj);
4809         kobject_put(&mddev->kobj);
4810 }
4811
4812 static int md_alloc(dev_t dev, char *name)
4813 {
4814         static DEFINE_MUTEX(disks_mutex);
4815         struct mddev *mddev = mddev_find(dev);
4816         struct gendisk *disk;
4817         int partitioned;
4818         int shift;
4819         int unit;
4820         int error;
4821
4822         if (!mddev)
4823                 return -ENODEV;
4824
4825         partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4826         shift = partitioned ? MdpMinorShift : 0;
4827         unit = MINOR(mddev->unit) >> shift;
4828
4829         /* wait for any previous instance of this device to be
4830          * completely removed (mddev_delayed_delete).
4831          */
4832         flush_workqueue(md_misc_wq);
4833
4834         mutex_lock(&disks_mutex);
4835         error = -EEXIST;
4836         if (mddev->gendisk)
4837                 goto abort;
4838
4839         if (name) {
4840                 /* Need to ensure that 'name' is not a duplicate.
4841                  */
4842                 struct mddev *mddev2;
4843                 spin_lock(&all_mddevs_lock);
4844
4845                 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4846                         if (mddev2->gendisk &&
4847                             strcmp(mddev2->gendisk->disk_name, name) == 0) {
4848                                 spin_unlock(&all_mddevs_lock);
4849                                 goto abort;
4850                         }
4851                 spin_unlock(&all_mddevs_lock);
4852         }
4853
4854         error = -ENOMEM;
4855         mddev->queue = blk_alloc_queue(GFP_KERNEL);
4856         if (!mddev->queue)
4857                 goto abort;
4858         mddev->queue->queuedata = mddev;
4859
4860         blk_queue_make_request(mddev->queue, md_make_request);
4861         blk_set_stacking_limits(&mddev->queue->limits);
4862
4863         disk = alloc_disk(1 << shift);
4864         if (!disk) {
4865                 blk_cleanup_queue(mddev->queue);
4866                 mddev->queue = NULL;
4867                 goto abort;
4868         }
4869         disk->major = MAJOR(mddev->unit);
4870         disk->first_minor = unit << shift;
4871         if (name)
4872                 strcpy(disk->disk_name, name);
4873         else if (partitioned)
4874                 sprintf(disk->disk_name, "md_d%d", unit);
4875         else
4876                 sprintf(disk->disk_name, "md%d", unit);
4877         disk->fops = &md_fops;
4878         disk->private_data = mddev;
4879         disk->queue = mddev->queue;
4880         blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4881         /* Allow extended partitions.  This makes the
4882          * 'mdp' device redundant, but we can't really
4883          * remove it now.
4884          */
4885         disk->flags |= GENHD_FL_EXT_DEVT;
4886         mddev->gendisk = disk;
4887         /* As soon as we call add_disk(), another thread could get
4888          * through to md_open, so make sure it doesn't get too far
4889          */
4890         mutex_lock(&mddev->open_mutex);
4891         add_disk(disk);
4892
4893         error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4894                                      &disk_to_dev(disk)->kobj, "%s", "md");
4895         if (error) {
4896                 /* This isn't possible, but as kobject_init_and_add is marked
4897                  * __must_check, we must do something with the result
4898                  */
4899                 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4900                        disk->disk_name);
4901                 error = 0;
4902         }
4903         if (mddev->kobj.sd &&
4904             sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4905                 printk(KERN_DEBUG "pointless warning\n");
4906         mutex_unlock(&mddev->open_mutex);
4907  abort:
4908         mutex_unlock(&disks_mutex);
4909         if (!error && mddev->kobj.sd) {
4910                 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4911                 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4912         }
4913         mddev_put(mddev);
4914         return error;
4915 }
4916
4917 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4918 {
4919         md_alloc(dev, NULL);
4920         return NULL;
4921 }
4922
4923 static int add_named_array(const char *val, struct kernel_param *kp)
4924 {
4925         /* val must be "md_*" where * is not all digits.
4926          * We allocate an array with a large free minor number, and
4927          * set the name to val.  val must not already be an active name.
4928          */
4929         int len = strlen(val);
4930         char buf[DISK_NAME_LEN];
4931
4932         while (len && val[len-1] == '\n')
4933                 len--;
4934         if (len >= DISK_NAME_LEN)
4935                 return -E2BIG;
4936         strlcpy(buf, val, len+1);
4937         if (strncmp(buf, "md_", 3) != 0)
4938                 return -EINVAL;
4939         return md_alloc(0, buf);
4940 }
4941
4942 static void md_safemode_timeout(unsigned long data)
4943 {
4944         struct mddev *mddev = (struct mddev *) data;
4945
4946         if (!atomic_read(&mddev->writes_pending)) {
4947                 mddev->safemode = 1;
4948                 if (mddev->external)
4949                         sysfs_notify_dirent_safe(mddev->sysfs_state);
4950         }
4951         md_wakeup_thread(mddev->thread);
4952 }
4953
4954 static int start_dirty_degraded;
4955
4956 int md_run(struct mddev *mddev)
4957 {
4958         int err;
4959         struct md_rdev *rdev;
4960         struct md_personality *pers;
4961
4962         if (list_empty(&mddev->disks))
4963                 /* cannot run an array with no devices.. */
4964                 return -EINVAL;
4965
4966         if (mddev->pers)
4967                 return -EBUSY;
4968         /* Cannot run until previous stop completes properly */
4969         if (mddev->sysfs_active)
4970                 return -EBUSY;
4971
4972         /*
4973          * Analyze all RAID superblock(s)
4974          */
4975         if (!mddev->raid_disks) {
4976                 if (!mddev->persistent)
4977                         return -EINVAL;
4978                 analyze_sbs(mddev);
4979         }
4980
4981         if (mddev->level != LEVEL_NONE)
4982                 request_module("md-level-%d", mddev->level);
4983         else if (mddev->clevel[0])
4984                 request_module("md-%s", mddev->clevel);
4985
4986         /*
4987          * Drop all container device buffers, from now on
4988          * the only valid external interface is through the md
4989          * device.
4990          */
4991         rdev_for_each(rdev, mddev) {
4992                 if (test_bit(Faulty, &rdev->flags))
4993                         continue;
4994                 sync_blockdev(rdev->bdev);
4995                 invalidate_bdev(rdev->bdev);
4996
4997                 /* perform some consistency tests on the device.
4998                  * We don't want the data to overlap the metadata,
4999                  * Internal Bitmap issues have been handled elsewhere.
5000                  */
5001                 if (rdev->meta_bdev) {
5002                         /* Nothing to check */;
5003                 } else if (rdev->data_offset < rdev->sb_start) {
5004                         if (mddev->dev_sectors &&
5005                             rdev->data_offset + mddev->dev_sectors
5006                             > rdev->sb_start) {
5007                                 printk("md: %s: data overlaps metadata\n",
5008                                        mdname(mddev));
5009                                 return -EINVAL;
5010                         }
5011                 } else {
5012                         if (rdev->sb_start + rdev->sb_size/512
5013                             > rdev->data_offset) {
5014                                 printk("md: %s: metadata overlaps data\n",
5015                                        mdname(mddev));
5016                                 return -EINVAL;
5017                         }
5018                 }
5019                 sysfs_notify_dirent_safe(rdev->sysfs_state);
5020         }
5021
5022         if (mddev->bio_set == NULL)
5023                 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
5024
5025         spin_lock(&pers_lock);
5026         pers = find_pers(mddev->level, mddev->clevel);
5027         if (!pers || !try_module_get(pers->owner)) {
5028                 spin_unlock(&pers_lock);
5029                 if (mddev->level != LEVEL_NONE)
5030                         printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
5031                                mddev->level);
5032                 else
5033                         printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5034                                mddev->clevel);
5035                 return -EINVAL;
5036         }
5037         mddev->pers = pers;
5038         spin_unlock(&pers_lock);
5039         if (mddev->level != pers->level) {
5040                 mddev->level = pers->level;
5041                 mddev->new_level = pers->level;
5042         }
5043         strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5044
5045         if (mddev->reshape_position != MaxSector &&
5046             pers->start_reshape == NULL) {
5047                 /* This personality cannot handle reshaping... */
5048                 mddev->pers = NULL;
5049                 module_put(pers->owner);
5050                 return -EINVAL;
5051         }
5052
5053         if (pers->sync_request) {
5054                 /* Warn if this is a potentially silly
5055                  * configuration.
5056                  */
5057                 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5058                 struct md_rdev *rdev2;
5059                 int warned = 0;
5060
5061                 rdev_for_each(rdev, mddev)
5062                         rdev_for_each(rdev2, mddev) {
5063                                 if (rdev < rdev2 &&
5064                                     rdev->bdev->bd_contains ==
5065                                     rdev2->bdev->bd_contains) {
5066                                         printk(KERN_WARNING
5067                                                "%s: WARNING: %s appears to be"
5068                                                " on the same physical disk as"
5069                                                " %s.\n",
5070                                                mdname(mddev),
5071                                                bdevname(rdev->bdev,b),
5072                                                bdevname(rdev2->bdev,b2));
5073                                         warned = 1;
5074                                 }
5075                         }
5076
5077                 if (warned)
5078                         printk(KERN_WARNING
5079                                "True protection against single-disk"
5080                                " failure might be compromised.\n");
5081         }
5082
5083         mddev->recovery = 0;
5084         /* may be over-ridden by personality */
5085         mddev->resync_max_sectors = mddev->dev_sectors;
5086
5087         mddev->ok_start_degraded = start_dirty_degraded;
5088
5089         if (start_readonly && mddev->ro == 0)
5090                 mddev->ro = 2; /* read-only, but switch on first write */
5091
5092         err = mddev->pers->run(mddev);
5093         if (err)
5094                 printk(KERN_ERR "md: pers->run() failed ...\n");
5095         else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5096                 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5097                           " but 'external_size' not in effect?\n", __func__);
5098                 printk(KERN_ERR
5099                        "md: invalid array_size %llu > default size %llu\n",
5100                        (unsigned long long)mddev->array_sectors / 2,
5101                        (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5102                 err = -EINVAL;
5103                 mddev->pers->stop(mddev);
5104         }
5105         if (err == 0 && mddev->pers->sync_request &&
5106             (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5107                 err = bitmap_create(mddev);
5108                 if (err) {
5109                         printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5110                                mdname(mddev), err);
5111                         mddev->pers->stop(mddev);
5112                 }
5113         }
5114         if (err) {
5115                 module_put(mddev->pers->owner);
5116                 mddev->pers = NULL;
5117                 bitmap_destroy(mddev);
5118                 return err;
5119         }
5120         if (mddev->pers->sync_request) {
5121                 if (mddev->kobj.sd &&
5122                     sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5123                         printk(KERN_WARNING
5124                                "md: cannot register extra attributes for %s\n",
5125                                mdname(mddev));
5126                 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5127         } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5128                 mddev->ro = 0;
5129
5130         atomic_set(&mddev->writes_pending,0);
5131         atomic_set(&mddev->max_corr_read_errors,
5132                    MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5133         mddev->safemode = 0;
5134         mddev->safemode_timer.function = md_safemode_timeout;
5135         mddev->safemode_timer.data = (unsigned long) mddev;
5136         mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5137         mddev->in_sync = 1;
5138         smp_wmb();
5139         mddev->ready = 1;
5140         rdev_for_each(rdev, mddev)
5141                 if (rdev->raid_disk >= 0)
5142                         if (sysfs_link_rdev(mddev, rdev))
5143                                 /* failure here is OK */;
5144         
5145         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5146         
5147         if (mddev->flags)
5148                 md_update_sb(mddev, 0);
5149
5150         md_new_event(mddev);
5151         sysfs_notify_dirent_safe(mddev->sysfs_state);
5152         sysfs_notify_dirent_safe(mddev->sysfs_action);
5153         sysfs_notify(&mddev->kobj, NULL, "degraded");
5154         return 0;
5155 }
5156 EXPORT_SYMBOL_GPL(md_run);
5157
5158 static int do_md_run(struct mddev *mddev)
5159 {
5160         int err;
5161
5162         err = md_run(mddev);
5163         if (err)
5164                 goto out;
5165         err = bitmap_load(mddev);
5166         if (err) {
5167                 bitmap_destroy(mddev);
5168                 goto out;
5169         }
5170
5171         md_wakeup_thread(mddev->thread);
5172         md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5173
5174         set_capacity(mddev->gendisk, mddev->array_sectors);
5175         revalidate_disk(mddev->gendisk);
5176         mddev->changed = 1;
5177         kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5178 out:
5179         return err;
5180 }
5181
5182 static int restart_array(struct mddev *mddev)
5183 {
5184         struct gendisk *disk = mddev->gendisk;
5185
5186         /* Complain if it has no devices */
5187         if (list_empty(&mddev->disks))
5188                 return -ENXIO;
5189         if (!mddev->pers)
5190                 return -EINVAL;
5191         if (!mddev->ro)
5192                 return -EBUSY;
5193         mddev->safemode = 0;
5194         mddev->ro = 0;
5195         set_disk_ro(disk, 0);
5196         printk(KERN_INFO "md: %s switched to read-write mode.\n",
5197                 mdname(mddev));
5198         /* Kick recovery or resync if necessary */
5199         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5200         md_wakeup_thread(mddev->thread);
5201         md_wakeup_thread(mddev->sync_thread);
5202         sysfs_notify_dirent_safe(mddev->sysfs_state);
5203         return 0;
5204 }
5205
5206 /* similar to deny_write_access, but accounts for our holding a reference
5207  * to the file ourselves */
5208 static int deny_bitmap_write_access(struct file * file)
5209 {
5210         struct inode *inode = file->f_mapping->host;
5211
5212         spin_lock(&inode->i_lock);
5213         if (atomic_read(&inode->i_writecount) > 1) {
5214                 spin_unlock(&inode->i_lock);
5215                 return -ETXTBSY;
5216         }
5217         atomic_set(&inode->i_writecount, -1);
5218         spin_unlock(&inode->i_lock);
5219
5220         return 0;
5221 }
5222
5223 void restore_bitmap_write_access(struct file *file)
5224 {
5225         struct inode *inode = file->f_mapping->host;
5226
5227         spin_lock(&inode->i_lock);
5228         atomic_set(&inode->i_writecount, 1);
5229         spin_unlock(&inode->i_lock);
5230 }
5231
5232 static void md_clean(struct mddev *mddev)
5233 {
5234         mddev->array_sectors = 0;
5235         mddev->external_size = 0;
5236         mddev->dev_sectors = 0;
5237         mddev->raid_disks = 0;
5238         mddev->recovery_cp = 0;
5239         mddev->resync_min = 0;
5240         mddev->resync_max = MaxSector;
5241         mddev->reshape_position = MaxSector;
5242         mddev->external = 0;
5243         mddev->persistent = 0;
5244         mddev->level = LEVEL_NONE;
5245         mddev->clevel[0] = 0;
5246         mddev->flags = 0;
5247         mddev->ro = 0;
5248         mddev->metadata_type[0] = 0;
5249         mddev->chunk_sectors = 0;
5250         mddev->ctime = mddev->utime = 0;
5251         mddev->layout = 0;
5252         mddev->max_disks = 0;
5253         mddev->events = 0;
5254         mddev->can_decrease_events = 0;
5255         mddev->delta_disks = 0;
5256         mddev->reshape_backwards = 0;
5257         mddev->new_level = LEVEL_NONE;
5258         mddev->new_layout = 0;
5259         mddev->new_chunk_sectors = 0;
5260         mddev->curr_resync = 0;
5261         atomic64_set(&mddev->resync_mismatches, 0);
5262         mddev->suspend_lo = mddev->suspend_hi = 0;
5263         mddev->sync_speed_min = mddev->sync_speed_max = 0;
5264         mddev->recovery = 0;
5265         mddev->in_sync = 0;
5266         mddev->changed = 0;
5267         mddev->degraded = 0;
5268         mddev->safemode = 0;
5269         mddev->merge_check_needed = 0;
5270         mddev->bitmap_info.offset = 0;
5271         mddev->bitmap_info.default_offset = 0;
5272         mddev->bitmap_info.default_space = 0;
5273         mddev->bitmap_info.chunksize = 0;
5274         mddev->bitmap_info.daemon_sleep = 0;
5275         mddev->bitmap_info.max_write_behind = 0;
5276 }
5277
5278 static void __md_stop_writes(struct mddev *mddev)
5279 {
5280         if (mddev->sync_thread) {
5281                 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5282                 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5283                 md_reap_sync_thread(mddev);
5284         }
5285
5286         del_timer_sync(&mddev->safemode_timer);
5287
5288         bitmap_flush(mddev);
5289         md_super_wait(mddev);
5290
5291         if (mddev->ro == 0 &&
5292             (!mddev->in_sync || mddev->flags)) {
5293                 /* mark array as shutdown cleanly */
5294                 mddev->in_sync = 1;
5295                 md_update_sb(mddev, 1);
5296         }
5297 }
5298
5299 void md_stop_writes(struct mddev *mddev)
5300 {
5301         mddev_lock(mddev);
5302         __md_stop_writes(mddev);
5303         mddev_unlock(mddev);
5304 }
5305 EXPORT_SYMBOL_GPL(md_stop_writes);
5306
5307 static void __md_stop(struct mddev *mddev)
5308 {
5309         mddev->ready = 0;
5310         mddev->pers->stop(mddev);
5311         if (mddev->pers->sync_request && mddev->to_remove == NULL)
5312                 mddev->to_remove = &md_redundancy_group;
5313         module_put(mddev->pers->owner);
5314         mddev->pers = NULL;
5315         clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5316 }
5317
5318 void md_stop(struct mddev *mddev)
5319 {
5320         /* stop the array and free an attached data structures.
5321          * This is called from dm-raid
5322          */
5323         __md_stop(mddev);
5324         bitmap_destroy(mddev);
5325         if (mddev->bio_set)
5326                 bioset_free(mddev->bio_set);
5327 }
5328
5329 EXPORT_SYMBOL_GPL(md_stop);
5330
5331 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5332 {
5333         int err = 0;
5334         mutex_lock(&mddev->open_mutex);
5335         if (atomic_read(&mddev->openers) > !!bdev) {
5336                 printk("md: %s still in use.\n",mdname(mddev));
5337                 err = -EBUSY;
5338                 goto out;
5339         }
5340         if (bdev)
5341                 sync_blockdev(bdev);
5342         if (mddev->pers) {
5343                 __md_stop_writes(mddev);
5344
5345                 err  = -ENXIO;
5346                 if (mddev->ro==1)
5347                         goto out;
5348                 mddev->ro = 1;
5349                 set_disk_ro(mddev->gendisk, 1);
5350                 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5351                 sysfs_notify_dirent_safe(mddev->sysfs_state);
5352                 err = 0;        
5353         }
5354 out:
5355         mutex_unlock(&mddev->open_mutex);
5356         return err;
5357 }
5358
5359 /* mode:
5360  *   0 - completely stop and dis-assemble array
5361  *   2 - stop but do not disassemble array
5362  */
5363 static int do_md_stop(struct mddev * mddev, int mode,
5364                       struct block_device *bdev)
5365 {
5366         struct gendisk *disk = mddev->gendisk;
5367         struct md_rdev *rdev;
5368
5369         mutex_lock(&mddev->open_mutex);
5370         if (atomic_read(&mddev->openers) > !!bdev ||
5371             mddev->sysfs_active) {
5372                 printk("md: %s still in use.\n",mdname(mddev));
5373                 mutex_unlock(&mddev->open_mutex);
5374                 return -EBUSY;
5375         }
5376         if (bdev)
5377                 /* It is possible IO was issued on some other
5378                  * open file which was closed before we took ->open_mutex.
5379                  * As that was not the last close __blkdev_put will not
5380                  * have called sync_blockdev, so we must.
5381                  */
5382                 sync_blockdev(bdev);
5383
5384         if (mddev->pers) {
5385                 if (mddev->ro)
5386                         set_disk_ro(disk, 0);
5387
5388                 __md_stop_writes(mddev);
5389                 __md_stop(mddev);
5390                 mddev->queue->merge_bvec_fn = NULL;
5391                 mddev->queue->backing_dev_info.congested_fn = NULL;
5392
5393                 /* tell userspace to handle 'inactive' */
5394                 sysfs_notify_dirent_safe(mddev->sysfs_state);
5395
5396                 rdev_for_each(rdev, mddev)
5397                         if (rdev->raid_disk >= 0)
5398                                 sysfs_unlink_rdev(mddev, rdev);
5399
5400                 set_capacity(disk, 0);
5401                 mutex_unlock(&mddev->open_mutex);
5402                 mddev->changed = 1;
5403                 revalidate_disk(disk);
5404
5405                 if (mddev->ro)
5406                         mddev->ro = 0;
5407         } else
5408                 mutex_unlock(&mddev->open_mutex);
5409         /*
5410          * Free resources if final stop
5411          */
5412         if (mode == 0) {
5413                 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5414
5415                 bitmap_destroy(mddev);
5416                 if (mddev->bitmap_info.file) {
5417                         restore_bitmap_write_access(mddev->bitmap_info.file);
5418                         fput(mddev->bitmap_info.file);
5419                         mddev->bitmap_info.file = NULL;
5420                 }
5421                 mddev->bitmap_info.offset = 0;
5422
5423                 export_array(mddev);
5424
5425                 md_clean(mddev);
5426                 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5427                 if (mddev->hold_active == UNTIL_STOP)
5428                         mddev->hold_active = 0;
5429         }
5430         blk_integrity_unregister(disk);
5431         md_new_event(mddev);
5432         sysfs_notify_dirent_safe(mddev->sysfs_state);
5433         return 0;
5434 }
5435
5436 #ifndef MODULE
5437 static void autorun_array(struct mddev *mddev)
5438 {
5439         struct md_rdev *rdev;
5440         int err;
5441
5442         if (list_empty(&mddev->disks))
5443                 return;
5444
5445         printk(KERN_INFO "md: running: ");
5446
5447         rdev_for_each(rdev, mddev) {
5448                 char b[BDEVNAME_SIZE];
5449                 printk("<%s>", bdevname(rdev->bdev,b));
5450         }
5451         printk("\n");
5452
5453         err = do_md_run(mddev);
5454         if (err) {
5455                 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
5456                 do_md_stop(mddev, 0, NULL);
5457         }
5458 }
5459
5460 /*
5461  * lets try to run arrays based on all disks that have arrived
5462  * until now. (those are in pending_raid_disks)
5463  *
5464  * the method: pick the first pending disk, collect all disks with
5465  * the same UUID, remove all from the pending list and put them into
5466  * the 'same_array' list. Then order this list based on superblock
5467  * update time (freshest comes first), kick out 'old' disks and
5468  * compare superblocks. If everything's fine then run it.
5469  *
5470  * If "unit" is allocated, then bump its reference count
5471  */
5472 static void autorun_devices(int part)
5473 {
5474         struct md_rdev *rdev0, *rdev, *tmp;
5475         struct mddev *mddev;
5476         char b[BDEVNAME_SIZE];
5477
5478         printk(KERN_INFO "md: autorun ...\n");
5479         while (!list_empty(&pending_raid_disks)) {
5480                 int unit;
5481                 dev_t dev;
5482                 LIST_HEAD(candidates);
5483                 rdev0 = list_entry(pending_raid_disks.next,
5484                                          struct md_rdev, same_set);
5485
5486                 printk(KERN_INFO "md: considering %s ...\n",
5487                         bdevname(rdev0->bdev,b));
5488                 INIT_LIST_HEAD(&candidates);
5489                 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5490                         if (super_90_load(rdev, rdev0, 0) >= 0) {
5491                                 printk(KERN_INFO "md:  adding %s ...\n",
5492                                         bdevname(rdev->bdev,b));
5493                                 list_move(&rdev->same_set, &candidates);
5494                         }
5495                 /*
5496                  * now we have a set of devices, with all of them having
5497                  * mostly sane superblocks. It's time to allocate the
5498                  * mddev.
5499                  */
5500                 if (part) {
5501                         dev = MKDEV(mdp_major,
5502                                     rdev0->preferred_minor << MdpMinorShift);
5503                         unit = MINOR(dev) >> MdpMinorShift;
5504                 } else {
5505                         dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5506                         unit = MINOR(dev);
5507                 }
5508                 if (rdev0->preferred_minor != unit) {
5509                         printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5510                                bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5511                         break;
5512                 }
5513
5514                 md_probe(dev, NULL, NULL);
5515                 mddev = mddev_find(dev);
5516                 if (!mddev || !mddev->gendisk) {
5517                         if (mddev)
5518                                 mddev_put(mddev);
5519                         printk(KERN_ERR
5520                                 "md: cannot allocate memory for md drive.\n");
5521                         break;
5522                 }
5523                 if (mddev_lock(mddev)) 
5524                         printk(KERN_WARNING "md: %s locked, cannot run\n",
5525                                mdname(mddev));
5526                 else if (mddev->raid_disks || mddev->major_version
5527                          || !list_empty(&mddev->disks)) {
5528                         printk(KERN_WARNING 
5529                                 "md: %s already running, cannot run %s\n",
5530                                 mdname(mddev), bdevname(rdev0->bdev,b));
5531                         mddev_unlock(mddev);
5532                 } else {
5533                         printk(KERN_INFO "md: created %s\n", mdname(mddev));
5534                         mddev->persistent = 1;
5535                         rdev_for_each_list(rdev, tmp, &candidates) {
5536                                 list_del_init(&rdev->same_set);
5537                                 if (bind_rdev_to_array(rdev, mddev))
5538                                         export_rdev(rdev);
5539                         }
5540                         autorun_array(mddev);
5541                         mddev_unlock(mddev);
5542                 }
5543                 /* on success, candidates will be empty, on error
5544                  * it won't...
5545                  */
5546                 rdev_for_each_list(rdev, tmp, &candidates) {
5547                         list_del_init(&rdev->same_set);
5548                         export_rdev(rdev);
5549                 }
5550                 mddev_put(mddev);
5551         }
5552         printk(KERN_INFO "md: ... autorun DONE.\n");
5553 }
5554 #endif /* !MODULE */
5555
5556 static int get_version(void __user * arg)
5557 {
5558         mdu_version_t ver;
5559
5560         ver.major = MD_MAJOR_VERSION;
5561         ver.minor = MD_MINOR_VERSION;
5562         ver.patchlevel = MD_PATCHLEVEL_VERSION;
5563
5564         if (copy_to_user(arg, &ver, sizeof(ver)))
5565                 return -EFAULT;
5566
5567         return 0;
5568 }
5569
5570 static int get_array_info(struct mddev * mddev, void __user * arg)
5571 {
5572         mdu_array_info_t info;
5573         int nr,working,insync,failed,spare;
5574         struct md_rdev *rdev;
5575
5576         nr = working = insync = failed = spare = 0;
5577         rcu_read_lock();
5578         rdev_for_each_rcu(rdev, mddev) {
5579                 nr++;
5580                 if (test_bit(Faulty, &rdev->flags))
5581                         failed++;
5582                 else {
5583                         working++;
5584                         if (test_bit(In_sync, &rdev->flags))
5585                                 insync++;       
5586                         else
5587                                 spare++;
5588                 }
5589         }
5590         rcu_read_unlock();
5591
5592         info.major_version = mddev->major_version;
5593         info.minor_version = mddev->minor_version;
5594         info.patch_version = MD_PATCHLEVEL_VERSION;
5595         info.ctime         = mddev->ctime;
5596         info.level         = mddev->level;
5597         info.size          = mddev->dev_sectors / 2;
5598         if (info.size != mddev->dev_sectors / 2) /* overflow */
5599                 info.size = -1;
5600         info.nr_disks      = nr;
5601         info.raid_disks    = mddev->raid_disks;
5602         info.md_minor      = mddev->md_minor;
5603         info.not_persistent= !mddev->persistent;
5604
5605         info.utime         = mddev->utime;
5606         info.state         = 0;
5607         if (mddev->in_sync)
5608                 info.state = (1<<MD_SB_CLEAN);
5609         if (mddev->bitmap && mddev->bitmap_info.offset)
5610                 info.state = (1<<MD_SB_BITMAP_PRESENT);
5611         info.active_disks  = insync;
5612         info.working_disks = working;
5613         info.failed_disks  = failed;
5614         info.spare_disks   = spare;
5615
5616         info.layout        = mddev->layout;
5617         info.chunk_size    = mddev->chunk_sectors << 9;
5618
5619         if (copy_to_user(arg, &info, sizeof(info)))
5620                 return -EFAULT;
5621
5622         return 0;
5623 }
5624
5625 static int get_bitmap_file(struct mddev * mddev, void __user * arg)
5626 {
5627         mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5628         char *ptr, *buf = NULL;
5629         int err = -ENOMEM;
5630
5631         if (md_allow_write(mddev))
5632                 file = kmalloc(sizeof(*file), GFP_NOIO);
5633         else
5634                 file = kmalloc(sizeof(*file), GFP_KERNEL);
5635
5636         if (!file)
5637                 goto out;
5638
5639         /* bitmap disabled, zero the first byte and copy out */
5640         if (!mddev->bitmap || !mddev->bitmap->storage.file) {
5641                 file->pathname[0] = '\0';
5642                 goto copy_out;
5643         }
5644
5645         buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5646         if (!buf)
5647                 goto out;
5648
5649         ptr = d_path(&mddev->bitmap->storage.file->f_path,
5650                      buf, sizeof(file->pathname));
5651         if (IS_ERR(ptr))
5652                 goto out;
5653
5654         strcpy(file->pathname, ptr);
5655
5656 copy_out:
5657         err = 0;
5658         if (copy_to_user(arg, file, sizeof(*file)))
5659                 err = -EFAULT;
5660 out:
5661         kfree(buf);
5662         kfree(file);
5663         return err;
5664 }
5665
5666 static int get_disk_info(struct mddev * mddev, void __user * arg)
5667 {
5668         mdu_disk_info_t info;
5669         struct md_rdev *rdev;
5670
5671         if (copy_from_user(&info, arg, sizeof(info)))
5672                 return -EFAULT;
5673
5674         rcu_read_lock();
5675         rdev = find_rdev_nr_rcu(mddev, info.number);
5676         if (rdev) {
5677                 info.major = MAJOR(rdev->bdev->bd_dev);
5678                 info.minor = MINOR(rdev->bdev->bd_dev);
5679                 info.raid_disk = rdev->raid_disk;
5680                 info.state = 0;
5681                 if (test_bit(Faulty, &rdev->flags))
5682                         info.state |= (1<<MD_DISK_FAULTY);
5683                 else if (test_bit(In_sync, &rdev->flags)) {
5684                         info.state |= (1<<MD_DISK_ACTIVE);
5685                         info.state |= (1<<MD_DISK_SYNC);
5686                 }
5687                 if (test_bit(WriteMostly, &rdev->flags))
5688                         info.state |= (1<<MD_DISK_WRITEMOSTLY);
5689         } else {
5690                 info.major = info.minor = 0;
5691                 info.raid_disk = -1;
5692                 info.state = (1<<MD_DISK_REMOVED);
5693         }
5694         rcu_read_unlock();
5695
5696         if (copy_to_user(arg, &info, sizeof(info)))
5697                 return -EFAULT;
5698
5699         return 0;
5700 }
5701
5702 static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
5703 {
5704         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5705         struct md_rdev *rdev;
5706         dev_t dev = MKDEV(info->major,info->minor);
5707
5708         if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5709                 return -EOVERFLOW;
5710
5711         if (!mddev->raid_disks) {
5712                 int err;
5713                 /* expecting a device which has a superblock */
5714                 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5715                 if (IS_ERR(rdev)) {
5716                         printk(KERN_WARNING 
5717                                 "md: md_import_device returned %ld\n",
5718                                 PTR_ERR(rdev));
5719                         return PTR_ERR(rdev);
5720                 }
5721                 if (!list_empty(&mddev->disks)) {
5722                         struct md_rdev *rdev0
5723                                 = list_entry(mddev->disks.next,
5724                                              struct md_rdev, same_set);
5725                         err = super_types[mddev->major_version]
5726                                 .load_super(rdev, rdev0, mddev->minor_version);
5727                         if (err < 0) {
5728                                 printk(KERN_WARNING 
5729                                         "md: %s has different UUID to %s\n",
5730                                         bdevname(rdev->bdev,b), 
5731                                         bdevname(rdev0->bdev,b2));
5732                                 export_rdev(rdev);
5733                                 return -EINVAL;
5734                         }
5735                 }
5736                 err = bind_rdev_to_array(rdev, mddev);
5737                 if (err)
5738                         export_rdev(rdev);
5739                 return err;
5740         }
5741
5742         /*
5743          * add_new_disk can be used once the array is assembled
5744          * to add "hot spares".  They must already have a superblock
5745          * written
5746          */
5747         if (mddev->pers) {
5748                 int err;
5749                 if (!mddev->pers->hot_add_disk) {
5750                         printk(KERN_WARNING 
5751                                 "%s: personality does not support diskops!\n",
5752                                mdname(mddev));
5753                         return -EINVAL;
5754                 }
5755                 if (mddev->persistent)
5756                         rdev = md_import_device(dev, mddev->major_version,
5757                                                 mddev->minor_version);
5758                 else
5759                         rdev = md_import_device(dev, -1, -1);
5760                 if (IS_ERR(rdev)) {
5761                         printk(KERN_WARNING 
5762                                 "md: md_import_device returned %ld\n",
5763                                 PTR_ERR(rdev));
5764                         return PTR_ERR(rdev);
5765                 }
5766                 /* set saved_raid_disk if appropriate */
5767                 if (!mddev->persistent) {
5768                         if (info->state & (1<<MD_DISK_SYNC)  &&
5769                             info->raid_disk < mddev->raid_disks) {
5770                                 rdev->raid_disk = info->raid_disk;
5771                                 set_bit(In_sync, &rdev->flags);
5772                         } else
5773                                 rdev->raid_disk = -1;
5774                 } else
5775                         super_types[mddev->major_version].
5776                                 validate_super(mddev, rdev);
5777                 if ((info->state & (1<<MD_DISK_SYNC)) &&
5778                      rdev->raid_disk != info->raid_disk) {
5779                         /* This was a hot-add request, but events doesn't
5780                          * match, so reject it.
5781                          */
5782                         export_rdev(rdev);
5783                         return -EINVAL;
5784                 }
5785
5786                 if (test_bit(In_sync, &rdev->flags))
5787                         rdev->saved_raid_disk = rdev->raid_disk;
5788                 else
5789                         rdev->saved_raid_disk = -1;
5790
5791                 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5792                 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5793                         set_bit(WriteMostly, &rdev->flags);
5794                 else
5795                         clear_bit(WriteMostly, &rdev->flags);
5796
5797                 rdev->raid_disk = -1;
5798                 err = bind_rdev_to_array(rdev, mddev);
5799                 if (!err && !mddev->pers->hot_remove_disk) {
5800                         /* If there is hot_add_disk but no hot_remove_disk
5801                          * then added disks for geometry changes,
5802                          * and should be added immediately.
5803                          */
5804                         super_types[mddev->major_version].
5805                                 validate_super(mddev, rdev);
5806                         err = mddev->pers->hot_add_disk(mddev, rdev);
5807                         if (err)
5808                                 unbind_rdev_from_array(rdev);
5809                 }
5810                 if (err)
5811                         export_rdev(rdev);
5812                 else
5813                         sysfs_notify_dirent_safe(rdev->sysfs_state);
5814
5815                 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5816                 if (mddev->degraded)
5817                         set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5818                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5819                 if (!err)
5820                         md_new_event(mddev);
5821                 md_wakeup_thread(mddev->thread);
5822                 return err;
5823         }
5824
5825         /* otherwise, add_new_disk is only allowed
5826          * for major_version==0 superblocks
5827          */
5828         if (mddev->major_version != 0) {
5829                 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5830                        mdname(mddev));
5831                 return -EINVAL;
5832         }
5833
5834         if (!(info->state & (1<<MD_DISK_FAULTY))) {
5835                 int err;
5836                 rdev = md_import_device(dev, -1, 0);
5837                 if (IS_ERR(rdev)) {
5838                         printk(KERN_WARNING 
5839                                 "md: error, md_import_device() returned %ld\n",
5840                                 PTR_ERR(rdev));
5841                         return PTR_ERR(rdev);
5842                 }
5843                 rdev->desc_nr = info->number;
5844                 if (info->raid_disk < mddev->raid_disks)
5845                         rdev->raid_disk = info->raid_disk;
5846                 else
5847                         rdev->raid_disk = -1;
5848
5849                 if (rdev->raid_disk < mddev->raid_disks)
5850                         if (info->state & (1<<MD_DISK_SYNC))
5851                                 set_bit(In_sync, &rdev->flags);
5852
5853                 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5854                         set_bit(WriteMostly, &rdev->flags);
5855
5856                 if (!mddev->persistent) {
5857                         printk(KERN_INFO "md: nonpersistent superblock ...\n");
5858                         rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5859                 } else
5860                         rdev->sb_start = calc_dev_sboffset(rdev);
5861                 rdev->sectors = rdev->sb_start;
5862
5863                 err = bind_rdev_to_array(rdev, mddev);
5864                 if (err) {
5865                         export_rdev(rdev);
5866                         return err;
5867                 }
5868         }
5869
5870         return 0;
5871 }
5872
5873 static int hot_remove_disk(struct mddev * mddev, dev_t dev)
5874 {
5875         char b[BDEVNAME_SIZE];
5876         struct md_rdev *rdev;
5877
5878         rdev = find_rdev(mddev, dev);
5879         if (!rdev)
5880                 return -ENXIO;
5881
5882         clear_bit(Blocked, &rdev->flags);
5883         remove_and_add_spares(mddev, rdev);
5884
5885         if (rdev->raid_disk >= 0)
5886                 goto busy;
5887
5888         kick_rdev_from_array(rdev);
5889         md_update_sb(mddev, 1);
5890         md_new_event(mddev);
5891
5892         return 0;
5893 busy:
5894         printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5895                 bdevname(rdev->bdev,b), mdname(mddev));
5896         return -EBUSY;
5897 }
5898
5899 static int hot_add_disk(struct mddev * mddev, dev_t dev)
5900 {
5901         char b[BDEVNAME_SIZE];
5902         int err;
5903         struct md_rdev *rdev;
5904
5905         if (!mddev->pers)
5906                 return -ENODEV;
5907
5908         if (mddev->major_version != 0) {
5909                 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5910                         " version-0 superblocks.\n",
5911                         mdname(mddev));
5912                 return -EINVAL;
5913         }
5914         if (!mddev->pers->hot_add_disk) {
5915                 printk(KERN_WARNING 
5916                         "%s: personality does not support diskops!\n",
5917                         mdname(mddev));
5918                 return -EINVAL;
5919         }
5920
5921         rdev = md_import_device(dev, -1, 0);
5922         if (IS_ERR(rdev)) {
5923                 printk(KERN_WARNING 
5924                         "md: error, md_import_device() returned %ld\n",
5925                         PTR_ERR(rdev));
5926                 return -EINVAL;
5927         }
5928
5929         if (mddev->persistent)
5930                 rdev->sb_start = calc_dev_sboffset(rdev);
5931         else
5932          &nbs