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