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