a71adb3c34b950eb2a7999e300cf8e03762d8845
[sfrench/cifs-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    Errors, Warnings, etc.
35    Please use:
36      pr_crit() for error conditions that risk data loss
37      pr_err() for error conditions that are unexpected, like an IO error
38          or internal inconsistency
39      pr_warn() for error conditions that could have been predicated, like
40          adding a device to an array when it has incompatible metadata
41      pr_info() for every interesting, very rare events, like an array starting
42          or stopping, or resync starting or stopping
43      pr_debug() for everything else.
44
45 */
46
47 #include <linux/sched/signal.h>
48 #include <linux/kthread.h>
49 #include <linux/blkdev.h>
50 #include <linux/badblocks.h>
51 #include <linux/sysctl.h>
52 #include <linux/seq_file.h>
53 #include <linux/fs.h>
54 #include <linux/poll.h>
55 #include <linux/ctype.h>
56 #include <linux/string.h>
57 #include <linux/hdreg.h>
58 #include <linux/proc_fs.h>
59 #include <linux/random.h>
60 #include <linux/module.h>
61 #include <linux/reboot.h>
62 #include <linux/file.h>
63 #include <linux/compat.h>
64 #include <linux/delay.h>
65 #include <linux/raid/md_p.h>
66 #include <linux/raid/md_u.h>
67 #include <linux/slab.h>
68 #include <linux/percpu-refcount.h>
69
70 #include <trace/events/block.h>
71 #include "md.h"
72 #include "md-bitmap.h"
73 #include "md-cluster.h"
74
75 #ifndef MODULE
76 static void autostart_arrays(int part);
77 #endif
78
79 /* pers_list is a list of registered personalities protected
80  * by pers_lock.
81  * pers_lock does extra service to protect accesses to
82  * mddev->thread when the mutex cannot be held.
83  */
84 static LIST_HEAD(pers_list);
85 static DEFINE_SPINLOCK(pers_lock);
86
87 struct md_cluster_operations *md_cluster_ops;
88 EXPORT_SYMBOL(md_cluster_ops);
89 struct module *md_cluster_mod;
90 EXPORT_SYMBOL(md_cluster_mod);
91
92 static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
93 static struct workqueue_struct *md_wq;
94 static struct workqueue_struct *md_misc_wq;
95
96 static int remove_and_add_spares(struct mddev *mddev,
97                                  struct md_rdev *this);
98 static void mddev_detach(struct mddev *mddev);
99
100 /*
101  * Default number of read corrections we'll attempt on an rdev
102  * before ejecting it from the array. We divide the read error
103  * count by 2 for every hour elapsed between read errors.
104  */
105 #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
106 /*
107  * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
108  * is 1000 KB/sec, so the extra system load does not show up that much.
109  * Increase it if you want to have more _guaranteed_ speed. Note that
110  * the RAID driver will use the maximum available bandwidth if the IO
111  * subsystem is idle. There is also an 'absolute maximum' reconstruction
112  * speed limit - in case reconstruction slows down your system despite
113  * idle IO detection.
114  *
115  * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
116  * or /sys/block/mdX/md/sync_speed_{min,max}
117  */
118
119 static int sysctl_speed_limit_min = 1000;
120 static int sysctl_speed_limit_max = 200000;
121 static inline int speed_min(struct mddev *mddev)
122 {
123         return mddev->sync_speed_min ?
124                 mddev->sync_speed_min : sysctl_speed_limit_min;
125 }
126
127 static inline int speed_max(struct mddev *mddev)
128 {
129         return mddev->sync_speed_max ?
130                 mddev->sync_speed_max : sysctl_speed_limit_max;
131 }
132
133 static struct ctl_table_header *raid_table_header;
134
135 static struct ctl_table raid_table[] = {
136         {
137                 .procname       = "speed_limit_min",
138                 .data           = &sysctl_speed_limit_min,
139                 .maxlen         = sizeof(int),
140                 .mode           = S_IRUGO|S_IWUSR,
141                 .proc_handler   = proc_dointvec,
142         },
143         {
144                 .procname       = "speed_limit_max",
145                 .data           = &sysctl_speed_limit_max,
146                 .maxlen         = sizeof(int),
147                 .mode           = S_IRUGO|S_IWUSR,
148                 .proc_handler   = proc_dointvec,
149         },
150         { }
151 };
152
153 static struct ctl_table raid_dir_table[] = {
154         {
155                 .procname       = "raid",
156                 .maxlen         = 0,
157                 .mode           = S_IRUGO|S_IXUGO,
158                 .child          = raid_table,
159         },
160         { }
161 };
162
163 static struct ctl_table raid_root_table[] = {
164         {
165                 .procname       = "dev",
166                 .maxlen         = 0,
167                 .mode           = 0555,
168                 .child          = raid_dir_table,
169         },
170         {  }
171 };
172
173 static const struct block_device_operations md_fops;
174
175 static int start_readonly;
176
177 /*
178  * The original mechanism for creating an md device is to create
179  * a device node in /dev and to open it.  This causes races with device-close.
180  * The preferred method is to write to the "new_array" module parameter.
181  * This can avoid races.
182  * Setting create_on_open to false disables the original mechanism
183  * so all the races disappear.
184  */
185 static bool create_on_open = true;
186
187 /* bio_clone_mddev
188  * like bio_clone_bioset, but with a local bio set
189  */
190
191 struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
192                             struct mddev *mddev)
193 {
194         struct bio *b;
195
196         if (!mddev || !mddev->bio_set)
197                 return bio_alloc(gfp_mask, nr_iovecs);
198
199         b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set);
200         if (!b)
201                 return NULL;
202         return b;
203 }
204 EXPORT_SYMBOL_GPL(bio_alloc_mddev);
205
206 static struct bio *md_bio_alloc_sync(struct mddev *mddev)
207 {
208         if (!mddev || !mddev->sync_set)
209                 return bio_alloc(GFP_NOIO, 1);
210
211         return bio_alloc_bioset(GFP_NOIO, 1, mddev->sync_set);
212 }
213
214 /*
215  * We have a system wide 'event count' that is incremented
216  * on any 'interesting' event, and readers of /proc/mdstat
217  * can use 'poll' or 'select' to find out when the event
218  * count increases.
219  *
220  * Events are:
221  *  start array, stop array, error, add device, remove device,
222  *  start build, activate spare
223  */
224 static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
225 static atomic_t md_event_count;
226 void md_new_event(struct mddev *mddev)
227 {
228         atomic_inc(&md_event_count);
229         wake_up(&md_event_waiters);
230 }
231 EXPORT_SYMBOL_GPL(md_new_event);
232
233 /*
234  * Enables to iterate over all existing md arrays
235  * all_mddevs_lock protects this list.
236  */
237 static LIST_HEAD(all_mddevs);
238 static DEFINE_SPINLOCK(all_mddevs_lock);
239
240 /*
241  * iterates through all used mddevs in the system.
242  * We take care to grab the all_mddevs_lock whenever navigating
243  * the list, and to always hold a refcount when unlocked.
244  * Any code which breaks out of this loop while own
245  * a reference to the current mddev and must mddev_put it.
246  */
247 #define for_each_mddev(_mddev,_tmp)                                     \
248                                                                         \
249         for (({ spin_lock(&all_mddevs_lock);                            \
250                 _tmp = all_mddevs.next;                                 \
251                 _mddev = NULL;});                                       \
252              ({ if (_tmp != &all_mddevs)                                \
253                         mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
254                 spin_unlock(&all_mddevs_lock);                          \
255                 if (_mddev) mddev_put(_mddev);                          \
256                 _mddev = list_entry(_tmp, struct mddev, all_mddevs);    \
257                 _tmp != &all_mddevs;});                                 \
258              ({ spin_lock(&all_mddevs_lock);                            \
259                 _tmp = _tmp->next;})                                    \
260                 )
261
262 /* Rather than calling directly into the personality make_request function,
263  * IO requests come here first so that we can check if the device is
264  * being suspended pending a reconfiguration.
265  * We hold a refcount over the call to ->make_request.  By the time that
266  * call has finished, the bio has been linked into some internal structure
267  * and so is visible to ->quiesce(), so we don't need the refcount any more.
268  */
269 static bool is_suspended(struct mddev *mddev, struct bio *bio)
270 {
271         if (mddev->suspended)
272                 return true;
273         if (bio_data_dir(bio) != WRITE)
274                 return false;
275         if (mddev->suspend_lo >= mddev->suspend_hi)
276                 return false;
277         if (bio->bi_iter.bi_sector >= mddev->suspend_hi)
278                 return false;
279         if (bio_end_sector(bio) < mddev->suspend_lo)
280                 return false;
281         return true;
282 }
283
284 void md_handle_request(struct mddev *mddev, struct bio *bio)
285 {
286 check_suspended:
287         rcu_read_lock();
288         if (is_suspended(mddev, bio)) {
289                 DEFINE_WAIT(__wait);
290                 for (;;) {
291                         prepare_to_wait(&mddev->sb_wait, &__wait,
292                                         TASK_UNINTERRUPTIBLE);
293                         if (!is_suspended(mddev, bio))
294                                 break;
295                         rcu_read_unlock();
296                         schedule();
297                         rcu_read_lock();
298                 }
299                 finish_wait(&mddev->sb_wait, &__wait);
300         }
301         atomic_inc(&mddev->active_io);
302         rcu_read_unlock();
303
304         if (!mddev->pers->make_request(mddev, bio)) {
305                 atomic_dec(&mddev->active_io);
306                 wake_up(&mddev->sb_wait);
307                 goto check_suspended;
308         }
309
310         if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
311                 wake_up(&mddev->sb_wait);
312 }
313 EXPORT_SYMBOL(md_handle_request);
314
315 static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio)
316 {
317         const int rw = bio_data_dir(bio);
318         struct mddev *mddev = q->queuedata;
319         unsigned int sectors;
320         int cpu;
321
322         blk_queue_split(q, &bio);
323
324         if (mddev == NULL || mddev->pers == NULL) {
325                 bio_io_error(bio);
326                 return BLK_QC_T_NONE;
327         }
328         if (mddev->ro == 1 && unlikely(rw == WRITE)) {
329                 if (bio_sectors(bio) != 0)
330                         bio->bi_status = BLK_STS_IOERR;
331                 bio_endio(bio);
332                 return BLK_QC_T_NONE;
333         }
334
335         /*
336          * save the sectors now since our bio can
337          * go away inside make_request
338          */
339         sectors = bio_sectors(bio);
340         /* bio could be mergeable after passing to underlayer */
341         bio->bi_opf &= ~REQ_NOMERGE;
342
343         md_handle_request(mddev, bio);
344
345         cpu = part_stat_lock();
346         part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
347         part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
348         part_stat_unlock();
349
350         return BLK_QC_T_NONE;
351 }
352
353 /* mddev_suspend makes sure no new requests are submitted
354  * to the device, and that any requests that have been submitted
355  * are completely handled.
356  * Once mddev_detach() is called and completes, the module will be
357  * completely unused.
358  */
359 void mddev_suspend(struct mddev *mddev)
360 {
361         WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk);
362         lockdep_assert_held(&mddev->reconfig_mutex);
363         if (mddev->suspended++)
364                 return;
365         synchronize_rcu();
366         wake_up(&mddev->sb_wait);
367         set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
368         smp_mb__after_atomic();
369         wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
370         mddev->pers->quiesce(mddev, 1);
371         clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags);
372         wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags));
373
374         del_timer_sync(&mddev->safemode_timer);
375 }
376 EXPORT_SYMBOL_GPL(mddev_suspend);
377
378 void mddev_resume(struct mddev *mddev)
379 {
380         lockdep_assert_held(&mddev->reconfig_mutex);
381         if (--mddev->suspended)
382                 return;
383         wake_up(&mddev->sb_wait);
384         mddev->pers->quiesce(mddev, 0);
385
386         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
387         md_wakeup_thread(mddev->thread);
388         md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
389 }
390 EXPORT_SYMBOL_GPL(mddev_resume);
391
392 int mddev_congested(struct mddev *mddev, int bits)
393 {
394         struct md_personality *pers = mddev->pers;
395         int ret = 0;
396
397         rcu_read_lock();
398         if (mddev->suspended)
399                 ret = 1;
400         else if (pers && pers->congested)
401                 ret = pers->congested(mddev, bits);
402         rcu_read_unlock();
403         return ret;
404 }
405 EXPORT_SYMBOL_GPL(mddev_congested);
406 static int md_congested(void *data, int bits)
407 {
408         struct mddev *mddev = data;
409         return mddev_congested(mddev, bits);
410 }
411
412 /*
413  * Generic flush handling for md
414  */
415
416 static void md_end_flush(struct bio *bio)
417 {
418         struct md_rdev *rdev = bio->bi_private;
419         struct mddev *mddev = rdev->mddev;
420
421         rdev_dec_pending(rdev, mddev);
422
423         if (atomic_dec_and_test(&mddev->flush_pending)) {
424                 /* The pre-request flush has finished */
425                 queue_work(md_wq, &mddev->flush_work);
426         }
427         bio_put(bio);
428 }
429
430 static void md_submit_flush_data(struct work_struct *ws);
431
432 static void submit_flushes(struct work_struct *ws)
433 {
434         struct mddev *mddev = container_of(ws, struct mddev, flush_work);
435         struct md_rdev *rdev;
436
437         INIT_WORK(&mddev->flush_work, md_submit_flush_data);
438         atomic_set(&mddev->flush_pending, 1);
439         rcu_read_lock();
440         rdev_for_each_rcu(rdev, mddev)
441                 if (rdev->raid_disk >= 0 &&
442                     !test_bit(Faulty, &rdev->flags)) {
443                         /* Take two references, one is dropped
444                          * when request finishes, one after
445                          * we reclaim rcu_read_lock
446                          */
447                         struct bio *bi;
448                         atomic_inc(&rdev->nr_pending);
449                         atomic_inc(&rdev->nr_pending);
450                         rcu_read_unlock();
451                         bi = bio_alloc_mddev(GFP_NOIO, 0, mddev);
452                         bi->bi_end_io = md_end_flush;
453                         bi->bi_private = rdev;
454                         bio_set_dev(bi, rdev->bdev);
455                         bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
456                         atomic_inc(&mddev->flush_pending);
457                         submit_bio(bi);
458                         rcu_read_lock();
459                         rdev_dec_pending(rdev, mddev);
460                 }
461         rcu_read_unlock();
462         if (atomic_dec_and_test(&mddev->flush_pending))
463                 queue_work(md_wq, &mddev->flush_work);
464 }
465
466 static void md_submit_flush_data(struct work_struct *ws)
467 {
468         struct mddev *mddev = container_of(ws, struct mddev, flush_work);
469         struct bio *bio = mddev->flush_bio;
470
471         /*
472          * must reset flush_bio before calling into md_handle_request to avoid a
473          * deadlock, because other bios passed md_handle_request suspend check
474          * could wait for this and below md_handle_request could wait for those
475          * bios because of suspend check
476          */
477         mddev->flush_bio = NULL;
478         wake_up(&mddev->sb_wait);
479
480         if (bio->bi_iter.bi_size == 0)
481                 /* an empty barrier - all done */
482                 bio_endio(bio);
483         else {
484                 bio->bi_opf &= ~REQ_PREFLUSH;
485                 md_handle_request(mddev, bio);
486         }
487 }
488
489 void md_flush_request(struct mddev *mddev, struct bio *bio)
490 {
491         spin_lock_irq(&mddev->lock);
492         wait_event_lock_irq(mddev->sb_wait,
493                             !mddev->flush_bio,
494                             mddev->lock);
495         mddev->flush_bio = bio;
496         spin_unlock_irq(&mddev->lock);
497
498         INIT_WORK(&mddev->flush_work, submit_flushes);
499         queue_work(md_wq, &mddev->flush_work);
500 }
501 EXPORT_SYMBOL(md_flush_request);
502
503 static inline struct mddev *mddev_get(struct mddev *mddev)
504 {
505         atomic_inc(&mddev->active);
506         return mddev;
507 }
508
509 static void mddev_delayed_delete(struct work_struct *ws);
510
511 static void mddev_put(struct mddev *mddev)
512 {
513         struct bio_set *bs = NULL, *sync_bs = NULL;
514
515         if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
516                 return;
517         if (!mddev->raid_disks && list_empty(&mddev->disks) &&
518             mddev->ctime == 0 && !mddev->hold_active) {
519                 /* Array is not configured at all, and not held active,
520                  * so destroy it */
521                 list_del_init(&mddev->all_mddevs);
522                 bs = mddev->bio_set;
523                 sync_bs = mddev->sync_set;
524                 mddev->bio_set = NULL;
525                 mddev->sync_set = NULL;
526                 if (mddev->gendisk) {
527                         /* We did a probe so need to clean up.  Call
528                          * queue_work inside the spinlock so that
529                          * flush_workqueue() after mddev_find will
530                          * succeed in waiting for the work to be done.
531                          */
532                         INIT_WORK(&mddev->del_work, mddev_delayed_delete);
533                         queue_work(md_misc_wq, &mddev->del_work);
534                 } else
535                         kfree(mddev);
536         }
537         spin_unlock(&all_mddevs_lock);
538         if (bs)
539                 bioset_free(bs);
540         if (sync_bs)
541                 bioset_free(sync_bs);
542 }
543
544 static void md_safemode_timeout(struct timer_list *t);
545
546 void mddev_init(struct mddev *mddev)
547 {
548         mutex_init(&mddev->open_mutex);
549         mutex_init(&mddev->reconfig_mutex);
550         mutex_init(&mddev->bitmap_info.mutex);
551         INIT_LIST_HEAD(&mddev->disks);
552         INIT_LIST_HEAD(&mddev->all_mddevs);
553         timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0);
554         atomic_set(&mddev->active, 1);
555         atomic_set(&mddev->openers, 0);
556         atomic_set(&mddev->active_io, 0);
557         spin_lock_init(&mddev->lock);
558         atomic_set(&mddev->flush_pending, 0);
559         init_waitqueue_head(&mddev->sb_wait);
560         init_waitqueue_head(&mddev->recovery_wait);
561         mddev->reshape_position = MaxSector;
562         mddev->reshape_backwards = 0;
563         mddev->last_sync_action = "none";
564         mddev->resync_min = 0;
565         mddev->resync_max = MaxSector;
566         mddev->level = LEVEL_NONE;
567 }
568 EXPORT_SYMBOL_GPL(mddev_init);
569
570 static struct mddev *mddev_find(dev_t unit)
571 {
572         struct mddev *mddev, *new = NULL;
573
574         if (unit && MAJOR(unit) != MD_MAJOR)
575                 unit &= ~((1<<MdpMinorShift)-1);
576
577  retry:
578         spin_lock(&all_mddevs_lock);
579
580         if (unit) {
581                 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
582                         if (mddev->unit == unit) {
583                                 mddev_get(mddev);
584                                 spin_unlock(&all_mddevs_lock);
585                                 kfree(new);
586                                 return mddev;
587                         }
588
589                 if (new) {
590                         list_add(&new->all_mddevs, &all_mddevs);
591                         spin_unlock(&all_mddevs_lock);
592                         new->hold_active = UNTIL_IOCTL;
593                         return new;
594                 }
595         } else if (new) {
596                 /* find an unused unit number */
597                 static int next_minor = 512;
598                 int start = next_minor;
599                 int is_free = 0;
600                 int dev = 0;
601                 while (!is_free) {
602                         dev = MKDEV(MD_MAJOR, next_minor);
603                         next_minor++;
604                         if (next_minor > MINORMASK)
605                                 next_minor = 0;
606                         if (next_minor == start) {
607                                 /* Oh dear, all in use. */
608                                 spin_unlock(&all_mddevs_lock);
609                                 kfree(new);
610                                 return NULL;
611                         }
612
613                         is_free = 1;
614                         list_for_each_entry(mddev, &all_mddevs, all_mddevs)
615                                 if (mddev->unit == dev) {
616                                         is_free = 0;
617                                         break;
618                                 }
619                 }
620                 new->unit = dev;
621                 new->md_minor = MINOR(dev);
622                 new->hold_active = UNTIL_STOP;
623                 list_add(&new->all_mddevs, &all_mddevs);
624                 spin_unlock(&all_mddevs_lock);
625                 return new;
626         }
627         spin_unlock(&all_mddevs_lock);
628
629         new = kzalloc(sizeof(*new), GFP_KERNEL);
630         if (!new)
631                 return NULL;
632
633         new->unit = unit;
634         if (MAJOR(unit) == MD_MAJOR)
635                 new->md_minor = MINOR(unit);
636         else
637                 new->md_minor = MINOR(unit) >> MdpMinorShift;
638
639         mddev_init(new);
640
641         goto retry;
642 }
643
644 static struct attribute_group md_redundancy_group;
645
646 void mddev_unlock(struct mddev *mddev)
647 {
648         if (mddev->to_remove) {
649                 /* These cannot be removed under reconfig_mutex as
650                  * an access to the files will try to take reconfig_mutex
651                  * while holding the file unremovable, which leads to
652                  * a deadlock.
653                  * So hold set sysfs_active while the remove in happeing,
654                  * and anything else which might set ->to_remove or my
655                  * otherwise change the sysfs namespace will fail with
656                  * -EBUSY if sysfs_active is still set.
657                  * We set sysfs_active under reconfig_mutex and elsewhere
658                  * test it under the same mutex to ensure its correct value
659                  * is seen.
660                  */
661                 struct attribute_group *to_remove = mddev->to_remove;
662                 mddev->to_remove = NULL;
663                 mddev->sysfs_active = 1;
664                 mutex_unlock(&mddev->reconfig_mutex);
665
666                 if (mddev->kobj.sd) {
667                         if (to_remove != &md_redundancy_group)
668                                 sysfs_remove_group(&mddev->kobj, to_remove);
669                         if (mddev->pers == NULL ||
670                             mddev->pers->sync_request == NULL) {
671                                 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
672                                 if (mddev->sysfs_action)
673                                         sysfs_put(mddev->sysfs_action);
674                                 mddev->sysfs_action = NULL;
675                         }
676                 }
677                 mddev->sysfs_active = 0;
678         } else
679                 mutex_unlock(&mddev->reconfig_mutex);
680
681         /* As we've dropped the mutex we need a spinlock to
682          * make sure the thread doesn't disappear
683          */
684         spin_lock(&pers_lock);
685         md_wakeup_thread(mddev->thread);
686         wake_up(&mddev->sb_wait);
687         spin_unlock(&pers_lock);
688 }
689 EXPORT_SYMBOL_GPL(mddev_unlock);
690
691 struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr)
692 {
693         struct md_rdev *rdev;
694
695         rdev_for_each_rcu(rdev, mddev)
696                 if (rdev->desc_nr == nr)
697                         return rdev;
698
699         return NULL;
700 }
701 EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu);
702
703 static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev)
704 {
705         struct md_rdev *rdev;
706
707         rdev_for_each(rdev, mddev)
708                 if (rdev->bdev->bd_dev == dev)
709                         return rdev;
710
711         return NULL;
712 }
713
714 static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev)
715 {
716         struct md_rdev *rdev;
717
718         rdev_for_each_rcu(rdev, mddev)
719                 if (rdev->bdev->bd_dev == dev)
720                         return rdev;
721
722         return NULL;
723 }
724
725 static struct md_personality *find_pers(int level, char *clevel)
726 {
727         struct md_personality *pers;
728         list_for_each_entry(pers, &pers_list, list) {
729                 if (level != LEVEL_NONE && pers->level == level)
730                         return pers;
731                 if (strcmp(pers->name, clevel)==0)
732                         return pers;
733         }
734         return NULL;
735 }
736
737 /* return the offset of the super block in 512byte sectors */
738 static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
739 {
740         sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
741         return MD_NEW_SIZE_SECTORS(num_sectors);
742 }
743
744 static int alloc_disk_sb(struct md_rdev *rdev)
745 {
746         rdev->sb_page = alloc_page(GFP_KERNEL);
747         if (!rdev->sb_page)
748                 return -ENOMEM;
749         return 0;
750 }
751
752 void md_rdev_clear(struct md_rdev *rdev)
753 {
754         if (rdev->sb_page) {
755                 put_page(rdev->sb_page);
756                 rdev->sb_loaded = 0;
757                 rdev->sb_page = NULL;
758                 rdev->sb_start = 0;
759                 rdev->sectors = 0;
760         }
761         if (rdev->bb_page) {
762                 put_page(rdev->bb_page);
763                 rdev->bb_page = NULL;
764         }
765         badblocks_exit(&rdev->badblocks);
766 }
767 EXPORT_SYMBOL_GPL(md_rdev_clear);
768
769 static void super_written(struct bio *bio)
770 {
771         struct md_rdev *rdev = bio->bi_private;
772         struct mddev *mddev = rdev->mddev;
773
774         if (bio->bi_status) {
775                 pr_err("md: super_written gets error=%d\n", bio->bi_status);
776                 md_error(mddev, rdev);
777                 if (!test_bit(Faulty, &rdev->flags)
778                     && (bio->bi_opf & MD_FAILFAST)) {
779                         set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags);
780                         set_bit(LastDev, &rdev->flags);
781                 }
782         } else
783                 clear_bit(LastDev, &rdev->flags);
784
785         if (atomic_dec_and_test(&mddev->pending_writes))
786                 wake_up(&mddev->sb_wait);
787         rdev_dec_pending(rdev, mddev);
788         bio_put(bio);
789 }
790
791 void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
792                    sector_t sector, int size, struct page *page)
793 {
794         /* write first size bytes of page to sector of rdev
795          * Increment mddev->pending_writes before returning
796          * and decrement it on completion, waking up sb_wait
797          * if zero is reached.
798          * If an error occurred, call md_error
799          */
800         struct bio *bio;
801         int ff = 0;
802
803         if (test_bit(Faulty, &rdev->flags))
804                 return;
805
806         bio = md_bio_alloc_sync(mddev);
807
808         atomic_inc(&rdev->nr_pending);
809
810         bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev);
811         bio->bi_iter.bi_sector = sector;
812         bio_add_page(bio, page, size, 0);
813         bio->bi_private = rdev;
814         bio->bi_end_io = super_written;
815
816         if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) &&
817             test_bit(FailFast, &rdev->flags) &&
818             !test_bit(LastDev, &rdev->flags))
819                 ff = MD_FAILFAST;
820         bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff;
821
822         atomic_inc(&mddev->pending_writes);
823         submit_bio(bio);
824 }
825
826 int md_super_wait(struct mddev *mddev)
827 {
828         /* wait for all superblock writes that were scheduled to complete */
829         wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0);
830         if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags))
831                 return -EAGAIN;
832         return 0;
833 }
834
835 int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
836                  struct page *page, int op, int op_flags, bool metadata_op)
837 {
838         struct bio *bio = md_bio_alloc_sync(rdev->mddev);
839         int ret;
840
841         if (metadata_op && rdev->meta_bdev)
842                 bio_set_dev(bio, rdev->meta_bdev);
843         else
844                 bio_set_dev(bio, rdev->bdev);
845         bio_set_op_attrs(bio, op, op_flags);
846         if (metadata_op)
847                 bio->bi_iter.bi_sector = sector + rdev->sb_start;
848         else if (rdev->mddev->reshape_position != MaxSector &&
849                  (rdev->mddev->reshape_backwards ==
850                   (sector >= rdev->mddev->reshape_position)))
851                 bio->bi_iter.bi_sector = sector + rdev->new_data_offset;
852         else
853                 bio->bi_iter.bi_sector = sector + rdev->data_offset;
854         bio_add_page(bio, page, size, 0);
855
856         submit_bio_wait(bio);
857
858         ret = !bio->bi_status;
859         bio_put(bio);
860         return ret;
861 }
862 EXPORT_SYMBOL_GPL(sync_page_io);
863
864 static int read_disk_sb(struct md_rdev *rdev, int size)
865 {
866         char b[BDEVNAME_SIZE];
867
868         if (rdev->sb_loaded)
869                 return 0;
870
871         if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true))
872                 goto fail;
873         rdev->sb_loaded = 1;
874         return 0;
875
876 fail:
877         pr_err("md: disabled device %s, could not read superblock.\n",
878                bdevname(rdev->bdev,b));
879         return -EINVAL;
880 }
881
882 static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
883 {
884         return  sb1->set_uuid0 == sb2->set_uuid0 &&
885                 sb1->set_uuid1 == sb2->set_uuid1 &&
886                 sb1->set_uuid2 == sb2->set_uuid2 &&
887                 sb1->set_uuid3 == sb2->set_uuid3;
888 }
889
890 static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
891 {
892         int ret;
893         mdp_super_t *tmp1, *tmp2;
894
895         tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
896         tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
897
898         if (!tmp1 || !tmp2) {
899                 ret = 0;
900                 goto abort;
901         }
902
903         *tmp1 = *sb1;
904         *tmp2 = *sb2;
905
906         /*
907          * nr_disks is not constant
908          */
909         tmp1->nr_disks = 0;
910         tmp2->nr_disks = 0;
911
912         ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
913 abort:
914         kfree(tmp1);
915         kfree(tmp2);
916         return ret;
917 }
918
919 static u32 md_csum_fold(u32 csum)
920 {
921         csum = (csum & 0xffff) + (csum >> 16);
922         return (csum & 0xffff) + (csum >> 16);
923 }
924
925 static unsigned int calc_sb_csum(mdp_super_t *sb)
926 {
927         u64 newcsum = 0;
928         u32 *sb32 = (u32*)sb;
929         int i;
930         unsigned int disk_csum, csum;
931
932         disk_csum = sb->sb_csum;
933         sb->sb_csum = 0;
934
935         for (i = 0; i < MD_SB_BYTES/4 ; i++)
936                 newcsum += sb32[i];
937         csum = (newcsum & 0xffffffff) + (newcsum>>32);
938
939 #ifdef CONFIG_ALPHA
940         /* This used to use csum_partial, which was wrong for several
941          * reasons including that different results are returned on
942          * different architectures.  It isn't critical that we get exactly
943          * the same return value as before (we always csum_fold before
944          * testing, and that removes any differences).  However as we
945          * know that csum_partial always returned a 16bit value on
946          * alphas, do a fold to maximise conformity to previous behaviour.
947          */
948         sb->sb_csum = md_csum_fold(disk_csum);
949 #else
950         sb->sb_csum = disk_csum;
951 #endif
952         return csum;
953 }
954
955 /*
956  * Handle superblock details.
957  * We want to be able to handle multiple superblock formats
958  * so we have a common interface to them all, and an array of
959  * different handlers.
960  * We rely on user-space to write the initial superblock, and support
961  * reading and updating of superblocks.
962  * Interface methods are:
963  *   int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
964  *      loads and validates a superblock on dev.
965  *      if refdev != NULL, compare superblocks on both devices
966  *    Return:
967  *      0 - dev has a superblock that is compatible with refdev
968  *      1 - dev has a superblock that is compatible and newer than refdev
969  *          so dev should be used as the refdev in future
970  *     -EINVAL superblock incompatible or invalid
971  *     -othererror e.g. -EIO
972  *
973  *   int validate_super(struct mddev *mddev, struct md_rdev *dev)
974  *      Verify that dev is acceptable into mddev.
975  *       The first time, mddev->raid_disks will be 0, and data from
976  *       dev should be merged in.  Subsequent calls check that dev
977  *       is new enough.  Return 0 or -EINVAL
978  *
979  *   void sync_super(struct mddev *mddev, struct md_rdev *dev)
980  *     Update the superblock for rdev with data in mddev
981  *     This does not write to disc.
982  *
983  */
984
985 struct super_type  {
986         char                *name;
987         struct module       *owner;
988         int                 (*load_super)(struct md_rdev *rdev,
989                                           struct md_rdev *refdev,
990                                           int minor_version);
991         int                 (*validate_super)(struct mddev *mddev,
992                                               struct md_rdev *rdev);
993         void                (*sync_super)(struct mddev *mddev,
994                                           struct md_rdev *rdev);
995         unsigned long long  (*rdev_size_change)(struct md_rdev *rdev,
996                                                 sector_t num_sectors);
997         int                 (*allow_new_offset)(struct md_rdev *rdev,
998                                                 unsigned long long new_offset);
999 };
1000
1001 /*
1002  * Check that the given mddev has no bitmap.
1003  *
1004  * This function is called from the run method of all personalities that do not
1005  * support bitmaps. It prints an error message and returns non-zero if mddev
1006  * has a bitmap. Otherwise, it returns 0.
1007  *
1008  */
1009 int md_check_no_bitmap(struct mddev *mddev)
1010 {
1011         if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
1012                 return 0;
1013         pr_warn("%s: bitmaps are not supported for %s\n",
1014                 mdname(mddev), mddev->pers->name);
1015         return 1;
1016 }
1017 EXPORT_SYMBOL(md_check_no_bitmap);
1018
1019 /*
1020  * load_super for 0.90.0
1021  */
1022 static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1023 {
1024         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1025         mdp_super_t *sb;
1026         int ret;
1027
1028         /*
1029          * Calculate the position of the superblock (512byte sectors),
1030          * it's at the end of the disk.
1031          *
1032          * It also happens to be a multiple of 4Kb.
1033          */
1034         rdev->sb_start = calc_dev_sboffset(rdev);
1035
1036         ret = read_disk_sb(rdev, MD_SB_BYTES);
1037         if (ret)
1038                 return ret;
1039
1040         ret = -EINVAL;
1041
1042         bdevname(rdev->bdev, b);
1043         sb = page_address(rdev->sb_page);
1044
1045         if (sb->md_magic != MD_SB_MAGIC) {
1046                 pr_warn("md: invalid raid superblock magic on %s\n", b);
1047                 goto abort;
1048         }
1049
1050         if (sb->major_version != 0 ||
1051             sb->minor_version < 90 ||
1052             sb->minor_version > 91) {
1053                 pr_warn("Bad version number %d.%d on %s\n",
1054                         sb->major_version, sb->minor_version, b);
1055                 goto abort;
1056         }
1057
1058         if (sb->raid_disks <= 0)
1059                 goto abort;
1060
1061         if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
1062                 pr_warn("md: invalid superblock checksum on %s\n", b);
1063                 goto abort;
1064         }
1065
1066         rdev->preferred_minor = sb->md_minor;
1067         rdev->data_offset = 0;
1068         rdev->new_data_offset = 0;
1069         rdev->sb_size = MD_SB_BYTES;
1070         rdev->badblocks.shift = -1;
1071
1072         if (sb->level == LEVEL_MULTIPATH)
1073                 rdev->desc_nr = -1;
1074         else
1075                 rdev->desc_nr = sb->this_disk.number;
1076
1077         if (!refdev) {
1078                 ret = 1;
1079         } else {
1080                 __u64 ev1, ev2;
1081                 mdp_super_t *refsb = page_address(refdev->sb_page);
1082                 if (!md_uuid_equal(refsb, sb)) {
1083                         pr_warn("md: %s has different UUID to %s\n",
1084                                 b, bdevname(refdev->bdev,b2));
1085                         goto abort;
1086                 }
1087                 if (!md_sb_equal(refsb, sb)) {
1088                         pr_warn("md: %s has same UUID but different superblock to %s\n",
1089                                 b, bdevname(refdev->bdev, b2));
1090                         goto abort;
1091                 }
1092                 ev1 = md_event(sb);
1093                 ev2 = md_event(refsb);
1094                 if (ev1 > ev2)
1095                         ret = 1;
1096                 else
1097                         ret = 0;
1098         }
1099         rdev->sectors = rdev->sb_start;
1100         /* Limit to 4TB as metadata cannot record more than that.
1101          * (not needed for Linear and RAID0 as metadata doesn't
1102          * record this size)
1103          */
1104         if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) &&
1105             sb->level >= 1)
1106                 rdev->sectors = (sector_t)(2ULL << 32) - 2;
1107
1108         if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
1109                 /* "this cannot possibly happen" ... */
1110                 ret = -EINVAL;
1111
1112  abort:
1113         return ret;
1114 }
1115
1116 /*
1117  * validate_super for 0.90.0
1118  */
1119 static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
1120 {
1121         mdp_disk_t *desc;
1122         mdp_super_t *sb = page_address(rdev->sb_page);
1123         __u64 ev1 = md_event(sb);
1124
1125         rdev->raid_disk = -1;
1126         clear_bit(Faulty, &rdev->flags);
1127         clear_bit(In_sync, &rdev->flags);
1128         clear_bit(Bitmap_sync, &rdev->flags);
1129         clear_bit(WriteMostly, &rdev->flags);
1130
1131         if (mddev->raid_disks == 0) {
1132                 mddev->major_version = 0;
1133                 mddev->minor_version = sb->minor_version;
1134                 mddev->patch_version = sb->patch_version;
1135                 mddev->external = 0;
1136                 mddev->chunk_sectors = sb->chunk_size >> 9;
1137                 mddev->ctime = sb->ctime;
1138                 mddev->utime = sb->utime;
1139                 mddev->level = sb->level;
1140                 mddev->clevel[0] = 0;
1141                 mddev->layout = sb->layout;
1142                 mddev->raid_disks = sb->raid_disks;
1143                 mddev->dev_sectors = ((sector_t)sb->size) * 2;
1144                 mddev->events = ev1;
1145                 mddev->bitmap_info.offset = 0;
1146                 mddev->bitmap_info.space = 0;
1147                 /* bitmap can use 60 K after the 4K superblocks */
1148                 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
1149                 mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9);
1150                 mddev->reshape_backwards = 0;
1151
1152                 if (mddev->minor_version >= 91) {
1153                         mddev->reshape_position = sb->reshape_position;
1154                         mddev->delta_disks = sb->delta_disks;
1155                         mddev->new_level = sb->new_level;
1156                         mddev->new_layout = sb->new_layout;
1157                         mddev->new_chunk_sectors = sb->new_chunk >> 9;
1158                         if (mddev->delta_disks < 0)
1159                                 mddev->reshape_backwards = 1;
1160                 } else {
1161                         mddev->reshape_position = MaxSector;
1162                         mddev->delta_disks = 0;
1163                         mddev->new_level = mddev->level;
1164                         mddev->new_layout = mddev->layout;
1165                         mddev->new_chunk_sectors = mddev->chunk_sectors;
1166                 }
1167
1168                 if (sb->state & (1<<MD_SB_CLEAN))
1169                         mddev->recovery_cp = MaxSector;
1170                 else {
1171                         if (sb->events_hi == sb->cp_events_hi &&
1172                                 sb->events_lo == sb->cp_events_lo) {
1173                                 mddev->recovery_cp = sb->recovery_cp;
1174                         } else
1175                                 mddev->recovery_cp = 0;
1176                 }
1177
1178                 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1179                 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1180                 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1181                 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1182
1183                 mddev->max_disks = MD_SB_DISKS;
1184
1185                 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
1186                     mddev->bitmap_info.file == NULL) {
1187                         mddev->bitmap_info.offset =
1188                                 mddev->bitmap_info.default_offset;
1189                         mddev->bitmap_info.space =
1190                                 mddev->bitmap_info.default_space;
1191                 }
1192
1193         } else if (mddev->pers == NULL) {
1194                 /* Insist on good event counter while assembling, except
1195                  * for spares (which don't need an event count) */
1196                 ++ev1;
1197                 if (sb->disks[rdev->desc_nr].state & (
1198                             (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1199                         if (ev1 < mddev->events)
1200                                 return -EINVAL;
1201         } else if (mddev->bitmap) {
1202                 /* if adding to array with a bitmap, then we can accept an
1203                  * older device ... but not too old.
1204                  */
1205                 if (ev1 < mddev->bitmap->events_cleared)
1206                         return 0;
1207                 if (ev1 < mddev->events)
1208                         set_bit(Bitmap_sync, &rdev->flags);
1209         } else {
1210                 if (ev1 < mddev->events)
1211                         /* just a hot-add of a new device, leave raid_disk at -1 */
1212                         return 0;
1213         }
1214
1215         if (mddev->level != LEVEL_MULTIPATH) {
1216                 desc = sb->disks + rdev->desc_nr;
1217
1218                 if (desc->state & (1<<MD_DISK_FAULTY))
1219                         set_bit(Faulty, &rdev->flags);
1220                 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1221                             desc->raid_disk < mddev->raid_disks */) {
1222                         set_bit(In_sync, &rdev->flags);
1223                         rdev->raid_disk = desc->raid_disk;
1224                         rdev->saved_raid_disk = desc->raid_disk;
1225                 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1226                         /* active but not in sync implies recovery up to
1227                          * reshape position.  We don't know exactly where
1228                          * that is, so set to zero for now */
1229                         if (mddev->minor_version >= 91) {
1230                                 rdev->recovery_offset = 0;
1231                                 rdev->raid_disk = desc->raid_disk;
1232                         }
1233                 }
1234                 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1235                         set_bit(WriteMostly, &rdev->flags);
1236                 if (desc->state & (1<<MD_DISK_FAILFAST))
1237                         set_bit(FailFast, &rdev->flags);
1238         } else /* MULTIPATH are always insync */
1239                 set_bit(In_sync, &rdev->flags);
1240         return 0;
1241 }
1242
1243 /*
1244  * sync_super for 0.90.0
1245  */
1246 static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
1247 {
1248         mdp_super_t *sb;
1249         struct md_rdev *rdev2;
1250         int next_spare = mddev->raid_disks;
1251
1252         /* make rdev->sb match mddev data..
1253          *
1254          * 1/ zero out disks
1255          * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1256          * 3/ any empty disks < next_spare become removed
1257          *
1258          * disks[0] gets initialised to REMOVED because
1259          * we cannot be sure from other fields if it has
1260          * been initialised or not.
1261          */
1262         int i;
1263         int active=0, working=0,failed=0,spare=0,nr_disks=0;
1264
1265         rdev->sb_size = MD_SB_BYTES;
1266
1267         sb = page_address(rdev->sb_page);
1268
1269         memset(sb, 0, sizeof(*sb));
1270
1271         sb->md_magic = MD_SB_MAGIC;
1272         sb->major_version = mddev->major_version;
1273         sb->patch_version = mddev->patch_version;
1274         sb->gvalid_words  = 0; /* ignored */
1275         memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1276         memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1277         memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1278         memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1279
1280         sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX);
1281         sb->level = mddev->level;
1282         sb->size = mddev->dev_sectors / 2;
1283         sb->raid_disks = mddev->raid_disks;
1284         sb->md_minor = mddev->md_minor;
1285         sb->not_persistent = 0;
1286         sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX);
1287         sb->state = 0;
1288         sb->events_hi = (mddev->events>>32);
1289         sb->events_lo = (u32)mddev->events;
1290
1291         if (mddev->reshape_position == MaxSector)
1292                 sb->minor_version = 90;
1293         else {
1294                 sb->minor_version = 91;
1295                 sb->reshape_position = mddev->reshape_position;
1296                 sb->new_level = mddev->new_level;
1297                 sb->delta_disks = mddev->delta_disks;
1298                 sb->new_layout = mddev->new_layout;
1299                 sb->new_chunk = mddev->new_chunk_sectors << 9;
1300         }
1301         mddev->minor_version = sb->minor_version;
1302         if (mddev->in_sync)
1303         {
1304                 sb->recovery_cp = mddev->recovery_cp;
1305                 sb->cp_events_hi = (mddev->events>>32);
1306                 sb->cp_events_lo = (u32)mddev->events;
1307                 if (mddev->recovery_cp == MaxSector)
1308                         sb->state = (1<< MD_SB_CLEAN);
1309         } else
1310                 sb->recovery_cp = 0;
1311
1312         sb->layout = mddev->layout;
1313         sb->chunk_size = mddev->chunk_sectors << 9;
1314
1315         if (mddev->bitmap && mddev->bitmap_info.file == NULL)
1316                 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1317
1318         sb->disks[0].state = (1<<MD_DISK_REMOVED);
1319         rdev_for_each(rdev2, mddev) {
1320                 mdp_disk_t *d;
1321                 int desc_nr;
1322                 int is_active = test_bit(In_sync, &rdev2->flags);
1323
1324                 if (rdev2->raid_disk >= 0 &&
1325                     sb->minor_version >= 91)
1326                         /* we have nowhere to store the recovery_offset,
1327                          * but if it is not below the reshape_position,
1328                          * we can piggy-back on that.
1329                          */
1330                         is_active = 1;
1331                 if (rdev2->raid_disk < 0 ||
1332                     test_bit(Faulty, &rdev2->flags))
1333                         is_active = 0;
1334                 if (is_active)
1335                         desc_nr = rdev2->raid_disk;
1336                 else
1337                         desc_nr = next_spare++;
1338                 rdev2->desc_nr = desc_nr;
1339                 d = &sb->disks[rdev2->desc_nr];
1340                 nr_disks++;
1341                 d->number = rdev2->desc_nr;
1342                 d->major = MAJOR(rdev2->bdev->bd_dev);
1343                 d->minor = MINOR(rdev2->bdev->bd_dev);
1344                 if (is_active)
1345                         d->raid_disk = rdev2->raid_disk;
1346                 else
1347                         d->raid_disk = rdev2->desc_nr; /* compatibility */
1348                 if (test_bit(Faulty, &rdev2->flags))
1349                         d->state = (1<<MD_DISK_FAULTY);
1350                 else if (is_active) {
1351                         d->state = (1<<MD_DISK_ACTIVE);
1352                         if (test_bit(In_sync, &rdev2->flags))
1353                                 d->state |= (1<<MD_DISK_SYNC);
1354                         active++;
1355                         working++;
1356                 } else {
1357                         d->state = 0;
1358                         spare++;
1359                         working++;
1360                 }
1361                 if (test_bit(WriteMostly, &rdev2->flags))
1362                         d->state |= (1<<MD_DISK_WRITEMOSTLY);
1363                 if (test_bit(FailFast, &rdev2->flags))
1364                         d->state |= (1<<MD_DISK_FAILFAST);
1365         }
1366         /* now set the "removed" and "faulty" bits on any missing devices */
1367         for (i=0 ; i < mddev->raid_disks ; i++) {
1368                 mdp_disk_t *d = &sb->disks[i];
1369                 if (d->state == 0 && d->number == 0) {
1370                         d->number = i;
1371                         d->raid_disk = i;
1372                         d->state = (1<<MD_DISK_REMOVED);
1373                         d->state |= (1<<MD_DISK_FAULTY);
1374                         failed++;
1375                 }
1376         }
1377         sb->nr_disks = nr_disks;
1378         sb->active_disks = active;
1379         sb->working_disks = working;
1380         sb->failed_disks = failed;
1381         sb->spare_disks = spare;
1382
1383         sb->this_disk = sb->disks[rdev->desc_nr];
1384         sb->sb_csum = calc_sb_csum(sb);
1385 }
1386
1387 /*
1388  * rdev_size_change for 0.90.0
1389  */
1390 static unsigned long long
1391 super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1392 {
1393         if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1394                 return 0; /* component must fit device */
1395         if (rdev->mddev->bitmap_info.offset)
1396                 return 0; /* can't move bitmap */
1397         rdev->sb_start = calc_dev_sboffset(rdev);
1398         if (!num_sectors || num_sectors > rdev->sb_start)
1399                 num_sectors = rdev->sb_start;
1400         /* Limit to 4TB as metadata cannot record more than that.
1401          * 4TB == 2^32 KB, or 2*2^32 sectors.
1402          */
1403         if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) &&
1404             rdev->mddev->level >= 1)
1405                 num_sectors = (sector_t)(2ULL << 32) - 2;
1406         do {
1407                 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1408                        rdev->sb_page);
1409         } while (md_super_wait(rdev->mddev) < 0);
1410         return num_sectors;
1411 }
1412
1413 static int
1414 super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset)
1415 {
1416         /* non-zero offset changes not possible with v0.90 */
1417         return new_offset == 0;
1418 }
1419
1420 /*
1421  * version 1 superblock
1422  */
1423
1424 static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb)
1425 {
1426         __le32 disk_csum;
1427         u32 csum;
1428         unsigned long long newcsum;
1429         int size = 256 + le32_to_cpu(sb->max_dev)*2;
1430         __le32 *isuper = (__le32*)sb;
1431
1432         disk_csum = sb->sb_csum;
1433         sb->sb_csum = 0;
1434         newcsum = 0;
1435         for (; size >= 4; size -= 4)
1436                 newcsum += le32_to_cpu(*isuper++);
1437
1438         if (size == 2)
1439                 newcsum += le16_to_cpu(*(__le16*) isuper);
1440
1441         csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1442         sb->sb_csum = disk_csum;
1443         return cpu_to_le32(csum);
1444 }
1445
1446 static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
1447 {
1448         struct mdp_superblock_1 *sb;
1449         int ret;
1450         sector_t sb_start;
1451         sector_t sectors;
1452         char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1453         int bmask;
1454
1455         /*
1456          * Calculate the position of the superblock in 512byte sectors.
1457          * It is always aligned to a 4K boundary and
1458          * depeding on minor_version, it can be:
1459          * 0: At least 8K, but less than 12K, from end of device
1460          * 1: At start of device
1461          * 2: 4K from start of device.
1462          */
1463         switch(minor_version) {
1464         case 0:
1465                 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
1466                 sb_start -= 8*2;
1467                 sb_start &= ~(sector_t)(4*2-1);
1468                 break;
1469         case 1:
1470                 sb_start = 0;
1471                 break;
1472         case 2:
1473                 sb_start = 8;
1474                 break;
1475         default:
1476                 return -EINVAL;
1477         }
1478         rdev->sb_start = sb_start;
1479
1480         /* superblock is rarely larger than 1K, but it can be larger,
1481          * and it is safe to read 4k, so we do that
1482          */
1483         ret = read_disk_sb(rdev, 4096);
1484         if (ret) return ret;
1485
1486         sb = page_address(rdev->sb_page);
1487
1488         if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1489             sb->major_version != cpu_to_le32(1) ||
1490             le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1491             le64_to_cpu(sb->super_offset) != rdev->sb_start ||
1492             (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
1493                 return -EINVAL;
1494
1495         if (calc_sb_1_csum(sb) != sb->sb_csum) {
1496                 pr_warn("md: invalid superblock checksum on %s\n",
1497                         bdevname(rdev->bdev,b));
1498                 return -EINVAL;
1499         }
1500         if (le64_to_cpu(sb->data_size) < 10) {
1501                 pr_warn("md: data_size too small on %s\n",
1502                         bdevname(rdev->bdev,b));
1503                 return -EINVAL;
1504         }
1505         if (sb->pad0 ||
1506             sb->pad3[0] ||
1507             memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1])))
1508                 /* Some padding is non-zero, might be a new feature */
1509                 return -EINVAL;
1510
1511         rdev->preferred_minor = 0xffff;
1512         rdev->data_offset = le64_to_cpu(sb->data_offset);
1513         rdev->new_data_offset = rdev->data_offset;
1514         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) &&
1515             (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
1516                 rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset);
1517         atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
1518
1519         rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
1520         bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1521         if (rdev->sb_size & bmask)
1522                 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1523
1524         if (minor_version
1525             && rdev->data_offset < sb_start + (rdev->sb_size/512))
1526                 return -EINVAL;
1527         if (minor_version
1528             && rdev->new_data_offset < sb_start + (rdev->sb_size/512))
1529                 return -EINVAL;
1530
1531         if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1532                 rdev->desc_nr = -1;
1533         else
1534                 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1535
1536         if (!rdev->bb_page) {
1537                 rdev->bb_page = alloc_page(GFP_KERNEL);
1538                 if (!rdev->bb_page)
1539                         return -ENOMEM;
1540         }
1541         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1542             rdev->badblocks.count == 0) {
1543                 /* need to load the bad block list.
1544                  * Currently we limit it to one page.
1545                  */
1546                 s32 offset;
1547                 sector_t bb_sector;
1548                 u64 *bbp;
1549                 int i;
1550                 int sectors = le16_to_cpu(sb->bblog_size);
1551                 if (sectors > (PAGE_SIZE / 512))
1552                         return -EINVAL;
1553                 offset = le32_to_cpu(sb->bblog_offset);
1554                 if (offset == 0)
1555                         return -EINVAL;
1556                 bb_sector = (long long)offset;
1557                 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1558                                   rdev->bb_page, REQ_OP_READ, 0, true))
1559                         return -EIO;
1560                 bbp = (u64 *)page_address(rdev->bb_page);
1561                 rdev->badblocks.shift = sb->bblog_shift;
1562                 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1563                         u64 bb = le64_to_cpu(*bbp);
1564                         int count = bb & (0x3ff);
1565                         u64 sector = bb >> 10;
1566                         sector <<= sb->bblog_shift;
1567                         count <<= sb->bblog_shift;
1568                         if (bb + 1 == 0)
1569                                 break;
1570                         if (badblocks_set(&rdev->badblocks, sector, count, 1))
1571                                 return -EINVAL;
1572                 }
1573         } else if (sb->bblog_offset != 0)
1574                 rdev->badblocks.shift = 0;
1575
1576         if ((le32_to_cpu(sb->feature_map) &
1577             (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) {
1578                 rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset);
1579                 rdev->ppl.size = le16_to_cpu(sb->ppl.size);
1580                 rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset;
1581         }
1582
1583         if (!refdev) {
1584                 ret = 1;
1585         } else {
1586                 __u64 ev1, ev2;
1587                 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
1588
1589                 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1590                     sb->level != refsb->level ||
1591                     sb->layout != refsb->layout ||
1592                     sb->chunksize != refsb->chunksize) {
1593                         pr_warn("md: %s has strangely different superblock to %s\n",
1594                                 bdevname(rdev->bdev,b),
1595                                 bdevname(refdev->bdev,b2));
1596                         return -EINVAL;
1597                 }
1598                 ev1 = le64_to_cpu(sb->events);
1599                 ev2 = le64_to_cpu(refsb->events);
1600
1601                 if (ev1 > ev2)
1602                         ret = 1;
1603                 else
1604                         ret = 0;
1605         }
1606         if (minor_version) {
1607                 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9);
1608                 sectors -= rdev->data_offset;
1609         } else
1610                 sectors = rdev->sb_start;
1611         if (sectors < le64_to_cpu(sb->data_size))
1612                 return -EINVAL;
1613         rdev->sectors = le64_to_cpu(sb->data_size);
1614         return ret;
1615 }
1616
1617 static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
1618 {
1619         struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
1620         __u64 ev1 = le64_to_cpu(sb->events);
1621
1622         rdev->raid_disk = -1;
1623         clear_bit(Faulty, &rdev->flags);
1624         clear_bit(In_sync, &rdev->flags);
1625         clear_bit(Bitmap_sync, &rdev->flags);
1626         clear_bit(WriteMostly, &rdev->flags);
1627
1628         if (mddev->raid_disks == 0) {
1629                 mddev->major_version = 1;
1630                 mddev->patch_version = 0;
1631                 mddev->external = 0;
1632                 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
1633                 mddev->ctime = le64_to_cpu(sb->ctime);
1634                 mddev->utime = le64_to_cpu(sb->utime);
1635                 mddev->level = le32_to_cpu(sb->level);
1636                 mddev->clevel[0] = 0;
1637                 mddev->layout = le32_to_cpu(sb->layout);
1638                 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1639                 mddev->dev_sectors = le64_to_cpu(sb->size);
1640                 mddev->events = ev1;
1641                 mddev->bitmap_info.offset = 0;
1642                 mddev->bitmap_info.space = 0;
1643                 /* Default location for bitmap is 1K after superblock
1644                  * using 3K - total of 4K
1645                  */
1646                 mddev->bitmap_info.default_offset = 1024 >> 9;
1647                 mddev->bitmap_info.default_space = (4096-1024) >> 9;
1648                 mddev->reshape_backwards = 0;
1649
1650                 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1651                 memcpy(mddev->uuid, sb->set_uuid, 16);
1652
1653                 mddev->max_disks =  (4096-256)/2;
1654
1655                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
1656                     mddev->bitmap_info.file == NULL) {
1657                         mddev->bitmap_info.offset =
1658                                 (__s32)le32_to_cpu(sb->bitmap_offset);
1659                         /* Metadata doesn't record how much space is available.
1660                          * For 1.0, we assume we can use up to the superblock
1661                          * if before, else to 4K beyond superblock.
1662                          * For others, assume no change is possible.
1663                          */
1664                         if (mddev->minor_version > 0)
1665                                 mddev->bitmap_info.space = 0;
1666                         else if (mddev->bitmap_info.offset > 0)
1667                                 mddev->bitmap_info.space =
1668                                         8 - mddev->bitmap_info.offset;
1669                         else
1670                                 mddev->bitmap_info.space =
1671                                         -mddev->bitmap_info.offset;
1672                 }
1673
1674                 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1675                         mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1676                         mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1677                         mddev->new_level = le32_to_cpu(sb->new_level);
1678                         mddev->new_layout = le32_to_cpu(sb->new_layout);
1679                         mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
1680                         if (mddev->delta_disks < 0 ||
1681                             (mddev->delta_disks == 0 &&
1682                              (le32_to_cpu(sb->feature_map)
1683                               & MD_FEATURE_RESHAPE_BACKWARDS)))
1684                                 mddev->reshape_backwards = 1;
1685                 } else {
1686                         mddev->reshape_position = MaxSector;
1687                         mddev->delta_disks = 0;
1688                         mddev->new_level = mddev->level;
1689                         mddev->new_layout = mddev->layout;
1690                         mddev->new_chunk_sectors = mddev->chunk_sectors;
1691                 }
1692
1693                 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)
1694                         set_bit(MD_HAS_JOURNAL, &mddev->flags);
1695
1696                 if (le32_to_cpu(sb->feature_map) &
1697                     (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) {
1698                         if (le32_to_cpu(sb->feature_map) &
1699                             (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL))
1700                                 return -EINVAL;
1701                         if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) &&
1702                             (le32_to_cpu(sb->feature_map) &
1703                                             MD_FEATURE_MULTIPLE_PPLS))
1704                                 return -EINVAL;
1705                         set_bit(MD_HAS_PPL, &mddev->flags);
1706                 }
1707         } else if (mddev->pers == NULL) {
1708                 /* Insist of good event counter while assembling, except for
1709                  * spares (which don't need an event count) */
1710                 ++ev1;
1711                 if (rdev->desc_nr >= 0 &&
1712                     rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1713                     (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX ||
1714                      le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))
1715                         if (ev1 < mddev->events)
1716                                 return -EINVAL;
1717         } else if (mddev->bitmap) {
1718                 /* If adding to array with a bitmap, then we can accept an
1719                  * older device, but not too old.
1720                  */
1721                 if (ev1 < mddev->bitmap->events_cleared)
1722                         return 0;
1723                 if (ev1 < mddev->events)
1724                         set_bit(Bitmap_sync, &rdev->flags);
1725         } else {
1726                 if (ev1 < mddev->events)
1727                         /* just a hot-add of a new device, leave raid_disk at -1 */
1728                         return 0;
1729         }
1730         if (mddev->level != LEVEL_MULTIPATH) {
1731                 int role;
1732                 if (rdev->desc_nr < 0 ||
1733                     rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1734                         role = MD_DISK_ROLE_SPARE;
1735                         rdev->desc_nr = -1;
1736                 } else
1737                         role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1738                 switch(role) {
1739                 case MD_DISK_ROLE_SPARE: /* spare */
1740                         break;
1741                 case MD_DISK_ROLE_FAULTY: /* faulty */
1742                         set_bit(Faulty, &rdev->flags);
1743                         break;
1744                 case MD_DISK_ROLE_JOURNAL: /* journal device */
1745                         if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) {
1746                                 /* journal device without journal feature */
1747                                 pr_warn("md: journal device provided without journal feature, ignoring the device\n");
1748                                 return -EINVAL;
1749                         }
1750                         set_bit(Journal, &rdev->flags);
1751                         rdev->journal_tail = le64_to_cpu(sb->journal_tail);
1752                         rdev->raid_disk = 0;
1753                         break;
1754                 default:
1755                         rdev->saved_raid_disk = role;
1756                         if ((le32_to_cpu(sb->feature_map) &
1757                              MD_FEATURE_RECOVERY_OFFSET)) {
1758                                 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1759                                 if (!(le32_to_cpu(sb->feature_map) &
1760                                       MD_FEATURE_RECOVERY_BITMAP))
1761                                         rdev->saved_raid_disk = -1;
1762                         } else
1763                                 set_bit(In_sync, &rdev->flags);
1764                         rdev->raid_disk = role;
1765                         break;
1766                 }
1767                 if (sb->devflags & WriteMostly1)
1768                         set_bit(WriteMostly, &rdev->flags);
1769                 if (sb->devflags & FailFast1)
1770                         set_bit(FailFast, &rdev->flags);
1771                 if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT)
1772                         set_bit(Replacement, &rdev->flags);
1773         } else /* MULTIPATH are always insync */
1774                 set_bit(In_sync, &rdev->flags);
1775
1776         return 0;
1777 }
1778
1779 static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
1780 {
1781         struct mdp_superblock_1 *sb;
1782         struct md_rdev *rdev2;
1783         int max_dev, i;
1784         /* make rdev->sb match mddev and rdev data. */
1785
1786         sb = page_address(rdev->sb_page);
1787
1788         sb->feature_map = 0;
1789         sb->pad0 = 0;
1790         sb->recovery_offset = cpu_to_le64(0);
1791         memset(sb->pad3, 0, sizeof(sb->pad3));
1792
1793         sb->utime = cpu_to_le64((__u64)mddev->utime);
1794         sb->events = cpu_to_le64(mddev->events);
1795         if (mddev->in_sync)
1796                 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1797         else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags))
1798                 sb->resync_offset = cpu_to_le64(MaxSector);
1799         else
1800                 sb->resync_offset = cpu_to_le64(0);
1801
1802         sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
1803
1804         sb->raid_disks = cpu_to_le32(mddev->raid_disks);
1805         sb->size = cpu_to_le64(mddev->dev_sectors);
1806         sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
1807         sb->level = cpu_to_le32(mddev->level);
1808         sb->layout = cpu_to_le32(mddev->layout);
1809         if (test_bit(FailFast, &rdev->flags))
1810                 sb->devflags |= FailFast1;
1811         else
1812                 sb->devflags &= ~FailFast1;
1813
1814         if (test_bit(WriteMostly, &rdev->flags))
1815                 sb->devflags |= WriteMostly1;
1816         else
1817                 sb->devflags &= ~WriteMostly1;
1818         sb->data_offset = cpu_to_le64(rdev->data_offset);
1819         sb->data_size = cpu_to_le64(rdev->sectors);
1820
1821         if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1822                 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
1823                 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1824         }
1825
1826         if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) &&
1827             !test_bit(In_sync, &rdev->flags)) {
1828                 sb->feature_map |=
1829                         cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1830                 sb->recovery_offset =
1831                         cpu_to_le64(rdev->recovery_offset);
1832                 if (rdev->saved_raid_disk >= 0 && mddev->bitmap)
1833                         sb->feature_map |=
1834                                 cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP);
1835         }
1836         /* Note: recovery_offset and journal_tail share space  */
1837         if (test_bit(Journal, &rdev->flags))
1838                 sb->journal_tail = cpu_to_le64(rdev->journal_tail);
1839         if (test_bit(Replacement, &rdev->flags))
1840                 sb->feature_map |=
1841                         cpu_to_le32(MD_FEATURE_REPLACEMENT);
1842
1843         if (mddev->reshape_position != MaxSector) {
1844                 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1845                 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1846                 sb->new_layout = cpu_to_le32(mddev->new_layout);
1847                 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1848                 sb->new_level = cpu_to_le32(mddev->new_level);
1849                 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
1850                 if (mddev->delta_disks == 0 &&
1851                     mddev->reshape_backwards)
1852                         sb->feature_map
1853                                 |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1854                 if (rdev->new_data_offset != rdev->data_offset) {
1855                         sb->feature_map
1856                                 |= cpu_to_le32(MD_FEATURE_NEW_OFFSET);
1857                         sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset
1858                                                              - rdev->data_offset));
1859                 }
1860         }
1861
1862         if (mddev_is_clustered(mddev))
1863                 sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED);
1864
1865         if (rdev->badblocks.count == 0)
1866                 /* Nothing to do for bad blocks*/ ;
1867         else if (sb->bblog_offset == 0)
1868                 /* Cannot record bad blocks on this device */
1869                 md_error(mddev, rdev);
1870         else {
1871                 struct badblocks *bb = &rdev->badblocks;
1872                 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1873                 u64 *p = bb->page;
1874                 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1875                 if (bb->changed) {
1876                         unsigned seq;
1877
1878 retry:
1879                         seq = read_seqbegin(&bb->lock);
1880
1881                         memset(bbp, 0xff, PAGE_SIZE);
1882
1883                         for (i = 0 ; i < bb->count ; i++) {
1884                                 u64 internal_bb = p[i];
1885                                 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1886                                                 | BB_LEN(internal_bb));
1887                                 bbp[i] = cpu_to_le64(store_bb);
1888                         }
1889                         bb->changed = 0;
1890                         if (read_seqretry(&bb->lock, seq))
1891                                 goto retry;
1892
1893                         bb->sector = (rdev->sb_start +
1894                                       (int)le32_to_cpu(sb->bblog_offset));
1895                         bb->size = le16_to_cpu(sb->bblog_size);
1896                 }
1897         }
1898
1899         max_dev = 0;
1900         rdev_for_each(rdev2, mddev)
1901                 if (rdev2->desc_nr+1 > max_dev)
1902                         max_dev = rdev2->desc_nr+1;
1903
1904         if (max_dev > le32_to_cpu(sb->max_dev)) {
1905                 int bmask;
1906                 sb->max_dev = cpu_to_le32(max_dev);
1907                 rdev->sb_size = max_dev * 2 + 256;
1908                 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1909                 if (rdev->sb_size & bmask)
1910                         rdev->sb_size = (rdev->sb_size | bmask) + 1;
1911         } else
1912                 max_dev = le32_to_cpu(sb->max_dev);
1913
1914         for (i=0; i<max_dev;i++)
1915                 sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
1916
1917         if (test_bit(MD_HAS_JOURNAL, &mddev->flags))
1918                 sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL);
1919
1920         if (test_bit(MD_HAS_PPL, &mddev->flags)) {
1921                 if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags))
1922                         sb->feature_map |=
1923                             cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS);
1924                 else
1925                         sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL);
1926                 sb->ppl.offset = cpu_to_le16(rdev->ppl.offset);
1927                 sb->ppl.size = cpu_to_le16(rdev->ppl.size);
1928         }
1929
1930         rdev_for_each(rdev2, mddev) {
1931                 i = rdev2->desc_nr;
1932                 if (test_bit(Faulty, &rdev2->flags))
1933                         sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY);
1934                 else if (test_bit(In_sync, &rdev2->flags))
1935                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1936                 else if (test_bit(Journal, &rdev2->flags))
1937                         sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL);
1938                 else if (rdev2->raid_disk >= 0)
1939                         sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1940                 else
1941                         sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE);
1942         }
1943
1944         sb->sb_csum = calc_sb_1_csum(sb);
1945 }
1946
1947 static unsigned long long
1948 super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
1949 {
1950         struct mdp_superblock_1 *sb;
1951         sector_t max_sectors;
1952         if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
1953                 return 0; /* component must fit device */
1954         if (rdev->data_offset != rdev->new_data_offset)
1955                 return 0; /* too confusing */
1956         if (rdev->sb_start < rdev->data_offset) {
1957                 /* minor versions 1 and 2; superblock before data */
1958                 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
1959                 max_sectors -= rdev->data_offset;
1960                 if (!num_sectors || num_sectors > max_sectors)
1961                         num_sectors = max_sectors;
1962         } else if (rdev->mddev->bitmap_info.offset) {
1963                 /* minor version 0 with bitmap we can't move */
1964                 return 0;
1965         } else {
1966                 /* minor version 0; superblock after data */
1967                 sector_t sb_start;
1968                 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
1969                 sb_start &= ~(sector_t)(4*2 - 1);
1970                 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
1971                 if (!num_sectors || num_sectors > max_sectors)
1972                         num_sectors = max_sectors;
1973                 rdev->sb_start = sb_start;
1974         }
1975         sb = page_address(rdev->sb_page);
1976         sb->data_size = cpu_to_le64(num_sectors);
1977         sb->super_offset = cpu_to_le64(rdev->sb_start);
1978         sb->sb_csum = calc_sb_1_csum(sb);
1979         do {
1980                 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
1981                                rdev->sb_page);
1982         } while (md_super_wait(rdev->mddev) < 0);
1983         return num_sectors;
1984
1985 }
1986
1987 static int
1988 super_1_allow_new_offset(struct md_rdev *rdev,
1989                          unsigned long long new_offset)
1990 {
1991         /* All necessary checks on new >= old have been done */
1992         struct bitmap *bitmap;
1993         if (new_offset >= rdev->data_offset)
1994                 return 1;
1995
1996         /* with 1.0 metadata, there is no metadata to tread on
1997          * so we can always move back */
1998         if (rdev->mddev->minor_version == 0)
1999                 return 1;
2000
2001         /* otherwise we must be sure not to step on
2002          * any metadata, so stay:
2003          * 36K beyond start of superblock
2004          * beyond end of badblocks
2005          * beyond write-intent bitmap
2006          */
2007         if (rdev->sb_start + (32+4)*2 > new_offset)
2008                 return 0;
2009         bitmap = rdev->mddev->bitmap;
2010         if (bitmap && !rdev->mddev->bitmap_info.file &&
2011             rdev->sb_start + rdev->mddev->bitmap_info.offset +
2012             bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset)
2013                 return 0;
2014         if (rdev->badblocks.sector + rdev->badblocks.size > new_offset)
2015                 return 0;
2016
2017         return 1;
2018 }
2019
2020 static struct super_type super_types[] = {
2021         [0] = {
2022                 .name   = "0.90.0",
2023                 .owner  = THIS_MODULE,
2024                 .load_super         = super_90_load,
2025                 .validate_super     = super_90_validate,
2026                 .sync_super         = super_90_sync,
2027                 .rdev_size_change   = super_90_rdev_size_change,
2028                 .allow_new_offset   = super_90_allow_new_offset,
2029         },
2030         [1] = {
2031                 .name   = "md-1",
2032                 .owner  = THIS_MODULE,
2033                 .load_super         = super_1_load,
2034                 .validate_super     = super_1_validate,
2035                 .sync_super         = super_1_sync,
2036                 .rdev_size_change   = super_1_rdev_size_change,
2037                 .allow_new_offset   = super_1_allow_new_offset,
2038         },
2039 };
2040
2041 static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
2042 {
2043         if (mddev->sync_super) {
2044                 mddev->sync_super(mddev, rdev);
2045                 return;
2046         }
2047
2048         BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
2049
2050         super_types[mddev->major_version].sync_super(mddev, rdev);
2051 }
2052
2053 static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
2054 {
2055         struct md_rdev *rdev, *rdev2;
2056
2057         rcu_read_lock();
2058         rdev_for_each_rcu(rdev, mddev1) {
2059                 if (test_bit(Faulty, &rdev->flags) ||
2060                     test_bit(Journal, &rdev->flags) ||
2061                     rdev->raid_disk == -1)
2062                         continue;
2063                 rdev_for_each_rcu(rdev2, mddev2) {
2064                         if (test_bit(Faulty, &rdev2->flags) ||
2065                             test_bit(Journal, &rdev2->flags) ||
2066                             rdev2->raid_disk == -1)
2067                                 continue;
2068                         if (rdev->bdev->bd_contains ==
2069                             rdev2->bdev->bd_contains) {
2070                                 rcu_read_unlock();
2071                                 return 1;
2072                         }
2073                 }
2074         }
2075         rcu_read_unlock();
2076         return 0;
2077 }
2078
2079 static LIST_HEAD(pending_raid_disks);
2080
2081 /*
2082  * Try to register data integrity profile for an mddev
2083  *
2084  * This is called when an array is started and after a disk has been kicked
2085  * from the array. It only succeeds if all working and active component devices
2086  * are integrity capable with matching profiles.
2087  */
2088 int md_integrity_register(struct mddev *mddev)
2089 {
2090         struct md_rdev *rdev, *reference = NULL;
2091
2092         if (list_empty(&mddev->disks))
2093                 return 0; /* nothing to do */
2094         if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
2095                 return 0; /* shouldn't register, or already is */
2096         rdev_for_each(rdev, mddev) {
2097                 /* skip spares and non-functional disks */
2098                 if (test_bit(Faulty, &rdev->flags))
2099                         continue;
2100                 if (rdev->raid_disk < 0)
2101                         continue;
2102                 if (!reference) {
2103                         /* Use the first rdev as the reference */
2104                         reference = rdev;
2105                         continue;
2106                 }
2107                 /* does this rdev's profile match the reference profile? */
2108                 if (blk_integrity_compare(reference->bdev->bd_disk,
2109                                 rdev->bdev->bd_disk) < 0)
2110                         return -EINVAL;
2111         }
2112         if (!reference || !bdev_get_integrity(reference->bdev))
2113                 return 0;
2114         /*
2115          * All component devices are integrity capable and have matching
2116          * profiles, register the common profile for the md device.
2117          */
2118         blk_integrity_register(mddev->gendisk,
2119                                bdev_get_integrity(reference->bdev));
2120
2121         pr_debug("md: data integrity enabled on %s\n", mdname(mddev));
2122         if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
2123                 pr_err("md: failed to create integrity pool for %s\n",
2124                        mdname(mddev));
2125                 return -EINVAL;
2126         }
2127         return 0;
2128 }
2129 EXPORT_SYMBOL(md_integrity_register);
2130
2131 /*
2132  * Attempt to add an rdev, but only if it is consistent with the current
2133  * integrity profile
2134  */
2135 int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
2136 {
2137         struct blk_integrity *bi_rdev;
2138         struct blk_integrity *bi_mddev;
2139         char name[BDEVNAME_SIZE];
2140
2141         if (!mddev->gendisk)
2142                 return 0;
2143
2144         bi_rdev = bdev_get_integrity(rdev->bdev);
2145         bi_mddev = blk_get_integrity(mddev->gendisk);
2146
2147         if (!bi_mddev) /* nothing to do */
2148                 return 0;
2149
2150         if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) {
2151                 pr_err("%s: incompatible integrity profile for %s\n",
2152                        mdname(mddev), bdevname(rdev->bdev, name));
2153                 return -ENXIO;
2154         }
2155
2156         return 0;
2157 }
2158 EXPORT_SYMBOL(md_integrity_add_rdev);
2159
2160 static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
2161 {
2162         char b[BDEVNAME_SIZE];
2163         struct kobject *ko;
2164         int err;
2165
2166         /* prevent duplicates */
2167         if (find_rdev(mddev, rdev->bdev->bd_dev))
2168                 return -EEXIST;
2169
2170         if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) &&
2171             mddev->pers)
2172                 return -EROFS;
2173
2174         /* make sure rdev->sectors exceeds mddev->dev_sectors */
2175         if (!test_bit(Journal, &rdev->flags) &&
2176             rdev->sectors &&
2177             (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) {
2178                 if (mddev->pers) {
2179                         /* Cannot change size, so fail
2180                          * If mddev->level <= 0, then we don't care
2181                          * about aligning sizes (e.g. linear)
2182                          */
2183                         if (mddev->level > 0)
2184                                 return -ENOSPC;
2185                 } else
2186                         mddev->dev_sectors = rdev->sectors;
2187         }
2188
2189         /* Verify rdev->desc_nr is unique.
2190          * If it is -1, assign a free number, else
2191          * check number is not in use
2192          */
2193         rcu_read_lock();
2194         if (rdev->desc_nr < 0) {
2195                 int choice = 0;
2196                 if (mddev->pers)
2197                         choice = mddev->raid_disks;
2198                 while (md_find_rdev_nr_rcu(mddev, choice))
2199                         choice++;
2200                 rdev->desc_nr = choice;
2201         } else {
2202                 if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) {
2203                         rcu_read_unlock();
2204                         return -EBUSY;
2205                 }
2206         }
2207         rcu_read_unlock();
2208         if (!test_bit(Journal, &rdev->flags) &&
2209             mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2210                 pr_warn("md: %s: array is limited to %d devices\n",
2211                         mdname(mddev), mddev->max_disks);
2212                 return -EBUSY;
2213         }
2214         bdevname(rdev->bdev,b);
2215         strreplace(b, '/', '!');
2216
2217         rdev->mddev = mddev;
2218         pr_debug("md: bind<%s>\n", b);
2219
2220         if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
2221                 goto fail;
2222
2223         ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
2224         if (sysfs_create_link(&rdev->kobj, ko, "block"))
2225                 /* failure here is OK */;
2226         rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
2227
2228         list_add_rcu(&rdev->same_set, &mddev->disks);
2229         bd_link_disk_holder(rdev->bdev, mddev->gendisk);
2230
2231         /* May as well allow recovery to be retried once */
2232         mddev->recovery_disabled++;
2233
2234         return 0;
2235
2236  fail:
2237         pr_warn("md: failed to register dev-%s for %s\n",
2238                 b, mdname(mddev));
2239         return err;
2240 }
2241
2242 static void md_delayed_delete(struct work_struct *ws)
2243 {
2244         struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
2245         kobject_del(&rdev->kobj);
2246         kobject_put(&rdev->kobj);
2247 }
2248
2249 static void unbind_rdev_from_array(struct md_rdev *rdev)
2250 {
2251         char b[BDEVNAME_SIZE];
2252
2253         bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
2254         list_del_rcu(&rdev->same_set);
2255         pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
2256         rdev->mddev = NULL;
2257         sysfs_remove_link(&rdev->kobj, "block");
2258         sysfs_put(rdev->sysfs_state);
2259         rdev->sysfs_state = NULL;
2260         rdev->badblocks.count = 0;
2261         /* We need to delay this, otherwise we can deadlock when
2262          * writing to 'remove' to "dev/state".  We also need
2263          * to delay it due to rcu usage.
2264          */
2265         synchronize_rcu();
2266         INIT_WORK(&rdev->del_work, md_delayed_delete);
2267         kobject_get(&rdev->kobj);
2268         queue_work(md_misc_wq, &rdev->del_work);
2269 }
2270
2271 /*
2272  * prevent the device from being mounted, repartitioned or
2273  * otherwise reused by a RAID array (or any other kernel
2274  * subsystem), by bd_claiming the device.
2275  */
2276 static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
2277 {
2278         int err = 0;
2279         struct block_device *bdev;
2280         char b[BDEVNAME_SIZE];
2281
2282         bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2283                                  shared ? (struct md_rdev *)lock_rdev : rdev);
2284         if (IS_ERR(bdev)) {
2285                 pr_warn("md: could not open %s.\n", __bdevname(dev, b));
2286                 return PTR_ERR(bdev);
2287         }
2288         rdev->bdev = bdev;
2289         return err;
2290 }
2291
2292 static void unlock_rdev(struct md_rdev *rdev)
2293 {
2294         struct block_device *bdev = rdev->bdev;
2295         rdev->bdev = NULL;
2296         blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
2297 }
2298
2299 void md_autodetect_dev(dev_t dev);
2300
2301 static void export_rdev(struct md_rdev *rdev)
2302 {
2303         char b[BDEVNAME_SIZE];
2304
2305         pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b));
2306         md_rdev_clear(rdev);
2307 #ifndef MODULE
2308         if (test_bit(AutoDetected, &rdev->flags))
2309                 md_autodetect_dev(rdev->bdev->bd_dev);
2310 #endif
2311         unlock_rdev(rdev);
2312         kobject_put(&rdev->kobj);
2313 }
2314
2315 void md_kick_rdev_from_array(struct md_rdev *rdev)
2316 {
2317         unbind_rdev_from_array(rdev);
2318         export_rdev(rdev);
2319 }
2320 EXPORT_SYMBOL_GPL(md_kick_rdev_from_array);
2321
2322 static void export_array(struct mddev *mddev)
2323 {
2324         struct md_rdev *rdev;
2325
2326         while (!list_empty(&mddev->disks)) {
2327                 rdev = list_first_entry(&mddev->disks, struct md_rdev,
2328                                         same_set);
2329                 md_kick_rdev_from_array(rdev);
2330         }
2331         mddev->raid_disks = 0;
2332         mddev->major_version = 0;
2333 }
2334
2335 static bool set_in_sync(struct mddev *mddev)
2336 {
2337         lockdep_assert_held(&mddev->lock);
2338         if (!mddev->in_sync) {
2339                 mddev->sync_checkers++;
2340                 spin_unlock(&mddev->lock);
2341                 percpu_ref_switch_to_atomic_sync(&mddev->writes_pending);
2342                 spin_lock(&mddev->lock);
2343                 if (!mddev->in_sync &&
2344                     percpu_ref_is_zero(&mddev->writes_pending)) {
2345                         mddev->in_sync = 1;
2346                         /*
2347                          * Ensure ->in_sync is visible before we clear
2348                          * ->sync_checkers.
2349                          */
2350                         smp_mb();
2351                         set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2352                         sysfs_notify_dirent_safe(mddev->sysfs_state);
2353                 }
2354                 if (--mddev->sync_checkers == 0)
2355                         percpu_ref_switch_to_percpu(&mddev->writes_pending);
2356         }
2357         if (mddev->safemode == 1)
2358                 mddev->safemode = 0;
2359         return mddev->in_sync;
2360 }
2361
2362 static void sync_sbs(struct mddev *mddev, int nospares)
2363 {
2364         /* Update each superblock (in-memory image), but
2365          * if we are allowed to, skip spares which already
2366          * have the right event counter, or have one earlier
2367          * (which would mean they aren't being marked as dirty
2368          * with the rest of the array)
2369          */
2370         struct md_rdev *rdev;
2371         rdev_for_each(rdev, mddev) {
2372                 if (rdev->sb_events == mddev->events ||
2373                     (nospares &&
2374                      rdev->raid_disk < 0 &&
2375                      rdev->sb_events+1 == mddev->events)) {
2376                         /* Don't update this superblock */
2377                         rdev->sb_loaded = 2;
2378                 } else {
2379                         sync_super(mddev, rdev);
2380                         rdev->sb_loaded = 1;
2381                 }
2382         }
2383 }
2384
2385 static bool does_sb_need_changing(struct mddev *mddev)
2386 {
2387         struct md_rdev *rdev;
2388         struct mdp_superblock_1 *sb;
2389         int role;
2390
2391         /* Find a good rdev */
2392         rdev_for_each(rdev, mddev)
2393                 if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags))
2394                         break;
2395
2396         /* No good device found. */
2397         if (!rdev)
2398                 return false;
2399
2400         sb = page_address(rdev->sb_page);
2401         /* Check if a device has become faulty or a spare become active */
2402         rdev_for_each(rdev, mddev) {
2403                 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
2404                 /* Device activated? */
2405                 if (role == 0xffff && rdev->raid_disk >=0 &&
2406                     !test_bit(Faulty, &rdev->flags))
2407                         return true;
2408                 /* Device turned faulty? */
2409                 if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd))
2410                         return true;
2411         }
2412
2413         /* Check if any mddev parameters have changed */
2414         if ((mddev->dev_sectors != le64_to_cpu(sb->size)) ||
2415             (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) ||
2416             (mddev->layout != le32_to_cpu(sb->layout)) ||
2417             (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) ||
2418             (mddev->chunk_sectors != le32_to_cpu(sb->chunksize)))
2419                 return true;
2420
2421         return false;
2422 }
2423
2424 void md_update_sb(struct mddev *mddev, int force_change)
2425 {
2426         struct md_rdev *rdev;
2427         int sync_req;
2428         int nospares = 0;
2429         int any_badblocks_changed = 0;
2430         int ret = -1;
2431
2432         if (mddev->ro) {
2433                 if (force_change)
2434                         set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2435                 return;
2436         }
2437
2438 repeat:
2439         if (mddev_is_clustered(mddev)) {
2440                 if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2441                         force_change = 1;
2442                 if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2443                         nospares = 1;
2444                 ret = md_cluster_ops->metadata_update_start(mddev);
2445                 /* Has someone else has updated the sb */
2446                 if (!does_sb_need_changing(mddev)) {
2447                         if (ret == 0)
2448                                 md_cluster_ops->metadata_update_cancel(mddev);
2449                         bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2450                                                          BIT(MD_SB_CHANGE_DEVS) |
2451                                                          BIT(MD_SB_CHANGE_CLEAN));
2452                         return;
2453                 }
2454         }
2455
2456         /*
2457          * First make sure individual recovery_offsets are correct
2458          * curr_resync_completed can only be used during recovery.
2459          * During reshape/resync it might use array-addresses rather
2460          * that device addresses.
2461          */
2462         rdev_for_each(rdev, mddev) {
2463                 if (rdev->raid_disk >= 0 &&
2464                     mddev->delta_disks >= 0 &&
2465                     test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
2466                     test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) &&
2467                     !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2468                     !test_bit(Journal, &rdev->flags) &&
2469                     !test_bit(In_sync, &rdev->flags) &&
2470                     mddev->curr_resync_completed > rdev->recovery_offset)
2471                                 rdev->recovery_offset = mddev->curr_resync_completed;
2472
2473         }
2474         if (!mddev->persistent) {
2475                 clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags);
2476                 clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2477                 if (!mddev->external) {
2478                         clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags);
2479                         rdev_for_each(rdev, mddev) {
2480                                 if (rdev->badblocks.changed) {
2481                                         rdev->badblocks.changed = 0;
2482                                         ack_all_badblocks(&rdev->badblocks);
2483                                         md_error(mddev, rdev);
2484                                 }
2485                                 clear_bit(Blocked, &rdev->flags);
2486                                 clear_bit(BlockedBadBlocks, &rdev->flags);
2487                                 wake_up(&rdev->blocked_wait);
2488                         }
2489                 }
2490                 wake_up(&mddev->sb_wait);
2491                 return;
2492         }
2493
2494         spin_lock(&mddev->lock);
2495
2496         mddev->utime = ktime_get_real_seconds();
2497
2498         if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags))
2499                 force_change = 1;
2500         if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags))
2501                 /* just a clean<-> dirty transition, possibly leave spares alone,
2502                  * though if events isn't the right even/odd, we will have to do
2503                  * spares after all
2504                  */
2505                 nospares = 1;
2506         if (force_change)
2507                 nospares = 0;
2508         if (mddev->degraded)
2509                 /* If the array is degraded, then skipping spares is both
2510                  * dangerous and fairly pointless.
2511                  * Dangerous because a device that was removed from the array
2512                  * might have a event_count that still looks up-to-date,
2513                  * so it can be re-added without a resync.
2514                  * Pointless because if there are any spares to skip,
2515                  * then a recovery will happen and soon that array won't
2516                  * be degraded any more and the spare can go back to sleep then.
2517                  */
2518                 nospares = 0;
2519
2520         sync_req = mddev->in_sync;
2521
2522         /* If this is just a dirty<->clean transition, and the array is clean
2523          * and 'events' is odd, we can roll back to the previous clean state */
2524         if (nospares
2525             && (mddev->in_sync && mddev->recovery_cp == MaxSector)
2526             && mddev->can_decrease_events
2527             && mddev->events != 1) {
2528                 mddev->events--;
2529                 mddev->can_decrease_events = 0;
2530         } else {
2531                 /* otherwise we have to go forward and ... */
2532                 mddev->events ++;
2533                 mddev->can_decrease_events = nospares;
2534         }
2535
2536         /*
2537          * This 64-bit counter should never wrap.
2538          * Either we are in around ~1 trillion A.C., assuming
2539          * 1 reboot per second, or we have a bug...
2540          */
2541         WARN_ON(mddev->events == 0);
2542
2543         rdev_for_each(rdev, mddev) {
2544                 if (rdev->badblocks.changed)
2545                         any_badblocks_changed++;
2546                 if (test_bit(Faulty, &rdev->flags))
2547                         set_bit(FaultRecorded, &rdev->flags);
2548         }
2549
2550         sync_sbs(mddev, nospares);
2551         spin_unlock(&mddev->lock);
2552
2553         pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2554                  mdname(mddev), mddev->in_sync);
2555
2556         if (mddev->queue)
2557                 blk_add_trace_msg(mddev->queue, "md md_update_sb");
2558 rewrite:
2559         bitmap_update_sb(mddev->bitmap);
2560         rdev_for_each(rdev, mddev) {
2561                 char b[BDEVNAME_SIZE];
2562
2563                 if (rdev->sb_loaded != 1)
2564                         continue; /* no noise on spare devices */
2565
2566                 if (!test_bit(Faulty, &rdev->flags)) {
2567                         md_super_write(mddev,rdev,
2568                                        rdev->sb_start, rdev->sb_size,
2569                                        rdev->sb_page);
2570                         pr_debug("md: (write) %s's sb offset: %llu\n",
2571                                  bdevname(rdev->bdev, b),
2572                                  (unsigned long long)rdev->sb_start);
2573                         rdev->sb_events = mddev->events;
2574                         if (rdev->badblocks.size) {
2575                                 md_super_write(mddev, rdev,
2576                                                rdev->badblocks.sector,
2577                                                rdev->badblocks.size << 9,
2578                                                rdev->bb_page);
2579                                 rdev->badblocks.size = 0;
2580                         }
2581
2582                 } else
2583                         pr_debug("md: %s (skipping faulty)\n",
2584                                  bdevname(rdev->bdev, b));
2585
2586                 if (mddev->level == LEVEL_MULTIPATH)
2587                         /* only need to write one superblock... */
2588                         break;
2589         }
2590         if (md_super_wait(mddev) < 0)
2591                 goto rewrite;
2592         /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */
2593
2594         if (mddev_is_clustered(mddev) && ret == 0)
2595                 md_cluster_ops->metadata_update_finish(mddev);
2596
2597         if (mddev->in_sync != sync_req ||
2598             !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING),
2599                                BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN)))
2600                 /* have to write it out again */
2601                 goto repeat;
2602         wake_up(&mddev->sb_wait);
2603         if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2604                 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
2605
2606         rdev_for_each(rdev, mddev) {
2607                 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2608                         clear_bit(Blocked, &rdev->flags);
2609
2610                 if (any_badblocks_changed)
2611                         ack_all_badblocks(&rdev->badblocks);
2612                 clear_bit(BlockedBadBlocks, &rdev->flags);
2613                 wake_up(&rdev->blocked_wait);
2614         }
2615 }
2616 EXPORT_SYMBOL(md_update_sb);
2617
2618 static int add_bound_rdev(struct md_rdev *rdev)
2619 {
2620         struct mddev *mddev = rdev->mddev;
2621         int err = 0;
2622         bool add_journal = test_bit(Journal, &rdev->flags);
2623
2624         if (!mddev->pers->hot_remove_disk || add_journal) {
2625                 /* If there is hot_add_disk but no hot_remove_disk
2626                  * then added disks for geometry changes,
2627                  * and should be added immediately.
2628                  */
2629                 super_types[mddev->major_version].
2630                         validate_super(mddev, rdev);
2631                 if (add_journal)
2632                         mddev_suspend(mddev);
2633                 err = mddev->pers->hot_add_disk(mddev, rdev);
2634                 if (add_journal)
2635                         mddev_resume(mddev);
2636                 if (err) {
2637                         md_kick_rdev_from_array(rdev);
2638                         return err;
2639                 }
2640         }
2641         sysfs_notify_dirent_safe(rdev->sysfs_state);
2642
2643         set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2644         if (mddev->degraded)
2645                 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2646         set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2647         md_new_event(mddev);
2648         md_wakeup_thread(mddev->thread);
2649         return 0;
2650 }
2651
2652 /* words written to sysfs files may, or may not, be \n terminated.
2653  * We want to accept with case. For this we use cmd_match.
2654  */
2655 static int cmd_match(const char *cmd, const char *str)
2656 {
2657         /* See if cmd, written into a sysfs file, matches
2658          * str.  They must either be the same, or cmd can
2659          * have a trailing newline
2660          */
2661         while (*cmd && *str && *cmd == *str) {
2662                 cmd++;
2663                 str++;
2664         }
2665         if (*cmd == '\n')
2666                 cmd++;
2667         if (*str || *cmd)
2668                 return 0;
2669         return 1;
2670 }
2671
2672 struct rdev_sysfs_entry {
2673         struct attribute attr;
2674         ssize_t (*show)(struct md_rdev *, char *);
2675         ssize_t (*store)(struct md_rdev *, const char *, size_t);
2676 };
2677
2678 static ssize_t
2679 state_show(struct md_rdev *rdev, char *page)
2680 {
2681         char *sep = ",";
2682         size_t len = 0;
2683         unsigned long flags = READ_ONCE(rdev->flags);
2684
2685         if (test_bit(Faulty, &flags) ||
2686             (!test_bit(ExternalBbl, &flags) &&
2687             rdev->badblocks.unacked_exist))
2688                 len += sprintf(page+len, "faulty%s", sep);
2689         if (test_bit(In_sync, &flags))
2690                 len += sprintf(page+len, "in_sync%s", sep);
2691         if (test_bit(Journal, &flags))
2692                 len += sprintf(page+len, "journal%s", sep);
2693         if (test_bit(WriteMostly, &flags))
2694                 len += sprintf(page+len, "write_mostly%s", sep);
2695         if (test_bit(Blocked, &flags) ||
2696             (rdev->badblocks.unacked_exist
2697              && !test_bit(Faulty, &flags)))
2698                 len += sprintf(page+len, "blocked%s", sep);
2699         if (!test_bit(Faulty, &flags) &&
2700             !test_bit(Journal, &flags) &&
2701             !test_bit(In_sync, &flags))
2702                 len += sprintf(page+len, "spare%s", sep);
2703         if (test_bit(WriteErrorSeen, &flags))
2704                 len += sprintf(page+len, "write_error%s", sep);
2705         if (test_bit(WantReplacement, &flags))
2706                 len += sprintf(page+len, "want_replacement%s", sep);
2707         if (test_bit(Replacement, &flags))
2708                 len += sprintf(page+len, "replacement%s", sep);
2709         if (test_bit(ExternalBbl, &flags))
2710                 len += sprintf(page+len, "external_bbl%s", sep);
2711         if (test_bit(FailFast, &flags))
2712                 len += sprintf(page+len, "failfast%s", sep);
2713
2714         if (len)
2715                 len -= strlen(sep);
2716
2717         return len+sprintf(page+len, "\n");
2718 }
2719
2720 static ssize_t
2721 state_store(struct md_rdev *rdev, const char *buf, size_t len)
2722 {
2723         /* can write
2724          *  faulty  - simulates an error
2725          *  remove  - disconnects the device
2726          *  writemostly - sets write_mostly
2727          *  -writemostly - clears write_mostly
2728          *  blocked - sets the Blocked flags
2729          *  -blocked - clears the Blocked and possibly simulates an error
2730          *  insync - sets Insync providing device isn't active
2731          *  -insync - clear Insync for a device with a slot assigned,
2732          *            so that it gets rebuilt based on bitmap
2733          *  write_error - sets WriteErrorSeen
2734          *  -write_error - clears WriteErrorSeen
2735          *  {,-}failfast - set/clear FailFast
2736          */
2737         int err = -EINVAL;
2738         if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2739                 md_error(rdev->mddev, rdev);
2740                 if (test_bit(Faulty, &rdev->flags))
2741                         err = 0;
2742                 else
2743                         err = -EBUSY;
2744         } else if (cmd_match(buf, "remove")) {
2745                 if (rdev->mddev->pers) {
2746                         clear_bit(Blocked, &rdev->flags);
2747                         remove_and_add_spares(rdev->mddev, rdev);
2748                 }
2749                 if (rdev->raid_disk >= 0)
2750                         err = -EBUSY;
2751                 else {
2752                         struct mddev *mddev = rdev->mddev;
2753                         err = 0;
2754                         if (mddev_is_clustered(mddev))
2755                                 err = md_cluster_ops->remove_disk(mddev, rdev);
2756
2757                         if (err == 0) {
2758                                 md_kick_rdev_from_array(rdev);
2759                                 if (mddev->pers) {
2760                                         set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
2761                                         md_wakeup_thread(mddev->thread);
2762                                 }
2763                                 md_new_event(mddev);
2764                         }
2765                 }
2766         } else if (cmd_match(buf, "writemostly")) {
2767                 set_bit(WriteMostly, &rdev->flags);
2768                 err = 0;
2769         } else if (cmd_match(buf, "-writemostly")) {
2770                 clear_bit(WriteMostly, &rdev->flags);
2771                 err = 0;
2772         } else if (cmd_match(buf, "blocked")) {
2773                 set_bit(Blocked, &rdev->flags);
2774                 err = 0;
2775         } else if (cmd_match(buf, "-blocked")) {
2776                 if (!test_bit(Faulty, &rdev->flags) &&
2777                     !test_bit(ExternalBbl, &rdev->flags) &&
2778                     rdev->badblocks.unacked_exist) {
2779                         /* metadata handler doesn't understand badblocks,
2780                          * so we need to fail the device
2781                          */
2782                         md_error(rdev->mddev, rdev);
2783                 }
2784                 clear_bit(Blocked, &rdev->flags);
2785                 clear_bit(BlockedBadBlocks, &rdev->flags);
2786                 wake_up(&rdev->blocked_wait);
2787                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2788                 md_wakeup_thread(rdev->mddev->thread);
2789
2790                 err = 0;
2791         } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2792                 set_bit(In_sync, &rdev->flags);
2793                 err = 0;
2794         } else if (cmd_match(buf, "failfast")) {
2795                 set_bit(FailFast, &rdev->flags);
2796                 err = 0;
2797         } else if (cmd_match(buf, "-failfast")) {
2798                 clear_bit(FailFast, &rdev->flags);
2799                 err = 0;
2800         } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 &&
2801                    !test_bit(Journal, &rdev->flags)) {
2802                 if (rdev->mddev->pers == NULL) {
2803                         clear_bit(In_sync, &rdev->flags);
2804                         rdev->saved_raid_disk = rdev->raid_disk;
2805                         rdev->raid_disk = -1;
2806                         err = 0;
2807                 }
2808         } else if (cmd_match(buf, "write_error")) {
2809                 set_bit(WriteErrorSeen, &rdev->flags);
2810                 err = 0;
2811         } else if (cmd_match(buf, "-write_error")) {
2812                 clear_bit(WriteErrorSeen, &rdev->flags);
2813                 err = 0;
2814         } else if (cmd_match(buf, "want_replacement")) {
2815                 /* Any non-spare device that is not a replacement can
2816                  * become want_replacement at any time, but we then need to
2817                  * check if recovery is needed.
2818                  */
2819                 if (rdev->raid_disk >= 0 &&
2820                     !test_bit(Journal, &rdev->flags) &&
2821                     !test_bit(Replacement, &rdev->flags))
2822                         set_bit(WantReplacement, &rdev->flags);
2823                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2824                 md_wakeup_thread(rdev->mddev->thread);
2825                 err = 0;
2826         } else if (cmd_match(buf, "-want_replacement")) {
2827                 /* Clearing 'want_replacement' is always allowed.
2828                  * Once replacements starts it is too late though.
2829                  */
2830                 err = 0;
2831                 clear_bit(WantReplacement, &rdev->flags);
2832         } else if (cmd_match(buf, "replacement")) {
2833                 /* Can only set a device as a replacement when array has not
2834                  * yet been started.  Once running, replacement is automatic
2835                  * from spares, or by assigning 'slot'.
2836                  */
2837                 if (rdev->mddev->pers)
2838                         err = -EBUSY;
2839                 else {
2840                         set_bit(Replacement, &rdev->flags);
2841                         err = 0;
2842                 }
2843         } else if (cmd_match(buf, "-replacement")) {
2844                 /* Similarly, can only clear Replacement before start */
2845                 if (rdev->mddev->pers)
2846                         err = -EBUSY;
2847                 else {
2848                         clear_bit(Replacement, &rdev->flags);
2849                         err = 0;
2850                 }
2851         } else if (cmd_match(buf, "re-add")) {
2852                 if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) {
2853                         /* clear_bit is performed _after_ all the devices
2854                          * have their local Faulty bit cleared. If any writes
2855                          * happen in the meantime in the local node, they
2856                          * will land in the local bitmap, which will be synced
2857                          * by this node eventually
2858                          */
2859                         if (!mddev_is_clustered(rdev->mddev) ||
2860                             (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) {
2861                                 clear_bit(Faulty, &rdev->flags);
2862                                 err = add_bound_rdev(rdev);
2863                         }
2864                 } else
2865                         err = -EBUSY;
2866         } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) {
2867                 set_bit(ExternalBbl, &rdev->flags);
2868                 rdev->badblocks.shift = 0;
2869                 err = 0;
2870         } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) {
2871                 clear_bit(ExternalBbl, &rdev->flags);
2872                 err = 0;
2873         }
2874         if (!err)
2875                 sysfs_notify_dirent_safe(rdev->sysfs_state);
2876         return err ? err : len;
2877 }
2878 static struct rdev_sysfs_entry rdev_state =
2879 __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store);
2880
2881 static ssize_t
2882 errors_show(struct md_rdev *rdev, char *page)
2883 {
2884         return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2885 }
2886
2887 static ssize_t
2888 errors_store(struct md_rdev *rdev, const char *buf, size_t len)
2889 {
2890         unsigned int n;
2891         int rv;
2892
2893         rv = kstrtouint(buf, 10, &n);
2894         if (rv < 0)
2895                 return rv;
2896         atomic_set(&rdev->corrected_errors, n);
2897         return len;
2898 }
2899 static struct rdev_sysfs_entry rdev_errors =
2900 __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
2901
2902 static ssize_t
2903 slot_show(struct md_rdev *rdev, char *page)
2904 {
2905         if (test_bit(Journal, &rdev->flags))
2906                 return sprintf(page, "journal\n");
2907         else if (rdev->raid_disk < 0)
2908                 return sprintf(page, "none\n");
2909         else
2910                 return sprintf(page, "%d\n", rdev->raid_disk);
2911 }
2912
2913 static ssize_t
2914 slot_store(struct md_rdev *rdev, const char *buf, size_t len)
2915 {
2916         int slot;
2917         int err;
2918
2919         if (test_bit(Journal, &rdev->flags))
2920                 return -EBUSY;
2921         if (strncmp(buf, "none", 4)==0)
2922                 slot = -1;
2923         else {
2924                 err = kstrtouint(buf, 10, (unsigned int *)&slot);
2925                 if (err < 0)
2926                         return err;
2927         }
2928         if (rdev->mddev->pers && slot == -1) {
2929                 /* Setting 'slot' on an active array requires also
2930                  * updating the 'rd%d' link, and communicating
2931                  * with the personality with ->hot_*_disk.
2932                  * For now we only support removing
2933                  * failed/spare devices.  This normally happens automatically,
2934                  * but not when the metadata is externally managed.
2935                  */
2936                 if (rdev->raid_disk == -1)
2937                         return -EEXIST;
2938                 /* personality does all needed checks */
2939                 if (rdev->mddev->pers->hot_remove_disk == NULL)
2940                         return -EINVAL;
2941                 clear_bit(Blocked, &rdev->flags);
2942                 remove_and_add_spares(rdev->mddev, rdev);
2943                 if (rdev->raid_disk >= 0)
2944                         return -EBUSY;
2945                 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2946                 md_wakeup_thread(rdev->mddev->thread);
2947         } else if (rdev->mddev->pers) {
2948                 /* Activating a spare .. or possibly reactivating
2949                  * if we ever get bitmaps working here.
2950                  */
2951                 int err;
2952
2953                 if (rdev->raid_disk != -1)
2954                         return -EBUSY;
2955
2956                 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2957                         return -EBUSY;
2958
2959                 if (rdev->mddev->pers->hot_add_disk == NULL)
2960                         return -EINVAL;
2961
2962                 if (slot >= rdev->mddev->raid_disks &&
2963                     slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2964                         return -ENOSPC;
2965
2966                 rdev->raid_disk = slot;
2967                 if (test_bit(In_sync, &rdev->flags))
2968                         rdev->saved_raid_disk = slot;
2969                 else
2970                         rdev->saved_raid_disk = -1;
2971                 clear_bit(In_sync, &rdev->flags);
2972                 clear_bit(Bitmap_sync, &rdev->flags);
2973                 err = rdev->mddev->pers->
2974                         hot_add_disk(rdev->mddev, rdev);
2975                 if (err) {
2976                         rdev->raid_disk = -1;
2977                         return err;
2978                 } else
2979                         sysfs_notify_dirent_safe(rdev->sysfs_state);
2980                 if (sysfs_link_rdev(rdev->mddev, rdev))
2981                         /* failure here is OK */;
2982                 /* don't wakeup anyone, leave that to userspace. */
2983         } else {
2984                 if (slot >= rdev->mddev->raid_disks &&
2985                     slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2986                         return -ENOSPC;
2987                 rdev->raid_disk = slot;
2988                 /* assume it is working */
2989                 clear_bit(Faulty, &rdev->flags);
2990                 clear_bit(WriteMostly, &rdev->flags);
2991                 set_bit(In_sync, &rdev->flags);
2992                 sysfs_notify_dirent_safe(rdev->sysfs_state);
2993         }
2994         return len;
2995 }
2996
2997 static struct rdev_sysfs_entry rdev_slot =
2998 __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
2999
3000 static ssize_t
3001 offset_show(struct md_rdev *rdev, char *page)
3002 {
3003         return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
3004 }
3005
3006 static ssize_t
3007 offset_store(struct md_rdev *rdev, const char *buf, size_t len)
3008 {
3009         unsigned long long offset;
3010         if (kstrtoull(buf, 10, &offset) < 0)
3011                 return -EINVAL;
3012         if (rdev->mddev->pers && rdev->raid_disk >= 0)
3013                 return -EBUSY;
3014         if (rdev->sectors && rdev->mddev->external)
3015                 /* Must set offset before size, so overlap checks
3016                  * can be sane */
3017                 return -EBUSY;
3018         rdev->data_offset = offset;
3019         rdev->new_data_offset = offset;
3020         return len;
3021 }
3022
3023 static struct rdev_sysfs_entry rdev_offset =
3024 __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
3025
3026 static ssize_t new_offset_show(struct md_rdev *rdev, char *page)
3027 {
3028         return sprintf(page, "%llu\n",
3029                        (unsigned long long)rdev->new_data_offset);
3030 }
3031
3032 static ssize_t new_offset_store(struct md_rdev *rdev,
3033                                 const char *buf, size_t len)
3034 {
3035         unsigned long long new_offset;
3036         struct mddev *mddev = rdev->mddev;
3037
3038         if (kstrtoull(buf, 10, &new_offset) < 0)
3039                 return -EINVAL;
3040
3041         if (mddev->sync_thread ||
3042             test_bit(MD_RECOVERY_RUNNING,&mddev->recovery))
3043                 return -EBUSY;
3044         if (new_offset == rdev->data_offset)
3045                 /* reset is always permitted */
3046                 ;
3047         else if (new_offset > rdev->data_offset) {
3048                 /* must not push array size beyond rdev_sectors */
3049                 if (new_offset - rdev->data_offset
3050                     + mddev->dev_sectors > rdev->sectors)
3051                                 return -E2BIG;
3052         }
3053         /* Metadata worries about other space details. */
3054
3055         /* decreasing the offset is inconsistent with a backwards
3056          * reshape.
3057          */
3058         if (new_offset < rdev->data_offset &&
3059             mddev->reshape_backwards)
3060                 return -EINVAL;
3061         /* Increasing offset is inconsistent with forwards
3062          * reshape.  reshape_direction should be set to
3063          * 'backwards' first.
3064          */
3065         if (new_offset > rdev->data_offset &&
3066             !mddev->reshape_backwards)
3067                 return -EINVAL;
3068
3069         if (mddev->pers && mddev->persistent &&
3070             !super_types[mddev->major_version]
3071             .allow_new_offset(rdev, new_offset))
3072                 return -E2BIG;
3073         rdev->new_data_offset = new_offset;
3074         if (new_offset > rdev->data_offset)
3075                 mddev->reshape_backwards = 1;
3076         else if (new_offset < rdev->data_offset)
3077                 mddev->reshape_backwards = 0;
3078
3079         return len;
3080 }
3081 static struct rdev_sysfs_entry rdev_new_offset =
3082 __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store);
3083
3084 static ssize_t
3085 rdev_size_show(struct md_rdev *rdev, char *page)
3086 {
3087         return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
3088 }
3089
3090 static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
3091 {
3092         /* check if two start/length pairs overlap */
3093         if (s1+l1 <= s2)
3094                 return 0;
3095         if (s2+l2 <= s1)
3096                 return 0;
3097         return 1;
3098 }
3099
3100 static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
3101 {
3102         unsigned long long blocks;
3103         sector_t new;
3104
3105         if (kstrtoull(buf, 10, &blocks) < 0)
3106                 return -EINVAL;
3107
3108         if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
3109                 return -EINVAL; /* sector conversion overflow */
3110
3111         new = blocks * 2;
3112         if (new != blocks * 2)
3113                 return -EINVAL; /* unsigned long long to sector_t overflow */
3114
3115         *sectors = new;
3116         return 0;
3117 }
3118
3119 static ssize_t
3120 rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3121 {
3122         struct mddev *my_mddev = rdev->mddev;
3123         sector_t oldsectors = rdev->sectors;
3124         sector_t sectors;
3125
3126         if (test_bit(Journal, &rdev->flags))
3127                 return -EBUSY;
3128         if (strict_blocks_to_sectors(buf, &sectors) < 0)
3129                 return -EINVAL;
3130         if (rdev->data_offset != rdev->new_data_offset)
3131                 return -EINVAL; /* too confusing */
3132         if (my_mddev->pers && rdev->raid_disk >= 0) {
3133                 if (my_mddev->persistent) {
3134                         sectors = super_types[my_mddev->major_version].
3135                                 rdev_size_change(rdev, sectors);
3136                         if (!sectors)
3137                                 return -EBUSY;
3138                 } else if (!sectors)
3139                         sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
3140                                 rdev->data_offset;
3141                 if (!my_mddev->pers->resize)
3142                         /* Cannot change size for RAID0 or Linear etc */
3143                         return -EINVAL;
3144         }
3145         if (sectors < my_mddev->dev_sectors)
3146                 return -EINVAL; /* component must fit device */
3147
3148         rdev->sectors = sectors;
3149         if (sectors > oldsectors && my_mddev->external) {
3150                 /* Need to check that all other rdevs with the same
3151                  * ->bdev do not overlap.  'rcu' is sufficient to walk
3152                  * the rdev lists safely.
3153                  * This check does not provide a hard guarantee, it
3154                  * just helps avoid dangerous mistakes.
3155                  */
3156                 struct mddev *mddev;
3157                 int overlap = 0;
3158                 struct list_head *tmp;
3159
3160                 rcu_read_lock();
3161                 for_each_mddev(mddev, tmp) {
3162                         struct md_rdev *rdev2;
3163
3164                         rdev_for_each(rdev2, mddev)
3165                                 if (rdev->bdev == rdev2->bdev &&
3166                                     rdev != rdev2 &&
3167                                     overlaps(rdev->data_offset, rdev->sectors,
3168                                              rdev2->data_offset,
3169                                              rdev2->sectors)) {
3170                                         overlap = 1;
3171                                         break;
3172                                 }
3173                         if (overlap) {
3174                                 mddev_put(mddev);
3175                                 break;
3176                         }
3177                 }
3178                 rcu_read_unlock();
3179                 if (overlap) {
3180                         /* Someone else could have slipped in a size
3181                          * change here, but doing so is just silly.
3182                          * We put oldsectors back because we *know* it is
3183                          * safe, and trust userspace not to race with
3184                          * itself
3185                          */
3186                         rdev->sectors = oldsectors;
3187                         return -EBUSY;
3188                 }
3189         }
3190         return len;
3191 }
3192
3193 static struct rdev_sysfs_entry rdev_size =
3194 __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
3195
3196 static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
3197 {
3198         unsigned long long recovery_start = rdev->recovery_offset;
3199
3200         if (test_bit(In_sync, &rdev->flags) ||
3201             recovery_start == MaxSector)
3202                 return sprintf(page, "none\n");
3203
3204         return sprintf(page, "%llu\n", recovery_start);
3205 }
3206
3207 static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
3208 {
3209         unsigned long long recovery_start;
3210
3211         if (cmd_match(buf, "none"))
3212                 recovery_start = MaxSector;
3213         else if (kstrtoull(buf, 10, &recovery_start))
3214                 return -EINVAL;
3215
3216         if (rdev->mddev->pers &&
3217             rdev->raid_disk >= 0)
3218                 return -EBUSY;
3219
3220         rdev->recovery_offset = recovery_start;
3221         if (recovery_start == MaxSector)
3222                 set_bit(In_sync, &rdev->flags);
3223         else
3224                 clear_bit(In_sync, &rdev->flags);
3225         return len;
3226 }
3227
3228 static struct rdev_sysfs_entry rdev_recovery_start =
3229 __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
3230
3231 /* sysfs access to bad-blocks list.
3232  * We present two files.
3233  * 'bad-blocks' lists sector numbers and lengths of ranges that
3234  *    are recorded as bad.  The list is truncated to fit within
3235  *    the one-page limit of sysfs.
3236  *    Writing "sector length" to this file adds an acknowledged
3237  *    bad block list.
3238  * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
3239  *    been acknowledged.  Writing to this file adds bad blocks
3240  *    without acknowledging them.  This is largely for testing.
3241  */
3242 static ssize_t bb_show(struct md_rdev *rdev, char *page)
3243 {
3244         return badblocks_show(&rdev->badblocks, page, 0);
3245 }
3246 static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
3247 {
3248         int rv = badblocks_store(&rdev->badblocks, page, len, 0);
3249         /* Maybe that ack was all we needed */
3250         if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
3251                 wake_up(&rdev->blocked_wait);
3252         return rv;
3253 }
3254 static struct rdev_sysfs_entry rdev_bad_blocks =
3255 __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
3256
3257 static ssize_t ubb_show(struct md_rdev *rdev, char *page)
3258 {
3259         return badblocks_show(&rdev->badblocks, page, 1);
3260 }
3261 static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
3262 {
3263         return badblocks_store(&rdev->badblocks, page, len, 1);
3264 }
3265 static struct rdev_sysfs_entry rdev_unack_bad_blocks =
3266 __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
3267
3268 static ssize_t
3269 ppl_sector_show(struct md_rdev *rdev, char *page)
3270 {
3271         return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector);
3272 }
3273
3274 static ssize_t
3275 ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len)
3276 {
3277         unsigned long long sector;
3278
3279         if (kstrtoull(buf, 10, &sector) < 0)
3280                 return -EINVAL;
3281         if (sector != (sector_t)sector)
3282                 return -EINVAL;
3283
3284         if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3285             rdev->raid_disk >= 0)
3286                 return -EBUSY;
3287
3288         if (rdev->mddev->persistent) {
3289                 if (rdev->mddev->major_version == 0)
3290                         return -EINVAL;
3291                 if ((sector > rdev->sb_start &&
3292                      sector - rdev->sb_start > S16_MAX) ||
3293                     (sector < rdev->sb_start &&
3294                      rdev->sb_start - sector > -S16_MIN))
3295                         return -EINVAL;
3296                 rdev->ppl.offset = sector - rdev->sb_start;
3297         } else if (!rdev->mddev->external) {
3298                 return -EBUSY;
3299         }
3300         rdev->ppl.sector = sector;
3301         return len;
3302 }
3303
3304 static struct rdev_sysfs_entry rdev_ppl_sector =
3305 __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store);
3306
3307 static ssize_t
3308 ppl_size_show(struct md_rdev *rdev, char *page)
3309 {
3310         return sprintf(page, "%u\n", rdev->ppl.size);
3311 }
3312
3313 static ssize_t
3314 ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len)
3315 {
3316         unsigned int size;
3317
3318         if (kstrtouint(buf, 10, &size) < 0)
3319                 return -EINVAL;
3320
3321         if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) &&
3322             rdev->raid_disk >= 0)
3323                 return -EBUSY;
3324
3325         if (rdev->mddev->persistent) {
3326                 if (rdev->mddev->major_version == 0)
3327                         return -EINVAL;
3328                 if (size > U16_MAX)
3329                         return -EINVAL;
3330         } else if (!rdev->mddev->external) {
3331                 return -EBUSY;
3332         }
3333         rdev->ppl.size = size;
3334         return len;
3335 }
3336
3337 static struct rdev_sysfs_entry rdev_ppl_size =
3338 __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store);
3339
3340 static struct attribute *rdev_default_attrs[] = {
3341         &rdev_state.attr,
3342         &rdev_errors.attr,
3343         &rdev_slot.attr,
3344         &rdev_offset.attr,
3345         &rdev_new_offset.attr,
3346         &rdev_size.attr,
3347         &rdev_recovery_start.attr,
3348         &rdev_bad_blocks.attr,
3349         &rdev_unack_bad_blocks.attr,
3350         &rdev_ppl_sector.attr,
3351         &rdev_ppl_size.attr,
3352         NULL,
3353 };
3354 static ssize_t
3355 rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3356 {
3357         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3358         struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3359
3360         if (!entry->show)
3361                 return -EIO;
3362         if (!rdev->mddev)
3363                 return -EBUSY;
3364         return entry->show(rdev, page);
3365 }
3366
3367 static ssize_t
3368 rdev_attr_store(struct kobject *kobj, struct attribute *attr,
3369               const char *page, size_t length)
3370 {
3371         struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
3372         struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
3373         ssize_t rv;
3374         struct mddev *mddev = rdev->mddev;
3375
3376         if (!entry->store)
3377                 return -EIO;
3378         if (!capable(CAP_SYS_ADMIN))
3379                 return -EACCES;
3380         rv = mddev ? mddev_lock(mddev): -EBUSY;
3381         if (!rv) {
3382                 if (rdev->mddev == NULL)
3383                         rv = -EBUSY;
3384                 else
3385                         rv = entry->store(rdev, page, length);
3386                 mddev_unlock(mddev);
3387         }
3388         return rv;
3389 }
3390
3391 static void rdev_free(struct kobject *ko)
3392 {
3393         struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
3394         kfree(rdev);
3395 }
3396 static const struct sysfs_ops rdev_sysfs_ops = {
3397         .show           = rdev_attr_show,
3398         .store          = rdev_attr_store,
3399 };
3400 static struct kobj_type rdev_ktype = {
3401         .release        = rdev_free,
3402         .sysfs_ops      = &rdev_sysfs_ops,
3403         .default_attrs  = rdev_default_attrs,
3404 };
3405
3406 int md_rdev_init(struct md_rdev *rdev)
3407 {
3408         rdev->desc_nr = -1;
3409         rdev->saved_raid_disk = -1;
3410         rdev->raid_disk = -1;
3411         rdev->flags = 0;
3412         rdev->data_offset = 0;
3413         rdev->new_data_offset = 0;
3414         rdev->sb_events = 0;
3415         rdev->last_read_error = 0;
3416         rdev->sb_loaded = 0;
3417         rdev->bb_page = NULL;
3418         atomic_set(&rdev->nr_pending, 0);
3419         atomic_set(&rdev->read_errors, 0);
3420         atomic_set(&rdev->corrected_errors, 0);
3421
3422         INIT_LIST_HEAD(&rdev->same_set);
3423         init_waitqueue_head(&rdev->blocked_wait);
3424
3425         /* Add space to store bad block list.
3426          * This reserves the space even on arrays where it cannot
3427          * be used - I wonder if that matters
3428          */
3429         return badblocks_init(&rdev->badblocks, 0);
3430 }
3431 EXPORT_SYMBOL_GPL(md_rdev_init);
3432 /*
3433  * Import a device. If 'super_format' >= 0, then sanity check the superblock
3434  *
3435  * mark the device faulty if:
3436  *
3437  *   - the device is nonexistent (zero size)
3438  *   - the device has no valid superblock
3439  *
3440  * a faulty rdev _never_ has rdev->sb set.
3441  */
3442 static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
3443 {
3444         char b[BDEVNAME_SIZE];
3445         int err;
3446         struct md_rdev *rdev;
3447         sector_t size;
3448
3449         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
3450         if (!rdev)
3451                 return ERR_PTR(-ENOMEM);
3452
3453         err = md_rdev_init(rdev);
3454         if (err)
3455                 goto abort_free;
3456         err = alloc_disk_sb(rdev);
3457         if (err)
3458                 goto abort_free;
3459
3460         err = lock_rdev(rdev, newdev, super_format == -2);
3461         if (err)
3462                 goto abort_free;
3463
3464         kobject_init(&rdev->kobj, &rdev_ktype);
3465
3466         size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
3467         if (!size) {
3468                 pr_warn("md: %s has zero or unknown size, marking faulty!\n",
3469                         bdevname(rdev->bdev,b));
3470                 err = -EINVAL;
3471                 goto abort_free;
3472         }
3473
3474         if (super_format >= 0) {
3475                 err = super_types[super_format].
3476                         load_super(rdev, NULL, super_minor);
3477                 if (err == -EINVAL) {
3478                         pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n",
3479                                 bdevname(rdev->bdev,b),
3480                                 super_format, super_minor);
3481                         goto abort_free;
3482                 }
3483                 if (err < 0) {
3484                         pr_warn("md: could not read %s's sb, not importing!\n",
3485                                 bdevname(rdev->bdev,b));
3486                         goto abort_free;
3487                 }
3488         }
3489
3490         return rdev;
3491
3492 abort_free:
3493         if (rdev->bdev)
3494                 unlock_rdev(rdev);
3495         md_rdev_clear(rdev);
3496         kfree(rdev);
3497         return ERR_PTR(err);
3498 }
3499
3500 /*
3501  * Check a full RAID array for plausibility
3502  */
3503
3504 static void analyze_sbs(struct mddev *mddev)
3505 {
3506         int i;
3507         struct md_rdev *rdev, *freshest, *tmp;
3508         char b[BDEVNAME_SIZE];
3509
3510         freshest = NULL;
3511         rdev_for_each_safe(rdev, tmp, mddev)
3512                 switch (super_types[mddev->major_version].
3513                         load_super(rdev, freshest, mddev->minor_version)) {
3514                 case 1:
3515                         freshest = rdev;
3516                         break;
3517                 case 0:
3518                         break;
3519                 default:
3520                         pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n",
3521                                 bdevname(rdev->bdev,b));
3522                         md_kick_rdev_from_array(rdev);
3523                 }
3524
3525         super_types[mddev->major_version].
3526                 validate_super(mddev, freshest);
3527
3528         i = 0;
3529         rdev_for_each_safe(rdev, tmp, mddev) {
3530                 if (mddev->max_disks &&
3531                     (rdev->desc_nr >= mddev->max_disks ||
3532                      i > mddev->max_disks)) {
3533                         pr_warn("md: %s: %s: only %d devices permitted\n",
3534                                 mdname(mddev), bdevname(rdev->bdev, b),
3535                                 mddev->max_disks);
3536                         md_kick_rdev_from_array(rdev);
3537                         continue;
3538                 }
3539                 if (rdev != freshest) {
3540                         if (super_types[mddev->major_version].
3541                             validate_super(mddev, rdev)) {
3542                                 pr_warn("md: kicking non-fresh %s from array!\n",
3543                                         bdevname(rdev->bdev,b));
3544                                 md_kick_rdev_from_array(rdev);
3545                                 continue;
3546                         }
3547                 }
3548                 if (mddev->level == LEVEL_MULTIPATH) {
3549                         rdev->desc_nr = i++;
3550                         rdev->raid_disk = rdev->desc_nr;
3551                         set_bit(In_sync, &rdev->flags);
3552                 } else if (rdev->raid_disk >=
3553                             (mddev->raid_disks - min(0, mddev->delta_disks)) &&
3554                            !test_bit(Journal, &rdev->flags)) {
3555                         rdev->raid_disk = -1;
3556                         clear_bit(In_sync, &rdev->flags);
3557                 }
3558         }
3559 }
3560
3561 /* Read a fixed-point number.
3562  * Numbers in sysfs attributes should be in "standard" units where
3563  * possible, so time should be in seconds.
3564  * However we internally use a a much smaller unit such as
3565  * milliseconds or jiffies.
3566  * This function takes a decimal number with a possible fractional
3567  * component, and produces an integer which is the result of
3568  * multiplying that number by 10^'scale'.
3569  * all without any floating-point arithmetic.
3570  */
3571 int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3572 {
3573         unsigned long result = 0;
3574         long decimals = -1;
3575         while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3576                 if (*cp == '.')
3577                         decimals = 0;
3578                 else if (decimals < scale) {
3579                         unsigned int value;
3580                         value = *cp - '0';
3581                         result = result * 10 + value;
3582                         if (decimals >= 0)
3583                                 decimals++;
3584                 }
3585                 cp++;
3586         }
3587         if (*cp == '\n')
3588                 cp++;
3589         if (*cp)
3590                 return -EINVAL;
3591         if (decimals < 0)
3592                 decimals = 0;
3593         while (decimals < scale) {
3594                 result *= 10;
3595                 decimals ++;
3596         }
3597         *res = result;
3598         return 0;
3599 }
3600
3601 static ssize_t
3602 safe_delay_show(struct mddev *mddev, char *page)
3603 {
3604         int msec = (mddev->safemode_delay*1000)/HZ;
3605         return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3606 }
3607 static ssize_t
3608 safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
3609 {
3610         unsigned long msec;
3611
3612         if (mddev_is_clustered(mddev)) {
3613                 pr_warn("md: Safemode is disabled for clustered mode\n");
3614                 return -EINVAL;
3615         }
3616
3617         if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
3618                 return -EINVAL;
3619         if (msec == 0)
3620                 mddev->safemode_delay = 0;
3621         else {
3622                 unsigned long old_delay = mddev->safemode_delay;
3623                 unsigned long new_delay = (msec*HZ)/1000;
3624
3625                 if (new_delay == 0)
3626                         new_delay = 1;
3627                 mddev->safemode_delay = new_delay;
3628                 if (new_delay < old_delay || old_delay == 0)
3629                         mod_timer(&mddev->safemode_timer, jiffies+1);
3630         }
3631         return len;
3632 }
3633 static struct md_sysfs_entry md_safe_delay =
3634 __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
3635
3636 static ssize_t
3637 level_show(struct mddev *mddev, char *page)
3638 {
3639         struct md_personality *p;
3640         int ret;
3641         spin_lock(&mddev->lock);
3642         p = mddev->pers;
3643         if (p)
3644                 ret = sprintf(page, "%s\n", p->name);
3645         else if (mddev->clevel[0])
3646                 ret = sprintf(page, "%s\n", mddev->clevel);
3647         else if (mddev->level != LEVEL_NONE)
3648                 ret = sprintf(page, "%d\n", mddev->level);
3649         else
3650                 ret = 0;
3651         spin_unlock(&mddev->lock);
3652         return ret;
3653 }
3654
3655 static ssize_t
3656 level_store(struct mddev *mddev, const char *buf, size_t len)
3657 {
3658         char clevel[16];
3659         ssize_t rv;
3660         size_t slen = len;
3661         struct md_personality *pers, *oldpers;
3662         long level;
3663         void *priv, *oldpriv;
3664         struct md_rdev *rdev;
3665
3666         if (slen == 0 || slen >= sizeof(clevel))
3667                 return -EINVAL;
3668
3669         rv = mddev_lock(mddev);
3670         if (rv)
3671                 return rv;
3672
3673         if (mddev->pers == NULL) {
3674                 strncpy(mddev->clevel, buf, slen);
3675                 if (mddev->clevel[slen-1] == '\n')
3676                         slen--;
3677                 mddev->clevel[slen] = 0;
3678                 mddev->level = LEVEL_NONE;
3679                 rv = len;
3680                 goto out_unlock;
3681         }
3682         rv = -EROFS;
3683         if (mddev->ro)
3684                 goto out_unlock;
3685
3686         /* request to change the personality.  Need to ensure:
3687          *  - array is not engaged in resync/recovery/reshape
3688          *  - old personality can be suspended
3689          *  - new personality will access other array.
3690          */
3691
3692         rv = -EBUSY;
3693         if (mddev->sync_thread ||
3694             test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3695             mddev->reshape_position != MaxSector ||
3696             mddev->sysfs_active)
3697                 goto out_unlock;
3698
3699         rv = -EINVAL;
3700         if (!mddev->pers->quiesce) {
3701                 pr_warn("md: %s: %s does not support online personality change\n",
3702                         mdname(mddev), mddev->pers->name);
3703                 goto out_unlock;
3704         }
3705
3706         /* Now find the new personality */
3707         strncpy(clevel, buf, slen);
3708         if (clevel[slen-1] == '\n')
3709                 slen--;
3710         clevel[slen] = 0;
3711         if (kstrtol(clevel, 10, &level))
3712                 level = LEVEL_NONE;
3713