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