]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/md/md.c
Merge tag 'drm-intel-fixes-2014-01-28' of git://people.freedesktop.org/~danvet/drm...
[karo-tx-linux.git] / drivers / md / md.c
1 /*
2    md.c : Multiple Devices driver for Linux
3           Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5      completely rewritten, based on the MD driver code from Marc Zyngier
6
7    Changes:
8
9    - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10    - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11    - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12    - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13    - kmod support by: Cyrus Durgin
14    - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15    - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17    - lots of fixes and improvements to the RAID1/RAID5 and generic
18      RAID code (such as request based resynchronization):
19
20      Neil Brown <neilb@cse.unsw.edu.au>.
21
22    - persistent bitmap code
23      Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
25    This program is free software; you can redistribute it and/or modify
26    it under the terms of the GNU General Public License as published by
27    the Free Software Foundation; either version 2, or (at your option)
28    any later version.
29
30    You should have received a copy of the GNU General Public License
31    (for example /usr/src/linux/COPYING); if not, write to the Free
32    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 */
34
35 #include <linux/kthread.h>
36 #include <linux/blkdev.h>
37 #include <linux/sysctl.h>
38 #include <linux/seq_file.h>
39 #include <linux/fs.h>
40 #include <linux/poll.h>
41 #include <linux/ctype.h>
42 #include <linux/string.h>
43 #include <linux/hdreg.h>
44 #include <linux/proc_fs.h>
45 #include <linux/random.h>
46 #include <linux/module.h>
47 #include <linux/reboot.h>
48 #include <linux/file.h>
49 #include <linux/compat.h>
50 #include <linux/delay.h>
51 #include <linux/raid/md_p.h>
52 #include <linux/raid/md_u.h>
53 #include <linux/slab.h>
54 #include "md.h"
55 #include "bitmap.h"
56
57 #ifndef MODULE
58 static void autostart_arrays(int part);
59 #endif
60
61 /* pers_list is a list of registered personalities protected
62  * by pers_lock.
63  * pers_lock does extra service to protect accesses to
64  * mddev->thread when the mutex cannot be held.
65  */
66 static LIST_HEAD(pers_list);
67 static DEFINE_SPINLOCK(pers_lock);
68
69 static void md_print_devices(void);
70
71 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
72 static struct workqueue_struct *md_wq;
73 static struct workqueue_struct *md_misc_wq;
74
75 static int remove_and_add_spares(struct mddev *mddev,
76                                  struct md_rdev *this);
77
78 #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
79
80 /*
81  * Default number of read corrections we'll attempt on an rdev
82  * before ejecting it from the array. We divide the read error
83  * count by 2 for every hour elapsed between read errors.
84  */
85 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
86 /*
87  * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
88  * is 1000 KB/sec, so the extra system load does not show up that much.
89  * Increase it if you want to have more _guaranteed_ speed. Note that
90  * the RAID driver will use the maximum available bandwidth if the IO
91  * subsystem is idle. There is also an 'absolute maximum' reconstruction
92  * speed limit - in case reconstruction slows down your system despite
93  * idle IO detection.
94  *
95  * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
96  * or /sys/block/mdX/md/sync_speed_{min,max}
97  */
98
99 static int sysctl_speed_limit_min = 1000;
100 static int sysctl_speed_limit_max = 200000;
101 static inline int speed_min(struct mddev *mddev)
102 {
103         return mddev->sync_speed_min ?
104                 mddev->sync_speed_min : sysctl_speed_limit_min;
105 }
106
107 static inline int speed_max(struct mddev *mddev)
108 {
109         return mddev->sync_speed_max ?
110                 mddev->sync_speed_max : sysctl_speed_limit_max;
111 }
112
113 static struct ctl_table_header *raid_table_header;
114
115 static struct ctl_table raid_table[] = {
116         {
117                 .procname       = "speed_limit_min",
118                 .data           = &sysctl_speed_limit_min,
119                 .maxlen         = sizeof(int),
120                 .mode           = S_IRUGO|S_IWUSR,
121                 .proc_handler   = proc_dointvec,
122         },
123         {
124                 .procname       = "speed_limit_max",
125                 .data           = &sysctl_speed_limit_max,
126                 .maxlen         = sizeof(int),
127                 .mode           = S_IRUGO|S_IWUSR,
128                 .proc_handler   = proc_dointvec,
129         },
130         { }
131 };
132
133 static struct ctl_table raid_dir_table[] = {
134         {
135                 .procname       = "raid",
136                 .maxlen         = 0,
137                 .mode           = S_IRUGO|S_IXUGO,
138                 .child          = raid_table,
139         },
140         { }
141 };
142
143 static struct ctl_table raid_root_table[] = {
144         {
145                 .procname       = "dev",
146                 .maxlen         = 0,
147                 .mode           = 0555,
148                 .child          = raid_dir_table,
149         },
150         {  }
151 };
152
153 static const struct block_device_operations md_fops;
154
155 static int start_readonly;
156
157 /* bio_clone_mddev
158  * like bio_clone, but with a local bio set
159  */
160
161 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
162                             struct mddev *mddev)
163 {
164         struct bio *b;
165
166         if (!mddev || !mddev->bio_set)
167                 return bio_alloc(gfp_mask, nr_iovecs);
168
169         b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
170         if (!b)
171                 return NULL;
172         return b;
173 }
174 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
175
176 struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
177                             struct mddev *mddev)
178 {
179         if (!mddev || !mddev->bio_set)
180                 return bio_clone(bio, gfp_mask);
181
182         return bio_clone_bioset(bio, gfp_mask, mddev->bio_set);
183 }
184 EXPORT_SYMBOL_GPL(bio_clone_mddev);
185
186 /*
187  * We have a system wide 'event count' that is incremented
188  * on any 'interesting' event, and readers of /proc/mdstat
189  * can use 'poll' or 'select' to find out when the event
190  * count increases.
191  *
192  * Events are:
193  *  start array, stop array, error, add device, remove device,
194  *  start build, activate spare
195  */
196 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
197 static atomic_t md_event_count;
198 void md_new_event(struct mddev *mddev)
199 {
200         atomic_inc(&md_event_count);
201         wake_up(&md_event_waiters);
202 }
203 EXPORT_SYMBOL_GPL(md_new_event);
204
205 /* Alternate version that can be called from interrupts
206  * when calling sysfs_notify isn't needed.
207  */
208 static void md_new_event_inintr(struct mddev *mddev)
209 {
210         atomic_inc(&md_event_count);
211         wake_up(&md_event_waiters);
212 }
213
214 /*
215  * Enables to iterate over all existing md arrays
216  * all_mddevs_lock protects this list.
217  */
218 static LIST_HEAD(all_mddevs);
219 static DEFINE_SPINLOCK(all_mddevs_lock);
220
221
222 /*
223  * iterates through all used mddevs in the system.
224  * We take care to grab the all_mddevs_lock whenever navigating
225  * the list, and to always hold a refcount when unlocked.
226  * Any code which breaks out of this loop while own
227  * a reference to the current mddev and must mddev_put it.
228  */
229 #define for_each_mddev(_mddev,_tmp)                                     \
230                                                                         \
231         for (({ spin_lock(&all_mddevs_lock);                            \
232                 _tmp = all_mddevs.next;                                 \
233                 _mddev = NULL;});                                       \
234              ({ if (_tmp != &all_mddevs)                                \
235                         mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
236                 spin_unlock(&all_mddevs_lock);                          \
237                 if (_mddev) mddev_put(_mddev);                          \
238                 _mddev = list_entry(_tmp, struct mddev, all_mddevs);    \
239                 _tmp != &all_mddevs;});                                 \
240              ({ spin_lock(&all_mddevs_lock);                            \
241                 _tmp = _tmp->next;})                                    \
242                 )
243
244
245 /* Rather than calling directly into the personality make_request function,
246  * IO requests come here first so that we can check if the device is
247  * being suspended pending a reconfiguration.
248  * We hold a refcount over the call to ->make_request.  By the time that
249  * call has finished, the bio has been linked into some internal structure
250  * and so is visible to ->quiesce(), so we don't need the refcount any more.
251  */
252 static void md_make_request(struct request_queue *q, struct bio *bio)
253 {
254         const int rw = bio_data_dir(bio);
255         struct mddev *mddev = q->queuedata;
256         int cpu;
257         unsigned int sectors;
258
259         if (mddev == NULL || mddev->pers == NULL
260             || !mddev->ready) {
261                 bio_io_error(bio);
262                 return;
263         }
264         if (mddev->ro == 1 && unlikely(rw == WRITE)) {
265                 bio_endio(bio, bio_sectors(bio) == 0 ? 0 : -EROFS);
266                 return;
267         }
268         smp_rmb(); /* Ensure implications of  'active' are visible */
269         rcu_read_lock();
270         if (mddev->suspended) {
271                 DEFINE_WAIT(__wait);
272                 for (;;) {
273                         prepare_to_wait(&mddev->sb_wait, &__wait,
274                                         TASK_UNINTERRUPTIBLE);
275                         if (!mddev->suspended)
276                                 break;
277                         rcu_read_unlock();
278                         schedule();
279                         rcu_read_lock();
280                 }
281                 finish_wait(&mddev->sb_wait, &__wait);
282         }
283         atomic_inc(&mddev->active_io);
284         rcu_read_unlock();
285
286         /*
287          * save the sectors now since our bio can
288          * go away inside make_request
289          */
290         sectors = bio_sectors(bio);
291         mddev->pers->make_request(mddev, bio);
292
293         cpu = part_stat_lock();
294         part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
295         part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
296         part_stat_unlock();
297
298         if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
299                 wake_up(&mddev->sb_wait);
300 }
301
302 /* mddev_suspend makes sure no new requests are submitted
303  * to the device, and that any requests that have been submitted
304  * are completely handled.
305  * Once ->stop is called and completes, the module will be completely
306  * unused.
307  */
308 void mddev_suspend(struct mddev *mddev)
309 {
310         BUG_ON(mddev->suspended);
311         mddev->suspended = 1;
312         synchronize_rcu();
313         wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
314         mddev->pers->quiesce(mddev, 1);
315
316         del_timer_sync(&mddev->safemode_timer);
317 }
318 EXPORT_SYMBOL_GPL(mddev_suspend);
319
320 void mddev_resume(struct mddev *mddev)
321 {
322         mddev->suspended = 0;
323         wake_up(&mddev->sb_wait);
324         mddev->pers->quiesce(mddev, 0);
325
326         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
327         md_wakeup_thread(mddev->thread);
328         md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
329 }
330 EXPORT_SYMBOL_GPL(mddev_resume);
331
332 int mddev_congested(struct mddev *mddev, int bits)
333 {
334         return mddev->suspended;
335 }
336 EXPORT_SYMBOL(mddev_congested);
337
338 /*
339  * Generic flush handling for md
340  */
341
342 static void md_end_flush(struct bio *bio, int err)
343 {
344         struct md_rdev *rdev = bio->bi_private;
345         struct mddev *mddev = rdev->mddev;
346
347         rdev_dec_pending(rdev, mddev);
348
349         if (atomic_dec_and_test(&mddev->flush_pending)) {
350                 /* The pre-request flush has finished */
351                 queue_work(md_wq, &mddev->flush_work);
352         }
353         bio_put(bio);
354 }
355
356 static void md_submit_flush_data(struct work_struct *ws);
357
358 static void submit_flushes(struct work_struct *ws)
359 {
360         struct mddev *mddev = container_of(ws, struct mddev, flush_work);
361         struct md_rdev *rdev;
362
363         INIT_WORK(&mddev->flush_work, md_submit_flush_data);
364         atomic_set(&mddev->flush_pending, 1);
365         rcu_read_lock();
366         rdev_for_each_rcu(rdev, mddev)
367                 if (rdev->raid_disk >= 0 &&
368                     !test_bit(Faulty, &rdev->flags)) {
369                         /* Take two references, one is dropped
370                          * when request finishes, one after
371                          * we reclaim rcu_read_lock
372                          */
373                         struct bio *bi;
374                         atomic_inc(&rdev->nr_pending);
375                         atomic_inc(&rdev->nr_pending);
376                         rcu_read_unlock();
377                         bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
378                         bi->bi_end_io = md_end_flush;
379                         bi->bi_private = rdev;
380                         bi->bi_bdev = rdev->bdev;
381                         atomic_inc(&mddev->flush_pending);
382                         submit_bio(WRITE_FLUSH, bi);
383                         rcu_read_lock();
384                         rdev_dec_pending(rdev, mddev);
385                 }
386         rcu_read_unlock();
387         if (atomic_dec_and_test(&mddev->flush_pending))
388                 queue_work(md_wq, &mddev->flush_work);
389 }
390
391 static void md_submit_flush_data(struct work_struct *ws)
392 {
393         struct mddev *mddev = container_of(ws, struct mddev, flush_work);
394         struct bio *bio = mddev->flush_bio;
395
396         if (bio->bi_size == 0)
397                 /* an empty barrier - all done */
398                 bio_endio(bio, 0);
399         else {
400                 bio->bi_rw &= ~REQ_FLUSH;
401                 mddev->pers->make_request(mddev, bio);
402         }
403
404         mddev->flush_bio = NULL;
405         wake_up(&mddev->sb_wait);
406 }
407
408 void md_flush_request(struct mddev *mddev, struct bio *bio)
409 {
410         spin_lock_irq(&mddev->write_lock);
411         wait_event_lock_irq(mddev->sb_wait,
412                             !mddev->flush_bio,
413                             mddev->write_lock);
414         mddev->flush_bio = bio;
415         spin_unlock_irq(&mddev->write_lock);
416
417         INIT_WORK(&mddev->flush_work, submit_flushes);
418         queue_work(md_wq, &mddev->flush_work);
419 }
420 EXPORT_SYMBOL(md_flush_request);
421
422 void md_unplug(struct blk_plug_cb *cb, bool from_schedule)
423 {
424         struct mddev *mddev = cb->data;
425         md_wakeup_thread(mddev->thread);
426         kfree(cb);
427 }
428 EXPORT_SYMBOL(md_unplug);
429
430 static inline struct mddev *mddev_get(struct mddev *mddev)
431 {
432         atomic_inc(&mddev->active);
433         return mddev;
434 }
435
436 static void mddev_delayed_delete(struct work_struct *ws);
437
438 static void mddev_put(struct mddev *mddev)
439 {
440         struct bio_set *bs = NULL;
441
442         if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
443                 return;
444         if (!mddev->raid_disks && list_empty(&mddev->disks) &&
445             mddev->ctime == 0 && !mddev->hold_active) {
446                 /* Array is not configured at all, and not held active,
447                  * so destroy it */
448                 list_del_init(&mddev->all_mddevs);
449                 bs = mddev->bio_set;
450                 mddev->bio_set = NULL;
451                 if (mddev->gendisk) {
452                         /* We did a probe so need to clean up.  Call
453                          * queue_work inside the spinlock so that
454                          * flush_workqueue() after mddev_find will
455                          * succeed in waiting for the work to be done.
456                          */
457                         INIT_WORK(&mddev->del_work, mddev_delayed_delete);
458                         queue_work(md_misc_wq, &mddev->del_work);
459                 } else
460                         kfree(mddev);
461         }
462         spin_unlock(&all_mddevs_lock);
463         if (bs)
464                 bioset_free(bs);
465 }
466
467 void mddev_init(struct mddev *mddev)
468 {
469         mutex_init(&mddev->open_mutex);
470         mutex_init(&mddev->reconfig_mutex);
471         mutex_init(&mddev->bitmap_info.mutex);
472         INIT_LIST_HEAD(&mddev->disks);
473         INIT_LIST_HEAD(&mddev->all_mddevs);
474         init_timer(&mddev->safemode_timer);
475         atomic_set(&mddev->active, 1);
476         atomic_set(&mddev->openers, 0);
477         atomic_set(&mddev->active_io, 0);
478         spin_lock_init(&mddev->write_lock);
479         atomic_set(&mddev->flush_pending, 0);
480         init_waitqueue_head(&mddev->sb_wait);
481         init_waitqueue_head(&mddev->recovery_wait);
482         mddev->reshape_position = MaxSector;
483         mddev->reshape_backwards = 0;
484         mddev->last_sync_action = "none";
485         mddev->resync_min = 0;
486         mddev->resync_max = MaxSector;
487         mddev->level = LEVEL_NONE;
488 }
489 EXPORT_SYMBOL_GPL(mddev_init);
490
491 static struct mddev * mddev_find(dev_t unit)
492 {
493         struct mddev *mddev, *new = NULL;
494
495         if (unit && MAJOR(unit) != MD_MAJOR)
496                 unit &= ~((1<<MdpMinorShift)-1);
497
498  retry:
499         spin_lock(&all_mddevs_lock);
500
501         if (unit) {
502                 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
503                         if (mddev->unit == unit) {
504                                 mddev_get(mddev);
505                                 spin_unlock(&all_mddevs_lock);
506                                 kfree(new);
507                                 return mddev;
508                         }
509
510                 if (new) {
511                         list_add(&new->all_mddevs, &all_mddevs);
512                         spin_unlock(&all_mddevs_lock);
513                         new->hold_active = UNTIL_IOCTL;
514                         return new;
515                 }
516         } else if (new) {
517                 /* find an unused unit number */
518                 static int next_minor = 512;
519                 int start = next_minor;
520                 int is_free = 0;
521                 int dev = 0;
522                 while (!is_free) {
523                         dev = MKDEV(MD_MAJOR, next_minor);
524                         next_minor++;
525                         if (next_minor > MINORMASK)
526                                 next_minor = 0;
527                         if (next_minor == start) {
528                                 /* Oh dear, all in use. */
529                                 spin_unlock(&all_mddevs_lock);
530                                 kfree(new);
531                                 return NULL;
532                         }
533                                 
534                         is_free = 1;
535                         list_for_each_entry(mddev, &all_mddevs, all_mddevs)
536                                 if (mddev->unit == dev) {
537                                         is_free = 0;
538                                         break;
539                                 }
540                 }
541                 new->unit = dev;
542                 new->md_minor = MINOR(dev);
543                 new->hold_active = UNTIL_STOP;
544                 list_add(&new->all_mddevs, &all_mddevs);
545                 spin_unlock(&all_mddevs_lock);
546                 return new;
547         }
548         spin_unlock(&all_mddevs_lock);
549
550         new = kzalloc(sizeof(*new), GFP_KERNEL);
551         if (!new)
552                 return NULL;
553
554         new->unit = unit;
555         if (MAJOR(unit) == MD_MAJOR)
556                 new->md_minor = MINOR(unit);
557         else
558                 new->md_minor = MINOR(unit) >> MdpMinorShift;
559
560         mddev_init(new);
561
562         goto retry;
563 }
564
565 static inline int __must_check mddev_lock(struct mddev * mddev)
566 {
567         return mutex_lock_interruptible(&mddev->reconfig_mutex);
568 }
569
570 /* Sometimes we need to take the lock in a situation where
571  * failure due to interrupts is not acceptable.
572  */
573 static inline void mddev_lock_nointr(struct mddev * mddev)
574 {
575         mutex_lock(&mddev->reconfig_mutex);
576 }
577
578 static inline int mddev_is_locked(struct mddev *mddev)
579 {
580         return mutex_is_locked(&mddev->reconfig_mutex);
581 }
582
583 static inline int mddev_trylock(struct mddev * mddev)
584 {
585         return mutex_trylock(&mddev->reconfig_mutex);
586 }
587
588 static struct attribute_group md_redundancy_group;
589
590 static void mddev_unlock(struct mddev * mddev)
591 {
592         if (mddev->to_remove) {
593                 /* These cannot be removed under reconfig_mutex as
594                  * an access to the files will try to take reconfig_mutex
595                  * while holding the file unremovable, which leads to
596                  * a deadlock.
597                  * So hold set sysfs_active while the remove in happeing,
598                  * and anything else which might set ->to_remove or my
599                  * otherwise change the sysfs namespace will fail with
600                  * -EBUSY if sysfs_active is still set.
601                  * We set sysfs_active under reconfig_mutex and elsewhere
602                  * test it under the same mutex to ensure its correct value
603                  * is seen.
604                  */
605                 struct attribute_group *to_remove = mddev->to_remove;
606                 mddev->to_remove = NULL;
607                 mddev->sysfs_active = 1;
608                 mutex_unlock(&mddev->reconfig_mutex);
609
610                 if (mddev->kobj.sd) {
611                         if (to_remove != &md_redundancy_group)
612                                 sysfs_remove_group(&mddev->kobj, to_remove);
613                         if (mddev->pers == NULL ||
614                             mddev->pers->sync_request == NULL) {
615                                 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
616                                 if (mddev->sysfs_action)
617                                         sysfs_put(mddev->sysfs_action);
618                                 mddev->sysfs_action = NULL;
619                         }
620                 }
621                 mddev->sysfs_active = 0;
622         } else
623                 mutex_unlock(&mddev->reconfig_mutex);
624
625         /* As we've dropped the mutex we need a spinlock to
626          * make sure the thread doesn't disappear
627          */
628         spin_lock(&pers_lock);
629         md_wakeup_thread(mddev->thread);
630         spin_unlock(&pers_lock);
631 }
632
633 static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
634 {
635         struct md_rdev *rdev;
636
637         rdev_for_each(rdev, mddev)
638                 if (rdev->desc_nr == nr)
639                         return rdev;
640
641         return NULL;
642 }
643
644 static struct md_rdev *find_rdev_nr_rcu(struct mddev *mddev, int nr)
645 {
646         struct md_rdev *rdev;
647
648         rdev_for_each_rcu(rdev, mddev)
649                 if (rdev->desc_nr == nr)
650                         return rdev;
651
652         return NULL;
653 }
654
655 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
656 {
657         struct md_rdev *rdev;
658
659         rdev_for_each(rdev, mddev)
660                 if (rdev->bdev->bd_dev == dev)
661                         return rdev;
662
663         return NULL;
664 }
665
666 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
667 {
668         struct md_rdev *rdev;
669
670         rdev_for_each_rcu(rdev, mddev)
671                 if (rdev->bdev->bd_dev == dev)
672                         return rdev;
673
674         return NULL;
675 }
676
677 static struct md_personality *find_pers(int level, char *clevel)
678 {
679         struct md_personality *pers;
680         list_for_each_entry(pers, &pers_list, list) {
681                 if (level != LEVEL_NONE && pers->level == level)
682                         return pers;
683                 if (strcmp(pers->name, clevel)==0)
684                         return pers;
685         }
686         return NULL;
687 }
688
689 /* return the offset of the super block in 512byte sectors */
690 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
691 {
692         sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
693         return MD_NEW_SIZE_SECTORS(num_sectors);
694 }
695
696 static int alloc_disk_sb(struct md_rdev * rdev)
697 {
698         if (rdev->sb_page)
699                 MD_BUG();
700
701         rdev->sb_page = alloc_page(GFP_KERNEL);
702         if (!rdev->sb_page) {
703                 printk(KERN_ALERT "md: out of memory.\n");
704                 return -ENOMEM;
705         }
706
707         return 0;
708 }
709
710 void md_rdev_clear(struct md_rdev *rdev)
711 {
712         if (rdev->sb_page) {
713                 put_page(rdev->sb_page);
714                 rdev->sb_loaded = 0;
715                 rdev->sb_page = NULL;
716                 rdev->sb_start = 0;
717                 rdev->sectors = 0;
718         }
719         if (rdev->bb_page) {
720                 put_page(rdev->bb_page);
721                 rdev->bb_page = NULL;
722         }
723         kfree(rdev->badblocks.page);
724         rdev->badblocks.page = NULL;
725 }
726 EXPORT_SYMBOL_GPL(md_rdev_clear);
727
728 static void super_written(struct bio *bio, int error)
729 {
730         struct md_rdev *rdev = bio->bi_private;
731         struct mddev *mddev = rdev->mddev;
732
733         if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
734                 printk("md: super_written gets error=%d, uptodate=%d\n",
735                        error, test_bit(BIO_UPTODATE, &bio->bi_flags));
736                 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
737                 md_error(mddev, rdev);
738         }
739
740         if (atomic_dec_and_test(&mddev->pending_writes))
741                 wake_up(&mddev->sb_wait);
742         bio_put(bio);
743 }
744
745 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
746                    sector_t sector, int size, struct page *page)
747 {
748         /* write first size bytes of page to sector of rdev
749          * Increment mddev->pending_writes before returning
750          * and decrement it on completion, waking up sb_wait
751          * if zero is reached.
752          * If an error occurred, call md_error
753          */
754         struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
755
756         bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
757         bio->bi_sector = sector;
758         bio_add_page(bio, page, size, 0);
759         bio->bi_private = rdev;
760         bio->bi_end_io = super_written;
761
762         atomic_inc(&mddev->pending_writes);
763         submit_bio(WRITE_FLUSH_FUA, bio);
764 }
765
766 void md_super_wait(struct mddev *mddev)
767 {
768         /* wait for all superblock writes that were scheduled to complete */
769         DEFINE_WAIT(wq);
770         for(;;) {
771                 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
772                 if (atomic_read(&mddev->pending_writes)==0)
773                         break;
774                 schedule();
775         }
776         finish_wait(&mddev->sb_wait, &wq);
777 }
778
779 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
780                  struct page *page, int rw, bool metadata_op)
781 {
782         struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
783         int ret;
784
785         rw |= REQ_SYNC;
786
787         bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
788                 rdev->meta_bdev : rdev->bdev;
789         if (metadata_op)
790                 bio->bi_sector = sector + rdev->sb_start;
791         else if (rdev->mddev->reshape_position != MaxSector &&
792                  (rdev->mddev->reshape_backwards ==
793                   (sector >= rdev->mddev->reshape_position)))
794                 bio->bi_sector = sector + rdev->new_data_offset;
795         else
796                 bio->bi_sector = sector + rdev->data_offset;
797         bio_add_page(bio, page, size, 0);
798         submit_bio_wait(rw, bio);
799
800         ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
801         bio_put(bio);
802         return ret;
803 }
804 EXPORT_SYMBOL_GPL(sync_page_io);
805
806 static int read_disk_sb(struct md_rdev * rdev, int size)
807 {
808         char b[BDEVNAME_SIZE];
809         if (!rdev->sb_page) {
810                 MD_BUG();
811                 return -EINVAL;
812         }
813         if (rdev->sb_loaded)
814                 return 0;
815
816
817         if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
818                 goto fail;
819         rdev->sb_loaded = 1;
820         return 0;
821
822 fail:
823         printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
824                 bdevname(rdev->bdev,b));
825         return -EINVAL;
826 }
827
828 static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
829 {
830         return  sb1->set_uuid0 == sb2->set_uuid0 &&
831                 sb1->set_uuid1 == sb2->set_uuid1 &&
832                 sb1->set_uuid2 == sb2->set_uuid2 &&
833                 sb1->set_uuid3 == sb2->set_uuid3;
834 }
835
836 static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
837 {
838         int ret;
839         mdp_super_t *tmp1, *tmp2;
840
841         tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
842         tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
843
844         if (!tmp1 || !tmp2) {
845                 ret = 0;
846                 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
847                 goto abort;
848         }
849
850         *tmp1 = *sb1;
851         *tmp2 = *sb2;
852
853         /*
854          * nr_disks is not constant
855          */
856         tmp1->nr_disks = 0;
857         tmp2->nr_disks = 0;
858
859         ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
860 abort:
861         kfree(tmp1);
862         kfree(tmp2);
863         return ret;
864 }
865
866
867 static u32 md_csum_fold(u32 csum)
868 {
869         csum = (csum & 0xffff) + (csum >> 16);
870         return (csum & 0xffff) + (csum >> 16);
871 }
872
873 static unsigned int calc_sb_csum(mdp_super_t * sb)
874 {
875         u64 newcsum = 0;
876         u32 *sb32 = (u32*)sb;
877         int i;
878         unsigned int disk_csum, csum;
879
880         disk_csum = sb->sb_csum;
881         sb->sb_csum = 0;
882
883         for (i = 0; i < MD_SB_BYTES/4 ; i++)
884                 newcsum += sb32[i];
885         csum = (newcsum & 0xffffffff) + (newcsum>>32);
886
887
888 #ifdef CONFIG_ALPHA
889         /* This used to use csum_partial, which was wrong for several
890          * reasons including that different results are returned on
891          * different architectures.  It isn't critical that we get exactly
892          * the same return value as before (we always csum_fold before
893          * testing, and that removes any differences).  However as we
894          * know that csum_partial always returned a 16bit value on
895          * alphas, do a fold to maximise conformity to previous behaviour.
896          */
897         sb->sb_csum = md_csum_fold(disk_csum);
898 #else
899         sb->sb_csum = disk_csum;
900 #endif
901         return csum;
902 }
903
904
905 /*
906  * Handle superblock details.
907  * We want to be able to handle multiple superblock formats
908  * so we have a common interface to them all, and an array of
909  * different handlers.
910  * We rely on user-space to write the initial superblock, and support
911  * reading and updating of superblocks.
912  * Interface methods are:
913  *   int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
914  *      loads and validates a superblock on dev.
915  *      if refdev != NULL, compare superblocks on both devices
916  *    Return:
917  *      0 - dev has a superblock that is compatible with refdev
918  *      1 - dev has a superblock that is compatible and newer than refdev
919  *          so dev should be used as the refdev in future
920  *     -EINVAL superblock incompatible or invalid
921  *     -othererror e.g. -EIO
922  *
923  *   int validate_super(struct mddev *mddev, struct md_rdev *dev)
924  *      Verify that dev is acceptable into mddev.
925  *       The first time, mddev->raid_disks will be 0, and data from
926  *       dev should be merged in.  Subsequent calls check that dev
927  *       is new enough.  Return 0 or -EINVAL
928  *
929  *   void sync_super(struct mddev *mddev, struct md_rdev *dev)
930  *     Update the superblock for rdev with data in mddev
931  *     This does not write to disc.
932  *
933  */
934
935 struct super_type  {
936         char                *name;
937         struct module       *owner;
938         int                 (*load_super)(struct md_rdev *rdev,
939                                           struct md_rdev *refdev,
940                                           int minor_version);
941         int                 (*validate_super)(struct mddev *mddev,
942                                               struct md_rdev *rdev);
943         void                (*sync_super)(struct mddev *mddev,
944                                           struct md_rdev *rdev);
945         unsigned long long  (*rdev_size_change)(struct md_rdev *rdev,
946                                                 sector_t num_sectors);
947         int                 (*allow_new_offset)(struct md_rdev *rdev,
948                                                 unsigned long long new_offset);
949 };
950
951 /*
952  * Check that the given mddev has no bitmap.
953  *
954  * This function is called from the run method of all personalities that do not
955  * support bitmaps. It prints an error message and returns non-zero if mddev
956  * has a bitmap. Otherwise, it returns 0.
957  *
958  */
959 int md_check_no_bitmap(struct mddev *mddev)
960 {
961         if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
962                 return 0;
963         printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
964                 mdname(mddev), mddev->pers->name);
965         return 1;
966 }
967 EXPORT_SYMBOL(md_check_no_bitmap);
968
969 /*
970  * load_super for 0.90.0 
971  */
972 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
973 {
974         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
975         mdp_super_t *sb;
976         int ret;
977
978         /*
979          * Calculate the position of the superblock (512byte sectors),
980          * it's at the end of the disk.
981          *
982          * It also happens to be a multiple of 4Kb.
983          */
984         rdev->sb_start = calc_dev_sboffset(rdev);
985
986         ret = read_disk_sb(rdev, MD_SB_BYTES);
987         if (ret) return ret;
988
989         ret = -EINVAL;
990
991         bdevname(rdev->bdev, b);
992         sb = page_address(rdev->sb_page);
993
994         if (sb->md_magic != MD_SB_MAGIC) {
995                 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
996                        b);
997                 goto abort;
998         }
999
1000         if (sb->major_version != 0 ||
1001             sb->minor_version < 90 ||
1002             sb->minor_version > 91) {
1003                 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1004                         sb->major_version, sb->minor_version,
1005                         b);
1006                 goto abort;
1007         }
1008
1009         if (sb->raid_disks <= 0)
1010                 goto abort;
1011
1012         if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1013                 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1014                         b);
1015                 goto abort;
1016         }
1017
1018         rdev->preferred_minor = sb->md_minor;
1019         rdev->data_offset = 0;
1020         rdev->new_data_offset = 0;
1021         rdev->sb_size = MD_SB_BYTES;
1022         rdev->badblocks.shift = -1;
1023
1024         if (sb->level == LEVEL_MULTIPATH)
1025                 rdev->desc_nr = -1;
1026         else
1027                 rdev->desc_nr = sb->this_disk.number;
1028
1029         if (!refdev) {
1030                 ret = 1;
1031         } else {
1032                 __u64 ev1, ev2;
1033                 mdp_super_t *refsb = page_address(refdev->sb_page);
1034                 if (!uuid_equal(refsb, sb)) {
1035                         printk(KERN_WARNING "md: %s has different UUID to %s\n",
1036                                 b, bdevname(refdev->bdev,b2));
1037                         goto abort;
1038                 }
1039                 if (!sb_equal(refsb, sb)) {
1040                         printk(KERN_WARNING "md: %s has same UUID"
1041                                " but different superblock to %s\n",
1042                                b, bdevname(refdev->bdev, b2));
1043                         goto abort;
1044                 }
1045                 ev1 = md_event(sb);
1046                 ev2 = md_event(refsb);
1047                 if (ev1 > ev2)
1048                         ret = 1;
1049                 else 
1050                         ret = 0;
1051         }
1052         rdev->sectors = rdev->sb_start;
1053         /* Limit to 4TB as metadata cannot record more than that.
1054          * (not needed for Linear and RAID0 as metadata doesn't
1055          * record this size)
1056          */
1057         if (rdev->sectors >= (2ULL << 32) && sb->level >= 1)
1058                 rdev->sectors = (2ULL << 32) - 2;
1059
1060         if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1061                 /* "this cannot possibly happen" ... */
1062                 ret = -EINVAL;
1063
1064  abort:
1065         return ret;
1066 }
1067
1068 /*
1069  * validate_super for 0.90.0
1070  */
1071 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1072 {
1073         mdp_disk_t *desc;
1074         mdp_super_t *sb = page_address(rdev->sb_page);
1075         __u64 ev1 = md_event(sb);
1076
1077         rdev->raid_disk = -1;
1078         clear_bit(Faulty, &rdev->flags);
1079         clear_bit(In_sync, &rdev->flags);
1080         clear_bit(Bitmap_sync, &rdev->flags);
1081         clear_bit(WriteMostly, &rdev->flags);
1082
1083         if (mddev->raid_disks == 0) {
1084                 mddev->major_version = 0;
1085                 mddev->minor_version = sb->minor_version;
1086                 mddev->patch_version = sb->patch_version;
1087                 mddev->external = 0;
1088                 mddev->chunk_sectors = sb->chunk_size >> 9;
1089                 mddev->ctime = sb->ctime;
1090                 mddev->utime = sb->utime;
1091                 mddev->level = sb->level;
1092                 mddev->clevel[0] = 0;
1093                 mddev->layout = sb->layout;
1094                 mddev->raid_disks = sb->raid_disks;
1095                 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1096                 mddev->events = ev1;
1097                 mddev->bitmap_info.offset = 0;
1098                 mddev->bitmap_info.space = 0;
1099                 /* bitmap can use 60 K after the 4K superblocks */
1100                 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1101                 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1102                 mddev->reshape_backwards = 0;
1103
1104                 if (mddev->minor_version >= 91) {
1105                         mddev->reshape_position = sb->reshape_position;
1106                         mddev->delta_disks = sb->delta_disks;
1107                         mddev->new_level = sb->new_level;
1108                         mddev->new_layout = sb->new_layout;
1109                         mddev->new_chunk_sectors = sb->new_chunk >> 9;
1110                         if (mddev->delta_disks < 0)
1111                                 mddev->reshape_backwards = 1;
1112                 } else {
1113                         mddev->reshape_position = MaxSector;
1114                         mddev->delta_disks = 0;
1115                         mddev->new_level = mddev->level;
1116                         mddev->new_layout = mddev->layout;
1117                         mddev->new_chunk_sectors = mddev->chunk_sectors;
1118                 }
1119
1120                 if (sb->state & (1<<MD_SB_CLEAN))
1121                         mddev->recovery_cp = MaxSector;
1122                 else {
1123                         if (sb->events_hi == sb->cp_events_hi && 
1124                                 sb->events_lo == sb->cp_events_lo) {
1125                                 mddev->recovery_cp = sb->recovery_cp;
1126                         } else
1127                                 mddev->recovery_cp = 0;
1128                 }
1129
1130                 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1131                 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1132                 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1133                 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1134
1135                 mddev->max_disks = MD_SB_DISKS;
1136
1137                 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1138                     mddev->bitmap_info.file == NULL) {
1139                         mddev->bitmap_info.offset =
1140                                 mddev->bitmap_info.default_offset;
1141                         mddev->bitmap_info.space =
1142                                 mddev->bitmap_info.default_space;
1143                 }
1144
1145         } else if (mddev->pers == NULL) {
1146                 /* Insist on good event counter while assembling, except
1147                  * for spares (which don't need an event count) */
1148                 ++ev1;
1149                 if (sb->disks[rdev->desc_nr].state & (
1150                             (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1151                         if (ev1 < mddev->events) 
1152                                 return -EINVAL;
1153         } else if (mddev->bitmap) {
1154                 /* if adding to array with a bitmap, then we can accept an
1155                  * older device ... but not too old.
1156                  */
1157                 if (ev1 < mddev->bitmap->events_cleared)
1158                         return 0;
1159                 if (ev1 < mddev->events)
1160                         set_bit(Bitmap_sync, &rdev->flags);
1161         } else {
1162                 if (ev1 < mddev->events)
1163                         /* just a hot-add of a new device, leave raid_disk at -1 */
1164                         return 0;
1165         }
1166
1167         if (mddev->level != LEVEL_MULTIPATH) {
1168                 desc = sb->disks + rdev->desc_nr;
1169
1170                 if (desc->state & (1<<MD_DISK_FAULTY))
1171                         set_bit(Faulty, &rdev->flags);
1172                 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1173                             desc->raid_disk < mddev->raid_disks */) {
1174                         set_bit(In_sync, &rdev->flags);
1175                         rdev->raid_disk = desc->raid_disk;
1176                 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1177                         /* active but not in sync implies recovery up to
1178                          * reshape position.  We don't know exactly where
1179                          * that is, so set to zero for now */
1180                         if (mddev->minor_version >= 91) {
1181                                 rdev->recovery_offset = 0;
1182                                 rdev->raid_disk = desc->raid_disk;
1183                         }
1184                 }
1185                 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1186                         set_bit(WriteMostly, &rdev->flags);
1187         } else /* MULTIPATH are always insync */
1188                 set_bit(In_sync, &rdev->flags);
1189         return 0;
1190 }
1191
1192 /*
1193  * sync_super for 0.90.0
1194  */
1195 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1196 {
1197         mdp_super_t *sb;
1198         struct md_rdev *rdev2;
1199         int next_spare = mddev->raid_disks;
1200
1201
1202         /* make rdev->sb match mddev data..
1203          *
1204          * 1/ zero out disks
1205          * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1206          * 3/ any empty disks < next_spare become removed
1207          *
1208          * disks[0] gets initialised to REMOVED because
1209          * we cannot be sure from other fields if it has
1210          * been initialised or not.
1211          */
1212         int i;
1213         int active=0, working=0,failed=0,spare=0,nr_disks=0;
1214
1215         rdev->sb_size = MD_SB_BYTES;
1216
1217         sb = page_address(rdev->sb_page);
1218
1219         memset(sb, 0, sizeof(*sb));
1220
1221         sb->md_magic = MD_SB_MAGIC;
1222         sb->major_version = mddev->major_version;
1223         sb->patch_version = mddev->patch_version;
1224         sb->gvalid_words  = 0; /* ignored */
1225         memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1226         memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1227         memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1228         memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1229
1230         sb->ctime = mddev->ctime;
1231         sb->level = mddev->level;
1232         sb->size = mddev->dev_sectors / 2;
1233         sb->raid_disks = mddev->raid_disks;
1234         sb->md_minor = mddev->md_minor;
1235         sb->not_persistent = 0;
1236         sb->utime = mddev->utime;
1237         sb->state = 0;
1238         sb->events_hi = (mddev->events>>32);
1239         sb->events_lo = (u32)mddev->events;
1240
1241         if (mddev->reshape_position == MaxSector)
1242                 sb->minor_version = 90;
1243         else {
1244                 sb->minor_version = 91;
1245                 sb->reshape_position = mddev->reshape_position;
1246                 sb->new_level = mddev->new_level;
1247                 sb->delta_disks = mddev->delta_disks;
1248                 sb->new_layout = mddev->new_layout;
1249                 sb->new_chunk = mddev->new_chunk_sectors << 9;
1250         }
1251         mddev->minor_version = sb->minor_version;
1252         if (mddev->in_sync)
1253         {
1254                 sb->recovery_cp = mddev->recovery_cp;
1255                 sb->cp_events_hi = (mddev->events>>32);
1256                 sb->cp_events_lo = (u32)mddev->events;
1257                 if (mddev->recovery_cp == MaxSector)
1258                         sb->state = (1<< MD_SB_CLEAN);
1259         } else
1260                 sb->recovery_cp = 0;
1261
1262         sb->layout = mddev->layout;
1263         sb->chunk_size = mddev->chunk_sectors << 9;
1264
1265         if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1266                 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1267
1268         sb->disks[0].state = (1<<MD_DISK_REMOVED);
1269         rdev_for_each(rdev2, mddev) {
1270                 mdp_disk_t *d;
1271                 int desc_nr;
1272                 int is_active = test_bit(In_sync, &rdev2->flags);
1273
1274                 if (rdev2->raid_disk >= 0 &&
1275                     sb->minor_version >= 91)
1276                         /* we have nowhere to store the recovery_offset,
1277                          * but if it is not below the reshape_position,
1278                          * we can piggy-back on that.
1279                          */
1280                         is_active = 1;
1281                 if (rdev2->raid_disk < 0 ||
1282                     test_bit(Faulty, &rdev2->flags))
1283                         is_active = 0;
1284                 if (is_active)
1285                         desc_nr = rdev2->raid_disk;
1286                 else
1287                         desc_nr = next_spare++;
1288                 rdev2->desc_nr = desc_nr;
1289                 d = &sb->disks[rdev2->desc_nr];
1290                 nr_disks++;
1291                 d->number = rdev2->desc_nr;
1292                 d->major = MAJOR(rdev2->bdev->bd_dev);
1293                 d->minor = MINOR(rdev2->bdev->bd_dev);
1294                 if (is_active)
1295                         d->raid_disk = rdev2->raid_disk;
1296                 else
1297                         d->raid_disk = rdev2->desc_nr; /* compatibility */
1298                 if (test_bit(Faulty, &rdev2->flags))
1299                         d->state = (1<<MD_DISK_FAULTY);
1300                 else if (is_active) {
1301                         d->state = (1<<MD_DISK_ACTIVE);
1302                         if (test_bit(In_sync, &rdev2->flags))
1303                                 d->state |= (1<<MD_DISK_SYNC);
1304                         active++;
1305                         working++;
1306                 } else {
1307                         d->state = 0;
1308                         spare++;
1309                         working++;
1310                 }
1311                 if (test_bit(WriteMostly, &rdev2->flags))
1312                         d->state |= (1<<MD_DISK_WRITEMOSTLY);
1313         }
1314         /* now set the "removed" and "faulty" bits on any missing devices */
1315         for (i=0 ; i < mddev->raid_disks ; i++) {
1316                 mdp_disk_t *d = &sb->disks[i];
1317                 if (d->state == 0 && d->number == 0) {
1318                         d->number = i;
1319                         d->raid_disk = i;
1320                         d->state = (1<<MD_DISK_REMOVED);
1321                         d->state |= (1<<MD_DISK_FAULTY);
1322                         failed++;
1323                 }
1324         }
1325         sb->nr_disks = nr_disks;
1326         sb->active_disks = active;
1327         sb->working_disks = working;
1328         sb->failed_disks = failed;
1329         sb->spare_disks = spare;
1330
1331         sb->this_disk = sb->disks[rdev->desc_nr];
1332         sb->sb_csum = calc_sb_csum(sb);
1333 }
1334
1335 /*
1336  * rdev_size_change for 0.90.0
1337  */
1338 static unsigned long long
1339 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1340 {
1341         if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1342                 return 0; /* component must fit device */
1343         if (rdev->mddev->bitmap_info.offset)
1344                 return 0; /* can't move bitmap */
1345         rdev->sb_start = calc_dev_sboffset(rdev);
1346         if (!num_sectors || num_sectors > rdev->sb_start)
1347                 num_sectors = rdev->sb_start;
1348         /* Limit to 4TB as metadata cannot record more than that.
1349          * 4TB == 2^32 KB, or 2*2^32 sectors.
1350          */
1351         if (num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1)
1352                 num_sectors = (2ULL << 32) - 2;
1353         md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1354                        rdev->sb_page);
1355         md_super_wait(rdev->mddev);
1356         return num_sectors;
1357 }
1358
1359 static int
1360 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1361 {
1362         /* non-zero offset changes not possible with v0.90 */
1363         return new_offset == 0;
1364 }
1365
1366 /*
1367  * version 1 superblock
1368  */
1369
1370 static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
1371 {
1372         __le32 disk_csum;
1373         u32 csum;
1374         unsigned long long newcsum;
1375         int size = 256 + le32_to_cpu(sb->max_dev)*2;
1376         __le32 *isuper = (__le32*)sb;
1377
1378         disk_csum = sb->sb_csum;
1379         sb->sb_csum = 0;
1380         newcsum = 0;
1381         for (; size >= 4; size -= 4)
1382                 newcsum += le32_to_cpu(*isuper++);
1383
1384         if (size == 2)
1385                 newcsum += le16_to_cpu(*(__le16*) isuper);
1386
1387         csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1388         sb->sb_csum = disk_csum;
1389         return cpu_to_le32(csum);
1390 }
1391
1392 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1393                             int acknowledged);
1394 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1395 {
1396         struct mdp_superblock_1 *sb;
1397         int ret;
1398         sector_t sb_start;
1399         sector_t sectors;
1400         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1401         int bmask;
1402
1403         /*
1404          * Calculate the position of the superblock in 512byte sectors.
1405          * It is always aligned to a 4K boundary and
1406          * depeding on minor_version, it can be:
1407          * 0: At least 8K, but less than 12K, from end of device
1408          * 1: At start of device
1409          * 2: 4K from start of device.
1410          */
1411         switch(minor_version) {
1412         case 0:
1413                 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1414                 sb_start -= 8*2;
1415                 sb_start &= ~(sector_t)(4*2-1);
1416                 break;
1417         case 1:
1418                 sb_start = 0;
1419                 break;
1420         case 2:
1421                 sb_start = 8;
1422                 break;
1423         default:
1424                 return -EINVAL;
1425         }
1426         rdev->sb_start = sb_start;
1427
1428         /* superblock is rarely larger than 1K, but it can be larger,
1429          * and it is safe to read 4k, so we do that
1430          */
1431         ret = read_disk_sb(rdev, 4096);
1432         if (ret) return ret;
1433
1434
1435         sb = page_address(rdev->sb_page);
1436
1437         if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1438             sb->major_version != cpu_to_le32(1) ||
1439             le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1440             le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1441             (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1442                 return -EINVAL;
1443
1444         if (calc_sb_1_csum(sb) != sb->sb_csum) {
1445                 printk("md: invalid superblock checksum on %s\n",
1446                         bdevname(rdev->bdev,b));
1447                 return -EINVAL;
1448         }
1449         if (le64_to_cpu(sb->data_size) < 10) {
1450                 printk("md: data_size too small on %s\n",
1451                        bdevname(rdev->bdev,b));
1452                 return -EINVAL;
1453         }
1454         if (sb->pad0 ||
1455             sb->pad3[0] ||
1456             memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1457                 /* Some padding is non-zero, might be a new feature */
1458                 return -EINVAL;
1459
1460         rdev->preferred_minor = 0xffff;
1461         rdev->data_offset = le64_to_cpu(sb->data_offset);
1462         rdev->new_data_offset = rdev->data_offset;
1463         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1464             (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1465                 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1466         atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1467
1468         rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1469         bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1470         if (rdev->sb_size & bmask)
1471                 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1472
1473         if (minor_version
1474             && rdev->data_offset < sb_start + (rdev->sb_size/512))
1475                 return -EINVAL;
1476         if (minor_version
1477             && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1478                 return -EINVAL;
1479
1480         if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1481                 rdev->desc_nr = -1;
1482         else
1483                 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1484
1485         if (!rdev->bb_page) {
1486                 rdev->bb_page = alloc_page(GFP_KERNEL);
1487                 if (!rdev->bb_page)
1488                         return -ENOMEM;
1489         }
1490         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1491             rdev->badblocks.count == 0) {
1492                 /* need to load the bad block list.
1493                  * Currently we limit it to one page.
1494                  */
1495                 s32 offset;
1496                 sector_t bb_sector;
1497                 u64 *bbp;
1498                 int i;
1499                 int sectors = le16_to_cpu(sb->bblog_size);
1500                 if (sectors > (PAGE_SIZE / 512))
1501                         return -EINVAL;
1502                 offset = le32_to_cpu(sb->bblog_offset);
1503                 if (offset == 0)
1504                         return -EINVAL;
1505                 bb_sector = (long long)offset;
1506                 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1507                                   rdev->bb_page, READ, true))
1508                         return -EIO;
1509                 bbp = (u64 *)page_address(rdev->bb_page);
1510                 rdev->badblocks.shift = sb->bblog_shift;
1511                 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1512                         u64 bb = le64_to_cpu(*bbp);
1513                         int count = bb & (0x3ff);
1514                         u64 sector = bb >> 10;
1515                         sector <<= sb->bblog_shift;
1516                         count <<= sb->bblog_shift;
1517                         if (bb + 1 == 0)
1518                                 break;
1519                         if (md_set_badblocks(&rdev->badblocks,
1520                                              sector, count, 1) == 0)
1521                                 return -EINVAL;
1522                 }
1523         } else if (sb->bblog_offset != 0)
1524                 rdev->badblocks.shift = 0;
1525
1526         if (!refdev) {
1527                 ret = 1;
1528         } else {
1529                 __u64 ev1, ev2;
1530                 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1531
1532                 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1533                     sb->level != refsb->level ||
1534                     sb->layout != refsb->layout ||
1535                     sb->chunksize != refsb->chunksize) {
1536                         printk(KERN_WARNING "md: %s has strangely different"
1537                                 " superblock to %s\n",
1538                                 bdevname(rdev->bdev,b),
1539                                 bdevname(refdev->bdev,b2));
1540                         return -EINVAL;
1541                 }
1542                 ev1 = le64_to_cpu(sb->events);
1543                 ev2 = le64_to_cpu(refsb->events);
1544
1545                 if (ev1 > ev2)
1546                         ret = 1;
1547                 else
1548                         ret = 0;
1549         }
1550         if (minor_version) {
1551                 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1552                 sectors -= rdev->data_offset;
1553         } else
1554                 sectors = rdev->sb_start;
1555         if (sectors < le64_to_cpu(sb->data_size))
1556                 return -EINVAL;
1557         rdev->sectors = le64_to_cpu(sb->data_size);
1558         return ret;
1559 }
1560
1561 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1562 {
1563         struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1564         __u64 ev1 = le64_to_cpu(sb->events);
1565
1566         rdev->raid_disk = -1;
1567         clear_bit(Faulty, &rdev->flags);
1568         clear_bit(In_sync, &rdev->flags);
1569         clear_bit(Bitmap_sync, &rdev->flags);
1570         clear_bit(WriteMostly, &rdev->flags);
1571
1572         if (mddev->raid_disks == 0) {
1573                 mddev->major_version = 1;
1574                 mddev->patch_version = 0;
1575                 mddev->external = 0;
1576                 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1577                 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1578                 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1579                 mddev->level = le32_to_cpu(sb->level);
1580                 mddev->clevel[0] = 0;
1581                 mddev->layout = le32_to_cpu(sb->layout);
1582                 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1583                 mddev->dev_sectors = le64_to_cpu(sb->size);
1584                 mddev->events = ev1;
1585                 mddev->bitmap_info.offset = 0;
1586                 mddev->bitmap_info.space = 0;
1587                 /* Default location for bitmap is 1K after superblock
1588                  * using 3K - total of 4K
1589                  */
1590                 mddev->bitmap_info.default_offset = 1024 >> 9;
1591                 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1592                 mddev->reshape_backwards = 0;
1593
1594                 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1595                 memcpy(mddev->uuid, sb->set_uuid, 16);
1596
1597                 mddev->max_disks =  (4096-256)/2;
1598
1599                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1600                     mddev->bitmap_info.file == NULL) {
1601                         mddev->bitmap_info.offset =
1602                                 (__s32)le32_to_cpu(sb->bitmap_offset);
1603                         /* Metadata doesn't record how much space is available.
1604                          * For 1.0, we assume we can use up to the superblock
1605                          * if before, else to 4K beyond superblock.
1606                          * For others, assume no change is possible.
1607                          */
1608                         if (mddev->minor_version > 0)
1609                                 mddev->bitmap_info.space = 0;
1610                         else if (mddev->bitmap_info.offset > 0)
1611                                 mddev->bitmap_info.space =
1612                                         8 - mddev->bitmap_info.offset;
1613                         else
1614                                 mddev->bitmap_info.space =
1615                                         -mddev->bitmap_info.offset;
1616                 }
1617
1618                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1619                         mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1620                         mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1621                         mddev->new_level = le32_to_cpu(sb->new_level);
1622                         mddev->new_layout = le32_to_cpu(sb->new_layout);
1623                         mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1624                         if (mddev->delta_disks < 0 ||
1625                             (mddev->delta_disks == 0 &&
1626                              (le32_to_cpu(sb->feature_map)
1627                               & MD_FEATURE_RESHAPE_BACKWARDS)))
1628                                 mddev->reshape_backwards = 1;
1629                 } else {
1630                         mddev->reshape_position = MaxSector;
1631                         mddev->delta_disks = 0;
1632                         mddev->new_level = mddev->level;
1633                         mddev->new_layout = mddev->layout;
1634                         mddev->new_chunk_sectors = mddev->chunk_sectors;
1635                 }
1636
1637         } else if (mddev->pers == NULL) {
1638                 /* Insist of good event counter while assembling, except for
1639                  * spares (which don't need an event count) */
1640                 ++ev1;
1641                 if (rdev->desc_nr >= 0 &&
1642                     rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1643                     le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1644                         if (ev1 < mddev->events)
1645                                 return -EINVAL;
1646         } else if (mddev->bitmap) {
1647                 /* If adding to array with a bitmap, then we can accept an
1648                  * older device, but not too old.
1649                  */
1650                 if (ev1 < mddev->bitmap->events_cleared)
1651                         return 0;
1652                 if (ev1 < mddev->events)
1653                         set_bit(Bitmap_sync, &rdev->flags);
1654         } else {
1655                 if (ev1 < mddev->events)
1656                         /* just a hot-add of a new device, leave raid_disk at -1 */
1657                         return 0;
1658         }
1659         if (mddev->level != LEVEL_MULTIPATH) {
1660                 int role;
1661                 if (rdev->desc_nr < 0 ||
1662                     rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1663                         role = 0xffff;
1664                         rdev->desc_nr = -1;
1665                 } else
1666                         role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1667                 switch(role) {
1668                 case 0xffff: /* spare */
1669                         break;
1670                 case 0xfffe: /* faulty */
1671                         set_bit(Faulty, &rdev->flags);
1672                         break;
1673                 default:
1674                         if ((le32_to_cpu(sb->feature_map) &
1675                              MD_FEATURE_RECOVERY_OFFSET))
1676                                 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1677                         else
1678                                 set_bit(In_sync, &rdev->flags);
1679                         rdev->raid_disk = role;
1680                         break;
1681                 }
1682                 if (sb->devflags & WriteMostly1)
1683                         set_bit(WriteMostly, &rdev->flags);
1684                 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1685                         set_bit(Replacement, &rdev->flags);
1686         } else /* MULTIPATH are always insync */
1687                 set_bit(In_sync, &rdev->flags);
1688
1689         return 0;
1690 }
1691
1692 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1693 {
1694         struct mdp_superblock_1 *sb;
1695         struct md_rdev *rdev2;
1696         int max_dev, i;
1697         /* make rdev->sb match mddev and rdev data. */
1698
1699         sb = page_address(rdev->sb_page);
1700
1701         sb->feature_map = 0;
1702         sb->pad0 = 0;
1703         sb->recovery_offset = cpu_to_le64(0);
1704         memset(sb->pad3, 0, sizeof(sb->pad3));
1705
1706         sb->utime = cpu_to_le64((__u64)mddev->utime);
1707         sb->events = cpu_to_le64(mddev->events);
1708         if (mddev->in_sync)
1709                 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1710         else
1711                 sb->resync_offset = cpu_to_le64(0);
1712
1713         sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1714
1715         sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1716         sb->size = cpu_to_le64(mddev->dev_sectors);
1717         sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1718         sb->level = cpu_to_le32(mddev->level);
1719         sb->layout = cpu_to_le32(mddev->layout);
1720
1721         if (test_bit(WriteMostly, &rdev->flags))
1722                 sb->devflags |= WriteMostly1;
1723         else
1724                 sb->devflags &= ~WriteMostly1;
1725         sb->data_offset = cpu_to_le64(rdev->data_offset);
1726         sb->data_size = cpu_to_le64(rdev->sectors);
1727
1728         if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1729                 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1730                 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1731         }
1732
1733         if (rdev->raid_disk >= 0 &&
1734             !test_bit(In_sync, &rdev->flags)) {
1735                 sb->feature_map |=
1736                         cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1737                 sb->recovery_offset =
1738                         cpu_to_le64(rdev->recovery_offset);
1739         }
1740         if (test_bit(Replacement, &rdev->flags))
1741                 sb->feature_map |=
1742                         cpu_to_le32(MD_FEATURE_REPLACEMENT);
1743
1744         if (mddev->reshape_position != MaxSector) {
1745                 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1746                 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1747                 sb->new_layout = cpu_to_le32(mddev->new_layout);
1748                 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1749                 sb->new_level = cpu_to_le32(mddev->new_level);
1750                 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1751                 if (mddev->delta_disks == 0 &&
1752                     mddev->reshape_backwards)
1753                         sb->feature_map
1754                                 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1755                 if (rdev->new_data_offset != rdev->data_offset) {
1756                         sb->feature_map
1757                                 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1758                         sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1759                                                              - rdev->data_offset));
1760                 }
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[i];
1783                                 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1784                                                 | BB_LEN(internal_bb));
1785                                 bbp[i] = cpu_to_le64(store_bb);
1786                         }
1787                         bb->changed = 0;
1788                         if (read_seqretry(&bb->lock, seq))
1789                                 goto retry;
1790
1791                         bb->sector = (rdev->sb_start +
1792                                       (int)le32_to_cpu(sb->bblog_offset));
1793                         bb->size = le16_to_cpu(sb->bblog_size);
1794                 }
1795         }
1796
1797         max_dev = 0;
1798         rdev_for_each(rdev2, mddev)
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         rdev_for_each(rdev2, mddev) {
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(struct md_rdev *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->data_offset != rdev->new_data_offset)
1838                 return 0; /* too confusing */
1839         if (rdev->sb_start < rdev->data_offset) {
1840                 /* minor versions 1 and 2; superblock before data */
1841                 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1842                 max_sectors -= rdev->data_offset;
1843                 if (!num_sectors || num_sectors > max_sectors)
1844                         num_sectors = max_sectors;
1845         } else if (rdev->mddev->bitmap_info.offset) {
1846                 /* minor version 0 with bitmap we can't move */
1847                 return 0;
1848         } else {
1849                 /* minor version 0; superblock after data */
1850                 sector_t sb_start;
1851                 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1852                 sb_start &= ~(sector_t)(4*2 - 1);
1853                 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1854                 if (!num_sectors || num_sectors > max_sectors)
1855                         num_sectors = max_sectors;
1856                 rdev->sb_start = sb_start;
1857         }
1858         sb = page_address(rdev->sb_page);
1859         sb->data_size = cpu_to_le64(num_sectors);
1860         sb->super_offset = rdev->sb_start;
1861         sb->sb_csum = calc_sb_1_csum(sb);
1862         md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1863                        rdev->sb_page);
1864         md_super_wait(rdev->mddev);
1865         return num_sectors;
1866
1867 }
1868
1869 static int
1870 super_1_allow_new_offset(struct md_rdev *rdev,
1871                          unsigned long long new_offset)
1872 {
1873         /* All necessary checks on new >= old have been done */
1874         struct bitmap *bitmap;
1875         if (new_offset >= rdev->data_offset)
1876                 return 1;
1877
1878         /* with 1.0 metadata, there is no metadata to tread on
1879          * so we can always move back */
1880         if (rdev->mddev->minor_version == 0)
1881                 return 1;
1882
1883         /* otherwise we must be sure not to step on
1884          * any metadata, so stay:
1885          * 36K beyond start of superblock
1886          * beyond end of badblocks
1887          * beyond write-intent bitmap
1888          */
1889         if (rdev->sb_start + (32+4)*2 > new_offset)
1890                 return 0;
1891         bitmap = rdev->mddev->bitmap;
1892         if (bitmap && !rdev->mddev->bitmap_info.file &&
1893             rdev->sb_start + rdev->mddev->bitmap_info.offset +
1894             bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
1895                 return 0;
1896         if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
1897                 return 0;
1898
1899         return 1;
1900 }
1901
1902 static struct super_type super_types[] = {
1903         [0] = {
1904                 .name   = "0.90.0",
1905                 .owner  = THIS_MODULE,
1906                 .load_super         = super_90_load,
1907                 .validate_super     = super_90_validate,
1908                 .sync_super         = super_90_sync,
1909                 .rdev_size_change   = super_90_rdev_size_change,
1910                 .allow_new_offset   = super_90_allow_new_offset,
1911         },
1912         [1] = {
1913                 .name   = "md-1",
1914                 .owner  = THIS_MODULE,
1915                 .load_super         = super_1_load,
1916                 .validate_super     = super_1_validate,
1917                 .sync_super         = super_1_sync,
1918                 .rdev_size_change   = super_1_rdev_size_change,
1919                 .allow_new_offset   = super_1_allow_new_offset,
1920         },
1921 };
1922
1923 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
1924 {
1925         if (mddev->sync_super) {
1926                 mddev->sync_super(mddev, rdev);
1927                 return;
1928         }
1929
1930         BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1931
1932         super_types[mddev->major_version].sync_super(mddev, rdev);
1933 }
1934
1935 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
1936 {
1937         struct md_rdev *rdev, *rdev2;
1938
1939         rcu_read_lock();
1940         rdev_for_each_rcu(rdev, mddev1)
1941                 rdev_for_each_rcu(rdev2, mddev2)
1942                         if (rdev->bdev->bd_contains ==
1943                             rdev2->bdev->bd_contains) {
1944                                 rcu_read_unlock();
1945                                 return 1;
1946                         }
1947         rcu_read_unlock();
1948         return 0;
1949 }
1950
1951 static LIST_HEAD(pending_raid_disks);
1952
1953 /*
1954  * Try to register data integrity profile for an mddev
1955  *
1956  * This is called when an array is started and after a disk has been kicked
1957  * from the array. It only succeeds if all working and active component devices
1958  * are integrity capable with matching profiles.
1959  */
1960 int md_integrity_register(struct mddev *mddev)
1961 {
1962         struct md_rdev *rdev, *reference = NULL;
1963
1964         if (list_empty(&mddev->disks))
1965                 return 0; /* nothing to do */
1966         if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1967                 return 0; /* shouldn't register, or already is */
1968         rdev_for_each(rdev, mddev) {
1969                 /* skip spares and non-functional disks */
1970                 if (test_bit(Faulty, &rdev->flags))
1971                         continue;
1972                 if (rdev->raid_disk < 0)
1973                         continue;
1974                 if (!reference) {
1975                         /* Use the first rdev as the reference */
1976                         reference = rdev;
1977                         continue;
1978                 }
1979                 /* does this rdev's profile match the reference profile? */
1980                 if (blk_integrity_compare(reference->bdev->bd_disk,
1981                                 rdev->bdev->bd_disk) < 0)
1982                         return -EINVAL;
1983         }
1984         if (!reference || !bdev_get_integrity(reference->bdev))
1985                 return 0;
1986         /*
1987          * All component devices are integrity capable and have matching
1988          * profiles, register the common profile for the md device.
1989          */
1990         if (blk_integrity_register(mddev->gendisk,
1991                         bdev_get_integrity(reference->bdev)) != 0) {
1992                 printk(KERN_ERR "md: failed to register integrity for %s\n",
1993                         mdname(mddev));
1994                 return -EINVAL;
1995         }
1996         printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1997         if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1998                 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1999                        mdname(mddev));
2000                 return -EINVAL;
2001         }
2002         return 0;
2003 }
2004 EXPORT_SYMBOL(md_integrity_register);
2005
2006 /* Disable data integrity if non-capable/non-matching disk is being added */
2007 void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2008 {
2009         struct blk_integrity *bi_rdev;
2010         struct blk_integrity *bi_mddev;
2011
2012         if (!mddev->gendisk)
2013                 return;
2014
2015         bi_rdev = bdev_get_integrity(rdev->bdev);
2016         bi_mddev = blk_get_integrity(mddev->gendisk);
2017
2018         if (!bi_mddev) /* nothing to do */
2019                 return;
2020         if (rdev->raid_disk < 0) /* skip spares */
2021                 return;
2022         if (bi_rdev && blk_integrity_compare(mddev->gendisk,
2023                                              rdev->bdev->bd_disk) >= 0)
2024                 return;
2025         printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2026         blk_integrity_unregister(mddev->gendisk);
2027 }
2028 EXPORT_SYMBOL(md_integrity_add_rdev);
2029
2030 static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
2031 {
2032         char b[BDEVNAME_SIZE];
2033         struct kobject *ko;
2034         char *s;
2035         int err;
2036
2037         if (rdev->mddev) {
2038                 MD_BUG();
2039                 return -EINVAL;
2040         }
2041
2042         /* prevent duplicates */
2043         if (find_rdev(mddev, rdev->bdev->bd_dev))
2044                 return -EEXIST;
2045
2046         /* make sure rdev->sectors exceeds mddev->dev_sectors */
2047         if (rdev->sectors && (mddev->dev_sectors == 0 ||
2048                         rdev->sectors < mddev->dev_sectors)) {
2049                 if (mddev->pers) {
2050                         /* Cannot change size, so fail
2051                          * If mddev->level <= 0, then we don't care
2052                          * about aligning sizes (e.g. linear)
2053                          */
2054                         if (mddev->level > 0)
2055                                 return -ENOSPC;
2056                 } else
2057                         mddev->dev_sectors = rdev->sectors;
2058         }
2059
2060         /* Verify rdev->desc_nr is unique.
2061          * If it is -1, assign a free number, else
2062          * check number is not in use
2063          */
2064         if (rdev->desc_nr < 0) {
2065                 int choice = 0;
2066                 if (mddev->pers) choice = mddev->raid_disks;
2067                 while (find_rdev_nr(mddev, choice))
2068                         choice++;
2069                 rdev->desc_nr = choice;
2070         } else {
2071                 if (find_rdev_nr(mddev, rdev->desc_nr))
2072                         return -EBUSY;
2073         }
2074         if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2075                 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2076                        mdname(mddev), mddev->max_disks);
2077                 return -EBUSY;
2078         }
2079         bdevname(rdev->bdev,b);
2080         while ( (s=strchr(b, '/')) != NULL)
2081                 *s = '!';
2082
2083         rdev->mddev = mddev;
2084         printk(KERN_INFO "md: bind<%s>\n", b);
2085
2086         if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2087                 goto fail;
2088
2089         ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2090         if (sysfs_create_link(&rdev->kobj, ko, "block"))
2091                 /* failure here is OK */;
2092         rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2093
2094         list_add_rcu(&rdev->same_set, &mddev->disks);
2095         bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2096
2097         /* May as well allow recovery to be retried once */
2098         mddev->recovery_disabled++;
2099
2100         return 0;
2101
2102  fail:
2103         printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2104                b, mdname(mddev));
2105         return err;
2106 }
2107
2108 static void md_delayed_delete(struct work_struct *ws)
2109 {
2110         struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2111         kobject_del(&rdev->kobj);
2112         kobject_put(&rdev->kobj);
2113 }
2114
2115 static void unbind_rdev_from_array(struct md_rdev * rdev)
2116 {
2117         char b[BDEVNAME_SIZE];
2118         if (!rdev->mddev) {
2119                 MD_BUG();
2120                 return;
2121         }
2122         bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2123         list_del_rcu(&rdev->same_set);
2124         printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2125         rdev->mddev = NULL;
2126         sysfs_remove_link(&rdev->kobj, "block");
2127         sysfs_put(rdev->sysfs_state);
2128         rdev->sysfs_state = NULL;
2129         rdev->badblocks.count = 0;
2130         /* We need to delay this, otherwise we can deadlock when
2131          * writing to 'remove' to "dev/state".  We also need
2132          * to delay it due to rcu usage.
2133          */
2134         synchronize_rcu();
2135         INIT_WORK(&rdev->del_work, md_delayed_delete);
2136         kobject_get(&rdev->kobj);
2137         queue_work(md_misc_wq, &rdev->del_work);
2138 }
2139
2140 /*
2141  * prevent the device from being mounted, repartitioned or
2142  * otherwise reused by a RAID array (or any other kernel
2143  * subsystem), by bd_claiming the device.
2144  */
2145 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2146 {
2147         int err = 0;
2148         struct block_device *bdev;
2149         char b[BDEVNAME_SIZE];
2150
2151         bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2152                                  shared ? (struct md_rdev *)lock_rdev : rdev);
2153         if (IS_ERR(bdev)) {
2154                 printk(KERN_ERR "md: could not open %s.\n",
2155                         __bdevname(dev, b));
2156                 return PTR_ERR(bdev);
2157         }
2158         rdev->bdev = bdev;
2159         return err;
2160 }
2161
2162 static void unlock_rdev(struct md_rdev *rdev)
2163 {
2164         struct block_device *bdev = rdev->bdev;
2165         rdev->bdev = NULL;
2166         if (!bdev)
2167                 MD_BUG();
2168         blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2169 }
2170
2171 void md_autodetect_dev(dev_t dev);
2172
2173 static void export_rdev(struct md_rdev * rdev)
2174 {
2175         char b[BDEVNAME_SIZE];
2176         printk(KERN_INFO "md: export_rdev(%s)\n",
2177                 bdevname(rdev->bdev,b));
2178         if (rdev->mddev)
2179                 MD_BUG();
2180         md_rdev_clear(rdev);
2181 #ifndef MODULE
2182         if (test_bit(AutoDetected, &rdev->flags))
2183                 md_autodetect_dev(rdev->bdev->bd_dev);
2184 #endif
2185         unlock_rdev(rdev);
2186         kobject_put(&rdev->kobj);
2187 }
2188
2189 static void kick_rdev_from_array(struct md_rdev * rdev)
2190 {
2191         unbind_rdev_from_array(rdev);
2192         export_rdev(rdev);
2193 }
2194
2195 static void export_array(struct mddev *mddev)
2196 {
2197         struct md_rdev *rdev, *tmp;
2198
2199         rdev_for_each_safe(rdev, tmp, mddev) {
2200                 if (!rdev->mddev) {
2201                         MD_BUG();
2202                         continue;
2203                 }
2204                 kick_rdev_from_array(rdev);
2205         }
2206         if (!list_empty(&mddev->disks))
2207                 MD_BUG();
2208         mddev->raid_disks = 0;
2209         mddev->major_version = 0;
2210 }
2211
2212 static void print_desc(mdp_disk_t *desc)
2213 {
2214         printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2215                 desc->major,desc->minor,desc->raid_disk,desc->state);
2216 }
2217
2218 static void print_sb_90(mdp_super_t *sb)
2219 {
2220         int i;
2221
2222         printk(KERN_INFO 
2223                 "md:  SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2224                 sb->major_version, sb->minor_version, sb->patch_version,
2225                 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2226                 sb->ctime);
2227         printk(KERN_INFO "md:     L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2228                 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2229                 sb->md_minor, sb->layout, sb->chunk_size);
2230         printk(KERN_INFO "md:     UT:%08x ST:%d AD:%d WD:%d"
2231                 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2232                 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2233                 sb->failed_disks, sb->spare_disks,
2234                 sb->sb_csum, (unsigned long)sb->events_lo);
2235
2236         printk(KERN_INFO);
2237         for (i = 0; i < MD_SB_DISKS; i++) {
2238                 mdp_disk_t *desc;
2239
2240                 desc = sb->disks + i;
2241                 if (desc->number || desc->major || desc->minor ||
2242                     desc->raid_disk || (desc->state && (desc->state != 4))) {
2243                         printk("     D %2d: ", i);
2244                         print_desc(desc);
2245                 }
2246         }
2247         printk(KERN_INFO "md:     THIS: ");
2248         print_desc(&sb->this_disk);
2249 }
2250
2251 static void print_sb_1(struct mdp_superblock_1 *sb)
2252 {
2253         __u8 *uuid;
2254
2255         uuid = sb->set_uuid;
2256         printk(KERN_INFO
2257                "md:  SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
2258                "md:    Name: \"%s\" CT:%llu\n",
2259                 le32_to_cpu(sb->major_version),
2260                 le32_to_cpu(sb->feature_map),
2261                 uuid,
2262                 sb->set_name,
2263                 (unsigned long long)le64_to_cpu(sb->ctime)
2264                        & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2265
2266         uuid = sb->device_uuid;
2267         printk(KERN_INFO
2268                "md:       L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
2269                         " RO:%llu\n"
2270                "md:     Dev:%08x UUID: %pU\n"
2271                "md:       (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2272                "md:         (MaxDev:%u) \n",
2273                 le32_to_cpu(sb->level),
2274                 (unsigned long long)le64_to_cpu(sb->size),
2275                 le32_to_cpu(sb->raid_disks),
2276                 le32_to_cpu(sb->layout),
2277                 le32_to_cpu(sb->chunksize),
2278                 (unsigned long long)le64_to_cpu(sb->data_offset),
2279                 (unsigned long long)le64_to_cpu(sb->data_size),
2280                 (unsigned long long)le64_to_cpu(sb->super_offset),
2281                 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2282                 le32_to_cpu(sb->dev_number),
2283                 uuid,
2284                 sb->devflags,
2285                 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2286                 (unsigned long long)le64_to_cpu(sb->events),
2287                 (unsigned long long)le64_to_cpu(sb->resync_offset),
2288                 le32_to_cpu(sb->sb_csum),
2289                 le32_to_cpu(sb->max_dev)
2290                 );
2291 }
2292
2293 static void print_rdev(struct md_rdev *rdev, int major_version)
2294 {
2295         char b[BDEVNAME_SIZE];
2296         printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2297                 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
2298                 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2299                 rdev->desc_nr);
2300         if (rdev->sb_loaded) {
2301                 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2302                 switch (major_version) {
2303                 case 0:
2304                         print_sb_90(page_address(rdev->sb_page));
2305                         break;
2306                 case 1:
2307                         print_sb_1(page_address(rdev->sb_page));
2308                         break;
2309                 }
2310         } else
2311                 printk(KERN_INFO "md: no rdev superblock!\n");
2312 }
2313
2314 static void md_print_devices(void)
2315 {
2316         struct list_head *tmp;
2317         struct md_rdev *rdev;
2318         struct mddev *mddev;
2319         char b[BDEVNAME_SIZE];
2320
2321         printk("\n");
2322         printk("md:     **********************************\n");
2323         printk("md:     * <COMPLETE RAID STATE PRINTOUT> *\n");
2324         printk("md:     **********************************\n");
2325         for_each_mddev(mddev, tmp) {
2326
2327                 if (mddev->bitmap)
2328                         bitmap_print_sb(mddev->bitmap);
2329                 else
2330                         printk("%s: ", mdname(mddev));
2331                 rdev_for_each(rdev, mddev)
2332                         printk("<%s>", bdevname(rdev->bdev,b));
2333                 printk("\n");
2334
2335                 rdev_for_each(rdev, mddev)
2336                         print_rdev(rdev, mddev->major_version);
2337         }
2338         printk("md:     **********************************\n");
2339         printk("\n");
2340 }
2341
2342
2343 static void sync_sbs(struct mddev * mddev, int nospares)
2344 {
2345         /* Update each superblock (in-memory image), but
2346          * if we are allowed to, skip spares which already
2347          * have the right event counter, or have one earlier
2348          * (which would mean they aren't being marked as dirty
2349          * with the rest of the array)
2350          */
2351         struct md_rdev *rdev;
2352         rdev_for_each(rdev, mddev) {
2353                 if (rdev->sb_events == mddev->events ||
2354                     (nospares &&
2355                      rdev->raid_disk < 0 &&
2356                      rdev->sb_events+1 == mddev->events)) {
2357                         /* Don't update this superblock */
2358                         rdev->sb_loaded = 2;
2359                 } else {
2360                         sync_super(mddev, rdev);
2361                         rdev->sb_loaded = 1;
2362                 }
2363         }
2364 }
2365
2366 static void md_update_sb(struct mddev * mddev, int force_change)
2367 {
2368         struct md_rdev *rdev;
2369         int sync_req;
2370         int nospares = 0;
2371         int any_badblocks_changed = 0;
2372
2373         if (mddev->ro) {
2374                 if (force_change)
2375                         set_bit(MD_CHANGE_DEVS, &mddev->flags);
2376                 return;
2377         }
2378 repeat:
2379         /* First make sure individual recovery_offsets are correct */
2380         rdev_for_each(rdev, mddev) {
2381                 if (rdev->raid_disk >= 0 &&
2382                     mddev->delta_disks >= 0 &&
2383                     !test_bit(In_sync, &rdev->flags) &&
2384                     mddev->curr_resync_completed > rdev->recovery_offset)
2385                                 rdev->recovery_offset = mddev->curr_resync_completed;
2386
2387         }       
2388         if (!mddev->persistent) {
2389                 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2390                 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2391                 if (!mddev->external) {
2392                         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2393                         rdev_for_each(rdev, mddev) {
2394                                 if (rdev->badblocks.changed) {
2395                                         rdev->badblocks.changed = 0;
2396                                         md_ack_all_badblocks(&rdev->badblocks);
2397                                         md_error(mddev, rdev);
2398                                 }
2399                                 clear_bit(Blocked, &rdev->flags);
2400                                 clear_bit(BlockedBadBlocks, &rdev->flags);
2401                                 wake_up(&rdev->blocked_wait);
2402                         }
2403                 }
2404                 wake_up(&mddev->sb_wait);
2405                 return;
2406         }
2407
2408         spin_lock_irq(&mddev->write_lock);
2409
2410         mddev->utime = get_seconds();
2411
2412         if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2413                 force_change = 1;
2414         if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2415                 /* just a clean<-> dirty transition, possibly leave spares alone,
2416                  * though if events isn't the right even/odd, we will have to do
2417                  * spares after all
2418                  */
2419                 nospares = 1;
2420         if (force_change)
2421                 nospares = 0;
2422         if (mddev->degraded)
2423                 /* If the array is degraded, then skipping spares is both
2424                  * dangerous and fairly pointless.
2425                  * Dangerous because a device that was removed from the array
2426                  * might have a event_count that still looks up-to-date,
2427                  * so it can be re-added without a resync.
2428                  * Pointless because if there are any spares to skip,
2429                  * then a recovery will happen and soon that array won't
2430                  * be degraded any more and the spare can go back to sleep then.
2431                  */
2432                 nospares = 0;
2433
2434         sync_req = mddev->in_sync;
2435
2436         /* If this is just a dirty<->clean transition, and the array is clean
2437          * and 'events' is odd, we can roll back to the previous clean state */
2438         if (nospares
2439             && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2440             && mddev->can_decrease_events
2441             && mddev->events != 1) {
2442                 mddev->events--;
2443                 mddev->can_decrease_events = 0;
2444         } else {
2445                 /* otherwise we have to go forward and ... */
2446                 mddev->events ++;
2447                 mddev->can_decrease_events = nospares;
2448         }
2449
2450         if (!mddev->events) {
2451                 /*
2452                  * oops, this 64-bit counter should never wrap.
2453                  * Either we are in around ~1 trillion A.C., assuming
2454                  * 1 reboot per second, or we have a bug:
2455                  */
2456                 MD_BUG();
2457                 mddev->events --;
2458         }
2459
2460         rdev_for_each(rdev, mddev) {
2461                 if (rdev->badblocks.changed)
2462                         any_badblocks_changed++;
2463                 if (test_bit(Faulty, &rdev->flags))
2464                         set_bit(FaultRecorded, &rdev->flags);
2465         }
2466
2467         sync_sbs(mddev, nospares);
2468         spin_unlock_irq(&mddev->write_lock);
2469
2470         pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2471                  mdname(mddev), mddev->in_sync);
2472
2473         bitmap_update_sb(mddev->bitmap);
2474         rdev_for_each(rdev, mddev) {
2475                 char b[BDEVNAME_SIZE];
2476
2477                 if (rdev->sb_loaded != 1)
2478                         continue; /* no noise on spare devices */
2479
2480                 if (!test_bit(Faulty, &rdev->flags) &&
2481                     rdev->saved_raid_disk == -1) {
2482                         md_super_write(mddev,rdev,
2483                                        rdev->sb_start, rdev->sb_size,
2484                                        rdev->sb_page);
2485                         pr_debug("md: (write) %s's sb offset: %llu\n",
2486                                  bdevname(rdev->bdev, b),
2487                                  (unsigned long long)rdev->sb_start);
2488                         rdev->sb_events = mddev->events;
2489                         if (rdev->badblocks.size) {
2490                                 md_super_write(mddev, rdev,
2491                                                rdev->badblocks.sector,
2492                                                rdev->badblocks.size << 9,
2493                                                rdev->bb_page);
2494                                 rdev->badblocks.size = 0;
2495                         }
2496
2497                 } else if (test_bit(Faulty, &rdev->flags))
2498                         pr_debug("md: %s (skipping faulty)\n",
2499                                  bdevname(rdev->bdev, b));
2500                 else
2501                         pr_debug("(skipping incremental s/r ");
2502
2503                 if (mddev->level == LEVEL_MULTIPATH)
2504                         /* only need to write one superblock... */
2505                         break;
2506         }
2507         md_super_wait(mddev);
2508         /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
2509
2510         spin_lock_irq(&mddev->write_lock);
2511         if (mddev->in_sync != sync_req ||
2512             test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
2513                 /* have to write it out again */
2514                 spin_unlock_irq(&mddev->write_lock);
2515                 goto repeat;
2516         }
2517         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2518         spin_unlock_irq(&mddev->write_lock);
2519         wake_up(&mddev->sb_wait);
2520         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2521                 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2522
2523         rdev_for_each(rdev, mddev) {
2524                 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2525                         clear_bit(Blocked, &rdev->flags);
2526
2527                 if (any_badblocks_changed)
2528                         md_ack_all_badblocks(&rdev->badblocks);
2529                 clear_bit(BlockedBadBlocks, &rdev->flags);
2530                 wake_up(&rdev->blocked_wait);
2531         }
2532 }
2533
2534 /* words written to sysfs files may, or may not, be \n terminated.
2535  * We want to accept with case. For this we use cmd_match.
2536  */
2537 static int cmd_match(const char *cmd, const char *str)
2538 {
2539         /* See if cmd, written into a sysfs file, matches
2540          * str.  They must either be the same, or cmd can
2541          * have a trailing newline
2542          */
2543         while (*cmd && *str && *cmd == *str) {
2544                 cmd++;
2545                 str++;
2546         }
2547         if (*cmd == '\n')
2548                 cmd++;
2549         if (*str || *cmd)
2550                 return 0;
2551         return 1;
2552 }
2553
2554 struct rdev_sysfs_entry {
2555         struct attribute attr;
2556         ssize_t (*show)(struct md_rdev *, char *);
2557         ssize_t (*store)(struct md_rdev *, const char *, size_t);
2558 };
2559
2560 static ssize_t
2561 state_show(struct md_rdev *rdev, char *page)
2562 {
2563         char *sep = "";
2564         size_t len = 0;
2565
2566         if (test_bit(Faulty, &rdev->flags) ||
2567             rdev->badblocks.unacked_exist) {
2568                 len+= sprintf(page+len, "%sfaulty",sep);
2569                 sep = ",";
2570         }
2571         if (test_bit(In_sync, &rdev->flags)) {
2572                 len += sprintf(page+len, "%sin_sync",sep);
2573                 sep = ",";
2574         }
2575         if (test_bit(WriteMostly, &rdev->flags)) {
2576                 len += sprintf(page+len, "%swrite_mostly",sep);
2577                 sep = ",";
2578         }
2579         if (test_bit(Blocked, &rdev->flags) ||
2580             (rdev->badblocks.unacked_exist
2581              && !test_bit(Faulty, &rdev->flags))) {
2582                 len += sprintf(page+len, "%sblocked", sep);
2583                 sep = ",";
2584         }
2585         if (!test_bit(Faulty, &rdev->flags) &&
2586             !test_bit(In_sync, &rdev->flags)) {
2587                 len += sprintf(page+len, "%sspare", sep);
2588                 sep = ",";
2589         }
2590         if (test_bit(WriteErrorSeen, &rdev->flags)) {
2591                 len += sprintf(page+len, "%swrite_error", sep);
2592                 sep = ",";
2593         }
2594         if (test_bit(WantReplacement, &rdev->flags)) {
2595                 len += sprintf(page+len, "%swant_replacement", sep);
2596                 sep = ",";
2597         }
2598         if (test_bit(Replacement, &rdev->flags)) {
2599                 len += sprintf(page+len, "%sreplacement", sep);
2600                 sep = ",";
2601         }
2602
2603         return len+sprintf(page+len, "\n");
2604 }
2605
2606 static ssize_t
2607 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2608 {
2609         /* can write
2610          *  faulty  - simulates an error
2611          *  remove  - disconnects the device
2612          *  writemostly - sets write_mostly
2613          *  -writemostly - clears write_mostly
2614          *  blocked - sets the Blocked flags
2615          *  -blocked - clears the Blocked and possibly simulates an error
2616          *  insync - sets Insync providing device isn't active
2617          *  write_error - sets WriteErrorSeen
2618          *  -write_error - clears WriteErrorSeen
2619          */
2620         int err = -EINVAL;
2621         if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2622                 md_error(rdev->mddev, rdev);
2623                 if (test_bit(Faulty, &rdev->flags))
2624                         err = 0;
2625                 else
2626                         err = -EBUSY;
2627         } else if (cmd_match(buf, "remove")) {
2628                 if (rdev->raid_disk >= 0)
2629                         err = -EBUSY;
2630                 else {
2631                         struct mddev *mddev = rdev->mddev;
2632                         kick_rdev_from_array(rdev);
2633                         if (mddev->pers)
2634                                 md_update_sb(mddev, 1);
2635                         md_new_event(mddev);
2636                         err = 0;
2637                 }
2638         } else if (cmd_match(buf, "writemostly")) {
2639                 set_bit(WriteMostly, &rdev->flags);
2640                 err = 0;
2641         } else if (cmd_match(buf, "-writemostly")) {
2642                 clear_bit(WriteMostly, &rdev->flags);
2643                 err = 0;
2644         } else if (cmd_match(buf, "blocked")) {
2645                 set_bit(Blocked, &rdev->flags);
2646                 err = 0;
2647         } else if (cmd_match(buf, "-blocked")) {
2648                 if (!test_bit(Faulty, &rdev->flags) &&
2649                     rdev->badblocks.unacked_exist) {
2650                         /* metadata handler doesn't understand badblocks,
2651                          * so we need to fail the device
2652                          */
2653                         md_error(rdev->mddev, rdev);
2654                 }
2655                 clear_bit(Blocked, &rdev->flags);
2656                 clear_bit(BlockedBadBlocks, &rdev->flags);
2657                 wake_up(&rdev->blocked_wait);
2658                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2659                 md_wakeup_thread(rdev->mddev->thread);
2660
2661                 err = 0;
2662         } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2663                 set_bit(In_sync, &rdev->flags);
2664                 err = 0;
2665         } else if (cmd_match(buf, "write_error")) {
2666                 set_bit(WriteErrorSeen, &rdev->flags);
2667                 err = 0;
2668         } else if (cmd_match(buf, "-write_error")) {
2669                 clear_bit(WriteErrorSeen, &rdev->flags);
2670                 err = 0;
2671         } else if (cmd_match(buf, "want_replacement")) {
2672                 /* Any non-spare device that is not a replacement can
2673                  * become want_replacement at any time, but we then need to
2674                  * check if recovery is needed.
2675                  */
2676                 if (rdev->raid_disk >= 0 &&
2677                     !test_bit(Replacement, &rdev->flags))
2678                         set_bit(WantReplacement, &rdev->flags);
2679                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2680                 md_wakeup_thread(rdev->mddev->thread);
2681                 err = 0;
2682         } else if (cmd_match(buf, "-want_replacement")) {
2683                 /* Clearing 'want_replacement' is always allowed.
2684                  * Once replacements starts it is too late though.
2685                  */
2686                 err = 0;
2687                 clear_bit(WantReplacement, &rdev->flags);
2688         } else if (cmd_match(buf, "replacement")) {
2689                 /* Can only set a device as a replacement when array has not
2690                  * yet been started.  Once running, replacement is automatic
2691                  * from spares, or by assigning 'slot'.
2692                  */
2693                 if (rdev->mddev->pers)
2694                         err = -EBUSY;
2695                 else {
2696                         set_bit(Replacement, &rdev->flags);
2697                         err = 0;
2698                 }
2699         } else if (cmd_match(buf, "-replacement")) {
2700                 /* Similarly, can only clear Replacement before start */
2701                 if (rdev->mddev->pers)
2702                         err = -EBUSY;
2703                 else {
2704                         clear_bit(Replacement, &rdev->flags);
2705                         err = 0;
2706                 }
2707         }
2708         if (!err)
2709                 sysfs_notify_dirent_safe(rdev->sysfs_state);
2710         return err ? err : len;
2711 }
2712 static struct rdev_sysfs_entry rdev_state =
2713 __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
2714
2715 static ssize_t
2716 errors_show(struct md_rdev *rdev, char *page)
2717 {
2718         return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2719 }
2720
2721 static ssize_t
2722 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2723 {
2724         char *e;
2725         unsigned long n = simple_strtoul(buf, &e, 10);
2726         if (*buf && (*e == 0 || *e == '\n')) {
2727                 atomic_set(&rdev->corrected_errors, n);
2728                 return len;
2729         }
2730         return -EINVAL;
2731 }
2732 static struct rdev_sysfs_entry rdev_errors =
2733 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2734
2735 static ssize_t
2736 slot_show(struct md_rdev *rdev, char *page)
2737 {
2738         if (rdev->raid_disk < 0)
2739                 return sprintf(page, "none\n");
2740         else
2741                 return sprintf(page, "%d\n", rdev->raid_disk);
2742 }
2743
2744 static ssize_t
2745 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2746 {
2747         char *e;
2748         int err;
2749         int slot = simple_strtoul(buf, &e, 10);
2750         if (strncmp(buf, "none", 4)==0)
2751                 slot = -1;
2752         else if (e==buf || (*e && *e!= '\n'))
2753                 return -EINVAL;
2754         if (rdev->mddev->pers && slot == -1) {
2755                 /* Setting 'slot' on an active array requires also
2756                  * updating the 'rd%d' link, and communicating
2757                  * with the personality with ->hot_*_disk.
2758                  * For now we only support removing
2759                  * failed/spare devices.  This normally happens automatically,
2760                  * but not when the metadata is externally managed.
2761                  */
2762                 if (rdev->raid_disk == -1)
2763                         return -EEXIST;
2764                 /* personality does all needed checks */
2765                 if (rdev->mddev->pers->hot_remove_disk == NULL)
2766                         return -EINVAL;
2767                 clear_bit(Blocked, &rdev->flags);
2768                 remove_and_add_spares(rdev->mddev, rdev);
2769                 if (rdev->raid_disk >= 0)
2770                         return -EBUSY;
2771                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2772                 md_wakeup_thread(rdev->mddev->thread);
2773         } else if (rdev->mddev->pers) {
2774                 /* Activating a spare .. or possibly reactivating
2775                  * if we ever get bitmaps working here.
2776                  */
2777
2778                 if (rdev->raid_disk != -1)
2779                         return -EBUSY;
2780
2781                 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2782                         return -EBUSY;
2783
2784                 if (rdev->mddev->pers->hot_add_disk == NULL)
2785                         return -EINVAL;
2786
2787                 if (slot >= rdev->mddev->raid_disks &&
2788                     slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2789                         return -ENOSPC;
2790
2791                 rdev->raid_disk = slot;
2792                 if (test_bit(In_sync, &rdev->flags))
2793                         rdev->saved_raid_disk = slot;
2794                 else
2795                         rdev->saved_raid_disk = -1;
2796                 clear_bit(In_sync, &rdev->flags);
2797                 clear_bit(Bitmap_sync, &rdev->flags);
2798                 err = rdev->mddev->pers->
2799                         hot_add_disk(rdev->mddev, rdev);
2800                 if (err) {
2801                         rdev->raid_disk = -1;
2802                         return err;
2803                 } else
2804                         sysfs_notify_dirent_safe(rdev->sysfs_state);
2805                 if (sysfs_link_rdev(rdev->mddev, rdev))
2806                         /* failure here is OK */;
2807                 /* don't wakeup anyone, leave that to userspace. */
2808         } else {
2809                 if (slot >= rdev->mddev->raid_disks &&
2810                     slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2811                         return -ENOSPC;
2812                 rdev->raid_disk = slot;
2813                 /* assume it is working */
2814                 clear_bit(Faulty, &rdev->flags);
2815                 clear_bit(WriteMostly, &rdev->flags);
2816                 set_bit(In_sync, &rdev->flags);
2817                 sysfs_notify_dirent_safe(rdev->sysfs_state);
2818         }
2819         return len;
2820 }
2821
2822
2823 static struct rdev_sysfs_entry rdev_slot =
2824 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2825
2826 static ssize_t
2827 offset_show(struct md_rdev *rdev, char *page)
2828 {
2829         return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
2830 }
2831
2832 static ssize_t
2833 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
2834 {
2835         unsigned long long offset;
2836         if (kstrtoull(buf, 10, &offset) < 0)
2837                 return -EINVAL;
2838         if (rdev->mddev->pers && rdev->raid_disk >= 0)
2839                 return -EBUSY;
2840         if (rdev->sectors && rdev->mddev->external)
2841                 /* Must set offset before size, so overlap checks
2842                  * can be sane */
2843                 return -EBUSY;
2844         rdev->data_offset = offset;
2845         rdev->new_data_offset = offset;
2846         return len;
2847 }
2848
2849 static struct rdev_sysfs_entry rdev_offset =
2850 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
2851
2852 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
2853 {
2854         return sprintf(page, "%llu\n",
2855                        (unsigned long long)rdev->new_data_offset);
2856 }
2857
2858 static ssize_t new_offset_store(struct md_rdev *rdev,
2859                                 const char *buf, size_t len)
2860 {
2861         unsigned long long new_offset;
2862         struct mddev *mddev = rdev->mddev;
2863
2864         if (kstrtoull(buf, 10, &new_offset) < 0)
2865                 return -EINVAL;
2866
2867         if (mddev->sync_thread)
2868                 return -EBUSY;
2869         if (new_offset == rdev->data_offset)
2870                 /* reset is always permitted */
2871                 ;
2872         else if (new_offset > rdev->data_offset) {
2873                 /* must not push array size beyond rdev_sectors */
2874                 if (new_offset - rdev->data_offset
2875                     + mddev->dev_sectors > rdev->sectors)
2876                                 return -E2BIG;
2877         }
2878         /* Metadata worries about other space details. */
2879
2880         /* decreasing the offset is inconsistent with a backwards
2881          * reshape.
2882          */
2883         if (new_offset < rdev->data_offset &&
2884             mddev->reshape_backwards)
2885                 return -EINVAL;
2886         /* Increasing offset is inconsistent with forwards
2887          * reshape.  reshape_direction should be set to
2888          * 'backwards' first.
2889          */
2890         if (new_offset > rdev->data_offset &&
2891             !mddev->reshape_backwards)
2892                 return -EINVAL;
2893
2894         if (mddev->pers && mddev->persistent &&
2895             !super_types[mddev->major_version]
2896             .allow_new_offset(rdev, new_offset))
2897                 return -E2BIG;
2898         rdev->new_data_offset = new_offset;
2899         if (new_offset > rdev->data_offset)
2900                 mddev->reshape_backwards = 1;
2901         else if (new_offset < rdev->data_offset)
2902                 mddev->reshape_backwards = 0;
2903
2904         return len;
2905 }
2906 static struct rdev_sysfs_entry rdev_new_offset =
2907 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
2908
2909 static ssize_t
2910 rdev_size_show(struct md_rdev *rdev, char *page)
2911 {
2912         return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
2913 }
2914
2915 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2916 {
2917         /* check if two start/length pairs overlap */
2918         if (s1+l1 <= s2)
2919                 return 0;
2920         if (s2+l2 <= s1)
2921                 return 0;
2922         return 1;
2923 }
2924
2925 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2926 {
2927         unsigned long long blocks;
2928         sector_t new;
2929
2930         if (kstrtoull(buf, 10, &blocks) < 0)
2931                 return -EINVAL;
2932
2933         if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2934                 return -EINVAL; /* sector conversion overflow */
2935
2936         new = blocks * 2;
2937         if (new != blocks * 2)
2938                 return -EINVAL; /* unsigned long long to sector_t overflow */
2939
2940         *sectors = new;
2941         return 0;
2942 }
2943
2944 static ssize_t
2945 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
2946 {
2947         struct mddev *my_mddev = rdev->mddev;
2948         sector_t oldsectors = rdev->sectors;
2949         sector_t sectors;
2950
2951         if (strict_blocks_to_sectors(buf, &sectors) < 0)
2952                 return -EINVAL;
2953         if (rdev->data_offset != rdev->new_data_offset)
2954                 return -EINVAL; /* too confusing */
2955         if (my_mddev->pers && rdev->raid_disk >= 0) {
2956                 if (my_mddev->persistent) {
2957                         sectors = super_types[my_mddev->major_version].
2958                                 rdev_size_change(rdev, sectors);
2959                         if (!sectors)
2960                                 return -EBUSY;
2961                 } else if (!sectors)
2962                         sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
2963                                 rdev->data_offset;
2964                 if (!my_mddev->pers->resize)
2965                         /* Cannot change size for RAID0 or Linear etc */
2966                         return -EINVAL;
2967         }
2968         if (sectors < my_mddev->dev_sectors)
2969                 return -EINVAL; /* component must fit device */
2970
2971         rdev->sectors = sectors;
2972         if (sectors > oldsectors && my_mddev->external) {
2973                 /* need to check that all other rdevs with the same ->bdev
2974                  * do not overlap.  We need to unlock the mddev to avoid
2975                  * a deadlock.  We have already changed rdev->sectors, and if
2976                  * we have to change it back, we will have the lock again.
2977                  */
2978                 struct mddev *mddev;
2979                 int overlap = 0;
2980                 struct list_head *tmp;
2981
2982                 mddev_unlock(my_mddev);
2983                 for_each_mddev(mddev, tmp) {
2984                         struct md_rdev *rdev2;
2985
2986                         mddev_lock_nointr(mddev);
2987                         rdev_for_each(rdev2, mddev)
2988                                 if (rdev->bdev == rdev2->bdev &&
2989                                     rdev != rdev2 &&
2990                                     overlaps(rdev->data_offset, rdev->sectors,
2991                                              rdev2->data_offset,
2992                                              rdev2->sectors)) {
2993                                         overlap = 1;
2994                                         break;
2995                                 }
2996                         mddev_unlock(mddev);
2997                         if (overlap) {
2998                                 mddev_put(mddev);
2999                                 break;
3000                         }
3001                 }
3002                 mddev_lock_nointr(my_mddev);
3003                 if (overlap) {
3004                         /* Someone else could have slipped in a size
3005                          * change here, but doing so is just silly.
3006                          * We put oldsectors back because we *know* it is
3007                          * safe, and trust userspace not to race with
3008                          * itself
3009                          */
3010                         rdev->sectors = oldsectors;
3011                         return -EBUSY;
3012                 }
3013         }
3014         return len;
3015 }
3016
3017 static struct rdev_sysfs_entry rdev_size =
3018 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3019
3020
3021 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3022 {
3023         unsigned long long recovery_start = rdev->recovery_offset;
3024
3025         if (test_bit(In_sync, &rdev->flags) ||
3026             recovery_start == MaxSector)
3027                 return sprintf(page, "none\n");
3028
3029         return sprintf(page, "%llu\n", recovery_start);
3030 }
3031
3032 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3033 {
3034         unsigned long long recovery_start;
3035
3036         if (cmd_match(buf, "none"))
3037                 recovery_start = MaxSector;
3038         else if (kstrtoull(buf, 10, &recovery_start))
3039                 return -EINVAL;
3040
3041         if (rdev->mddev->pers &&
3042             rdev->raid_disk >= 0)
3043                 return -EBUSY;
3044
3045         rdev->recovery_offset = recovery_start;
3046         if (recovery_start == MaxSector)
3047                 set_bit(In_sync, &rdev->flags);
3048         else
3049                 clear_bit(In_sync, &rdev->flags);
3050         return len;
3051 }
3052
3053 static struct rdev_sysfs_entry rdev_recovery_start =
3054 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3055
3056
3057 static ssize_t
3058 badblocks_show(struct badblocks *bb, char *page, int unack);
3059 static ssize_t
3060 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
3061
3062 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3063 {
3064         return badblocks_show(&rdev->badblocks, page, 0);
3065 }
3066 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3067 {
3068         int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3069         /* Maybe that ack was all we needed */
3070         if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3071                 wake_up(&rdev->blocked_wait);
3072         return rv;
3073 }
3074 static struct rdev_sysfs_entry rdev_bad_blocks =
3075 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3076
3077
3078 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3079 {
3080         return badblocks_show(&rdev->badblocks, page, 1);
3081 }
3082 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3083 {
3084         return badblocks_store(&rdev->badblocks, page, len, 1);
3085 }
3086 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3087 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3088
3089 static struct attribute *rdev_default_attrs[] = {
3090         &rdev_state.attr,
3091         &rdev_errors.attr,
3092         &rdev_slot.attr,
3093         &rdev_offset.attr,
3094         &rdev_new_offset.attr,
3095         &rdev_size.attr,
3096         &rdev_recovery_start.attr,
3097         &rdev_bad_blocks.attr,
3098         &rdev_unack_bad_blocks.attr,
3099         NULL,
3100 };
3101 static ssize_t
3102 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3103 {
3104         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3105         struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3106         struct mddev *mddev = rdev->mddev;
3107         ssize_t rv;
3108
3109         if (!entry->show)
3110                 return -EIO;
3111
3112         rv = mddev ? mddev_lock(mddev) : -EBUSY;
3113         if (!rv) {
3114                 if (rdev->mddev == NULL)
3115                         rv = -EBUSY;
3116                 else
3117                         rv = entry->show(rdev, page);
3118                 mddev_unlock(mddev);
3119         }
3120         return rv;
3121 }
3122
3123 static ssize_t
3124 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3125               const char *page, size_t length)
3126 {
3127         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3128         struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3129         ssize_t rv;
3130         struct mddev *mddev = rdev->mddev;
3131
3132         if (!entry->store)
3133                 return -EIO;
3134         if (!capable(CAP_SYS_ADMIN))
3135                 return -EACCES;
3136         rv = mddev ? mddev_lock(mddev): -EBUSY;
3137         if (!rv) {
3138                 if (rdev->mddev == NULL)
3139                         rv = -EBUSY;
3140                 else
3141                         rv = entry->store(rdev, page, length);
3142                 mddev_unlock(mddev);
3143         }
3144         return rv;
3145 }
3146
3147 static void rdev_free(struct kobject *ko)
3148 {
3149         struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3150         kfree(rdev);
3151 }
3152 static const struct sysfs_ops rdev_sysfs_ops = {
3153         .show           = rdev_attr_show,
3154         .store          = rdev_attr_store,
3155 };
3156 static struct kobj_type rdev_ktype = {
3157         .release        = rdev_free,
3158         .sysfs_ops      = &rdev_sysfs_ops,
3159         .default_attrs  = rdev_default_attrs,
3160 };
3161
3162 int md_rdev_init(struct md_rdev *rdev)
3163 {
3164         rdev->desc_nr = -1;
3165         rdev->saved_raid_disk = -1;
3166         rdev->raid_disk = -1;
3167         rdev->flags = 0;
3168         rdev->data_offset = 0;
3169         rdev->new_data_offset = 0;
3170         rdev->sb_events = 0;
3171         rdev->last_read_error.tv_sec  = 0;
3172         rdev->last_read_error.tv_nsec = 0;
3173         rdev->sb_loaded = 0;
3174         rdev->bb_page = NULL;
3175         atomic_set(&rdev->nr_pending, 0);
3176         atomic_set(&rdev->read_errors, 0);
3177         atomic_set(&rdev->corrected_errors, 0);
3178
3179         INIT_LIST_HEAD(&rdev->same_set);
3180         init_waitqueue_head(&rdev->blocked_wait);
3181
3182         /* Add space to store bad block list.
3183          * This reserves the space even on arrays where it cannot
3184          * be used - I wonder if that matters
3185          */
3186         rdev->badblocks.count = 0;
3187         rdev->badblocks.shift = -1; /* disabled until explicitly enabled */
3188         rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3189         seqlock_init(&rdev->badblocks.lock);
3190         if (rdev->badblocks.page == NULL)
3191                 return -ENOMEM;
3192
3193         return 0;
3194 }
3195 EXPORT_SYMBOL_GPL(md_rdev_init);
3196 /*
3197  * Import a device. If 'super_format' >= 0, then sanity check the superblock
3198  *
3199  * mark the device faulty if:
3200  *
3201  *   - the device is nonexistent (zero size)
3202  *   - the device has no valid superblock
3203  *
3204  * a faulty rdev _never_ has rdev->sb set.
3205  */
3206 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3207 {
3208         char b[BDEVNAME_SIZE];
3209         int err;
3210         struct md_rdev *rdev;
3211         sector_t size;
3212
3213         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3214         if (!rdev) {
3215                 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3216                 return ERR_PTR(-ENOMEM);
3217         }
3218
3219         err = md_rdev_init(rdev);
3220         if (err)
3221                 goto abort_free;
3222         err = alloc_disk_sb(rdev);
3223         if (err)
3224                 goto abort_free;
3225
3226         err = lock_rdev(rdev, newdev, super_format == -2);
3227         if (err)
3228                 goto abort_free;
3229
3230         kobject_init(&rdev->kobj, &rdev_ktype);
3231
3232         size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3233         if (!size) {
3234                 printk(KERN_WARNING 
3235                         "md: %s has zero or unknown size, marking faulty!\n",
3236                         bdevname(rdev->bdev,b));
3237                 err = -EINVAL;
3238                 goto abort_free;
3239         }
3240
3241         if (super_format >= 0) {
3242                 err = super_types[super_format].
3243                         load_super(rdev, NULL, super_minor);
3244                 if (err == -EINVAL) {
3245                         printk(KERN_WARNING
3246                                 "md: %s does not have a valid v%d.%d "
3247                                "superblock, not importing!\n",
3248                                 bdevname(rdev->bdev,b),
3249                                super_format, super_minor);
3250                         goto abort_free;
3251                 }
3252                 if (err < 0) {
3253                         printk(KERN_WARNING 
3254                                 "md: could not read %s's sb, not importing!\n",
3255                                 bdevname(rdev->bdev,b));
3256                         goto abort_free;
3257                 }
3258         }
3259
3260         return rdev;
3261
3262 abort_free:
3263         if (rdev->bdev)
3264                 unlock_rdev(rdev);
3265         md_rdev_clear(rdev);
3266         kfree(rdev);
3267         return ERR_PTR(err);
3268 }
3269
3270 /*
3271  * Check a full RAID array for plausibility
3272  */
3273
3274
3275 static void analyze_sbs(struct mddev * mddev)
3276 {
3277         int i;
3278         struct md_rdev *rdev, *freshest, *tmp;
3279         char b[BDEVNAME_SIZE];
3280
3281         freshest = NULL;
3282         rdev_for_each_safe(rdev, tmp, mddev)
3283                 switch (super_types[mddev->major_version].
3284                         load_super(rdev, freshest, mddev->minor_version)) {
3285                 case 1:
3286                         freshest = rdev;
3287                         break;
3288                 case 0:
3289                         break;
3290                 default:
3291                         printk( KERN_ERR \
3292                                 "md: fatal superblock inconsistency in %s"
3293                                 " -- removing from array\n", 
3294                                 bdevname(rdev->bdev,b));
3295                         kick_rdev_from_array(rdev);
3296                 }
3297
3298
3299         super_types[mddev->major_version].
3300                 validate_super(mddev, freshest);
3301
3302         i = 0;
3303         rdev_for_each_safe(rdev, tmp, mddev) {
3304                 if (mddev->max_disks &&
3305                     (rdev->desc_nr >= mddev->max_disks ||
3306                      i > mddev->max_disks)) {
3307                         printk(KERN_WARNING
3308                                "md: %s: %s: only %d devices permitted\n",
3309                                mdname(mddev), bdevname(rdev->bdev, b),
3310                                mddev->max_disks);
3311                         kick_rdev_from_array(rdev);
3312                         continue;
3313                 }
3314                 if (rdev != freshest)
3315                         if (super_types[mddev->major_version].
3316                             validate_super(mddev, rdev)) {
3317                                 printk(KERN_WARNING "md: kicking non-fresh %s"
3318                                         " from array!\n",
3319                                         bdevname(rdev->bdev,b));
3320                                 kick_rdev_from_array(rdev);
3321                                 continue;
3322                         }
3323                 if (mddev->level == LEVEL_MULTIPATH) {
3324                         rdev->desc_nr = i++;
3325                         rdev->raid_disk = rdev->desc_nr;
3326                         set_bit(In_sync, &rdev->flags);
3327                 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
3328                         rdev->raid_disk = -1;
3329                         clear_bit(In_sync, &rdev->flags);
3330                 }
3331         }
3332 }
3333
3334 /* Read a fixed-point number.
3335  * Numbers in sysfs attributes should be in "standard" units where
3336  * possible, so time should be in seconds.
3337  * However we internally use a a much smaller unit such as 
3338  * milliseconds or jiffies.
3339  * This function takes a decimal number with a possible fractional
3340  * component, and produces an integer which is the result of
3341  * multiplying that number by 10^'scale'.
3342  * all without any floating-point arithmetic.
3343  */
3344 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3345 {
3346         unsigned long result = 0;
3347         long decimals = -1;
3348         while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3349                 if (*cp == '.')
3350                         decimals = 0;
3351                 else if (decimals < scale) {
3352                         unsigned int value;
3353                         value = *cp - '0';
3354                         result = result * 10 + value;
3355                         if (decimals >= 0)
3356                                 decimals++;
3357                 }
3358                 cp++;
3359         }
3360         if (*cp == '\n')
3361                 cp++;
3362         if (*cp)
3363                 return -EINVAL;
3364         if (decimals < 0)
3365                 decimals = 0;
3366         while (decimals < scale) {
3367                 result *= 10;
3368                 decimals ++;
3369         }
3370         *res = result;
3371         return 0;
3372 }
3373
3374
3375 static void md_safemode_timeout(unsigned long data);
3376
3377 static ssize_t
3378 safe_delay_show(struct mddev *mddev, char *page)
3379 {
3380         int msec = (mddev->safemode_delay*1000)/HZ;
3381         return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3382 }
3383 static ssize_t
3384 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3385 {
3386         unsigned long msec;
3387
3388         if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3389                 return -EINVAL;
3390         if (msec == 0)
3391                 mddev->safemode_delay = 0;
3392         else {
3393                 unsigned long old_delay = mddev->safemode_delay;
3394                 mddev->safemode_delay = (msec*HZ)/1000;
3395                 if (mddev->safemode_delay == 0)
3396                         mddev->safemode_delay = 1;
3397                 if (mddev->safemode_delay < old_delay || old_delay == 0)
3398                         md_safemode_timeout((unsigned long)mddev);
3399         }
3400         return len;
3401 }
3402 static struct md_sysfs_entry md_safe_delay =
3403 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3404
3405 static ssize_t
3406 level_show(struct mddev *mddev, char *page)
3407 {
3408         struct md_personality *p = mddev->pers;
3409         if (p)
3410                 return sprintf(page, "%s\n", p->name);
3411         else if (mddev->clevel[0])
3412                 return sprintf(page, "%s\n", mddev->clevel);
3413         else if (mddev->level != LEVEL_NONE)
3414                 return sprintf(page, "%d\n", mddev->level);
3415         else
3416                 return 0;
3417 }
3418
3419 static ssize_t
3420 level_store(struct mddev *mddev, const char *buf, size_t len)
3421 {
3422         char clevel[16];
3423         ssize_t rv = len;
3424         struct md_personality *pers;
3425         long level;
3426         void *priv;
3427         struct md_rdev *rdev;
3428
3429         if (mddev->pers == NULL) {
3430                 if (len == 0)
3431                         return 0;
3432                 if (len >= sizeof(mddev->clevel))
3433                         return -ENOSPC;
3434                 strncpy(mddev->clevel, buf, len);
3435                 if (mddev->clevel[len-1] == '\n')
3436                         len--;
3437                 mddev->clevel[len] = 0;
3438                 mddev->level = LEVEL_NONE;
3439                 return rv;
3440         }
3441
3442         /* request to change the personality.  Need to ensure:
3443          *  - array is not engaged in resync/recovery/reshape
3444          *  - old personality can be suspended
3445          *  - new personality will access other array.
3446          */
3447
3448         if (mddev->sync_thread ||
3449             mddev->reshape_position != MaxSector ||
3450             mddev->sysfs_active)
3451                 return -EBUSY;
3452
3453         if (!mddev->pers->quiesce) {
3454                 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3455                        mdname(mddev), mddev->pers->name);
3456                 return -EINVAL;
3457         }
3458
3459         /* Now find the new personality */
3460         if (len == 0 || len >= sizeof(clevel))
3461                 return -EINVAL;
3462         strncpy(clevel, buf, len);
3463         if (clevel[len-1] == '\n')
3464                 len--;
3465         clevel[len] = 0;
3466         if (kstrtol(clevel, 10, &level))
3467                 level = LEVEL_NONE;
3468
3469         if (request_module("md-%s", clevel) != 0)
3470                 request_module("md-level-%s", clevel);
3471         spin_lock(&pers_lock);
3472         pers = find_pers(level, clevel);
3473         if (!pers || !try_module_get(pers->owner)) {
3474                 spin_unlock(&pers_lock);
3475                 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
3476                 return -EINVAL;
3477         }
3478         spin_unlock(&pers_lock);
3479
3480         if (pers == mddev->pers) {
3481                 /* Nothing to do! */
3482                 module_put(pers->owner);
3483                 return rv;
3484         }
3485         if (!pers->takeover) {
3486                 module_put(pers->owner);
3487                 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
3488                        mdname(mddev), clevel);
3489                 return -EINVAL;
3490         }
3491
3492         rdev_for_each(rdev, mddev)
3493                 rdev->new_raid_disk = rdev->raid_disk;
3494
3495         /* ->takeover must set new_* and/or delta_disks
3496          * if it succeeds, and may set them when it fails.
3497          */
3498         priv = pers->takeover(mddev);
3499         if (IS_ERR(priv)) {
3500                 mddev->new_level = mddev->level;
3501                 mddev->new_layout = mddev->layout;
3502                 mddev->new_chunk_sectors = mddev->chunk_sectors;
3503                 mddev->raid_disks -= mddev->delta_disks;
3504                 mddev->delta_disks = 0;
3505                 mddev->reshape_backwards = 0;
3506                 module_put(pers->owner);
3507                 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3508                        mdname(mddev), clevel);
3509                 return PTR_ERR(priv);
3510         }
3511
3512         /* Looks like we have a winner */
3513         mddev_suspend(mddev);
3514         mddev->pers->stop(mddev);
3515         
3516         if (mddev->pers->sync_request == NULL &&
3517             pers->sync_request != NULL) {
3518                 /* need to add the md_redundancy_group */
3519                 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3520                         printk(KERN_WARNING
3521                                "md: cannot register extra attributes for %s\n",
3522                                mdname(mddev));
3523                 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3524         }               
3525         if (mddev->pers->sync_request != NULL &&
3526             pers->sync_request == NULL) {
3527                 /* need to remove the md_redundancy_group */
3528                 if (mddev->to_remove == NULL)
3529                         mddev->to_remove = &md_redundancy_group;
3530         }
3531
3532         if (mddev->pers->sync_request == NULL &&
3533             mddev->external) {
3534                 /* We are converting from a no-redundancy array
3535                  * to a redundancy array and metadata is managed
3536                  * externally so we need to be sure that writes
3537                  * won't block due to a need to transition
3538                  *      clean->dirty
3539                  * until external management is started.
3540                  */
3541                 mddev->in_sync = 0;
3542                 mddev->safemode_delay = 0;
3543                 mddev->safemode = 0;
3544         }
3545
3546         rdev_for_each(rdev, mddev) {
3547                 if (rdev->raid_disk < 0)
3548                         continue;
3549                 if (rdev->new_raid_disk >= mddev->raid_disks)
3550                         rdev->new_raid_disk = -1;
3551                 if (rdev->new_raid_disk == rdev->raid_disk)
3552                         continue;
3553                 sysfs_unlink_rdev(mddev, rdev);
3554         }
3555         rdev_for_each(rdev, mddev) {
3556                 if (rdev->raid_disk < 0)
3557                         continue;
3558                 if (rdev->new_raid_disk == rdev->raid_disk)
3559                         continue;
3560                 rdev->raid_disk = rdev->new_raid_disk;
3561                 if (rdev->raid_disk < 0)
3562                         clear_bit(In_sync, &rdev->flags);
3563                 else {
3564                         if (sysfs_link_rdev(mddev, rdev))
3565                                 printk(KERN_WARNING "md: cannot register rd%d"
3566                                        " for %s after level change\n",
3567                                        rdev->raid_disk, mdname(mddev));
3568                 }
3569         }
3570
3571         module_put(mddev->pers->owner);
3572         mddev->pers = pers;
3573         mddev->private = priv;
3574         strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3575         mddev->level = mddev->new_level;
3576         mddev->layout = mddev->new_layout;
3577         mddev->chunk_sectors = mddev->new_chunk_sectors;
3578         mddev->delta_disks = 0;
3579         mddev->reshape_backwards = 0;
3580         mddev->degraded = 0;
3581         if (mddev->pers->sync_request == NULL) {
3582                 /* this is now an array without redundancy, so
3583                  * it must always be in_sync
3584                  */
3585                 mddev->in_sync = 1;
3586                 del_timer_sync(&mddev->safemode_timer);
3587         }
3588         blk_set_stacking_limits(&mddev->queue->limits);
3589         pers->run(mddev);
3590         set_bit(MD_CHANGE_DEVS, &mddev->flags);
3591         mddev_resume(mddev);
3592         sysfs_notify(&mddev->kobj, NULL, "level");
3593         md_new_event(mddev);
3594         return rv;
3595 }
3596
3597 static struct md_sysfs_entry md_level =
3598 __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
3599
3600
3601 static ssize_t
3602 layout_show(struct mddev *mddev, char *page)
3603 {
3604         /* just a number, not meaningful for all levels */
3605         if (mddev->reshape_position != MaxSector &&
3606             mddev->layout != mddev->new_layout)
3607                 return sprintf(page, "%d (%d)\n",
3608                                mddev->new_layout, mddev->layout);
3609         return sprintf(page, "%d\n", mddev->layout);
3610 }
3611
3612 static ssize_t
3613 layout_store(struct mddev *mddev, const char *buf, size_t len)
3614 {
3615         char *e;
3616         unsigned long n = simple_strtoul(buf, &e, 10);
3617
3618         if (!*buf || (*e && *e != '\n'))
3619                 return -EINVAL;
3620
3621         if (mddev->pers) {
3622                 int err;
3623                 if (mddev->pers->check_reshape == NULL)
3624                         return -EBUSY;
3625                 mddev->new_layout = n;
3626                 err = mddev->pers->check_reshape(mddev);
3627                 if (err) {
3628                         mddev->new_layout = mddev->layout;
3629                         return err;
3630                 }
3631         } else {
3632                 mddev->new_layout = n;
3633                 if (mddev->reshape_position == MaxSector)
3634                         mddev->layout = n;
3635         }
3636         return len;
3637 }
3638 static struct md_sysfs_entry md_layout =
3639 __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
3640
3641
3642 static ssize_t
3643 raid_disks_show(struct mddev *mddev, char *page)
3644 {
3645         if (mddev->raid_disks == 0)
3646                 return 0;
3647         if (mddev->reshape_position != MaxSector &&
3648             mddev->delta_disks != 0)
3649                 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3650                                mddev->raid_disks - mddev->delta_disks);
3651         return sprintf(page, "%d\n", mddev->raid_disks);
3652 }
3653
3654 static int update_raid_disks(struct mddev *mddev, int raid_disks);
3655
3656 static ssize_t
3657 raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
3658 {
3659         char *e;
3660         int rv = 0;
3661         unsigned long n = simple_strtoul(buf, &e, 10);
3662
3663         if (!*buf || (*e && *e != '\n'))
3664                 return -EINVAL;
3665
3666         if (mddev->pers)
3667                 rv = update_raid_disks(mddev, n);
3668         else if (mddev->reshape_position != MaxSector) {
3669                 struct md_rdev *rdev;
3670                 int olddisks = mddev->raid_disks - mddev->delta_disks;
3671
3672                 rdev_for_each(rdev, mddev) {
3673                         if (olddisks < n &&
3674                             rdev->data_offset < rdev->new_data_offset)
3675                                 return -EINVAL;
3676                         if (olddisks > n &&
3677                             rdev->data_offset > rdev->new_data_offset)
3678                                 return -EINVAL;
3679                 }
3680                 mddev->delta_disks = n - olddisks;
3681                 mddev->raid_disks = n;
3682                 mddev->reshape_backwards = (mddev->delta_disks < 0);
3683         } else
3684                 mddev->raid_disks = n;
3685         return rv ? rv : len;
3686 }
3687 static struct md_sysfs_entry md_raid_disks =
3688 __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
3689
3690 static ssize_t
3691 chunk_size_show(struct mddev *mddev, char *page)
3692 {
3693         if (mddev->reshape_position != MaxSector &&
3694             mddev->chunk_sectors != mddev->new_chunk_sectors)
3695                 return sprintf(page, "%d (%d)\n",
3696                                mddev->new_chunk_sectors << 9,
3697                                mddev->chunk_sectors << 9);
3698         return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
3699 }
3700
3701 static ssize_t
3702 chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
3703 {
3704         char *e;
3705         unsigned long n = simple_strtoul(buf, &e, 10);
3706
3707         if (!*buf || (*e && *e != '\n'))
3708                 return -EINVAL;
3709
3710         if (mddev->pers) {
3711                 int err;
3712                 if (mddev->pers->check_reshape == NULL)
3713                         return -EBUSY;
3714                 mddev->new_chunk_sectors = n >> 9;
3715                 err = mddev->pers->check_reshape(mddev);
3716                 if (err) {
3717                         mddev->new_chunk_sectors = mddev->chunk_sectors;
3718                         return err;
3719                 }
3720         } else {
3721                 mddev->new_chunk_sectors = n >> 9;
3722                 if (mddev->reshape_position == MaxSector)
3723                         mddev->chunk_sectors = n >> 9;
3724         }
3725         return len;
3726 }
3727 static struct md_sysfs_entry md_chunk_size =
3728 __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
3729
3730 static ssize_t
3731 resync_start_show(struct mddev *mddev, char *page)
3732 {
3733         if (mddev->recovery_cp == MaxSector)
3734                 return sprintf(page, "none\n");
3735         return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3736 }
3737
3738 static ssize_t
3739 resync_start_store(struct mddev *mddev, const char *buf, size_t len)
3740 {
3741         char *e;
3742         unsigned long long n = simple_strtoull(buf, &e, 10);
3743
3744         if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3745                 return -EBUSY;
3746         if (cmd_match(buf, "none"))
3747                 n = MaxSector;
3748         else if (!*buf || (*e && *e != '\n'))
3749                 return -EINVAL;
3750
3751         mddev->recovery_cp = n;
3752         if (mddev->pers)
3753                 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3754         return len;
3755 }
3756 static struct md_sysfs_entry md_resync_start =
3757 __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
3758
3759 /*
3760  * The array state can be:
3761  *
3762  * clear
3763  *     No devices, no size, no level
3764  *     Equivalent to STOP_ARRAY ioctl
3765  * inactive
3766  *     May have some settings, but array is not active
3767  *        all IO results in error
3768  *     When written, doesn't tear down array, but just stops it
3769  * suspended (not supported yet)
3770  *     All IO requests will block. The array can be reconfigured.
3771  *     Writing this, if accepted, will block until array is quiescent
3772  * readonly
3773  *     no resync can happen.  no superblocks get written.
3774  *     write requests fail
3775  * read-auto
3776  *     like readonly, but behaves like 'clean' on a write request.
3777  *
3778  * clean - no pending writes, but otherwise active.
3779  *     When written to inactive array, starts without resync
3780  *     If a write request arrives then
3781  *       if metadata is known, mark 'dirty' and switch to 'active'.
3782  *       if not known, block and switch to write-pending
3783  *     If written to an active array that has pending writes, then fails.
3784  * active
3785  *     fully active: IO and resync can be happening.
3786  *     When written to inactive array, starts with resync
3787  *
3788  * write-pending
3789  *     clean, but writes are blocked waiting for 'active' to be written.
3790  *
3791  * active-idle
3792  *     like active, but no writes have been seen for a while (100msec).
3793  *
3794  */
3795 enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3796                    write_pending, active_idle, bad_word};
3797 static char *array_states[] = {
3798         "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3799         "write-pending", "active-idle", NULL };
3800
3801 static int match_word(const char *word, char **list)
3802 {
3803         int n;
3804         for (n=0; list[n]; n++)
3805                 if (cmd_match(word, list[n]))
3806                         break;
3807         return n;
3808 }
3809
3810 static ssize_t
3811 array_state_show(struct mddev *mddev, char *page)
3812 {
3813         enum array_state st = inactive;
3814
3815         if (mddev->pers)
3816                 switch(mddev->ro) {
3817                 case 1:
3818                         st = readonly;
3819                         break;
3820                 case 2:
3821                         st = read_auto;
3822                         break;
3823                 case 0:
3824                         if (mddev->in_sync)
3825                                 st = clean;
3826                         else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
3827                                 st = write_pending;
3828                         else if (mddev->safemode)
3829                                 st = active_idle;
3830                         else
3831                                 st = active;
3832                 }
3833         else {
3834                 if (list_empty(&mddev->disks) &&
3835                     mddev->raid_disks == 0 &&
3836                     mddev->dev_sectors == 0)
3837                         st = clear;
3838                 else
3839                         st = inactive;
3840         }
3841         return sprintf(page, "%s\n", array_states[st]);
3842 }
3843
3844 static int do_md_stop(struct mddev * mddev, int ro, struct block_device *bdev);
3845 static int md_set_readonly(struct mddev * mddev, struct block_device *bdev);
3846 static int do_md_run(struct mddev * mddev);
3847 static int restart_array(struct mddev *mddev);
3848
3849 static ssize_t
3850 array_state_store(struct mddev *mddev, const char *buf, size_t len)
3851 {
3852         int err = -EINVAL;
3853         enum array_state st = match_word(buf, array_states);
3854         switch(st) {
3855         case bad_word:
3856                 break;
3857         case clear:
3858                 /* stopping an active array */
3859                 err = do_md_stop(mddev, 0, NULL);
3860                 break;
3861         case inactive:
3862                 /* stopping an active array */
3863                 if (mddev->pers)
3864                         err = do_md_stop(mddev, 2, NULL);
3865                 else
3866                         err = 0; /* already inactive */
3867                 break;
3868         case suspended:
3869                 break; /* not supported yet */
3870         case readonly:
3871                 if (mddev->pers)
3872                         err = md_set_readonly(mddev, NULL);
3873                 else {
3874                         mddev->ro = 1;
3875                         set_disk_ro(mddev->gendisk, 1);
3876                         err = do_md_run(mddev);
3877                 }
3878                 break;
3879         case read_auto:
3880                 if (mddev->pers) {
3881                         if (mddev->ro == 0)
3882                                 err = md_set_readonly(mddev, NULL);
3883                         else if (mddev->ro == 1)
3884                                 err = restart_array(mddev);
3885                         if (err == 0) {
3886                                 mddev->ro = 2;
3887                                 set_disk_ro(mddev->gendisk, 0);
3888                         }
3889                 } else {
3890                         mddev->ro = 2;
3891                         err = do_md_run(mddev);
3892                 }
3893                 break;
3894         case clean:
3895                 if (mddev->pers) {
3896                         restart_array(mddev);
3897                         spin_lock_irq(&mddev->write_lock);
3898                         if (atomic_read(&mddev->writes_pending) == 0) {
3899                                 if (mddev->in_sync == 0) {
3900                                         mddev->in_sync = 1;
3901                                         if (mddev->safemode == 1)
3902                                                 mddev->safemode = 0;
3903                                         set_bit(MD_CHANGE_CLEAN, &mddev->flags);
3904                                 }
3905                                 err = 0;
3906                         } else
3907                                 err = -EBUSY;
3908                         spin_unlock_irq(&mddev->write_lock);
3909                 } else
3910                         err = -EINVAL;
3911                 break;
3912         case active:
3913                 if (mddev->pers) {
3914                         restart_array(mddev);
3915                         clear_bit(MD_CHANGE_PENDING, &mddev->flags);
3916                         wake_up(&mddev->sb_wait);
3917                         err = 0;
3918                 } else {
3919                         mddev->ro = 0;
3920                         set_disk_ro(mddev->gendisk, 0);
3921                         err = do_md_run(mddev);
3922                 }
3923                 break;
3924         case write_pending:
3925         case active_idle:
3926                 /* these cannot be set */
3927                 break;
3928         }
3929         if (err)
3930                 return err;
3931         else {
3932                 if (mddev->hold_active == UNTIL_IOCTL)
3933                         mddev->hold_active = 0;
3934                 sysfs_notify_dirent_safe(mddev->sysfs_state);
3935                 return len;
3936         }
3937 }
3938 static struct md_sysfs_entry md_array_state =
3939 __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
3940
3941 static ssize_t
3942 max_corrected_read_errors_show(struct mddev *mddev, char *page) {
3943         return sprintf(page, "%d\n",
3944                        atomic_read(&mddev->max_corr_read_errors));
3945 }
3946
3947 static ssize_t
3948 max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
3949 {
3950         char *e;
3951         unsigned long n = simple_strtoul(buf, &e, 10);
3952
3953         if (*buf && (*e == 0 || *e == '\n')) {
3954                 atomic_set(&mddev->max_corr_read_errors, n);
3955                 return len;
3956         }
3957         return -EINVAL;
3958 }
3959
3960 static struct md_sysfs_entry max_corr_read_errors =
3961 __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3962         max_corrected_read_errors_store);
3963
3964 static ssize_t
3965 null_show(struct mddev *mddev, char *page)
3966 {
3967         return -EINVAL;
3968 }
3969
3970 static ssize_t
3971 new_dev_store(struct mddev *mddev, const char *buf, size_t len)
3972 {
3973         /* buf must be %d:%d\n? giving major and minor numbers */
3974         /* The new device is added to the array.
3975          * If the array has a persistent superblock, we read the
3976          * superblock to initialise info and check validity.
3977          * Otherwise, only checking done is that in bind_rdev_to_array,
3978          * which mainly checks size.
3979          */
3980         char *e;
3981         int major = simple_strtoul(buf, &e, 10);
3982         int minor;
3983         dev_t dev;
3984         struct md_rdev *rdev;
3985         int err;
3986
3987         if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3988                 return -EINVAL;
3989         minor = simple_strtoul(e+1, &e, 10);
3990         if (*e && *e != '\n')
3991                 return -EINVAL;
3992         dev = MKDEV(major, minor);
3993         if (major != MAJOR(dev) ||
3994             minor != MINOR(dev))
3995                 return -EOVERFLOW;
3996
3997
3998         if (mddev->persistent) {
3999                 rdev = md_import_device(dev, mddev->major_version,
4000                                         mddev->minor_version);
4001                 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
4002                         struct md_rdev *rdev0
4003                                 = list_entry(mddev->disks.next,
4004                                              struct md_rdev, same_set);
4005                         err = super_types[mddev->major_version]
4006                                 .load_super(rdev, rdev0, mddev->minor_version);
4007                         if (err < 0)
4008                                 goto out;
4009                 }
4010         } else if (mddev->external)
4011                 rdev = md_import_device(dev, -2, -1);
4012         else
4013                 rdev = md_import_device(dev, -1, -1);
4014
4015         if (IS_ERR(rdev))
4016                 return PTR_ERR(rdev);
4017         err = bind_rdev_to_array(rdev, mddev);
4018  out:
4019         if (err)
4020                 export_rdev(rdev);
4021         return err ? err : len;
4022 }
4023
4024 static struct md_sysfs_entry md_new_device =
4025 __ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
4026
4027 static ssize_t
4028 bitmap_store(struct mddev *mddev, const char *buf, size_t len)
4029 {
4030         char *end;
4031         unsigned long chunk, end_chunk;
4032
4033         if (!mddev->bitmap)
4034                 goto out;
4035         /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
4036         while (*buf) {
4037                 chunk = end_chunk = simple_strtoul(buf, &end, 0);
4038                 if (buf == end) break;
4039                 if (*end == '-') { /* range */
4040                         buf = end + 1;
4041                         end_chunk = simple_strtoul(buf, &end, 0);
4042                         if (buf == end) break;
4043                 }
4044                 if (*end && !isspace(*end)) break;
4045                 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
4046                 buf = skip_spaces(end);
4047         }
4048         bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
4049 out:
4050         return len;
4051 }
4052
4053 static struct md_sysfs_entry md_bitmap =
4054 __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
4055
4056 static ssize_t
4057 size_show(struct mddev *mddev, char *page)
4058 {
4059         return sprintf(page, "%llu\n",
4060                 (unsigned long long)mddev->dev_sectors / 2);
4061 }
4062
4063 static int update_size(struct mddev *mddev, sector_t num_sectors);
4064
4065 static ssize_t
4066 size_store(struct mddev *mddev, const char *buf, size_t len)
4067 {
4068         /* If array is inactive, we can reduce the component size, but
4069          * not increase it (except from 0).
4070          * If array is active, we can try an on-line resize
4071          */
4072         sector_t sectors;
4073         int err = strict_blocks_to_sectors(buf, &sectors);
4074
4075         if (err < 0)
4076                 return err;
4077         if (mddev->pers) {
4078                 err = update_size(mddev, sectors);
4079                 md_update_sb(mddev, 1);
4080         } else {
4081                 if (mddev->dev_sectors == 0 ||
4082                     mddev->dev_sectors > sectors)
4083                         mddev->dev_sectors = sectors;
4084                 else
4085                         err = -ENOSPC;
4086         }
4087         return err ? err : len;
4088 }
4089
4090 static struct md_sysfs_entry md_size =
4091 __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
4092
4093
4094 /* Metadata version.
4095  * This is one of
4096  *   'none' for arrays with no metadata (good luck...)
4097  *   'external' for arrays with externally managed metadata,
4098  * or N.M for internally known formats
4099  */
4100 static ssize_t
4101 metadata_show(struct mddev *mddev, char *page)
4102 {
4103         if (mddev->persistent)
4104                 return sprintf(page, "%d.%d\n",
4105                                mddev->major_version, mddev->minor_version);
4106         else if (mddev->external)
4107                 return sprintf(page, "external:%s\n", mddev->metadata_type);
4108         else
4109                 return sprintf(page, "none\n");
4110 }
4111
4112 static ssize_t
4113 metadata_store(struct mddev *mddev, const char *buf, size_t len)
4114 {
4115         int major, minor;
4116         char *e;
4117         /* Changing the details of 'external' metadata is
4118          * always permitted.  Otherwise there must be
4119          * no devices attached to the array.
4120          */
4121         if (mddev->external && strncmp(buf, "external:", 9) == 0)
4122                 ;
4123         else if (!list_empty(&mddev->disks))
4124                 return -EBUSY;
4125
4126         if (cmd_match(buf, "none")) {
4127                 mddev->persistent = 0;
4128                 mddev->external = 0;
4129                 mddev->major_version = 0;
4130                 mddev->minor_version = 90;
4131                 return len;
4132         }
4133         if (strncmp(buf, "external:", 9) == 0) {
4134                 size_t namelen = len-9;
4135                 if (namelen >= sizeof(mddev->metadata_type))
4136                         namelen = sizeof(mddev->metadata_type)-1;
4137                 strncpy(mddev->metadata_type, buf+9, namelen);
4138                 mddev->metadata_type[namelen] = 0;
4139                 if (namelen && mddev->metadata_type[namelen-1] == '\n')
4140                         mddev->metadata_type[--namelen] = 0;
4141                 mddev->persistent = 0;
4142                 mddev->external = 1;
4143                 mddev->major_version = 0;
4144                 mddev->minor_version = 90;
4145                 return len;
4146         }
4147         major = simple_strtoul(buf, &e, 10);
4148         if (e==buf || *e != '.')
4149                 return -EINVAL;
4150         buf = e+1;
4151         minor = simple_strtoul(buf, &e, 10);
4152         if (e==buf || (*e && *e != '\n') )
4153                 return -EINVAL;
4154         if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
4155                 return -ENOENT;
4156         mddev->major_version = major;
4157         mddev->minor_version = minor;
4158         mddev->persistent = 1;
4159         mddev->external = 0;
4160         return len;
4161 }
4162
4163 static struct md_sysfs_entry md_metadata =
4164 __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
4165
4166 static ssize_t
4167 action_show(struct mddev *mddev, char *page)
4168 {
4169         char *type = "idle";
4170         if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4171                 type = "frozen";
4172         else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4173             (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
4174                 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4175                         type = "reshape";
4176                 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4177                         if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4178                                 type = "resync";
4179                         else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4180                                 type = "check";
4181                         else
4182                                 type = "repair";
4183                 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
4184                         type = "recover";
4185         }
4186         return sprintf(page, "%s\n", type);
4187 }
4188
4189 static ssize_t
4190 action_store(struct mddev *mddev, const char *page, size_t len)
4191 {
4192         if (!mddev->pers || !mddev->pers->sync_request)
4193                 return -EINVAL;
4194
4195         if (cmd_match(page, "frozen"))
4196                 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4197         else
4198                 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4199
4200         if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
4201                 if (mddev->sync_thread) {
4202                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4203                         md_reap_sync_thread(mddev);
4204                 }
4205         } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4206                    test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
4207                 return -EBUSY;
4208         else if (cmd_match(page, "resync"))
4209                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4210         else if (cmd_match(page, "recover")) {
4211                 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4212                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4213         } else if (cmd_match(page, "reshape")) {
4214                 int err;
4215                 if (mddev->pers->start_reshape == NULL)
4216                         return -EINVAL;
4217                 err = mddev->pers->start_reshape(mddev);
4218                 if (err)
4219                         return err;
4220                 sysfs_notify(&mddev->kobj, NULL, "degraded");
4221         } else {
4222                 if (cmd_match(page, "check"))
4223                         set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4224                 else if (!cmd_match(page, "repair"))
4225                         return -EINVAL;
4226                 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4227                 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4228         }
4229         if (mddev->ro == 2) {
4230                 /* A write to sync_action is enough to justify
4231                  * canceling read-auto mode
4232                  */
4233                 mddev->ro = 0;
4234                 md_wakeup_thread(mddev->sync_thread);
4235         }
4236         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4237         md_wakeup_thread(mddev->thread);
4238         sysfs_notify_dirent_safe(mddev->sysfs_action);
4239         return len;
4240 }
4241
4242 static struct md_sysfs_entry md_scan_mode =
4243 __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
4244
4245 static ssize_t
4246 last_sync_action_show(struct mddev *mddev, char *page)
4247 {
4248         return sprintf(page, "%s\n", mddev->last_sync_action);
4249 }
4250
4251 static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action);
4252
4253 static ssize_t
4254 mismatch_cnt_show(struct mddev *mddev, char *page)
4255 {
4256         return sprintf(page, "%llu\n",
4257                        (unsigned long long)
4258                        atomic64_read(&mddev->resync_mismatches));
4259 }
4260
4261 static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
4262
4263 static ssize_t
4264 sync_min_show(struct mddev *mddev, char *page)
4265 {
4266         return sprintf(page, "%d (%s)\n", speed_min(mddev),
4267                        mddev->sync_speed_min ? "local": "system");
4268 }
4269
4270 static ssize_t
4271 sync_min_store(struct mddev *mddev, const char *buf, size_t len)
4272 {
4273         int min;
4274         char *e;
4275         if (strncmp(buf, "system", 6)==0) {
4276                 mddev->sync_speed_min = 0;
4277                 return len;
4278         }
4279         min = simple_strtoul(buf, &e, 10);
4280         if (buf == e || (*e && *e != '\n') || min <= 0)
4281                 return -EINVAL;
4282         mddev->sync_speed_min = min;
4283         return len;
4284 }
4285
4286 static struct md_sysfs_entry md_sync_min =
4287 __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4288
4289 static ssize_t
4290 sync_max_show(struct mddev *mddev, char *page)
4291 {
4292         return sprintf(page, "%d (%s)\n", speed_max(mddev),
4293                        mddev->sync_speed_max ? "local": "system");
4294 }
4295
4296 static ssize_t
4297 sync_max_store(struct mddev *mddev, const char *buf, size_t len)
4298 {
4299         int max;
4300         char *e;
4301         if (strncmp(buf, "system", 6)==0) {
4302                 mddev->sync_speed_max = 0;
4303                 return len;
4304         }
4305         max = simple_strtoul(buf, &e, 10);
4306         if (buf == e || (*e && *e != '\n') || max <= 0)
4307                 return -EINVAL;
4308         mddev->sync_speed_max = max;
4309         return len;
4310 }
4311
4312 static struct md_sysfs_entry md_sync_max =
4313 __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4314
4315 static ssize_t
4316 degraded_show(struct mddev *mddev, char *page)
4317 {
4318         return sprintf(page, "%d\n", mddev->degraded);
4319 }
4320 static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
4321
4322 static ssize_t
4323 sync_force_parallel_show(struct mddev *mddev, char *page)
4324 {
4325         return sprintf(page, "%d\n", mddev->parallel_resync);
4326 }
4327
4328 static ssize_t
4329 sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
4330 {
4331         long n;
4332
4333         if (kstrtol(buf, 10, &n))
4334                 return -EINVAL;
4335
4336         if (n != 0 && n != 1)
4337                 return -EINVAL;
4338
4339         mddev->parallel_resync = n;
4340
4341         if (mddev->sync_thread)
4342                 wake_up(&resync_wait);
4343
4344         return len;
4345 }
4346
4347 /* force parallel resync, even with shared block devices */
4348 static struct md_sysfs_entry md_sync_force_parallel =
4349 __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4350        sync_force_parallel_show, sync_force_parallel_store);
4351
4352 static ssize_t
4353 sync_speed_show(struct mddev *mddev, char *page)
4354 {
4355         unsigned long resync, dt, db;
4356         if (mddev->curr_resync == 0)
4357                 return sprintf(page, "none\n");
4358         resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4359         dt = (jiffies - mddev->resync_mark) / HZ;
4360         if (!dt) dt++;
4361         db = resync - mddev->resync_mark_cnt;
4362         return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
4363 }
4364
4365 static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
4366
4367 static ssize_t
4368 sync_completed_show(struct mddev *mddev, char *page)
4369 {
4370         unsigned long long max_sectors, resync;
4371
4372         if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4373                 return sprintf(page, "none\n");
4374
4375         if (mddev->curr_resync == 1 ||
4376             mddev->curr_resync == 2)
4377                 return sprintf(page, "delayed\n");
4378
4379         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
4380             test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4381                 max_sectors = mddev->resync_max_sectors;
4382         else
4383                 max_sectors = mddev->dev_sectors;
4384
4385         resync = mddev->curr_resync_completed;
4386         return sprintf(page, "%llu / %llu\n", resync, max_sectors);
4387 }
4388
4389 static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
4390
4391 static ssize_t
4392 min_sync_show(struct mddev *mddev, char *page)
4393 {
4394         return sprintf(page, "%llu\n",
4395                        (unsigned long long)mddev->resync_min);
4396 }
4397 static ssize_t
4398 min_sync_store(struct mddev *mddev, const char *buf, size_t len)
4399 {
4400         unsigned long long min;
4401         if (kstrtoull(buf, 10, &min))
4402                 return -EINVAL;
4403         if (min > mddev->resync_max)
4404                 return -EINVAL;
4405         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4406                 return -EBUSY;
4407
4408         /* Must be a multiple of chunk_size */
4409         if (mddev->chunk_sectors) {
4410                 sector_t temp = min;
4411                 if (sector_div(temp, mddev->chunk_sectors))
4412                         return -EINVAL;
4413         }
4414         mddev->resync_min = min;
4415
4416         return len;
4417 }
4418
4419 static struct md_sysfs_entry md_min_sync =
4420 __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4421
4422 static ssize_t
4423 max_sync_show(struct mddev *mddev, char *page)
4424 {
4425         if (mddev->resync_max == MaxSector)
4426                 return sprintf(page, "max\n");
4427         else
4428                 return sprintf(page, "%llu\n",
4429                                (unsigned long long)mddev->resync_max);
4430 }
4431 static ssize_t
4432 max_sync_store(struct mddev *mddev, const char *buf, size_t len)
4433 {
4434         if (strncmp(buf, "max", 3) == 0)
4435                 mddev->resync_max = MaxSector;
4436         else {
4437                 unsigned long long max;
4438                 if (kstrtoull(buf, 10, &max))
4439                         return -EINVAL;
4440                 if (max < mddev->resync_min)
4441                         return -EINVAL;
4442                 if (max < mddev->resync_max &&
4443                     mddev->ro == 0 &&
4444                     test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4445                         return -EBUSY;
4446
4447                 /* Must be a multiple of chunk_size */
4448                 if (mddev->chunk_sectors) {
4449                         sector_t temp = max;
4450                         if (sector_div(temp, mddev->chunk_sectors))
4451                                 return -EINVAL;
4452                 }
4453                 mddev->resync_max = max;
4454         }
4455         wake_up(&mddev->recovery_wait);
4456         return len;
4457 }
4458
4459 static struct md_sysfs_entry md_max_sync =
4460 __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4461
4462 static ssize_t
4463 suspend_lo_show(struct mddev *mddev, char *page)
4464 {
4465         return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4466 }
4467
4468 static ssize_t
4469 suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
4470 {
4471         char *e;
4472         unsigned long long new = simple_strtoull(buf, &e, 10);
4473         unsigned long long old = mddev->suspend_lo;
4474
4475         if (mddev->pers == NULL || 
4476             mddev->pers->quiesce == NULL)
4477                 return -EINVAL;
4478         if (buf == e || (*e && *e != '\n'))
4479                 return -EINVAL;
4480
4481         mddev->suspend_lo = new;
4482         if (new >= old)
4483                 /* Shrinking suspended region */
4484                 mddev->pers->quiesce(mddev, 2);
4485         else {
4486                 /* Expanding suspended region - need to wait */
4487                 mddev->pers->quiesce(mddev, 1);
4488                 mddev->pers->quiesce(mddev, 0);
4489         }
4490         return len;
4491 }
4492 static struct md_sysfs_entry md_suspend_lo =
4493 __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4494
4495
4496 static ssize_t
4497 suspend_hi_show(struct mddev *mddev, char *page)
4498 {
4499         return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4500 }
4501
4502 static ssize_t
4503 suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
4504 {
4505         char *e;
4506         unsigned long long new = simple_strtoull(buf, &e, 10);
4507         unsigned long long old = mddev->suspend_hi;
4508
4509         if (mddev->pers == NULL ||
4510             mddev->pers->quiesce == NULL)
4511                 return -EINVAL;
4512         if (buf == e || (*e && *e != '\n'))
4513                 return -EINVAL;
4514
4515         mddev->suspend_hi = new;
4516         if (new <= old)
4517                 /* Shrinking suspended region */
4518                 mddev->pers->quiesce(mddev, 2);
4519         else {
4520                 /* Expanding suspended region - need to wait */
4521                 mddev->pers->quiesce(mddev, 1);
4522                 mddev->pers->quiesce(mddev, 0);
4523         }
4524         return len;
4525 }
4526 static struct md_sysfs_entry md_suspend_hi =
4527 __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4528
4529 static ssize_t
4530 reshape_position_show(struct mddev *mddev, char *page)
4531 {
4532         if (mddev->reshape_position != MaxSector)
4533                 return sprintf(page, "%llu\n",
4534                                (unsigned long long)mddev->reshape_position);
4535         strcpy(page, "none\n");
4536         return 5;
4537 }
4538
4539 static ssize_t
4540 reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
4541 {
4542         struct md_rdev *rdev;
4543         char *e;
4544         unsigned long long new = simple_strtoull(buf, &e, 10);
4545         if (mddev->pers)
4546                 return -EBUSY;
4547         if (buf == e || (*e && *e != '\n'))
4548                 return -EINVAL;
4549         mddev->reshape_position = new;
4550         mddev->delta_disks = 0;
4551         mddev->reshape_backwards = 0;
4552         mddev->new_level = mddev->level;
4553         mddev->new_layout = mddev->layout;
4554         mddev->new_chunk_sectors = mddev->chunk_sectors;
4555         rdev_for_each(rdev, mddev)
4556                 rdev->new_data_offset = rdev->data_offset;
4557         return len;
4558 }
4559
4560 static struct md_sysfs_entry md_reshape_position =
4561 __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4562        reshape_position_store);
4563
4564 static ssize_t
4565 reshape_direction_show(struct mddev *mddev, char *page)
4566 {
4567         return sprintf(page, "%s\n",
4568                        mddev->reshape_backwards ? "backwards" : "forwards");
4569 }
4570
4571 static ssize_t
4572 reshape_direction_store(struct mddev *mddev, const char *buf, size_t len)
4573 {
4574         int backwards = 0;
4575         if (cmd_match(buf, "forwards"))
4576                 backwards = 0;
4577         else if (cmd_match(buf, "backwards"))
4578                 backwards = 1;
4579         else
4580                 return -EINVAL;
4581         if (mddev->reshape_backwards == backwards)
4582                 return len;
4583
4584         /* check if we are allowed to change */
4585         if (mddev->delta_disks)
4586                 return -EBUSY;
4587
4588         if (mddev->persistent &&
4589             mddev->major_version == 0)
4590                 return -EINVAL;
4591
4592         mddev->reshape_backwards = backwards;
4593         return len;
4594 }
4595
4596 static struct md_sysfs_entry md_reshape_direction =
4597 __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show,
4598        reshape_direction_store);
4599
4600 static ssize_t
4601 array_size_show(struct mddev *mddev, char *page)
4602 {
4603         if (mddev->external_size)
4604                 return sprintf(page, "%llu\n",
4605                                (unsigned long long)mddev->array_sectors/2);
4606         else
4607                 return sprintf(page, "default\n");
4608 }
4609
4610 static ssize_t
4611 array_size_store(struct mddev *mddev, const char *buf, size_t len)
4612 {
4613         sector_t sectors;
4614
4615         if (strncmp(buf, "default", 7) == 0) {
4616                 if (mddev->pers)
4617                         sectors = mddev->pers->size(mddev, 0, 0);
4618                 else
4619                         sectors = mddev->array_sectors;
4620
4621                 mddev->external_size = 0;
4622         } else {
4623                 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4624                         return -EINVAL;
4625                 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
4626                         return -E2BIG;
4627
4628                 mddev->external_size = 1;
4629         }
4630
4631         mddev->array_sectors = sectors;
4632         if (mddev->pers) {
4633                 set_capacity(mddev->gendisk, mddev->array_sectors);
4634                 revalidate_disk(mddev->gendisk);
4635         }
4636         return len;
4637 }
4638
4639 static struct md_sysfs_entry md_array_size =
4640 __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4641        array_size_store);
4642
4643 static struct attribute *md_default_attrs[] = {
4644         &md_level.attr,
4645         &md_layout.attr,
4646         &md_raid_disks.attr,
4647         &md_chunk_size.attr,
4648         &md_size.attr,
4649         &md_resync_start.attr,
4650         &md_metadata.attr,
4651         &md_new_device.attr,
4652         &md_safe_delay.attr,
4653         &md_array_state.attr,
4654         &md_reshape_position.attr,
4655         &md_reshape_direction.attr,
4656         &md_array_size.attr,
4657         &max_corr_read_errors.attr,
4658         NULL,
4659 };
4660
4661 static struct attribute *md_redundancy_attrs[] = {
4662         &md_scan_mode.attr,
4663         &md_last_scan_mode.attr,
4664         &md_mismatches.attr,
4665         &md_sync_min.attr,
4666         &md_sync_max.attr,
4667         &md_sync_speed.attr,
4668         &md_sync_force_parallel.attr,
4669         &md_sync_completed.attr,
4670         &md_min_sync.attr,
4671         &md_max_sync.attr,
4672         &md_suspend_lo.attr,
4673         &md_suspend_hi.attr,
4674         &md_bitmap.attr,
4675         &md_degraded.attr,
4676         NULL,
4677 };
4678 static struct attribute_group md_redundancy_group = {
4679         .name = NULL,
4680         .attrs = md_redundancy_attrs,
4681 };
4682
4683
4684 static ssize_t
4685 md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4686 {
4687         struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4688         struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4689         ssize_t rv;
4690
4691         if (!entry->show)
4692                 return -EIO;
4693         spin_lock(&all_mddevs_lock);
4694         if (list_empty(&mddev->all_mddevs)) {
4695                 spin_unlock(&all_mddevs_lock);
4696                 return -EBUSY;
4697         }
4698         mddev_get(mddev);
4699         spin_unlock(&all_mddevs_lock);
4700
4701         rv = mddev_lock(mddev);
4702         if (!rv) {
4703                 rv = entry->show(mddev, page);
4704                 mddev_unlock(mddev);
4705         }
4706         mddev_put(mddev);
4707         return rv;
4708 }
4709
4710 static ssize_t
4711 md_attr_store(struct kobject *kobj, struct attribute *attr,
4712               const char *page, size_t length)
4713 {
4714         struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4715         struct mddev *mddev = container_of(kobj, struct mddev, kobj);
4716         ssize_t rv;
4717
4718         if (!entry->store)
4719                 return -EIO;
4720         if (!capable(CAP_SYS_ADMIN))
4721                 return -EACCES;
4722         spin_lock(&all_mddevs_lock);
4723         if (list_empty(&mddev->all_mddevs)) {
4724                 spin_unlock(&all_mddevs_lock);
4725                 return -EBUSY;
4726         }
4727         mddev_get(mddev);
4728         spin_unlock(&all_mddevs_lock);
4729         if (entry->store == new_dev_store)
4730                 flush_workqueue(md_misc_wq);
4731         rv = mddev_lock(mddev);
4732         if (!rv) {
4733                 rv = entry->store(mddev, page, length);
4734                 mddev_unlock(mddev);
4735         }
4736         mddev_put(mddev);
4737         return rv;
4738 }
4739
4740 static void md_free(struct kobject *ko)
4741 {
4742         struct mddev *mddev = container_of(ko, struct mddev, kobj);
4743
4744         if (mddev->sysfs_state)
4745                 sysfs_put(mddev->sysfs_state);
4746
4747         if (mddev->gendisk) {
4748                 del_gendisk(mddev->gendisk);
4749                 put_disk(mddev->gendisk);
4750         }
4751         if (mddev->queue)
4752                 blk_cleanup_queue(mddev->queue);
4753
4754         kfree(mddev);
4755 }
4756
4757 static const struct sysfs_ops md_sysfs_ops = {
4758         .show   = md_attr_show,
4759         .store  = md_attr_store,
4760 };
4761 static struct kobj_type md_ktype = {
4762         .release        = md_free,
4763         .sysfs_ops      = &md_sysfs_ops,
4764         .default_attrs  = md_default_attrs,
4765 };
4766
4767 int mdp_major = 0;
4768
4769 static void mddev_delayed_delete(struct work_struct *ws)
4770 {
4771         struct mddev *mddev = container_of(ws, struct mddev, del_work);
4772
4773         sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
4774         kobject_del(&mddev->kobj);
4775         kobject_put(&mddev->kobj);
4776 }
4777
4778 static int md_alloc(dev_t dev, char *name)
4779 {
4780         static DEFINE_MUTEX(disks_mutex);
4781         struct mddev *mddev = mddev_find(dev);
4782         struct gendisk *disk;
4783         int partitioned;
4784         int shift;
4785         int unit;
4786         int error;
4787
4788         if (!mddev)
4789                 return -ENODEV;
4790
4791         partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4792         shift = partitioned ? MdpMinorShift : 0;
4793         unit = MINOR(mddev->unit) >> shift;
4794
4795         /* wait for any previous instance of this device to be
4796          * completely removed (mddev_delayed_delete).
4797          */
4798         flush_workqueue(md_misc_wq);
4799
4800         mutex_lock(&disks_mutex);
4801         error = -EEXIST;
4802         if (mddev->gendisk)
4803                 goto abort;
4804
4805         if (name) {
4806                 /* Need to ensure that 'name' is not a duplicate.
4807                  */
4808                 struct mddev *mddev2;
4809                 spin_lock(&all_mddevs_lock);
4810
4811                 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4812                         if (mddev2->gendisk &&
4813                             strcmp(mddev2->gendisk->disk_name, name) == 0) {
4814                                 spin_unlock(&all_mddevs_lock);
4815                                 goto abort;
4816                         }
4817                 spin_unlock(&all_mddevs_lock);
4818         }
4819
4820         error = -ENOMEM;
4821         mddev->queue = blk_alloc_queue(GFP_KERNEL);
4822         if (!mddev->queue)
4823                 goto abort;
4824         mddev->queue->queuedata = mddev;
4825
4826         blk_queue_make_request(mddev->queue, md_make_request);
4827         blk_set_stacking_limits(&mddev->queue->limits);
4828
4829         disk = alloc_disk(1 << shift);
4830         if (!disk) {
4831                 blk_cleanup_queue(mddev->queue);
4832                 mddev->queue = NULL;
4833                 goto abort;
4834         }
4835         disk->major = MAJOR(mddev->unit);
4836         disk->first_minor = unit << shift;
4837         if (name)
4838                 strcpy(disk->disk_name, name);
4839         else if (partitioned)
4840                 sprintf(disk->disk_name, "md_d%d", unit);
4841         else
4842                 sprintf(disk->disk_name, "md%d", unit);
4843         disk->fops = &md_fops;
4844         disk->private_data = mddev;
4845         disk->queue = mddev->queue;
4846         blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
4847         /* Allow extended partitions.  This makes the
4848          * 'mdp' device redundant, but we can't really
4849          * remove it now.
4850          */
4851         disk->flags |= GENHD_FL_EXT_DEVT;
4852         mddev->gendisk = disk;
4853         /* As soon as we call add_disk(), another thread could get
4854          * through to md_open, so make sure it doesn't get too far
4855          */
4856         mutex_lock(&mddev->open_mutex);
4857         add_disk(disk);
4858
4859         error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4860                                      &disk_to_dev(disk)->kobj, "%s", "md");
4861         if (error) {
4862                 /* This isn't possible, but as kobject_init_and_add is marked
4863                  * __must_check, we must do something with the result
4864                  */
4865                 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4866                        disk->disk_name);
4867                 error = 0;
4868         }
4869         if (mddev->kobj.sd &&
4870             sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4871                 printk(KERN_DEBUG "pointless warning\n");
4872         mutex_unlock(&mddev->open_mutex);
4873  abort:
4874         mutex_unlock(&disks_mutex);
4875         if (!error && mddev->kobj.sd) {
4876                 kobject_uevent(&mddev->kobj, KOBJ_ADD);
4877                 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
4878         }
4879         mddev_put(mddev);
4880         return error;
4881 }
4882
4883 static struct kobject *md_probe(dev_t dev, int *part, void *data)
4884 {
4885         md_alloc(dev, NULL);
4886         return NULL;
4887 }
4888
4889 static int add_named_array(const char *val, struct kernel_param *kp)
4890 {
4891         /* val must be "md_*" where * is not all digits.
4892          * We allocate an array with a large free minor number, and
4893          * set the name to val.  val must not already be an active name.
4894          */
4895         int len = strlen(val);
4896         char buf[DISK_NAME_LEN];
4897
4898         while (len && val[len-1] == '\n')
4899                 len--;
4900         if (len >= DISK_NAME_LEN)
4901                 return -E2BIG;
4902         strlcpy(buf, val, len+1);
4903         if (strncmp(buf, "md_", 3) != 0)
4904                 return -EINVAL;
4905         return md_alloc(0, buf);
4906 }
4907
4908 static void md_safemode_timeout(unsigned long data)
4909 {
4910         struct mddev *mddev = (struct mddev *) data;
4911
4912         if (!atomic_read(&mddev->writes_pending)) {
4913                 mddev->safemode = 1;
4914                 if (mddev->external)
4915                         sysfs_notify_dirent_safe(mddev->sysfs_state);
4916         }
4917         md_wakeup_thread(mddev->thread);
4918 }
4919
4920 static int start_dirty_degraded;
4921
4922 int md_run(struct mddev *mddev)
4923 {
4924         int err;
4925         struct md_rdev *rdev;
4926         struct md_personality *pers;
4927
4928         if (list_empty(&mddev->disks))
4929                 /* cannot run an array with no devices.. */
4930                 return -EINVAL;
4931
4932         if (mddev->pers)
4933                 return -EBUSY;
4934         /* Cannot run until previous stop completes properly */
4935         if (mddev->sysfs_active)
4936                 return -EBUSY;
4937
4938         /*
4939          * Analyze all RAID superblock(s)
4940          */
4941         if (!mddev->raid_disks) {
4942                 if (!mddev->persistent)
4943                         return -EINVAL;
4944                 analyze_sbs(mddev);
4945         }
4946
4947         if (mddev->level != LEVEL_NONE)
4948                 request_module("md-level-%d", mddev->level);
4949         else if (mddev->clevel[0])
4950                 request_module("md-%s", mddev->clevel);
4951
4952         /*
4953          * Drop all container device buffers, from now on
4954          * the only valid external interface is through the md
4955          * device.
4956          */
4957         rdev_for_each(rdev, mddev) {
4958                 if (test_bit(Faulty, &rdev->flags))
4959                         continue;
4960                 sync_blockdev(rdev->bdev);
4961                 invalidate_bdev(rdev->bdev);
4962
4963                 /* perform some consistency tests on the device.
4964                  * We don't want the data to overlap the metadata,
4965                  * Internal Bitmap issues have been handled elsewhere.
4966                  */
4967                 if (rdev->meta_bdev) {
4968                         /* Nothing to check */;
4969                 } else if (rdev->data_offset < rdev->sb_start) {
4970                         if (mddev->dev_sectors &&
4971                             rdev->data_offset + mddev->dev_sectors
4972                             > rdev->sb_start) {
4973                                 printk("md: %s: data overlaps metadata\n",
4974                                        mdname(mddev));
4975                                 return -EINVAL;
4976                         }
4977                 } else {
4978                         if (rdev->sb_start + rdev->sb_size/512
4979                             > rdev->data_offset) {
4980                                 printk("md: %s: metadata overlaps data\n",
4981                                        mdname(mddev));
4982                                 return -EINVAL;
4983                         }
4984                 }
4985                 sysfs_notify_dirent_safe(rdev->sysfs_state);
4986         }
4987
4988         if (mddev->bio_set == NULL)
4989                 mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0);
4990
4991         spin_lock(&pers_lock);
4992         pers = find_pers(mddev->level, mddev->clevel);
4993         if (!pers || !try_module_get(pers->owner)) {
4994                 spin_unlock(&pers_lock);
4995                 if (mddev->level != LEVEL_NONE)
4996                         printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4997                                mddev->level);
4998                 else
4999                         printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
5000                                mddev->clevel);
5001                 return -EINVAL;
5002         }
5003         mddev->pers = pers;
5004         spin_unlock(&pers_lock);
5005         if (mddev->level != pers->level) {
5006                 mddev->level = pers->level;
5007                 mddev->new_level = pers->level;
5008         }
5009         strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
5010
5011         if (mddev->reshape_position != MaxSector &&
5012             pers->start_reshape == NULL) {
5013                 /* This personality cannot handle reshaping... */
5014                 mddev->pers = NULL;
5015                 module_put(pers->owner);
5016                 return -EINVAL;
5017         }
5018
5019         if (pers->sync_request) {
5020                 /* Warn if this is a potentially silly
5021                  * configuration.
5022                  */
5023                 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5024                 struct md_rdev *rdev2;
5025                 int warned = 0;
5026
5027                 rdev_for_each(rdev, mddev)
5028                         rdev_for_each(rdev2, mddev) {
5029                                 if (rdev < rdev2 &&
5030                                     rdev->bdev->bd_contains ==
5031                                     rdev2->bdev->bd_contains) {
5032                                         printk(KERN_WARNING
5033                                                "%s: WARNING: %s appears to be"
5034                                                " on the same physical disk as"
5035                                                " %s.\n",
5036                                                mdname(mddev),
5037                                                bdevname(rdev->bdev,b),
5038                                                bdevname(rdev2->bdev,b2));
5039                                         warned = 1;
5040                                 }
5041                         }
5042
5043                 if (warned)
5044                         printk(KERN_WARNING
5045                                "True protection against single-disk"
5046                                " failure might be compromised.\n");
5047         }
5048
5049         mddev->recovery = 0;
5050         /* may be over-ridden by personality */
5051         mddev->resync_max_sectors = mddev->dev_sectors;
5052
5053         mddev->ok_start_degraded = start_dirty_degraded;
5054
5055         if (start_readonly && mddev->ro == 0)
5056                 mddev->ro = 2; /* read-only, but switch on first write */
5057
5058         err = mddev->pers->run(mddev);
5059         if (err)
5060                 printk(KERN_ERR "md: pers->run() failed ...\n");
5061         else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
5062                 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
5063                           " but 'external_size' not in effect?\n", __func__);
5064                 printk(KERN_ERR
5065                        "md: invalid array_size %llu > default size %llu\n",
5066                        (unsigned long long)mddev->array_sectors / 2,
5067                        (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
5068                 err = -EINVAL;
5069                 mddev->pers->stop(mddev);
5070         }
5071         if (err == 0 && mddev->pers->sync_request &&
5072             (mddev->bitmap_info.file || mddev->bitmap_info.offset)) {
5073                 err = bitmap_create(mddev);
5074                 if (err) {
5075                         printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
5076                                mdname(mddev), err);
5077                         mddev->pers->stop(mddev);
5078                 }
5079         }
5080         if (err) {
5081                 module_put(mddev->pers->owner);
5082                 mddev->pers = NULL;
5083                 bitmap_destroy(mddev);
5084                 return err;
5085         }
5086         if (mddev->pers->sync_request) {
5087                 if (mddev->kobj.sd &&
5088                     sysfs_create_group(&mddev->kobj, &md_redundancy_group))
5089                         printk(KERN_WARNING
5090                                "md: cannot register extra attributes for %s\n",
5091                                mdname(mddev));
5092                 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
5093         } else if (mddev->ro == 2) /* auto-readonly not meaningful */
5094                 mddev->ro = 0;
5095
5096         atomic_set(&mddev->writes_pending,0);
5097         atomic_set(&mddev->max_corr_read_errors,
5098                    MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
5099         mddev->safemode = 0;
5100         mddev->safemode_timer.function = md_safemode_timeout;
5101         mddev->safemode_timer.data = (unsigned long) mddev;
5102         mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
5103         mddev->in_sync = 1;
5104         smp_wmb();
5105         mddev->ready = 1;
5106         rdev_for_each(rdev, mddev)
5107                 if (rdev->raid_disk >= 0)
5108                         if (sysfs_link_rdev(mddev, rdev))
5109                                 /* failure here is OK */;
5110         
5111         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5112         
5113         if (mddev->flags & MD_UPDATE_SB_FLAGS)
5114                 md_update_sb(mddev, 0);
5115
5116         md_new_event(mddev);
5117         sysfs_notify_dirent_safe(mddev->sysfs_state);
5118         sysfs_notify_dirent_safe(mddev->sysfs_action);
5119         sysfs_notify(&mddev->kobj, NULL, "degraded");
5120         return 0;
5121 }
5122 EXPORT_SYMBOL_GPL(md_run);
5123
5124 static int do_md_run(struct mddev *mddev)
5125 {
5126         int err;
5127
5128         err = md_run(mddev);
5129         if (err)
5130                 goto out;
5131         err = bitmap_load(mddev);
5132         if (err) {
5133                 bitmap_destroy(mddev);
5134                 goto out;
5135         }
5136
5137         md_wakeup_thread(mddev->thread);
5138         md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
5139
5140         set_capacity(mddev->gendisk, mddev->array_sectors);
5141         revalidate_disk(mddev->gendisk);
5142         mddev->changed = 1;
5143         kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5144 out:
5145         return err;
5146 }
5147
5148 static int restart_array(struct mddev *mddev)
5149 {
5150         struct gendisk *disk = mddev->gendisk;
5151
5152         /* Complain if it has no devices */
5153         if (list_empty(&mddev->disks))
5154                 return -ENXIO;
5155         if (!mddev->pers)
5156                 return -EINVAL;
5157         if (!mddev->ro)
5158                 return -EBUSY;
5159         mddev->safemode = 0;
5160         mddev->ro = 0;
5161         set_disk_ro(disk, 0);
5162         printk(KERN_INFO "md: %s switched to read-write mode.\n",
5163                 mdname(mddev));
5164         /* Kick recovery or resync if necessary */
5165         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5166         md_wakeup_thread(mddev->thread);
5167         md_wakeup_thread(mddev->sync_thread);
5168         sysfs_notify_dirent_safe(mddev->sysfs_state);
5169         return 0;
5170 }
5171
5172 /* similar to deny_write_access, but accounts for our holding a reference
5173  * to the file ourselves */
5174 static int deny_bitmap_write_access(struct file * file)
5175 {
5176         struct inode *inode = file->f_mapping->host;
5177
5178         spin_lock(&inode->i_lock);
5179         if (atomic_read(&inode->i_writecount) > 1) {
5180                 spin_unlock(&inode->i_lock);
5181                 return -ETXTBSY;
5182         }
5183         atomic_set(&inode->i_writecount, -1);
5184         spin_unlock(&inode->i_lock);
5185
5186         return 0;
5187 }
5188
5189 void restore_bitmap_write_access(struct file *file)
5190 {
5191         struct inode *inode = file->f_mapping->host;
5192
5193         spin_lock(&inode->i_lock);
5194         atomic_set(&inode->i_writecount, 1);
5195         spin_unlock(&inode->i_lock);
5196 }
5197
5198 static void md_clean(struct mddev *mddev)
5199 {
5200         mddev->array_sectors = 0;
5201         mddev->external_size = 0;
5202         mddev->dev_sectors = 0;
5203         mddev->raid_disks = 0;
5204         mddev->recovery_cp = 0;
5205         mddev->resync_min = 0;
5206         mddev->resync_max = MaxSector;
5207         mddev->reshape_position = MaxSector;
5208         mddev->external = 0;
5209         mddev->persistent = 0;
5210         mddev->level = LEVEL_NONE;
5211         mddev->clevel[0] = 0;
5212         mddev->flags = 0;
5213         mddev->ro = 0;
5214         mddev->metadata_type[0] = 0;
5215         mddev->chunk_sectors = 0;
5216         mddev->ctime = mddev->utime = 0;
5217         mddev->layout = 0;
5218         mddev->max_disks = 0;
5219         mddev->events = 0;
5220         mddev->can_decrease_events = 0;
5221         mddev->delta_disks = 0;
5222         mddev->reshape_backwards = 0;
5223         mddev->new_level = LEVEL_NONE;
5224         mddev->new_layout = 0;
5225         mddev->new_chunk_sectors = 0;
5226         mddev->curr_resync = 0;
5227         atomic64_set(&mddev->resync_mismatches, 0);
5228         mddev->suspend_lo = mddev->suspend_hi = 0;
5229         mddev->sync_speed_min = mddev->sync_speed_max = 0;
5230         mddev->recovery = 0;
5231         mddev->in_sync = 0;
5232         mddev->changed = 0;
5233         mddev->degraded = 0;
5234         mddev->safemode = 0;
5235         mddev->merge_check_needed = 0;
5236         mddev->bitmap_info.offset = 0;
5237         mddev->bitmap_info.default_offset = 0;
5238         mddev->bitmap_info.default_space = 0;
5239         mddev->bitmap_info.chunksize = 0;
5240         mddev->bitmap_info.daemon_sleep = 0;
5241         mddev->bitmap_info.max_write_behind = 0;
5242 }
5243
5244 static void __md_stop_writes(struct mddev *mddev)
5245 {
5246         set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5247         if (mddev->sync_thread) {
5248                 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5249                 md_reap_sync_thread(mddev);
5250         }
5251
5252         del_timer_sync(&mddev->safemode_timer);
5253
5254         bitmap_flush(mddev);
5255         md_super_wait(mddev);
5256
5257         if (mddev->ro == 0 &&
5258             (!mddev->in_sync || (mddev->flags & MD_UPDATE_SB_FLAGS))) {
5259                 /* mark array as shutdown cleanly */
5260                 mddev->in_sync = 1;
5261                 md_update_sb(mddev, 1);
5262         }
5263 }
5264
5265 void md_stop_writes(struct mddev *mddev)
5266 {
5267         mddev_lock_nointr(mddev);
5268         __md_stop_writes(mddev);
5269         mddev_unlock(mddev);
5270 }
5271 EXPORT_SYMBOL_GPL(md_stop_writes);
5272
5273 static void __md_stop(struct mddev *mddev)
5274 {
5275         mddev->ready = 0;
5276         mddev->pers->stop(mddev);
5277         if (mddev->pers->sync_request && mddev->to_remove == NULL)
5278                 mddev->to_remove = &md_redundancy_group;
5279         module_put(mddev->pers->owner);
5280         mddev->pers = NULL;
5281         clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5282 }
5283
5284 void md_stop(struct mddev *mddev)
5285 {
5286         /* stop the array and free an attached data structures.
5287          * This is called from dm-raid
5288          */
5289         __md_stop(mddev);
5290         bitmap_destroy(mddev);
5291         if (mddev->bio_set)
5292                 bioset_free(mddev->bio_set);
5293 }
5294
5295 EXPORT_SYMBOL_GPL(md_stop);
5296
5297 static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
5298 {
5299         int err = 0;
5300         int did_freeze = 0;
5301
5302         if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5303                 did_freeze = 1;
5304                 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5305                 md_wakeup_thread(mddev->thread);
5306         }
5307         if (mddev->sync_thread) {
5308                 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5309                 /* Thread might be blocked waiting for metadata update
5310                  * which will now never happen */
5311                 wake_up_process(mddev->sync_thread->tsk);
5312         }
5313         mddev_unlock(mddev);
5314         wait_event(resync_wait, mddev->sync_thread == NULL);
5315         mddev_lock_nointr(mddev);
5316
5317         mutex_lock(&mddev->open_mutex);
5318         if (atomic_read(&mddev->openers) > !!bdev ||
5319             mddev->sync_thread ||
5320             (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5321                 printk("md: %s still in use.\n",mdname(mddev));
5322                 if (did_freeze) {
5323                         clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5324                         md_wakeup_thread(mddev->thread);
5325                 }
5326                 err = -EBUSY;
5327                 goto out;
5328         }
5329         if (mddev->pers) {
5330                 __md_stop_writes(mddev);
5331
5332                 err  = -ENXIO;
5333                 if (mddev->ro==1)
5334                         goto out;
5335                 mddev->ro = 1;
5336                 set_disk_ro(mddev->gendisk, 1);
5337                 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5338                 sysfs_notify_dirent_safe(mddev->sysfs_state);
5339                 err = 0;
5340         }
5341 out:
5342         mutex_unlock(&mddev->open_mutex);
5343         return err;
5344 }
5345
5346 /* mode:
5347  *   0 - completely stop and dis-assemble array
5348  *   2 - stop but do not disassemble array
5349  */
5350 static int do_md_stop(struct mddev * mddev, int mode,
5351                       struct block_device *bdev)
5352 {
5353         struct gendisk *disk = mddev->gendisk;
5354         struct md_rdev *rdev;
5355         int did_freeze = 0;
5356
5357         if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
5358                 did_freeze = 1;
5359                 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5360                 md_wakeup_thread(mddev->thread);
5361         }
5362         if (mddev->sync_thread) {
5363                 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5364                 /* Thread might be blocked waiting for metadata update
5365                  * which will now never happen */
5366                 wake_up_process(mddev->sync_thread->tsk);
5367         }
5368         mddev_unlock(mddev);
5369         wait_event(resync_wait, mddev->sync_thread == NULL);
5370         mddev_lock_nointr(mddev);
5371
5372         mutex_lock(&mddev->open_mutex);
5373         if (atomic_read(&mddev->openers) > !!bdev ||
5374             mddev->sysfs_active ||
5375             mddev->sync_thread ||
5376             (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) {
5377                 printk("md: %s still in use.\n",mdname(mddev));
5378                 mutex_unlock(&mddev->open_mutex);
5379                 if (did_freeze) {
5380                         clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5381                         md_wakeup_thread(mddev->thread);
5382                 }
5383                 return -EBUSY;
5384         }
5385         if (mddev->pers) {
5386                 if (mddev->ro)
5387                         set_disk_ro(disk, 0);
5388
5389                 __md_stop_writes(mddev);
5390                 __md_stop(mddev);
5391                 mddev->queue->merge_bvec_fn = NULL;
5392                 mddev->queue->backing_dev_info.congested_fn = NULL;
5393
5394                 /* tell userspace to handle 'inactive' */
5395                 sysfs_notify_dirent_safe(mddev->sysfs_state);
5396
5397                 rdev_for_each(rdev, mddev)
5398                         if (rdev->raid_disk >= 0)
5399                                 sysfs_unlink_rdev(mddev, rdev);
5400
5401                 set_capacity(disk, 0);
5402                 mutex_unlock(&mddev->open_mutex);
5403                 mddev->changed = 1;
5404                 revalidate_disk(disk);
5405
5406                 if (mddev->ro)
5407                         mddev->ro = 0;
5408         } else
5409                 mutex_unlock(&mddev->open_mutex);
5410         /*
5411          * Free resources if final stop
5412          */
5413         if (mode == 0) {
5414                 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5415
5416                 bitmap_destroy(mddev);
5417                 if (mddev->bitmap_info.file) {
5418                         restore_bitmap_write_access(mddev->bitmap_info.file);
5419                         fput(mddev->bitmap_info.file);
5420                         mddev->bitmap_info.file = NULL;
5421                 }
5422                 mddev->bitmap_info.offset = 0;
5423
5424                 export_array(mddev);
5425
5426                 md_clean(mddev);
5427                 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
5428                 if (mddev->hold_active == UNTIL_STOP)
5429                         mddev->hold_active = 0;
5430         }
5431         blk_integrity_unregister(disk);
5432         md_new_event(mddev);
5433         sysfs_notify_dirent_safe(mddev->sysfs_state);
5434         return 0;
5435 }
5436
5437 #ifndef MODULE
5438 static void autorun_array(struct mddev *mddev)
5439 {
5440         struct md_rdev *rdev;
5441         int err;
5442
5443         if (list_empty(&mddev->disks))
5444                 return;
5445
5446         printk(KERN_INFO "md: running: ");
5447
5448         rdev_for_each(rdev, mddev) {
5449                 char b[BDEVNAME_SIZE];
5450                 printk("<%s>", bdevname(rdev->bdev,b));
5451         }
5452         printk("\n");
5453
5454         err = do_md_run(mddev);
5455         if (err) {
5456                 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
5457                 do_md_stop(mddev, 0, NULL);
5458         }
5459 }
5460
5461 /*
5462  * lets try to run arrays based on all disks that have arrived
5463  * until now. (those are in pending_raid_disks)
5464  *
5465  * the method: pick the first pending disk, collect all disks with
5466  * the same UUID, remove all from the pending list and put them into
5467  * the 'same_array' list. Then order this list based on superblock
5468  * update time (freshest comes first), kick out 'old' disks and
5469  * compare superblocks. If everything's fine then run it.
5470  *
5471  * If "unit" is allocated, then bump its reference count
5472  */
5473 static void autorun_devices(int part)
5474 {
5475         struct md_rdev *rdev0, *rdev, *tmp;
5476         struct mddev *mddev;
5477         char b[BDEVNAME_SIZE];
5478
5479         printk(KERN_INFO "md: autorun ...\n");
5480         while (!list_empty(&pending_raid_disks)) {
5481                 int unit;
5482                 dev_t dev;
5483                 LIST_HEAD(candidates);
5484                 rdev0 = list_entry(pending_raid_disks.next,
5485                                          struct md_rdev, same_set);
5486
5487                 printk(KERN_INFO "md: considering %s ...\n",
5488                         bdevname(rdev0->bdev,b));
5489                 INIT_LIST_HEAD(&candidates);
5490                 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
5491                         if (super_90_load(rdev, rdev0, 0) >= 0) {
5492                                 printk(KERN_INFO "md:  adding %s ...\n",
5493                                         bdevname(rdev->bdev,b));
5494                                 list_move(&rdev->same_set, &candidates);
5495                         }
5496                 /*
5497                  * now we have a set of devices, with all of them having
5498                  * mostly sane superblocks. It's time to allocate the
5499                  * mddev.
5500                  */
5501                 if (part) {
5502                         dev = MKDEV(mdp_major,
5503                                     rdev0->preferred_minor << MdpMinorShift);
5504                         unit = MINOR(dev) >> MdpMinorShift;
5505                 } else {
5506                         dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5507                         unit = MINOR(dev);
5508                 }
5509                 if (rdev0->preferred_minor != unit) {
5510                         printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5511                                bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5512                         break;
5513                 }
5514
5515                 md_probe(dev, NULL, NULL);
5516                 mddev = mddev_find(dev);
5517                 if (!mddev || !mddev->gendisk) {
5518                         if (mddev)
5519                                 mddev_put(mddev);
5520                         printk(KERN_ERR
5521                                 "md: cannot allocate memory for md drive.\n");
5522                         break;
5523                 }
5524                 if (mddev_lock(mddev)) 
5525                         printk(KERN_WARNING "md: %s locked, cannot run\n",
5526                                mdname(mddev));
5527                 else if (mddev->raid_disks || mddev->major_version
5528                          || !list_empty(&mddev->disks)) {
5529                         printk(KERN_WARNING 
5530                                 "md: %s already running, cannot run %s\n",
5531                                 mdname(mddev), bdevname(rdev0->bdev,b));
5532                         mddev_unlock(mddev);
5533                 } else {
5534                         printk(KERN_INFO "md: created %s\n", mdname(mddev));
5535                         mddev->persistent = 1;
5536                         rdev_for_each_list(rdev, tmp, &candidates) {
5537                                 list_del_init(&rdev->same_set);
5538                                 if (bind_rdev_to_array(rdev, mddev))
5539                                         export_rdev(rdev);
5540                         }
5541                         autorun_array(mddev);
5542                         mddev_unlock(mddev);
5543                 }
5544                 /* on success, candidates will be empty, on error
5545                  * it won't...
5546                  */
5547                 rdev_for_each_list(rdev, tmp, &candidates) {
5548                         list_del_init(&rdev->same_set);
5549                         export_rdev(rdev);
5550                 }
5551                 mddev_put(mddev);
5552         }
5553         printk(KERN_INFO "md: ... autorun DONE.\n");
5554 }
5555 #endif /* !MODULE */
5556
5557 static int get_version(void __user * arg)
5558 {
5559         mdu_version_t ver;
5560
5561         ver.major = MD_MAJOR_VERSION;
5562         ver.minor = MD_MINOR_VERSION;
5563         ver.patchlevel = MD_PATCHLEVEL_VERSION;
5564
5565         if (copy_to_user(arg, &ver, sizeof(ver)))
5566                 return -EFAULT;
5567
5568         return 0;
5569 }
5570
5571 static int get_array_info(struct mddev * mddev, void __user * arg)
5572 {
5573         mdu_array_info_t info;
5574         int nr,working,insync,failed,spare;
5575         struct md_rdev *rdev;
5576
5577         nr = working = insync = failed = spare = 0;
5578         rcu_read_lock();
5579         rdev_for_each_rcu(rdev, mddev) {
5580                 nr++;
5581                 if (test_bit(Faulty, &rdev->flags))
5582                         failed++;
5583                 else {
5584                         working++;
5585                         if (test_bit(In_sync, &rdev->flags))
5586                                 insync++;       
5587                         else
5588                                 spare++;
5589                 }
5590         }
5591         rcu_read_unlock();
5592
5593         info.major_version = mddev->major_version;
5594         info.minor_version = mddev->minor_version;
5595         info.patch_version = MD_PATCHLEVEL_VERSION;
5596         info.ctime         = mddev->ctime;
5597         info.level         = mddev->level;
5598         info.size          = mddev->dev_sectors / 2;
5599         if (info.size != mddev->dev_sectors / 2) /* overflow */
5600                 info.size = -1;
5601         info.nr_disks      = nr;
5602         info.raid_disks    = mddev->raid_disks;
5603         info.md_minor      = mddev->md_minor;
5604         info.not_persistent= !mddev->persistent;
5605
5606         info.utime         = mddev->utime;
5607         info.state         = 0;
5608         if (mddev->in_sync)
5609                 info.state = (1<<MD_SB_CLEAN);
5610         if (mddev->bitmap && mddev->bitmap_info.offset)
5611                 info.state = (1<<MD_SB_BITMAP_PRESENT);
5612         info.active_disks  = insync;
5613         info.working_disks = working;
5614         info.failed_disks  = failed;
5615         info.spare_disks   = spare;
5616
5617         info.layout        = mddev->layout;
5618         info.chunk_size    = mddev->chunk_sectors << 9;
5619
5620         if (copy_to_user(arg, &info, sizeof(info)))
5621                 return -EFAULT;
5622
5623         return 0;
5624 }
5625
5626 static int get_bitmap_file(struct mddev * mddev, void __user * arg)
5627 {
5628         mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5629         char *ptr, *buf = NULL;
5630         int err = -ENOMEM;
5631
5632         file = kmalloc(sizeof(*file), GFP_NOIO);
5633
5634         if (!file)
5635                 goto out;
5636
5637         /* bitmap disabled, zero the first byte and copy out */
5638         if (!mddev->bitmap || !mddev->bitmap->storage.file) {
5639                 file->pathname[0] = '\0';
5640                 goto copy_out;
5641         }
5642
5643         buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5644         if (!buf)
5645                 goto out;
5646
5647         ptr = d_path(&mddev->bitmap->storage.file->f_path,
5648                      buf, sizeof(file->pathname));
5649         if (IS_ERR(ptr))
5650                 goto out;
5651
5652         strcpy(file->pathname, ptr);
5653
5654 copy_out:
5655         err = 0;
5656         if (copy_to_user(arg, file, sizeof(*file)))
5657                 err = -EFAULT;
5658 out:
5659         kfree(buf);
5660         kfree(file);
5661         return err;
5662 }
5663
5664 static int get_disk_info(struct mddev * mddev, void __user * arg)
5665 {
5666         mdu_disk_info_t info;
5667         struct md_rdev *rdev;
5668
5669         if (copy_from_user(&info, arg, sizeof(info)))
5670                 return -EFAULT;
5671
5672         rcu_read_lock();
5673         rdev = find_rdev_nr_rcu(mddev, info.number);
5674         if (rdev) {
5675                 info.major = MAJOR(rdev->bdev->bd_dev);
5676                 info.minor = MINOR(rdev->bdev->bd_dev);
5677                 info.raid_disk = rdev->raid_disk;
5678                 info.state = 0;
5679                 if (test_bit(Faulty, &rdev->flags))
5680                         info.state |= (1<<MD_DISK_FAULTY);
5681                 else if (test_bit(In_sync, &rdev->flags)) {
5682                         info.state |= (1<<MD_DISK_ACTIVE);
5683                         info.state |= (1<<MD_DISK_SYNC);
5684                 }
5685                 if (test_bit(WriteMostly, &rdev->flags))
5686                         info.state |= (1<<MD_DISK_WRITEMOSTLY);
5687         } else {
5688                 info.major = info.minor = 0;
5689                 info.raid_disk = -1;
5690                 info.state = (1<<MD_DISK_REMOVED);
5691         }
5692         rcu_read_unlock();
5693
5694         if (copy_to_user(arg, &info, sizeof(info)))
5695                 return -EFAULT;
5696
5697         return 0;
5698 }
5699
5700 static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
5701 {
5702         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5703         struct md_rdev *rdev;
5704         dev_t dev = MKDEV(info->major,info->minor);
5705
5706         if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5707                 return -EOVERFLOW;
5708
5709         if (!mddev->raid_disks) {
5710                 int err;
5711                 /* expecting a device which has a superblock */
5712                 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5713                 if (IS_ERR(rdev)) {
5714                         printk(KERN_WARNING 
5715                                 "md: md_import_device returned %ld\n",
5716                                 PTR_ERR(rdev));
5717                         return PTR_ERR(rdev);
5718                 }
5719                 if (!list_empty(&mddev->disks)) {
5720                         struct md_rdev *rdev0
5721                                 = list_entry(mddev->disks.next,
5722                                              struct md_rdev, same_set);
5723                         err = super_types[mddev->major_version]
5724                                 .load_super(rdev, rdev0, mddev->minor_version);
5725                         if (err < 0) {
5726                                 printk(KERN_WARNING 
5727                                         "md: %s has different UUID to %s\n",
5728                                         bdevname(rdev->bdev,b), 
5729                                         bdevname(rdev0->bdev,b2));
5730                                 export_rdev(rdev);
5731                                 return -EINVAL;
5732                         }
5733                 }
5734                 err = bind_rdev_to_array(rdev, mddev);
5735                 if (err)
5736                         export_rdev(rdev);
5737                 return err;
5738         }
5739
5740         /*
5741          * add_new_disk can be used once the array is assembled
5742          * to add "hot spares".  They must already have a superblock
5743          * written
5744          */
5745         if (mddev->pers) {
5746                 int err;
5747                 if (!mddev->pers->hot_add_disk) {
5748                         printk(KERN_WARNING 
5749                                 "%s: personality does not support diskops!\n",
5750                                mdname(mddev));
5751                         return -EINVAL;
5752                 }
5753                 if (mddev->persistent)
5754                         rdev = md_import_device(dev, mddev->major_version,
5755                                                 mddev->minor_version);
5756                 else
5757                         rdev = md_import_device(dev, -1, -1);
5758                 if (IS_ERR(rdev)) {
5759                         printk(KERN_WARNING 
5760                                 "md: md_import_device returned %ld\n",
5761                                 PTR_ERR(rdev));
5762                         return PTR_ERR(rdev);
5763                 }
5764                 /* set saved_raid_disk if appropriate */
5765                 if (!mddev->persistent) {
5766                         if (info->state & (1<<MD_DISK_SYNC)  &&
5767                             info->raid_disk < mddev->raid_disks) {
5768                                 rdev->raid_disk = info->raid_disk;
5769                                 set_bit(In_sync, &rdev->flags);
5770                                 clear_bit(Bitmap_sync, &rdev->flags);
5771                         } else
5772                                 rdev->raid_disk = -1;
5773                 } else
5774                         super_types[mddev->major_version].
5775                                 validate_super(mddev, rdev);
5776                 if ((info->state & (1<<MD_DISK_SYNC)) &&
5777                      rdev->raid_disk != info->raid_disk) {
5778                         /* This was a hot-add request, but events doesn't
5779                          * match, so reject it.
5780                          */
5781                         export_rdev(rdev);
5782                         return -EINVAL;
5783                 }
5784
5785                 if (test_bit(In_sync, &rdev->flags))
5786                         rdev->saved_raid_disk = rdev->raid_disk;
5787                 else
5788                         rdev->saved_raid_disk = -1;
5789
5790                 clear_bit(In_sync, &rdev->flags); /* just to be sure */
5791                 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5792                         set_bit(WriteMostly, &rdev->flags);
5793                 else
5794                         clear_bit(WriteMostly, &rdev->flags);
5795
5796                 rdev->raid_disk = -1;
5797                 err = bind_rdev_to_array(rdev, mddev);
5798                 if (!err && !mddev->pers->hot_remove_disk) {
5799                         /* If there is hot_add_disk but no hot_remove_disk
5800                          * then added disks for geometry changes,
5801                          * and should be added immediately.
5802                          */
5803                         super_types[mddev->major_version].
5804                                 validate_super(mddev, rdev);
5805                         err = mddev->pers->hot_add_disk(mddev, rdev);
5806                         if (err)
5807                                 unbind_rdev_from_array(rdev);
5808                 }
5809                 if (err)
5810                         export_rdev(rdev);
5811                 else
5812                         sysfs_notify_dirent_safe(rdev->sysfs_state);
5813
5814                 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5815                 if (mddev->degraded)
5816                         set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
5817                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5818                 if (!err)
5819                         md_new_event(mddev);
5820                 md_wakeup_thread(mddev->thread);
5821                 return err;
5822         }
5823
5824         /* otherwise, add_new_disk is only allowed
5825          * for major_version==0 superblocks
5826          */
5827         if (mddev->major_version != 0) {
5828                 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5829                        mdname(mddev));
5830                 return -EINVAL;
5831         }
5832
5833         if (!(info->state & (1<<MD_DISK_FAULTY))) {
5834                 int err;
5835                 rdev = md_import_device(dev, -1, 0);
5836                 if (IS_ERR(rdev)) {
5837                         printk(KERN_WARNING 
5838                                 "md: error, md_import_device() returned %ld\n",
5839                                 PTR_ERR(rdev));
5840                         return PTR_ERR(rdev);
5841                 }
5842                 rdev->desc_nr = info->number;
5843                 if (info->raid_disk < mddev->raid_disks)
5844                         rdev->raid_disk = info->raid_disk;
5845                 else
5846                         rdev->raid_disk = -1;
5847
5848                 if (rdev->raid_disk < mddev->raid_disks)
5849                         if (info->state & (1<<MD_DISK_SYNC))
5850                                 set_bit(In_sync, &rdev->flags);
5851
5852                 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5853                         set_bit(WriteMostly, &rdev->flags);
5854
5855                 if (!mddev->persistent) {
5856                         printk(KERN_INFO "md: nonpersistent superblock ...\n");
5857                         rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5858                 } else
5859                         rdev->sb_start = calc_dev_sboffset(rdev);
5860                 rdev->sectors = rdev->sb_start;
5861
5862                 err = bind_rdev_to_array(rdev, mddev);
5863                 if (err) {
5864                         export_rdev(rdev);
5865                         return err;
5866                 }
5867         }
5868
5869         return 0;
5870 }
5871
5872 static int hot_remove_disk(struct mddev * mddev, dev_t dev)
5873 {
5874         char b[BDEVNAME_SIZE];
5875         struct md_rdev *rdev;
5876
5877         rdev = find_rdev(mddev, dev);
5878         if (!rdev)
5879                 return -ENXIO;
5880
5881         clear_bit(Blocked, &rdev->flags);
5882         remove_and_add_spares(mddev, rdev);
5883
5884         if (rdev->raid_disk >= 0)
5885                 goto busy;
5886
5887         kick_rdev_from_array(rdev);
5888         md_update_sb(mddev, 1);
5889         md_new_event(mddev);
5890
5891         return 0;
5892 busy:
5893         printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
5894                 bdevname(rdev->bdev,b), mdname(mddev));
5895         return -EBUSY;
5896 }
5897
5898 static int hot_add_disk(struct mddev * mddev, dev_t dev)
5899 {
5900         char b[BDEVNAME_SIZE];
5901         int err;
5902         struct md_rdev *rdev;
5903
5904         if (!mddev->pers)
5905                 return -ENODEV;
5906
5907         if (mddev->major_version != 0) {
5908                 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5909                         " version-0 superblocks.\n",
5910                         mdname(mddev));
5911                 return -EINVAL;
5912         }
5913         if (!mddev->pers->hot_add_disk) {
5914                 printk(KERN_WARNING 
5915                         "%s: personality does not support diskops!\n",
5916                         mdname(mddev));
5917                 return -EINVAL;
5918         }
5919
5920         rdev = md_import_device(dev, -1, 0);
5921         if (IS_ERR(rdev)) {
5922                 printk(KERN_WARNING 
5923                         "md: error, md_import_device() returned %ld\n",
5924                         PTR_ERR(rdev));
5925                 return -EINVAL;
5926         }
5927
5928         if (mddev->persistent)
5929                 rdev->sb_start = calc_dev_sboffset(rdev);
5930         else
5931                 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5932
5933         rdev->sectors = rdev->sb_start;
5934
5935         if (test_bit(Faulty, &rdev->flags)) {
5936                 printk(KERN_WARNING 
5937                         "md: can not hot-add faulty %s disk to %s!\n",
5938                         bdevname(rdev->bdev,b), mdname(mddev));
5939                 err = -EINVAL;
5940                 goto abort_export;
5941         }
5942         clear_bit(In_sync, &rdev->flags);
5943         rdev->desc_nr = -1;
5944         rdev->saved_raid_disk = -1;
5945         err = bind_rdev_to_array(rdev, mddev);
5946         if (err)
5947                 goto abort_export;
5948
5949         /*
5950          * The rest should better be atomic, we can have disk failures
5951          * noticed in interrupt contexts ...
5952          */
5953
5954         rdev->raid_disk = -1;
5955
5956         md_update_sb(mddev, 1);
5957
5958         /*
5959          * Kick recovery, maybe this spare has to be added to the
5960          * array immediately.
5961          */
5962         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5963         md_wakeup_thread(mddev->thread);
5964         md_new_event(mddev);
5965         return 0;
5966
5967 abort_export:
5968         export_rdev(rdev);
5969         return err;
5970 }
5971
5972 static int set_bitmap_file(struct mddev *mddev, int fd)
5973 {
5974         int err;
5975
5976         if (mddev->pers) {
5977                 if (!mddev->pers->quiesce)
5978                         return -EBUSY;
5979                 if (mddev->recovery || mddev->sync_thread)
5980                         return -EBUSY;
5981                 /* we should be able to change the bitmap.. */
5982         }
5983
5984
5985         if (fd >= 0) {
5986                 if (mddev->bitmap)
5987                         return -EEXIST; /* cannot add when bitmap is present */
5988                 mddev->bitmap_info.file = fget(fd);
5989
5990                 if (mddev->bitmap_info.file == NULL) {
5991                         printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5992                                mdname(mddev));
5993                         return -EBADF;
5994                 }
5995
5996                 err = deny_bitmap_write_access(mddev->bitmap_info.file);
5997                 if (err) {
5998                         printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5999                                mdname(mddev));
6000                         fput(mddev->bitmap_info.file);
6001                         mddev->bitmap_info.file = NULL;
6002                         return err;
6003                 }
6004                 mddev->bitmap_info.offset = 0; /* file overrides offset */
6005         } else if (mddev->bitmap == NULL)
6006                 return -ENOENT; /* cannot remove what isn't there */
6007         err = 0;
6008         if (mddev->pers) {
6009                 mddev->pers->quiesce(mddev, 1);
6010                 if (fd >= 0) {
6011                         err = bitmap_create(mddev);
6012                         if (!err)
6013                                 err = bitmap_load(mddev);
6014                 }
6015                 if (fd < 0 || err) {
6016                         bitmap_destroy(mddev);
6017                         fd = -1; /* make sure to put the file */
6018                 }
6019                 mddev->pers->quiesce(mddev, 0);
6020         }
6021         if (fd < 0) {
6022                 if (mddev->bitmap_info.file) {
6023                         restore_bitmap_write_access(mddev->bitmap_info.file);
6024                         fput(mddev->bitmap_info.file);
6025                 }
6026                 mddev->bitmap_info.file = NULL;
6027         }
6028
6029         return err;
6030 }
6031
6032 /*
6033  * set_array_info is used two different ways
6034  * The original usage is when creating a new array.
6035  * In this usage, raid_disks is > 0 and it together with
6036  *  level, size, not_persistent,layout,chunksize determine the
6037  *  shape of the array.
6038  *  This will always create an array with a type-0.90.0 superblock.
6039  * The newer usage is when assembling an array.
6040  *  In this case raid_disks will be 0, and the major_version field is
6041  *  use to determine which style super-blocks are to be found on the devices.
6042  *  The minor and patch _version numbers are also kept incase the
6043  *  super_block handler wishes to interpret them.
6044  */
6045 static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
6046 {
6047
6048         if (info->raid_disks == 0) {
6049                 /* just setting version number for superblock loading */
6050                 if (info->major_version < 0 ||
6051                     info->major_version >= ARRAY_SIZE(super_types) ||
6052                     super_types[info->major_version].name == NULL) {
6053                         /* maybe try to auto-load a module? */
6054                         printk(KERN_INFO 
6055                                 "md: superblock version %d not known\n",
6056                                 info->major_version);
6057                         return -EINVAL;
6058                 }
6059                 mddev->major_version = info->major_version;
6060                 mddev->minor_version = info->minor_version;
6061                 mddev->patch_version = info->patch_version;
6062                 mddev->persistent = !info->not_persistent;
6063                 /* ensure mddev_put doesn't delete this now that there
6064                  * is some minimal configuration.
6065                  */
6066                 mddev->ctime         = get_seconds();
6067                 return 0;
6068         }
6069         mddev->major_version = MD_MAJOR_VERSION;
6070         mddev->minor_version = MD_MINOR_VERSION;
6071         mddev->patch_version = MD_PATCHLEVEL_VERSION;
6072         mddev->ctime         = get_seconds();
6073
6074         mddev->level         = info->level;
6075         mddev->clevel[0]     = 0;
6076         mddev->dev_sectors   = 2 * (sector_t)info->size;
6077         mddev->raid_disks    = info->raid_disks;
6078         /* don't set md_minor, it is determined by which /dev/md* was
6079          * openned
6080          */
6081         if (info->state & (1<<MD_SB_CLEAN))
6082                 mddev->recovery_cp = MaxSector;
6083         else
6084                 mddev->recovery_cp = 0;
6085         mddev->persistent    = ! info->not_persistent;
6086         mddev->external      = 0;
6087
6088         mddev->layout        = info->layout;
6089         mddev->chunk_sectors = info->chunk_size >> 9;
6090
6091         mddev->max_disks     = MD_SB_DISKS;
6092
6093         if (mddev->persistent)
6094                 mddev->flags         = 0;
6095         set_bit(MD_CHANGE_DEVS, &mddev->flags);
6096
6097         mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
6098         mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
6099         mddev->bitmap_info.offset = 0;
6100
6101         mddev->reshape_position = MaxSector;
6102
6103         /*
6104          * Generate a 128 bit UUID
6105          */
6106         get_random_bytes(mddev->uuid, 16);
6107
6108         mddev->new_level = mddev->level;
6109         mddev->new_chunk_sectors = mddev->chunk_sectors;
6110         mddev->new_layout = mddev->layout;
6111         mddev->delta_disks = 0;
6112         mddev->reshape_backwards = 0;
6113
6114         return 0;
6115 }
6116
6117 void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
6118 {
6119         WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
6120
6121         if (mddev->external_size)
6122                 return;
6123
6124         mddev->array_sectors = array_sectors;
6125 }
6126 EXPORT_SYMBOL(md_set_array_sectors);
6127
6128 static int update_size(struct mddev *mddev, sector_t num_sectors)
6129 {
6130         struct md_rdev *rdev;
6131         int rv;
6132         int fit = (num_sectors == 0);
6133
6134         if (mddev->pers->resize == NULL)
6135                 return -EINVAL;
6136         /* The "num_sectors" is the number of sectors of each device that
6137          * is used.  This can only make sense for arrays with redundancy.
6138          * linear and raid0 always use whatever space is available. We can only
6139          * consider changing this number if no resync or reconstruction is
6140          * happening, and if the new size is acceptable. It must fit before the
6141          * sb_start or, if that is <data_offset, it must fit before the size
6142          * of each device.  If num_sectors is zero, we find the largest size
6143          * that fits.
6144          */
6145         if (mddev->sync_thread)
6146                 return -EBUSY;
6147
6148         rdev_for_each(rdev, mddev) {
6149                 sector_t avail = rdev->sectors;
6150
6151                 if (fit && (num_sectors == 0 || num_sectors > avail))
6152                         num_sectors = avail;
6153                 if (avail < num_sectors)
6154                         return -ENOSPC;
6155         }
6156         rv = mddev->pers->resize(mddev, num_sectors);
6157         if (!rv)
6158                 revalidate_disk(mddev->gendisk);
6159         return rv;
6160 }
6161
6162 static int update_raid_disks(struct mddev *mddev, int raid_disks)
6163 {
6164         int rv;
6165         struct md_rdev *rdev;
6166         /* change the number of raid disks */
6167         if (mddev->pers->check_reshape == NULL)
6168                 return -EINVAL;
6169         if (raid_disks <= 0 ||
6170             (mddev->max_disks && raid_disks >= mddev->max_disks))
6171                 return -EINVAL;
6172         if (mddev->sync_thread || mddev->reshape_position != MaxSector)
6173                 return -EBUSY;
6174
6175         rdev_for_each(rdev, mddev) {
6176                 if (mddev->raid_disks < raid_disks &&
6177                     rdev->data_offset < rdev->new_data_offset)
6178                         return -EINVAL;
6179                 if (mddev->raid_disks > raid_disks &&
6180                     rdev->data_offset > rdev->new_data_offset)
6181                         return -EINVAL;
6182         }
6183
6184         mddev->delta_disks = raid_disks - mddev->raid_disks;
6185         if (mddev->delta_disks < 0)
6186                 mddev->reshape_backwards = 1;
6187         else if (mddev->delta_disks > 0)
6188                 mddev->reshape_backwards = 0;
6189
6190         rv = mddev->pers->check_reshape(mddev);
6191         if (rv < 0) {
6192                 mddev->delta_disks = 0;
6193                 mddev->reshape_backwards = 0;
6194         }
6195         return rv;
6196 }
6197
6198
6199 /*
6200  * update_array_info is used to change the configuration of an
6201  * on-line array.
6202  * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
6203  * fields in the info are checked against the array.
6204  * Any differences that cannot be handled will cause an error.
6205  * Normally, only one change can be managed at a time.
6206  */
6207 static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
6208 {
6209         int rv = 0;
6210         int cnt = 0;
6211         int state = 0;
6212
6213         /* calculate expected state,ignoring low bits */
6214         if (mddev->bitmap && mddev->bitmap_info.offset)
6215                 state |= (1 << MD_SB_BITMAP_PRESENT);
6216
6217         if (mddev->major_version != info->major_version ||
6218             mddev->minor_version != info->minor_version ||
6219 /*          mddev->patch_version != info->patch_version || */
6220             mddev->ctime         != info->ctime         ||
6221             mddev->level         != info->level         ||
6222 /*          mddev->layout        != info->layout        || */
6223             !mddev->persistent   != info->not_persistent||
6224             mddev->chunk_sectors != info->chunk_size >> 9 ||
6225             /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
6226             ((state^info->state) & 0xfffffe00)
6227                 )
6228                 return -EINVAL;
6229         /* Check there is only one change */
6230         if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6231                 cnt++;
6232         if (mddev->raid_disks != info->raid_disks)
6233                 cnt++;
6234         if (mddev->layout != info->layout)
6235                 cnt++;
6236         if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
6237                 cnt++;
6238         if (cnt == 0)
6239                 return 0;
6240         if (cnt > 1)
6241                 return -EINVAL;
6242
6243         if (mddev->layout != info->layout) {
6244                 /* Change layout
6245                  * we don't need to do anything at the md level, the
6246                  * personality will take care of it all.
6247                  */
6248                 if (mddev->pers->check_reshape == NULL)
6249                         return -EINVAL;
6250                 else {
6251                         mddev->new_layout = info->layout;
6252                         rv = mddev->pers->check_reshape(mddev);
6253                         if (rv)
6254                                 mddev->new_layout = mddev->layout;
6255                         return rv;
6256                 }
6257         }
6258         if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
6259                 rv = update_size(mddev, (sector_t)info->size * 2);
6260
6261         if (mddev->raid_disks    != info->raid_disks)
6262                 rv = update_raid_disks(mddev, info->raid_disks);
6263
6264         if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
6265                 if (mddev->pers->quiesce == NULL)
6266                         return -EINVAL;
6267                 if (mddev->recovery || mddev->sync_thread)
6268                         return -EBUSY;
6269                 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
6270                         /* add the bitmap */
6271                         if (mddev->bitmap)
6272                                 return -EEXIST;
6273                         if (mddev->bitmap_info.default_offset == 0)
6274                                 return -EINVAL;
6275                         mddev->bitmap_info.offset =
6276                                 mddev->bitmap_info.default_offset;
6277                         mddev->bitmap_info.space =
6278                                 mddev->bitmap_info.default_space;
6279                         mddev->pers->quiesce(mddev, 1);
6280                         rv = bitmap_create(mddev);
6281                         if (!rv)
6282                                 rv = bitmap_load(mddev);
6283                         if (rv)
6284                                 bitmap_destroy(mddev);
6285                         mddev->pers->quiesce(mddev, 0);
6286                 } else {
6287                         /* remove the bitmap */
6288                         if (!mddev->bitmap)
6289                                 return -ENOENT;
6290                         if (mddev->bitmap->storage.file)
6291                                 return -EINVAL;
6292                         mddev->pers->quiesce(mddev, 1);
6293                         bitmap_destroy(mddev);
6294                         mddev->pers->quiesce(mddev, 0);
6295                         mddev->bitmap_info.offset = 0;
6296                 }
6297         }
6298         md_update_sb(mddev, 1);
6299         return rv;
6300 }
6301
6302 static int set_disk_faulty(struct mddev *mddev, dev_t dev)
6303 {
6304         struct md_rdev *rdev;
6305         int err = 0;
6306
6307         if (mddev->pers == NULL)
6308                 return -ENODEV;
6309
6310         rcu_read_lock();
6311         rdev = find_rdev_rcu(mddev, dev);
6312         if (!rdev)
6313                 err =  -ENODEV;
6314         else {
6315                 md_error(mddev, rdev);
6316                 if (!test_bit(Faulty, &rdev->flags))
6317                         err = -EBUSY;
6318         }
6319         rcu_read_unlock();
6320         return err;
6321 }
6322
6323 /*
6324  * We have a problem here : there is no easy way to give a CHS
6325  * virtual geometry. We currently pretend that we have a 2 heads
6326  * 4 sectors (with a BIG number of cylinders...). This drives
6327  * dosfs just mad... ;-)
6328  */
6329 static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6330 {
6331         struct mddev *mddev = bdev->bd_disk->private_data;
6332
6333         geo->heads = 2;
6334         geo->sectors = 4;
6335         geo->cylinders = mddev->array_sectors / 8;
6336         return 0;
6337 }
6338
6339 static int md_ioctl(struct block_device *bdev, fmode_t mode,
6340                         unsigned int cmd, unsigned long arg)
6341 {
6342         int err = 0;
6343         void __user *argp = (void __user *)arg;
6344         struct mddev *mddev = NULL;
6345         int ro;
6346
6347         switch (cmd) {
6348         case RAID_VERSION:
6349         case GET_ARRAY_INFO:
6350         case GET_DISK_INFO:
6351                 break;
6352         default:
6353                 if (!capable(CAP_SYS_ADMIN))
6354                         return -EACCES;
6355         }
6356
6357         /*
6358          * Commands dealing with the RAID driver but not any
6359          * particular array:
6360          */
6361         switch (cmd) {
6362         case RAID_VERSION:
6363                 err = get_version(argp);
6364                 goto done;
6365
6366         case PRINT_RAID_DEBUG:
6367                 err = 0;
6368                 md_print_devices();
6369                 goto done;
6370
6371 #ifndef MODULE
6372         case RAID_AUTORUN:
6373                 err = 0;
6374                 autostart_arrays(arg);
6375                 goto done;
6376 #endif
6377         default:;
6378         }
6379
6380         /*
6381          * Commands creating/starting a new array:
6382          */
6383
6384         mddev = bdev->bd_disk->private_data;
6385
6386         if (!mddev) {
6387                 BUG();
6388                 goto abort;
6389         }
6390
6391         /* Some actions do not requires the mutex */
6392         switch (cmd) {
6393         case GET_ARRAY_INFO:
6394                 if (!mddev->raid_disks && !mddev->external)
6395                         err = -ENODEV;
6396                 else
6397                         err = get_array_info(mddev, argp);
6398                 goto abort;
6399
6400         case GET_DISK_INFO:
6401                 if (!mddev->raid_disks && !mddev->external)
6402                         err = -ENODEV;
6403                 else
6404                         err = get_disk_info(mddev, argp);
6405                 goto abort;
6406
6407         case SET_DISK_FAULTY:
6408                 err = set_disk_faulty(mddev, new_decode_dev(arg));
6409                 goto abort;
6410         }
6411
6412         if (cmd == ADD_NEW_DISK)
6413                 /* need to ensure md_delayed_delete() has completed */
6414                 flush_workqueue(md_misc_wq);
6415
6416         if (cmd == HOT_REMOVE_DISK)
6417                 /* need to ensure recovery thread has run */
6418                 wait_event_interruptible_timeout(mddev->sb_wait,
6419                                                  !test_bit(MD_RECOVERY_NEEDED,
6420                                                            &mddev->flags),
6421                                                  msecs_to_jiffies(5000));
6422         if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) {
6423                 /* Need to flush page cache, and ensure no-one else opens
6424                  * and writes
6425                  */
6426                 mutex_lock(&mddev->open_mutex);
6427                 if (atomic_read(&mddev->openers) > 1) {
6428                         mutex_unlock(&mddev->open_mutex);
6429                         err = -EBUSY;
6430                         goto abort;
6431                 }
6432                 set_bit(MD_STILL_CLOSED, &mddev->flags);
6433                 mutex_unlock(&mddev->open_mutex);
6434                 sync_blockdev(bdev);
6435         }
6436         err = mddev_lock(mddev);
6437         if (err) {
6438                 printk(KERN_INFO 
6439                         "md: ioctl lock interrupted, reason %d, cmd %d\n",
6440                         err, cmd);
6441                 goto abort;
6442         }
6443
6444         if (cmd == SET_ARRAY_INFO) {
6445                 mdu_array_info_t info;
6446                 if (!arg)
6447                         memset(&info, 0, sizeof(info));
6448                 else if (copy_from_user(&info, argp, sizeof(info))) {
6449                         err = -EFAULT;
6450                         goto abort_unlock;
6451                 }
6452                 if (mddev->pers) {
6453                         err = update_array_info(mddev, &info);
6454                         if (err) {
6455                                 printk(KERN_WARNING "md: couldn't update"
6456                                        " array info. %d\n", err);
6457                                 goto abort_unlock;
6458                         }
6459                         goto done_unlock;
6460                 }
6461                 if (!list_empty(&mddev->disks)) {
6462                         printk(KERN_WARNING
6463                                "md: array %s already has disks!\n",
6464                                mdname(mddev));
6465                         err = -EBUSY;
6466                         goto abort_unlock;
6467                 }
6468                 if (mddev->raid_disks) {
6469                         printk(KERN_WARNING
6470                                "md: array %s already initialised!\n",
6471                                mdname(mddev));
6472                         err = -EBUSY;
6473                         goto abort_unlock;
6474                 }
6475                 err = set_array_info(mddev, &info);
6476                 if (err) {
6477                         printk(KERN_WARNING "md: couldn't set"
6478                                " array info. %d\n", err);
6479                         goto abort_unlock;
6480                 }
6481                 goto done_unlock;
6482         }
6483
6484         /*
6485          * Commands querying/configuring an existing array:
6486          */
6487         /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
6488          * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
6489         if ((!mddev->raid_disks && !mddev->external)
6490             && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6491             && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6492             && cmd != GET_BITMAP_FILE) {
6493                 err = -ENODEV;
6494                 goto abort_unlock;
6495         }
6496
6497         /*
6498          * Commands even a read-only array can execute:
6499          */
6500         switch (cmd) {
6501         case GET_BITMAP_FILE:
6502                 err = get_bitmap_file(mddev, argp);
6503                 goto done_unlock;
6504
6505         case RESTART_ARRAY_RW:
6506                 err = restart_array(mddev);
6507                 goto done_unlock;
6508
6509         case STOP_ARRAY:
6510                 err = do_md_stop(mddev, 0, bdev);
6511                 goto done_unlock;
6512
6513         case STOP_ARRAY_RO:
6514                 err = md_set_readonly(mddev, bdev);
6515                 goto done_unlock;
6516
6517         case HOT_REMOVE_DISK:
6518                 err = hot_remove_disk(mddev, new_decode_dev(arg));
6519                 goto done_unlock;
6520
6521         case ADD_NEW_DISK:
6522                 /* We can support ADD_NEW_DISK on read-only arrays
6523                  * on if we are re-adding a preexisting device.
6524                  * So require mddev->pers and MD_DISK_SYNC.
6525                  */
6526                 if (mddev->pers) {
6527                         mdu_disk_info_t info;
6528                         if (copy_from_user(&info, argp, sizeof(info)))
6529                                 err = -EFAULT;
6530                         else if (!(info.state & (1<<MD_DISK_SYNC)))
6531                                 /* Need to clear read-only for this */
6532                                 break;
6533                         else
6534                                 err = add_new_disk(mddev, &info);
6535                         goto done_unlock;
6536                 }
6537                 break;
6538
6539         case BLKROSET:
6540                 if (get_user(ro, (int __user *)(arg))) {
6541                         err = -EFAULT;
6542                         goto done_unlock;
6543                 }
6544                 err = -EINVAL;
6545
6546                 /* if the bdev is going readonly the value of mddev->ro
6547                  * does not matter, no writes are coming
6548                  */
6549                 if (ro)
6550                         goto done_unlock;
6551
6552                 /* are we are already prepared for writes? */
6553                 if (mddev->ro != 1)
6554                         goto done_unlock;
6555
6556                 /* transitioning to readauto need only happen for
6557                  * arrays that call md_write_start
6558                  */
6559                 if (mddev->pers) {
6560                         err = restart_array(mddev);
6561                         if (err == 0) {
6562                                 mddev->ro = 2;
6563                                 set_disk_ro(mddev->gendisk, 0);
6564                         }
6565                 }
6566                 goto done_unlock;
6567         }
6568
6569         /*
6570          * The remaining ioctls are changing the state of the
6571          * superblock, so we do not allow them on read-only arrays.
6572          * However non-MD ioctls (e.g. get-size) will still come through
6573          * here and hit the 'default' below, so only disallow
6574          * 'md' ioctls, and switch to rw mode if started auto-readonly.
6575          */
6576         if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
6577                 if (mddev->ro == 2) {
6578                         mddev->ro = 0;
6579                         sysfs_notify_dirent_safe(mddev->sysfs_state);
6580                         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6581                         /* mddev_unlock will wake thread */
6582                         /* If a device failed while we were read-only, we
6583                          * need to make sure the metadata is updated now.
6584                          */
6585                         if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
6586                                 mddev_unlock(mddev);
6587                                 wait_event(mddev->sb_wait,
6588                                            !test_bit(MD_CHANGE_DEVS, &mddev->flags) &&
6589                                            !test_bit(MD_CHANGE_PENDING, &mddev->flags));
6590                                 mddev_lock_nointr(mddev);
6591                         }
6592                 } else {
6593                         err = -EROFS;
6594                         goto abort_unlock;
6595                 }
6596         }
6597
6598         switch (cmd) {
6599         case ADD_NEW_DISK:
6600         {
6601                 mdu_disk_info_t info;
6602                 if (copy_from_user(&info, argp, sizeof(info)))
6603                         err = -EFAULT;
6604                 else
6605                         err = add_new_disk(mddev, &info);
6606                 goto done_unlock;
6607         }
6608
6609         case HOT_ADD_DISK:
6610                 err = hot_add_disk(mddev, new_decode_dev(arg));
6611                 goto done_unlock;
6612
6613         case RUN_ARRAY:
6614                 err = do_md_run(mddev);
6615                 goto done_unlock;
6616
6617         case SET_BITMAP_FILE:
6618                 err = set_bitmap_file(mddev, (int)arg);
6619                 goto done_unlock;
6620
6621         default:
6622                 err = -EINVAL;
6623                 goto abort_unlock;
6624         }
6625
6626 done_unlock:
6627 abort_unlock:
6628         if (mddev->hold_active == UNTIL_IOCTL &&
6629             err != -EINVAL)
6630                 mddev->hold_active = 0;
6631         mddev_unlock(mddev);
6632
6633         return err;
6634 done:
6635         if (err)
6636                 MD_BUG();
6637 abort:
6638         return err;
6639 }
6640 #ifdef CONFIG_COMPAT
6641 static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6642                     unsigned int cmd, unsigned long arg)
6643 {
6644         switch (cmd) {
6645         case HOT_REMOVE_DISK:
6646         case HOT_ADD_DISK:
6647         case SET_DISK_FAULTY:
6648         case SET_BITMAP_FILE:
6649                 /* These take in integer arg, do not convert */
6650                 break;
6651         default:
6652                 arg = (unsigned long)compat_ptr(arg);
6653                 break;
6654         }
6655
6656         return md_ioctl(bdev, mode, cmd, arg);
6657 }
6658 #endif /* CONFIG_COMPAT */
6659
6660 static int md_open(struct block_device *bdev, fmode_t mode)
6661 {
6662         /*
6663          * Succeed if we can lock the mddev, which confirms that
6664          * it isn't being stopped right now.
6665          */
6666         struct mddev *mddev = mddev_find(bdev->bd_dev);
6667         int err;
6668
6669         if (!mddev)
6670                 return -ENODEV;
6671
6672         if (mddev->gendisk != bdev->bd_disk) {
6673                 /* we are racing with mddev_put which is discarding this
6674                  * bd_disk.
6675                  */
6676                 mddev_put(mddev);
6677                 /* Wait until bdev->bd_disk is definitely gone */
6678                 flush_workqueue(md_misc_wq);
6679                 /* Then retry the open from the top */
6680                 return -ERESTARTSYS;
6681         }
6682         BUG_ON(mddev != bdev->bd_disk->private_data);
6683
6684         if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
6685                 goto out;
6686
6687         err = 0;
6688         atomic_inc(&mddev->openers);
6689         clear_bit(MD_STILL_CLOSED, &mddev->flags);
6690         mutex_unlock(&mddev->open_mutex);
6691
6692         check_disk_change(bdev);
6693  out:
6694         return err;
6695 }
6696
6697 static void md_release(struct gendisk *disk, fmode_t mode)
6698 {
6699         struct mddev *mddev = disk->private_data;
6700
6701         BUG_ON(!mddev);
6702         atomic_dec(&mddev->openers);
6703         mddev_put(mddev);
6704 }
6705
6706 static int md_media_changed(struct gendisk *disk)
6707 {
6708         struct mddev *mddev = disk->private_data;
6709
6710         return mddev->changed;
6711 }
6712
6713 static int md_revalidate(struct gendisk *disk)
6714 {
6715         struct mddev *mddev = disk->private_data;
6716
6717         mddev->changed = 0;
6718         return 0;
6719 }
6720 static const struct block_device_operations md_fops =
6721 {
6722         .owner          = THIS_MODULE,
6723         .open           = md_open,
6724         .release        = md_release,
6725         .ioctl          = md_ioctl,
6726 #ifdef CONFIG_COMPAT
6727         .compat_ioctl   = md_compat_ioctl,
6728 #endif
6729         .getgeo         = md_getgeo,
6730         .media_changed  = md_media_changed,
6731         .revalidate_disk= md_revalidate,
6732 };
6733
6734 static int md_thread(void * arg)
6735 {
6736         struct md_thread *thread = arg;
6737
6738         /*
6739          * md_thread is a 'system-thread', it's priority should be very
6740          * high. We avoid resource deadlocks individually in each
6741          * raid personality. (RAID5 does preallocation) We also use RR and
6742          * the very same RT priority as kswapd, thus we will never get
6743          * into a priority inversion deadlock.
6744          *
6745          * we definitely have to have equal or higher priority than
6746          * bdflush, otherwise bdflush will deadlock if there are too
6747          * many dirty RAID5 blocks.
6748          */
6749
6750         allow_signal(SIGKILL);
6751         while (!kthread_should_stop()) {
6752
6753                 /* We need to wait INTERRUPTIBLE so that
6754                  * we don't add to the load-average.
6755                  * That means we need to be sure no signals are
6756                  * pending
6757                  */
6758                 if (signal_pending(current))
6759                         flush_signals(current);
6760
6761                 wait_event_interruptible_timeout
6762                         (thread->wqueue,
6763                          test_bit(THREAD_WAKEUP, &thread->flags)
6764                          || kthread_should_stop(),
6765                          thread->timeout);
6766
6767                 clear_bit(THREAD_WAKEUP, &thread->flags);
6768                 if (!kthread_should_stop())
6769                         thread->run(thread);
6770         }
6771
6772         return 0;
6773 }
6774
6775 void md_wakeup_thread(struct md_thread *thread)
6776 {
6777         if (thread) {
6778                 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
6779                 set_bit(THREAD_WAKEUP, &thread->flags);
6780                 wake_up(&thread->wqueue);
6781         }
6782 }
6783
6784 struct md_thread *md_register_thread(void (*run) (struct md_thread *),
6785                 struct mddev *mddev, const char *name)
6786 {
6787         struct md_thread *thread;
6788
6789         thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
6790         if (!thread)
6791                 return NULL;
6792
6793         init_waitqueue_head(&thread->wqueue);
6794
6795         thread->run = run;
6796         thread->mddev = mddev;
6797         thread->timeout = MAX_SCHEDULE_TIMEOUT;
6798         thread->tsk = kthread_run(md_thread, thread,
6799                                   "%s_%s",
6800                                   mdname(thread->mddev),
6801                                   name);
6802         if (IS_ERR(thread->tsk)) {
6803                 kfree(thread);
6804                 return NULL;
6805         }
6806         return thread;
6807 }
6808
6809 void md_unregister_thread(struct md_thread **threadp)
6810 {
6811         struct md_thread *thread = *threadp;
6812         if (!thread)
6813                 return;
6814         pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
6815         /* Locking ensures that mddev_unlock does not wake_up a
6816          * non-existent thread
6817          */
6818         spin_lock(&pers_lock);
6819         *threadp = NULL;
6820         spin_unlock(&pers_lock);
6821
6822         kthread_stop(thread->tsk);
6823         kfree(thread);
6824 }
6825
6826 void md_error(struct mddev *mddev, struct md_rdev *rdev)
6827 {
6828         if (!mddev) {
6829                 MD_BUG();
6830                 return;
6831         }
6832
6833         if (!rdev || test_bit(Faulty, &rdev->flags))
6834                 return;
6835
6836         if (!mddev->pers || !mddev->pers->error_handler)
6837                 return;
6838         mddev->pers->error_handler(mddev,rdev);
6839         if (mddev->degraded)
6840                 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
6841         sysfs_notify_dirent_safe(rdev->sysfs_state);
6842         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6843         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6844         md_wakeup_thread(mddev->thread);
6845         if (mddev->event_work.func)
6846                 queue_work(md_misc_wq, &mddev->event_work);
6847         md_new_event_inintr(mddev);
6848 }
6849
6850 /* seq_file implementation /proc/mdstat */
6851
6852 static void status_unused(struct seq_file *seq)
6853 {
6854         int i = 0;
6855         struct md_rdev *rdev;
6856
6857         seq_printf(seq, "unused devices: ");
6858
6859         list_for_each_entry(rdev, &pending_raid_disks, same_set) {
6860                 char b[BDEVNAME_SIZE];
6861                 i++;
6862                 seq_printf(seq, "%s ",
6863                               bdevname(rdev->bdev,b));
6864         }
6865         if (!i)
6866                 seq_printf(seq, "<none>");
6867
6868         seq_printf(seq, "\n");
6869 }
6870
6871
6872 static void status_resync(struct seq_file *seq, struct mddev * mddev)
6873 {
6874         sector_t max_sectors, resync, res;
6875         unsigned long dt, db;
6876         sector_t rt;
6877         int scale;
6878         unsigned int per_milli;
6879
6880         if (mddev->curr_resync <= 3)
6881                 resync = 0;
6882         else
6883                 resync = mddev->curr_resync
6884                         - atomic_read(&mddev->recovery_active);
6885
6886         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
6887             test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6888                 max_sectors = mddev->resync_max_sectors;
6889         else
6890                 max_sectors = mddev->dev_sectors;
6891
6892         /*
6893          * Should not happen.
6894          */
6895         if (!max_sectors) {
6896                 MD_BUG();
6897                 return;
6898         }
6899         /* Pick 'scale' such that (resync>>scale)*1000 will fit
6900          * in a sector_t, and (max_sectors>>scale) will fit in a
6901          * u32, as those are the requirements for sector_div.
6902          * Thus 'scale' must be at least 10
6903          */
6904         scale = 10;
6905         if (sizeof(sector_t) > sizeof(unsigned long)) {
6906                 while ( max_sectors/2 > (1ULL<<(scale+32)))
6907                         scale++;
6908         }
6909         res = (resync>>scale)*1000;
6910         sector_div(res, (u32)((max_sectors>>scale)+1));
6911
6912         per_milli = res;
6913         {
6914                 int i, x = per_milli/50, y = 20-x;
6915                 seq_printf(seq, "[");
6916                 for (i = 0; i < x; i++)
6917                         seq_printf(seq, "=");
6918                 seq_printf(seq, ">");
6919                 for (i = 0; i < y; i++)
6920                         seq_printf(seq, ".");
6921                 seq_printf(seq, "] ");
6922         }
6923         seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
6924                    (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6925                     "reshape" :
6926                     (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6927                      "check" :
6928                      (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6929                       "resync" : "recovery"))),
6930                    per_milli/10, per_milli % 10,
6931                    (unsigned long long) resync/2,
6932                    (unsigned long long) max_sectors/2);
6933
6934         /*
6935          * dt: time from mark until now
6936          * db: blocks written from mark until now
6937          * rt: remaining time
6938          *
6939          * rt is a sector_t, so could be 32bit or 64bit.
6940          * So we divide before multiply in case it is 32bit and close
6941          * to the limit.
6942          * We scale the divisor (db) by 32 to avoid losing precision
6943          * near the end of resync when the number of remaining sectors
6944          * is close to 'db'.
6945          * We then divide rt by 32 after multiplying by db to compensate.
6946          * The '+1' avoids division by zero if db is very small.
6947          */
6948         dt = ((jiffies - mddev->resync_mark) / HZ);
6949         if (!dt) dt++;
6950         db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6951                 - mddev->resync_mark_cnt;
6952
6953         rt = max_sectors - resync;    /* number of remaining sectors */
6954         sector_div(rt, db/32+1);
6955         rt *= dt;
6956         rt >>= 5;
6957
6958         seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6959                    ((unsigned long)rt % 60)/6);
6960
6961         seq_printf(seq, " speed=%ldK/sec", db/2/dt);
6962 }
6963
6964 static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6965 {
6966         struct list_head *tmp;
6967         loff_t l = *pos;
6968         struct mddev *mddev;
6969
6970         if (l >= 0x10000)
6971                 return NULL;
6972         if (!l--)
6973                 /* header */
6974                 return (void*)1;
6975
6976         spin_lock(&all_mddevs_lock);
6977         list_for_each(tmp,&all_mddevs)
6978                 if (!l--) {
6979                         mddev = list_entry(tmp, struct mddev, all_mddevs);
6980                         mddev_get(mddev);
6981                         spin_unlock(&all_mddevs_lock);
6982                         return mddev;
6983                 }
6984         spin_unlock(&all_mddevs_lock);
6985         if (!l--)
6986                 return (void*)2;/* tail */
6987         return NULL;
6988 }
6989
6990 static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6991 {
6992         struct list_head *tmp;
6993         struct mddev *next_mddev, *mddev = v;
6994         
6995         ++*pos;
6996         if (v == (void*)2)
6997                 return NULL;
6998
6999         spin_lock(&all_mddevs_lock);
7000         if (v == (void*)1)
7001                 tmp = all_mddevs.next;
7002         else
7003                 tmp = mddev->all_mddevs.next;
7004         if (tmp != &all_mddevs)
7005                 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
7006         else {
7007                 next_mddev = (void*)2;
7008                 *pos = 0x10000;
7009         }               
7010         spin_unlock(&all_mddevs_lock);
7011
7012         if (v != (void*)1)
7013                 mddev_put(mddev);
7014         return next_mddev;
7015
7016 }
7017
7018 static void md_seq_stop(struct seq_file *seq, void *v)
7019 {
7020         struct mddev *mddev = v;
7021
7022         if (mddev && v != (void*)1 && v != (void*)2)
7023                 mddev_put(mddev);
7024 }
7025
7026 static int md_seq_show(struct seq_file *seq, void *v)
7027 {
7028         struct mddev *mddev = v;
7029         sector_t sectors;
7030         struct md_rdev *rdev;
7031
7032         if (v == (void*)1) {
7033                 struct md_personality *pers;
7034                 seq_printf(seq, "Personalities : ");
7035                 spin_lock(&pers_lock);
7036                 list_for_each_entry(pers, &pers_list, list)
7037                         seq_printf(seq, "[%s] ", pers->name);
7038
7039                 spin_unlock(&pers_lock);
7040                 seq_printf(seq, "\n");
7041                 seq->poll_event = atomic_read(&md_event_count);
7042                 return 0;
7043         }
7044         if (v == (void*)2) {
7045                 status_unused(seq);
7046                 return 0;
7047         }
7048
7049         if (mddev_lock(mddev) < 0)
7050                 return -EINTR;
7051
7052         if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
7053                 seq_printf(seq, "%s : %sactive", mdname(mddev),
7054                                                 mddev->pers ? "" : "in");
7055                 if (mddev->pers) {
7056                         if (mddev->ro==1)
7057                                 seq_printf(seq, " (read-only)");
7058                         if (mddev->ro==2)
7059                                 seq_printf(seq, " (auto-read-only)");
7060                         seq_printf(seq, " %s", mddev->pers->name);
7061                 }
7062
7063                 sectors = 0;
7064                 rdev_for_each(rdev, mddev) {
7065                         char b[BDEVNAME_SIZE];
7066                         seq_printf(seq, " %s[%d]",
7067                                 bdevname(rdev->bdev,b), rdev->desc_nr);
7068                         if (test_bit(WriteMostly, &rdev->flags))
7069                                 seq_printf(seq, "(W)");
7070                         if (test_bit(Faulty, &rdev->flags)) {
7071                                 seq_printf(seq, "(F)");
7072                                 continue;
7073                         }
7074                         if (rdev->raid_disk < 0)
7075                                 seq_printf(seq, "(S)"); /* spare */
7076                         if (test_bit(Replacement, &rdev->flags))
7077                                 seq_printf(seq, "(R)");
7078                         sectors += rdev->sectors;
7079                 }
7080
7081                 if (!list_empty(&mddev->disks)) {
7082                         if (mddev->pers)
7083                                 seq_printf(seq, "\n      %llu blocks",
7084                                            (unsigned long long)
7085                                            mddev->array_sectors / 2);
7086                         else
7087                                 seq_printf(seq, "\n      %llu blocks",
7088                                            (unsigned long long)sectors / 2);
7089                 }
7090                 if (mddev->persistent) {
7091                         if (mddev->major_version != 0 ||
7092                             mddev->minor_version != 90) {
7093                                 seq_printf(seq," super %d.%d",
7094                                            mddev->major_version,
7095                                            mddev->minor_version);
7096                         }
7097                 } else if (mddev->external)
7098                         seq_printf(seq, " super external:%s",
7099                                    mddev->metadata_type);
7100                 else
7101                         seq_printf(seq, " super non-persistent");
7102
7103                 if (mddev->pers) {
7104                         mddev->pers->status(seq, mddev);
7105                         seq_printf(seq, "\n      ");
7106                         if (mddev->pers->sync_request) {
7107                                 if (mddev->curr_resync > 2) {
7108                                         status_resync(seq, mddev);
7109                                         seq_printf(seq, "\n      ");
7110                                 } else if (mddev->curr_resync >= 1)
7111                                         seq_printf(seq, "\tresync=DELAYED\n      ");
7112                                 else if (mddev->recovery_cp < MaxSector)
7113                                         seq_printf(seq, "\tresync=PENDING\n      ");
7114                         }
7115                 } else
7116                         seq_printf(seq, "\n       ");
7117
7118                 bitmap_status(seq, mddev->bitmap);
7119
7120                 seq_printf(seq, "\n");
7121         }
7122         mddev_unlock(mddev);
7123         
7124         return 0;
7125 }
7126
7127 static const struct seq_operations md_seq_ops = {
7128         .start  = md_seq_start,
7129         .next   = md_seq_next,
7130         .stop   = md_seq_stop,
7131         .show   = md_seq_show,
7132 };
7133
7134 static int md_seq_open(struct inode *inode, struct file *file)
7135 {
7136         struct seq_file *seq;
7137         int error;
7138
7139         error = seq_open(file, &md_seq_ops);
7140         if (error)
7141                 return error;
7142
7143         seq = file->private_data;
7144         seq->poll_event = atomic_read(&md_event_count);
7145         return error;
7146 }
7147
7148 static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
7149 {
7150         struct seq_file *seq = filp->private_data;
7151         int mask;
7152
7153         poll_wait(filp, &md_event_waiters, wait);
7154
7155         /* always allow read */
7156         mask = POLLIN | POLLRDNORM;
7157
7158         if (seq->poll_event != atomic_read(&md_event_count))
7159                 mask |= POLLERR | POLLPRI;
7160         return mask;
7161 }
7162
7163 static const struct file_operations md_seq_fops = {
7164         .owner          = THIS_MODULE,
7165         .open           = md_seq_open,
7166         .read           = seq_read,
7167         .llseek         = seq_lseek,
7168         .release        = seq_release_private,
7169         .poll           = mdstat_poll,
7170 };
7171
7172 int register_md_personality(struct md_personality *p)
7173 {
7174         spin_lock(&pers_lock);
7175         list_add_tail(&p->list, &pers_list);
7176         printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
7177         spin_unlock(&pers_lock);
7178         return 0;
7179 }
7180
7181 int unregister_md_personality(struct md_personality *p)
7182 {
7183         printk(KERN_INFO "md: %s personality unregistered\n", p->name);
7184         spin_lock(&pers_lock);
7185         list_del_init(&p->list);
7186         spin_unlock(&pers_lock);
7187         return 0;
7188 }
7189
7190 static int is_mddev_idle(struct mddev *mddev, int init)
7191 {
7192         struct md_rdev * rdev;
7193         int idle;
7194         int curr_events;
7195
7196         idle = 1;
7197         rcu_read_lock();
7198         rdev_for_each_rcu(rdev, mddev) {
7199                 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
7200                 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
7201                               (int)part_stat_read(&disk->part0, sectors[1]) -
7202                               atomic_read(&disk->sync_io);
7203                 /* sync IO will cause sync_io to increase before the disk_stats
7204                  * as sync_io is counted when a request starts, and
7205                  * disk_stats is counted when it completes.
7206                  * So resync activity will cause curr_events to be smaller than
7207                  * when there was no such activity.
7208                  * non-sync IO will cause disk_stat to increase without
7209                  * increasing sync_io so curr_events will (eventually)
7210                  * be larger than it was before.  Once it becomes
7211                  * substantially larger, the test below will cause
7212                  * the array to appear non-idle, and resync will slow
7213                  * down.
7214                  * If there is a lot of outstanding resync activity when
7215                  * we set last_event to curr_events, then all that activity
7216                  * completing might cause the array to appear non-idle
7217                  * and resync will be slowed down even though there might
7218                  * not have been non-resync activity.  This will only
7219                  * happen once though.  'last_events' will soon reflect
7220                  * the state where there is little or no outstanding
7221                  * resync requests, and further resync activity will
7222                  * always make curr_events less than last_events.
7223                  *
7224                  */
7225                 if (init || curr_events - rdev->last_events > 64) {
7226                         rdev->last_events = curr_events;
7227                         idle = 0;
7228                 }
7229         }
7230         rcu_read_unlock();
7231         return idle;
7232 }
7233
7234 void md_done_sync(struct mddev *mddev, int blocks, int ok)
7235 {
7236         /* another "blocks" (512byte) blocks have been synced */
7237         atomic_sub(blocks, &mddev->recovery_active);
7238         wake_up(&mddev->recovery_wait);
7239         if (!ok) {
7240                 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7241                 set_bit(MD_RECOVERY_ERROR, &mddev->recovery);
7242                 md_wakeup_thread(mddev->thread);
7243                 // stop recovery, signal do_sync ....
7244         }
7245 }
7246
7247
7248 /* md_write_start(mddev, bi)
7249  * If we need to update some array metadata (e.g. 'active' flag
7250  * in superblock) before writing, schedule a superblock update
7251  * and wait for it to complete.
7252  */
7253 void md_write_start(struct mddev *mddev, struct bio *bi)
7254 {
7255         int did_change = 0;
7256         if (bio_data_dir(bi) != WRITE)
7257                 return;
7258
7259         BUG_ON(mddev->ro == 1);
7260         if (mddev->ro == 2) {
7261                 /* need to switch to read/write */
7262                 mddev->ro = 0;
7263                 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7264                 md_wakeup_thread(mddev->thread);
7265                 md_wakeup_thread(mddev->sync_thread);
7266                 did_change = 1;
7267         }
7268         atomic_inc(&mddev->writes_pending);
7269         if (mddev->safemode == 1)
7270                 mddev->safemode = 0;
7271         if (mddev->in_sync) {
7272                 spin_lock_irq(&mddev->write_lock);
7273                 if (mddev->in_sync) {
7274                         mddev->in_sync = 0;
7275                         set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7276                         set_bit(MD_CHANGE_PENDING, &mddev->flags);
7277                         md_wakeup_thread(mddev->thread);
7278                         did_change = 1;
7279                 }
7280                 spin_unlock_irq(&mddev->write_lock);
7281         }
7282         if (did_change)
7283                 sysfs_notify_dirent_safe(mddev->sysfs_state);
7284         wait_event(mddev->sb_wait,
7285                    !test_bit(MD_CHANGE_PENDING, &mddev->flags));
7286 }
7287
7288 void md_write_end(struct mddev *mddev)
7289 {
7290         if (atomic_dec_and_test(&mddev->writes_pending)) {
7291                 if (mddev->safemode == 2)
7292                         md_wakeup_thread(mddev->thread);
7293                 else if (mddev->safemode_delay)
7294                         mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
7295         }
7296 }
7297
7298 /* md_allow_write(mddev)
7299  * Calling this ensures that the array is marked 'active' so that writes
7300  * may proceed without blocking.  It is important to call this before
7301  * attempting a GFP_KERNEL allocation while holding the mddev lock.
7302  * Must be called with mddev_lock held.
7303  *
7304  * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
7305  * is dropped, so return -EAGAIN after notifying userspace.
7306  */
7307 int md_allow_write(struct mddev *mddev)
7308 {
7309         if (!mddev->pers)
7310                 return 0;
7311         if (mddev->ro)
7312                 return 0;
7313         if (!mddev->pers->sync_request)
7314                 return 0;
7315
7316         spin_lock_irq(&mddev->write_lock);
7317         if (mddev->in_sync) {
7318                 mddev->in_sync = 0;
7319                 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7320                 set_bit(MD_CHANGE_PENDING, &mddev->flags);
7321                 if (mddev->safemode_delay &&
7322                     mddev->safemode == 0)
7323                         mddev->safemode = 1;
7324                 spin_unlock_irq(&mddev->write_lock);
7325                 md_update_sb(mddev, 0);
7326                 sysfs_notify_dirent_safe(mddev->sysfs_state);
7327         } else
7328                 spin_unlock_irq(&mddev->write_lock);
7329
7330         if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
7331                 return -EAGAIN;
7332         else
7333                 return 0;
7334 }
7335 EXPORT_SYMBOL_GPL(md_allow_write);
7336
7337 #define SYNC_MARKS      10
7338 #define SYNC_MARK_STEP  (3*HZ)
7339 #define UPDATE_FREQUENCY (5*60*HZ)
7340 void md_do_sync(struct md_thread *thread)
7341 {
7342         struct mddev *mddev = thread->mddev;
7343         struct mddev *mddev2;
7344         unsigned int currspeed = 0,
7345                  window;
7346         sector_t max_sectors,j, io_sectors;
7347         unsigned long mark[SYNC_MARKS];
7348         unsigned long update_time;
7349         sector_t mark_cnt[SYNC_MARKS];
7350         int last_mark,m;
7351         struct list_head *tmp;
7352         sector_t last_check;
7353         int skipped = 0;
7354         struct md_rdev *rdev;
7355         char *desc, *action = NULL;
7356         struct blk_plug plug;
7357
7358         /* just incase thread restarts... */
7359         if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
7360                 return;
7361         if (mddev->ro) /* never try to sync a read-only array */
7362                 return;
7363
7364         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7365                 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) {
7366                         desc = "data-check";
7367                         action = "check";
7368                 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7369                         desc = "requested-resync";
7370                         action = "repair";
7371                 } else
7372                         desc = "resync";
7373         } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7374                 desc = "reshape";
7375         else
7376                 desc = "recovery";
7377
7378         mddev->last_sync_action = action ?: desc;
7379
7380         /* we overload curr_resync somewhat here.
7381          * 0 == not engaged in resync at all
7382          * 2 == checking that there is no conflict with another sync
7383          * 1 == like 2, but have yielded to allow conflicting resync to
7384          *              commense
7385          * other == active in resync - this many blocks
7386          *
7387          * Before starting a resync we must have set curr_resync to
7388          * 2, and then checked that every "conflicting" array has curr_resync
7389          * less than ours.  When we find one that is the same or higher
7390          * we wait on resync_wait.  To avoid deadlock, we reduce curr_resync
7391          * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7392          * This will mean we have to start checking from the beginning again.
7393          *
7394          */
7395
7396         do {
7397                 mddev->curr_resync = 2;
7398
7399         try_again:
7400                 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7401                         goto skip;
7402                 for_each_mddev(mddev2, tmp) {
7403                         if (mddev2 == mddev)
7404                                 continue;
7405                         if (!mddev->parallel_resync
7406                         &&  mddev2->curr_resync
7407                         &&  match_mddev_units(mddev, mddev2)) {
7408                                 DEFINE_WAIT(wq);
7409                                 if (mddev < mddev2 && mddev->curr_resync == 2) {
7410                                         /* arbitrarily yield */
7411                                         mddev->curr_resync = 1;
7412                                         wake_up(&resync_wait);
7413                                 }
7414                                 if (mddev > mddev2 && mddev->curr_resync == 1)
7415                                         /* no need to wait here, we can wait the next
7416                                          * time 'round when curr_resync == 2
7417                                          */
7418                                         continue;
7419                                 /* We need to wait 'interruptible' so as not to
7420                                  * contribute to the load average, and not to
7421                                  * be caught by 'softlockup'
7422                                  */
7423                                 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
7424                                 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7425                                     mddev2->curr_resync >= mddev->curr_resync) {
7426                                         printk(KERN_INFO "md: delaying %s of %s"
7427                                                " until %s has finished (they"
7428                                                " share one or more physical units)\n",
7429                                                desc, mdname(mddev), mdname(mddev2));
7430                                         mddev_put(mddev2);
7431                                         if (signal_pending(current))
7432                                                 flush_signals(current);
7433                                         schedule();
7434                                         finish_wait(&resync_wait, &wq);
7435                                         goto try_again;
7436                                 }
7437                                 finish_wait(&resync_wait, &wq);
7438                         }
7439                 }
7440         } while (mddev->curr_resync < 2);
7441
7442         j = 0;
7443         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7444                 /* resync follows the size requested by the personality,
7445                  * which defaults to physical size, but can be virtual size
7446                  */
7447                 max_sectors = mddev->resync_max_sectors;
7448                 atomic64_set(&mddev->resync_mismatches, 0);
7449                 /* we don't use the checkpoint if there's a bitmap */
7450                 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7451                         j = mddev->resync_min;
7452                 else if (!mddev->bitmap)
7453                         j = mddev->recovery_cp;
7454
7455         } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7456                 max_sectors = mddev->resync_max_sectors;
7457         else {
7458                 /* recovery follows the physical size of devices */
7459                 max_sectors = mddev->dev_sectors;
7460                 j = MaxSector;
7461                 rcu_read_lock();
7462                 rdev_for_each_rcu(rdev, mddev)
7463                         if (rdev->raid_disk >= 0 &&
7464                             !test_bit(Faulty, &rdev->flags) &&
7465                             !test_bit(In_sync, &rdev->flags) &&
7466                             rdev->recovery_offset < j)
7467                                 j = rdev->recovery_offset;
7468                 rcu_read_unlock();
7469         }
7470
7471         printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7472         printk(KERN_INFO "md: minimum _guaranteed_  speed:"
7473                 " %d KB/sec/disk.\n", speed_min(mddev));
7474         printk(KERN_INFO "md: using maximum available idle IO bandwidth "
7475                "(but not more than %d KB/sec) for %s.\n",
7476                speed_max(mddev), desc);
7477
7478         is_mddev_idle(mddev, 1); /* this initializes IO event counters */
7479
7480         io_sectors = 0;
7481         for (m = 0; m < SYNC_MARKS; m++) {
7482                 mark[m] = jiffies;
7483                 mark_cnt[m] = io_sectors;
7484         }
7485         last_mark = 0;
7486         mddev->resync_mark = mark[last_mark];
7487         mddev->resync_mark_cnt = mark_cnt[last_mark];
7488
7489         /*
7490          * Tune reconstruction:
7491          */
7492         window = 32*(PAGE_SIZE/512);
7493         printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7494                 window/2, (unsigned long long)max_sectors/2);
7495
7496         atomic_set(&mddev->recovery_active, 0);
7497         last_check = 0;
7498
7499         if (j>2) {
7500                 printk(KERN_INFO
7501                        "md: resuming %s of %s from checkpoint.\n",
7502                        desc, mdname(mddev));
7503                 mddev->curr_resync = j;
7504         } else
7505                 mddev->curr_resync = 3; /* no longer delayed */
7506         mddev->curr_resync_completed = j;
7507         sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7508         md_new_event(mddev);
7509         update_time = jiffies;
7510
7511         blk_start_plug(&plug);
7512         while (j < max_sectors) {
7513                 sector_t sectors;
7514
7515                 skipped = 0;
7516
7517                 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7518                     ((mddev->curr_resync > mddev->curr_resync_completed &&
7519                       (mddev->curr_resync - mddev->curr_resync_completed)
7520                       > (max_sectors >> 4)) ||
7521                      time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) ||
7522                      (j - mddev->curr_resync_completed)*2
7523                      >= mddev->resync_max - mddev->curr_resync_completed
7524                             )) {
7525                         /* time to update curr_resync_completed */
7526                         wait_event(mddev->recovery_wait,
7527                                    atomic_read(&mddev->recovery_active) == 0);
7528                         mddev->curr_resync_completed = j;
7529                         if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
7530                             j > mddev->recovery_cp)
7531                                 mddev->recovery_cp = j;
7532                         update_time = jiffies;
7533                         set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7534                         sysfs_notify(&mddev->kobj, NULL, "sync_completed");
7535                 }
7536
7537                 while (j >= mddev->resync_max &&
7538                        !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7539                         /* As this condition is controlled by user-space,
7540                          * we can block indefinitely, so use '_interruptible'
7541                          * to avoid triggering warnings.
7542                          */
7543                         flush_signals(current); /* just in case */
7544                         wait_event_interruptible(mddev->recovery_wait,
7545                                                  mddev->resync_max > j
7546                                                  || test_bit(MD_RECOVERY_INTR,
7547                                                              &mddev->recovery));
7548                 }
7549
7550                 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7551                         break;
7552
7553                 sectors = mddev->pers->sync_request(mddev, j, &skipped,
7554                                                   currspeed < speed_min(mddev));
7555                 if (sectors == 0) {
7556                         set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7557                         break;
7558                 }
7559
7560                 if (!skipped) { /* actual IO requested */
7561                         io_sectors += sectors;
7562                         atomic_add(sectors, &mddev->recovery_active);
7563                 }
7564
7565                 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7566                         break;
7567
7568                 j += sectors;
7569                 if (j > 2)
7570                         mddev->curr_resync = j;
7571                 mddev->curr_mark_cnt = io_sectors;
7572                 if (last_check == 0)
7573                         /* this is the earliest that rebuild will be
7574                          * visible in /proc/mdstat
7575                          */
7576                         md_new_event(mddev);
7577
7578                 if (last_check + window > io_sectors || j == max_sectors)
7579                         continue;
7580
7581                 last_check = io_sectors;
7582         repeat:
7583                 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7584                         /* step marks */
7585                         int next = (last_mark+1) % SYNC_MARKS;
7586
7587                         mddev->resync_mark = mark[next];
7588                         mddev->resync_mark_cnt = mark_cnt[next];
7589                         mark[next] = jiffies;
7590                         mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
7591                         last_mark = next;
7592                 }
7593
7594                 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7595                         break;
7596
7597                 /*
7598                  * this loop exits only if either when we are slower than
7599                  * the 'hard' speed limit, or the system was IO-idle for
7600                  * a jiffy.
7601                  * the system might be non-idle CPU-wise, but we only care
7602                  * about not overloading the IO subsystem. (things like an
7603                  * e2fsck being done on the RAID array should execute fast)
7604                  */
7605                 cond_resched();
7606
7607                 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7608                         /((jiffies-mddev->resync_mark)/HZ +1) +1;
7609
7610                 if (currspeed > speed_min(mddev)) {
7611                         if ((currspeed > speed_max(mddev)) ||
7612                                         !is_mddev_idle(mddev, 0)) {
7613                                 msleep(500);
7614                                 goto repeat;
7615                         }
7616                 }
7617         }
7618         printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc,
7619                test_bit(MD_RECOVERY_INTR, &mddev->recovery)
7620                ? "interrupted" : "done");
7621         /*
7622          * this also signals 'finished resyncing' to md_stop
7623          */
7624         blk_finish_plug(&plug);
7625         wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7626
7627         /* tell personality that we are finished */
7628         mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
7629
7630         if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
7631             mddev->curr_resync > 2) {
7632                 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7633                         if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7634                                 if (mddev->curr_resync >= mddev->recovery_cp) {
7635                                         printk(KERN_INFO
7636                                                "md: checkpointing %s of %s.\n",
7637                                                desc, mdname(mddev));
7638                                         if (test_bit(MD_RECOVERY_ERROR,
7639                                                 &mddev->recovery))
7640                                                 mddev->recovery_cp =
7641                                                         mddev->curr_resync_completed;
7642                                         else
7643                                                 mddev->recovery_cp =
7644                                                         mddev->curr_resync;
7645                                 }
7646                         } else
7647                                 mddev->recovery_cp = MaxSector;
7648                 } else {
7649                         if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7650                                 mddev->curr_resync = MaxSector;
7651                         rcu_read_lock();
7652                         rdev_for_each_rcu(rdev, mddev)
7653                                 if (rdev->raid_disk >= 0 &&
7654                                     mddev->delta_disks >= 0 &&
7655                                     !test_bit(Faulty, &rdev->flags) &&
7656                                     !test_bit(In_sync, &rdev->flags) &&
7657                                     rdev->recovery_offset < mddev->curr_resync)
7658                                         rdev->recovery_offset = mddev->curr_resync;
7659                         rcu_read_unlock();
7660                 }
7661         }
7662  skip:
7663         set_bit(MD_CHANGE_DEVS, &mddev->flags);
7664
7665         if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7666                 /* We completed so min/max setting can be forgotten if used. */
7667                 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7668                         mddev->resync_min = 0;
7669                 mddev->resync_max = MaxSector;
7670         } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7671                 mddev->resync_min = mddev->curr_resync_completed;
7672         mddev->curr_resync = 0;
7673         wake_up(&resync_wait);
7674         set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7675         md_wakeup_thread(mddev->thread);
7676         return;
7677 }
7678 EXPORT_SYMBOL_GPL(md_do_sync);
7679
7680 static int remove_and_add_spares(struct mddev *mddev,
7681                                  struct md_rdev *this)
7682 {
7683         struct md_rdev *rdev;
7684         int spares = 0;
7685         int removed = 0;
7686
7687         rdev_for_each(rdev, mddev)
7688                 if ((this == NULL || rdev == this) &&
7689                     rdev->raid_disk >= 0 &&
7690                     !test_bit(Blocked, &rdev->flags) &&
7691                     (test_bit(Faulty, &rdev->flags) ||
7692                      ! test_bit(In_sync, &rdev->flags)) &&
7693                     atomic_read(&rdev->nr_pending)==0) {
7694                         if (mddev->pers->hot_remove_disk(
7695                                     mddev, rdev) == 0) {
7696                                 sysfs_unlink_rdev(mddev, rdev);
7697                                 rdev->raid_disk = -1;
7698                                 removed++;
7699                         }
7700                 }
7701         if (removed && mddev->kobj.sd)
7702                 sysfs_notify(&mddev->kobj, NULL, "degraded");
7703
7704         if (this)
7705                 goto no_add;
7706
7707         rdev_for_each(rdev, mddev) {
7708                 if (rdev->raid_disk >= 0 &&
7709                     !test_bit(In_sync, &rdev->flags) &&
7710                     !test_bit(Faulty, &rdev->flags))
7711                         spares++;
7712                 if (rdev->raid_disk >= 0)
7713                         continue;
7714                 if (test_bit(Faulty, &rdev->flags))
7715                         continue;
7716                 if (mddev->ro &&
7717                     ! (rdev->saved_raid_disk >= 0 &&
7718                        !test_bit(Bitmap_sync, &rdev->flags)))
7719                         continue;
7720
7721                 rdev->recovery_offset = 0;
7722                 if (mddev->pers->
7723                     hot_add_disk(mddev, rdev) == 0) {
7724                         if (sysfs_link_rdev(mddev, rdev))
7725                                 /* failure here is OK */;
7726                         spares++;
7727                         md_new_event(mddev);
7728                         set_bit(MD_CHANGE_DEVS, &mddev->flags);
7729                 }
7730         }
7731 no_add:
7732         if (removed)
7733                 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7734         return spares;
7735 }
7736
7737 /*
7738  * This routine is regularly called by all per-raid-array threads to
7739  * deal with generic issues like resync and super-block update.
7740  * Raid personalities that don't have a thread (linear/raid0) do not
7741  * need this as they never do any recovery or update the superblock.
7742  *
7743  * It does not do any resync itself, but rather "forks" off other threads
7744  * to do that as needed.
7745  * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7746  * "->recovery" and create a thread at ->sync_thread.
7747  * When the thread finishes it sets MD_RECOVERY_DONE
7748  * and wakeups up this thread which will reap the thread and finish up.
7749  * This thread also removes any faulty devices (with nr_pending == 0).
7750  *
7751  * The overall approach is:
7752  *  1/ if the superblock needs updating, update it.
7753  *  2/ If a recovery thread is running, don't do anything else.
7754  *  3/ If recovery has finished, clean up, possibly marking spares active.
7755  *  4/ If there are any faulty devices, remove them.
7756  *  5/ If array is degraded, try to add spares devices
7757  *  6/ If array has spares or is not in-sync, start a resync thread.
7758  */
7759 void md_check_recovery(struct mddev *mddev)
7760 {
7761         if (mddev->suspended)
7762                 return;
7763
7764         if (mddev->bitmap)
7765                 bitmap_daemon_work(mddev);
7766
7767         if (signal_pending(current)) {
7768                 if (mddev->pers->sync_request && !mddev->external) {
7769                         printk(KERN_INFO "md: %s in immediate safe mode\n",
7770                                mdname(mddev));
7771                         mddev->safemode = 2;
7772                 }
7773                 flush_signals(current);
7774         }
7775
7776         if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7777                 return;
7778         if ( ! (
7779                 (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) ||
7780                 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7781                 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
7782                 (mddev->external == 0 && mddev->safemode == 1) ||
7783                 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7784                  && !mddev->in_sync && mddev->recovery_cp == MaxSector)
7785                 ))
7786                 return;
7787
7788         if (mddev_trylock(mddev)) {
7789                 int spares = 0;
7790
7791                 if (mddev->ro) {
7792                         /* On a read-only array we can:
7793                          * - remove failed devices
7794                          * - add already-in_sync devices if the array itself
7795                          *   is in-sync.
7796                          * As we only add devices that are already in-sync,
7797                          * we can activate the spares immediately.
7798                          */
7799                         remove_and_add_spares(mddev, NULL);
7800                         /* There is no thread, but we need to call
7801                          * ->spare_active and clear saved_raid_disk
7802                          */
7803                         md_reap_sync_thread(mddev);
7804                         clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7805                         goto unlock;
7806                 }
7807
7808                 if (!mddev->external) {
7809                         int did_change = 0;
7810                         spin_lock_irq(&mddev->write_lock);
7811                         if (mddev->safemode &&
7812                             !atomic_read(&mddev->writes_pending) &&
7813                             !mddev->in_sync &&
7814                             mddev->recovery_cp == MaxSector) {
7815                                 mddev->in_sync = 1;
7816                                 did_change = 1;
7817                                 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7818                         }
7819                         if (mddev->safemode == 1)
7820                                 mddev->safemode = 0;
7821                         spin_unlock_irq(&mddev->write_lock);
7822                         if (did_change)
7823                                 sysfs_notify_dirent_safe(mddev->sysfs_state);
7824                 }
7825
7826                 if (mddev->flags & MD_UPDATE_SB_FLAGS)
7827                         md_update_sb(mddev, 0);
7828
7829                 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7830                     !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7831                         /* resync/recovery still happening */
7832                         clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7833                         goto unlock;
7834                 }
7835                 if (mddev->sync_thread) {
7836                         md_reap_sync_thread(mddev);
7837                         goto unlock;
7838                 }
7839                 /* Set RUNNING before clearing NEEDED to avoid
7840                  * any transients in the value of "sync_action".
7841                  */
7842                 mddev->curr_resync_completed = 0;
7843                 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7844                 /* Clear some bits that don't mean anything, but
7845                  * might be left set
7846                  */
7847                 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7848                 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
7849
7850                 if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
7851                     test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7852                         goto unlock;
7853                 /* no recovery is running.
7854                  * remove any failed drives, then
7855                  * add spares if possible.
7856                  * Spares are also removed and re-added, to allow
7857                  * the personality to fail the re-add.
7858                  */
7859
7860                 if (mddev->reshape_position != MaxSector) {
7861                         if (mddev->pers->check_reshape == NULL ||
7862                             mddev->pers->check_reshape(mddev) != 0)
7863                                 /* Cannot proceed */
7864                                 goto unlock;
7865                         set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7866                         clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7867                 } else if ((spares = remove_and_add_spares(mddev, NULL))) {
7868                         clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7869                         clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7870                         clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7871                         set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7872                 } else if (mddev->recovery_cp < MaxSector) {
7873                         set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7874                         clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
7875                 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7876                         /* nothing to be done ... */
7877                         goto unlock;
7878
7879                 if (mddev->pers->sync_request) {
7880                         if (spares) {
7881                                 /* We are adding a device or devices to an array
7882                                  * which has the bitmap stored on all devices.
7883                                  * So make sure all bitmap pages get written
7884                                  */
7885                                 bitmap_write_all(mddev->bitmap);
7886                         }
7887                         mddev->sync_thread = md_register_thread(md_do_sync,
7888                                                                 mddev,
7889                                                                 "resync");
7890                         if (!mddev->sync_thread) {
7891                                 printk(KERN_ERR "%s: could not start resync"
7892                                         " thread...\n", 
7893                                         mdname(mddev));
7894                                 /* leave the spares where they are, it shouldn't hurt */
7895                                 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7896                                 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7897                                 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7898                                 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7899                                 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7900                         } else
7901                                 md_wakeup_thread(mddev->sync_thread);
7902                         sysfs_notify_dirent_safe(mddev->sysfs_action);
7903                         md_new_event(mddev);
7904                 }
7905         unlock:
7906                 wake_up(&mddev->sb_wait);
7907
7908                 if (!mddev->sync_thread) {
7909                         clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7910                         if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7911                                                &mddev->recovery))
7912                                 if (mddev->sysfs_action)
7913                                         sysfs_notify_dirent_safe(mddev->sysfs_action);
7914                 }
7915                 mddev_unlock(mddev);
7916         }
7917 }
7918
7919 void md_reap_sync_thread(struct mddev *mddev)
7920 {
7921         struct md_rdev *rdev;
7922
7923         /* resync has finished, collect result */
7924         md_unregister_thread(&mddev->sync_thread);
7925         wake_up(&resync_wait);
7926         if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7927             !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7928                 /* success...*/
7929                 /* activate any spares */
7930                 if (mddev->pers->spare_active(mddev)) {
7931                         sysfs_notify(&mddev->kobj, NULL,
7932                                      "degraded");
7933                         set_bit(MD_CHANGE_DEVS, &mddev->flags);
7934                 }
7935         }
7936         if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7937             mddev->pers->finish_reshape)
7938                 mddev->pers->finish_reshape(mddev);
7939
7940         /* If array is no-longer degraded, then any saved_raid_disk
7941          * information must be scrapped.  Also if any device is now
7942          * In_sync we must scrape the saved_raid_disk for that device
7943          * do the superblock for an incrementally recovered device
7944          * written out.
7945          */
7946         rdev_for_each(rdev, mddev)
7947                 if (!mddev->degraded ||
7948                     test_bit(In_sync, &rdev->flags))
7949                         rdev->saved_raid_disk = -1;
7950
7951         md_update_sb(mddev, 1);
7952         clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7953         clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7954         clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7955         clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7956         clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7957         /* flag recovery needed just to double check */
7958         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7959         sysfs_notify_dirent_safe(mddev->sysfs_action);
7960         md_new_event(mddev);
7961         if (mddev->event_work.func)
7962                 queue_work(md_misc_wq, &mddev->event_work);
7963 }
7964
7965 void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
7966 {
7967         sysfs_notify_dirent_safe(rdev->sysfs_state);
7968         wait_event_timeout(rdev->blocked_wait,
7969                            !test_bit(Blocked, &rdev->flags) &&
7970                            !test_bit(BlockedBadBlocks, &rdev->flags),
7971                            msecs_to_jiffies(5000));
7972         rdev_dec_pending(rdev, mddev);
7973 }
7974 EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7975
7976 void md_finish_reshape(struct mddev *mddev)
7977 {
7978         /* called be personality module when reshape completes. */
7979         struct md_rdev *rdev;
7980
7981         rdev_for_each(rdev, mddev) {
7982                 if (rdev->data_offset > rdev->new_data_offset)
7983                         rdev->sectors += rdev->data_offset - rdev->new_data_offset;
7984                 else
7985                         rdev->sectors -= rdev->new_data_offset - rdev->data_offset;
7986                 rdev->data_offset = rdev->new_data_offset;
7987         }
7988 }
7989 EXPORT_SYMBOL(md_finish_reshape);
7990
7991 /* Bad block management.
7992  * We can record which blocks on each device are 'bad' and so just
7993  * fail those blocks, or that stripe, rather than the whole device.
7994  * Entries in the bad-block table are 64bits wide.  This comprises:
7995  * Length of bad-range, in sectors: 0-511 for lengths 1-512
7996  * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7997  *  A 'shift' can be set so that larger blocks are tracked and
7998  *  consequently larger devices can be covered.
7999  * 'Acknowledged' flag - 1 bit. - the most significant bit.
8000  *
8001  * Locking of the bad-block table uses a seqlock so md_is_badblock
8002  * might need to retry if it is very unlucky.
8003  * We will sometimes want to check for bad blocks in a bi_end_io function,
8004  * so we use the write_seqlock_irq variant.
8005  *
8006  * When looking for a bad block we specify a range and want to
8007  * know if any block in the range is bad.  So we binary-search
8008  * to the last range that starts at-or-before the given endpoint,
8009  * (or "before the sector after the target range")
8010  * then see if it ends after the given start.
8011  * We return
8012  *  0 if there are no known bad blocks in the range
8013  *  1 if there are known bad block which are all acknowledged
8014  * -1 if there are bad blocks which have not yet been acknowledged in metadata.
8015  * plus the start/length of the first bad section we overlap.
8016  */
8017 int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
8018                    sector_t *first_bad, int *bad_sectors)
8019 {
8020         int hi;
8021         int lo;
8022         u64 *p = bb->page;
8023         int rv;
8024         sector_t target = s + sectors;
8025         unsigned seq;
8026
8027         if (bb->shift > 0) {
8028                 /* round the start down, and the end up */
8029                 s >>= bb->shift;
8030                 target += (1<<bb->shift) - 1;
8031                 target >>= bb->shift;
8032                 sectors = target - s;
8033         }
8034         /* 'target' is now the first block after the bad range */
8035
8036 retry:
8037         seq = read_seqbegin(&bb->lock);
8038         lo = 0;
8039         rv = 0;
8040         hi = bb->count;
8041
8042         /* Binary search between lo and hi for 'target'
8043          * i.e. for the last range that starts before 'target'
8044          */
8045         /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
8046          * are known not to be the last range before target.
8047          * VARIANT: hi-lo is the number of possible
8048          * ranges, and decreases until it reaches 1
8049          */
8050         while (hi - lo > 1) {
8051                 int mid = (lo + hi) / 2;
8052                 sector_t a = BB_OFFSET(p[mid]);
8053                 if (a < target)
8054                         /* This could still be the one, earlier ranges
8055                          * could not. */
8056                         lo = mid;
8057                 else
8058                         /* This and later ranges are definitely out. */
8059                         hi = mid;
8060         }
8061         /* 'lo' might be the last that started before target, but 'hi' isn't */
8062         if (hi > lo) {
8063                 /* need to check all range that end after 's' to see if
8064                  * any are unacknowledged.
8065                  */
8066                 while (lo >= 0 &&
8067                        BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8068                         if (BB_OFFSET(p[lo]) < target) {
8069                                 /* starts before the end, and finishes after
8070                                  * the start, so they must overlap
8071                                  */
8072                                 if (rv != -1 && BB_ACK(p[lo]))
8073                                         rv = 1;
8074                                 else
8075                                         rv = -1;
8076                                 *first_bad = BB_OFFSET(p[lo]);
8077                                 *bad_sectors = BB_LEN(p[lo]);
8078                         }
8079                         lo--;
8080                 }
8081         }
8082
8083         if (read_seqretry(&bb->lock, seq))
8084                 goto retry;
8085
8086         return rv;
8087 }
8088 EXPORT_SYMBOL_GPL(md_is_badblock);
8089
8090 /*
8091  * Add a range of bad blocks to the table.
8092  * This might extend the table, or might contract it
8093  * if two adjacent ranges can be merged.
8094  * We binary-search to find the 'insertion' point, then
8095  * decide how best to handle it.
8096  */
8097 static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
8098                             int acknowledged)
8099 {
8100         u64 *p;
8101         int lo, hi;
8102         int rv = 1;
8103         unsigned long flags;
8104
8105         if (bb->shift < 0)
8106                 /* badblocks are disabled */
8107                 return 0;
8108
8109         if (bb->shift) {
8110                 /* round the start down, and the end up */
8111                 sector_t next = s + sectors;
8112                 s >>= bb->shift;
8113                 next += (1<<bb->shift) - 1;
8114                 next >>= bb->shift;
8115                 sectors = next - s;
8116         }
8117
8118         write_seqlock_irqsave(&bb->lock, flags);
8119
8120         p = bb->page;
8121         lo = 0;
8122         hi = bb->count;
8123         /* Find the last range that starts at-or-before 's' */
8124         while (hi - lo > 1) {
8125                 int mid = (lo + hi) / 2;
8126                 sector_t a = BB_OFFSET(p[mid]);
8127                 if (a <= s)
8128                         lo = mid;
8129                 else
8130                         hi = mid;
8131         }
8132         if (hi > lo && BB_OFFSET(p[lo]) > s)
8133                 hi = lo;
8134
8135         if (hi > lo) {
8136                 /* we found a range that might merge with the start
8137                  * of our new range
8138                  */
8139                 sector_t a = BB_OFFSET(p[lo]);
8140                 sector_t e = a + BB_LEN(p[lo]);
8141                 int ack = BB_ACK(p[lo]);
8142                 if (e >= s) {
8143                         /* Yes, we can merge with a previous range */
8144                         if (s == a && s + sectors >= e)
8145                                 /* new range covers old */
8146                                 ack = acknowledged;
8147                         else
8148                                 ack = ack && acknowledged;
8149
8150                         if (e < s + sectors)
8151                                 e = s + sectors;
8152                         if (e - a <= BB_MAX_LEN) {
8153                                 p[lo] = BB_MAKE(a, e-a, ack);
8154                                 s = e;
8155                         } else {
8156                                 /* does not all fit in one range,
8157                                  * make p[lo] maximal
8158                                  */
8159                                 if (BB_LEN(p[lo]) != BB_MAX_LEN)
8160                                         p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
8161                                 s = a + BB_MAX_LEN;
8162                         }
8163                         sectors = e - s;
8164                 }
8165         }
8166         if (sectors && hi < bb->count) {
8167                 /* 'hi' points to the first range that starts after 's'.
8168                  * Maybe we can merge with the start of that range */
8169                 sector_t a = BB_OFFSET(p[hi]);
8170                 sector_t e = a + BB_LEN(p[hi]);
8171                 int ack = BB_ACK(p[hi]);
8172                 if (a <= s + sectors) {
8173                         /* merging is possible */
8174                         if (e <= s + sectors) {
8175                                 /* full overlap */
8176                                 e = s + sectors;
8177                                 ack = acknowledged;
8178                         } else
8179                                 ack = ack && acknowledged;
8180
8181                         a = s;
8182                         if (e - a <= BB_MAX_LEN) {
8183                                 p[hi] = BB_MAKE(a, e-a, ack);
8184                                 s = e;
8185                         } else {
8186                                 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
8187                                 s = a + BB_MAX_LEN;
8188                         }
8189                         sectors = e - s;
8190                         lo = hi;
8191                         hi++;
8192                 }
8193         }
8194         if (sectors == 0 && hi < bb->count) {
8195                 /* we might be able to combine lo and hi */
8196                 /* Note: 's' is at the end of 'lo' */
8197                 sector_t a = BB_OFFSET(p[hi]);
8198                 int lolen = BB_LEN(p[lo]);
8199                 int hilen = BB_LEN(p[hi]);
8200                 int newlen = lolen + hilen - (s - a);
8201                 if (s >= a && newlen < BB_MAX_LEN) {
8202                         /* yes, we can combine them */
8203                         int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
8204                         p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
8205                         memmove(p + hi, p + hi + 1,
8206                                 (bb->count - hi - 1) * 8);
8207                         bb->count--;
8208                 }
8209         }
8210         while (sectors) {
8211                 /* didn't merge (it all).
8212                  * Need to add a range just before 'hi' */
8213                 if (bb->count >= MD_MAX_BADBLOCKS) {
8214                         /* No room for more */
8215                         rv = 0;
8216                         break;
8217                 } else {
8218                         int this_sectors = sectors;
8219                         memmove(p + hi + 1, p + hi,
8220                                 (bb->count - hi) * 8);
8221                         bb->count++;
8222
8223                         if (this_sectors > BB_MAX_LEN)
8224                                 this_sectors = BB_MAX_LEN;
8225                         p[hi] = BB_MAKE(s, this_sectors, acknowledged);
8226                         sectors -= this_sectors;
8227                         s += this_sectors;
8228                 }
8229         }
8230
8231         bb->changed = 1;
8232         if (!acknowledged)
8233                 bb->unacked_exist = 1;
8234         write_sequnlock_irqrestore(&bb->lock, flags);
8235
8236         return rv;
8237 }
8238
8239 int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8240                        int is_new)
8241 {
8242         int rv;
8243         if (is_new)
8244                 s += rdev->new_data_offset;
8245         else
8246                 s += rdev->data_offset;
8247         rv = md_set_badblocks(&rdev->badblocks,
8248                               s, sectors, 0);
8249         if (rv) {
8250                 /* Make sure they get written out promptly */
8251                 sysfs_notify_dirent_safe(rdev->sysfs_state);
8252                 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
8253                 md_wakeup_thread(rdev->mddev->thread);
8254         }
8255         return rv;
8256 }
8257 EXPORT_SYMBOL_GPL(rdev_set_badblocks);
8258
8259 /*
8260  * Remove a range of bad blocks from the table.
8261  * This may involve extending the table if we spilt a region,
8262  * but it must not fail.  So if the table becomes full, we just
8263  * drop the remove request.
8264  */
8265 static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
8266 {
8267         u64 *p;
8268         int lo, hi;
8269         sector_t target = s + sectors;
8270         int rv = 0;
8271
8272         if (bb->shift > 0) {
8273                 /* When clearing we round the start up and the end down.
8274                  * This should not matter as the shift should align with
8275                  * the block size and no rounding should ever be needed.
8276                  * However it is better the think a block is bad when it
8277                  * isn't than to think a block is not bad when it is.
8278                  */
8279                 s += (1<<bb->shift) - 1;
8280                 s >>= bb->shift;
8281                 target >>= bb->shift;
8282                 sectors = target - s;
8283         }
8284
8285         write_seqlock_irq(&bb->lock);
8286
8287         p = bb->page;
8288         lo = 0;
8289         hi = bb->count;
8290         /* Find the last range that starts before 'target' */
8291         while (hi - lo > 1) {
8292                 int mid = (lo + hi) / 2;
8293                 sector_t a = BB_OFFSET(p[mid]);
8294                 if (a < target)
8295                         lo = mid;
8296                 else
8297                         hi = mid;
8298         }
8299         if (hi > lo) {
8300                 /* p[lo] is the last range that could overlap the
8301                  * current range.  Earlier ranges could also overlap,
8302                  * but only this one can overlap the end of the range.
8303                  */
8304                 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
8305                         /* Partial overlap, leave the tail of this range */
8306                         int ack = BB_ACK(p[lo]);
8307                         sector_t a = BB_OFFSET(p[lo]);
8308                         sector_t end = a + BB_LEN(p[lo]);
8309
8310                         if (a < s) {
8311                                 /* we need to split this range */
8312                                 if (bb->count >= MD_MAX_BADBLOCKS) {
8313                                         rv = 0;
8314                                         goto out;
8315                                 }
8316                                 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
8317                                 bb->count++;
8318                                 p[lo] = BB_MAKE(a, s-a, ack);
8319                                 lo++;
8320                         }
8321                         p[lo] = BB_MAKE(target, end - target, ack);
8322                         /* there is no longer an overlap */
8323                         hi = lo;
8324                         lo--;
8325                 }
8326                 while (lo >= 0 &&
8327                        BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
8328                         /* This range does overlap */
8329                         if (BB_OFFSET(p[lo]) < s) {
8330                                 /* Keep the early parts of this range. */
8331                                 int ack = BB_ACK(p[lo]);
8332                                 sector_t start = BB_OFFSET(p[lo]);
8333                                 p[lo] = BB_MAKE(start, s - start, ack);
8334                                 /* now low doesn't overlap, so.. */
8335                                 break;
8336                         }
8337                         lo--;
8338                 }
8339                 /* 'lo' is strictly before, 'hi' is strictly after,
8340                  * anything between needs to be discarded
8341                  */
8342                 if (hi - lo > 1) {
8343                         memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
8344                         bb->count -= (hi - lo - 1);
8345                 }
8346         }
8347
8348         bb->changed = 1;
8349 out:
8350         write_sequnlock_irq(&bb->lock);
8351         return rv;
8352 }
8353
8354 int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
8355                          int is_new)
8356 {
8357         if (is_new)
8358                 s += rdev->new_data_offset;
8359         else
8360                 s += rdev->data_offset;
8361         return md_clear_badblocks(&rdev->badblocks,
8362                                   s, sectors);
8363 }
8364 EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
8365
8366 /*
8367  * Acknowledge all bad blocks in a list.
8368  * This only succeeds if ->changed is clear.  It is used by
8369  * in-kernel metadata updates
8370  */
8371 void md_ack_all_badblocks(struct badblocks *bb)
8372 {
8373         if (bb->page == NULL || bb->changed)
8374                 /* no point even trying */
8375                 return;
8376         write_seqlock_irq(&bb->lock);
8377
8378         if (bb->changed == 0 && bb->unacked_exist) {
8379                 u64 *p = bb->page;
8380                 int i;
8381                 for (i = 0; i < bb->count ; i++) {
8382                         if (!BB_ACK(p[i])) {
8383                                 sector_t start = BB_OFFSET(p[i]);
8384                                 int len = BB_LEN(p[i]);
8385                                 p[i] = BB_MAKE(start, len, 1);
8386                         }
8387                 }
8388                 bb->unacked_exist = 0;
8389         }
8390         write_sequnlock_irq(&bb->lock);
8391 }
8392 EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
8393
8394 /* sysfs access to bad-blocks list.
8395  * We present two files.
8396  * 'bad-blocks' lists sector numbers and lengths of ranges that
8397  *    are recorded as bad.  The list is truncated to fit within
8398  *    the one-page limit of sysfs.
8399  *    Writing "sector length" to this file adds an acknowledged
8400  *    bad block list.
8401  * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
8402  *    been acknowledged.  Writing to this file adds bad blocks
8403  *    without acknowledging them.  This is largely for testing.
8404  */
8405
8406 static ssize_t
8407 badblocks_show(struct badblocks *bb, char *page, int unack)
8408 {
8409         size_t len;
8410         int i;
8411         u64 *p = bb->page;
8412         unsigned seq;
8413
8414         if (bb->shift < 0)
8415                 return 0;
8416
8417 retry:
8418         seq = read_seqbegin(&bb->lock);
8419
8420         len = 0;
8421         i = 0;
8422
8423         while (len < PAGE_SIZE && i < bb->count) {
8424                 sector_t s = BB_OFFSET(p[i]);
8425                 unsigned int length = BB_LEN(p[i]);
8426                 int ack = BB_ACK(p[i]);
8427                 i++;
8428
8429                 if (unack && ack)
8430                         continue;
8431
8432                 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8433                                 (unsigned long long)s << bb->shift,
8434                                 length << bb->shift);
8435         }
8436         if (unack && len == 0)
8437                 bb->unacked_exist = 0;
8438
8439         if (read_seqretry(&bb->lock, seq))
8440                 goto retry;
8441
8442         return len;
8443 }
8444
8445 #define DO_DEBUG 1
8446
8447 static ssize_t
8448 badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8449 {
8450         unsigned long long sector;
8451         int length;
8452         char newline;
8453 #ifdef DO_DEBUG
8454         /* Allow clearing via sysfs *only* for testing/debugging.
8455          * Normally only a successful write may clear a badblock
8456          */
8457         int clear = 0;
8458         if (page[0] == '-') {
8459                 clear = 1;
8460                 page++;
8461         }
8462 #endif /* DO_DEBUG */
8463
8464         switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8465         case 3:
8466                 if (newline != '\n')
8467                         return -EINVAL;
8468         case 2:
8469                 if (length <= 0)
8470                         return -EINVAL;
8471                 break;
8472         default:
8473                 return -EINVAL;
8474         }
8475
8476 #ifdef DO_DEBUG
8477         if (clear) {
8478                 md_clear_badblocks(bb, sector, length);
8479                 return len;
8480         }
8481 #endif /* DO_DEBUG */
8482         if (md_set_badblocks(bb, sector, length, !unack))
8483                 return len;
8484         else
8485                 return -ENOSPC;
8486 }
8487
8488 static int md_notify_reboot(struct notifier_block *this,
8489                             unsigned long code, void *x)
8490 {
8491         struct list_head *tmp;
8492         struct mddev *mddev;
8493         int need_delay = 0;
8494
8495         for_each_mddev(mddev, tmp) {
8496                 if (mddev_trylock(mddev)) {
8497                         if (mddev->pers)
8498                                 __md_stop_writes(mddev);
8499                         mddev->safemode = 2;
8500                         mddev_unlock(mddev);
8501                 }
8502                 need_delay = 1;
8503         }
8504         /*
8505          * certain more exotic SCSI devices are known to be
8506          * volatile wrt too early system reboots. While the
8507          * right place to handle this issue is the given
8508          * driver, we do want to have a safe RAID driver ...
8509          */
8510         if (need_delay)
8511                 mdelay(1000*1);
8512
8513         return NOTIFY_DONE;
8514 }
8515
8516 static struct notifier_block md_notifier = {
8517         .notifier_call  = md_notify_reboot,
8518         .next           = NULL,
8519         .priority       = INT_MAX, /* before any real devices */
8520 };
8521
8522 static void md_geninit(void)
8523 {
8524         pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8525
8526         proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
8527 }
8528
8529 static int __init md_init(void)
8530 {
8531         int ret = -ENOMEM;
8532
8533         md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
8534         if (!md_wq)
8535                 goto err_wq;
8536
8537         md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8538         if (!md_misc_wq)
8539                 goto err_misc_wq;
8540
8541         if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8542                 goto err_md;
8543
8544         if ((ret = register_blkdev(0, "mdp")) < 0)
8545                 goto err_mdp;
8546         mdp_major = ret;
8547
8548         blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
8549                             md_probe, NULL, NULL);
8550         blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
8551                             md_probe, NULL, NULL);
8552
8553         register_reboot_notifier(&md_notifier);
8554         raid_table_header = register_sysctl_table(raid_root_table);
8555
8556         md_geninit();
8557         return 0;
8558
8559 err_mdp:
8560         unregister_blkdev(MD_MAJOR, "md");
8561 err_md:
8562         destroy_workqueue(md_misc_wq);
8563 err_misc_wq:
8564         destroy_workqueue(md_wq);
8565 err_wq:
8566         return ret;
8567 }
8568
8569 #ifndef MODULE
8570
8571 /*
8572  * Searches all registered partitions for autorun RAID arrays
8573  * at boot time.
8574  */
8575
8576 static LIST_HEAD(all_detected_devices);
8577 struct detected_devices_node {
8578         struct list_head list;
8579         dev_t dev;
8580 };
8581
8582 void md_autodetect_dev(dev_t dev)
8583 {
8584         struct detected_devices_node *node_detected_dev;
8585
8586         node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8587         if (node_detected_dev) {
8588                 node_detected_dev->dev = dev;
8589                 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8590         } else {
8591                 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8592                         ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8593         }
8594 }
8595
8596
8597 static void autostart_arrays(int part)
8598 {
8599         struct md_rdev *rdev;
8600         struct detected_devices_node *node_detected_dev;
8601         dev_t dev;
8602         int i_scanned, i_passed;
8603
8604         i_scanned = 0;
8605         i_passed = 0;
8606
8607         printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8608
8609         while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8610                 i_scanned++;
8611                 node_detected_dev = list_entry(all_detected_devices.next,
8612                                         struct detected_devices_node, list);
8613                 list_del(&node_detected_dev->list);
8614                 dev = node_detected_dev->dev;
8615                 kfree(node_detected_dev);
8616                 rdev = md_import_device(dev,0, 90);
8617                 if (IS_ERR(rdev))
8618                         continue;
8619
8620                 if (test_bit(Faulty, &rdev->flags)) {
8621                         MD_BUG();
8622                         continue;
8623                 }
8624                 set_bit(AutoDetected, &rdev->flags);
8625                 list_add(&rdev->same_set, &pending_raid_disks);
8626                 i_passed++;
8627         }
8628
8629         printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8630                                                 i_scanned, i_passed);
8631
8632         autorun_devices(part);
8633 }
8634
8635 #endif /* !MODULE */
8636
8637 static __exit void md_exit(void)
8638 {
8639         struct mddev *mddev;
8640         struct list_head *tmp;
8641
8642         blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
8643         blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
8644
8645         unregister_blkdev(MD_MAJOR,"md");
8646         unregister_blkdev(mdp_major, "mdp");
8647         unregister_reboot_notifier(&md_notifier);
8648         unregister_sysctl_table(raid_table_header);
8649         remove_proc_entry("mdstat", NULL);
8650         for_each_mddev(mddev, tmp) {
8651                 export_array(mddev);
8652                 mddev->hold_active = 0;
8653         }
8654         destroy_workqueue(md_misc_wq);
8655         destroy_workqueue(md_wq);
8656 }
8657
8658 subsys_initcall(md_init);
8659 module_exit(md_exit)
8660
8661 static int get_ro(char *buffer, struct kernel_param *kp)
8662 {
8663         return sprintf(buffer, "%d", start_readonly);
8664 }
8665 static int set_ro(const char *val, struct kernel_param *kp)
8666 {
8667         char *e;
8668         int num = simple_strtoul(val, &e, 10);
8669         if (*val && (*e == '\0' || *e == '\n')) {
8670                 start_readonly = num;
8671                 return 0;
8672         }
8673         return -EINVAL;
8674 }
8675
8676 module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8677 module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
8678
8679 module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
8680
8681 EXPORT_SYMBOL(register_md_personality);
8682 EXPORT_SYMBOL(unregister_md_personality);
8683 EXPORT_SYMBOL(md_error);
8684 EXPORT_SYMBOL(md_done_sync);
8685 EXPORT_SYMBOL(md_write_start);
8686 EXPORT_SYMBOL(md_write_end);
8687 EXPORT_SYMBOL(md_register_thread);
8688 EXPORT_SYMBOL(md_unregister_thread);
8689 EXPORT_SYMBOL(md_wakeup_thread);
8690 EXPORT_SYMBOL(md_check_recovery);
8691 EXPORT_SYMBOL(md_reap_sync_thread);
8692 MODULE_LICENSE("GPL");
8693 MODULE_DESCRIPTION("MD RAID framework");
8694 MODULE_ALIAS("md");
8695 MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);