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